From c626fe9aeb550498cb3fc15bb64a9640de39458a Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 6 Sep 2024 14:37:13 +1200 Subject: [PATCH] API Update API to reflect changes to CLI interaction --- .../CMSExternalLinksController.php | 5 +- src/Jobs/CheckExternalLinksJob.php | 3 +- src/Tasks/CheckExternalLinksTask.php | 51 +++++-------------- tests/php/ExternalLinksTest.php | 7 ++- 4 files changed, 20 insertions(+), 46 deletions(-) diff --git a/src/Controllers/CMSExternalLinksController.php b/src/Controllers/CMSExternalLinksController.php index f5e8ba6..9df96b2 100644 --- a/src/Controllers/CMSExternalLinksController.php +++ b/src/Controllers/CMSExternalLinksController.php @@ -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' @@ -47,7 +47,6 @@ public function getJobStatus() } } - /** * Starts a broken external link check */ @@ -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)); } } } diff --git a/src/Jobs/CheckExternalLinksJob.php b/src/Jobs/CheckExternalLinksJob.php index 642ce3e..b8fa82d 100644 --- a/src/Jobs/CheckExternalLinksJob.php +++ b/src/Jobs/CheckExternalLinksJob.php @@ -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; @@ -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'; diff --git a/src/Tasks/CheckExternalLinksTask.php b/src/Tasks/CheckExternalLinksTask.php index 2203396..bd35f0f 100644 --- a/src/Tasks/CheckExternalLinksTask.php +++ b/src/Tasks/CheckExternalLinksTask.php @@ -7,15 +7,17 @@ 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 { @@ -23,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 @@ -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; } /** @@ -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(); @@ -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; @@ -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 diff --git a/tests/php/ExternalLinksTest.php b/tests/php/ExternalLinksTest.php index edbdd2d..4fb7b2c 100644 --- a/tests/php/ExternalLinksTest.php +++ b/tests/php/ExternalLinksTest.php @@ -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; @@ -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(); @@ -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());