Run the upgrader & linting tools
An initial (untested) run at a proper upgrade. Progress commit.
This commit is contained in:
parent
e6ddf0fe47
commit
1ed9a23a71
|
@ -2,62 +2,68 @@
|
||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Controllers;
|
namespace SilverStripe\ExternalLinks\Controllers;
|
||||||
|
|
||||||
use Controller;
|
use SilverStripe\Control\HTTP;
|
||||||
use HTTP;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
use BrokenExternalPageTrackStatus;
|
use SilverStripe\ExternalLinks\Jobs\CheckExternalLinksJob;
|
||||||
use CheckExternalLinksJob;
|
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
|
||||||
use CheckExternalLinksTask;
|
use SilverStripe\Control\Controller;
|
||||||
|
|
||||||
|
class CMSExternalLinks_Controller extends Controller
|
||||||
|
{
|
||||||
|
|
||||||
class CMSExternalLinks_Controller extends Controller {
|
private static $allowed_actions = array('getJobStatus', 'start');
|
||||||
|
|
||||||
private static $allowed_actions = array('getJobStatus', 'start');
|
/*
|
||||||
|
|
||||||
/*
|
|
||||||
* Respond to Ajax requests for info on a running job
|
* Respond to Ajax requests for info on a running job
|
||||||
*
|
*
|
||||||
* @return string JSON string detailing status of the job
|
* @return string JSON string detailing status of the job
|
||||||
*/
|
*/
|
||||||
public function getJobStatus() {
|
public function getJobStatus()
|
||||||
// Set headers
|
{
|
||||||
HTTP::set_cache_age(0);
|
// Set headers
|
||||||
HTTP::add_cache_headers($this->response);
|
HTTP::set_cache_age(0);
|
||||||
$this->response
|
HTTP::add_cache_headers($this->response);
|
||||||
->addHeader('Content-Type', 'application/json')
|
$this->response
|
||||||
->addHeader('Content-Encoding', 'UTF-8')
|
->addHeader('Content-Type', 'application/json')
|
||||||
->addHeader('X-Content-Type-Options', 'nosniff');
|
->addHeader('Content-Encoding', 'UTF-8')
|
||||||
|
->addHeader('X-Content-Type-Options', 'nosniff');
|
||||||
|
|
||||||
// Format status
|
// Format status
|
||||||
$track = BrokenExternalPageTrackStatus::get_latest();
|
$track = BrokenExternalPageTrackStatus::get_latest();
|
||||||
if($track) return json_encode(array(
|
if ($track) {
|
||||||
'TrackID' => $track->ID,
|
return json_encode(array(
|
||||||
'Status' => $track->Status,
|
'TrackID' => $track->ID,
|
||||||
'Completed' => $track->getCompletedPages(),
|
'Status' => $track->Status,
|
||||||
'Total' => $track->getTotalPages()
|
'Completed' => $track->getCompletedPages(),
|
||||||
));
|
'Total' => $track->getTotalPages()
|
||||||
}
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Starts a broken external link check
|
* Starts a broken external link check
|
||||||
*/
|
*/
|
||||||
public function start() {
|
public function start()
|
||||||
// return if the a job is already running
|
{
|
||||||
$status = BrokenExternalPageTrackStatus::get_latest();
|
// return if the a job is already running
|
||||||
if ($status && $status->Status == 'Running') return;
|
$status = BrokenExternalPageTrackStatus::get_latest();
|
||||||
|
if ($status && $status->Status == 'Running') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new job
|
// Create a new job
|
||||||
if (class_exists('QueuedJobService')) {
|
if (class_exists('QueuedJobService')) {
|
||||||
// Force the creation of a new run
|
// Force the creation of a new run
|
||||||
BrokenExternalPageTrackStatus::create_status();
|
BrokenExternalPageTrackStatus::create_status();
|
||||||
$checkLinks = new CheckExternalLinksJob();
|
$checkLinks = new CheckExternalLinksJob();
|
||||||
singleton('QueuedJobService')->queueJob($checkLinks);
|
singleton('QueuedJobService')->queueJob($checkLinks);
|
||||||
} else {
|
} else {
|
||||||
//TODO this hangs as it waits for the connection to be released
|
//TODO this hangs as it waits for the connection to be released
|
||||||
// should return back and continue processing
|
// should return back and continue processing
|
||||||
// http://us3.php.net/manual/en/features.connection-handling.php
|
// http://us3.php.net/manual/en/features.connection-handling.php
|
||||||
$task = CheckExternalLinksTask::create();
|
$task = CheckExternalLinksTask::create();
|
||||||
$task->runLinksCheck();
|
$task->runLinksCheck();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,38 +4,44 @@ namespace SilverStripe\ExternalLinks\Jobs;
|
||||||
|
|
||||||
use AbstractQueuedJob;
|
use AbstractQueuedJob;
|
||||||
use QueuedJob;
|
use QueuedJob;
|
||||||
use CheckExternalLinksTask;
|
|
||||||
|
|
||||||
|
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
|
||||||
|
|
||||||
if(!class_exists('AbstractQueuedJob')) return;
|
if (!class_exists('AbstractQueuedJob')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Job for running a external link check for published pages
|
* A Job for running a external link check for published pages
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob {
|
class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob
|
||||||
|
{
|
||||||
|
|
||||||
public function getTitle() {
|
public function getTitle()
|
||||||
return _t('CheckExternalLiksJob.TITLE', 'Checking for external broken links');
|
{
|
||||||
}
|
return _t('CheckExternalLiksJob.TITLE', 'Checking for external broken links');
|
||||||
|
}
|
||||||
|
|
||||||
public function getJobType() {
|
public function getJobType()
|
||||||
return QueuedJob::QUEUED;
|
{
|
||||||
}
|
return QueuedJob::QUEUED;
|
||||||
|
}
|
||||||
|
|
||||||
public function getSignature() {
|
public function getSignature()
|
||||||
return md5(get_class($this));
|
{
|
||||||
}
|
return md5(get_class($this));
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Check an individual page
|
|
||||||
*/
|
|
||||||
public function process() {
|
|
||||||
$task = CheckExternalLinksTask::create();
|
|
||||||
$track = $task->runLinksCheck(1);
|
|
||||||
$this->currentStep = $track->CompletedPages;
|
|
||||||
$this->totalSteps = $track->TotalPages;
|
|
||||||
$this->isComplete = $track->Status === 'Completed';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check an individual page
|
||||||
|
*/
|
||||||
|
public function process()
|
||||||
|
{
|
||||||
|
$task = CheckExternalLinksTask::create();
|
||||||
|
$track = $task->runLinksCheck(1);
|
||||||
|
$this->currentStep = $track->CompletedPages;
|
||||||
|
$this->totalSteps = $track->TotalPages;
|
||||||
|
$this->isComplete = $track->Status === 'Completed';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,13 @@
|
||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Model;
|
namespace SilverStripe\ExternalLinks\Model;
|
||||||
|
|
||||||
use DataObject;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
|
||||||
use Member;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
use Permission;
|
use SilverStripe\Security\Member;
|
||||||
use Config;
|
use SilverStripe\Security\Permission;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
|
use SilverStripe\Control\HTTPResponse;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a single link checked for a single run that is broken
|
* Represents a single link checked for a single run that is broken
|
||||||
|
@ -14,66 +16,68 @@ use Config;
|
||||||
* @method BrokenExternalPageTrack Track()
|
* @method BrokenExternalPageTrack Track()
|
||||||
* @method BrokenExternalPageTrackStatus Status()
|
* @method BrokenExternalPageTrackStatus Status()
|
||||||
*/
|
*/
|
||||||
class BrokenExternalLink extends DataObject {
|
class BrokenExternalLink extends DataObject
|
||||||
|
{
|
||||||
|
|
||||||
private static $db = array(
|
private static $db = array(
|
||||||
'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer.
|
'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer.
|
||||||
'HTTPCode' =>'Int'
|
'HTTPCode' =>'Int'
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $has_one = array(
|
private static $has_one = array(
|
||||||
'Track' => 'BrokenExternalPageTrack',
|
'Track' => BrokenExternalPageTrack::class,
|
||||||
'Status' => 'BrokenExternalPageTrackStatus'
|
'Status' => BrokenExternalPageTrackStatus::class
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $summary_fields = array(
|
private static $summary_fields = array(
|
||||||
'Created' => 'Checked',
|
'Created' => 'Checked',
|
||||||
'Link' => 'External Link',
|
'Link' => 'External Link',
|
||||||
'HTTPCodeDescription' => 'HTTP Error Code',
|
'HTTPCodeDescription' => 'HTTP Error Code',
|
||||||
'Page.Title' => 'Page link is on'
|
'Page.Title' => 'Page link is on'
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $searchable_fields = array(
|
private static $searchable_fields = array(
|
||||||
'HTTPCode' => array('title' => 'HTTP Code')
|
'HTTPCode' => array('title' => 'HTTP Code')
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return SiteTree
|
* @return SiteTree
|
||||||
*/
|
*/
|
||||||
public function Page() {
|
public function Page()
|
||||||
return $this->Track()->Page();
|
{
|
||||||
}
|
return $this->Track()->Page();
|
||||||
|
}
|
||||||
|
|
||||||
public function canEdit($member = false) {
|
public function canEdit($member = false)
|
||||||
return false;
|
{
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function canView($member = false) {
|
public function canView($member = false)
|
||||||
$member = $member ? $member : Member::currentUser();
|
{
|
||||||
$codes = array('content-authors', 'administrators');
|
$member = $member ? $member : Member::currentUser();
|
||||||
return Permission::checkMember($member, $codes);
|
$codes = array('content-authors', 'administrators');
|
||||||
}
|
return Permission::checkMember($member, $codes);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a human readable description of a response code
|
* Retrieve a human readable description of a response code
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getHTTPCodeDescription() {
|
public function getHTTPCodeDescription()
|
||||||
$code = $this->HTTPCode;
|
{
|
||||||
if(empty($code)) {
|
$code = $this->HTTPCode;
|
||||||
// Assume that $code = 0 means there was no response
|
if (empty($code)) {
|
||||||
$description = _t('BrokenExternalLink.NOTAVAILABLE', 'Server Not Available');
|
// Assume that $code = 0 means there was no response
|
||||||
} elseif(
|
$description = _t('BrokenExternalLink.NOTAVAILABLE', 'Server Not Available');
|
||||||
($descriptions = Config::inst()->get('SS_HTTPResponse', 'status_codes'))
|
} elseif (($descriptions = Config::inst()->get(HTTPResponse::class, 'status_codes'))
|
||||||
&& isset($descriptions[$code])
|
&& isset($descriptions[$code])
|
||||||
) {
|
) {
|
||||||
$description = $descriptions[$code];
|
$description = $descriptions[$code];
|
||||||
} else {
|
} else {
|
||||||
$description = _t('BrokenExternalLink.UNKNOWNRESPONSE', 'Unknown Response Code');
|
$description = _t('BrokenExternalLink.UNKNOWNRESPONSE', 'Unknown Response Code');
|
||||||
}
|
}
|
||||||
return sprintf("%d (%s)", $code, $description);
|
return sprintf("%d (%s)", $code, $description);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,33 +2,37 @@
|
||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Model;
|
namespace SilverStripe\ExternalLinks\Model;
|
||||||
|
|
||||||
use DataObject;
|
use SilverStripe\CMS\Model\SiteTree;
|
||||||
use Versioned;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
|
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a track for a single page
|
* Represents a track for a single page
|
||||||
*/
|
*/
|
||||||
class BrokenExternalPageTrack extends DataObject {
|
class BrokenExternalPageTrack extends DataObject
|
||||||
|
{
|
||||||
|
|
||||||
private static $db = array(
|
private static $db = array(
|
||||||
'Processed' => 'Boolean'
|
'Processed' => 'Boolean'
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $has_one = array(
|
private static $has_one = array(
|
||||||
'Page' => 'SiteTree',
|
'Page' => SiteTree::class,
|
||||||
'Status' => 'BrokenExternalPageTrackStatus'
|
'Status' => BrokenExternalPageTrackStatus::class
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $has_many = array(
|
private static $has_many = array(
|
||||||
'BrokenLinks' => 'BrokenExternalLink'
|
'BrokenLinks' => BrokenExternalLink::class
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return SiteTree
|
* @return SiteTree
|
||||||
*/
|
*/
|
||||||
public function Page() {
|
public function Page()
|
||||||
return Versioned::get_by_stage('SiteTree', 'Stage')
|
{
|
||||||
->byID($this->PageID);
|
return Versioned::get_by_stage(SiteTree::class, 'Stage')
|
||||||
}
|
->byID($this->PageID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,11 @@
|
||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Model;
|
namespace SilverStripe\ExternalLinks\Model;
|
||||||
|
|
||||||
use DataObject;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
|
||||||
use Versioned;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
|
||||||
|
use SilverStripe\CMS\Model\SiteTree;
|
||||||
|
use SilverStripe\Versioned\Versioned;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the status of a track run
|
* Represents the status of a track run
|
||||||
|
@ -14,121 +16,133 @@ use Versioned;
|
||||||
* @property int $TotalPages Get total pages count
|
* @property int $TotalPages Get total pages count
|
||||||
* @property int $CompletedPages Get completed pages count
|
* @property int $CompletedPages Get completed pages count
|
||||||
*/
|
*/
|
||||||
class BrokenExternalPageTrackStatus extends DataObject {
|
class BrokenExternalPageTrackStatus extends DataObject
|
||||||
|
{
|
||||||
|
|
||||||
private static $db = array(
|
private static $db = array(
|
||||||
'Status' => 'Enum("Completed, Running", "Running")',
|
'Status' => 'Enum("Completed, Running", "Running")',
|
||||||
'JobInfo' => 'Varchar(255)'
|
'JobInfo' => 'Varchar(255)'
|
||||||
);
|
);
|
||||||
|
|
||||||
private static $has_many = array(
|
private static $has_many = array(
|
||||||
'TrackedPages' => 'BrokenExternalPageTrack',
|
'TrackedPages' => BrokenExternalPageTrack::class,
|
||||||
'BrokenLinks' => 'BrokenExternalLink'
|
'BrokenLinks' => BrokenExternalLink::class
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the latest track status
|
* Get the latest track status
|
||||||
*
|
*
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public static function get_latest() {
|
public static function get_latest()
|
||||||
return self::get()
|
{
|
||||||
->sort('ID', 'DESC')
|
return self::get()
|
||||||
->first();
|
->sort('ID', 'DESC')
|
||||||
}
|
->first();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the list of Pages yet to be checked
|
* Gets the list of Pages yet to be checked
|
||||||
*
|
*
|
||||||
* @return DataList
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
public function getIncompletePageList() {
|
public function getIncompletePageList()
|
||||||
$pageIDs = $this
|
{
|
||||||
->getIncompleteTracks()
|
$pageIDs = $this
|
||||||
->column('PageID');
|
->getIncompleteTracks()
|
||||||
if($pageIDs) return Versioned::get_by_stage('SiteTree', 'Stage')
|
->column('PageID');
|
||||||
->byIDs($pageIDs);
|
if ($pageIDs) {
|
||||||
}
|
return Versioned::get_by_stage(SiteTree::class, 'Stage')
|
||||||
|
->byIDs($pageIDs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of incomplete BrokenExternalPageTrack
|
* Get the list of incomplete BrokenExternalPageTrack
|
||||||
*
|
*
|
||||||
* @return DataList
|
* @return DataList
|
||||||
*/
|
*/
|
||||||
public function getIncompleteTracks() {
|
public function getIncompleteTracks()
|
||||||
return $this
|
{
|
||||||
->TrackedPages()
|
return $this
|
||||||
->filter('Processed', 0);
|
->TrackedPages()
|
||||||
}
|
->filter('Processed', 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get total pages count
|
* Get total pages count
|
||||||
*/
|
*/
|
||||||
public function getTotalPages() {
|
public function getTotalPages()
|
||||||
return $this->TrackedPages()->count();
|
{
|
||||||
}
|
return $this->TrackedPages()->count();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get completed pages count
|
* Get completed pages count
|
||||||
*/
|
*/
|
||||||
public function getCompletedPages() {
|
public function getCompletedPages()
|
||||||
return $this
|
{
|
||||||
->TrackedPages()
|
return $this
|
||||||
->filter('Processed', 1)
|
->TrackedPages()
|
||||||
->count();
|
->filter('Processed', 1)
|
||||||
}
|
->count();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the latest run, or otherwise creates a new one
|
* Returns the latest run, or otherwise creates a new one
|
||||||
*
|
*
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public static function get_or_create() {
|
public static function get_or_create()
|
||||||
// Check the current status
|
{
|
||||||
$status = self::get_latest();
|
// Check the current status
|
||||||
if ($status && $status->Status == 'Running') {
|
$status = self::get_latest();
|
||||||
$status->updateStatus();
|
if ($status && $status->Status == 'Running') {
|
||||||
return $status;
|
$status->updateStatus();
|
||||||
}
|
return $status;
|
||||||
|
}
|
||||||
|
|
||||||
return self::create_status();
|
return self::create_status();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create and prepare a new status
|
* Create and prepare a new status
|
||||||
*
|
*
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public static function create_status() {
|
public static function create_status()
|
||||||
// If the script is to be started create a new status
|
{
|
||||||
$status = self::create();
|
// If the script is to be started create a new status
|
||||||
$status->updateJobInfo('Creating new tracking object');
|
$status = self::create();
|
||||||
|
$status->updateJobInfo('Creating new tracking object');
|
||||||
|
|
||||||
// Setup all pages to test
|
// Setup all pages to test
|
||||||
$pageIDs = Versioned::get_by_stage('SiteTree', 'Stage')
|
$pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage')
|
||||||
->column('ID');
|
->column('ID');
|
||||||
foreach ($pageIDs as $pageID) {
|
foreach ($pageIDs as $pageID) {
|
||||||
$trackPage = BrokenExternalPageTrack::create();
|
$trackPage = BrokenExternalPageTrack::create();
|
||||||
$trackPage->PageID = $pageID;
|
$trackPage->PageID = $pageID;
|
||||||
$trackPage->StatusID = $status->ID;
|
$trackPage->StatusID = $status->ID;
|
||||||
$trackPage->write();
|
$trackPage->write();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $status;
|
return $status;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateJobInfo($message) {
|
public function updateJobInfo($message)
|
||||||
$this->JobInfo = $message;
|
{
|
||||||
$this->write();
|
$this->JobInfo = $message;
|
||||||
}
|
$this->write();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Self check status
|
* Self check status
|
||||||
*/
|
*/
|
||||||
public function updateStatus() {
|
public function updateStatus()
|
||||||
if ($this->CompletedPages == $this->TotalPages) {
|
{
|
||||||
$this->Status = 'Completed';
|
if ($this->CompletedPages == $this->TotalPages) {
|
||||||
$this->updateJobInfo('Setting to completed');
|
$this->Status = 'Completed';
|
||||||
}
|
$this->updateJobInfo('Setting to completed');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,13 +2,12 @@
|
||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Reports;
|
namespace SilverStripe\ExternalLinks\Reports;
|
||||||
|
|
||||||
use SS_Report;
|
use SilverStripe\Core\Convert;
|
||||||
use Convert;
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
use BrokenExternalPageTrackStatus;
|
use SilverStripe\ORM\ArrayList;
|
||||||
use ArrayList;
|
use SilverStripe\View\Requirements;
|
||||||
use Requirements;
|
use SilverStripe\Forms\LiteralField;
|
||||||
use LiteralField;
|
use SilverStripe\Reports\Report;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Content side-report listing pages with external broken links
|
* Content side-report listing pages with external broken links
|
||||||
|
@ -16,78 +15,86 @@ use LiteralField;
|
||||||
* @subpackage content
|
* @subpackage content
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class BrokenExternalLinksReport extends SS_Report {
|
class BrokenExternalLinksReport extends Report
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the report title
|
* Returns the report title
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function title() {
|
public function title()
|
||||||
return _t('ExternalBrokenLinksReport.EXTERNALBROKENLINKS', "External broken links report");
|
{
|
||||||
}
|
return _t('ExternalBrokenLinksReport.EXTERNALBROKENLINKS', "External broken links report");
|
||||||
|
}
|
||||||
|
|
||||||
public function columns() {
|
public function columns()
|
||||||
return array(
|
{
|
||||||
"Created" => "Checked",
|
return array(
|
||||||
'Link' => array(
|
"Created" => "Checked",
|
||||||
'title' => 'External Link',
|
'Link' => array(
|
||||||
'formatting' => function($value, $item) {
|
'title' => 'External Link',
|
||||||
return sprintf(
|
'formatting' => function ($value, $item) {
|
||||||
'<a target="_blank" href="%s">%s</a>',
|
return sprintf(
|
||||||
Convert::raw2att($item->Link),
|
'<a target="_blank" href="%s">%s</a>',
|
||||||
Convert::raw2xml($item->Link)
|
Convert::raw2att($item->Link),
|
||||||
);
|
Convert::raw2xml($item->Link)
|
||||||
}
|
);
|
||||||
),
|
}
|
||||||
'HTTPCodeDescription' => 'HTTP Error Code',
|
),
|
||||||
"Title" => array(
|
'HTTPCodeDescription' => 'HTTP Error Code',
|
||||||
"title" => 'Page link is on',
|
"Title" => array(
|
||||||
'formatting' => function($value, $item) {
|
"title" => 'Page link is on',
|
||||||
$page = $item->Page();
|
'formatting' => function ($value, $item) {
|
||||||
return sprintf(
|
$page = $item->Page();
|
||||||
'<a href="%s">%s</a>',
|
return sprintf(
|
||||||
Convert::raw2att($page->CMSEditLink()),
|
'<a href="%s">%s</a>',
|
||||||
Convert::raw2xml($page->Title)
|
Convert::raw2att($page->CMSEditLink()),
|
||||||
);
|
Convert::raw2xml($page->Title)
|
||||||
}
|
);
|
||||||
)
|
}
|
||||||
);
|
)
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias of columns(), to support the export to csv action
|
* Alias of columns(), to support the export to csv action
|
||||||
* in {@link GridFieldExportButton} generateExportFileData method.
|
* in {@link GridFieldExportButton} generateExportFileData method.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getColumns() {
|
public function getColumns()
|
||||||
return $this->columns();
|
{
|
||||||
}
|
return $this->columns();
|
||||||
|
}
|
||||||
|
|
||||||
public function sourceRecords() {
|
public function sourceRecords()
|
||||||
$track = BrokenExternalPageTrackStatus::get_latest();
|
{
|
||||||
if ($track) return $track->BrokenLinks();
|
$track = BrokenExternalPageTrackStatus::get_latest();
|
||||||
return new ArrayList();
|
if ($track) {
|
||||||
}
|
return $track->BrokenLinks();
|
||||||
|
}
|
||||||
|
return new ArrayList();
|
||||||
|
}
|
||||||
|
|
||||||
public function getCMSFields() {
|
public function getCMSFields()
|
||||||
Requirements::javascript('externallinks/javascript/BrokenExternalLinksReport.js');
|
{
|
||||||
$fields = parent::getCMSFields();
|
Requirements::javascript('externallinks/javascript/BrokenExternalLinksReport.js');
|
||||||
|
$fields = parent::getCMSFields();
|
||||||
|
|
||||||
$reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>';
|
$reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>';
|
||||||
$reportResult = new LiteralField('ResultTitle', $reportResultSpan);
|
$reportResult = new LiteralField('ResultTitle', $reportResultSpan);
|
||||||
$fields->push($reportResult);
|
$fields->push($reportResult);
|
||||||
|
|
||||||
$button = '<button id="externalLinksReport" type="button">%s</button>';
|
$button = '<button id="externalLinksReport" type="button">%s</button>';
|
||||||
$runReportButton = new LiteralField(
|
$runReportButton = new LiteralField(
|
||||||
'runReport',
|
'runReport',
|
||||||
sprintf(
|
sprintf(
|
||||||
$button,
|
$button,
|
||||||
_t('ExternalBrokenLinksReport.RUNREPORT', 'Create new report')
|
_t('ExternalBrokenLinksReport.RUNREPORT', 'Create new report')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$fields->push($runReportButton);
|
$fields->push($runReportButton);
|
||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,200 +2,229 @@
|
||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Tasks;
|
namespace SilverStripe\ExternalLinks\Tasks;
|
||||||
|
|
||||||
use BuildTask;
|
|
||||||
use Debug;
|
|
||||||
use BrokenExternalPageTrack;
|
|
||||||
use DOMNode;
|
use DOMNode;
|
||||||
use BrokenExternalLink;
|
|
||||||
use Config;
|
|
||||||
use BrokenExternalPageTrackStatus;
|
|
||||||
use Injector;
|
|
||||||
use DB;
|
|
||||||
|
|
||||||
|
|
||||||
class CheckExternalLinksTask extends BuildTask {
|
|
||||||
|
|
||||||
private static $dependencies = array(
|
|
||||||
'LinkChecker' => '%$LinkChecker'
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected $silent = false;
|
|
||||||
|
|
||||||
/**
|
use SilverStripe\Dev\Debug;
|
||||||
* @var LinkChecker
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
|
||||||
*/
|
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
|
||||||
protected $linkChecker;
|
use SilverStripe\Core\Config\Config;
|
||||||
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
use SilverStripe\Dev\BuildTask;
|
||||||
|
|
||||||
protected $title = 'Checking broken External links in the SiteTree';
|
class CheckExternalLinksTask extends BuildTask
|
||||||
|
{
|
||||||
|
|
||||||
protected $description = 'A task that records external broken links in the SiteTree';
|
private static $dependencies = array(
|
||||||
|
'LinkChecker' => '%$LinkChecker'
|
||||||
|
);
|
||||||
|
|
||||||
protected $enabled = true;
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $silent = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log a message
|
* @var LinkChecker
|
||||||
*
|
*/
|
||||||
* @param string $message
|
protected $linkChecker;
|
||||||
*/
|
|
||||||
protected function log($message) {
|
|
||||||
if(!$this->silent) Debug::message($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function run($request) {
|
protected $title = 'Checking broken External links in the SiteTree';
|
||||||
$this->runLinksCheck();
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Turn on or off message output
|
|
||||||
*
|
|
||||||
* @param bool $silent
|
|
||||||
*/
|
|
||||||
public function setSilent($silent) {
|
|
||||||
$this->silent = $silent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
protected $description = 'A task that records external broken links in the SiteTree';
|
||||||
* @param LinkChecker $linkChecker
|
|
||||||
*/
|
|
||||||
public function setLinkChecker(LinkChecker $linkChecker) {
|
|
||||||
$this->linkChecker = $linkChecker;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
protected $enabled = true;
|
||||||
* @return LinkChecker
|
|
||||||
*/
|
|
||||||
public function getLinkChecker() {
|
|
||||||
return $this->linkChecker;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check the status of a single link on a page
|
* Log a message
|
||||||
*
|
*
|
||||||
* @param BrokenExternalPageTrack $pageTrack
|
* @param string $message
|
||||||
* @param DOMNode $link
|
*/
|
||||||
*/
|
protected function log($message)
|
||||||
protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link) {
|
{
|
||||||
$class = $link->getAttribute('class');
|
if (!$this->silent) {
|
||||||
$href = $link->getAttribute('href');
|
Debug::message($message);
|
||||||
$markedBroken = preg_match('/\b(ss-broken)\b/', $class);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check link
|
public function run($request)
|
||||||
$httpCode = $this->linkChecker->checkLink($href);
|
{
|
||||||
if($httpCode === null) return; // Null link means uncheckable, such as an internal link
|
$this->runLinksCheck();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Turn on or off message output
|
||||||
|
*
|
||||||
|
* @param bool $silent
|
||||||
|
*/
|
||||||
|
public function setSilent($silent)
|
||||||
|
{
|
||||||
|
$this->silent = $silent;
|
||||||
|
}
|
||||||
|
|
||||||
// If this code is broken then mark as such
|
/**
|
||||||
if($foundBroken = $this->isCodeBroken($httpCode)) {
|
* @param LinkChecker $linkChecker
|
||||||
// Create broken record
|
*/
|
||||||
$brokenLink = new BrokenExternalLink();
|
public function setLinkChecker(LinkChecker $linkChecker)
|
||||||
$brokenLink->Link = $href;
|
{
|
||||||
$brokenLink->HTTPCode = $httpCode;
|
$this->linkChecker = $linkChecker;
|
||||||
$brokenLink->TrackID = $pageTrack->ID;
|
}
|
||||||
$brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons
|
|
||||||
$brokenLink->write();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we need to update CSS class, otherwise return
|
/**
|
||||||
if($markedBroken == $foundBroken) return;
|
* @return LinkChecker
|
||||||
if($foundBroken) {
|
*/
|
||||||
$class .= ' ss-broken';
|
public function getLinkChecker()
|
||||||
} else {
|
{
|
||||||
$class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class);
|
return $this->linkChecker;
|
||||||
}
|
}
|
||||||
$link->setAttribute('class', trim($class));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the given HTTP code is "broken"
|
* Check the status of a single link on a page
|
||||||
*
|
*
|
||||||
* @param int $httpCode
|
* @param BrokenExternalPageTrack $pageTrack
|
||||||
* @return bool True if this is a broken code
|
* @param DOMNode $link
|
||||||
*/
|
*/
|
||||||
protected function isCodeBroken($httpCode) {
|
protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link)
|
||||||
// Null represents no request attempted
|
{
|
||||||
if($httpCode === null) return false;
|
$class = $link->getAttribute('class');
|
||||||
|
$href = $link->getAttribute('href');
|
||||||
|
$markedBroken = preg_match('/\b(ss-broken)\b/', $class);
|
||||||
|
|
||||||
// do we have any whitelisted codes
|
// Check link
|
||||||
$ignoreCodes = Config::inst()->get('CheckExternalLinks', 'IgnoreCodes');
|
$httpCode = $this->linkChecker->checkLink($href);
|
||||||
if(is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) return false;
|
if ($httpCode === null) {
|
||||||
|
return; // Null link means uncheckable, such as an internal link
|
||||||
|
}
|
||||||
|
|
||||||
// Check if code is outside valid range
|
// If this code is broken then mark as such
|
||||||
return $httpCode < 200 || $httpCode > 302;
|
if ($foundBroken = $this->isCodeBroken($httpCode)) {
|
||||||
}
|
// Create broken record
|
||||||
|
$brokenLink = new BrokenExternalLink();
|
||||||
|
$brokenLink->Link = $href;
|
||||||
|
$brokenLink->HTTPCode = $httpCode;
|
||||||
|
$brokenLink->TrackID = $pageTrack->ID;
|
||||||
|
$brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons
|
||||||
|
$brokenLink->write();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
// Check if we need to update CSS class, otherwise return
|
||||||
* Runs the links checker and returns the track used
|
if ($markedBroken == $foundBroken) {
|
||||||
*
|
return;
|
||||||
* @param int $limit Limit to number of pages to run, or null to run all
|
}
|
||||||
* @return BrokenExternalPageTrackStatus
|
if ($foundBroken) {
|
||||||
*/
|
$class .= ' ss-broken';
|
||||||
public function runLinksCheck($limit = null) {
|
} else {
|
||||||
// Check the current status
|
$class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class);
|
||||||
$status = BrokenExternalPageTrackStatus::get_or_create();
|
}
|
||||||
|
$link->setAttribute('class', trim($class));
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate pages to run
|
/**
|
||||||
$pageTracks = $status->getIncompleteTracks();
|
* Determine if the given HTTP code is "broken"
|
||||||
if($limit) $pageTracks = $pageTracks->limit($limit);
|
*
|
||||||
|
* @param int $httpCode
|
||||||
|
* @return bool True if this is a broken code
|
||||||
|
*/
|
||||||
|
protected function isCodeBroken($httpCode)
|
||||||
|
{
|
||||||
|
// Null represents no request attempted
|
||||||
|
if ($httpCode === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Check each page
|
// do we have any whitelisted codes
|
||||||
foreach ($pageTracks as $pageTrack) {
|
$ignoreCodes = Config::inst()->get('CheckExternalLinks', 'IgnoreCodes');
|
||||||
// Flag as complete
|
if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) {
|
||||||
$pageTrack->Processed = 1;
|
return false;
|
||||||
$pageTrack->write();
|
}
|
||||||
|
|
||||||
// Check value of html area
|
// Check if code is outside valid range
|
||||||
$page = $pageTrack->Page();
|
return $httpCode < 200 || $httpCode > 302;
|
||||||
$this->log("Checking {$page->Title}");
|
}
|
||||||
$htmlValue = Injector::inst()->create('HTMLValue', $page->Content);
|
|
||||||
if (!$htmlValue->isValid()) continue;
|
|
||||||
|
|
||||||
// Check each link
|
/**
|
||||||
$links = $htmlValue->getElementsByTagName('a');
|
* Runs the links checker and returns the track used
|
||||||
foreach($links as $link) {
|
*
|
||||||
$this->checkPageLink($pageTrack, $link);
|
* @param int $limit Limit to number of pages to run, or null to run all
|
||||||
}
|
* @return BrokenExternalPageTrackStatus
|
||||||
|
*/
|
||||||
|
public function runLinksCheck($limit = null)
|
||||||
|
{
|
||||||
|
// Check the current status
|
||||||
|
$status = BrokenExternalPageTrackStatus::get_or_create();
|
||||||
|
|
||||||
// Update content of page based on link fixes / breakages
|
// Calculate pages to run
|
||||||
$htmlValue->saveHTML();
|
$pageTracks = $status->getIncompleteTracks();
|
||||||
$page->Content = $htmlValue->getContent();
|
if ($limit) {
|
||||||
$page->write();
|
$pageTracks = $pageTracks->limit($limit);
|
||||||
|
}
|
||||||
|
|
||||||
// Once all links have been created for this page update HasBrokenLinks
|
// Check each page
|
||||||
$count = $pageTrack->BrokenLinks()->count();
|
foreach ($pageTracks as $pageTrack) {
|
||||||
$this->log("Found {$count} broken links");
|
// Flag as complete
|
||||||
if($count) {
|
$pageTrack->Processed = 1;
|
||||||
// Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
|
$pageTrack->write();
|
||||||
DB::query(sprintf(
|
|
||||||
'UPDATE "SiteTree" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'',
|
|
||||||
intval($pageTrack->ID)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$status->updateJobInfo('Updating completed pages');
|
// Check value of html area
|
||||||
$status->updateStatus();
|
$page = $pageTrack->Page();
|
||||||
return $status;
|
$this->log("Checking {$page->Title}");
|
||||||
}
|
$htmlValue = Injector::inst()->create('HTMLValue', $page->Content);
|
||||||
|
if (!$htmlValue->isValid()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
private function updateCompletedPages($trackID = 0) {
|
// Check each link
|
||||||
$noPages = BrokenExternalPageTrack::get()
|
$links = $htmlValue->getElementsByTagName('a');
|
||||||
->filter(array(
|
foreach ($links as $link) {
|
||||||
'TrackID' => $trackID,
|
$this->checkPageLink($pageTrack, $link);
|
||||||
'Processed' => 1
|
}
|
||||||
))
|
|
||||||
->count();
|
|
||||||
$track = BrokenExternalPageTrackStatus::get_latest();
|
|
||||||
$track->CompletedPages = $noPages;
|
|
||||||
$track->write();
|
|
||||||
return $noPages;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function updateJobInfo($message) {
|
// Update content of page based on link fixes / breakages
|
||||||
$track = BrokenExternalPageTrackStatus::get_latest();
|
$htmlValue->saveHTML();
|
||||||
if($track) {
|
$page->Content = $htmlValue->getContent();
|
||||||
$track->JobInfo = $message;
|
$page->write();
|
||||||
$track->write();
|
|
||||||
}
|
// Once all links have been created for this page update HasBrokenLinks
|
||||||
}
|
$count = $pageTrack->BrokenLinks()->count();
|
||||||
|
$this->log("Found {$count} broken links");
|
||||||
|
if ($count) {
|
||||||
|
// Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
|
||||||
|
DB::query(sprintf(
|
||||||
|
'UPDATE "SiteTree" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'',
|
||||||
|
intval($pageTrack->ID)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$status->updateJobInfo('Updating completed pages');
|
||||||
|
$status->updateStatus();
|
||||||
|
return $status;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateCompletedPages($trackID = 0)
|
||||||
|
{
|
||||||
|
$noPages = BrokenExternalPageTrack::get()
|
||||||
|
->filter(array(
|
||||||
|
'TrackID' => $trackID,
|
||||||
|
'Processed' => 1
|
||||||
|
))
|
||||||
|
->count();
|
||||||
|
$track = BrokenExternalPageTrackStatus::get_latest();
|
||||||
|
$track->CompletedPages = $noPages;
|
||||||
|
$track->write();
|
||||||
|
return $noPages;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateJobInfo($message)
|
||||||
|
{
|
||||||
|
$track = BrokenExternalPageTrackStatus::get_latest();
|
||||||
|
if ($track) {
|
||||||
|
$track->JobInfo = $message;
|
||||||
|
$track->write();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,51 +4,57 @@ namespace SilverStripe\ExternalLinks\Tasks;
|
||||||
|
|
||||||
use SS_Cache;
|
use SS_Cache;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check links using curl
|
* Check links using curl
|
||||||
*/
|
*/
|
||||||
class CurlLinkChecker implements LinkChecker {
|
class CurlLinkChecker implements LinkChecker
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return cache
|
* Return cache
|
||||||
*
|
*
|
||||||
* @return Zend_Cache_Frontend
|
* @return Zend_Cache_Frontend
|
||||||
*/
|
*/
|
||||||
protected function getCache() {
|
protected function getCache()
|
||||||
return SS_Cache::factory(
|
{
|
||||||
__CLASS__,
|
return SS_Cache::factory(
|
||||||
'Output',
|
__CLASS__,
|
||||||
array('automatic_serialization' => true)
|
'Output',
|
||||||
);
|
array('automatic_serialization' => true)
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine the http status code for a given link
|
* Determine the http status code for a given link
|
||||||
*
|
*
|
||||||
* @param string $href URL to check
|
* @param string $href URL to check
|
||||||
* @return int HTTP status code, or null if not checkable (not a link)
|
* @return int HTTP status code, or null if not checkable (not a link)
|
||||||
*/
|
*/
|
||||||
public function checkLink($href) {
|
public function checkLink($href)
|
||||||
// Skip non-external links
|
{
|
||||||
if(!preg_match('/^https?[^:]*:\/\//', $href)) return null;
|
// Skip non-external links
|
||||||
|
if (!preg_match('/^https?[^:]*:\/\//', $href)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if we have a cached result
|
// Check if we have a cached result
|
||||||
$cacheKey = md5($href);
|
$cacheKey = md5($href);
|
||||||
$result = $this->getCache()->load($cacheKey);
|
$result = $this->getCache()->load($cacheKey);
|
||||||
if($result !== false) return $result;
|
if ($result !== false) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
// No cached result so just request
|
// No cached result so just request
|
||||||
$handle = curl_init($href);
|
$handle = curl_init($href);
|
||||||
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
|
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
|
||||||
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
|
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
|
||||||
curl_exec($handle);
|
curl_exec($handle);
|
||||||
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
|
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
|
||||||
curl_close($handle);
|
curl_close($handle);
|
||||||
|
|
||||||
// Cache result
|
// Cache result
|
||||||
$this->getCache()->save($httpCode, $cacheKey);
|
$this->getCache()->save($httpCode, $cacheKey);
|
||||||
return $httpCode;
|
return $httpCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,17 @@
|
||||||
|
|
||||||
namespace SilverStripe\ExternalLinks\Tasks;
|
namespace SilverStripe\ExternalLinks\Tasks;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides an interface for checking that a link is valid
|
* Provides an interface for checking that a link is valid
|
||||||
*/
|
*/
|
||||||
interface LinkChecker {
|
interface LinkChecker
|
||||||
|
{
|
||||||
/**
|
|
||||||
* Determine the http status code for a given link
|
/**
|
||||||
*
|
* Determine the http status code for a given link
|
||||||
* @param string $href URL to check
|
*
|
||||||
* @return int HTTP status code, or null if not checkable (not a link)
|
* @param string $href URL to check
|
||||||
*/
|
* @return int HTTP status code, or null if not checkable (not a link)
|
||||||
public function checkLink($href);
|
*/
|
||||||
|
public function checkLink($href);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,148 +1,165 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
class ExternalLinksTest extends SapphireTest {
|
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
|
||||||
|
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
|
||||||
|
use SilverStripe\i18n\i18n;
|
||||||
|
use SilverStripe\Reports\Report;
|
||||||
|
use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
|
||||||
protected static $fixture_file = 'ExternalLinksTest.yml';
|
class ExternalLinksTest extends SapphireTest
|
||||||
|
{
|
||||||
|
|
||||||
protected $extraDataObjects = array(
|
protected static $fixture_file = 'ExternalLinksTest.yml';
|
||||||
'ExternalLinksTestPage'
|
|
||||||
);
|
protected $extraDataObjects = array(
|
||||||
|
'ExternalLinksTestPage'
|
||||||
|
);
|
||||||
|
|
||||||
protected $illegalExtensions = array(
|
protected $illegalExtensions = array(
|
||||||
'SiteTree' => array('Translatable')
|
'SiteTree' => array('Translatable')
|
||||||
);
|
);
|
||||||
|
|
||||||
public function setUpOnce() {
|
public function setUpOnce()
|
||||||
if (class_exists('Phockito')) {
|
{
|
||||||
Phockito::include_hamcrest(false);
|
if (class_exists('Phockito')) {
|
||||||
}
|
Phockito::include_hamcrest(false);
|
||||||
|
}
|
||||||
|
|
||||||
parent::setUpOnce();
|
parent::setUpOnce();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp()
|
||||||
parent::setUp();
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
// Check dependencies
|
// Check dependencies
|
||||||
if (!class_exists('Phockito')) {
|
if (!class_exists('Phockito')) {
|
||||||
$this->skipTest = true;
|
$this->skipTest = true;
|
||||||
return $this->markTestSkipped("These tests need the Phockito module installed to run");
|
return $this->markTestSkipped("These tests need the Phockito module installed to run");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mock link checker
|
// Mock link checker
|
||||||
$checker = Phockito::mock('LinkChecker');
|
$checker = Phockito::mock(LinkChecker::class);
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('http://www.working.com')
|
->checkLink('http://www.working.com')
|
||||||
->return(200);
|
->return(200);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('http://www.broken.com/url/thing') // 404 on working site
|
->checkLink('http://www.broken.com/url/thing') // 404 on working site
|
||||||
->return(404);
|
->return(404);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('http://www.broken.com') // 403 on working site
|
->checkLink('http://www.broken.com') // 403 on working site
|
||||||
->return(403);
|
->return(403);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('http://www.nodomain.com') // no ping
|
->checkLink('http://www.nodomain.com') // no ping
|
||||||
->return(0);
|
->return(0);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('/internal/link')
|
->checkLink('/internal/link')
|
||||||
->return(null);
|
->return(null);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('[sitetree_link,id=9999]')
|
->checkLink('[sitetree_link,id=9999]')
|
||||||
->return(null);
|
->return(null);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('home')
|
->checkLink('home')
|
||||||
->return(null);
|
->return(null);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('broken-internal')
|
->checkLink('broken-internal')
|
||||||
->return(null);
|
->return(null);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink('[sitetree_link,id=1]')
|
->checkLink('[sitetree_link,id=1]')
|
||||||
->return(null);
|
->return(null);
|
||||||
|
|
||||||
Phockito::when($checker)
|
Phockito::when($checker)
|
||||||
->checkLink(Hamcrest_Matchers::anything()) // anything else is 404
|
->checkLink(Hamcrest_Matchers::anything()) // anything else is 404
|
||||||
->return(404);
|
->return(404);
|
||||||
|
|
||||||
Injector::inst()->registerService($checker, 'LinkChecker');
|
Injector::inst()->registerService($checker, LinkChecker::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLinks() {
|
public function testLinks()
|
||||||
// Run link checker
|
{
|
||||||
$task = CheckExternalLinksTask::create();
|
// Run link checker
|
||||||
$task->setSilent(true); // Be quiet during the test!
|
$task = CheckExternalLinksTask::create();
|
||||||
$task->runLinksCheck();
|
$task->setSilent(true); // Be quiet during the test!
|
||||||
|
$task->runLinksCheck();
|
||||||
|
|
||||||
// Get all links checked
|
// Get all links checked
|
||||||
$status = BrokenExternalPageTrackStatus::get_latest();
|
$status = BrokenExternalPageTrackStatus::get_latest();
|
||||||
$this->assertEquals('Completed', $status->Status);
|
$this->assertEquals('Completed', $status->Status);
|
||||||
$this->assertEquals(5, $status->TotalPages);
|
$this->assertEquals(5, $status->TotalPages);
|
||||||
$this->assertEquals(5, $status->CompletedPages);
|
$this->assertEquals(5, $status->CompletedPages);
|
||||||
|
|
||||||
// Check all pages have had the correct HTML adjusted
|
// Check all pages have had the correct HTML adjusted
|
||||||
for($i = 1; $i <= 5; $i++) {
|
for ($i = 1; $i <= 5; $i++) {
|
||||||
$page = $this->objFromFixture('ExternalLinksTestPage', 'page'.$i);
|
$page = $this->objFromFixture('ExternalLinksTestPage', 'page'.$i);
|
||||||
$this->assertNotEmpty($page->Content);
|
$this->assertNotEmpty($page->Content);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$page->ExpectedContent,
|
$page->ExpectedContent,
|
||||||
$page->Content,
|
$page->Content,
|
||||||
"Assert that the content of page{$i} has been updated"
|
"Assert that the content of page{$i} has been updated"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the correct report of broken links is generated
|
// Check that the correct report of broken links is generated
|
||||||
$links = $status
|
$links = $status
|
||||||
->BrokenLinks()
|
->BrokenLinks()
|
||||||
->sort('Link');
|
->sort('Link');
|
||||||
|
|
||||||
$this->assertEquals(4, $links->count());
|
$this->assertEquals(4, $links->count());
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
array(
|
array(
|
||||||
'http://www.broken.com',
|
'http://www.broken.com',
|
||||||
'http://www.broken.com/url/thing',
|
'http://www.broken.com/url/thing',
|
||||||
'http://www.broken.com/url/thing',
|
'http://www.broken.com/url/thing',
|
||||||
'http://www.nodomain.com'
|
'http://www.nodomain.com'
|
||||||
),
|
),
|
||||||
array_values($links->map('ID', 'Link')->toArray())
|
array_values($links->map('ID', 'Link')->toArray())
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check response codes are correct
|
// Check response codes are correct
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'http://www.broken.com' => 403,
|
'http://www.broken.com' => 403,
|
||||||
'http://www.broken.com/url/thing' => 404,
|
'http://www.broken.com/url/thing' => 404,
|
||||||
'http://www.nodomain.com' => 0
|
'http://www.nodomain.com' => 0
|
||||||
);
|
);
|
||||||
$actual = $links->map('Link', 'HTTPCode')->toArray();
|
$actual = $links->map('Link', 'HTTPCode')->toArray();
|
||||||
$this->assertEquals($expected, $actual);
|
$this->assertEquals($expected, $actual);
|
||||||
|
|
||||||
// Check response descriptions are correct
|
// Check response descriptions are correct
|
||||||
i18n::set_locale('en_NZ');
|
i18n::set_locale('en_NZ');
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'http://www.broken.com' => '403 (Forbidden)',
|
'http://www.broken.com' => '403 (Forbidden)',
|
||||||
'http://www.broken.com/url/thing' => '404 (Not Found)',
|
'http://www.broken.com/url/thing' => '404 (Not Found)',
|
||||||
'http://www.nodomain.com' => '0 (Server Not Available)'
|
'http://www.nodomain.com' => '0 (Server Not Available)'
|
||||||
);
|
);
|
||||||
$actual = $links->map('Link', 'HTTPCodeDescription')->toArray();
|
$actual = $links->map('Link', 'HTTPCodeDescription')->toArray();
|
||||||
$this->assertEquals($expected, $actual);
|
$this->assertEquals($expected, $actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test that broken links appears in the reports list
|
* Test that broken links appears in the reports list
|
||||||
*/
|
*/
|
||||||
public function testReportExists() {
|
public function testReportExists()
|
||||||
$reports = SS_Report::get_reports();
|
{
|
||||||
$reportNames = array();
|
$reports = Report::get_reports();
|
||||||
foreach($reports as $report) {
|
$reportNames = array();
|
||||||
$reportNames[] = $report->class;
|
foreach ($reports as $report) {
|
||||||
}
|
$reportNames[] = $report->class;
|
||||||
$this->assertContains('BrokenExternalLinksReport',$reportNames,
|
}
|
||||||
'BrokenExternalLinksReport is in reports list');
|
$this->assertContains(
|
||||||
}
|
BrokenExternalLinksReport::class,
|
||||||
|
$reportNames,
|
||||||
|
'BrokenExternalLinksReport is in reports list'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
class ExternalLinksTestPage extends Page implements TestOnly
|
class ExternalLinksTestPage extends Page implements TestOnly
|
||||||
{
|
{
|
||||||
private static $db = array(
|
private static $db = array(
|
||||||
|
|
Loading…
Reference in New Issue