@@ -11,47 +11,47 @@ |
||
11 | 11 | class CurlLinkChecker implements LinkChecker |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * Return cache |
|
16 | - * |
|
17 | - * @return CacheInterface |
|
18 | - */ |
|
19 | - protected function getCache() |
|
20 | - { |
|
21 | - return Injector::inst()->get(CacheInterface::class . '.CurlLinkChecker'); |
|
22 | - } |
|
23 | - |
|
24 | - /** |
|
25 | - * Determine the http status code for a given link |
|
26 | - * |
|
27 | - * @param string $href URL to check |
|
28 | - * @return int HTTP status code, or null if not checkable (not a link) |
|
29 | - */ |
|
30 | - public function checkLink($href) |
|
31 | - { |
|
32 | - // Skip non-external links |
|
33 | - if (!preg_match('/^https?[^:]*:\/\//', $href)) { |
|
34 | - return null; |
|
35 | - } |
|
36 | - |
|
37 | - // Check if we have a cached result |
|
38 | - $cacheKey = md5($href); |
|
39 | - $result = $this->getCache()->get($cacheKey, false); |
|
40 | - if ($result !== false) { |
|
41 | - return $result; |
|
42 | - } |
|
43 | - |
|
44 | - // No cached result so just request |
|
45 | - $handle = curl_init($href); |
|
46 | - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
|
47 | - curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
|
48 | - curl_setopt($handle, CURLOPT_TIMEOUT, 10); |
|
49 | - curl_exec($handle); |
|
50 | - $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
|
51 | - curl_close($handle); |
|
52 | - |
|
53 | - // Cache result |
|
54 | - $this->getCache()->set($cacheKey, $httpCode); |
|
55 | - return $httpCode; |
|
56 | - } |
|
14 | + /** |
|
15 | + * Return cache |
|
16 | + * |
|
17 | + * @return CacheInterface |
|
18 | + */ |
|
19 | + protected function getCache() |
|
20 | + { |
|
21 | + return Injector::inst()->get(CacheInterface::class . '.CurlLinkChecker'); |
|
22 | + } |
|
23 | + |
|
24 | + /** |
|
25 | + * Determine the http status code for a given link |
|
26 | + * |
|
27 | + * @param string $href URL to check |
|
28 | + * @return int HTTP status code, or null if not checkable (not a link) |
|
29 | + */ |
|
30 | + public function checkLink($href) |
|
31 | + { |
|
32 | + // Skip non-external links |
|
33 | + if (!preg_match('/^https?[^:]*:\/\//', $href)) { |
|
34 | + return null; |
|
35 | + } |
|
36 | + |
|
37 | + // Check if we have a cached result |
|
38 | + $cacheKey = md5($href); |
|
39 | + $result = $this->getCache()->get($cacheKey, false); |
|
40 | + if ($result !== false) { |
|
41 | + return $result; |
|
42 | + } |
|
43 | + |
|
44 | + // No cached result so just request |
|
45 | + $handle = curl_init($href); |
|
46 | + curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
|
47 | + curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5); |
|
48 | + curl_setopt($handle, CURLOPT_TIMEOUT, 10); |
|
49 | + curl_exec($handle); |
|
50 | + $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
|
51 | + curl_close($handle); |
|
52 | + |
|
53 | + // Cache result |
|
54 | + $this->getCache()->set($cacheKey, $httpCode); |
|
55 | + return $httpCode; |
|
56 | + } |
|
57 | 57 | } |
@@ -13,62 +13,62 @@ |
||
13 | 13 | class CMSExternalLinksController extends Controller |
14 | 14 | { |
15 | 15 | |
16 | - private static $allowed_actions = [ |
|
17 | - 'getJobStatus', |
|
18 | - 'start' |
|
19 | - ]; |
|
16 | + private static $allowed_actions = [ |
|
17 | + 'getJobStatus', |
|
18 | + 'start' |
|
19 | + ]; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Respond to Ajax requests for info on a running job |
|
23 | - * |
|
24 | - * @return string JSON string detailing status of the job |
|
25 | - */ |
|
26 | - public function getJobStatus() |
|
27 | - { |
|
28 | - // Set headers |
|
29 | - HTTP::set_cache_age(0); |
|
30 | - HTTP::add_cache_headers($this->response); |
|
31 | - $this->response |
|
32 | - ->addHeader('Content-Type', 'application/json') |
|
33 | - ->addHeader('Content-Encoding', 'UTF-8') |
|
34 | - ->addHeader('X-Content-Type-Options', 'nosniff'); |
|
21 | + /** |
|
22 | + * Respond to Ajax requests for info on a running job |
|
23 | + * |
|
24 | + * @return string JSON string detailing status of the job |
|
25 | + */ |
|
26 | + public function getJobStatus() |
|
27 | + { |
|
28 | + // Set headers |
|
29 | + HTTP::set_cache_age(0); |
|
30 | + HTTP::add_cache_headers($this->response); |
|
31 | + $this->response |
|
32 | + ->addHeader('Content-Type', 'application/json') |
|
33 | + ->addHeader('Content-Encoding', 'UTF-8') |
|
34 | + ->addHeader('X-Content-Type-Options', 'nosniff'); |
|
35 | 35 | |
36 | - // Format status |
|
37 | - $track = BrokenExternalPageTrackStatus::get_latest(); |
|
38 | - if ($track) { |
|
39 | - return Convert::array2json([ |
|
40 | - 'TrackID' => $track->ID, |
|
41 | - 'Status' => $track->Status, |
|
42 | - 'Completed' => $track->getCompletedPages(), |
|
43 | - 'Total' => $track->getTotalPages() |
|
44 | - ]); |
|
45 | - } |
|
46 | - } |
|
36 | + // Format status |
|
37 | + $track = BrokenExternalPageTrackStatus::get_latest(); |
|
38 | + if ($track) { |
|
39 | + return Convert::array2json([ |
|
40 | + 'TrackID' => $track->ID, |
|
41 | + 'Status' => $track->Status, |
|
42 | + 'Completed' => $track->getCompletedPages(), |
|
43 | + 'Total' => $track->getTotalPages() |
|
44 | + ]); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | 48 | |
49 | - /** |
|
50 | - * Starts a broken external link check |
|
51 | - */ |
|
52 | - public function start() |
|
53 | - { |
|
54 | - // return if the a job is already running |
|
55 | - $status = BrokenExternalPageTrackStatus::get_latest(); |
|
56 | - if ($status && $status->Status == 'Running') { |
|
57 | - return; |
|
58 | - } |
|
49 | + /** |
|
50 | + * Starts a broken external link check |
|
51 | + */ |
|
52 | + public function start() |
|
53 | + { |
|
54 | + // return if the a job is already running |
|
55 | + $status = BrokenExternalPageTrackStatus::get_latest(); |
|
56 | + if ($status && $status->Status == 'Running') { |
|
57 | + return; |
|
58 | + } |
|
59 | 59 | |
60 | - // Create a new job |
|
61 | - if (class_exists(QueuedJobService::class)) { |
|
62 | - // Force the creation of a new run |
|
63 | - BrokenExternalPageTrackStatus::create_status(); |
|
64 | - $checkLinks = new CheckExternalLinksJob(); |
|
65 | - singleton(QueuedJobService::class)->queueJob($checkLinks); |
|
66 | - } else { |
|
67 | - //TODO this hangs as it waits for the connection to be released |
|
68 | - // should return back and continue processing |
|
69 | - // http://us3.php.net/manual/en/features.connection-handling.php |
|
70 | - $task = CheckExternalLinksTask::create(); |
|
71 | - $task->runLinksCheck(); |
|
72 | - } |
|
73 | - } |
|
60 | + // Create a new job |
|
61 | + if (class_exists(QueuedJobService::class)) { |
|
62 | + // Force the creation of a new run |
|
63 | + BrokenExternalPageTrackStatus::create_status(); |
|
64 | + $checkLinks = new CheckExternalLinksJob(); |
|
65 | + singleton(QueuedJobService::class)->queueJob($checkLinks); |
|
66 | + } else { |
|
67 | + //TODO this hangs as it waits for the connection to be released |
|
68 | + // should return back and continue processing |
|
69 | + // http://us3.php.net/manual/en/features.connection-handling.php |
|
70 | + $task = CheckExternalLinksTask::create(); |
|
71 | + $task->runLinksCheck(); |
|
72 | + } |
|
73 | + } |
|
74 | 74 | } |