From 5d0192d7d58812a68fffa03894f2e1fa7961666f Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 26 Jul 2022 09:05:01 -0700 Subject: [PATCH] --section option for utils/prune-revisions Resolves #8783 --- CHANGELOG-WIP.md | 1 + .../utils/PruneRevisionsController.php | 42 +++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 8f38e6330e4..118332cd34f 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -26,6 +26,7 @@ - The “Keep me signed in” checkbox label on the control panel’s login page now includes the remembered session duration, e.g. “Keep me signed in for 2 weeks”. ([#11594](https://github.com/craftcms/cms/discussions/11594)) - Dashboard widgets no longer show a confirmation dialog when deleted. Their delete notifications include an “Undo” button instead. ([#11573](https://github.com/craftcms/cms/discussions/11573)) - Improved the behavior of some console commands for non-interactive shells. ([#11650](https://github.com/craftcms/cms/issues/11650)) +- The `utils/prune-revisions` console command now has a `--section` option. ([#8783](https://github.com/craftcms/cms/discussions/8783)) - `config/general.php` and `config/db.php` can now return `craft\config\GeneralConfig`/`DbConfig` objects, which can be defined using new fluent setter methods. ([#11591](https://github.com/craftcms/cms/pull/11591), [#11656](https://github.com/craftcms/cms/pull/11656)) - The `|duration` Twig filter can now be used with an integer representing a number of seconds, and its `showSeconds` argument is no longer required. Seconds will be output if the duration is less than one minute by default. - The `|length` Twig filter now checks if the variable is a query, and if so, returns its count. ([#11625](https://github.com/craftcms/cms/discussions/11625)) diff --git a/src/console/controllers/utils/PruneRevisionsController.php b/src/console/controllers/utils/PruneRevisionsController.php index c18db869d05..ca8d678379e 100644 --- a/src/console/controllers/utils/PruneRevisionsController.php +++ b/src/console/controllers/utils/PruneRevisionsController.php @@ -13,6 +13,7 @@ use craft\db\Query; use craft\db\Table; use craft\helpers\Console; +use craft\helpers\StringHelper; use yii\console\ExitCode; use yii\db\Expression; @@ -24,6 +25,12 @@ */ class PruneRevisionsController extends Controller { + /** + * @var string|null The section handle(s) to prune revisions from. Can be set to multiple comma-separated sections. + * @since 4.2.0 + */ + public ?string $section = null; + /** * @var int|null The maximum number of revisions an element can have. */ @@ -41,6 +48,7 @@ class PruneRevisionsController extends Controller public function options($actionID): array { $options = parent::options($actionID); + $options[] = 'section'; $options[] = 'maxRevisions'; $options[] = 'dryRun'; return $options; @@ -53,6 +61,20 @@ public function options($actionID): array */ public function actionIndex(): int { + $sectionIds = []; + if ($this->section) { + $sectionsService = Craft::$app->getSections(); + $sectionHandles = StringHelper::split($this->section); + foreach ($sectionHandles as $sectionHandle) { + $section = $sectionsService->getSectionByHandle($sectionHandle); + if (!$section) { + $this->stderr("$sectionHandle isn’t a valid section handle.\n", Console::FG_RED); + return ExitCode::UNSPECIFIED_ERROR; + } + $sectionIds[] = $section->id; + } + } + if (!isset($this->maxRevisions)) { $this->maxRevisions = (int)$this->prompt('What is the max number of revisions an element can have?', [ 'default' => Craft::$app->getConfig()->getGeneral()->maxRevisions, @@ -63,6 +85,18 @@ public function actionIndex(): int } // Get the elements with too many revisions + $subQuery = (new Query()) + ->select(['canonicalId', 'count' => 'COUNT(*)']) + ->from(['r' => Table::REVISIONS]) + ->groupBy(['canonicalId']) + ->having(['>', 'COUNT(*)', $this->maxRevisions]); + + if (!empty($sectionIds)) { + $subQuery + ->innerJoin(['entries' => Table::ENTRIES], '[[entries.id]] = [[r.canonicalId]]') + ->andWhere(['entries.sectionId' => $sectionIds]); + } + $this->stdout('Finding elements with too many revisions ... '); $elements = (new Query()) ->select([ @@ -73,13 +107,7 @@ public function actionIndex(): int ->from([Table::ELEMENTS]) ->where(new Expression('[[id]] = [[s.canonicalId]]')), ]) - ->from([ - 's' => (new Query()) - ->select(['canonicalId', 'count' => 'COUNT(*)']) - ->from(['r' => Table::REVISIONS]) - ->groupBy(['canonicalId']) - ->having(['>', 'COUNT(*)', $this->maxRevisions]), - ]) + ->from(['s' => $subQuery]) ->all(); $this->stdout('done' . PHP_EOL . PHP_EOL, Console::FG_GREEN);