-
Notifications
You must be signed in to change notification settings - Fork 638
/
EntryRevisions.php
632 lines (537 loc) · 19.5 KB
/
EntryRevisions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\services;
use Craft;
use craft\base\Element;
use craft\base\Field;
use craft\db\Query;
use craft\elements\Entry;
use craft\errors\EntryDraftNotFoundException;
use craft\events\DraftEvent;
use craft\events\VersionEvent;
use craft\helpers\Json;
use craft\models\BaseEntryRevisionModel;
use craft\models\EntryDraft;
use craft\models\EntryVersion;
use craft\models\Section;
use craft\records\EntryDraft as EntryDraftRecord;
use craft\records\EntryVersion as EntryVersionRecord;
use yii\base\Component;
use yii\base\InvalidConfigException;
/**
* Entry Revisions service.
* An instance of the Entry Revisions service is globally accessible in Craft via [[\craft\base\ApplicationTrait::getEntryRevisions()|`Craft::$app->entryRevisions`]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0
*/
class EntryRevisions extends Component
{
// Constants
// =========================================================================
/**
* @event DraftEvent The event that is triggered before a draft is saved.
*/
const EVENT_BEFORE_SAVE_DRAFT = 'beforeSaveDraft';
/**
* @event DraftEvent The event that is triggered after a draft is saved.
*/
const EVENT_AFTER_SAVE_DRAFT = 'afterSaveDraft';
/**
* @event DraftEvent The event that is triggered before a draft is published.
*/
const EVENT_BEFORE_PUBLISH_DRAFT = 'beforePublishDraft';
/**
* @event DraftEvent The event that is triggered after a draft is published.
*/
const EVENT_AFTER_PUBLISH_DRAFT = 'afterPublishDraft';
/**
* @event DraftEvent The event that is triggered before a draft is deleted.
*/
const EVENT_BEFORE_DELETE_DRAFT = 'beforeDeleteDraft';
/**
* @event DraftEvent The event that is triggered after a draft is deleted.
*/
const EVENT_AFTER_DELETE_DRAFT = 'afterDeleteDraft';
/**
* @event VersionEvent The event that is triggered before an entry is reverted to an old version.
*/
const EVENT_BEFORE_REVERT_ENTRY_TO_VERSION = 'beforeRevertEntryToVersion';
/**
* @event VersionEvent The event that is triggered after an entry is reverted to an old version.
*/
const EVENT_AFTER_REVERT_ENTRY_TO_VERSION = 'afterRevertEntryToVersion';
// Public Methods
// =========================================================================
/**
* Returns a draft by its ID.
*
* @param int $draftId
* @return EntryDraft|null
*/
public function getDraftById(int $draftId)
{
$result = $this->_getDraftsQuery()
->where(['id' => $draftId])
->one();
if (!$result) {
return null;
}
$result['data'] = Json::decode($result['data']);
$draft = new EntryDraft($result);
$entry = Craft::$app->getEntries()->getEntryById($draft->id, $draft->siteId);
$this->_configureRevisionWithEntryProperties($draft, $entry);
return $draft;
}
/**
* Returns drafts of a given entry.
*
* @param int $entryId
* @param int|null $siteId
* @param bool $withContent Whether the field content should be included on the drafts
* @return EntryDraft[]
*/
public function getDraftsByEntryId(int $entryId, int $siteId = null, bool $withContent = false): array
{
if ($siteId === null) {
$siteId = Craft::$app->getSites()->getPrimarySite()->id;
}
if (($entry = Craft::$app->getEntries()->getEntryById($entryId, $siteId)) === null) {
return [];
}
$drafts = [];
$results = $this->_getDraftsQuery()
->where(['entryId' => $entryId, 'siteId' => $siteId])
->orderBy(['name' => SORT_ASC])
->all();
foreach ($results as $result) {
$result['data'] = Json::decode($result['data']);
if (!$withContent) {
unset($result['data']['fields']);
}
$draft = new EntryDraft($result);
$this->_configureRevisionWithEntryProperties($draft, $entry);
$drafts[] = $draft;
}
return $drafts;
}
/**
* Returns the drafts of a given entry that are editable by the current user.
*
* @param int $entryId
* @param int|null $siteId
* @return EntryDraft[]
*/
public function getEditableDraftsByEntryId(int $entryId, int $siteId = null): array
{
$editableDrafts = [];
$user = Craft::$app->getUser()->getIdentity();
if ($user) {
$allDrafts = $this->getDraftsByEntryId($entryId, $siteId);
foreach ($allDrafts as $draft) {
if (
($draft->creatorId && $draft->creatorId == $user->id) ||
$user->can('editPeerEntryDrafts:' . $draft->sectionId)
) {
$editableDrafts[] = $draft;
}
}
}
return $editableDrafts;
}
/**
* Saves a draft.
*
* @param EntryDraft $draft The draft to be saved
* @param bool $runValidation Whether to perform validation
* @return bool
*/
public function saveDraft(EntryDraft $draft, bool $runValidation = true): bool
{
$isNewDraft = !$draft->draftId;
if (!$draft->name && $draft->id) {
// Get the total number of existing drafts for this entry/site
$totalDrafts = (new Query())
->from(['{{%entrydrafts}}'])
->where(['entryId' => $draft->id, 'siteId' => $draft->siteId])
->count('[[id]]');
$draft->name = Craft::t('app', 'Draft {num}',
['num' => $totalDrafts + 1]);
}
// Fire a 'beforeSaveDraft' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_SAVE_DRAFT)) {
$this->trigger(self::EVENT_BEFORE_SAVE_DRAFT, new DraftEvent([
'draft' => $draft,
'isNew' => $isNewDraft,
]));
}
if ($runValidation && !$draft->validate()) {
Craft::info('Draft not saved due to validation error.', __METHOD__);
return false;
}
$draftRecord = $this->_getDraftRecord($draft);
$draftRecord->name = $draft->name;
$draftRecord->notes = $draft->revisionNotes;
$draftRecord->data = $this->_getRevisionData($draft);
$draftRecord->save(false);
if ($isNewDraft) {
$draft->draftId = $draftRecord->id;
}
// Fire an 'afterSaveDraft' event
if ($this->hasEventHandlers(self::EVENT_AFTER_SAVE_DRAFT)) {
$this->trigger(self::EVENT_AFTER_SAVE_DRAFT, new DraftEvent([
'draft' => $draft,
'isNew' => $isNewDraft,
]));
}
return true;
}
/**
* Publishes a draft.
*
* @param EntryDraft $draft The draft to be published
* @param bool $runValidation Whether to perform validation
* @return bool
*/
public function publishDraft(EntryDraft $draft, bool $runValidation = true): bool
{
// If this is a single, we'll have to set the title manually
if ($draft->getSection()->type == Section::TYPE_SINGLE) {
$draft->title = $draft->getSection()->name;
}
// Set the version notes
if (!$draft->revisionNotes) {
$draft->revisionNotes = Craft::t('app', 'Published draft “{name}”.', ['name' => $draft->name]);
}
// Fire a 'beforePublishDraft' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_PUBLISH_DRAFT)) {
$this->trigger(self::EVENT_BEFORE_PUBLISH_DRAFT, new DraftEvent([
'draft' => $draft
]));
}
if ($draft->enabled && $draft->enabledForSite) {
$draft->setScenario(Element::SCENARIO_LIVE);
}
if ($runValidation && !$draft->validate()) {
Craft::info('Draft not published due to validation error.', __METHOD__);
return false;
}
// Save the entry without re-running validation on it
Craft::$app->getElements()->saveElement($draft, false);
// Delete the draft
$this->deleteDraft($draft);
// Should we save a new version?
if ($draft->getSection()->enableVersioning) {
$this->saveVersion($draft);
}
// Fire an 'afterPublishDraft' event
if ($this->hasEventHandlers(self::EVENT_AFTER_PUBLISH_DRAFT)) {
$this->trigger(self::EVENT_AFTER_PUBLISH_DRAFT, new DraftEvent([
'draft' => $draft
]));
}
return true;
}
/**
* Deletes a draft by it's model.
*
* @param EntryDraft $draft The draft to be deleted
* @return bool Whether the draft was deleted successfully
*/
public function deleteDraft(EntryDraft $draft): bool
{
// Fire a 'beforeDeleteDraft' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_DELETE_DRAFT)) {
$this->trigger(self::EVENT_BEFORE_DELETE_DRAFT, new DraftEvent([
'draft' => $draft
]));
}
// Delete it
$this->_getDraftRecord($draft)->delete();
// Fire an 'afterDeleteDraft' event
if ($this->hasEventHandlers(self::EVENT_AFTER_DELETE_DRAFT)) {
$this->trigger(self::EVENT_AFTER_DELETE_DRAFT, new DraftEvent([
'draft' => $draft
]));
}
return true;
}
/**
* Returns a version by its ID.
*
* @param int $versionId
* @return EntryVersion|null
*/
public function getVersionById(int $versionId)
{
$result = $this->_getRevisionsQuery()
->where(['id' => $versionId])
->one();
if (!$result) {
return null;
}
$result['data'] = Json::decode($result['data']);
$version = new EntryVersion($result);
$entry = Craft::$app->getEntries()->getEntryById($version->id, $version->siteId);
$this->_configureRevisionWithEntryProperties($version, $entry);
return $version;
}
/**
* Returns whether an entry has any versions stored.
*
* @param int $entryId The entry ID to search for
* @param int|null $siteId The site ID to search for
* @return bool
*/
public function doesEntryHaveVersions(int $entryId, int $siteId = null): bool
{
if (!$siteId) {
$siteId = Craft::$app->getSites()->getPrimarySite()->id;
}
return $this->_getRevisionsQuery()
->where(['entryId' => $entryId, 'siteId' => $siteId])
->exists();
}
/**
* Returns versions by an entry ID.
*
* @param int $entryId The entry ID to search for.
* @param int|null $siteId The site ID to search for.
* @param int|null $limit The limit on the number of versions to retrieve.
* @param bool $includeCurrent Whether to include the current "top" version of the entry.
* @param bool $withContent Whether the field content should be included on the versions
* @return EntryVersion[]
*/
public function getVersionsByEntryId(int $entryId, int $siteId = null, int $limit = null, bool $includeCurrent = false, bool $withContent = false): array
{
if (!$siteId) {
$siteId = Craft::$app->getSites()->getPrimarySite()->id;
}
if (($entry = Craft::$app->getEntries()->getEntryById($entryId, $siteId)) === null) {
return [];
}
$versions = [];
$results = $this->_getRevisionsQuery()
->where(['entryId' => $entryId, 'siteId' => $siteId])
->orderBy(['id' => SORT_DESC])
->offset($includeCurrent ? 0 : 1)
->limit($limit)
->all();
foreach ($results as $result) {
$result['data'] = Json::decode($result['data']);
if (!$withContent) {
unset($result['data']['fields']);
}
$version = new EntryVersion($result);
$this->_configureRevisionWithEntryProperties($version, $entry);
$versions[] = $version;
}
return $versions;
}
/**
* Saves a new version.
*
* @param Entry $entry
* @return bool
*/
public function saveVersion(Entry $entry): bool
{
// Get the total number of existing versions for this entry/site
$versionQuery = (new Query())
->from(['{{%entryversions}}'])
->where(['entryId' => $entry->id, 'siteId' => $entry->siteId]);
$totalVersions = $versionQuery->count('[[id]]');
$revisionData = $this->_getRevisionData($entry);
if ($totalVersions) {
$lastVersionData = $versionQuery
->select(['data'])
->orderBy(['id' => SORT_DESC])
->scalar();
$lastVersionData = Json::decode($lastVersionData);
// If nothing changed, then we're done here
if (!$this->compareRevisionData($lastVersionData, $revisionData)) {
return true;
}
}
$versionRecord = new EntryVersionRecord();
$versionRecord->entryId = $entry->id;
$versionRecord->sectionId = $entry->sectionId;
$versionRecord->creatorId = $entry->revisionCreatorId ?? Craft::$app->getUser()->getIdentity()->id ?? $entry->authorId;
$versionRecord->siteId = $entry->siteId;
$versionRecord->num = $totalVersions + 1;
$versionRecord->data = $revisionData;
$versionRecord->notes = $entry->revisionNotes;
return $versionRecord->save();
}
/**
* Reverts an entry to a version.
*
* @param EntryVersion $version
* @param bool $runValidation Whether to perform validation
* @return bool
*/
public function revertEntryToVersion(EntryVersion $version, bool $runValidation = true): bool
{
// If this is a single, we'll have to set the title manually
if ($version->getSection()->type === Section::TYPE_SINGLE) {
$version->title = $version->getSection()->name;
}
// Set the version notes
$version->revisionNotes = Craft::t('app', 'Reverted version {num}.', ['num' => $version->num]);
// Fire a 'beforeRevertEntryToVersion' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_REVERT_ENTRY_TO_VERSION)) {
$this->trigger(self::EVENT_BEFORE_REVERT_ENTRY_TO_VERSION, new VersionEvent([
'version' => $version,
]));
}
if ($version->enabled && $version->enabledForSite) {
$version->setScenario(Element::SCENARIO_LIVE);
}
if ($runValidation && !$version->validate()) {
Craft::info('Entry not reverted due to validation error.', __METHOD__);
return false;
}
// Revert the entry without re-running validation on it
Craft::$app->getElements()->saveElement($version, false);
// Fire an 'afterRevertEntryToVersion' event
if ($this->hasEventHandlers(self::EVENT_AFTER_REVERT_ENTRY_TO_VERSION)) {
$this->trigger(self::EVENT_AFTER_REVERT_ENTRY_TO_VERSION, new VersionEvent([
'version' => $version,
]));
}
return true;
}
// Protected Methods
// =========================================================================
/**
* Compares two revisions' data and returns whether it has changed.
*
* @param array $revisionA
* @param array $revisionB
* @return bool whether it looks like something has changed
*/
protected function compareRevisionData(array $revisionA, array $revisionB): bool
{
return $revisionA !== $revisionB;
}
// Private Methods
// =========================================================================
/**
* Returns a draft record.
*
* @param EntryDraft $draft
* @return EntryDraftRecord
* @throws EntryDraftNotFoundException if $draft->draftId is invalid
*/
private function _getDraftRecord(EntryDraft $draft): EntryDraftRecord
{
if ($draft->draftId) {
$draftRecord = EntryDraftRecord::findOne($draft->draftId);
if (!$draftRecord) {
throw new EntryDraftNotFoundException('Invalid entry draft ID: ' . $draft->draftId);
}
} else {
$draftRecord = new EntryDraftRecord();
$draftRecord->entryId = $draft->id;
$draftRecord->sectionId = $draft->sectionId;
$draftRecord->creatorId = $draft->creatorId;
$draftRecord->siteId = $draft->siteId;
}
return $draftRecord;
}
/**
* Returns an array of all the revision data for a draft or version.
*
* @param Entry $revision
* @return array
*/
private function _getRevisionData(Entry $revision): array
{
$revisionData = [
'typeId' => $revision->typeId,
'authorId' => $revision->authorId,
'title' => $revision->title,
'slug' => $revision->slug,
'postDate' => $revision->postDate ? $revision->postDate->getTimestamp() : null,
'expiryDate' => $revision->expiryDate ? $revision->expiryDate->getTimestamp() : null,
'enabled' => $revision->enabled,
'newParentId' => $revision->newParentId,
'fields' => [],
];
$content = $revision->getSerializedFieldValues();
foreach (Craft::$app->getFields()->getAllFields() as $field) {
/** @var Field $field */
if (isset($content[$field->handle]) && $content[$field->handle] !== null) {
$revisionData['fields'][$field->id] = $content[$field->handle];
}
}
return $revisionData;
}
/**
* Updates a revision model with entry properties that aren't saved in the revision tables.
*
* @param BaseEntryRevisionModel $revision
* @param Entry $entry
*/
private function _configureRevisionWithEntryProperties(BaseEntryRevisionModel $revision, Entry $entry)
{
$revision->contentId = $entry->contentId;
$revision->root = $entry->root;
$revision->lft = $entry->lft;
$revision->rgt = $entry->rgt;
$revision->level = $entry->level;
// Make sure the revision's entry type still exists
try {
$revision->getType();
} catch (InvalidConfigException $e) {
// Nope. Use the entry's current type instead
$revision->typeId = $entry->typeId;
}
}
/**
* @return Query
*/
private function _getDraftsQuery(): Query
{
return (new Query())
->select([
'id',
'entryId',
'sectionId',
'creatorId',
'siteId',
'name',
'notes',
'data',
'dateCreated',
'dateUpdated',
'uid',
])
->from(['{{%entrydrafts}}']);
}
/**
* @return Query
*/
private function _getRevisionsQuery(): Query
{
return (new Query())
->select([
'id',
'entryId',
'sectionId',
'creatorId',
'siteId',
'num',
'notes',
'data',
'dateCreated',
'dateUpdated',
'uid',
])
->from(['{{%entryversions}}']);
}
}