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