@@ -11,74 +11,74 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class CurlLinkChecker implements LinkChecker |
| 13 | 13 | { |
| 14 | - use Configurable; |
|
| 14 | + use Configurable; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * If we want to follow redirects a 301 http code for example |
|
| 18 | - * Set via YAML file |
|
| 19 | - * |
|
| 20 | - * @config |
|
| 21 | - * @var boolean |
|
| 22 | - */ |
|
| 23 | - private static $follow_location = false; |
|
| 16 | + /** |
|
| 17 | + * If we want to follow redirects a 301 http code for example |
|
| 18 | + * Set via YAML file |
|
| 19 | + * |
|
| 20 | + * @config |
|
| 21 | + * @var boolean |
|
| 22 | + */ |
|
| 23 | + private static $follow_location = false; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * If we want to bypass the cache |
|
| 27 | - * Set via YAML file |
|
| 28 | - * |
|
| 29 | - * @config |
|
| 30 | - * @var boolean |
|
| 31 | - */ |
|
| 32 | - private static $bypass_cache = false; |
|
| 25 | + /** |
|
| 26 | + * If we want to bypass the cache |
|
| 27 | + * Set via YAML file |
|
| 28 | + * |
|
| 29 | + * @config |
|
| 30 | + * @var boolean |
|
| 31 | + */ |
|
| 32 | + private static $bypass_cache = false; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Return cache |
|
| 36 | - * |
|
| 37 | - * @return CacheInterface |
|
| 38 | - */ |
|
| 39 | - protected function getCache() |
|
| 40 | - { |
|
| 41 | - return Injector::inst()->get(CacheInterface::class . '.CurlLinkChecker'); |
|
| 42 | - } |
|
| 34 | + /** |
|
| 35 | + * Return cache |
|
| 36 | + * |
|
| 37 | + * @return CacheInterface |
|
| 38 | + */ |
|
| 39 | + protected function getCache() |
|
| 40 | + { |
|
| 41 | + return Injector::inst()->get(CacheInterface::class . '.CurlLinkChecker'); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Determine the http status code for a given link |
|
| 46 | - * |
|
| 47 | - * @param string $href URL to check |
|
| 48 | - * @return int HTTP status code, or null if not checkable (not a link) |
|
| 49 | - */ |
|
| 50 | - public function checkLink($href) |
|
| 51 | - { |
|
| 52 | - // Skip non-external links |
|
| 53 | - if (!preg_match('/^https?[^:]*:\/\//', $href)) { |
|
| 54 | - return null; |
|
| 55 | - } |
|
| 44 | + /** |
|
| 45 | + * Determine the http status code for a given link |
|
| 46 | + * |
|
| 47 | + * @param string $href URL to check |
|
| 48 | + * @return int HTTP status code, or null if not checkable (not a link) |
|
| 49 | + */ |
|
| 50 | + public function checkLink($href) |
|
| 51 | + { |
|
| 52 | + // Skip non-external links |
|
| 53 | + if (!preg_match('/^https?[^:]*:\/\//', $href)) { |
|
| 54 | + return null; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - $cacheKey = md5($href); |
|
| 58 | - if (!$this->config()->get('bypass_cache')) { |
|
| 59 | - // Check if we have a cached result |
|
| 60 | - $result = $this->getCache()->get($cacheKey, false); |
|
| 61 | - if ($result !== false) { |
|
| 62 | - return $result; |
|
| 63 | - } |
|
| 64 | - } |
|
| 57 | + $cacheKey = md5($href); |
|
| 58 | + if (!$this->config()->get('bypass_cache')) { |
|
| 59 | + // Check if we have a cached result |
|
| 60 | + $result = $this->getCache()->get($cacheKey, false); |
|
| 61 | + if ($result !== false) { |
|
| 62 | + return $result; |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // No cached result so just request |
|
| 67 | - $handle = curl_init($href); |
|
| 68 | - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
|
| 69 | - if ($this->config()->get('follow_location')) { |
|
| 70 | - curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true); |
|
| 71 | - } |
|
| 72 | - curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
|
| 73 | - curl_setopt($handle, CURLOPT_TIMEOUT, 10); |
|
| 74 | - curl_exec($handle); |
|
| 75 | - $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
|
| 76 | - curl_close($handle); |
|
| 66 | + // No cached result so just request |
|
| 67 | + $handle = curl_init($href); |
|
| 68 | + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
|
| 69 | + if ($this->config()->get('follow_location')) { |
|
| 70 | + curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true); |
|
| 71 | + } |
|
| 72 | + curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
|
| 73 | + curl_setopt($handle, CURLOPT_TIMEOUT, 10); |
|
| 74 | + curl_exec($handle); |
|
| 75 | + $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
|
| 76 | + curl_close($handle); |
|
| 77 | 77 | |
| 78 | - if (!$this->config()->get('bypass_cache')) { |
|
| 79 | - // Cache result |
|
| 80 | - $this->getCache()->set($cacheKey, $httpCode); |
|
| 81 | - } |
|
| 82 | - return $httpCode; |
|
| 83 | - } |
|
| 78 | + if (!$this->config()->get('bypass_cache')) { |
|
| 79 | + // Cache result |
|
| 80 | + $this->getCache()->set($cacheKey, $httpCode); |
|
| 81 | + } |
|
| 82 | + return $httpCode; |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -17,153 +17,153 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class BrokenExternalPageTrackStatus extends DataObject implements i18nEntityProvider |
| 19 | 19 | { |
| 20 | - private static $table_name = 'BrokenExternalPageTrackStatus'; |
|
| 21 | - |
|
| 22 | - private static $db = array( |
|
| 23 | - 'Status' => 'Enum("Completed, Running", "Running")', |
|
| 24 | - 'JobInfo' => 'Varchar(255)' |
|
| 25 | - ); |
|
| 26 | - |
|
| 27 | - private static $has_many = array( |
|
| 28 | - 'TrackedPages' => BrokenExternalPageTrack::class, |
|
| 29 | - 'BrokenLinks' => BrokenExternalLink::class |
|
| 30 | - ); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Get the latest track status |
|
| 34 | - * |
|
| 35 | - * @return BrokenExternalPageTrackStatus |
|
| 36 | - */ |
|
| 37 | - public static function get_latest() |
|
| 38 | - { |
|
| 39 | - return self::get() |
|
| 40 | - ->sort('ID', 'DESC') |
|
| 41 | - ->first(); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Returns the list of provided translations for this object |
|
| 46 | - * |
|
| 47 | - * @return array |
|
| 48 | - */ |
|
| 49 | - public function provideI18nEntities() |
|
| 50 | - { |
|
| 51 | - return [ |
|
| 52 | - __CLASS__ . '.SINGULARNAME' => 'Broken External Page Track Status', |
|
| 53 | - __CLASS__ . '.PLURALNAME' => 'Broken External Page Track Statuses', |
|
| 54 | - __CLASS__ . '.PLURALS' => [ |
|
| 55 | - 'one' => 'A Broken External Page Track Status', |
|
| 56 | - 'other' => '{count} Broken External Page Track Statuses', |
|
| 57 | - ], |
|
| 58 | - ]; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Gets the list of Pages yet to be checked |
|
| 63 | - * |
|
| 64 | - * @return DataList |
|
| 65 | - */ |
|
| 66 | - public function getIncompletePageList() |
|
| 67 | - { |
|
| 68 | - $pageIDs = $this |
|
| 69 | - ->getIncompleteTracks() |
|
| 70 | - ->column('PageID'); |
|
| 71 | - if ($pageIDs) { |
|
| 72 | - return Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
| 73 | - ->byIDs($pageIDs); |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the list of incomplete BrokenExternalPageTrack |
|
| 79 | - * |
|
| 80 | - * @return DataList |
|
| 81 | - */ |
|
| 82 | - public function getIncompleteTracks() |
|
| 83 | - { |
|
| 84 | - return $this |
|
| 85 | - ->TrackedPages() |
|
| 86 | - ->filter('Processed', 0); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Get total pages count |
|
| 91 | - * |
|
| 92 | - * @return int |
|
| 93 | - */ |
|
| 94 | - public function getTotalPages() |
|
| 95 | - { |
|
| 96 | - return $this->TrackedPages()->count(); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Get completed pages count |
|
| 101 | - * |
|
| 102 | - * @return int |
|
| 103 | - */ |
|
| 104 | - public function getCompletedPages() |
|
| 105 | - { |
|
| 106 | - return $this |
|
| 107 | - ->TrackedPages() |
|
| 108 | - ->filter('Processed', 1) |
|
| 109 | - ->count(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Returns the latest run, or otherwise creates a new one |
|
| 114 | - * |
|
| 115 | - * @return BrokenExternalPageTrackStatus |
|
| 116 | - */ |
|
| 117 | - public static function get_or_create() |
|
| 118 | - { |
|
| 119 | - // Check the current status |
|
| 120 | - $status = self::get_latest(); |
|
| 121 | - if ($status && $status->Status == 'Running') { |
|
| 122 | - $status->updateStatus(); |
|
| 123 | - return $status; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return self::create_status(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Create and prepare a new status |
|
| 131 | - * |
|
| 132 | - * @return BrokenExternalPageTrackStatus |
|
| 133 | - */ |
|
| 134 | - public static function create_status() |
|
| 135 | - { |
|
| 136 | - // If the script is to be started create a new status |
|
| 137 | - $status = self::create(); |
|
| 138 | - $status->updateJobInfo('Creating new tracking object'); |
|
| 139 | - |
|
| 140 | - // Setup all pages to test |
|
| 141 | - $pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
| 142 | - ->column('ID'); |
|
| 143 | - foreach ($pageIDs as $pageID) { |
|
| 144 | - $trackPage = BrokenExternalPageTrack::create(); |
|
| 145 | - $trackPage->PageID = $pageID; |
|
| 146 | - $trackPage->StatusID = $status->ID; |
|
| 147 | - $trackPage->write(); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return $status; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function updateJobInfo($message) |
|
| 154 | - { |
|
| 155 | - $this->JobInfo = $message; |
|
| 156 | - $this->write(); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Self check status |
|
| 161 | - */ |
|
| 162 | - public function updateStatus() |
|
| 163 | - { |
|
| 164 | - if ($this->CompletedPages == $this->TotalPages) { |
|
| 165 | - $this->Status = 'Completed'; |
|
| 166 | - $this->updateJobInfo('Setting to completed'); |
|
| 167 | - } |
|
| 168 | - } |
|
| 20 | + private static $table_name = 'BrokenExternalPageTrackStatus'; |
|
| 21 | + |
|
| 22 | + private static $db = array( |
|
| 23 | + 'Status' => 'Enum("Completed, Running", "Running")', |
|
| 24 | + 'JobInfo' => 'Varchar(255)' |
|
| 25 | + ); |
|
| 26 | + |
|
| 27 | + private static $has_many = array( |
|
| 28 | + 'TrackedPages' => BrokenExternalPageTrack::class, |
|
| 29 | + 'BrokenLinks' => BrokenExternalLink::class |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Get the latest track status |
|
| 34 | + * |
|
| 35 | + * @return BrokenExternalPageTrackStatus |
|
| 36 | + */ |
|
| 37 | + public static function get_latest() |
|
| 38 | + { |
|
| 39 | + return self::get() |
|
| 40 | + ->sort('ID', 'DESC') |
|
| 41 | + ->first(); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Returns the list of provided translations for this object |
|
| 46 | + * |
|
| 47 | + * @return array |
|
| 48 | + */ |
|
| 49 | + public function provideI18nEntities() |
|
| 50 | + { |
|
| 51 | + return [ |
|
| 52 | + __CLASS__ . '.SINGULARNAME' => 'Broken External Page Track Status', |
|
| 53 | + __CLASS__ . '.PLURALNAME' => 'Broken External Page Track Statuses', |
|
| 54 | + __CLASS__ . '.PLURALS' => [ |
|
| 55 | + 'one' => 'A Broken External Page Track Status', |
|
| 56 | + 'other' => '{count} Broken External Page Track Statuses', |
|
| 57 | + ], |
|
| 58 | + ]; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Gets the list of Pages yet to be checked |
|
| 63 | + * |
|
| 64 | + * @return DataList |
|
| 65 | + */ |
|
| 66 | + public function getIncompletePageList() |
|
| 67 | + { |
|
| 68 | + $pageIDs = $this |
|
| 69 | + ->getIncompleteTracks() |
|
| 70 | + ->column('PageID'); |
|
| 71 | + if ($pageIDs) { |
|
| 72 | + return Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
| 73 | + ->byIDs($pageIDs); |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the list of incomplete BrokenExternalPageTrack |
|
| 79 | + * |
|
| 80 | + * @return DataList |
|
| 81 | + */ |
|
| 82 | + public function getIncompleteTracks() |
|
| 83 | + { |
|
| 84 | + return $this |
|
| 85 | + ->TrackedPages() |
|
| 86 | + ->filter('Processed', 0); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Get total pages count |
|
| 91 | + * |
|
| 92 | + * @return int |
|
| 93 | + */ |
|
| 94 | + public function getTotalPages() |
|
| 95 | + { |
|
| 96 | + return $this->TrackedPages()->count(); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Get completed pages count |
|
| 101 | + * |
|
| 102 | + * @return int |
|
| 103 | + */ |
|
| 104 | + public function getCompletedPages() |
|
| 105 | + { |
|
| 106 | + return $this |
|
| 107 | + ->TrackedPages() |
|
| 108 | + ->filter('Processed', 1) |
|
| 109 | + ->count(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Returns the latest run, or otherwise creates a new one |
|
| 114 | + * |
|
| 115 | + * @return BrokenExternalPageTrackStatus |
|
| 116 | + */ |
|
| 117 | + public static function get_or_create() |
|
| 118 | + { |
|
| 119 | + // Check the current status |
|
| 120 | + $status = self::get_latest(); |
|
| 121 | + if ($status && $status->Status == 'Running') { |
|
| 122 | + $status->updateStatus(); |
|
| 123 | + return $status; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return self::create_status(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Create and prepare a new status |
|
| 131 | + * |
|
| 132 | + * @return BrokenExternalPageTrackStatus |
|
| 133 | + */ |
|
| 134 | + public static function create_status() |
|
| 135 | + { |
|
| 136 | + // If the script is to be started create a new status |
|
| 137 | + $status = self::create(); |
|
| 138 | + $status->updateJobInfo('Creating new tracking object'); |
|
| 139 | + |
|
| 140 | + // Setup all pages to test |
|
| 141 | + $pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage') |
|
| 142 | + ->column('ID'); |
|
| 143 | + foreach ($pageIDs as $pageID) { |
|
| 144 | + $trackPage = BrokenExternalPageTrack::create(); |
|
| 145 | + $trackPage->PageID = $pageID; |
|
| 146 | + $trackPage->StatusID = $status->ID; |
|
| 147 | + $trackPage->write(); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return $status; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function updateJobInfo($message) |
|
| 154 | + { |
|
| 155 | + $this->JobInfo = $message; |
|
| 156 | + $this->write(); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Self check status |
|
| 161 | + */ |
|
| 162 | + public function updateStatus() |
|
| 163 | + { |
|
| 164 | + if ($this->CompletedPages == $this->TotalPages) { |
|
| 165 | + $this->Status = 'Completed'; |
|
| 166 | + $this->updateJobInfo('Setting to completed'); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | 169 | } |
@@ -7,29 +7,29 @@ |
||
| 7 | 7 | |
| 8 | 8 | class BrokenExternalLinkTest extends SapphireTest |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @param int $httpCode |
|
| 12 | - * @param string $expected |
|
| 13 | - * @dataProvider httpCodeProvider |
|
| 14 | - */ |
|
| 15 | - public function testGetHTTPCodeDescription($httpCode, $expected) |
|
| 16 | - { |
|
| 17 | - $link = new BrokenExternalLink(); |
|
| 18 | - $link->HTTPCode = $httpCode; |
|
| 19 | - $this->assertSame($expected, $link->getHTTPCodeDescription()); |
|
| 20 | - } |
|
| 10 | + /** |
|
| 11 | + * @param int $httpCode |
|
| 12 | + * @param string $expected |
|
| 13 | + * @dataProvider httpCodeProvider |
|
| 14 | + */ |
|
| 15 | + public function testGetHTTPCodeDescription($httpCode, $expected) |
|
| 16 | + { |
|
| 17 | + $link = new BrokenExternalLink(); |
|
| 18 | + $link->HTTPCode = $httpCode; |
|
| 19 | + $this->assertSame($expected, $link->getHTTPCodeDescription()); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @return array[] |
|
| 24 | - */ |
|
| 25 | - public function httpCodeProvider() |
|
| 26 | - { |
|
| 27 | - return [ |
|
| 28 | - [200, '200 (OK)'], |
|
| 29 | - [302, '302 (Found)'], |
|
| 30 | - [404, '404 (Not Found)'], |
|
| 31 | - [500, '500 (Internal Server Error)'], |
|
| 32 | - [789, '789 (Unknown Response Code)'], |
|
| 33 | - ]; |
|
| 34 | - } |
|
| 22 | + /** |
|
| 23 | + * @return array[] |
|
| 24 | + */ |
|
| 25 | + public function httpCodeProvider() |
|
| 26 | + { |
|
| 27 | + return [ |
|
| 28 | + [200, '200 (OK)'], |
|
| 29 | + [302, '302 (Found)'], |
|
| 30 | + [404, '404 (Not Found)'], |
|
| 31 | + [500, '500 (Internal Server Error)'], |
|
| 32 | + [789, '789 (Unknown Response Code)'], |
|
| 33 | + ]; |
|
| 34 | + } |
|
| 35 | 35 | } |