Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-29451: As an Administrator I want to have a CLI command for database cleanup #2431

Merged
merged 13 commits into from
Dec 19, 2018
Merged
38 changes: 19 additions & 19 deletions eZ/Bundle/EzPublishCoreBundle/Command/CleanupVersionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class CleanupVersionsCommand extends Command
const VERSION_PUBLISHED = 'published';
const VERSION_ALL = 'all';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: replace 'all' with something akin to 'all-except-published'


const VERSION_STATUS = [
self::VERSION_DRAFT => VersionInfo::STATUS_DRAFT,
self::VERSION_ARCHIVED => VersionInfo::STATUS_ARCHIVED,
self::VERSION_PUBLISHED => VersionInfo::STATUS_PUBLISHED
];

/**
* @var \eZ\Publish\API\Repository\Repository
*/
Expand Down Expand Up @@ -133,7 +139,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$contentIdsCount = count($contentIds);

if ($contentIdsCount === 0) {
$output->writeln('<info>There is no Contents matching given criteria.</info>');
$output->writeln('<info>There is no Content matching given criteria.</info>');

return;
}
Expand All @@ -145,6 +151,10 @@ protected function execute(InputInterface $input, OutputInterface $output)

$removedVersionsCounter = 0;

$removeAll = $status === self::VERSION_ALL;
$removeDrafts = $status === self::VERSION_DRAFT;
$removeArchived = $status === self::VERSION_ARCHIVED;

foreach ($contentIds as $contentId) {
try {
$contentInfo = $this->contentService->loadContentInfo((int) $contentId);
Expand All @@ -157,16 +167,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$versionsCount
), Output::VERBOSITY_VERBOSE);

$removeAll = $status === self::VERSION_ALL;
$removeDrafts = $status === self::VERSION_DRAFT;
$removeArchived = $status === self::VERSION_ARCHIVED;

$versions = array_slice(
array_filter($versions, function ($version) use ($removeAll, $removeDrafts, $removeArchived) {
if (
($removeAll && $version->status !== VersionInfo::STATUS_PUBLISHED) ||
($removeDrafts && $version->status === VersionInfo::STATUS_DRAFT) ||
($removeArchived && $version->status === VersionInfo::STATUS_ARCHIVED)
($removeAll && $version->status !== self::VERSION_STATUS[self::VERSION_PUBLISHED]) ||
($removeDrafts && $version->status === self::VERSION_STATUS[self::VERSION_DRAFT]) ||
($removeArchived && $version->status === self::VERSION_STATUS[self::VERSION_ARCHIVED])
) {
return $version;
}
Expand Down Expand Up @@ -222,10 +228,10 @@ protected function getObjectsIds($keep, $status)

if ($status !== self::VERSION_ALL) {
$query->where('v.status = :status');
$query->setParameter('status', $this->getVersionInfoStatus($status));
$query->setParameter('status', $this->mapStatusToVersionInfoStatus($status));
} else {
$query->andWhere('v.status != :status');
$query->setParameter('status', $this->getVersionInfoStatus(self::VERSION_PUBLISHED));
$query->setParameter('status', $this->mapStatusToVersionInfoStatus(self::VERSION_PUBLISHED));
}

$stmt = $query->execute();
Expand All @@ -240,16 +246,10 @@ protected function getObjectsIds($keep, $status)
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
*/
private function getVersionInfoStatus($status)
private function mapStatusToVersionInfoStatus($status)
{
if ($status === self::VERSION_ARCHIVED) {
return VersionInfo::STATUS_ARCHIVED;
}
if ($status === self::VERSION_DRAFT) {
return VersionInfo::STATUS_DRAFT;
}
if ($status === self::VERSION_PUBLISHED) {
return VersionInfo::STATUS_PUBLISHED;
if (array_key_exists($status, self::VERSION_STATUS)) {
return self::VERSION_STATUS[$status];
}

throw new InvalidArgumentException(
Expand Down