Skip to content

Commit

Permalink
--section option for utils/prune-revisions
Browse files Browse the repository at this point in the history
Resolves #8783
  • Loading branch information
brandonkelly committed Jul 26, 2022
1 parent 1c1baf6 commit 5d0192d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
42 changes: 35 additions & 7 deletions src/console/controllers/utils/PruneRevisionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*/
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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([
Expand All @@ -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);

Expand Down

0 comments on commit 5d0192d

Please sign in to comment.