Skip to content

Commit

Permalink
NEW: Setting up Queued job and task to support being sent a single page
Browse files Browse the repository at this point in the history
  • Loading branch information
kmayo-ss committed Jul 24, 2014
1 parent 3b976fd commit 72dc652
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 59 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ The external links module is a task and ModelAdmin to track and to report on bro
## Features

* Add external links to broken links reports
* Add a model admin for external broken links
* Add a task to track external broken links

## Installation
Expand All @@ -25,8 +24,7 @@ The external links module is a task and ModelAdmin to track and to report on bro
3. Make sure the folder after being extracted is named 'externallinks'
4. Place this directory in your sites root directory. This is the one with framework and cms in it.
5. Run in your browser - `/dev/build` to rebuild the database.
6. You should see a new menu called *Broken Ext. Links*
7. Run the following task *http://path.to.silverstripe/dev/tasks/CheckExternalLinks* to check for broken external links
6. Run the following task *http://path.to.silverstripe/dev/tasks/CheckExternalLinks* to check for broken external links

## Dev task ##

Expand All @@ -38,6 +36,3 @@ Add the following code to the mysite config to run the job every 24 hours (86400
`Config::inst()->update('CheckExternalLinks', 'QueuedJob', 86400);`


## TODO ##

Fix setting the class attribute of broken links to ss-broken
89 changes: 38 additions & 51 deletions code/jobs/CheckExternalLinksJob.php
Original file line number Diff line number Diff line change
@@ -1,82 +1,69 @@
<?php

/**
* An check external links job
* A Job for running a external link check for published pages
*
*/
class CheckExternalLinksJob extends AbstractQueuedJob {

public static $regenerate_time = 43200;
class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob {

public function __construct() {
$this->pagesToProcess = SiteTree::get();
$this->pagesToProcess = Versioned::get_by_stage('SiteTree', 'Live')->column();
$this->currentStep = 0;
$this->totalSteps = count($this->pagesToProcess);
}

/**
* Sitemap job is going to run for a while...
*/
public function getJobType() {
return QueuedJob::QUEUED;
}

/**
* @return string
*/
public function getTitle() {
return 'Checking external links';
}

/**
* Return a signature for this queued job
*
* For the generate sitemap job, we only ever want one instance running, so just use the class name
*
* @return String
*/
public function getJobType() {
return QueuedJob::QUEUED;
}

public function getSignature() {
return md5(get_class($this));
}

/**
* Note that this is duplicated for backwards compatibility purposes...
*/
public function setup() {
public function setup() {
parent::setup();
increase_time_limit_to();

$restart = $this->currentStep == 0;

if ($restart) {
$this->pagesToProcess = SiteTree::get();
$this->pagesToProcess = Versioned::get_by_stage('SiteTree', 'Live')->column();
}

}

/**
* On any restart, make sure to check that our temporary file is being created still.
* Check a individual page
*/
public function prepareForRestart() {
parent::prepareForRestart();
}

public function process() {
$remainingPages = $this->pagesToProcess;
if (!count($remainingPages)) {
$this->isComplete = true;
return;
}

// lets process our first item - note that we take it off the list of things left to do
$ID = array_shift($remainingPages);

// get the page
$page = Versioned::get_by_stage('SiteTree', 'Live', 'ID = '.$ID);

if (!$page || !$page->Count()) {
$this->addMessage("Page ID #$ID could not be found, skipping");
}

$task = new CheckExternalLinks();
$task->run();
$data = $this->getJobData();
$completedPages = $task->getCompletedPages();
$totalPages = $task->getTotalPages();
$this->addMessage("$completedPages/$totalPages pages completed");
$this->completeJob();
}
$task->run($page);

/**
* Outputs the completed file to the site's webroot
*/
protected function completeJob() {
$this->isComplete = 1;
$nextgeneration = new CheckExternalLinksJob();
singleton('QueuedJobService')->queueJob($nextgeneration,
date('Y-m-d H:i:s', time() + self::$regenerate_time));
// and now we store the new list of remaining children
$this->pagesToProcess = $remainingPages;
$this->currentStep++;

if (!count($remainingPages)) {
$this->isComplete = true;
return;
}
}
}

}
9 changes: 7 additions & 2 deletions code/tasks/CheckExternalLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public function getTotalPages() {
}

function run($request) {
$pages = Versioned::get_by_stage('SiteTree', 'Live');
if (isset($request->ID)) {
$pages = $request;
} else {
$pages = Versioned::get_by_stage('SiteTree', 'Live');
}
foreach ($pages as $page) {
++$this->totalPages;

Expand Down Expand Up @@ -63,9 +67,10 @@ function run($request) {
$htmlValue->__call('saveHTML', array());

$page->Content = $htmlValue->getContent();
$page->write();
$page->owner->write();

if (!$page->HasBrokenLink) {

// bypass the ORM as syncLinkTracking does not allow you
// to update HasBrokenLink to true
$query = "UPDATE \"SiteTree_Live\" SET \"HasBrokenLink\" = 1 ";
Expand Down

0 comments on commit 72dc652

Please sign in to comment.