/
PruneRevisions.php
90 lines (78 loc) · 2.22 KB
/
PruneRevisions.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\queue\jobs;
use Craft;
use craft\base\ElementInterface;
use craft\i18n\Translation;
use craft\queue\BaseJob;
/**
* PruneRevisions job
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.5.0
*/
class PruneRevisions extends BaseJob
{
/**
* @var string The type of elements to update.
* @phpstan-var class-string<ElementInterface>
*/
public string $elementType;
/**
* @var int The ID of the canonical element.
*/
public int $canonicalId;
/**
* @var int The site ID of the source element
*/
public int $siteId;
/**
* @var int|null The maximum number of revisions an element can have
* @since 3.5.13
*/
public ?int $maxRevisions = null;
/**
* @inheritdoc
*/
public function execute($queue): void
{
if (!$this->maxRevisions) {
// Make sure maxRevisions is still set
$generalConfig = Craft::$app->getConfig()->getGeneral();
if (!$generalConfig->maxRevisions) {
return;
}
$this->maxRevisions = $generalConfig->maxRevisions;
}
/** @var string|ElementInterface $class */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $class */
$class = $this->elementType;
$extraRevisions = $class::find()
->revisionOf($this->canonicalId)
->siteId($this->siteId)
->status(null)
->orderBy(['num' => SORT_DESC])
->offset($this->maxRevisions)
->all();
if (empty($extraRevisions)) {
return;
}
$total = count($extraRevisions);
$elementsService = Craft::$app->getElements();
foreach ($extraRevisions as $i => $extraRevision) {
$this->setProgress($queue, ($i + 1) / $total);
$elementsService->deleteElement($extraRevision, true);
}
}
/**
* @inheritdoc
*/
protected function defaultDescription(): ?string
{
return Translation::prep('app', 'Pruning extra revisions');
}
}