Bläddra i källkod

add clear obsolete websites command

Olivier Massot 3 år sedan
förälder
incheckning
c145b94401

+ 107 - 0
ot_admin/Classes/Command/ClearObsoleteWebsitesSiteCommand.php

@@ -0,0 +1,107 @@
+<?php
+
+namespace Opentalent\OtAdmin\Command;
+
+
+use Opentalent\OtAdmin\Controller\SiteController;
+use Opentalent\OtCore\Exception\NoSuchOrganizationException;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+
+/**
+ * This CLI command loop over the ot_websites and delete the ones whom organization doesn't exist anymore
+ *
+ * @package Opentalent\OtAdmin\Command
+ */
+class ClearObsoleteWebsitesSiteCommand extends Command
+{
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * Allows to configure the command.
+     * Allows to add a description, a help text, and / or define arguments.
+     *
+     */
+    protected function configure()
+    {
+        $this
+            ->setName("ot:clear-obsoletes-websites")
+            ->setDescription("Delete obsolete websites")
+            ->setHelp(" This CLI command loop over the ot_websites and delete 
+                        the ones whom organization doesn't exist anymore")
+            ->addOption(
+                'preview',
+                'p',
+                InputOption::VALUE_NONE,
+                "Only preview, perform no deletion"
+            );
+    }
+
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @return int
+     * @throws NoSuchOrganizationException
+     * @throws \Doctrine\DBAL\ConnectionException
+     * @throws \Doctrine\DBAL\DBALException
+     * @throws \Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException
+     * @throws \Opentalent\OtCore\Exception\NoSuchRecordException
+     * @throws \Opentalent\OtCore\Exception\NoSuchWebsiteException
+     * @throws \TYPO3\CMS\Extbase\Object\Exception
+     * @throws \Throwable
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $preview = $input->getOption('preview');
+
+        $io = new SymfonyStyle($input, $output);
+
+        $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
+
+        $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
+        $queryBuilder = $connectionPool->getQueryBuilderForTable('ot_websites');
+        $sites = $queryBuilder
+            ->select('organization_id')
+            ->from('ot_websites')
+            ->where($queryBuilder->expr()->eq('deleted', 0))
+            ->execute()
+            ->fetchAll();
+
+        $io->progressStart(count($sites));
+
+        $n = 0;
+        foreach ($sites as $site) {
+            try {
+                $siteController->fetchOrganization($site['organization_id']);
+            } catch (NoSuchOrganizationException $e) {
+                $n++;
+                if ($preview) {
+                    $io->info('Site ' . $site['uid'] . ' to delete : ' . $site['organization_id'] . ' [' . $site['organization_name'] . ']');
+                    continue;
+                }
+                $siteController->deleteSiteAction($site['organization_id']);
+                $io->info('Website ' . $site['uid'] . ' deleted : ' . $site['organization_id'] . ' [' . $site['organization_name'] . ']');
+            }
+            /** @noinspection DisconnectedForeachInstructionInspection */
+            $io->progressAdvance();
+        }
+        $io->progressFinish();
+
+        if (!$preview) {
+            $io->success(sprintf($n . " websites have been successfully deleted"));
+        } else {
+            $io->success(sprintf("Preview ended -- " . $n . ' websites would be deleted'));
+        }
+
+        return Command::SUCCESS;
+    }
+}

+ 1 - 1
ot_admin/Classes/Controller/SiteController.php

@@ -1845,7 +1845,7 @@ class SiteController extends ActionController
      * @return Organization
      * @return Organization
      * @throws NoSuchOrganizationException
      * @throws NoSuchOrganizationException
      */
      */
-    private function fetchOrganization($organizationId): object
+    public function fetchOrganization($organizationId): object
     {
     {
         try {
         try {
             return $this->organizationRepository->findById($organizationId);
             return $this->organizationRepository->findById($organizationId);

+ 4 - 1
ot_admin/Configuration/Commands.php

@@ -45,6 +45,9 @@ return [
     ],
     ],
     'ot:regen-config-files' => [
     'ot:regen-config-files' => [
         'class' => Opentalent\OtAdmin\Command\RegenConfigFilesCommand::class
         'class' => Opentalent\OtAdmin\Command\RegenConfigFilesCommand::class
-    ]
+    ],
+    'ot:clear-obsoletes-websites' => [
+        'class' => Opentalent\OtAdmin\Command\ClearObsoleteWebsitesSiteCommand::class
+    ],
 ];
 ];