浏览代码

Merge branch 'feature/V8-3630-review-site-deletion' into develop

Olivier Massot 3 年之前
父节点
当前提交
6772e6e719

+ 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;
+    }
+}

+ 0 - 105
ot_admin/Classes/Command/UpdateRoutingIndexCommand.php

@@ -1,105 +0,0 @@
-<?php
-
-namespace Opentalent\OtAdmin\Command;
-
-
-use Opentalent\OtAdmin\Controller\SiteController;
-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 updates the routing index for the given website
- *
- * @package Opentalent\OtAdmin\Command
- */
-class UpdateRoutingIndexCommand 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:site:index")
-            ->setDescription("Update the routes index for the given website(s)")
-            ->setHelp("This CLI command updates the routing index for the given website")
-            ->addOption(
-                'all',
-                null,
-                InputOption::VALUE_NONE,
-                "Update all of the organization websites"
-            )
-            ->addArgument(
-                'organization-id',
-                InputArgument::OPTIONAL,
-                "The organization's id in the opentalent DB"
-            );
-    }
-
-    /**
-     * -- This method is expected by Typo3, do not rename ou remove --
-     *
-     * @param InputInterface $input
-     * @param OutputInterface $output
-     * @return int
-     * @throws \TYPO3\CMS\Extbase\Object\Exception
-     */
-    protected function execute(InputInterface $input, OutputInterface $output)
-    {
-        $org_id = $input->getArgument('organization-id');
-        $all = $input->getOption('all');
-
-        if ($all && $org_id) {
-            throw new \InvalidArgumentException("You can not pass both an organization id and the --all option");
-        }
-        if (!$all && !$org_id) {
-            throw new \InvalidArgumentException("You shall either pass an organization id or use the --all option");
-        }
-
-        $io = new SymfonyStyle($input, $output);
-
-        $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
-
-        if ($all) {
-            $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
-            $queryBuilder = $connectionPool->getQueryBuilderForTable('ot_websites');
-            $sites = $queryBuilder
-                ->select('organization_id')
-                ->from('ot_websites')
-                ->where($queryBuilder->expr()->eq('deleted', 0))
-                ->andWhere($queryBuilder->expr()->gt('organization_id', 0))
-                ->execute()
-                ->fetchAll();
-
-            $io->progressStart(count($sites));
-
-            foreach ($sites as $site) {
-                $org_id = $site['organization_id'];
-                try {
-                    $siteController->updateRoutingIndexAction($org_id);
-                } catch (\Throwable $e) {
-                    $io->error('Organization Id: ' . $org_id . ' - ' . $e->getMessage());
-                }
-                $io->progressAdvance(1);
-            }
-            $io->progressFinish();
-
-            $io->success(sprintf("The routing index has all been fully updated"));
-        } else {
-            $rootUid = $siteController->updateRoutingIndexAction($org_id);
-            $io->success(sprintf("The website with root uid " . $rootUid . " routing index has been updated"));
-        }
-        return 0;
-    }
-}

+ 13 - 8
ot_admin/Classes/Controller/SiteController.php

@@ -171,11 +171,12 @@ class SiteController extends ActionController
      */
     public function getSiteInfosAction(int $organizationId): SiteInfos
     {
-        $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId);
-        $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid']);
+        $website = $this->otWebsiteRepository->getWebsiteByOrganizationId($organizationId, false);
+        $rootUid = $this->otWebsiteRepository->getWebsiteRootUid($website['uid'], false);
 
         $organizationExtraData = $this->fetchOrganizationExtraData($organizationId);
 
+        # /!\ because of restrictions in the typo3 repo, this will return an empty array if the page is deleted
         $rootPage = $this->otPageRepository->getPage($rootUid);
 
         $site = new SiteInfos(
@@ -186,7 +187,7 @@ class SiteController extends ActionController
             $website['template_preferences'],
             $website['matomo_id'],
             self::IS_PRODUCT_PREMIUM[$organizationExtraData['admin']['product']] ?? false,
-            (bool)$rootPage['deleted'],
+            !$rootPage || $rootPage['deleted'],
             ($rootPage['hidden'] || $rootPage['fe_group'] < 0),
             null,
             null,
@@ -783,11 +784,15 @@ class SiteController extends ActionController
         if ($redirectToParent) {
             $originDomain = $this->otWebsiteRepository->resolveWebsiteDomain($website);
 
-            $organization = $this->fetchOrganization($organizationId);
+            try {
+                $organization = $this->fetchOrganization($organizationId);
 
-            $targetOrganizationId = $organization->getParentId();
-            $targetOrganizationWebsite = $this->otWebsiteRepository->getWebsiteByOrganizationId($targetOrganizationId);
-            $targetDomain = $this->otWebsiteRepository->resolveWebsiteDomain($targetOrganizationWebsite);
+                $targetOrganizationId = $organization->getParentId();
+                $targetOrganizationWebsite = $this->otWebsiteRepository->getWebsiteByOrganizationId($targetOrganizationId);
+                $targetDomain = $this->otWebsiteRepository->resolveWebsiteDomain($targetOrganizationWebsite);
+            } catch (NoSuchOrganizationException $e) {
+                $targetDomain = 'opentalent.fr';
+            }
         }
 
         // start transactions
@@ -1840,7 +1845,7 @@ class SiteController extends ActionController
      * @return Organization
      * @throws NoSuchOrganizationException
      */
-    private function fetchOrganization($organizationId): object
+    public function fetchOrganization($organizationId): object
     {
         try {
             return $this->organizationRepository->findById($organizationId);

+ 4 - 4
ot_admin/Configuration/Commands.php

@@ -31,9 +31,6 @@ return [
     'ot:site:reset-perms' => [
         'class' => Opentalent\OtAdmin\Command\ResetBeUserPermsCommand::class
     ],
-    'ot:site:index' => [
-        'class' => Opentalent\OtAdmin\Command\UpdateRoutingIndexCommand::class
-    ],
     'ot:site:status' => [
         'class' => Opentalent\OtAdmin\Command\GetSiteStatusCommand::class
     ],
@@ -48,6 +45,9 @@ return [
     ],
     'ot:regen-config-files' => [
         'class' => Opentalent\OtAdmin\Command\RegenConfigFilesCommand::class
-    ]
+    ],
+    'ot:clear-obsoletes-websites' => [
+        'class' => Opentalent\OtAdmin\Command\ClearObsoleteWebsitesSiteCommand::class
+    ],
 ];
 

+ 2 - 2
ot_core/Classes/Website/OtPageRepository.php

@@ -144,7 +144,7 @@ class OtPageRepository
 
         $rootPage = $this->getRootPageFor($pageUid);
         $rootUid = $rootPage['uid'] ?? 0;
-        if (!$rootUid > 0) {
+        if (!($rootUid > 0)) {
             throw new NoSiteSelected();
         }
         return $rootUid;
@@ -170,6 +170,6 @@ class OtPageRepository
 
     public function getPage(int $uid): array
     {
-        return $this->pageRepository->getPage($uid, true);
+        return $this->pageRepository->getPage_noCheck($uid);
     }
 }