|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\VersionPruner\PruningTemplates; |
|
4
|
|
|
|
|
5
|
|
|
use Sunnysideup\VersionPruner\PruningTemplatesTemplate; |
|
6
|
|
|
|
|
7
|
|
|
class UserChanged extends PruningTemplatesTemplate |
|
8
|
|
|
{ |
|
9
|
|
|
protected $keepVersions = 10; |
|
10
|
|
|
|
|
11
|
|
|
protected $fieldsWithChangesToKeep = [ |
|
12
|
|
|
'AuthorID', |
|
13
|
|
|
]; |
|
14
|
|
|
|
|
15
|
|
|
public function setKeepVersions(int $keepVersions): self |
|
16
|
|
|
{ |
|
17
|
|
|
$this->keepVersions = $keepVersions; |
|
18
|
|
|
|
|
19
|
|
|
return $this; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getTitle(): string |
|
23
|
|
|
{ |
|
24
|
|
|
return 'Prune automated saves'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getDescription(): string |
|
28
|
|
|
{ |
|
29
|
|
|
return 'Delete versions that are not edited by a logged-in user.'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function run() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->markOlderItemsWithoutAuthor(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* these can be deleted. |
|
39
|
|
|
* |
|
40
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
protected function markOlderItemsWithoutAuthor() |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
foreach ($this->fieldsWithChangesToKeep as $field) { |
|
46
|
|
|
$filter['"' . $field . '" = ?'] = 0; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$query = $this->getBaseQuery($this->fieldsWithChangesToKeep) |
|
50
|
|
|
->addWhere($this->normaliseWhere($filter)) |
|
|
|
|
|
|
51
|
|
|
->setLimit($this->normaliseLimit(), $this->normaliseOffset($this->keepVersions)) |
|
52
|
|
|
; |
|
53
|
|
|
|
|
54
|
|
|
$this->toDelete[$this->getUniqueKey()] = $this->addVersionNumberToArray( |
|
55
|
|
|
$this->toDelete[$this->getUniqueKey()], |
|
56
|
|
|
$query->execute() |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function getItemsToKeep(): array |
|
61
|
|
|
{ |
|
62
|
|
|
// Get the most recent Version IDs of all published pages to ensure |
|
63
|
|
|
// we leave at least X versions even if a URLSegment or ParentID |
|
64
|
|
|
// has changed. |
|
65
|
|
|
$query = $this->getBaseQuery([]) |
|
66
|
|
|
->addWhere( |
|
67
|
|
|
[ |
|
68
|
|
|
'"RecordID" = ?' => $this->object->ID, |
|
69
|
|
|
] |
|
70
|
|
|
) |
|
71
|
|
|
->setLimit($this->keepVersions, 0) |
|
72
|
|
|
; |
|
73
|
|
|
|
|
74
|
|
|
return $this->addVersionNumberToArray( |
|
75
|
|
|
[], |
|
76
|
|
|
$query->execute() |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|