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