@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 PholioMock extends PholioDAO
4 implements
5 PhabricatorPolicyInterface,
6 PhabricatorSubscribableInterface,
7 PhabricatorTokenReceiverInterface,
8 PhabricatorFlaggableInterface,
9 PhabricatorApplicationTransactionInterface,
10 PhabricatorTimelineInterface,
11 PhabricatorProjectInterface,
12 PhabricatorDestructibleInterface,
13 PhabricatorSpacesInterface,
14 PhabricatorMentionableInterface,
15 PhabricatorFulltextInterface,
16 PhabricatorFerretInterface {
17
18 const STATUS_OPEN = 'open';
19 const STATUS_CLOSED = 'closed';
20
21 protected $authorPHID;
22 protected $viewPolicy;
23 protected $editPolicy;
24
25 protected $name;
26 protected $description;
27 protected $coverPHID;
28 protected $status;
29 protected $spacePHID;
30
31 private $images = self::ATTACHABLE;
32 private $coverFile = self::ATTACHABLE;
33 private $tokenCount = self::ATTACHABLE;
34
35 public static function initializeNewMock(PhabricatorUser $actor) {
36 $app = id(new PhabricatorApplicationQuery())
37 ->setViewer($actor)
38 ->withClasses(array(PhabricatorPholioApplication::class))
39 ->executeOne();
40
41 $view_policy = $app->getPolicy(PholioDefaultViewCapability::CAPABILITY);
42 $edit_policy = $app->getPolicy(PholioDefaultEditCapability::CAPABILITY);
43
44 return id(new PholioMock())
45 ->setAuthorPHID($actor->getPHID())
46 ->attachImages(array())
47 ->setStatus(self::STATUS_OPEN)
48 ->setViewPolicy($view_policy)
49 ->setEditPolicy($edit_policy)
50 ->setSpacePHID($actor->getDefaultSpacePHID());
51 }
52
53 public function getMonogram() {
54 return 'M'.$this->getID();
55 }
56
57 public function getURI() {
58 return '/'.$this->getMonogram();
59 }
60
61 protected function getConfiguration() {
62 return array(
63 self::CONFIG_AUX_PHID => true,
64 self::CONFIG_COLUMN_SCHEMA => array(
65 'name' => 'text128',
66 'description' => 'text',
67 'status' => 'text12',
68 ),
69 self::CONFIG_KEY_SCHEMA => array(
70 'authorPHID' => array(
71 'columns' => array('authorPHID'),
72 ),
73 ),
74 ) + parent::getConfiguration();
75 }
76
77 public function getPHIDType() {
78 return PholioMockPHIDType::TYPECONST;
79 }
80
81 /**
82 * @param array<PholioImage> $images
83 */
84 public function attachImages(array $images) {
85 assert_instances_of($images, PholioImage::class);
86 $images = mpull($images, null, 'getPHID');
87 $images = msort($images, 'getSequence');
88 $this->images = $images;
89 return $this;
90 }
91
92 public function getImages() {
93 return $this->assertAttached($this->images);
94 }
95
96 public function getActiveImages() {
97 $images = $this->getImages();
98
99 foreach ($images as $phid => $image) {
100 if ($image->getIsObsolete()) {
101 unset($images[$phid]);
102 }
103 }
104
105 return $images;
106 }
107
108 public function attachCoverFile(PhabricatorFile $file) {
109 $this->coverFile = $file;
110 return $this;
111 }
112
113 public function getCoverFile() {
114 $this->assertAttached($this->coverFile);
115 return $this->coverFile;
116 }
117
118 public function getTokenCount() {
119 $this->assertAttached($this->tokenCount);
120 return $this->tokenCount;
121 }
122
123 public function attachTokenCount($count) {
124 $this->tokenCount = $count;
125 return $this;
126 }
127
128 public function getImageHistorySet($image_id) {
129 $images = $this->getImages();
130 $images = mpull($images, null, 'getID');
131 $selected_image = $images[$image_id];
132
133 $replace_map = mpull($images, null, 'getReplacesImagePHID');
134 $phid_map = mpull($images, null, 'getPHID');
135
136 // find the earliest image
137 $image = $selected_image;
138 while (isset($phid_map[$image->getReplacesImagePHID()])) {
139 $image = $phid_map[$image->getReplacesImagePHID()];
140 }
141
142 // now build history moving forward
143 $history = array($image->getID() => $image);
144 while (isset($replace_map[$image->getPHID()])) {
145 $image = $replace_map[$image->getPHID()];
146 $history[$image->getID()] = $image;
147 }
148
149 return $history;
150 }
151
152 public function getStatuses() {
153 $options = array();
154 $options[self::STATUS_OPEN] = pht('Open');
155 $options[self::STATUS_CLOSED] = pht('Closed');
156 return $options;
157 }
158
159 public function isClosed() {
160 return ($this->getStatus() == 'closed');
161 }
162
163
164/* -( PhabricatorSubscribableInterface Implementation )-------------------- */
165
166
167 public function isAutomaticallySubscribed($phid) {
168 return ($this->authorPHID == $phid);
169 }
170
171
172/* -( PhabricatorPolicyInterface Implementation )-------------------------- */
173
174
175 public function getCapabilities() {
176 return array(
177 PhabricatorPolicyCapability::CAN_VIEW,
178 PhabricatorPolicyCapability::CAN_EDIT,
179 );
180 }
181
182 public function getPolicy($capability) {
183 switch ($capability) {
184 case PhabricatorPolicyCapability::CAN_VIEW:
185 return $this->getViewPolicy();
186 case PhabricatorPolicyCapability::CAN_EDIT:
187 return $this->getEditPolicy();
188 }
189 }
190
191 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
192 return ($viewer->getPHID() == $this->getAuthorPHID());
193 }
194
195 public function describeAutomaticCapability($capability) {
196 return pht('The author of a mock can always view and edit it.');
197 }
198
199
200/* -( PhabricatorApplicationTransactionInterface )------------------------- */
201
202
203 public function getApplicationTransactionEditor() {
204 return new PholioMockEditor();
205 }
206
207 public function getApplicationTransactionTemplate() {
208 return new PholioTransaction();
209 }
210
211
212/* -( PhabricatorTokenReceiverInterface )---------------------------------- */
213
214
215 public function getUsersToNotifyOfTokenGiven() {
216 return array(
217 $this->getAuthorPHID(),
218 );
219 }
220
221
222/* -( PhabricatorDestructibleInterface )----------------------------------- */
223
224
225 public function destroyObjectPermanently(
226 PhabricatorDestructionEngine $engine) {
227
228 $this->openTransaction();
229 $images = id(new PholioImageQuery())
230 ->setViewer($engine->getViewer())
231 ->withMockPHIDs(array($this->getPHID()))
232 ->execute();
233 foreach ($images as $image) {
234 $image->delete();
235 }
236
237 $this->delete();
238 $this->saveTransaction();
239 }
240
241
242/* -( PhabricatorSpacesInterface )----------------------------------------- */
243
244
245 public function getSpacePHID() {
246 return $this->spacePHID;
247 }
248
249
250/* -( PhabricatorFulltextInterface )--------------------------------------- */
251
252
253 public function newFulltextEngine() {
254 return new PholioMockFulltextEngine();
255 }
256
257
258/* -( PhabricatorFerretInterface )----------------------------------------- */
259
260
261 public function newFerretEngine() {
262 return new PholioMockFerretEngine();
263 }
264
265
266/* -( PhabricatorTimelineInterace )---------------------------------------- */
267
268
269 public function newTimelineEngine() {
270 return new PholioMockTimelineEngine();
271 }
272
273
274}