Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 25, 2015
1 parent fe8d93c commit 4d08953
Show file tree
Hide file tree
Showing 10 changed files with 771 additions and 693 deletions.
97 changes: 52 additions & 45 deletions code/controllers/CMSExternalLinks.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
<?php

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
*
* @return string JSON string detailing status of the job
*/
public function getJobStatus() {
// Set headers
HTTP::set_cache_age(0);
HTTP::add_cache_headers($this->response);
$this->response
->addHeader('Content-Type', 'application/json')
->addHeader('Content-Encoding', 'UTF-8')
->addHeader('X-Content-Type-Options', 'nosniff');
/*
* Respond to Ajax requests for info on a running job
*
* @return string JSON string detailing status of the job
*/
public function getJobStatus()
{
// Set headers
HTTP::set_cache_age(0);
HTTP::add_cache_headers($this->response);
$this->response
->addHeader('Content-Type', 'application/json')
->addHeader('Content-Encoding', 'UTF-8')
->addHeader('X-Content-Type-Options', 'nosniff');

// Format status
$track = BrokenExternalPageTrackStatus::get_latest();
if($track) return json_encode(array(
'TrackID' => $track->ID,
'Status' => $track->Status,
'Completed' => $track->getCompletedPages(),
'Total' => $track->getTotalPages()
));
}
// Format status
$track = BrokenExternalPageTrackStatus::get_latest();
if ($track) {
return json_encode(array(
'TrackID' => $track->ID,
'Status' => $track->Status,
'Completed' => $track->getCompletedPages(),
'Total' => $track->getTotalPages()
));
}
}


/*
* Starts a broken external link check
*/
public function start() {
// return if the a job is already running
$status = BrokenExternalPageTrackStatus::get_latest();
if ($status && $status->Status == 'Running') return;
/*
* Starts a broken external link check
*/
public function start()
{
// return if the a job is already running
$status = BrokenExternalPageTrackStatus::get_latest();
if ($status && $status->Status == 'Running') {
return;
}

// Create a new job
if (class_exists('QueuedJobService')) {
// Force the creation of a new run
BrokenExternalPageTrackStatus::create_status();
$checkLinks = new CheckExternalLinksJob();
singleton('QueuedJobService')->queueJob($checkLinks);
} else {
//TODO this hangs as it waits for the connection to be released
// should return back and continue processing
// http://us3.php.net/manual/en/features.connection-handling.php
$task = CheckExternalLinksTask::create();
$task->runLinksCheck();
}
}
// Create a new job
if (class_exists('QueuedJobService')) {
// Force the creation of a new run
BrokenExternalPageTrackStatus::create_status();
$checkLinks = new CheckExternalLinksJob();
singleton('QueuedJobService')->queueJob($checkLinks);
} else {
//TODO this hangs as it waits for the connection to be released
// should return back and continue processing
// http://us3.php.net/manual/en/features.connection-handling.php
$task = CheckExternalLinksTask::create();
$task->runLinksCheck();
}
}
}
50 changes: 28 additions & 22 deletions code/jobs/CheckExternalLinksJob.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
<?php

if(!class_exists('AbstractQueuedJob')) return;
if (!class_exists('AbstractQueuedJob')) {
return;
}

/**
* A Job for running a external link check for published pages
*
*/
class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob {

public function getTitle() {
return _t('CheckExternalLiksJob.TITLE', 'Checking for external broken links');
}
class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob
{

public function getJobType() {
return QueuedJob::QUEUED;
}
public function getTitle()
{
return _t('CheckExternalLiksJob.TITLE', 'Checking for external broken links');
}

public function getSignature() {
return md5(get_class($this));
}
public function getJobType()
{
return QueuedJob::QUEUED;
}

/**
* 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';
}
public function getSignature()
{
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';
}
}
111 changes: 57 additions & 54 deletions code/model/BrokenExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,69 @@
* @method BrokenExternalPageTrack Track()
* @method BrokenExternalPageTrackStatus Status()
*/
class BrokenExternalLink extends DataObject {
class BrokenExternalLink extends DataObject
{

private static $db = array(
'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer.
'HTTPCode' =>'Int'
);
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(
'Track' => 'BrokenExternalPageTrack',
'Status' => 'BrokenExternalPageTrackStatus'
);
private static $has_one = array(
'Track' => 'BrokenExternalPageTrack',
'Status' => 'BrokenExternalPageTrackStatus'
);

private static $summary_fields = array(
'Created' => 'Checked',
'Link' => 'External Link',
'HTTPCodeDescription' => 'HTTP Error Code',
'Page.Title' => 'Page link is on'
);
private static $summary_fields = array(
'Created' => 'Checked',
'Link' => 'External Link',
'HTTPCodeDescription' => 'HTTP Error Code',
'Page.Title' => 'Page link is on'
);

private static $searchable_fields = array(
'HTTPCode' => array('title' => 'HTTP Code')
);
private static $searchable_fields = array(
'HTTPCode' => array('title' => 'HTTP Code')
);

/**
* @return SiteTree
*/
public function Page() {
return $this->Track()->Page();
}
/**
* @return SiteTree
*/
public function Page()
{
return $this->Track()->Page();
}

public function canEdit($member = false) {
return false;
}
public function canEdit($member = false)
{
return false;
}

public function canView($member = false) {
$member = $member ? $member : Member::currentUser();
$codes = array('content-authors', 'administrators');
return Permission::checkMember($member, $codes);
}
public function canView($member = false)
{
$member = $member ? $member : Member::currentUser();
$codes = array('content-authors', 'administrators');
return Permission::checkMember($member, $codes);
}

/**
* Retrieve a human readable description of a response code
*
* @return string
*/
public function getHTTPCodeDescription() {
$code = $this->HTTPCode;
if(empty($code)) {
// Assume that $code = 0 means there was no response
$description = _t('BrokenExternalLink.NOTAVAILABLE', 'Server Not Available');
} elseif(
($descriptions = Config::inst()->get('SS_HTTPResponse', 'status_codes'))
&& isset($descriptions[$code])
) {
$description = $descriptions[$code];
} else {
$description = _t('BrokenExternalLink.UNKNOWNRESPONSE', 'Unknown Response Code');
}
return sprintf("%d (%s)", $code, $description);
}
/**
* Retrieve a human readable description of a response code
*
* @return string
*/
public function getHTTPCodeDescription()
{
$code = $this->HTTPCode;
if (empty($code)) {
// Assume that $code = 0 means there was no response
$description = _t('BrokenExternalLink.NOTAVAILABLE', 'Server Not Available');
} elseif (
($descriptions = Config::inst()->get('SS_HTTPResponse', 'status_codes'))
&& isset($descriptions[$code])
) {
$description = $descriptions[$code];
} else {
$description = _t('BrokenExternalLink.UNKNOWNRESPONSE', 'Unknown Response Code');
}
return sprintf("%d (%s)", $code, $description);
}
}


38 changes: 20 additions & 18 deletions code/model/BrokenExternalPageTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
/**
* Represents a track for a single page
*/
class BrokenExternalPageTrack extends DataObject {
class BrokenExternalPageTrack extends DataObject
{

private static $db = array(
'Processed' => 'Boolean'
);
private static $db = array(
'Processed' => 'Boolean'
);

private static $has_one = array(
'Page' => 'SiteTree',
'Status' => 'BrokenExternalPageTrackStatus'
);
private static $has_one = array(
'Page' => 'SiteTree',
'Status' => 'BrokenExternalPageTrackStatus'
);

private static $has_many = array(
'BrokenLinks' => 'BrokenExternalLink'
);
private static $has_many = array(
'BrokenLinks' => 'BrokenExternalLink'
);

/**
* @return SiteTree
*/
public function Page() {
return Versioned::get_by_stage('SiteTree', 'Stage')
->byID($this->PageID);
}
/**
* @return SiteTree
*/
public function Page()
{
return Versioned::get_by_stage('SiteTree', 'Stage')
->byID($this->PageID);
}
}
Loading

0 comments on commit 4d08953

Please sign in to comment.