ClearObsoleteWebsitesSiteCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Opentalent\OtAdmin\Command;
  3. use Opentalent\OtAdmin\Controller\SiteController;
  4. use Opentalent\OtCore\Exception\NoSuchOrganizationException;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. use TYPO3\CMS\Core\Database\ConnectionPool;
  12. use TYPO3\CMS\Core\Utility\GeneralUtility;
  13. use TYPO3\CMS\Extbase\Object\ObjectManager;
  14. /**
  15. * This CLI command loop over the ot_websites and delete the ones whom organization doesn't exist anymore
  16. *
  17. * @package Opentalent\OtAdmin\Command
  18. */
  19. class ClearObsoleteWebsitesSiteCommand extends Command
  20. {
  21. /**
  22. * -- This method is expected by Typo3, do not rename ou remove --
  23. *
  24. * Allows to configure the command.
  25. * Allows to add a description, a help text, and / or define arguments.
  26. *
  27. */
  28. protected function configure()
  29. {
  30. $this
  31. ->setName("ot:clear-obsoletes-websites")
  32. ->setDescription("Delete obsolete websites")
  33. ->setHelp(" This CLI command loop over the ot_websites and delete
  34. the ones whom organization doesn't exist anymore")
  35. ->addOption(
  36. 'preview',
  37. 'p',
  38. InputOption::VALUE_NONE,
  39. "Only preview, perform no deletion"
  40. );
  41. }
  42. /**
  43. * -- This method is expected by Typo3, do not rename ou remove --
  44. *
  45. * @param InputInterface $input
  46. * @param OutputInterface $output
  47. * @return int
  48. * @throws NoSuchOrganizationException
  49. * @throws \Doctrine\DBAL\ConnectionException
  50. * @throws \Doctrine\DBAL\DBALException
  51. * @throws \Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException
  52. * @throws \Opentalent\OtCore\Exception\NoSuchRecordException
  53. * @throws \Opentalent\OtCore\Exception\NoSuchWebsiteException
  54. * @throws \TYPO3\CMS\Extbase\Object\Exception
  55. * @throws \Throwable
  56. */
  57. protected function execute(InputInterface $input, OutputInterface $output)
  58. {
  59. $preview = $input->getOption('preview');
  60. $io = new SymfonyStyle($input, $output);
  61. $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  62. $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
  63. $queryBuilder = $connectionPool->getQueryBuilderForTable('ot_websites');
  64. $sites = $queryBuilder
  65. ->select('organization_id')
  66. ->from('ot_websites')
  67. ->where($queryBuilder->expr()->eq('deleted', 0))
  68. ->execute()
  69. ->fetchAll();
  70. $io->progressStart(count($sites));
  71. $n = 0;
  72. foreach ($sites as $site) {
  73. try {
  74. $siteController->fetchOrganization($site['organization_id']);
  75. } catch (NoSuchOrganizationException $e) {
  76. $n++;
  77. if ($preview) {
  78. $io->info('Site ' . $site['uid'] . ' to delete : ' . $site['organization_id'] . ' [' . $site['organization_name'] . ']');
  79. continue;
  80. }
  81. $siteController->deleteSiteAction($site['organization_id']);
  82. $io->info('Website ' . $site['uid'] . ' deleted : ' . $site['organization_id'] . ' [' . $site['organization_name'] . ']');
  83. }
  84. /** @noinspection DisconnectedForeachInstructionInspection */
  85. $io->progressAdvance();
  86. }
  87. $io->progressFinish();
  88. if (!$preview) {
  89. $io->success(sprintf($n . " websites have been successfully deleted"));
  90. } else {
  91. $io->success(sprintf("Preview ended -- " . $n . ' websites would be deleted'));
  92. }
  93. return Command::SUCCESS;
  94. }
  95. }