Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/diff/interface/PhabricatorInlineComment.php
12242 views
1
<?php
2
3
abstract class PhabricatorInlineComment
4
extends Phobject
5
implements
6
PhabricatorMarkupInterface {
7
8
const MARKUP_FIELD_BODY = 'markup:body';
9
10
const STATE_UNDONE = 'undone';
11
const STATE_DRAFT = 'draft';
12
const STATE_UNDRAFT = 'undraft';
13
const STATE_DONE = 'done';
14
15
private $storageObject;
16
private $syntheticAuthor;
17
private $isGhost;
18
private $versionedDrafts = array();
19
20
public function __clone() {
21
$this->storageObject = clone $this->storageObject;
22
}
23
24
final public static function loadAndAttachVersionedDrafts(
25
PhabricatorUser $viewer,
26
array $inlines) {
27
28
$viewer_phid = $viewer->getPHID();
29
if (!$viewer_phid) {
30
return;
31
}
32
33
$inlines = mpull($inlines, null, 'getPHID');
34
35
$load = array();
36
foreach ($inlines as $key => $inline) {
37
if (!$inline->getIsEditing()) {
38
continue;
39
}
40
41
if ($inline->getAuthorPHID() !== $viewer_phid) {
42
continue;
43
}
44
45
$load[$key] = $inline;
46
}
47
48
if (!$load) {
49
return;
50
}
51
52
$drafts = PhabricatorVersionedDraft::loadDrafts(
53
array_keys($load),
54
$viewer_phid);
55
56
$drafts = mpull($drafts, null, 'getObjectPHID');
57
foreach ($inlines as $inline) {
58
$draft = idx($drafts, $inline->getPHID());
59
$inline->attachVersionedDraftForViewer($viewer, $draft);
60
}
61
}
62
63
public function setSyntheticAuthor($synthetic_author) {
64
$this->syntheticAuthor = $synthetic_author;
65
return $this;
66
}
67
68
public function getSyntheticAuthor() {
69
return $this->syntheticAuthor;
70
}
71
72
public function setStorageObject($storage_object) {
73
$this->storageObject = $storage_object;
74
return $this;
75
}
76
77
public function getStorageObject() {
78
if (!$this->storageObject) {
79
$this->storageObject = $this->newStorageObject();
80
}
81
82
return $this->storageObject;
83
}
84
85
public function getInlineCommentCacheFragment() {
86
$phid = $this->getPHID();
87
88
if ($phid === null) {
89
return null;
90
}
91
92
return sprintf('inline(%s)', $phid);
93
}
94
95
abstract protected function newStorageObject();
96
abstract public function getControllerURI();
97
98
abstract public function setChangesetID($id);
99
abstract public function getChangesetID();
100
101
abstract public function supportsHiding();
102
abstract public function isHidden();
103
104
public function isDraft() {
105
return !$this->getTransactionPHID();
106
}
107
108
public function getTransactionPHID() {
109
return $this->getStorageObject()->getTransactionPHID();
110
}
111
112
public function isCompatible(PhabricatorInlineComment $comment) {
113
return
114
($this->getAuthorPHID() === $comment->getAuthorPHID()) &&
115
($this->getSyntheticAuthor() === $comment->getSyntheticAuthor()) &&
116
($this->getContent() === $comment->getContent());
117
}
118
119
public function setIsGhost($is_ghost) {
120
$this->isGhost = $is_ghost;
121
return $this;
122
}
123
124
public function getIsGhost() {
125
return $this->isGhost;
126
}
127
128
public function setContent($content) {
129
$this->getStorageObject()->setContent($content);
130
return $this;
131
}
132
133
public function getContent() {
134
return $this->getStorageObject()->getContent();
135
}
136
137
public function getID() {
138
return $this->getStorageObject()->getID();
139
}
140
141
public function getPHID() {
142
return $this->getStorageObject()->getPHID();
143
}
144
145
public function setIsNewFile($is_new) {
146
$this->getStorageObject()->setIsNewFile($is_new);
147
return $this;
148
}
149
150
public function getIsNewFile() {
151
return $this->getStorageObject()->getIsNewFile();
152
}
153
154
public function setFixedState($state) {
155
$this->getStorageObject()->setFixedState($state);
156
return $this;
157
}
158
159
public function setHasReplies($has_replies) {
160
$this->getStorageObject()->setHasReplies($has_replies);
161
return $this;
162
}
163
164
public function getHasReplies() {
165
return $this->getStorageObject()->getHasReplies();
166
}
167
168
public function getFixedState() {
169
return $this->getStorageObject()->getFixedState();
170
}
171
172
public function setLineNumber($number) {
173
$this->getStorageObject()->setLineNumber($number);
174
return $this;
175
}
176
177
public function getLineNumber() {
178
return $this->getStorageObject()->getLineNumber();
179
}
180
181
public function setLineLength($length) {
182
$this->getStorageObject()->setLineLength($length);
183
return $this;
184
}
185
186
public function getLineLength() {
187
return $this->getStorageObject()->getLineLength();
188
}
189
190
public function setAuthorPHID($phid) {
191
$this->getStorageObject()->setAuthorPHID($phid);
192
return $this;
193
}
194
195
public function getAuthorPHID() {
196
return $this->getStorageObject()->getAuthorPHID();
197
}
198
199
public function setReplyToCommentPHID($phid) {
200
$this->getStorageObject()->setReplyToCommentPHID($phid);
201
return $this;
202
}
203
204
public function getReplyToCommentPHID() {
205
return $this->getStorageObject()->getReplyToCommentPHID();
206
}
207
208
public function setIsDeleted($is_deleted) {
209
$this->getStorageObject()->setIsDeleted($is_deleted);
210
return $this;
211
}
212
213
public function getIsDeleted() {
214
return $this->getStorageObject()->getIsDeleted();
215
}
216
217
public function setIsEditing($is_editing) {
218
$this->getStorageObject()->setAttribute('editing', (bool)$is_editing);
219
return $this;
220
}
221
222
public function getIsEditing() {
223
return (bool)$this->getStorageObject()->getAttribute('editing', false);
224
}
225
226
public function setDocumentEngineKey($engine_key) {
227
$this->getStorageObject()->setAttribute('documentEngineKey', $engine_key);
228
return $this;
229
}
230
231
public function getDocumentEngineKey() {
232
return $this->getStorageObject()->getAttribute('documentEngineKey');
233
}
234
235
public function setStartOffset($offset) {
236
$this->getStorageObject()->setAttribute('startOffset', $offset);
237
return $this;
238
}
239
240
public function getStartOffset() {
241
return $this->getStorageObject()->getAttribute('startOffset');
242
}
243
244
public function setEndOffset($offset) {
245
$this->getStorageObject()->setAttribute('endOffset', $offset);
246
return $this;
247
}
248
249
public function getEndOffset() {
250
return $this->getStorageObject()->getAttribute('endOffset');
251
}
252
253
public function getDateModified() {
254
return $this->getStorageObject()->getDateModified();
255
}
256
257
public function getDateCreated() {
258
return $this->getStorageObject()->getDateCreated();
259
}
260
261
public function openTransaction() {
262
$this->getStorageObject()->openTransaction();
263
}
264
265
public function saveTransaction() {
266
$this->getStorageObject()->saveTransaction();
267
}
268
269
public function save() {
270
$this->getTransactionCommentForSave()->save();
271
return $this;
272
}
273
274
public function delete() {
275
$this->getStorageObject()->delete();
276
return $this;
277
}
278
279
public function makeEphemeral() {
280
$this->getStorageObject()->makeEphemeral();
281
return $this;
282
}
283
284
public function attachVersionedDraftForViewer(
285
PhabricatorUser $viewer,
286
PhabricatorVersionedDraft $draft = null) {
287
288
$key = $viewer->getCacheFragment();
289
$this->versionedDrafts[$key] = $draft;
290
291
return $this;
292
}
293
294
public function hasVersionedDraftForViewer(PhabricatorUser $viewer) {
295
$key = $viewer->getCacheFragment();
296
return array_key_exists($key, $this->versionedDrafts);
297
}
298
299
public function getVersionedDraftForViewer(PhabricatorUser $viewer) {
300
$key = $viewer->getCacheFragment();
301
if (!array_key_exists($key, $this->versionedDrafts)) {
302
throw new Exception(
303
pht(
304
'Versioned draft is not attached for user with fragment "%s".',
305
$key));
306
}
307
308
return $this->versionedDrafts[$key];
309
}
310
311
public function isVoidComment(PhabricatorUser $viewer) {
312
return $this->getContentStateForEdit($viewer)->isEmptyContentState();
313
}
314
315
public function getContentStateForEdit(PhabricatorUser $viewer) {
316
$state = $this->getContentState();
317
318
if ($this->hasVersionedDraftForViewer($viewer)) {
319
$versioned_draft = $this->getVersionedDraftForViewer($viewer);
320
if ($versioned_draft) {
321
$storage_map = $versioned_draft->getProperty('inline.state');
322
if (is_array($storage_map)) {
323
$state->readStorageMap($storage_map);
324
}
325
}
326
}
327
328
return $state;
329
}
330
331
protected function newContentState() {
332
return new PhabricatorDiffInlineCommentContentState();
333
}
334
335
public function newContentStateFromRequest(AphrontRequest $request) {
336
return $this->newContentState()->readFromRequest($request);
337
}
338
339
public function getInitialContentState() {
340
return $this->getNamedContentState('inline.state.initial');
341
}
342
343
public function setInitialContentState(
344
PhabricatorInlineCommentContentState $state) {
345
return $this->setNamedContentState('inline.state.initial', $state);
346
}
347
348
public function getCommittedContentState() {
349
return $this->getNamedContentState('inline.state.committed');
350
}
351
352
public function setCommittedContentState(
353
PhabricatorInlineCommentContentState $state) {
354
return $this->setNamedContentState('inline.state.committed', $state);
355
}
356
357
public function getContentState() {
358
$state = $this->getNamedContentState('inline.state');
359
360
if (!$state) {
361
$state = $this->newContentState();
362
}
363
364
$state->setContentText($this->getContent());
365
366
return $state;
367
}
368
369
public function setContentState(PhabricatorInlineCommentContentState $state) {
370
$this->setContent($state->getContentText());
371
372
return $this->setNamedContentState('inline.state', $state);
373
}
374
375
private function getNamedContentState($key) {
376
$storage = $this->getStorageObject();
377
378
$storage_map = $storage->getAttribute($key);
379
if (!is_array($storage_map)) {
380
return null;
381
}
382
383
$state = $this->newContentState();
384
$state->readStorageMap($storage_map);
385
return $state;
386
}
387
388
private function setNamedContentState(
389
$key,
390
PhabricatorInlineCommentContentState $state) {
391
392
$storage = $this->getStorageObject();
393
$storage_map = $state->newStorageMap();
394
$storage->setAttribute($key, $storage_map);
395
396
return $this;
397
}
398
399
public function getInlineContext() {
400
return $this->getStorageObject()->getInlineContext();
401
}
402
403
public function getContentStateMapForEdit(PhabricatorUser $viewer) {
404
return $this->getWireContentStateMap(true, $viewer);
405
}
406
407
public function getContentStateMap() {
408
return $this->getWireContentStateMap(false, null);
409
}
410
411
private function getWireContentStateMap(
412
$is_edit,
413
PhabricatorUser $viewer = null) {
414
415
$initial_state = $this->getInitialContentState();
416
$committed_state = $this->getCommittedContentState();
417
418
if ($is_edit) {
419
$active_state = $this->getContentStateForEdit($viewer);
420
} else {
421
$active_state = $this->getContentState();
422
}
423
424
return array(
425
'initial' => $this->getWireContentState($initial_state),
426
'committed' => $this->getWireContentState($committed_state),
427
'active' => $this->getWireContentState($active_state),
428
);
429
}
430
431
private function getWireContentState($content_state) {
432
if ($content_state === null) {
433
return null;
434
}
435
436
return $content_state->newStorageMap();
437
}
438
439
public function getDefaultSuggestionText() {
440
$context = $this->getInlineContext();
441
442
if (!$context) {
443
return null;
444
}
445
446
$default = $context->getBodyLines();
447
$default = implode('', $default);
448
449
return $default;
450
}
451
452
453
/* -( PhabricatorMarkupInterface Implementation )-------------------------- */
454
455
456
public function getMarkupFieldKey($field) {
457
$content = $this->getMarkupText($field);
458
return PhabricatorMarkupEngine::digestRemarkupContent($this, $content);
459
}
460
461
public function newMarkupEngine($field) {
462
return PhabricatorMarkupEngine::newDifferentialMarkupEngine();
463
}
464
465
public function getMarkupText($field) {
466
return $this->getContent();
467
}
468
469
public function didMarkupText($field, $output, PhutilMarkupEngine $engine) {
470
return $output;
471
}
472
473
public function shouldUseMarkupCache($field) {
474
return !$this->isDraft();
475
}
476
477
}
478
479