-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,69 @@ | |
|
||
class CMSExternalLinks_Controller extends Controller { | ||
|
||
private static $allowed_actions = array('createQueuedReport'); | ||
private static $allowed_actions = array('getJobStatus', 'clear', 'start'); | ||
|
||
/* | ||
* Respond to Ajax requests for info on a running job | ||
* also calls continueJob and clear depending on the status of the job | ||
* | ||
* @return string JSON string detailing status of the job | ||
*/ | ||
public function getJobStatus() { | ||
$trackID = Session::get('ExternalLinksTrackID'); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
if (!$trackID) return; | ||
$noPages = Versioned::get_by_stage('SiteTree', 'Live')->count(); | ||
$result = BrokenExternalPageTrack::get() | ||
->filter('TrackID', $trackID) | ||
->exclude('PageID', 0); | ||
$completedPages = count($result); | ||
|
||
public function createQueuedReport() { | ||
if (!Permission::check('ADMIN')) return; | ||
echo json_encode(array( | ||
'TrackID' => $trackID, | ||
'Completed' => $completedPages, | ||
'Total' => $noPages | ||
)); | ||
|
||
// setup external links job | ||
$externalLinks = new CheckExternalLinksJob(); | ||
$job = singleton('QueuedJobService'); | ||
$jobID = $job->queueJob($externalLinks); | ||
if ($completedPages >= $noPages) { | ||
$this->clear(); | ||
} else { | ||
$this->continueJob(); | ||
} | ||
} | ||
|
||
/* | ||
* Clears the tracking id and any surplus entries for the BrokenExternalPageTrack model | ||
*/ | ||
public function clear() { | ||
// clear any old entries | ||
$trackID = Session::get('ExternalLinksTrackID'); | ||
$oldEntries = BrokenExternalPageTrack::get() | ||
->exclude('TrackID', $trackID); | ||
foreach ($oldEntries as $entry) { | ||
$entry->delete(); | ||
} | ||
Session::clear('ExternalLinksTrackID'); | ||
} | ||
|
||
/* | ||
* Starts a broken external link check | ||
*/ | ||
public function start() { | ||
$track = BrokenExternalPageTrack::create(); | ||
$track->write(); | ||
$track->TrackID = $track->ID; | ||
This comment has been minimized.
Sorry, something went wrong.
tractorcow
|
||
$track->write(); | ||
|
||
Session::set('ExternalLinksTrackID', $track->ID); | ||
|
||
$this->continueJob(); | ||
} | ||
|
||
// redirect to the jobs page | ||
$admin = QueuedJobsAdmin::create(); | ||
$this->Redirect($admin->Link()); | ||
/* | ||
* Continues a broken external link check | ||
*/ | ||
public function continueJob() { | ||
$task = new CheckExternalLinks(); | ||
$task->run(null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,13 @@ function canView($member = false) { | |
return Permission::checkMember($member, $codes); | ||
} | ||
} | ||
|
||
class BrokenExternalPageTrack extends DataObject { | ||
This comment has been minimized.
Sorry, something went wrong.
tractorcow
|
||
private static $db = array( | ||
'TrackID' => 'Int' | ||
); | ||
|
||
private static $has_one = array( | ||
'Page' => 'Page' | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(function($) { | ||
$('#externalLinksReport').entwine({ | ||
onclick: function() { | ||
$(this).start(); | ||
$(this).poll(); | ||
}, | ||
start: function() { | ||
// initiate a new job | ||
$('#ReportHolder').empty(); | ||
$('#ReportHolder').text('Running report 0%'); | ||
$('#ReportHolder').append('<span class="ss-ui-loading-icon"></span>'); | ||
$.ajax({url: "admin/externallinks/start", async: true, timeout: 1000 }); | ||
}, | ||
poll: function() { | ||
This comment has been minimized.
Sorry, something went wrong.
tractorcow
|
||
// poll the current job and update the front end status | ||
$.ajax({ | ||
url: "admin/externallinks/getJobStatus", | ||
async: true, | ||
success: function(data) { | ||
var obj = $.parseJSON(data); | ||
if (!obj) return; | ||
var completed = obj.Completed ? obj.Completed : 0; | ||
var total = obj.Total ? obj.Total : 0; | ||
if (total > 0 && completed == total) { | ||
$('#ReportHolder').text('Report Finished ' + completed + '/' + total); | ||
} else { | ||
setTimeout(function() { $('#externalLinksReport').poll(); }, 1); | ||
} | ||
if (total && completed) { | ||
if (completed < total) { | ||
var percent = (completed / total) * 100; | ||
$('#ReportHolder').text('Running report ' + completed + '/' + | ||
total + ' (' + percent.toFixed(2) + '%)'); | ||
$('#ReportHolder'). | ||
append('<span class="ss-ui-loading-icon"></span>'); | ||
} | ||
} | ||
}, | ||
error: function(e) { | ||
console.log(e); | ||
} | ||
}); | ||
} | ||
}); | ||
}(jQuery)); |
What if another user comes to make a report? Or if the user logs out and then in again? You could end up with multiple concurrent reports. Perhaps we should limit it to one report at any time, and block creation of further reports while one is already running. Maybe all users should be able to view the status of the report progress as well.