diff --git a/bin/maintenance b/bin/maintenance index fcf33890a3..1cc2760600 100755 --- a/bin/maintenance +++ b/bin/maintenance @@ -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(); diff --git a/lib/Alchemy/Phrasea/Command/Maintenance/CleanBases.php b/lib/Alchemy/Phrasea/Command/Maintenance/CleanBases.php new file mode 100644 index 0000000000..d922f475e7 --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Maintenance/CleanBases.php @@ -0,0 +1,49 @@ +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!"); + } + } +} diff --git a/lib/Alchemy/Phrasea/Command/Maintenance/CleanRecords.php b/lib/Alchemy/Phrasea/Command/Maintenance/CleanRecords.php index ae745bbf53..9761e32f6d 100644 --- a/lib/Alchemy/Phrasea/Command/Maintenance/CleanRecords.php +++ b/lib/Alchemy/Phrasea/Command/Maintenance/CleanRecords.php @@ -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(" Remove orphans records from basket"); $this->removeOrphansFromBasket(); }