@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorPackagesPackage
4 extends PhabricatorPackagesDAO
5 implements
6 PhabricatorPolicyInterface,
7 PhabricatorApplicationTransactionInterface,
8 PhabricatorDestructibleInterface,
9 PhabricatorSubscribableInterface,
10 PhabricatorProjectInterface,
11 PhabricatorConduitResultInterface,
12 PhabricatorNgramsInterface {
13
14 protected $name;
15 protected $publisherPHID;
16 protected $packageKey;
17 protected $viewPolicy;
18 protected $editPolicy;
19
20 private $publisher = self::ATTACHABLE;
21
22 public static function initializeNewPackage(PhabricatorUser $actor) {
23 $packages_application = id(new PhabricatorApplicationQuery())
24 ->setViewer($actor)
25 ->withClasses(array(PhabricatorPackagesApplication::class))
26 ->executeOne();
27
28 $view_policy = $packages_application->getPolicy(
29 PhabricatorPackagesPackageDefaultViewCapability::CAPABILITY);
30
31 $edit_policy = $packages_application->getPolicy(
32 PhabricatorPackagesPackageDefaultEditCapability::CAPABILITY);
33
34 return id(new self())
35 ->setViewPolicy($view_policy)
36 ->setEditPolicy($edit_policy);
37 }
38
39 protected function getConfiguration() {
40 return array(
41 self::CONFIG_AUX_PHID => true,
42 self::CONFIG_COLUMN_SCHEMA => array(
43 'name' => 'sort64',
44 'packageKey' => 'sort64',
45 ),
46 self::CONFIG_KEY_SCHEMA => array(
47 'key_package' => array(
48 'columns' => array('publisherPHID', 'packageKey'),
49 'unique' => true,
50 ),
51 ),
52 ) + parent::getConfiguration();
53 }
54
55 public function generatePHID() {
56 return PhabricatorPHID::generateNewPHID(
57 PhabricatorPackagesPackagePHIDType::TYPECONST);
58 }
59
60 public function getURI() {
61 $full_key = $this->getFullKey();
62 return "/package/{$full_key}/";
63 }
64
65 public function getFullKey() {
66 $publisher = $this->getPublisher();
67 $publisher_key = $publisher->getPublisherKey();
68 $package_key = $this->getPackageKey();
69 return "{$publisher_key}/{$package_key}";
70 }
71
72 public function attachPublisher(PhabricatorPackagesPublisher $publisher) {
73 $this->publisher = $publisher;
74 return $this;
75 }
76
77 public function getPublisher() {
78 return $this->assertAttached($this->publisher);
79 }
80
81 public static function assertValidPackageName($value) {
82 $length = phutil_utf8_strlen($value);
83 if (!$length) {
84 throw new Exception(
85 pht(
86 'Package name "%s" is not valid: package names are required.',
87 $value));
88 }
89
90 $max_length = 64;
91 if ($length > $max_length) {
92 throw new Exception(
93 pht(
94 'Package name "%s" is not valid: package names must not be '.
95 'more than %s characters long.',
96 $value,
97 new PhutilNumber($max_length)));
98 }
99 }
100
101 public static function assertValidPackageKey($value) {
102 $length = phutil_utf8_strlen($value);
103 if (!$length) {
104 throw new Exception(
105 pht(
106 'Package key "%s" is not valid: package keys are required.',
107 $value));
108 }
109
110 $max_length = 64;
111 if ($length > $max_length) {
112 throw new Exception(
113 pht(
114 'Package key "%s" is not valid: package keys must not be '.
115 'more than %s characters long.',
116 $value,
117 new PhutilNumber($max_length)));
118 }
119
120 if (!preg_match('/^[a-z]+\z/', $value)) {
121 throw new Exception(
122 pht(
123 'Package key "%s" is not valid: package keys may only contain '.
124 'lowercase latin letters.',
125 $value));
126 }
127 }
128
129
130/* -( PhabricatorSubscribableInterface )----------------------------------- */
131
132
133 public function isAutomaticallySubscribed($phid) {
134 return false;
135 }
136
137
138/* -( Policy Interface )--------------------------------------------------- */
139
140
141 public function getCapabilities() {
142 return array(
143 PhabricatorPolicyCapability::CAN_VIEW,
144 PhabricatorPolicyCapability::CAN_EDIT,
145 );
146 }
147
148 public function getPolicy($capability) {
149 switch ($capability) {
150 case PhabricatorPolicyCapability::CAN_VIEW:
151 return $this->getViewPolicy();
152 case PhabricatorPolicyCapability::CAN_EDIT:
153 return $this->getEditPolicy();
154 }
155 }
156
157 public function hasAutomaticCapability($capability, PhabricatorUser $user) {
158 return false;
159 }
160
161
162/* -( PhabricatorDestructibleInterface )----------------------------------- */
163
164
165 public function destroyObjectPermanently(
166 PhabricatorDestructionEngine $engine) {
167 $viewer = $engine->getViewer();
168
169 $this->openTransaction();
170
171 $versions = id(new PhabricatorPackagesVersionQuery())
172 ->setViewer($viewer)
173 ->withPackagePHIDs(array($this->getPHID()))
174 ->execute();
175 foreach ($versions as $version) {
176 $engine->destroyObject($version);
177 }
178
179 $this->delete();
180
181 $this->saveTransaction();
182 }
183
184
185/* -( PhabricatorApplicationTransactionInterface )------------------------- */
186
187
188 public function getApplicationTransactionEditor() {
189 return new PhabricatorPackagesPackageEditor();
190 }
191
192 public function getApplicationTransactionTemplate() {
193 return new PhabricatorPackagesPackageTransaction();
194 }
195
196
197/* -( PhabricatorNgramsInterface )----------------------------------------- */
198
199
200 public function newNgrams() {
201 return array(
202 id(new PhabricatorPackagesPackageNameNgrams())
203 ->setValue($this->getName()),
204 );
205 }
206
207
208/* -( PhabricatorConduitResultInterface )---------------------------------- */
209
210
211 public function getFieldSpecificationsForConduit() {
212 return array(
213 id(new PhabricatorConduitSearchFieldSpecification())
214 ->setKey('name')
215 ->setType('string')
216 ->setDescription(pht('The name of the package.')),
217 id(new PhabricatorConduitSearchFieldSpecification())
218 ->setKey('packageKey')
219 ->setType('string')
220 ->setDescription(pht('The unique key of the package.')),
221 );
222 }
223
224 public function getFieldValuesForConduit() {
225 $publisher = $this->getPublisher();
226
227 $publisher_map = array(
228 'id' => (int)$publisher->getID(),
229 'phid' => $publisher->getPHID(),
230 'name' => $publisher->getName(),
231 'publisherKey' => $publisher->getPublisherKey(),
232 );
233
234 return array(
235 'name' => $this->getName(),
236 'packageKey' => $this->getPackageKey(),
237 'fullKey' => $this->getFullKey(),
238 'publisher' => $publisher_map,
239 );
240 }
241
242 public function getConduitSearchAttachments() {
243 return array();
244 }
245
246
247}