@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 PhabricatorPaste extends PhabricatorPasteDAO
4 implements
5 PhabricatorSubscribableInterface,
6 PhabricatorTokenReceiverInterface,
7 PhabricatorFlaggableInterface,
8 PhabricatorMentionableInterface,
9 PhabricatorPolicyInterface,
10 PhabricatorProjectInterface,
11 PhabricatorDestructibleInterface,
12 PhabricatorApplicationTransactionInterface,
13 PhabricatorSpacesInterface,
14 PhabricatorConduitResultInterface,
15 PhabricatorFerretInterface,
16 PhabricatorFulltextInterface {
17
18 protected $title;
19 protected $authorPHID;
20 protected $filePHID;
21 protected $language;
22 protected $parentPHID;
23 protected $viewPolicy;
24 protected $editPolicy;
25 protected $status;
26 protected $spacePHID;
27
28 const STATUS_ACTIVE = 'active';
29 const STATUS_ARCHIVED = 'archived';
30
31 private $content = self::ATTACHABLE;
32 private $rawContent = self::ATTACHABLE;
33 private $snippet = self::ATTACHABLE;
34
35 public static function initializeNewPaste(PhabricatorUser $actor) {
36 $app = id(new PhabricatorApplicationQuery())
37 ->setViewer($actor)
38 ->withClasses(array(PhabricatorPasteApplication::class))
39 ->executeOne();
40
41 $view_policy = $app->getPolicy(PasteDefaultViewCapability::CAPABILITY);
42 $edit_policy = $app->getPolicy(PasteDefaultEditCapability::CAPABILITY);
43
44 return id(new PhabricatorPaste())
45 ->setTitle('')
46 ->setStatus(self::STATUS_ACTIVE)
47 ->setAuthorPHID($actor->getPHID())
48 ->setViewPolicy($view_policy)
49 ->setEditPolicy($edit_policy)
50 ->setSpacePHID($actor->getDefaultSpacePHID())
51 ->attachRawContent(null);
52 }
53
54 public static function getStatusNameMap() {
55 return array(
56 self::STATUS_ACTIVE => pht('Active'),
57 self::STATUS_ARCHIVED => pht('Archived'),
58 );
59 }
60
61 public function getURI() {
62 return '/'.$this->getMonogram();
63 }
64
65 public function getMonogram() {
66 return 'P'.$this->getID();
67 }
68
69 protected function getConfiguration() {
70 return array(
71 self::CONFIG_AUX_PHID => true,
72 self::CONFIG_COLUMN_SCHEMA => array(
73 'status' => 'text32',
74 'title' => 'text255',
75 'language' => 'text64?',
76 'parentPHID' => 'phid?',
77
78 // T6203/NULLABILITY
79 // Pastes should always have a view policy.
80 'viewPolicy' => 'policy?',
81 ),
82 self::CONFIG_KEY_SCHEMA => array(
83 'parentPHID' => array(
84 'columns' => array('parentPHID'),
85 ),
86 'authorPHID' => array(
87 'columns' => array('authorPHID'),
88 ),
89 'key_dateCreated' => array(
90 'columns' => array('dateCreated'),
91 ),
92 'key_language' => array(
93 'columns' => array('language'),
94 ),
95 ),
96 ) + parent::getConfiguration();
97 }
98
99 public function generatePHID() {
100 return PhabricatorPHID::generateNewPHID(
101 PhabricatorPastePastePHIDType::TYPECONST);
102 }
103
104 public function isArchived() {
105 return ($this->getStatus() == self::STATUS_ARCHIVED);
106 }
107
108 public function getFullName() {
109 $title = $this->getTitle();
110 if (!$title) {
111 $title = pht('(An Untitled Masterwork)');
112 }
113 return 'P'.$this->getID().' '.$title;
114 }
115
116 public function getContent() {
117 return $this->assertAttached($this->content);
118 }
119
120 public function attachContent($content) {
121 $this->content = $content;
122 return $this;
123 }
124
125 public function getRawContent() {
126 return $this->assertAttached($this->rawContent);
127 }
128
129 public function attachRawContent($raw_content) {
130 $this->rawContent = $raw_content;
131 return $this;
132 }
133
134 public function getSnippet() {
135 return $this->assertAttached($this->snippet);
136 }
137
138 public function attachSnippet(PhabricatorPasteSnippet $snippet) {
139 $this->snippet = $snippet;
140 return $this;
141 }
142
143/* -( PhabricatorSubscribableInterface )----------------------------------- */
144
145
146 public function isAutomaticallySubscribed($phid) {
147 return ($this->authorPHID == $phid);
148 }
149
150
151/* -( PhabricatorTokenReceiverInterface )---------------------------------- */
152
153 public function getUsersToNotifyOfTokenGiven() {
154 return array(
155 $this->getAuthorPHID(),
156 );
157 }
158
159
160/* -( PhabricatorPolicyInterface )----------------------------------------- */
161
162
163 public function getCapabilities() {
164 return array(
165 PhabricatorPolicyCapability::CAN_VIEW,
166 PhabricatorPolicyCapability::CAN_EDIT,
167 );
168 }
169
170 public function getPolicy($capability) {
171 if ($capability == PhabricatorPolicyCapability::CAN_VIEW) {
172 return $this->viewPolicy;
173 } else if ($capability == PhabricatorPolicyCapability::CAN_EDIT) {
174 return $this->editPolicy;
175 }
176 return PhabricatorPolicies::POLICY_NOONE;
177 }
178
179 public function hasAutomaticCapability($capability, PhabricatorUser $user) {
180 return ($user->getPHID() == $this->getAuthorPHID());
181 }
182
183 public function describeAutomaticCapability($capability) {
184 return pht('The author of a paste can always view and edit it.');
185 }
186
187
188/* -( PhabricatorDestructibleInterface )----------------------------------- */
189
190
191 public function destroyObjectPermanently(
192 PhabricatorDestructionEngine $engine) {
193
194 if ($this->filePHID) {
195 $file = id(new PhabricatorFileQuery())
196 ->setViewer($engine->getViewer())
197 ->withPHIDs(array($this->filePHID))
198 ->executeOne();
199 if ($file) {
200 $engine->destroyObject($file);
201 }
202 }
203
204 $this->delete();
205 }
206
207
208/* -( PhabricatorApplicationTransactionInterface )------------------------- */
209
210
211 public function getApplicationTransactionEditor() {
212 return new PhabricatorPasteEditor();
213 }
214
215 public function getApplicationTransactionTemplate() {
216 return new PhabricatorPasteTransaction();
217 }
218
219
220/* -( PhabricatorSpacesInterface )----------------------------------------- */
221
222
223 public function getSpacePHID() {
224 return $this->spacePHID;
225 }
226
227
228/* -( PhabricatorConduitResultInterface )---------------------------------- */
229
230
231 public function getFieldSpecificationsForConduit() {
232 return array(
233 id(new PhabricatorConduitSearchFieldSpecification())
234 ->setKey('title')
235 ->setType('string')
236 ->setDescription(pht('The title of the paste.')),
237 id(new PhabricatorConduitSearchFieldSpecification())
238 ->setKey('uri')
239 ->setType('uri')
240 ->setDescription(pht('View URI for the paste.')),
241 id(new PhabricatorConduitSearchFieldSpecification())
242 ->setKey('authorPHID')
243 ->setType('phid')
244 ->setDescription(pht('User PHID of the author.')),
245 id(new PhabricatorConduitSearchFieldSpecification())
246 ->setKey('language')
247 ->setType('string?')
248 ->setDescription(pht('Language to use for syntax highlighting.')),
249 id(new PhabricatorConduitSearchFieldSpecification())
250 ->setKey('status')
251 ->setType('string')
252 ->setDescription(pht('Active or archived status of the paste.')),
253 );
254 }
255
256 public function getFieldValuesForConduit() {
257 return array(
258 'title' => $this->getTitle(),
259 'uri' => PhabricatorEnv::getURI($this->getURI()),
260 'authorPHID' => $this->getAuthorPHID(),
261 'language' => nonempty($this->getLanguage(), null),
262 'status' => $this->getStatus(),
263 );
264 }
265
266 public function getConduitSearchAttachments() {
267 return array(
268 id(new PhabricatorPasteContentSearchEngineAttachment())
269 ->setAttachmentKey('content'),
270 );
271 }
272
273
274/* -( PhabricatorFerretInterface )----------------------------------------- */
275
276
277 public function newFerretEngine() {
278 return new PhabricatorPasteFerretEngine();
279 }
280
281
282/* -( PhabricatorFulltextInterface )--------------------------------------- */
283
284 public function newFulltextEngine() {
285 return new PhabricatorPasteFulltextEngine();
286 }
287
288}