2014-07-28 01:23:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CMSExternalLinks_Controller extends Controller {
|
|
|
|
|
2014-07-31 06:49:20 +02:00
|
|
|
private static $allowed_actions = array('getJobStatus', 'start');
|
2014-07-28 01:23:33 +02:00
|
|
|
|
2014-07-30 02:34:39 +02:00
|
|
|
/*
|
|
|
|
* Respond to Ajax requests for info on a running job
|
|
|
|
*
|
|
|
|
* @return string JSON string detailing status of the job
|
|
|
|
*/
|
|
|
|
public function getJobStatus() {
|
2014-07-31 06:49:20 +02:00
|
|
|
$track = CheckExternalLinks::getLatestTrack();
|
|
|
|
if (!$track || !$track->exists()) return null;
|
2014-07-30 02:34:39 +02:00
|
|
|
echo json_encode(array(
|
2014-07-31 06:49:20 +02:00
|
|
|
'TrackID' => $track->ID,
|
|
|
|
'Status' => $track->Status,
|
|
|
|
'Completed' => $track->CompletedPages,
|
|
|
|
'Total' => $track->TotalPages
|
2014-07-30 02:34:39 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Starts a broken external link check
|
|
|
|
*/
|
|
|
|
public function start() {
|
2014-07-31 06:49:20 +02:00
|
|
|
$status = checkExternalLinks::getLatestTrackStatus();
|
|
|
|
// return if the a job is already running
|
|
|
|
if ($status == 'Running') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (class_exists('QueuedJobService')) {
|
2014-08-04 07:38:28 +02:00
|
|
|
$pages = Versioned::get_by_stage('SiteTree', 'Stage');
|
|
|
|
$noPages = count($pages);
|
|
|
|
|
|
|
|
$track = BrokenExternalPageTrackStatus::create();
|
|
|
|
$track->TotalPages = $noPages;
|
|
|
|
$track->Status = 'Running';
|
|
|
|
$track->write();
|
|
|
|
|
|
|
|
foreach ($pages as $page) {
|
|
|
|
$trackPage = BrokenExternalPageTrack::create();
|
|
|
|
$trackPage->PageID = $page->ID;
|
|
|
|
$trackPage->TrackID = $track->ID;
|
|
|
|
$trackPage->write();
|
|
|
|
}
|
|
|
|
|
2014-07-31 06:49:20 +02:00
|
|
|
$checkLinks = new CheckExternalLinksJob();
|
|
|
|
singleton('QueuedJobService')
|
|
|
|
->queueJob($checkLinks, date('Y-m-d H:i:s', time() + 1));
|
|
|
|
} 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 = new CheckExternalLinks();
|
|
|
|
$task->run();
|
|
|
|
}
|
2014-07-28 01:23:33 +02:00
|
|
|
}
|
|
|
|
}
|