1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ExternalLinks\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\i18n\i18nEntityProvider; |
7
|
|
|
use SilverStripe\Versioned\Versioned; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Represents the status of a track run |
12
|
|
|
* |
13
|
|
|
* @method DataList TrackedPages() |
14
|
|
|
* @method DataList BrokenLinks() |
15
|
|
|
* @property int $TotalPages Get total pages count |
16
|
|
|
* @property int $CompletedPages Get completed pages count |
17
|
|
|
*/ |
18
|
|
|
class BrokenExternalPageTrackStatus extends DataObject implements i18nEntityProvider |
19
|
|
|
{ |
20
|
|
|
private static $table_name = 'BrokenExternalPageTrackStatus'; |
|
|
|
|
21
|
|
|
|
22
|
|
|
private static $db = array( |
|
|
|
|
23
|
|
|
'Status' => 'Enum("Completed, Running", "Running")', |
24
|
|
|
'JobInfo' => 'Varchar(255)' |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
private static $has_many = array( |
|
|
|
|
28
|
|
|
'TrackedPages' => BrokenExternalPageTrack::class, |
29
|
|
|
'BrokenLinks' => BrokenExternalLink::class |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the latest track status |
34
|
|
|
* |
35
|
|
|
* @return BrokenExternalPageTrackStatus |
36
|
|
|
*/ |
37
|
|
|
public static function get_latest() |
38
|
|
|
{ |
39
|
|
|
return self::get() |
40
|
|
|
->sort('ID', 'DESC') |
41
|
|
|
->first(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns the list of provided translations for this object |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function provideI18nEntities() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
__CLASS__ . '.SINGULARNAME' => 'Broken External Page Track Status', |
53
|
|
|
__CLASS__ . '.PLURALNAME' => 'Broken External Page Track Statuses', |
54
|
|
|
__CLASS__ . '.PLURALS' => [ |
55
|
|
|
'one' => 'A Broken External Page Track Status', |
56
|
|
|
'other' => '{count} Broken External Page Track Statuses', |
57
|
|
|
], |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets the list of Pages yet to be checked |
63
|
|
|
* |
64
|
|
|
* @return DataList |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
public function getIncompletePageList() |
67
|
|
|
{ |
68
|
|
|
$pageIDs = $this |
69
|
|
|
->getIncompleteTracks() |
70
|
|
|
->column('PageID'); |
71
|
|
|
if ($pageIDs) { |
72
|
|
|
return Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
|
|
|
73
|
|
|
->byIDs($pageIDs); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the list of incomplete BrokenExternalPageTrack |
79
|
|
|
* |
80
|
|
|
* @return DataList |
81
|
|
|
*/ |
82
|
|
|
public function getIncompleteTracks() |
83
|
|
|
{ |
84
|
|
|
return $this |
85
|
|
|
->TrackedPages() |
86
|
|
|
->filter('Processed', 0); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get total pages count |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public function getTotalPages() |
95
|
|
|
{ |
96
|
|
|
return $this->TrackedPages()->count(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get completed pages count |
101
|
|
|
* |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
|
public function getCompletedPages() |
105
|
|
|
{ |
106
|
|
|
return $this |
107
|
|
|
->TrackedPages() |
108
|
|
|
->filter('Processed', 1) |
109
|
|
|
->count(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the latest run, or otherwise creates a new one |
114
|
|
|
* |
115
|
|
|
* @return BrokenExternalPageTrackStatus |
116
|
|
|
*/ |
117
|
|
|
public static function get_or_create() |
118
|
|
|
{ |
119
|
|
|
// Check the current status |
120
|
|
|
$status = self::get_latest(); |
121
|
|
|
if ($status && $status->Status == 'Running') { |
122
|
|
|
$status->updateStatus(); |
123
|
|
|
return $status; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return self::create_status(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Create and prepare a new status |
131
|
|
|
* |
132
|
|
|
* @return BrokenExternalPageTrackStatus |
133
|
|
|
*/ |
134
|
|
|
public static function create_status() |
135
|
|
|
{ |
136
|
|
|
// If the script is to be started create a new status |
137
|
|
|
$status = self::create(); |
138
|
|
|
$status->updateJobInfo('Creating new tracking object'); |
139
|
|
|
|
140
|
|
|
// Setup all pages to test |
141
|
|
|
$pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage') |
142
|
|
|
->column('ID'); |
143
|
|
|
foreach ($pageIDs as $pageID) { |
144
|
|
|
$trackPage = BrokenExternalPageTrack::create(); |
145
|
|
|
$trackPage->PageID = $pageID; |
146
|
|
|
$trackPage->StatusID = $status->ID; |
147
|
|
|
$trackPage->write(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $status; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function updateJobInfo($message) |
154
|
|
|
{ |
155
|
|
|
$this->JobInfo = $message; |
|
|
|
|
156
|
|
|
$this->write(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Self check status |
161
|
|
|
*/ |
162
|
|
|
public function updateStatus() |
163
|
|
|
{ |
164
|
|
|
if ($this->CompletedPages == $this->TotalPages) { |
165
|
|
|
$this->Status = 'Completed'; |
|
|
|
|
166
|
|
|
$this->updateJobInfo('Setting to completed'); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|