Skip to content

Instantly share code, notes, and snippets.

@alexwilson
Created October 8, 2020 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexwilson/56aadce433b8cdc32fd22c5d29283d52 to your computer and use it in GitHub Desktop.
Save alexwilson/56aadce433b8cdc32fd22c5d29283d52 to your computer and use it in GitHub Desktop.
eZ - empty trash
<?php
declare(strict_types=1);
namespace AppBundle\Command;
use AppKernel;
use DateTime;
use Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
/**
* Class RestoreTrashCommand
* @package AppBundle\Command
*/
class EmptyTrashCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('ezplatform:empty-trash')
->setDescription(
'Empty trash'
)
;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|null|void
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function execute(InputInterface $input, OutputInterface $output)
{
ini_set('memory_limit', '1024M');
$dbalConnection = $this->getContainer()->get('database_connection');
$repository = $this->getContainer()->get('ezpublish.api.repository');
$permissionResolver = $repository->getPermissionResolver();
$userService = $repository->getUserService();
$trashService = $repository->getTrashService();
$adminUser = $userService->loadUserByLogin('admin');
$permissionResolver->setCurrentUserReference($adminUser);
$sql = "SELECT node_id FROM ezcontentobject_trash;";
$statement = $dbalConnection->prepare($sql);
$statement->execute();
foreach ($statement->fetchAll() as $trashedItem) {
$locationId = $trashedItem['node_id'];
try {
$trashItem = $trashService->loadTrashItem($locationId);
$trashService->deleteTrashItem($trashItem);
$output->writeln("<info>Deleted ${locationId}</info>");
} catch (Exception $e) {
$output->writeln("<error>{$e->getMessage()}</error>");
}
}
$output->writeln("<info>Finished deleting trash.</info>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment