Skip to content

Instantly share code, notes, and snippets.

@wizhippo
Last active May 18, 2017 13:38
Show Gist options
  • Save wizhippo/cd362454743c1f096c0e85525f6a81fa to your computer and use it in GitHub Desktop.
Save wizhippo/cd362454743c1f096c0e85525f6a81fa to your computer and use it in GitHub Desktop.
<?php
namespace ExampleBundle\Command;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\Core\Repository\Values\User\UserReference;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class PurgeVersionsCommand extends ContainerAwareCommand
{
protected function configure()
{
parent::configure();
$this->setName('example:purge-versions');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var $repository \eZ\Publish\API\Repository\Repository */
$repository = $this->getContainer()->get('ezpublish.api.repository');
$repository->getPermissionResolver()->setCurrentUserReference(new UserReference(14));
// We use legacy here to make sure we traverse all content as items might not be indexed yet by other
// search engines
// TODO: replace when correct api available
$searchHandler = $this->getContainer()->get('ezpublish.spi.search.legacy');
$query = new Query();
$query->limit = 0;
$searchResult = $searchHandler->findContent($query);
$nbResults = $searchResult->totalCount;
$pageSize = 100;
$query->limit = $pageSize;
$progress = new ProgressBar($output, $nbResults);
while ($searchHits = $searchHandler->findContent($query)->searchHits) {
foreach ($searchHits as $searchHit) {
$contentInfo = $repository->getContentService()->loadContentInfo($searchHit->valueObject->id);
$versions = $repository->getContentService()->loadVersions($contentInfo);
foreach ($versions as $versionInfo) {
if ($versionInfo->versionNo !== $contentInfo->currentVersionNo) {
$repository->getContentService()->deleteVersion($versionInfo);
}
}
$progress->advance();
}
$query->offset += $pageSize;
if ($query->offset > $nbResults) {
break;
}
}
$progress->finish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment