API Update API to reflect changes to CLI interaction (#145)

This commit is contained in:
Guy Sartorelli 2024-09-26 17:18:08 +12:00 committed by GitHub
parent 13ee761f05
commit 357750148c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 56 deletions

View File

@ -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

View File

@ -8,11 +8,11 @@ use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
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'
@ -47,7 +47,6 @@ class CMSExternalLinksController extends Controller
}
}
/**
* Starts a broken external link check
*/
@ -70,7 +69,7 @@ class CMSExternalLinksController extends Controller
singleton(QueuedJobService::class)->queueJob($checkLinks);
} else {
$task = CheckExternalLinksTask::create();
$task->runLinksCheck();
$task->runLinksCheck(PolyOutput::create(PolyOutput::FORMAT_HTML));
}
}
}

View File

@ -5,6 +5,7 @@ namespace SilverStripe\ExternalLinks\Jobs;
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;
@ -38,7 +39,7 @@ class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob
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';

View File

@ -7,16 +7,17 @@ use SilverStripe\CMS\Model\SiteTree;
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
{
@ -24,7 +25,7 @@ class CheckExternalLinksTask extends BuildTask
'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
@ -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 string $title = 'Checking broken External links in the SiteTree';
protected $description = 'A task that records external broken links in the SiteTree';
protected static string $description = 'A task that records external broken 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)
protected function execute(InputInterface $input, PolyOutput $output): int
{
Deprecation::notice('3.4.0', 'Will be replaced with new $output parameter in the run() method');
if (!$this->silent) {
Debug::message($message);
}
}
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)
{
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;
}
/**
@ -168,7 +137,7 @@ class CheckExternalLinksTask extends BuildTask
* @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();
@ -187,7 +156,7 @@ class CheckExternalLinksTask extends BuildTask
// 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;
@ -205,15 +174,13 @@ class CheckExternalLinksTask extends BuildTask
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

View File

@ -3,7 +3,6 @@
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;
@ -11,6 +10,7 @@ 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;
@ -37,8 +37,7 @@ class ExternalLinksTest extends FunctionalTest
{
// 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();
@ -114,8 +113,7 @@ class ExternalLinksTest extends FunctionalTest
{
// 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());