1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\VersionPruner\Api; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
|
|
|
6
|
|
|
use SilverStripe\Core\ClassInfo; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Dev\BuildTask; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
use SilverStripe\ORM\DB; |
11
|
|
|
|
12
|
|
|
class PruneAllVersionedRecordsBasic extends BuildTask |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
protected const MAX_ITEMS_PER_CLASS = 500; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $title = 'Basic SiteTree Prune of Older Records'; |
23
|
|
|
|
24
|
|
|
protected $description = 'See getDescription method for more information.'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private static $segment = 'prune-all-versioned-records-sitetree-basic'; |
30
|
|
|
|
31
|
|
|
private static $delete_older_than_strtotime_phrase = '-12 months'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Prune all published DataObjects which are published according to config. |
35
|
|
|
* |
36
|
|
|
* @param mixed $request |
37
|
|
|
*/ |
38
|
|
|
public function run($request) |
39
|
|
|
{ |
40
|
|
|
$numberOfRecords = DB::query('SELECT COUNT(ID) FROM SiteTree_Versions')->value(); |
41
|
|
|
$oldestRecord = DB::query('SELECT MIN(LastEdited) FROM SiteTree_Versions')->value(); |
42
|
|
|
$newestRecord = DB::query('SELECT MAX(LastEdited) FROM SiteTree_Versions')->value(); |
43
|
|
|
DB::alteration_message("BEFORE: Total pages: {$numberOfRecords}, oldest record: {$oldestRecord}, newest record: {$newestRecord}", 'created'); |
44
|
|
|
|
45
|
|
|
$classTables = ['SiteTree']; |
46
|
|
|
$allClasses = ClassInfo::subclassesFor(SiteTree::class); |
47
|
|
|
foreach ($allClasses as $class) { |
48
|
|
|
if (DataObject::getSchema()->classHasTable($class)) { |
49
|
|
|
$classTables[] = DataObject::getSchema()->tableName($class); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$classTables = array_unique($classTables); |
54
|
|
|
$beforeDate = date('Y-m-d', strtotime($this->Config()->get('delete_older_than_strtotime_phrase'))); |
55
|
|
|
DB::alteration_message("Looking for all versions that are older than {$beforeDate}", 'created'); |
56
|
|
|
foreach ($classTables as $classTable) { |
57
|
|
|
$tableName = $classTable . '_Versions'; |
58
|
|
|
if ('SiteTree_Versions' === $tableName) { |
59
|
|
|
// first we delete the oldies |
60
|
|
|
$joinWhere = " \"LastEdited\" < date '{$beforeDate}'"; |
61
|
|
|
$leftJoin = ''; |
62
|
|
|
} else { |
63
|
|
|
// now we delete the non-linking ones |
64
|
|
|
$leftJoin = " LEFT JOIN SiteTree_Versions ON {$tableName}.RecordID = SiteTree_Versions.RecordID AND {$tableName}.Version = SiteTree_Versions.Version "; |
65
|
|
|
$joinWhere = ' SiteTree_Versions.RecordID IS NULL'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
DB::alteration_message("DELETING ALL ENTRIES FROM {$tableName}"); |
69
|
|
|
$sql = "DELETE {$tableName}.* FROM \"{$tableName}\" {$leftJoin} WHERE {$joinWhere};"; |
70
|
|
|
DB::query($sql); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$numberOfRecords = DB::query('SELECT COUNT(ID) FROM SiteTree_Versions')->value(); |
74
|
|
|
$oldestRecord = DB::query('SELECT MIN(LastEdited) FROM SiteTree_Versions')->value(); |
75
|
|
|
$newestRecord = DB::query('SELECT MAX(LastEdited) FROM SiteTree_Versions')->value(); |
76
|
|
|
DB::alteration_message("Total pages: {$numberOfRecords}, oldest record: {$oldestRecord}, newest record: {$newestRecord}", 'created'); |
77
|
|
|
} |
78
|
|
|
/** |
79
|
|
|
* @return string HTML formatted description |
80
|
|
|
*/ |
81
|
|
|
public function getDescription() |
82
|
|
|
{ |
83
|
|
|
return ' |
84
|
|
|
Basic SiteTree prune of older records. |
85
|
|
|
This task will remove all versions of SiteTree records with a LastEdited date of: '. |
86
|
|
|
$this->Config()->get('delete_older_than_strtotime_phrase'). |
87
|
|
|
' or older. |
88
|
|
|
Up to '.self::MAX_ITEMS_PER_CLASS.' records will be deleted per run. |
89
|
|
|
For a more advanced approach, you can set up a template for a pruning service.'; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths