[TASK] Use RootlineUtility in Site Finder 64/57764/9
authorBenni Mack <benni@typo3.org>
Wed, 1 Aug 2018 20:08:59 +0000 (22:08 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Fri, 3 Aug 2018 07:44:39 +0000 (09:44 +0200)
The SiteFinder uses a custom rootline traversal, but
since this is cleared up with the Context API and
the separation of RootlineUtility from PageRepository,
this can now be used directly - thus caching is
utilized by default.

Resolves: #85714
Releases: master
Change-Id: Ie60bc059e24058b1bd893ad4102b39363eba85d7
Reviewed-on: https://review.typo3.org/57764
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benjamin Kott <benjamin.kott@outlook.com>
Tested-by: Benjamin Kott <benjamin.kott@outlook.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/core/Classes/Site/SiteFinder.php

index 1b82c16..fb24580 100644 (file)
@@ -17,13 +17,14 @@ namespace TYPO3\CMS\Core\Site;
  */
 
 use TYPO3\CMS\Core\Configuration\SiteConfiguration;
+use TYPO3\CMS\Core\Context\Context;
 use TYPO3\CMS\Core\Core\Environment;
-use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
+use TYPO3\CMS\Core\Exception\Page\PageNotFoundException;
 use TYPO3\CMS\Core\Exception\SiteNotFoundException;
 use TYPO3\CMS\Core\Site\Entity\Site;
 use TYPO3\CMS\Core\Site\Entity\SiteInterface;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Core\Utility\RootlineUtility;
 
 /**
  * Is used in backend and frontend for all places where to read / identify sites and site languages.
@@ -70,7 +71,7 @@ class SiteFinder
      *
      * @return array
      */
-    protected function getBaseUris(): array
+    public function getBaseUris(): array
     {
         $baseUrls = [];
         foreach ($this->sites as $site) {
@@ -140,37 +141,29 @@ class SiteFinder
      * Traverses the rootline of a page up until a Site was found.
      *
      * @param int $pageId
-     * @param array $alternativeRootLine
+     * @param array $rootLine
      * @return SiteInterface
      * @throws SiteNotFoundException
      */
-    public function getSiteByPageId(int $pageId, array $alternativeRootLine = null): SiteInterface
+    public function getSiteByPageId(int $pageId, array $rootLine = null): SiteInterface
     {
-        if (is_array($alternativeRootLine)) {
-            foreach ($alternativeRootLine as $pageInRootLine) {
-                if ($pageInRootLine['uid'] > 0) {
-                    try {
-                        return $this->getSiteByRootPageId((int)$pageInRootLine['uid']);
-                    } catch (SiteNotFoundException $e) {
-                        // continue looping
-                    }
-                }
+        if (!is_array($rootLine)) {
+            try {
+                $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId)->get();
+            } catch (PageNotFoundException $e) {
+                // Usually when a page was hidden or disconnected
+                // This could be improved by handing in a Context object and decide whether hidden pages
+                // Should be linkeable too
+                $rootLine = [];
             }
         }
-        // Do your own root line traversing
-        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
-        $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
-        $queryBuilder->select('pid')->from('pages');
-        $rootLinePageId = $pageId;
-        while ($rootLinePageId > 0) {
-            try {
-                return $this->getSiteByRootPageId($rootLinePageId);
-            } catch (SiteNotFoundException $e) {
-                // get parent page ID
-                $queryBuilder->where(
-                    $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($rootLinePageId))
-                );
-                $rootLinePageId = (int)$queryBuilder->execute()->fetchColumn(0);
+        foreach ($rootLine as $pageInRootLine) {
+            if ($pageInRootLine['uid'] > 0) {
+                try {
+                    return $this->getSiteByRootPageId((int)$pageInRootLine['uid']);
+                } catch (SiteNotFoundException $e) {
+                    // continue looping
+                }
             }
         }
         throw new SiteNotFoundException('No site found in root line of page  ' . $pageId, 1521716622);