From 208504f9a2d0a38c707038da7f0a2a290aa2cbec 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 --- README.md | 2 +- .../CMSExternalLinksController.php | 5 +- src/Jobs/CheckExternalLinksJob.php | 3 +- src/Tasks/CheckExternalLinksTask.php | 59 ++++--------------- tests/php/ExternalLinksTest.php | 8 +-- 5 files changed, 21 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 7c5c4f3..77b06fb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Controllers/CMSExternalLinksController.php b/src/Controllers/CMSExternalLinksController.php index f5e8ba6..9977614 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\PolyExecution\PolyOutput; 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(PolyOutput::create(PolyOutput::FORMAT_HTML)); } } } diff --git a/src/Jobs/CheckExternalLinksJob.php b/src/Jobs/CheckExternalLinksJob.php index 642ce3e..a57117d 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\PolyExecution\PolyOutput; 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(PolyOutput::create(PolyOutput::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 fc049fe..20d8c69 100644 --- a/src/Tasks/CheckExternalLinksTask.php +++ b/src/Tasks/CheckExternalLinksTask.php @@ -7,16 +7,17 @@ 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 $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; } /** @@ -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(); @@ -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; @@ -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 diff --git a/tests/php/ExternalLinksTest.php b/tests/php/ExternalLinksTest.php index 4644a7b..63bedf6 100644 --- a/tests/php/ExternalLinksTest.php +++ b/tests/php/ExternalLinksTest.php @@ -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\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 @@ 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(); @@ -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());