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 ## 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. broken links.
## Queued job ## Queued job

View File

@ -8,11 +8,11 @@ use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use Symbiote\QueuedJobs\Services\QueuedJobService; use Symbiote\QueuedJobs\Services\QueuedJobService;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
class CMSExternalLinksController extends Controller class CMSExternalLinksController extends Controller
{ {
private static $allowed_actions = [ private static $allowed_actions = [
'getJobStatus', 'getJobStatus',
'start' 'start'
@ -47,7 +47,6 @@ class CMSExternalLinksController extends Controller
} }
} }
/** /**
* Starts a broken external link check * Starts a broken external link check
*/ */
@ -70,7 +69,7 @@ class CMSExternalLinksController extends Controller
singleton(QueuedJobService::class)->queueJob($checkLinks); singleton(QueuedJobService::class)->queueJob($checkLinks);
} else { } else {
$task = CheckExternalLinksTask::create(); $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\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJob; use Symbiote\QueuedJobs\Services\QueuedJob;
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask; use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
use SilverStripe\PolyExecution\PolyOutput;
if (!class_exists(AbstractQueuedJob::class)) { if (!class_exists(AbstractQueuedJob::class)) {
return; return;
@ -38,7 +39,7 @@ class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob
public function process() public function process()
{ {
$task = CheckExternalLinksTask::create(); $task = CheckExternalLinksTask::create();
$track = $task->runLinksCheck(1); $track = $task->runLinksCheck(PolyOutput::create(PolyOutput::FORMAT_ANSI), 1);
$this->currentStep = $track->CompletedPages; $this->currentStep = $track->CompletedPages;
$this->totalSteps = $track->TotalPages; $this->totalSteps = $track->TotalPages;
$this->isComplete = $track->Status === 'Completed'; $this->isComplete = $track->Status === 'Completed';

View File

@ -7,16 +7,17 @@ use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BuildTask; use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ExternalLinks\Model\BrokenExternalLink; use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack; use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus; use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\ExternalLinks\Tasks\LinkChecker; use SilverStripe\ExternalLinks\Tasks\LinkChecker;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\Core\Validation\ValidationException; use SilverStripe\Core\Validation\ValidationException;
use SilverStripe\View\Parsers\HTMLValue; use SilverStripe\View\Parsers\HTMLValue;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
class CheckExternalLinksTask extends BuildTask class CheckExternalLinksTask extends BuildTask
{ {
@ -24,7 +25,7 @@ class CheckExternalLinksTask extends BuildTask
'LinkChecker' => '%$' . LinkChecker::class '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 * 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 = []; 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 * @var LinkChecker
*/ */
protected $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; protected function execute(InputInterface $input, PolyOutput $output): int
/**
* 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'); $this->runLinksCheck($output);
if (!$this->silent) { return Command::SUCCESS;
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;
} }
/** /**
@ -168,7 +137,7 @@ class CheckExternalLinksTask extends BuildTask
* @param int $limit Limit to number of pages to run, or null to run all * @param int $limit Limit to number of pages to run, or null to run all
* @return BrokenExternalPageTrackStatus * @return BrokenExternalPageTrackStatus
*/ */
public function runLinksCheck($limit = null) public function runLinksCheck(PolyOutput $output, $limit = null)
{ {
// Check the current status // Check the current status
$status = BrokenExternalPageTrackStatus::get_or_create(); $status = BrokenExternalPageTrackStatus::get_or_create();
@ -187,7 +156,7 @@ class CheckExternalLinksTask extends BuildTask
// Check value of html area // Check value of html area
$page = $pageTrack->Page(); $page = $pageTrack->Page();
Deprecation::withSuppressedNotice(fn() => $this->log("Checking {$page->Title}")); $output->writeln("Checking {$page->Title}");
$htmlValue = Injector::inst()->create(HTMLValue::class, $page->Content); $htmlValue = Injector::inst()->create(HTMLValue::class, $page->Content);
if (!$htmlValue->isValid()) { if (!$htmlValue->isValid()) {
continue; continue;
@ -205,15 +174,13 @@ class CheckExternalLinksTask extends BuildTask
try { try {
$page->write(); $page->write();
} catch (ValidationException $ex) { } catch (ValidationException $ex) {
Deprecation::withSuppressedNotice(function () use ($page, $ex) { $output->writeln("Exception caught for {$page->Title}, skipping. Message: " . $ex->getMessage());
$this->log("Exception caught for {$page->Title}, skipping. Message: " . $ex->getMessage());
});
continue; continue;
} }
// Once all links have been created for this page update HasBrokenLinks // Once all links have been created for this page update HasBrokenLinks
$count = $pageTrack->BrokenLinks()->count(); $count = $pageTrack->BrokenLinks()->count();
Deprecation::withSuppressedNotice(fn() => $this->log("Found {$count} broken links")); $output->writeln("Found {$count} broken links");
if ($count) { if ($count) {
$siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class); $siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class);
// Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true

View File

@ -3,7 +3,6 @@
namespace SilverStripe\ExternalLinks\Tests; namespace SilverStripe\ExternalLinks\Tests;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus; use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport; use SilverStripe\ExternalLinks\Reports\BrokenExternalLinksReport;
@ -11,6 +10,7 @@ use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
use SilverStripe\ExternalLinks\Tasks\LinkChecker; use SilverStripe\ExternalLinks\Tasks\LinkChecker;
use SilverStripe\ExternalLinks\Tests\Stubs\ExternalLinksTestPage; use SilverStripe\ExternalLinks\Tests\Stubs\ExternalLinksTestPage;
use SilverStripe\ExternalLinks\Tests\Stubs\PretendLinkChecker; use SilverStripe\ExternalLinks\Tests\Stubs\PretendLinkChecker;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\i18n\i18n; use SilverStripe\i18n\i18n;
use SilverStripe\Reports\Report; use SilverStripe\Reports\Report;
use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProvider;
@ -37,8 +37,7 @@ class ExternalLinksTest extends FunctionalTest
{ {
// Run link checker // Run link checker
$task = CheckExternalLinksTask::create(); $task = CheckExternalLinksTask::create();
Deprecation::withSuppressedNotice(fn() => $task->setSilent(true)); // Be quiet during the test! $task->runLinksCheck(PolyOutput::create(PolyOutput::FORMAT_ANSI, PolyOutput::VERBOSITY_QUIET));
$task->runLinksCheck();
// Get all links checked // Get all links checked
$status = BrokenExternalPageTrackStatus::get_latest(); $status = BrokenExternalPageTrackStatus::get_latest();
@ -114,8 +113,7 @@ class ExternalLinksTest extends FunctionalTest
{ {
// Run link checker // Run link checker
$task = CheckExternalLinksTask::create(); $task = CheckExternalLinksTask::create();
Deprecation::withSuppressedNotice(fn() => $task->setSilent(true)); // Be quiet during the test! $task->runLinksCheck(PolyOutput::create(PolyOutput::FORMAT_ANSI, PolyOutput::VERBOSITY_QUIET));
$task->runLinksCheck();
// Ensure report lists all broken links // Ensure report lists all broken links
$this->assertEquals(4, BrokenExternalLinksReport::create()->sourceRecords()->count()); $this->assertEquals(4, BrokenExternalLinksReport::create()->sourceRecords()->count());