@@ -18,229 +18,229 @@ |
||
18 | 18 | |
19 | 19 | class CheckExternalLinksTask extends BuildTask |
20 | 20 | { |
21 | - private static $dependencies = [ |
|
22 | - 'LinkChecker' => '%$' . LinkChecker::class |
|
23 | - ]; |
|
24 | - |
|
25 | - private static $segment = 'CheckExternalLinksTask'; |
|
26 | - |
|
27 | - /** |
|
28 | - * Define a list of HTTP response codes that should not be treated as "broken", where they usually |
|
29 | - * might be. |
|
30 | - * |
|
31 | - * @config |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - private static $ignore_codes = []; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var bool |
|
38 | - */ |
|
39 | - protected $silent = false; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var LinkChecker |
|
43 | - */ |
|
44 | - protected $linkChecker; |
|
45 | - |
|
46 | - protected $title = 'Checking broken External links in the SiteTree'; |
|
47 | - |
|
48 | - protected $description = 'A task that records external broken links in the SiteTree'; |
|
49 | - |
|
50 | - protected $enabled = true; |
|
51 | - |
|
52 | - /** |
|
53 | - * Log a message |
|
54 | - * |
|
55 | - * @param string $message |
|
56 | - */ |
|
57 | - protected function log($message) |
|
58 | - { |
|
59 | - if (!$this->silent) { |
|
60 | - Debug::message($message); |
|
61 | - } |
|
62 | - } |
|
63 | - |
|
64 | - public function run($request) |
|
65 | - { |
|
66 | - $this->runLinksCheck(); |
|
67 | - } |
|
68 | - /** |
|
69 | - * Turn on or off message output |
|
70 | - * |
|
71 | - * @param bool $silent |
|
72 | - */ |
|
73 | - public function setSilent($silent) |
|
74 | - { |
|
75 | - $this->silent = $silent; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param LinkChecker $linkChecker |
|
80 | - */ |
|
81 | - public function setLinkChecker(LinkChecker $linkChecker) |
|
82 | - { |
|
83 | - $this->linkChecker = $linkChecker; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @return LinkChecker |
|
88 | - */ |
|
89 | - public function getLinkChecker() |
|
90 | - { |
|
91 | - return $this->linkChecker; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Check the status of a single link on a page |
|
96 | - * |
|
97 | - * @param BrokenExternalPageTrack $pageTrack |
|
98 | - * @param DOMNode $link |
|
99 | - */ |
|
100 | - protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link) |
|
101 | - { |
|
102 | - $class = $link->getAttribute('class'); |
|
103 | - $href = $link->getAttribute('href'); |
|
104 | - $markedBroken = preg_match('/\b(ss-broken)\b/', $class); |
|
105 | - |
|
106 | - // Check link |
|
107 | - $httpCode = $this->linkChecker->checkLink($href); |
|
108 | - if ($httpCode === null) { |
|
109 | - return; // Null link means uncheckable, such as an internal link |
|
110 | - } |
|
111 | - |
|
112 | - // If this code is broken then mark as such |
|
113 | - if ($foundBroken = $this->isCodeBroken($httpCode)) { |
|
114 | - // Create broken record |
|
115 | - $brokenLink = new BrokenExternalLink(); |
|
116 | - $brokenLink->Link = $href; |
|
117 | - $brokenLink->HTTPCode = $httpCode; |
|
118 | - $brokenLink->TrackID = $pageTrack->ID; |
|
119 | - $brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons |
|
120 | - $brokenLink->write(); |
|
121 | - } |
|
122 | - |
|
123 | - // Check if we need to update CSS class, otherwise return |
|
124 | - if ($markedBroken == $foundBroken) { |
|
125 | - return; |
|
126 | - } |
|
127 | - if ($foundBroken) { |
|
128 | - $class .= ' ss-broken'; |
|
129 | - } else { |
|
130 | - $class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class); |
|
131 | - } |
|
132 | - $link->setAttribute('class', trim($class)); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Determine if the given HTTP code is "broken" |
|
137 | - * |
|
138 | - * @param int $httpCode |
|
139 | - * @return bool True if this is a broken code |
|
140 | - */ |
|
141 | - protected function isCodeBroken($httpCode) |
|
142 | - { |
|
143 | - // Null represents no request attempted |
|
144 | - if ($httpCode === null) { |
|
145 | - return false; |
|
146 | - } |
|
147 | - |
|
148 | - // do we have any whitelisted codes |
|
149 | - $ignoreCodes = $this->config()->get('ignore_codes'); |
|
150 | - if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) { |
|
151 | - return false; |
|
152 | - } |
|
153 | - |
|
154 | - // Check if code is outside valid range |
|
155 | - return $httpCode < 200 || $httpCode > 302; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Runs the links checker and returns the track used |
|
160 | - * |
|
161 | - * @param int $limit Limit to number of pages to run, or null to run all |
|
162 | - * @return BrokenExternalPageTrackStatus |
|
163 | - */ |
|
164 | - public function runLinksCheck($limit = null) |
|
165 | - { |
|
166 | - // Check the current status |
|
167 | - $status = BrokenExternalPageTrackStatus::get_or_create(); |
|
168 | - |
|
169 | - // Calculate pages to run |
|
170 | - $pageTracks = $status->getIncompleteTracks(); |
|
171 | - if ($limit) { |
|
172 | - $pageTracks = $pageTracks->limit($limit); |
|
173 | - } |
|
174 | - |
|
175 | - // Check each page |
|
176 | - foreach ($pageTracks as $pageTrack) { |
|
177 | - // Flag as complete |
|
178 | - $pageTrack->Processed = 1; |
|
179 | - $pageTrack->write(); |
|
180 | - |
|
181 | - // Check value of html area |
|
182 | - $page = $pageTrack->Page(); |
|
183 | - $this->log("Checking {$page->Title}"); |
|
184 | - $htmlValue = Injector::inst()->create('HTMLValue', $page->Content); |
|
185 | - if (!$htmlValue->isValid()) { |
|
186 | - continue; |
|
187 | - } |
|
188 | - |
|
189 | - // Check each link |
|
190 | - $links = $htmlValue->getElementsByTagName('a'); |
|
191 | - foreach ($links as $link) { |
|
192 | - $this->checkPageLink($pageTrack, $link); |
|
193 | - } |
|
194 | - |
|
195 | - // Update content of page based on link fixes / breakages |
|
196 | - $htmlValue->saveHTML(); |
|
197 | - $page->Content = $htmlValue->getContent(); |
|
198 | - try { |
|
199 | - $page->write(); |
|
200 | - } catch (ValidationException $ex) { |
|
201 | - $this->log("Exception caught for {$page->Title}, skipping. Message: " . $ex->getMessage()); |
|
202 | - continue; |
|
203 | - } |
|
204 | - |
|
205 | - // Once all links have been created for this page update HasBrokenLinks |
|
206 | - $count = $pageTrack->BrokenLinks()->count(); |
|
207 | - $this->log("Found {$count} broken links"); |
|
208 | - if ($count) { |
|
209 | - $siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class); |
|
210 | - // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true |
|
211 | - DB::query(sprintf( |
|
212 | - 'UPDATE "%s" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'', |
|
213 | - $siteTreeTable, |
|
214 | - intval($pageTrack->ID) |
|
215 | - )); |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - $status->updateJobInfo('Updating completed pages'); |
|
220 | - $status->updateStatus(); |
|
221 | - return $status; |
|
222 | - } |
|
223 | - |
|
224 | - private function updateCompletedPages($trackID = 0) |
|
225 | - { |
|
226 | - $noPages = BrokenExternalPageTrack::get() |
|
227 | - ->filter(array( |
|
228 | - 'TrackID' => $trackID, |
|
229 | - 'Processed' => 1 |
|
230 | - )) |
|
231 | - ->count(); |
|
232 | - $track = BrokenExternalPageTrackStatus::get_latest(); |
|
233 | - $track->CompletedPages = $noPages; |
|
234 | - $track->write(); |
|
235 | - return $noPages; |
|
236 | - } |
|
237 | - |
|
238 | - private function updateJobInfo($message) |
|
239 | - { |
|
240 | - $track = BrokenExternalPageTrackStatus::get_latest(); |
|
241 | - if ($track) { |
|
242 | - $track->JobInfo = $message; |
|
243 | - $track->write(); |
|
244 | - } |
|
245 | - } |
|
21 | + private static $dependencies = [ |
|
22 | + 'LinkChecker' => '%$' . LinkChecker::class |
|
23 | + ]; |
|
24 | + |
|
25 | + private static $segment = 'CheckExternalLinksTask'; |
|
26 | + |
|
27 | + /** |
|
28 | + * Define a list of HTTP response codes that should not be treated as "broken", where they usually |
|
29 | + * might be. |
|
30 | + * |
|
31 | + * @config |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + private static $ignore_codes = []; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var bool |
|
38 | + */ |
|
39 | + protected $silent = false; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var LinkChecker |
|
43 | + */ |
|
44 | + protected $linkChecker; |
|
45 | + |
|
46 | + protected $title = 'Checking broken External links in the SiteTree'; |
|
47 | + |
|
48 | + protected $description = 'A task that records external broken links in the SiteTree'; |
|
49 | + |
|
50 | + protected $enabled = true; |
|
51 | + |
|
52 | + /** |
|
53 | + * Log a message |
|
54 | + * |
|
55 | + * @param string $message |
|
56 | + */ |
|
57 | + protected function log($message) |
|
58 | + { |
|
59 | + if (!$this->silent) { |
|
60 | + Debug::message($message); |
|
61 | + } |
|
62 | + } |
|
63 | + |
|
64 | + public function run($request) |
|
65 | + { |
|
66 | + $this->runLinksCheck(); |
|
67 | + } |
|
68 | + /** |
|
69 | + * Turn on or off message output |
|
70 | + * |
|
71 | + * @param bool $silent |
|
72 | + */ |
|
73 | + public function setSilent($silent) |
|
74 | + { |
|
75 | + $this->silent = $silent; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param LinkChecker $linkChecker |
|
80 | + */ |
|
81 | + public function setLinkChecker(LinkChecker $linkChecker) |
|
82 | + { |
|
83 | + $this->linkChecker = $linkChecker; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @return LinkChecker |
|
88 | + */ |
|
89 | + public function getLinkChecker() |
|
90 | + { |
|
91 | + return $this->linkChecker; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Check the status of a single link on a page |
|
96 | + * |
|
97 | + * @param BrokenExternalPageTrack $pageTrack |
|
98 | + * @param DOMNode $link |
|
99 | + */ |
|
100 | + protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link) |
|
101 | + { |
|
102 | + $class = $link->getAttribute('class'); |
|
103 | + $href = $link->getAttribute('href'); |
|
104 | + $markedBroken = preg_match('/\b(ss-broken)\b/', $class); |
|
105 | + |
|
106 | + // Check link |
|
107 | + $httpCode = $this->linkChecker->checkLink($href); |
|
108 | + if ($httpCode === null) { |
|
109 | + return; // Null link means uncheckable, such as an internal link |
|
110 | + } |
|
111 | + |
|
112 | + // If this code is broken then mark as such |
|
113 | + if ($foundBroken = $this->isCodeBroken($httpCode)) { |
|
114 | + // Create broken record |
|
115 | + $brokenLink = new BrokenExternalLink(); |
|
116 | + $brokenLink->Link = $href; |
|
117 | + $brokenLink->HTTPCode = $httpCode; |
|
118 | + $brokenLink->TrackID = $pageTrack->ID; |
|
119 | + $brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons |
|
120 | + $brokenLink->write(); |
|
121 | + } |
|
122 | + |
|
123 | + // Check if we need to update CSS class, otherwise return |
|
124 | + if ($markedBroken == $foundBroken) { |
|
125 | + return; |
|
126 | + } |
|
127 | + if ($foundBroken) { |
|
128 | + $class .= ' ss-broken'; |
|
129 | + } else { |
|
130 | + $class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class); |
|
131 | + } |
|
132 | + $link->setAttribute('class', trim($class)); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Determine if the given HTTP code is "broken" |
|
137 | + * |
|
138 | + * @param int $httpCode |
|
139 | + * @return bool True if this is a broken code |
|
140 | + */ |
|
141 | + protected function isCodeBroken($httpCode) |
|
142 | + { |
|
143 | + // Null represents no request attempted |
|
144 | + if ($httpCode === null) { |
|
145 | + return false; |
|
146 | + } |
|
147 | + |
|
148 | + // do we have any whitelisted codes |
|
149 | + $ignoreCodes = $this->config()->get('ignore_codes'); |
|
150 | + if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) { |
|
151 | + return false; |
|
152 | + } |
|
153 | + |
|
154 | + // Check if code is outside valid range |
|
155 | + return $httpCode < 200 || $httpCode > 302; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Runs the links checker and returns the track used |
|
160 | + * |
|
161 | + * @param int $limit Limit to number of pages to run, or null to run all |
|
162 | + * @return BrokenExternalPageTrackStatus |
|
163 | + */ |
|
164 | + public function runLinksCheck($limit = null) |
|
165 | + { |
|
166 | + // Check the current status |
|
167 | + $status = BrokenExternalPageTrackStatus::get_or_create(); |
|
168 | + |
|
169 | + // Calculate pages to run |
|
170 | + $pageTracks = $status->getIncompleteTracks(); |
|
171 | + if ($limit) { |
|
172 | + $pageTracks = $pageTracks->limit($limit); |
|
173 | + } |
|
174 | + |
|
175 | + // Check each page |
|
176 | + foreach ($pageTracks as $pageTrack) { |
|
177 | + // Flag as complete |
|
178 | + $pageTrack->Processed = 1; |
|
179 | + $pageTrack->write(); |
|
180 | + |
|
181 | + // Check value of html area |
|
182 | + $page = $pageTrack->Page(); |
|
183 | + $this->log("Checking {$page->Title}"); |
|
184 | + $htmlValue = Injector::inst()->create('HTMLValue', $page->Content); |
|
185 | + if (!$htmlValue->isValid()) { |
|
186 | + continue; |
|
187 | + } |
|
188 | + |
|
189 | + // Check each link |
|
190 | + $links = $htmlValue->getElementsByTagName('a'); |
|
191 | + foreach ($links as $link) { |
|
192 | + $this->checkPageLink($pageTrack, $link); |
|
193 | + } |
|
194 | + |
|
195 | + // Update content of page based on link fixes / breakages |
|
196 | + $htmlValue->saveHTML(); |
|
197 | + $page->Content = $htmlValue->getContent(); |
|
198 | + try { |
|
199 | + $page->write(); |
|
200 | + } catch (ValidationException $ex) { |
|
201 | + $this->log("Exception caught for {$page->Title}, skipping. Message: " . $ex->getMessage()); |
|
202 | + continue; |
|
203 | + } |
|
204 | + |
|
205 | + // Once all links have been created for this page update HasBrokenLinks |
|
206 | + $count = $pageTrack->BrokenLinks()->count(); |
|
207 | + $this->log("Found {$count} broken links"); |
|
208 | + if ($count) { |
|
209 | + $siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class); |
|
210 | + // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true |
|
211 | + DB::query(sprintf( |
|
212 | + 'UPDATE "%s" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'', |
|
213 | + $siteTreeTable, |
|
214 | + intval($pageTrack->ID) |
|
215 | + )); |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + $status->updateJobInfo('Updating completed pages'); |
|
220 | + $status->updateStatus(); |
|
221 | + return $status; |
|
222 | + } |
|
223 | + |
|
224 | + private function updateCompletedPages($trackID = 0) |
|
225 | + { |
|
226 | + $noPages = BrokenExternalPageTrack::get() |
|
227 | + ->filter(array( |
|
228 | + 'TrackID' => $trackID, |
|
229 | + 'Processed' => 1 |
|
230 | + )) |
|
231 | + ->count(); |
|
232 | + $track = BrokenExternalPageTrackStatus::get_latest(); |
|
233 | + $track->CompletedPages = $noPages; |
|
234 | + $track->write(); |
|
235 | + return $noPages; |
|
236 | + } |
|
237 | + |
|
238 | + private function updateJobInfo($message) |
|
239 | + { |
|
240 | + $track = BrokenExternalPageTrackStatus::get_latest(); |
|
241 | + if ($track) { |
|
242 | + $track->JobInfo = $message; |
|
243 | + $track->write(); |
|
244 | + } |
|
245 | + } |
|
246 | 246 | } |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | class CheckExternalLinksTask extends BuildTask |
20 | 20 | { |
21 | 21 | private static $dependencies = [ |
22 | - 'LinkChecker' => '%$' . LinkChecker::class |
|
22 | + 'LinkChecker' => '%$'.LinkChecker::class |
|
23 | 23 | ]; |
24 | 24 | |
25 | 25 | private static $segment = 'CheckExternalLinksTask'; |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | try { |
199 | 199 | $page->write(); |
200 | 200 | } catch (ValidationException $ex) { |
201 | - $this->log("Exception caught for {$page->Title}, skipping. Message: " . $ex->getMessage()); |
|
201 | + $this->log("Exception caught for {$page->Title}, skipping. Message: ".$ex->getMessage()); |
|
202 | 202 | continue; |
203 | 203 | } |
204 | 204 |
@@ -17,153 +17,153 @@ |
||
17 | 17 | */ |
18 | 18 | class BrokenExternalPageTrackStatus extends DataObject implements i18nEntityProvider |
19 | 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 | - } |
|
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 | 169 | } |
@@ -49,9 +49,9 @@ |
||
49 | 49 | public function provideI18nEntities() |
50 | 50 | { |
51 | 51 | return [ |
52 | - __CLASS__ . '.SINGULARNAME' => 'Broken External Page Track Status', |
|
53 | - __CLASS__ . '.PLURALNAME' => 'Broken External Page Track Statuses', |
|
54 | - __CLASS__ . '.PLURALS' => [ |
|
52 | + __CLASS__.'.SINGULARNAME' => 'Broken External Page Track Status', |
|
53 | + __CLASS__.'.PLURALNAME' => 'Broken External Page Track Statuses', |
|
54 | + __CLASS__.'.PLURALS' => [ |
|
55 | 55 | 'one' => 'A Broken External Page Track Status', |
56 | 56 | 'other' => '{count} Broken External Page Track Statuses', |
57 | 57 | ], |
@@ -7,29 +7,29 @@ |
||
7 | 7 | |
8 | 8 | class BrokenExternalLinkTest extends SapphireTest |
9 | 9 | { |
10 | - /** |
|
11 | - * @param int $httpCode |
|
12 | - * @param string $expected |
|
13 | - * @dataProvider httpCodeProvider |
|
14 | - */ |
|
15 | - public function testGetHTTPCodeDescription($httpCode, $expected) |
|
16 | - { |
|
17 | - $link = new BrokenExternalLink(); |
|
18 | - $link->HTTPCode = $httpCode; |
|
19 | - $this->assertSame($expected, $link->getHTTPCodeDescription()); |
|
20 | - } |
|
10 | + /** |
|
11 | + * @param int $httpCode |
|
12 | + * @param string $expected |
|
13 | + * @dataProvider httpCodeProvider |
|
14 | + */ |
|
15 | + public function testGetHTTPCodeDescription($httpCode, $expected) |
|
16 | + { |
|
17 | + $link = new BrokenExternalLink(); |
|
18 | + $link->HTTPCode = $httpCode; |
|
19 | + $this->assertSame($expected, $link->getHTTPCodeDescription()); |
|
20 | + } |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return array[] |
|
24 | - */ |
|
25 | - public function httpCodeProvider() |
|
26 | - { |
|
27 | - return [ |
|
28 | - [200, '200 (OK)'], |
|
29 | - [302, '302 (Found)'], |
|
30 | - [404, '404 (Not Found)'], |
|
31 | - [500, '500 (Internal Server Error)'], |
|
32 | - [789, '789 (Unknown Response Code)'], |
|
33 | - ]; |
|
34 | - } |
|
22 | + /** |
|
23 | + * @return array[] |
|
24 | + */ |
|
25 | + public function httpCodeProvider() |
|
26 | + { |
|
27 | + return [ |
|
28 | + [200, '200 (OK)'], |
|
29 | + [302, '302 (Found)'], |
|
30 | + [404, '404 (Not Found)'], |
|
31 | + [500, '500 (Internal Server Error)'], |
|
32 | + [789, '789 (Unknown Response Code)'], |
|
33 | + ]; |
|
34 | + } |
|
35 | 35 | } |