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 23, 2024
1 parent 13ee761 commit 208504f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 56 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\PolyExecution\PolyOutput;
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(PolyOutput::create(PolyOutput::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\PolyExecution\PolyOutput;

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(PolyOutput::create(PolyOutput::FORMAT_ANSI), 1);
$this->currentStep = $track->CompletedPages;
$this->totalSteps = $track->TotalPages;
$this->isComplete = $track->Status === 'Completed';
Expand Down
59 changes: 13 additions & 46 deletions src/Tasks/CheckExternalLinksTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\Core\Validation\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 @@ -35,51 +36,19 @@ class CheckExternalLinksTask extends BuildTask
*/
private static $ignore_codes = [];

/**
* @var bool
* @deprecated 3.4.0 Will be replaced with new $output parameter in the run() method
*/
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
* @deprecated 3.4.0 Will be replaced with new $output parameter in the run() method
*/
protected function log($message)
{
Deprecation::notice('3.4.0', 'Will be replaced with new $output parameter in the run() method');
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
* @deprecated 3.4.0 Will be replaced with new $output parameter in the run() method
*/
public function setSilent($silent)
protected function execute(InputInterface $input, PolyOutput $output): int
{
Deprecation::notice('3.4.0', 'Will be replaced with new $output parameter in the run() method');
$this->silent = $silent;
$this->runLinksCheck($output);
return Command::SUCCESS;
}

/**
Expand Down Expand Up @@ -168,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(PolyOutput $output, $limit = null)
{
// Check the current status
$status = BrokenExternalPageTrackStatus::get_or_create();
Expand All @@ -187,7 +156,7 @@ public function runLinksCheck($limit = null)

// Check value of html area
$page = $pageTrack->Page();
Deprecation::withSuppressedNotice(fn() => $this->log("Checking {$page->Title}"));
$output->writeln("Checking {$page->Title}");
$htmlValue = Injector::inst()->create(HTMLValue::class, $page->Content);
if (!$htmlValue->isValid()) {
continue;
Expand All @@ -205,15 +174,13 @@ public function runLinksCheck($limit = null)
try {
$page->write();
} catch (ValidationException $ex) {
Deprecation::withSuppressedNotice(function () use ($page, $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();
Deprecation::withSuppressedNotice(fn() => $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
8 changes: 3 additions & 5 deletions tests/php/ExternalLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace SilverStripe\ExternalLinks\Tests;

use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport;
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
use SilverStripe\ExternalLinks\Tasks\LinkChecker;
use SilverStripe\ExternalLinks\Tests\Stubs\ExternalLinksTestPage;
use SilverStripe\ExternalLinks\Tests\Stubs\PretendLinkChecker;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\i18n\i18n;
use SilverStripe\Reports\Report;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -37,8 +37,7 @@ public function testLinks()
{
// Run link checker
$task = CheckExternalLinksTask::create();
Deprecation::withSuppressedNotice(fn() => $task->setSilent(true)); // Be quiet during the test!
$task->runLinksCheck();
$task->runLinksCheck(PolyOutput::create(PolyOutput::FORMAT_ANSI, PolyOutput::VERBOSITY_QUIET));

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

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

0 comments on commit 208504f

Please sign in to comment.