@@ -5,24 +5,24 @@ |
||
| 5 | 5 | |
| 6 | 6 | class PretendLinkChecker implements LinkChecker |
| 7 | 7 | { |
| 8 | - public function checkLink($href) |
|
| 9 | - { |
|
| 10 | - switch ($href) { |
|
| 11 | - case 'http://www.working.com': |
|
| 12 | - return 200; |
|
| 13 | - case 'http://www.broken.com': |
|
| 14 | - return 403; |
|
| 15 | - case 'http://www.nodomain.com': |
|
| 16 | - return 0; |
|
| 17 | - case '/internal/link': |
|
| 18 | - case '[sitetree_link,id=9999]': |
|
| 19 | - case 'home': |
|
| 20 | - case 'broken-internal': |
|
| 21 | - case '[sitetree_link,id=1]': |
|
| 22 | - return null; |
|
| 23 | - case 'http://www.broken.com/url/thing': |
|
| 24 | - default: |
|
| 25 | - return 404; |
|
| 26 | - } |
|
| 27 | - } |
|
| 8 | + public function checkLink($href) |
|
| 9 | + { |
|
| 10 | + switch ($href) { |
|
| 11 | + case 'http://www.working.com': |
|
| 12 | + return 200; |
|
| 13 | + case 'http://www.broken.com': |
|
| 14 | + return 403; |
|
| 15 | + case 'http://www.nodomain.com': |
|
| 16 | + return 0; |
|
| 17 | + case '/internal/link': |
|
| 18 | + case '[sitetree_link,id=9999]': |
|
| 19 | + case 'home': |
|
| 20 | + case 'broken-internal': |
|
| 21 | + case '[sitetree_link,id=1]': |
|
| 22 | + return null; |
|
| 23 | + case 'http://www.broken.com/url/thing': |
|
| 24 | + default: |
|
| 25 | + return 404; |
|
| 26 | + } |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -16,95 +16,95 @@ |
||
| 16 | 16 | class ExternalLinksTest extends SapphireTest |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - protected static $fixture_file = 'ExternalLinksTest.yml'; |
|
| 19 | + protected static $fixture_file = 'ExternalLinksTest.yml'; |
|
| 20 | 20 | |
| 21 | - protected static $extra_dataobjects = array( |
|
| 22 | - ExternalLinksTestPage::class |
|
| 23 | - ); |
|
| 21 | + protected static $extra_dataobjects = array( |
|
| 22 | + ExternalLinksTestPage::class |
|
| 23 | + ); |
|
| 24 | 24 | |
| 25 | - protected function setUp() |
|
| 26 | - { |
|
| 27 | - parent::setUp(); |
|
| 25 | + protected function setUp() |
|
| 26 | + { |
|
| 27 | + parent::setUp(); |
|
| 28 | 28 | |
| 29 | - // Stub link checker |
|
| 30 | - $checker = new PretendLinkChecker; |
|
| 31 | - Injector::inst()->registerService($checker, LinkChecker::class); |
|
| 32 | - } |
|
| 29 | + // Stub link checker |
|
| 30 | + $checker = new PretendLinkChecker; |
|
| 31 | + Injector::inst()->registerService($checker, LinkChecker::class); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public function testLinks() |
|
| 35 | - { |
|
| 36 | - // Run link checker |
|
| 37 | - $task = CheckExternalLinksTask::create(); |
|
| 38 | - $task->setSilent(true); // Be quiet during the test! |
|
| 39 | - $task->runLinksCheck(); |
|
| 34 | + public function testLinks() |
|
| 35 | + { |
|
| 36 | + // Run link checker |
|
| 37 | + $task = CheckExternalLinksTask::create(); |
|
| 38 | + $task->setSilent(true); // Be quiet during the test! |
|
| 39 | + $task->runLinksCheck(); |
|
| 40 | 40 | |
| 41 | - // Get all links checked |
|
| 42 | - $status = BrokenExternalPageTrackStatus::get_latest(); |
|
| 43 | - $this->assertEquals('Completed', $status->Status); |
|
| 44 | - $this->assertEquals(5, $status->TotalPages); |
|
| 45 | - $this->assertEquals(5, $status->CompletedPages); |
|
| 41 | + // Get all links checked |
|
| 42 | + $status = BrokenExternalPageTrackStatus::get_latest(); |
|
| 43 | + $this->assertEquals('Completed', $status->Status); |
|
| 44 | + $this->assertEquals(5, $status->TotalPages); |
|
| 45 | + $this->assertEquals(5, $status->CompletedPages); |
|
| 46 | 46 | |
| 47 | - // Check all pages have had the correct HTML adjusted |
|
| 48 | - for ($i = 1; $i <= 5; $i++) { |
|
| 49 | - $page = $this->objFromFixture(ExternalLinksTestPage::class, 'page'.$i); |
|
| 50 | - $this->assertNotEmpty($page->Content); |
|
| 51 | - $this->assertEquals( |
|
| 52 | - $page->ExpectedContent, |
|
| 53 | - $page->Content, |
|
| 54 | - "Assert that the content of page{$i} has been updated" |
|
| 55 | - ); |
|
| 56 | - } |
|
| 47 | + // Check all pages have had the correct HTML adjusted |
|
| 48 | + for ($i = 1; $i <= 5; $i++) { |
|
| 49 | + $page = $this->objFromFixture(ExternalLinksTestPage::class, 'page'.$i); |
|
| 50 | + $this->assertNotEmpty($page->Content); |
|
| 51 | + $this->assertEquals( |
|
| 52 | + $page->ExpectedContent, |
|
| 53 | + $page->Content, |
|
| 54 | + "Assert that the content of page{$i} has been updated" |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - // Check that the correct report of broken links is generated |
|
| 59 | - $links = $status |
|
| 60 | - ->BrokenLinks() |
|
| 61 | - ->sort('Link'); |
|
| 58 | + // Check that the correct report of broken links is generated |
|
| 59 | + $links = $status |
|
| 60 | + ->BrokenLinks() |
|
| 61 | + ->sort('Link'); |
|
| 62 | 62 | |
| 63 | - $this->assertEquals(4, $links->count()); |
|
| 64 | - $this->assertEquals( |
|
| 65 | - array( |
|
| 66 | - 'http://www.broken.com', |
|
| 67 | - 'http://www.broken.com/url/thing', |
|
| 68 | - 'http://www.broken.com/url/thing', |
|
| 69 | - 'http://www.nodomain.com' |
|
| 70 | - ), |
|
| 71 | - array_values($links->map('ID', 'Link')->toArray()) |
|
| 72 | - ); |
|
| 63 | + $this->assertEquals(4, $links->count()); |
|
| 64 | + $this->assertEquals( |
|
| 65 | + array( |
|
| 66 | + 'http://www.broken.com', |
|
| 67 | + 'http://www.broken.com/url/thing', |
|
| 68 | + 'http://www.broken.com/url/thing', |
|
| 69 | + 'http://www.nodomain.com' |
|
| 70 | + ), |
|
| 71 | + array_values($links->map('ID', 'Link')->toArray()) |
|
| 72 | + ); |
|
| 73 | 73 | |
| 74 | - // Check response codes are correct |
|
| 75 | - $expected = array( |
|
| 76 | - 'http://www.broken.com' => 403, |
|
| 77 | - 'http://www.broken.com/url/thing' => 404, |
|
| 78 | - 'http://www.nodomain.com' => 0 |
|
| 79 | - ); |
|
| 80 | - $actual = $links->map('Link', 'HTTPCode')->toArray(); |
|
| 81 | - $this->assertEquals($expected, $actual); |
|
| 74 | + // Check response codes are correct |
|
| 75 | + $expected = array( |
|
| 76 | + 'http://www.broken.com' => 403, |
|
| 77 | + 'http://www.broken.com/url/thing' => 404, |
|
| 78 | + 'http://www.nodomain.com' => 0 |
|
| 79 | + ); |
|
| 80 | + $actual = $links->map('Link', 'HTTPCode')->toArray(); |
|
| 81 | + $this->assertEquals($expected, $actual); |
|
| 82 | 82 | |
| 83 | - // Check response descriptions are correct |
|
| 84 | - i18n::set_locale('en_NZ'); |
|
| 85 | - $expected = array( |
|
| 86 | - 'http://www.broken.com' => '403 (Forbidden)', |
|
| 87 | - 'http://www.broken.com/url/thing' => '404 (Not Found)', |
|
| 88 | - 'http://www.nodomain.com' => '0 (Server Not Available)' |
|
| 89 | - ); |
|
| 90 | - $actual = $links->map('Link', 'HTTPCodeDescription')->toArray(); |
|
| 91 | - $this->assertEquals($expected, $actual); |
|
| 92 | - } |
|
| 83 | + // Check response descriptions are correct |
|
| 84 | + i18n::set_locale('en_NZ'); |
|
| 85 | + $expected = array( |
|
| 86 | + 'http://www.broken.com' => '403 (Forbidden)', |
|
| 87 | + 'http://www.broken.com/url/thing' => '404 (Not Found)', |
|
| 88 | + 'http://www.nodomain.com' => '0 (Server Not Available)' |
|
| 89 | + ); |
|
| 90 | + $actual = $links->map('Link', 'HTTPCodeDescription')->toArray(); |
|
| 91 | + $this->assertEquals($expected, $actual); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Test that broken links appears in the reports list |
|
| 96 | - */ |
|
| 97 | - public function testReportExists() |
|
| 98 | - { |
|
| 99 | - $reports = Report::get_reports(); |
|
| 100 | - $reportNames = array(); |
|
| 101 | - foreach ($reports as $report) { |
|
| 102 | - $reportNames[] = get_class($report); |
|
| 103 | - } |
|
| 104 | - $this->assertContains( |
|
| 105 | - BrokenExternalLinksReport::class, |
|
| 106 | - $reportNames, |
|
| 107 | - 'BrokenExternalLinksReport is in reports list' |
|
| 108 | - ); |
|
| 109 | - } |
|
| 94 | + /** |
|
| 95 | + * Test that broken links appears in the reports list |
|
| 96 | + */ |
|
| 97 | + public function testReportExists() |
|
| 98 | + { |
|
| 99 | + $reports = Report::get_reports(); |
|
| 100 | + $reportNames = array(); |
|
| 101 | + foreach ($reports as $report) { |
|
| 102 | + $reportNames[] = get_class($report); |
|
| 103 | + } |
|
| 104 | + $this->assertContains( |
|
| 105 | + BrokenExternalLinksReport::class, |
|
| 106 | + $reportNames, |
|
| 107 | + 'BrokenExternalLinksReport is in reports list' |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | 110 | } |
@@ -16,211 +16,211 @@ |
||
| 16 | 16 | class CheckExternalLinksTask extends BuildTask |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - private static $dependencies = [ |
|
| 20 | - 'LinkChecker' => '%$' . LinkChecker::class |
|
| 21 | - ]; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var bool |
|
| 25 | - */ |
|
| 26 | - protected $silent = false; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var LinkChecker |
|
| 30 | - */ |
|
| 31 | - protected $linkChecker; |
|
| 32 | - |
|
| 33 | - protected $title = 'Checking broken External links in the SiteTree'; |
|
| 34 | - |
|
| 35 | - protected $description = 'A task that records external broken links in the SiteTree'; |
|
| 36 | - |
|
| 37 | - protected $enabled = true; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Log a message |
|
| 41 | - * |
|
| 42 | - * @param string $message |
|
| 43 | - */ |
|
| 44 | - protected function log($message) |
|
| 45 | - { |
|
| 46 | - if (!$this->silent) { |
|
| 47 | - Debug::message($message); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public function run($request) |
|
| 52 | - { |
|
| 53 | - $this->runLinksCheck(); |
|
| 54 | - } |
|
| 55 | - /** |
|
| 56 | - * Turn on or off message output |
|
| 57 | - * |
|
| 58 | - * @param bool $silent |
|
| 59 | - */ |
|
| 60 | - public function setSilent($silent) |
|
| 61 | - { |
|
| 62 | - $this->silent = $silent; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param LinkChecker $linkChecker |
|
| 67 | - */ |
|
| 68 | - public function setLinkChecker(LinkChecker $linkChecker) |
|
| 69 | - { |
|
| 70 | - $this->linkChecker = $linkChecker; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @return LinkChecker |
|
| 75 | - */ |
|
| 76 | - public function getLinkChecker() |
|
| 77 | - { |
|
| 78 | - return $this->linkChecker; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Check the status of a single link on a page |
|
| 83 | - * |
|
| 84 | - * @param BrokenExternalPageTrack $pageTrack |
|
| 85 | - * @param DOMNode $link |
|
| 86 | - */ |
|
| 87 | - protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link) |
|
| 88 | - { |
|
| 89 | - $class = $link->getAttribute('class'); |
|
| 90 | - $href = $link->getAttribute('href'); |
|
| 91 | - $markedBroken = preg_match('/\b(ss-broken)\b/', $class); |
|
| 92 | - |
|
| 93 | - // Check link |
|
| 94 | - $httpCode = $this->linkChecker->checkLink($href); |
|
| 95 | - if ($httpCode === null) { |
|
| 96 | - return; // Null link means uncheckable, such as an internal link |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // If this code is broken then mark as such |
|
| 100 | - if ($foundBroken = $this->isCodeBroken($httpCode)) { |
|
| 101 | - // Create broken record |
|
| 102 | - $brokenLink = new BrokenExternalLink(); |
|
| 103 | - $brokenLink->Link = $href; |
|
| 104 | - $brokenLink->HTTPCode = $httpCode; |
|
| 105 | - $brokenLink->TrackID = $pageTrack->ID; |
|
| 106 | - $brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons |
|
| 107 | - $brokenLink->write(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Check if we need to update CSS class, otherwise return |
|
| 111 | - if ($markedBroken == $foundBroken) { |
|
| 112 | - return; |
|
| 113 | - } |
|
| 114 | - if ($foundBroken) { |
|
| 115 | - $class .= ' ss-broken'; |
|
| 116 | - } else { |
|
| 117 | - $class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class); |
|
| 118 | - } |
|
| 119 | - $link->setAttribute('class', trim($class)); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Determine if the given HTTP code is "broken" |
|
| 124 | - * |
|
| 125 | - * @param int $httpCode |
|
| 126 | - * @return bool True if this is a broken code |
|
| 127 | - */ |
|
| 128 | - protected function isCodeBroken($httpCode) |
|
| 129 | - { |
|
| 130 | - // Null represents no request attempted |
|
| 131 | - if ($httpCode === null) { |
|
| 132 | - return false; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // do we have any whitelisted codes |
|
| 136 | - $ignoreCodes = $this->config()->get('IgnoreCodes'); |
|
| 137 | - if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) { |
|
| 138 | - return false; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // Check if code is outside valid range |
|
| 142 | - return $httpCode < 200 || $httpCode > 302; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Runs the links checker and returns the track used |
|
| 147 | - * |
|
| 148 | - * @param int $limit Limit to number of pages to run, or null to run all |
|
| 149 | - * @return BrokenExternalPageTrackStatus |
|
| 150 | - */ |
|
| 151 | - public function runLinksCheck($limit = null) |
|
| 152 | - { |
|
| 153 | - // Check the current status |
|
| 154 | - $status = BrokenExternalPageTrackStatus::get_or_create(); |
|
| 155 | - |
|
| 156 | - // Calculate pages to run |
|
| 157 | - $pageTracks = $status->getIncompleteTracks(); |
|
| 158 | - if ($limit) { |
|
| 159 | - $pageTracks = $pageTracks->limit($limit); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - // Check each page |
|
| 163 | - foreach ($pageTracks as $pageTrack) { |
|
| 164 | - // Flag as complete |
|
| 165 | - $pageTrack->Processed = 1; |
|
| 166 | - $pageTrack->write(); |
|
| 167 | - |
|
| 168 | - // Check value of html area |
|
| 169 | - $page = $pageTrack->Page(); |
|
| 170 | - $this->log("Checking {$page->Title}"); |
|
| 171 | - $htmlValue = Injector::inst()->create('HTMLValue', $page->Content); |
|
| 172 | - if (!$htmlValue->isValid()) { |
|
| 173 | - continue; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // Check each link |
|
| 177 | - $links = $htmlValue->getElementsByTagName('a'); |
|
| 178 | - foreach ($links as $link) { |
|
| 179 | - $this->checkPageLink($pageTrack, $link); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - // Update content of page based on link fixes / breakages |
|
| 183 | - $htmlValue->saveHTML(); |
|
| 184 | - $page->Content = $htmlValue->getContent(); |
|
| 185 | - $page->write(); |
|
| 186 | - |
|
| 187 | - // Once all links have been created for this page update HasBrokenLinks |
|
| 188 | - $count = $pageTrack->BrokenLinks()->count(); |
|
| 189 | - $this->log("Found {$count} broken links"); |
|
| 190 | - if ($count) { |
|
| 191 | - // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true |
|
| 192 | - DB::query(sprintf( |
|
| 193 | - 'UPDATE "SiteTree" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'', |
|
| 194 | - intval($pageTrack->ID) |
|
| 195 | - )); |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - $status->updateJobInfo('Updating completed pages'); |
|
| 200 | - $status->updateStatus(); |
|
| 201 | - return $status; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - private function updateCompletedPages($trackID = 0) |
|
| 205 | - { |
|
| 206 | - $noPages = BrokenExternalPageTrack::get() |
|
| 207 | - ->filter(array( |
|
| 208 | - 'TrackID' => $trackID, |
|
| 209 | - 'Processed' => 1 |
|
| 210 | - )) |
|
| 211 | - ->count(); |
|
| 212 | - $track = BrokenExternalPageTrackStatus::get_latest(); |
|
| 213 | - $track->CompletedPages = $noPages; |
|
| 214 | - $track->write(); |
|
| 215 | - return $noPages; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - private function updateJobInfo($message) |
|
| 219 | - { |
|
| 220 | - $track = BrokenExternalPageTrackStatus::get_latest(); |
|
| 221 | - if ($track) { |
|
| 222 | - $track->JobInfo = $message; |
|
| 223 | - $track->write(); |
|
| 224 | - } |
|
| 225 | - } |
|
| 19 | + private static $dependencies = [ |
|
| 20 | + 'LinkChecker' => '%$' . LinkChecker::class |
|
| 21 | + ]; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var bool |
|
| 25 | + */ |
|
| 26 | + protected $silent = false; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var LinkChecker |
|
| 30 | + */ |
|
| 31 | + protected $linkChecker; |
|
| 32 | + |
|
| 33 | + protected $title = 'Checking broken External links in the SiteTree'; |
|
| 34 | + |
|
| 35 | + protected $description = 'A task that records external broken links in the SiteTree'; |
|
| 36 | + |
|
| 37 | + protected $enabled = true; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Log a message |
|
| 41 | + * |
|
| 42 | + * @param string $message |
|
| 43 | + */ |
|
| 44 | + protected function log($message) |
|
| 45 | + { |
|
| 46 | + if (!$this->silent) { |
|
| 47 | + Debug::message($message); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public function run($request) |
|
| 52 | + { |
|
| 53 | + $this->runLinksCheck(); |
|
| 54 | + } |
|
| 55 | + /** |
|
| 56 | + * Turn on or off message output |
|
| 57 | + * |
|
| 58 | + * @param bool $silent |
|
| 59 | + */ |
|
| 60 | + public function setSilent($silent) |
|
| 61 | + { |
|
| 62 | + $this->silent = $silent; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param LinkChecker $linkChecker |
|
| 67 | + */ |
|
| 68 | + public function setLinkChecker(LinkChecker $linkChecker) |
|
| 69 | + { |
|
| 70 | + $this->linkChecker = $linkChecker; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @return LinkChecker |
|
| 75 | + */ |
|
| 76 | + public function getLinkChecker() |
|
| 77 | + { |
|
| 78 | + return $this->linkChecker; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Check the status of a single link on a page |
|
| 83 | + * |
|
| 84 | + * @param BrokenExternalPageTrack $pageTrack |
|
| 85 | + * @param DOMNode $link |
|
| 86 | + */ |
|
| 87 | + protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link) |
|
| 88 | + { |
|
| 89 | + $class = $link->getAttribute('class'); |
|
| 90 | + $href = $link->getAttribute('href'); |
|
| 91 | + $markedBroken = preg_match('/\b(ss-broken)\b/', $class); |
|
| 92 | + |
|
| 93 | + // Check link |
|
| 94 | + $httpCode = $this->linkChecker->checkLink($href); |
|
| 95 | + if ($httpCode === null) { |
|
| 96 | + return; // Null link means uncheckable, such as an internal link |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // If this code is broken then mark as such |
|
| 100 | + if ($foundBroken = $this->isCodeBroken($httpCode)) { |
|
| 101 | + // Create broken record |
|
| 102 | + $brokenLink = new BrokenExternalLink(); |
|
| 103 | + $brokenLink->Link = $href; |
|
| 104 | + $brokenLink->HTTPCode = $httpCode; |
|
| 105 | + $brokenLink->TrackID = $pageTrack->ID; |
|
| 106 | + $brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons |
|
| 107 | + $brokenLink->write(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Check if we need to update CSS class, otherwise return |
|
| 111 | + if ($markedBroken == $foundBroken) { |
|
| 112 | + return; |
|
| 113 | + } |
|
| 114 | + if ($foundBroken) { |
|
| 115 | + $class .= ' ss-broken'; |
|
| 116 | + } else { |
|
| 117 | + $class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class); |
|
| 118 | + } |
|
| 119 | + $link->setAttribute('class', trim($class)); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Determine if the given HTTP code is "broken" |
|
| 124 | + * |
|
| 125 | + * @param int $httpCode |
|
| 126 | + * @return bool True if this is a broken code |
|
| 127 | + */ |
|
| 128 | + protected function isCodeBroken($httpCode) |
|
| 129 | + { |
|
| 130 | + // Null represents no request attempted |
|
| 131 | + if ($httpCode === null) { |
|
| 132 | + return false; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // do we have any whitelisted codes |
|
| 136 | + $ignoreCodes = $this->config()->get('IgnoreCodes'); |
|
| 137 | + if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) { |
|
| 138 | + return false; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // Check if code is outside valid range |
|
| 142 | + return $httpCode < 200 || $httpCode > 302; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Runs the links checker and returns the track used |
|
| 147 | + * |
|
| 148 | + * @param int $limit Limit to number of pages to run, or null to run all |
|
| 149 | + * @return BrokenExternalPageTrackStatus |
|
| 150 | + */ |
|
| 151 | + public function runLinksCheck($limit = null) |
|
| 152 | + { |
|
| 153 | + // Check the current status |
|
| 154 | + $status = BrokenExternalPageTrackStatus::get_or_create(); |
|
| 155 | + |
|
| 156 | + // Calculate pages to run |
|
| 157 | + $pageTracks = $status->getIncompleteTracks(); |
|
| 158 | + if ($limit) { |
|
| 159 | + $pageTracks = $pageTracks->limit($limit); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + // Check each page |
|
| 163 | + foreach ($pageTracks as $pageTrack) { |
|
| 164 | + // Flag as complete |
|
| 165 | + $pageTrack->Processed = 1; |
|
| 166 | + $pageTrack->write(); |
|
| 167 | + |
|
| 168 | + // Check value of html area |
|
| 169 | + $page = $pageTrack->Page(); |
|
| 170 | + $this->log("Checking {$page->Title}"); |
|
| 171 | + $htmlValue = Injector::inst()->create('HTMLValue', $page->Content); |
|
| 172 | + if (!$htmlValue->isValid()) { |
|
| 173 | + continue; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // Check each link |
|
| 177 | + $links = $htmlValue->getElementsByTagName('a'); |
|
| 178 | + foreach ($links as $link) { |
|
| 179 | + $this->checkPageLink($pageTrack, $link); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + // Update content of page based on link fixes / breakages |
|
| 183 | + $htmlValue->saveHTML(); |
|
| 184 | + $page->Content = $htmlValue->getContent(); |
|
| 185 | + $page->write(); |
|
| 186 | + |
|
| 187 | + // Once all links have been created for this page update HasBrokenLinks |
|
| 188 | + $count = $pageTrack->BrokenLinks()->count(); |
|
| 189 | + $this->log("Found {$count} broken links"); |
|
| 190 | + if ($count) { |
|
| 191 | + // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true |
|
| 192 | + DB::query(sprintf( |
|
| 193 | + 'UPDATE "SiteTree" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'', |
|
| 194 | + intval($pageTrack->ID) |
|
| 195 | + )); |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + $status->updateJobInfo('Updating completed pages'); |
|
| 200 | + $status->updateStatus(); |
|
| 201 | + return $status; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + private function updateCompletedPages($trackID = 0) |
|
| 205 | + { |
|
| 206 | + $noPages = BrokenExternalPageTrack::get() |
|
| 207 | + ->filter(array( |
|
| 208 | + 'TrackID' => $trackID, |
|
| 209 | + 'Processed' => 1 |
|
| 210 | + )) |
|
| 211 | + ->count(); |
|
| 212 | + $track = BrokenExternalPageTrackStatus::get_latest(); |
|
| 213 | + $track->CompletedPages = $noPages; |
|
| 214 | + $track->write(); |
|
| 215 | + return $noPages; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + private function updateJobInfo($message) |
|
| 219 | + { |
|
| 220 | + $track = BrokenExternalPageTrackStatus::get_latest(); |
|
| 221 | + if ($track) { |
|
| 222 | + $track->JobInfo = $message; |
|
| 223 | + $track->write(); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | 226 | } |
@@ -17,7 +17,7 @@ |
||
| 17 | 17 | { |
| 18 | 18 | |
| 19 | 19 | private static $dependencies = [ |
| 20 | - 'LinkChecker' => '%$' . LinkChecker::class |
|
| 20 | + 'LinkChecker' => '%$'.LinkChecker::class |
|
| 21 | 21 | ]; |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -18,85 +18,85 @@ |
||
| 18 | 18 | class BrokenExternalLinksReport extends Report |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Returns the report title |
|
| 23 | - * |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - public function title() |
|
| 27 | - { |
|
| 28 | - return _t(__CLASS__ . '.EXTERNALBROKENLINKS', "External broken links report"); |
|
| 29 | - } |
|
| 21 | + /** |
|
| 22 | + * Returns the report title |
|
| 23 | + * |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + public function title() |
|
| 27 | + { |
|
| 28 | + return _t(__CLASS__ . '.EXTERNALBROKENLINKS', "External broken links report"); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - public function columns() |
|
| 32 | - { |
|
| 33 | - return array( |
|
| 34 | - "Created" => "Checked", |
|
| 35 | - 'Link' => array( |
|
| 36 | - 'title' => 'External Link', |
|
| 37 | - 'formatting' => function ($value, $item) { |
|
| 38 | - return sprintf( |
|
| 39 | - '<a target="_blank" href="%s">%s</a>', |
|
| 40 | - Convert::raw2att($item->Link), |
|
| 41 | - Convert::raw2xml($item->Link) |
|
| 42 | - ); |
|
| 43 | - } |
|
| 44 | - ), |
|
| 45 | - 'HTTPCodeDescription' => 'HTTP Error Code', |
|
| 46 | - "Title" => array( |
|
| 47 | - "title" => 'Page link is on', |
|
| 48 | - 'formatting' => function ($value, $item) { |
|
| 49 | - $page = $item->Page(); |
|
| 50 | - return sprintf( |
|
| 51 | - '<a href="%s">%s</a>', |
|
| 52 | - Convert::raw2att($page->CMSEditLink()), |
|
| 53 | - Convert::raw2xml($page->Title) |
|
| 54 | - ); |
|
| 55 | - } |
|
| 56 | - ) |
|
| 57 | - ); |
|
| 58 | - } |
|
| 31 | + public function columns() |
|
| 32 | + { |
|
| 33 | + return array( |
|
| 34 | + "Created" => "Checked", |
|
| 35 | + 'Link' => array( |
|
| 36 | + 'title' => 'External Link', |
|
| 37 | + 'formatting' => function ($value, $item) { |
|
| 38 | + return sprintf( |
|
| 39 | + '<a target="_blank" href="%s">%s</a>', |
|
| 40 | + Convert::raw2att($item->Link), |
|
| 41 | + Convert::raw2xml($item->Link) |
|
| 42 | + ); |
|
| 43 | + } |
|
| 44 | + ), |
|
| 45 | + 'HTTPCodeDescription' => 'HTTP Error Code', |
|
| 46 | + "Title" => array( |
|
| 47 | + "title" => 'Page link is on', |
|
| 48 | + 'formatting' => function ($value, $item) { |
|
| 49 | + $page = $item->Page(); |
|
| 50 | + return sprintf( |
|
| 51 | + '<a href="%s">%s</a>', |
|
| 52 | + Convert::raw2att($page->CMSEditLink()), |
|
| 53 | + Convert::raw2xml($page->Title) |
|
| 54 | + ); |
|
| 55 | + } |
|
| 56 | + ) |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Alias of columns(), to support the export to csv action |
|
| 62 | - * in {@link GridFieldExportButton} generateExportFileData method. |
|
| 63 | - * @return array |
|
| 64 | - */ |
|
| 65 | - public function getColumns() |
|
| 66 | - { |
|
| 67 | - return $this->columns(); |
|
| 68 | - } |
|
| 60 | + /** |
|
| 61 | + * Alias of columns(), to support the export to csv action |
|
| 62 | + * in {@link GridFieldExportButton} generateExportFileData method. |
|
| 63 | + * @return array |
|
| 64 | + */ |
|
| 65 | + public function getColumns() |
|
| 66 | + { |
|
| 67 | + return $this->columns(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - public function sourceRecords() |
|
| 71 | - { |
|
| 72 | - $track = BrokenExternalPageTrackStatus::get_latest(); |
|
| 73 | - if ($track) { |
|
| 74 | - return $track->BrokenLinks(); |
|
| 75 | - } |
|
| 76 | - return ArrayList::create(); |
|
| 77 | - } |
|
| 70 | + public function sourceRecords() |
|
| 71 | + { |
|
| 72 | + $track = BrokenExternalPageTrackStatus::get_latest(); |
|
| 73 | + if ($track) { |
|
| 74 | + return $track->BrokenLinks(); |
|
| 75 | + } |
|
| 76 | + return ArrayList::create(); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - public function getCMSFields() |
|
| 80 | - { |
|
| 81 | - Requirements::javascript('silverstripe/externallinks: javascript/BrokenExternalLinksReport.js'); |
|
| 82 | - $fields = parent::getCMSFields(); |
|
| 79 | + public function getCMSFields() |
|
| 80 | + { |
|
| 81 | + Requirements::javascript('silverstripe/externallinks: javascript/BrokenExternalLinksReport.js'); |
|
| 82 | + $fields = parent::getCMSFields(); |
|
| 83 | 83 | |
| 84 | - $reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>'; |
|
| 85 | - $reportResult = LiteralField::create('ResultTitle', $reportResultSpan); |
|
| 86 | - $fields->push($reportResult); |
|
| 84 | + $reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>'; |
|
| 85 | + $reportResult = LiteralField::create('ResultTitle', $reportResultSpan); |
|
| 86 | + $fields->push($reportResult); |
|
| 87 | 87 | |
| 88 | - $button = HTML::createTag( |
|
| 89 | - 'button', |
|
| 90 | - [ |
|
| 91 | - 'id' => 'externalLinksReport', |
|
| 92 | - 'type' => 'button', |
|
| 93 | - 'class' => 'btn btn-primary' |
|
| 94 | - ], |
|
| 95 | - _t(__CLASS__ . '.RUNREPORT', 'Create new report') |
|
| 96 | - ); |
|
| 97 | - $runReportButton = LiteralField::create('runReport', $button); |
|
| 98 | - $fields->push($runReportButton); |
|
| 88 | + $button = HTML::createTag( |
|
| 89 | + 'button', |
|
| 90 | + [ |
|
| 91 | + 'id' => 'externalLinksReport', |
|
| 92 | + 'type' => 'button', |
|
| 93 | + 'class' => 'btn btn-primary' |
|
| 94 | + ], |
|
| 95 | + _t(__CLASS__ . '.RUNREPORT', 'Create new report') |
|
| 96 | + ); |
|
| 97 | + $runReportButton = LiteralField::create('runReport', $button); |
|
| 98 | + $fields->push($runReportButton); |
|
| 99 | 99 | |
| 100 | - return $fields; |
|
| 101 | - } |
|
| 100 | + return $fields; |
|
| 101 | + } |
|
| 102 | 102 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | public function title() |
| 27 | 27 | { |
| 28 | - return _t(__CLASS__ . '.EXTERNALBROKENLINKS', "External broken links report"); |
|
| 28 | + return _t(__CLASS__.'.EXTERNALBROKENLINKS', "External broken links report"); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | public function columns() |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | "Created" => "Checked", |
| 35 | 35 | 'Link' => array( |
| 36 | 36 | 'title' => 'External Link', |
| 37 | - 'formatting' => function ($value, $item) { |
|
| 37 | + 'formatting' => function($value, $item) { |
|
| 38 | 38 | return sprintf( |
| 39 | 39 | '<a target="_blank" href="%s">%s</a>', |
| 40 | 40 | Convert::raw2att($item->Link), |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | 'HTTPCodeDescription' => 'HTTP Error Code', |
| 46 | 46 | "Title" => array( |
| 47 | 47 | "title" => 'Page link is on', |
| 48 | - 'formatting' => function ($value, $item) { |
|
| 48 | + 'formatting' => function($value, $item) { |
|
| 49 | 49 | $page = $item->Page(); |
| 50 | 50 | return sprintf( |
| 51 | 51 | '<a href="%s">%s</a>', |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | 'type' => 'button', |
| 93 | 93 | 'class' => 'btn btn-primary' |
| 94 | 94 | ], |
| 95 | - _t(__CLASS__ . '.RUNREPORT', 'Create new report') |
|
| 95 | + _t(__CLASS__.'.RUNREPORT', 'Create new report') |
|
| 96 | 96 | ); |
| 97 | 97 | $runReportButton = LiteralField::create('runReport', $button); |
| 98 | 98 | $fields->push($runReportButton); |
@@ -18,136 +18,136 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class BrokenExternalPageTrackStatus extends DataObject |
| 20 | 20 | { |
| 21 | - private static $table_name = 'BrokenExternalPageTrackStatus'; |
|
| 22 | - |
|
| 23 | - private static $db = array( |
|
| 24 | - 'Status' => 'Enum("Completed, Running", "Running")', |
|
| 25 | - 'JobInfo' => 'Varchar(255)' |
|
| 26 | - ); |
|
| 27 | - |
|
| 28 | - private static $has_many = array( |
|
| 29 | - 'TrackedPages' => BrokenExternalPageTrack::class, |
|
| 30 | - 'BrokenLinks' => BrokenExternalLink::class |
|
| 31 | - ); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Get the latest track status |
|
| 35 | - * |
|
| 36 | - * @return BrokenExternalPageTrackStatus |
|
| 37 | - */ |
|
| 38 | - public static function get_latest() |
|
| 39 | - { |
|
| 40 | - return self::get() |
|
| 41 | - ->sort('ID', 'DESC') |
|
| 42 | - ->first(); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Gets the list of Pages yet to be checked |
|
| 47 | - * |
|
| 48 | - * @return DataList |
|
| 49 | - */ |
|
| 50 | - public function getIncompletePageList() |
|
| 51 | - { |
|
| 52 | - $pageIDs = $this |
|
| 53 | - ->getIncompleteTracks() |
|
| 54 | - ->column('PageID'); |
|
| 55 | - if ($pageIDs) { |
|
| 56 | - return Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
| 57 | - ->byIDs($pageIDs); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Get the list of incomplete BrokenExternalPageTrack |
|
| 63 | - * |
|
| 64 | - * @return DataList |
|
| 65 | - */ |
|
| 66 | - public function getIncompleteTracks() |
|
| 67 | - { |
|
| 68 | - return $this |
|
| 69 | - ->TrackedPages() |
|
| 70 | - ->filter('Processed', 0); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Get total pages count |
|
| 75 | - * |
|
| 76 | - * @return int |
|
| 77 | - */ |
|
| 78 | - public function getTotalPages() |
|
| 79 | - { |
|
| 80 | - return $this->TrackedPages()->count(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Get completed pages count |
|
| 85 | - * |
|
| 86 | - * @return int |
|
| 87 | - */ |
|
| 88 | - public function getCompletedPages() |
|
| 89 | - { |
|
| 90 | - return $this |
|
| 91 | - ->TrackedPages() |
|
| 92 | - ->filter('Processed', 1) |
|
| 93 | - ->count(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Returns the latest run, or otherwise creates a new one |
|
| 98 | - * |
|
| 99 | - * @return BrokenExternalPageTrackStatus |
|
| 100 | - */ |
|
| 101 | - public static function get_or_create() |
|
| 102 | - { |
|
| 103 | - // Check the current status |
|
| 104 | - $status = self::get_latest(); |
|
| 105 | - if ($status && $status->Status == 'Running') { |
|
| 106 | - $status->updateStatus(); |
|
| 107 | - return $status; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - return self::create_status(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Create and prepare a new status |
|
| 115 | - * |
|
| 116 | - * @return BrokenExternalPageTrackStatus |
|
| 117 | - */ |
|
| 118 | - public static function create_status() |
|
| 119 | - { |
|
| 120 | - // If the script is to be started create a new status |
|
| 121 | - $status = self::create(); |
|
| 122 | - $status->updateJobInfo('Creating new tracking object'); |
|
| 123 | - |
|
| 124 | - // Setup all pages to test |
|
| 125 | - $pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
| 126 | - ->column('ID'); |
|
| 127 | - foreach ($pageIDs as $pageID) { |
|
| 128 | - $trackPage = BrokenExternalPageTrack::create(); |
|
| 129 | - $trackPage->PageID = $pageID; |
|
| 130 | - $trackPage->StatusID = $status->ID; |
|
| 131 | - $trackPage->write(); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $status; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - public function updateJobInfo($message) |
|
| 138 | - { |
|
| 139 | - $this->JobInfo = $message; |
|
| 140 | - $this->write(); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Self check status |
|
| 145 | - */ |
|
| 146 | - public function updateStatus() |
|
| 147 | - { |
|
| 148 | - if ($this->CompletedPages == $this->TotalPages) { |
|
| 149 | - $this->Status = 'Completed'; |
|
| 150 | - $this->updateJobInfo('Setting to completed'); |
|
| 151 | - } |
|
| 152 | - } |
|
| 21 | + private static $table_name = 'BrokenExternalPageTrackStatus'; |
|
| 22 | + |
|
| 23 | + private static $db = array( |
|
| 24 | + 'Status' => 'Enum("Completed, Running", "Running")', |
|
| 25 | + 'JobInfo' => 'Varchar(255)' |
|
| 26 | + ); |
|
| 27 | + |
|
| 28 | + private static $has_many = array( |
|
| 29 | + 'TrackedPages' => BrokenExternalPageTrack::class, |
|
| 30 | + 'BrokenLinks' => BrokenExternalLink::class |
|
| 31 | + ); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Get the latest track status |
|
| 35 | + * |
|
| 36 | + * @return BrokenExternalPageTrackStatus |
|
| 37 | + */ |
|
| 38 | + public static function get_latest() |
|
| 39 | + { |
|
| 40 | + return self::get() |
|
| 41 | + ->sort('ID', 'DESC') |
|
| 42 | + ->first(); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Gets the list of Pages yet to be checked |
|
| 47 | + * |
|
| 48 | + * @return DataList |
|
| 49 | + */ |
|
| 50 | + public function getIncompletePageList() |
|
| 51 | + { |
|
| 52 | + $pageIDs = $this |
|
| 53 | + ->getIncompleteTracks() |
|
| 54 | + ->column('PageID'); |
|
| 55 | + if ($pageIDs) { |
|
| 56 | + return Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
| 57 | + ->byIDs($pageIDs); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Get the list of incomplete BrokenExternalPageTrack |
|
| 63 | + * |
|
| 64 | + * @return DataList |
|
| 65 | + */ |
|
| 66 | + public function getIncompleteTracks() |
|
| 67 | + { |
|
| 68 | + return $this |
|
| 69 | + ->TrackedPages() |
|
| 70 | + ->filter('Processed', 0); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Get total pages count |
|
| 75 | + * |
|
| 76 | + * @return int |
|
| 77 | + */ |
|
| 78 | + public function getTotalPages() |
|
| 79 | + { |
|
| 80 | + return $this->TrackedPages()->count(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Get completed pages count |
|
| 85 | + * |
|
| 86 | + * @return int |
|
| 87 | + */ |
|
| 88 | + public function getCompletedPages() |
|
| 89 | + { |
|
| 90 | + return $this |
|
| 91 | + ->TrackedPages() |
|
| 92 | + ->filter('Processed', 1) |
|
| 93 | + ->count(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Returns the latest run, or otherwise creates a new one |
|
| 98 | + * |
|
| 99 | + * @return BrokenExternalPageTrackStatus |
|
| 100 | + */ |
|
| 101 | + public static function get_or_create() |
|
| 102 | + { |
|
| 103 | + // Check the current status |
|
| 104 | + $status = self::get_latest(); |
|
| 105 | + if ($status && $status->Status == 'Running') { |
|
| 106 | + $status->updateStatus(); |
|
| 107 | + return $status; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + return self::create_status(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Create and prepare a new status |
|
| 115 | + * |
|
| 116 | + * @return BrokenExternalPageTrackStatus |
|
| 117 | + */ |
|
| 118 | + public static function create_status() |
|
| 119 | + { |
|
| 120 | + // If the script is to be started create a new status |
|
| 121 | + $status = self::create(); |
|
| 122 | + $status->updateJobInfo('Creating new tracking object'); |
|
| 123 | + |
|
| 124 | + // Setup all pages to test |
|
| 125 | + $pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
| 126 | + ->column('ID'); |
|
| 127 | + foreach ($pageIDs as $pageID) { |
|
| 128 | + $trackPage = BrokenExternalPageTrack::create(); |
|
| 129 | + $trackPage->PageID = $pageID; |
|
| 130 | + $trackPage->StatusID = $status->ID; |
|
| 131 | + $trackPage->write(); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $status; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + public function updateJobInfo($message) |
|
| 138 | + { |
|
| 139 | + $this->JobInfo = $message; |
|
| 140 | + $this->write(); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Self check status |
|
| 145 | + */ |
|
| 146 | + public function updateStatus() |
|
| 147 | + { |
|
| 148 | + if ($this->CompletedPages == $this->TotalPages) { |
|
| 149 | + $this->Status = 'Completed'; |
|
| 150 | + $this->updateJobInfo('Setting to completed'); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | } |
@@ -18,68 +18,68 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class BrokenExternalLink extends DataObject |
| 20 | 20 | { |
| 21 | - private static $table_name = 'BrokenExternalLink'; |
|
| 21 | + private static $table_name = 'BrokenExternalLink'; |
|
| 22 | 22 | |
| 23 | - private static $db = array( |
|
| 24 | - 'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer. |
|
| 25 | - 'HTTPCode' =>'Int' |
|
| 26 | - ); |
|
| 23 | + private static $db = array( |
|
| 24 | + 'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer. |
|
| 25 | + 'HTTPCode' =>'Int' |
|
| 26 | + ); |
|
| 27 | 27 | |
| 28 | - private static $has_one = array( |
|
| 29 | - 'Track' => BrokenExternalPageTrack::class, |
|
| 30 | - 'Status' => BrokenExternalPageTrackStatus::class |
|
| 31 | - ); |
|
| 28 | + private static $has_one = array( |
|
| 29 | + 'Track' => BrokenExternalPageTrack::class, |
|
| 30 | + 'Status' => BrokenExternalPageTrackStatus::class |
|
| 31 | + ); |
|
| 32 | 32 | |
| 33 | - private static $summary_fields = array( |
|
| 34 | - 'Created' => 'Checked', |
|
| 35 | - 'Link' => 'External Link', |
|
| 36 | - 'HTTPCodeDescription' => 'HTTP Error Code', |
|
| 37 | - 'Page.Title' => 'Page link is on' |
|
| 38 | - ); |
|
| 33 | + private static $summary_fields = array( |
|
| 34 | + 'Created' => 'Checked', |
|
| 35 | + 'Link' => 'External Link', |
|
| 36 | + 'HTTPCodeDescription' => 'HTTP Error Code', |
|
| 37 | + 'Page.Title' => 'Page link is on' |
|
| 38 | + ); |
|
| 39 | 39 | |
| 40 | - private static $searchable_fields = array( |
|
| 41 | - 'HTTPCode' => array('title' => 'HTTP Code') |
|
| 42 | - ); |
|
| 40 | + private static $searchable_fields = array( |
|
| 41 | + 'HTTPCode' => array('title' => 'HTTP Code') |
|
| 42 | + ); |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @return SiteTree |
|
| 46 | - */ |
|
| 47 | - public function Page() |
|
| 48 | - { |
|
| 49 | - return $this->Track()->Page(); |
|
| 50 | - } |
|
| 44 | + /** |
|
| 45 | + * @return SiteTree |
|
| 46 | + */ |
|
| 47 | + public function Page() |
|
| 48 | + { |
|
| 49 | + return $this->Track()->Page(); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public function canEdit($member = false) |
|
| 53 | - { |
|
| 54 | - return false; |
|
| 55 | - } |
|
| 52 | + public function canEdit($member = false) |
|
| 53 | + { |
|
| 54 | + return false; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function canView($member = false) |
|
| 58 | - { |
|
| 59 | - $member = $member ? $member : Member::currentUser(); |
|
| 60 | - $codes = array('content-authors', 'administrators'); |
|
| 61 | - return Permission::checkMember($member, $codes); |
|
| 62 | - } |
|
| 57 | + public function canView($member = false) |
|
| 58 | + { |
|
| 59 | + $member = $member ? $member : Member::currentUser(); |
|
| 60 | + $codes = array('content-authors', 'administrators'); |
|
| 61 | + return Permission::checkMember($member, $codes); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Retrieve a human readable description of a response code |
|
| 66 | - * |
|
| 67 | - * @return string |
|
| 68 | - */ |
|
| 69 | - public function getHTTPCodeDescription() |
|
| 70 | - { |
|
| 71 | - $code = $this->HTTPCode; |
|
| 64 | + /** |
|
| 65 | + * Retrieve a human readable description of a response code |
|
| 66 | + * |
|
| 67 | + * @return string |
|
| 68 | + */ |
|
| 69 | + public function getHTTPCodeDescription() |
|
| 70 | + { |
|
| 71 | + $code = $this->HTTPCode; |
|
| 72 | 72 | |
| 73 | - try { |
|
| 74 | - $response = HTTPResponse::create('', $code); |
|
| 75 | - // Assume that $code = 0 means there was no response |
|
| 76 | - $description = $code ? |
|
| 77 | - $response->getStatusDescription() : |
|
| 78 | - _t(__CLASS__ . '.NOTAVAILABLE', 'Server Not Available'); |
|
| 79 | - } catch (InvalidArgumentException $e) { |
|
| 80 | - $description = _t(__CLASS__ . '.UNKNOWNRESPONSE', 'Unknown Response Code'); |
|
| 81 | - } |
|
| 73 | + try { |
|
| 74 | + $response = HTTPResponse::create('', $code); |
|
| 75 | + // Assume that $code = 0 means there was no response |
|
| 76 | + $description = $code ? |
|
| 77 | + $response->getStatusDescription() : |
|
| 78 | + _t(__CLASS__ . '.NOTAVAILABLE', 'Server Not Available'); |
|
| 79 | + } catch (InvalidArgumentException $e) { |
|
| 80 | + $description = _t(__CLASS__ . '.UNKNOWNRESPONSE', 'Unknown Response Code'); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - return sprintf("%d (%s)", $code, $description); |
|
| 84 | - } |
|
| 83 | + return sprintf("%d (%s)", $code, $description); |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -74,10 +74,9 @@ |
||
| 74 | 74 | $response = HTTPResponse::create('', $code); |
| 75 | 75 | // Assume that $code = 0 means there was no response |
| 76 | 76 | $description = $code ? |
| 77 | - $response->getStatusDescription() : |
|
| 78 | - _t(__CLASS__ . '.NOTAVAILABLE', 'Server Not Available'); |
|
| 77 | + $response->getStatusDescription() : _t(__CLASS__.'.NOTAVAILABLE', 'Server Not Available'); |
|
| 79 | 78 | } catch (InvalidArgumentException $e) { |
| 80 | - $description = _t(__CLASS__ . '.UNKNOWNRESPONSE', 'Unknown Response Code'); |
|
| 79 | + $description = _t(__CLASS__.'.UNKNOWNRESPONSE', 'Unknown Response Code'); |
|
| 81 | 80 | } |
| 82 | 81 | |
| 83 | 82 | return sprintf("%d (%s)", $code, $description); |
@@ -12,62 +12,62 @@ |
||
| 12 | 12 | class CMSExternalLinksController extends Controller |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - private static $allowed_actions = [ |
|
| 16 | - 'getJobStatus', |
|
| 17 | - 'start' |
|
| 18 | - ]; |
|
| 15 | + private static $allowed_actions = [ |
|
| 16 | + 'getJobStatus', |
|
| 17 | + 'start' |
|
| 18 | + ]; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Respond to Ajax requests for info on a running job |
|
| 22 | - * |
|
| 23 | - * @return string JSON string detailing status of the job |
|
| 24 | - */ |
|
| 25 | - public function getJobStatus() |
|
| 26 | - { |
|
| 27 | - // Set headers |
|
| 28 | - HTTP::set_cache_age(0); |
|
| 29 | - HTTP::add_cache_headers($this->response); |
|
| 30 | - $this->response |
|
| 31 | - ->addHeader('Content-Type', 'application/json') |
|
| 32 | - ->addHeader('Content-Encoding', 'UTF-8') |
|
| 33 | - ->addHeader('X-Content-Type-Options', 'nosniff'); |
|
| 20 | + /** |
|
| 21 | + * Respond to Ajax requests for info on a running job |
|
| 22 | + * |
|
| 23 | + * @return string JSON string detailing status of the job |
|
| 24 | + */ |
|
| 25 | + public function getJobStatus() |
|
| 26 | + { |
|
| 27 | + // Set headers |
|
| 28 | + HTTP::set_cache_age(0); |
|
| 29 | + HTTP::add_cache_headers($this->response); |
|
| 30 | + $this->response |
|
| 31 | + ->addHeader('Content-Type', 'application/json') |
|
| 32 | + ->addHeader('Content-Encoding', 'UTF-8') |
|
| 33 | + ->addHeader('X-Content-Type-Options', 'nosniff'); |
|
| 34 | 34 | |
| 35 | - // Format status |
|
| 36 | - $track = BrokenExternalPageTrackStatus::get_latest(); |
|
| 37 | - if ($track) { |
|
| 38 | - return json_encode([ |
|
| 39 | - 'TrackID' => $track->ID, |
|
| 40 | - 'Status' => $track->Status, |
|
| 41 | - 'Completed' => $track->getCompletedPages(), |
|
| 42 | - 'Total' => $track->getTotalPages() |
|
| 43 | - ]); |
|
| 44 | - } |
|
| 45 | - } |
|
| 35 | + // Format status |
|
| 36 | + $track = BrokenExternalPageTrackStatus::get_latest(); |
|
| 37 | + if ($track) { |
|
| 38 | + return json_encode([ |
|
| 39 | + 'TrackID' => $track->ID, |
|
| 40 | + 'Status' => $track->Status, |
|
| 41 | + 'Completed' => $track->getCompletedPages(), |
|
| 42 | + 'Total' => $track->getTotalPages() |
|
| 43 | + ]); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Starts a broken external link check |
|
| 50 | - */ |
|
| 51 | - public function start() |
|
| 52 | - { |
|
| 53 | - // return if the a job is already running |
|
| 54 | - $status = BrokenExternalPageTrackStatus::get_latest(); |
|
| 55 | - if ($status && $status->Status == 'Running') { |
|
| 56 | - return; |
|
| 57 | - } |
|
| 48 | + /** |
|
| 49 | + * Starts a broken external link check |
|
| 50 | + */ |
|
| 51 | + public function start() |
|
| 52 | + { |
|
| 53 | + // return if the a job is already running |
|
| 54 | + $status = BrokenExternalPageTrackStatus::get_latest(); |
|
| 55 | + if ($status && $status->Status == 'Running') { |
|
| 56 | + return; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - // Create a new job |
|
| 60 | - if (class_exists(QueuedJobService::class)) { |
|
| 61 | - // Force the creation of a new run |
|
| 62 | - BrokenExternalPageTrackStatus::create_status(); |
|
| 63 | - $checkLinks = new CheckExternalLinksJob(); |
|
| 64 | - singleton(QueuedJobService::class)->queueJob($checkLinks); |
|
| 65 | - } else { |
|
| 66 | - //TODO this hangs as it waits for the connection to be released |
|
| 67 | - // should return back and continue processing |
|
| 68 | - // http://us3.php.net/manual/en/features.connection-handling.php |
|
| 69 | - $task = CheckExternalLinksTask::create(); |
|
| 70 | - $task->runLinksCheck(); |
|
| 71 | - } |
|
| 72 | - } |
|
| 59 | + // Create a new job |
|
| 60 | + if (class_exists(QueuedJobService::class)) { |
|
| 61 | + // Force the creation of a new run |
|
| 62 | + BrokenExternalPageTrackStatus::create_status(); |
|
| 63 | + $checkLinks = new CheckExternalLinksJob(); |
|
| 64 | + singleton(QueuedJobService::class)->queueJob($checkLinks); |
|
| 65 | + } else { |
|
| 66 | + //TODO this hangs as it waits for the connection to be released |
|
| 67 | + // should return back and continue processing |
|
| 68 | + // http://us3.php.net/manual/en/features.connection-handling.php |
|
| 69 | + $task = CheckExternalLinksTask::create(); |
|
| 70 | + $task->runLinksCheck(); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | } |