PruneAllVersionedRecordsBasic   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 37
c 3
b 0
f 0
dl 0
loc 77
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 8 1
A run() 0 39 5
1
<?php
2
3
namespace Sunnysideup\VersionPruner\Api;
4
5
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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