From acd940042a7018be695f677819be60b7305e020e Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 6 Sep 2024 14:57:35 +1200 Subject: [PATCH] API Update API to reflect changes to CLI interaction --- README.md | 5 +- docs/en/index.md | 2 +- src/Jobs/ContentReviewNotificationJob.php | 7 ++- src/Tasks/ContentReviewEmails.php | 28 ++++++----- src/Tasks/ContentReviewOwnerMigrationTask.php | 47 ------------------- tests/php/ContentReviewNotificationTest.php | 15 +++++- 6 files changed, 38 insertions(+), 66 deletions(-) delete mode 100644 src/Tasks/ContentReviewOwnerMigrationTask.php diff --git a/README.md b/README.md index 1371452..ef6ce42 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,7 @@ composer require silverstripe/contentreview composer require silverstripe/contentreview ``` -You'll also need to run `dev/build`. - -Run dev/build either via the web server by opening the URL `http:///dev/build?flush` or -by running the dev/build via a CLI: `sake dev/build flush=1` +You'll also need to build the database either via the web server by opening the URL `http:///dev/build?flush` or via a CLI: `sake db:build --flush` ## Documentation diff --git a/docs/en/index.md b/docs/en/index.md index 383c077..42a9fd3 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -11,7 +11,7 @@ The module is set up in the `Settings` section of the CMS, see the [User guide]( In order for the contentreview module to send emails, you need to *either*: * Setup the `ContentReviewEmails` script to run daily via a system cron job. - * Install the [queuedjobs](https://github.com/symbiote/silverstripe-queuedjobs) module and follow the configuration steps to create a cron job for that module. Once installed, you can just run `dev/build` to have a job created, which will run at 9am every day by default. + * Install the [queuedjobs](https://github.com/symbiote/silverstripe-queuedjobs) module and follow the configuration steps to create a cron job for that module. Once installed, you can just run `sake db:build` to have a job created, which will run at 9am every day by default. ## Using diff --git a/src/Jobs/ContentReviewNotificationJob.php b/src/Jobs/ContentReviewNotificationJob.php index a558952..7b432ee 100644 --- a/src/Jobs/ContentReviewNotificationJob.php +++ b/src/Jobs/ContentReviewNotificationJob.php @@ -5,9 +5,11 @@ use SilverStripe\ContentReview\Tasks\ContentReviewEmails; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Config\Config; +use SilverStripe\PolyExecution\PolyOutput; use Symbiote\QueuedJobs\Services\AbstractQueuedJob; use Symbiote\QueuedJobs\Services\QueuedJob; use Symbiote\QueuedJobs\Services\QueuedJobService; +use Symfony\Component\Console\Input\ArrayInput; if (!class_exists(AbstractQueuedJob::class)) { return; @@ -94,7 +96,10 @@ public function process() $this->queueNextRun(); $task = ContentReviewEmails::create(); - $task->run(new HTTPRequest("GET", "/dev/tasks/ContentReviewEmails")); + $output = PolyOutput::create(PolyOutput::FORMAT_ANSI); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); $this->currentStep = 1; $this->isComplete = true; diff --git a/src/Tasks/ContentReviewEmails.php b/src/Tasks/ContentReviewEmails.php index f84846c..4d9138f 100644 --- a/src/Tasks/ContentReviewEmails.php +++ b/src/Tasks/ContentReviewEmails.php @@ -3,11 +3,10 @@ namespace SilverStripe\ContentReview\Tasks; use Page; -use RuntimeException; use SilverStripe\ContentReview\Compatibility\ContentReviewCompatability; use SilverStripe\Control\Email\Email; -use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\BuildTask; +use SilverStripe\PolyExecution\PolyOutput; use SilverStripe\Model\List\ArrayList; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBField; @@ -16,6 +15,8 @@ use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\Model\ArrayData; use SilverStripe\View\SSViewer; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; /** * Daily task to send emails to the owners of content items when the review date rolls around. @@ -24,19 +25,21 @@ class ContentReviewEmails extends BuildTask { private array $invalid_emails = []; - /** - * @param HTTPRequest $request - * @throws RuntimeException - */ - public function run($request) + protected static string $commandName = 'content-review-emails'; + + protected static string $description = 'Daily task to send emails to the owners of content items when the review' + . ' date rolls around'; + + protected function execute(InputInterface $input, PolyOutput $output): int { if (!$this->isValidEmail($senderEmail = SiteConfig::current_site_config()->ReviewFrom)) { - throw new RuntimeException( + $output->writeln( sprintf( - 'Provided sender email address is invalid: "%s".', + 'Provided sender email address is invalid: "%s".', $senderEmail ) ); + return Command::FAILURE; } $compatibility = ContentReviewCompatability::start(); @@ -57,13 +60,16 @@ public function run($request) if (is_array($this->invalid_emails) && count($this->invalid_emails) > 0) { $plural = count($this->invalid_emails) > 1 ? 's are' : ' is'; - throw new RuntimeException( + $output->writeln( sprintf( - 'Provided email' . $plural . ' invalid: "%s".', + 'Provided email' . $plural . ' invalid: "%s".', implode(', ', $this->invalid_emails) ) ); + return Command::FAILURE; } + + return Command::SUCCESS; } /** diff --git a/src/Tasks/ContentReviewOwnerMigrationTask.php b/src/Tasks/ContentReviewOwnerMigrationTask.php deleted file mode 100644 index ea22472..0000000 --- a/src/Tasks/ContentReviewOwnerMigrationTask.php +++ /dev/null @@ -1,47 +0,0 @@ -OwnerID column to a new column name. - * @deprecated 5.4.0 Will be removed without equivalent functionality to replace it - */ -class ContentReviewOwnerMigrationTask extends BuildTask -{ - public function __construct() - { - parent::__construct(); - Deprecation::withSuppressedNotice(function () { - Deprecation::notice( - '5.4.0', - 'Will be removed without equivalent functionality to replace it', - Deprecation::SCOPE_CLASS - ); - }); - } - - /** - * @param HTTPRequest $request - */ - public function run($request) - { - $results = DB::query("SHOW columns from \"SiteTree\" WHERE \"field\" = 'OwnerID'"); - - if ($results->numRecords() == 0) { - echo "

No need to run task. SiteTree->OwnerID doesn't exist

"; - } else { - DB::query("UPDATE \"SiteTree\" SET \"ContentReviewOwnerID\" = \"OwnerID\""); - DB::query("UPDATE \"SiteTree_Live\" SET \"ContentReviewOwnerID\" = \"OwnerID\""); - DB::query("UPDATE \"SiteTree_versions\" SET \"ContentReviewOwnerID\" = \"OwnerID\""); - DB::query("ALTER TABLE \"SiteTree\" DROP COLUMN \"OwnerID\""); - DB::query("ALTER TABLE \"SiteTree_Live\" DROP COLUMN \"OwnerID\""); - DB::query("ALTER TABLE \"SiteTree_Versions\" DROP COLUMN \"OwnerID\""); - echo "

Migrated 3 tables. Dropped obsolete OwnerID column

"; - } - } -} diff --git a/tests/php/ContentReviewNotificationTest.php b/tests/php/ContentReviewNotificationTest.php index 241dd9c..7eb5af0 100644 --- a/tests/php/ContentReviewNotificationTest.php +++ b/tests/php/ContentReviewNotificationTest.php @@ -18,6 +18,9 @@ use SilverStripe\Security\Member; use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\ContentReview\Models\ContentReviewLog; +use SilverStripe\PolyExecution\PolyOutput; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\BufferedOutput; class ContentReviewNotificationTest extends SapphireTest { @@ -58,7 +61,11 @@ public function testContentReviewEmails() $childParentPage->write(); $task = new ContentReviewEmails(); - $task->run(new HTTPRequest('GET', '/dev/tasks/ContentReviewEmails')); + $buffer = new BufferedOutput(); + $output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); // Set template variables (as per variable case) $ToEmail = 'author@example.com'; @@ -124,7 +131,11 @@ public function testContentReviewNeeded() $this->assertCount(1, $childParentPage->ReviewLogs()); $task = new ContentReviewEmails(); - $task->run(new HTTPRequest('GET', '/dev/tasks/ContentReviewEmails')); + $buffer = new BufferedOutput(); + $output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer); + $input = new ArrayInput([]); + $input->setInteractive(false); + $task->run($input, $output); // Expecting to not send the email as content review for page is done $email = $this->findEmail($member->Email);