Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 10, 2024
1 parent 815b758 commit c60f889
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Any subsequent report may not be generated until a prior report has completed.

## Dev task

Run the following task *http://path.to.silverstripe/dev/tasks/CheckExternalLinksTask* to check your site for external
Run `sake tasks:CheckExternalLinksTask` to check your site for external
broken links.

## Queued job
Expand Down
5 changes: 2 additions & 3 deletions src/Controllers/CMSExternalLinksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use SilverStripe\Control\Controller;
use Symbiote\QueuedJobs\Services\QueuedJobService;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\Security\Permission;

class CMSExternalLinksController extends Controller
{

private static $allowed_actions = [
'getJobStatus',
'start'
Expand Down Expand Up @@ -47,7 +47,6 @@ public function getJobStatus()
}
}


/**
* Starts a broken external link check
*/
Expand All @@ -70,7 +69,7 @@ public function start()
singleton(QueuedJobService::class)->queueJob($checkLinks);
} else {
$task = CheckExternalLinksTask::create();
$task->runLinksCheck();
$task->runLinksCheck(HybridOutput::create(HybridOutput::FORMAT_HTML));
}
}
}
3 changes: 2 additions & 1 deletion src/Jobs/CheckExternalLinksJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJob;
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
use SilverStripe\HybridExecution\HybridOutput;

if (!class_exists(AbstractQueuedJob::class)) {
return;
Expand Down Expand Up @@ -38,7 +39,7 @@ public function getSignature()
public function process()
{
$task = CheckExternalLinksTask::create();
$track = $task->runLinksCheck(1);
$track = $task->runLinksCheck(HybridOutput::create(HybridOutput::FORMAT_ANSI), 1);
$this->currentStep = $track->CompletedPages;
$this->totalSteps = $track->TotalPages;
$this->isComplete = $track->Status === 'Completed';
Expand Down
51 changes: 13 additions & 38 deletions src/Tasks/CheckExternalLinksTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Debug;
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\ValidationException;
use SilverStripe\View\Parsers\HTMLValue;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

class CheckExternalLinksTask extends BuildTask
{
private static $dependencies = [
'LinkChecker' => '%$' . LinkChecker::class
];

private static $segment = 'CheckExternalLinksTask';
protected static string $commandName = 'CheckExternalLinksTask';

/**
* Define a list of HTTP response codes that should not be treated as "broken", where they usually
Expand All @@ -34,46 +36,19 @@ class CheckExternalLinksTask extends BuildTask
*/
private static $ignore_codes = [];

/**
* @var bool
*/
protected $silent = false;

/**
* @var LinkChecker
*/
protected $linkChecker;

protected $title = 'Checking broken External links in the SiteTree';

protected $description = 'A task that records external broken links in the SiteTree';
protected string $title = 'Checking broken External links in the SiteTree';

protected $enabled = true;

/**
* Log a message
*
* @param string $message
*/
protected function log($message)
{
if (!$this->silent) {
Debug::message($message);
}
}
protected static string $description = 'A task that records external broken links in the SiteTree';

public function run($request)
{
$this->runLinksCheck();
}
/**
* Turn on or off message output
*
* @param bool $silent
*/
public function setSilent($silent)
protected function execute(InputInterface $input, HybridOutput $output): int
{
$this->silent = $silent;
$this->runLinksCheck($output);
return Command::SUCCESS;
}

/**
Expand Down Expand Up @@ -162,7 +137,7 @@ protected function isCodeBroken($httpCode)
* @param int $limit Limit to number of pages to run, or null to run all
* @return BrokenExternalPageTrackStatus
*/
public function runLinksCheck($limit = null)
public function runLinksCheck(HybridOutput $output, $limit = null)
{
// Check the current status
$status = BrokenExternalPageTrackStatus::get_or_create();
Expand All @@ -181,7 +156,7 @@ public function runLinksCheck($limit = null)

// Check value of html area
$page = $pageTrack->Page();
$this->log("Checking {$page->Title}");
$output->writeln("Checking {$page->Title}");
$htmlValue = Injector::inst()->create(HTMLValue::class, $page->Content);
if (!$htmlValue->isValid()) {
continue;
Expand All @@ -199,13 +174,13 @@ public function runLinksCheck($limit = null)
try {
$page->write();
} catch (ValidationException $ex) {
$this->log("Exception caught for {$page->Title}, skipping. Message: " . $ex->getMessage());
$output->writeln("Exception caught for {$page->Title}, skipping. Message: " . $ex->getMessage());
continue;
}

// Once all links have been created for this page update HasBrokenLinks
$count = $pageTrack->BrokenLinks()->count();
$this->log("Found {$count} broken links");
$output->writeln("Found {$count} broken links");
if ($count) {
$siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class);
// Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
Expand Down
7 changes: 3 additions & 4 deletions tests/php/ExternalLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
use SilverStripe\ExternalLinks\Tests\Stubs\ExternalLinksTestPage;
use SilverStripe\ExternalLinks\Tests\Stubs\PretendLinkChecker;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\i18n\i18n;
use SilverStripe\Reports\Report;

Expand All @@ -35,8 +36,7 @@ public function testLinks()
{
// Run link checker
$task = CheckExternalLinksTask::create();
$task->setSilent(true); // Be quiet during the test!
$task->runLinksCheck();
$task->runLinksCheck(HybridOutput::create(HybridOutput::FORMAT_ANSI, HybridOutput::VERBOSITY_QUIET));

// Get all links checked
$status = BrokenExternalPageTrackStatus::get_latest();
Expand Down Expand Up @@ -112,8 +112,7 @@ public function testArchivedPagesAreHiddenFromReport()
{
// Run link checker
$task = CheckExternalLinksTask::create();
$task->setSilent(true); // Be quiet during the test!
$task->runLinksCheck();
$task->runLinksCheck(HybridOutput::create(HybridOutput::FORMAT_ANSI, HybridOutput::VERBOSITY_QUIET));

// Ensure report lists all broken links
$this->assertEquals(4, BrokenExternalLinksReport::create()->sourceRecords()->count());
Expand Down

0 comments on commit c60f889

Please sign in to comment.