clean:bases command

This commit is contained in:
aynsix
2021-12-23 15:07:00 +03:00
parent 0977796a3c
commit 2a870239dd
3 changed files with 53 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ use Alchemy\Phrasea\Command\Maintenance\CleanNotifications;
use Alchemy\Phrasea\Command\Maintenance\CleanUsers;
use Alchemy\Phrasea\Core\Version;
use Alchemy\Phrasea\Command\Maintenance\CleanRecords;
use Alchemy\Phrasea\Command\Maintenance\CleanBases;
require_once __DIR__ . '/../lib/autoload.php';
@@ -32,4 +33,6 @@ $cli->command(new CleanRecords());
$cli->command(new CleanUsers());
$cli->command(new CleanBases());
$cli->run();

View File

@@ -0,0 +1,49 @@
<?php
namespace Alchemy\Phrasea\Command\Maintenance;
use Alchemy\Phrasea\Collection\Reference\CollectionReference;
use Alchemy\Phrasea\Collection\Reference\DbalCollectionReferenceRepository;
use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Databox\DataboxRepository;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class CleanBases extends Command
{
public function __construct()
{
parent::__construct('clean:bases');
$this
->setDescription('remove orphan collections ')
->addOption('orphan', null, InputOption::VALUE_NONE, 'remove collections bas that refers a non existing sbas')
;
}
protected function doExecute(InputInterface $input, OutputInterface $output)
{
/** @var DbalCollectionReferenceRepository $dbalCollectionReferenceRepository */
$dbalCollectionReferenceRepository = $this->container['repo.collection-references'];
/** @var DataboxRepository $databoxRepository */
$databoxRepository = $this->container['repo.databoxes'];
if ($input->getOption('orphan') === true) {
/** @var CollectionReference $collectionReference */
foreach ($dbalCollectionReferenceRepository->findAll() as $collectionReference) {
if ($databoxRepository->find($collectionReference->getDataboxId()) == null) {
// if the databox of the collectionReference not found, remove the collectionReference (bas)
try {
$dbalCollectionReferenceRepository->delete($collectionReference);
} catch (\Exception $e) {
$output->writeln("Can't delete bas with base_id = " . $collectionReference->getBaseId());
}
}
}
} else {
$output->writeln("No given options!");
}
}
}

View File

@@ -24,9 +24,7 @@ class CleanRecords extends Command
protected function doExecute(InputInterface $input, OutputInterface $output)
{
$fromBaskets = $input->getOption('from_baskets');
if ($fromBaskets === true) {
if ($input->getOption('from_baskets') === true) {
$output->writeln("<info> Remove orphans records from basket</info>");
$this->removeOrphansFromBasket();
}