-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
helpfulrobot
committed
Dec 25, 2015
1 parent
9708dfd
commit 1cef1d1
Showing
3 changed files
with
177 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,46 @@ | ||
<?php | ||
|
||
class BrokenExternalLinks extends DataObject { | ||
|
||
private static $db = array( | ||
'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer. | ||
'HTTPCode' =>'Int' | ||
); | ||
|
||
private static $has_one = array( | ||
'Page' => 'Page' | ||
); | ||
|
||
public static $summary_fields = array( | ||
'Page.Title' => 'Page', | ||
'HTTPCode' => 'HTTP Code', | ||
'Created' => 'Created' | ||
); | ||
|
||
public static $searchable_fields = array( | ||
'HTTPCode' => array('title' => 'HTTP Code') | ||
); | ||
|
||
function canEdit($member = false) { | ||
return false; | ||
} | ||
|
||
class BrokenExternalLinks extends DataObject | ||
{ | ||
|
||
private static $db = array( | ||
'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer. | ||
'HTTPCode' =>'Int' | ||
); | ||
|
||
private static $has_one = array( | ||
'Page' => 'Page' | ||
); | ||
|
||
public static $summary_fields = array( | ||
'Page.Title' => 'Page', | ||
'HTTPCode' => 'HTTP Code', | ||
'Created' => 'Created' | ||
); | ||
|
||
public static $searchable_fields = array( | ||
'HTTPCode' => array('title' => 'HTTP Code') | ||
); | ||
|
||
public function canEdit($member = false) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
class BrokenExternalLinksAdmin extends ModelAdmin { | ||
|
||
public static $url_segment = 'broken-external-links-admin'; | ||
class BrokenExternalLinksAdmin extends ModelAdmin | ||
{ | ||
|
||
public static $managed_models = array( | ||
'BrokenExternalLinks' | ||
); | ||
public static $url_segment = 'broken-external-links-admin'; | ||
|
||
public static $menu_title = 'Broken Ext. links'; | ||
public static $managed_models = array( | ||
'BrokenExternalLinks' | ||
); | ||
|
||
public function init() { | ||
parent::init(); | ||
} | ||
public static $menu_title = 'Broken Ext. links'; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,81 @@ | ||
<?php | ||
|
||
class CheckExternalLinks extends BuildTask { | ||
protected $title = 'Checking broken External links in the SiteTree'; | ||
class CheckExternalLinks extends BuildTask | ||
{ | ||
protected $title = 'Checking broken External links in the SiteTree'; | ||
|
||
protected $description = 'A task that records external broken links in the SiteTree'; | ||
protected $description = 'A task that records external broken links in the SiteTree'; | ||
|
||
protected $enabled = true; | ||
protected $enabled = true; | ||
|
||
function run($request) { | ||
// clear broken external link table | ||
$table = 'BrokenExternalLinks'; | ||
if(method_exists(DB::getConn(), 'clearTable')) DB::getConn()->clearTable($table); | ||
else DB::query("TRUNCATE \"$table\""); | ||
$pages = SiteTree::get(); | ||
foreach ($pages as $page) { | ||
$htmlValue = Injector::inst()->create('HTMLValue', $page->Content); | ||
public function run($request) | ||
{ | ||
// clear broken external link table | ||
$table = 'BrokenExternalLinks'; | ||
if (method_exists(DB::getConn(), 'clearTable')) { | ||
DB::getConn()->clearTable($table); | ||
} else { | ||
DB::query("TRUNCATE \"$table\""); | ||
} | ||
$pages = SiteTree::get(); | ||
foreach ($pages as $page) { | ||
$htmlValue = Injector::inst()->create('HTMLValue', $page->Content); | ||
|
||
// Populate link tracking for internal links & links to asset files. | ||
if($links = $htmlValue->getElementsByTagName('a')) foreach($links as $link) { | ||
$href = Director::makeRelative($link->getAttribute('href')); | ||
if ($href == 'admin/') continue; | ||
// Populate link tracking for internal links & links to asset files. | ||
if ($links = $htmlValue->getElementsByTagName('a')) { | ||
foreach ($links as $link) { | ||
$href = Director::makeRelative($link->getAttribute('href')); | ||
if ($href == 'admin/') { | ||
continue; | ||
} | ||
|
||
// ignore SiteTree and assets links as they will be caught by SiteTreeLinkTracking | ||
if(preg_match('/\[sitetree_link,id=([0-9]+)\]/i', $href, $matches)) { | ||
continue; | ||
} else if(substr($href, 0, strlen(ASSETS_DIR) + 1) == ASSETS_DIR.'/') { | ||
continue; | ||
} | ||
if($href && function_exists('curl_init')) { | ||
$handle = curl_init($href); | ||
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); | ||
$response = curl_exec($handle); | ||
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); | ||
curl_close($handle); | ||
if (($httpCode < 200 || $httpCode > 302) | ||
|| ($href == '' || $href[0] == '/')) | ||
{ | ||
$brokenLink = new BrokenExternalLinks(); | ||
$brokenLink->PageID = $page->ID; | ||
$brokenLink->Link = $href; | ||
$brokenLink->HTTPCode = $httpCode; | ||
$brokenLink->write(); | ||
// ignore SiteTree and assets links as they will be caught by SiteTreeLinkTracking | ||
if (preg_match('/\[sitetree_link,id=([0-9]+)\]/i', $href, $matches)) { | ||
continue; | ||
} elseif (substr($href, 0, strlen(ASSETS_DIR) + 1) == ASSETS_DIR.'/') { | ||
continue; | ||
} | ||
if ($href && function_exists('curl_init')) { | ||
$handle = curl_init($href); | ||
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); | ||
$response = curl_exec($handle); | ||
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); | ||
curl_close($handle); | ||
if (($httpCode < 200 || $httpCode > 302) | ||
|| ($href == '' || $href[0] == '/')) { | ||
$brokenLink = new BrokenExternalLinks(); | ||
$brokenLink->PageID = $page->ID; | ||
$brokenLink->Link = $href; | ||
$brokenLink->HTTPCode = $httpCode; | ||
$brokenLink->write(); | ||
|
||
// TODO set the broken link class | ||
/* | ||
$class = $link->getAttribute('class'); | ||
$class = ($class) ? $class . 'ss-broken' : 'ss-broken'; | ||
$link->setAttribute('class', ($class ? "$class ss-broken" : 'ss-broken')); | ||
*/ | ||
// TODO set the broken link class | ||
/* | ||
$class = $link->getAttribute('class'); | ||
$class = ($class) ? $class . 'ss-broken' : 'ss-broken'; | ||
$link->setAttribute('class', ($class ? "$class ss-broken" : 'ss-broken')); | ||
*/ | ||
|
||
// use raw sql query to set broken link as calling the dataobject write | ||
// method will reset the links if no broken internal links are found | ||
$query = "UPDATE \"SiteTree\" SET \"HasBrokenLink\" = 1 "; | ||
$query .= "WHERE \"ID\" = " . (int)$page->ID; | ||
$result = DB::query($query); | ||
if (!$result) { | ||
// error updating hasBrokenLink | ||
} | ||
// use raw sql query to set broken link as calling the dataobject write | ||
// method will reset the links if no broken internal links are found | ||
$query = "UPDATE \"SiteTree\" SET \"HasBrokenLink\" = 1 "; | ||
$query .= "WHERE \"ID\" = " . (int)$page->ID; | ||
$result = DB::query($query); | ||
if (!$result) { | ||
// error updating hasBrokenLink | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
// run this again if queued jobs exists and is a valid int | ||
$queuedJob = Config::inst()->get('CheckExternalLinks', 'QueuedJob'); | ||
if (isset($queuedJob) && is_int($queuedJob) && class_exists('QueuedJobService')) { | ||
$checkLinks = new CheckExternalLinksJob(); | ||
singleton('QueuedJobService') | ||
->queueJob($checkLinks, date('Y-m-d H:i:s', time() + $queuedJob)); | ||
} | ||
|
||
} | ||
// run this again if queued jobs exists and is a valid int | ||
$queuedJob = Config::inst()->get('CheckExternalLinks', 'QueuedJob'); | ||
if (isset($queuedJob) && is_int($queuedJob) && class_exists('QueuedJobService')) { | ||
$checkLinks = new CheckExternalLinksJob(); | ||
singleton('QueuedJobService') | ||
->queueJob($checkLinks, date('Y-m-d H:i:s', time() + $queuedJob)); | ||
} | ||
} | ||
} |