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