[TASK] Use SiteFinder for fetching related domain in page tree 93/58193/11
authorGeorg Ringer <georg.ringer@gmail.com>
Wed, 5 Sep 2018 18:16:23 +0000 (20:16 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Thu, 13 Sep 2018 14:26:25 +0000 (16:26 +0200)
The SiteFinder retrieves the correct domain for root pages
also for pages having a site configuration.

Resolves: #86158
Releases: master
Change-Id: If99da2deea08ca7bbe05c13f37472a070ffc6d62
Reviewed-on: https://review.typo3.org/58193
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/backend/Classes/Controller/Page/TreeController.php
typo3/sysext/backend/Classes/Tree/View/BrowseTreeView.php
typo3/sysext/core/Classes/Site/PseudoSiteFinder.php

index 0892c64..da52643 100644 (file)
@@ -21,11 +21,13 @@ use TYPO3\CMS\Backend\Configuration\BackendUserConfiguration;
 use TYPO3\CMS\Backend\Tree\Repository\PageTreeRepository;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
-use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Exception\Page\RootLineException;
+use TYPO3\CMS\Core\Exception\SiteNotFoundException;
 use TYPO3\CMS\Core\Http\JsonResponse;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
+use TYPO3\CMS\Core\Site\PseudoSiteFinder;
+use TYPO3\CMS\Core\Site\SiteFinder;
 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
 use TYPO3\CMS\Core\Type\Bitmask\Permission;
 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
@@ -88,13 +90,6 @@ class TreeController
     protected $expandedState = [];
 
     /**
-     * Associative array containing all pageIds as key, and domain names as values.
-     *
-     * @var array|null
-     */
-    protected $domains;
-
-    /**
      * Instance of the icon factory, to be used for generating the items.
      *
      * @var IconFactory
@@ -271,7 +266,7 @@ class TreeController
         }
         $visibleText = GeneralUtility::fixed_lgd_cs($visibleText, (int)$this->getBackendUser()->uc['titleLen'] ?: 40);
 
-        if ($this->addDomainName) {
+        if ($this->addDomainName && $page['is_siteroot']) {
             $domain = $this->getDomainNameForPage($pageId);
             $suffix = $domain !== '' ? ' [' . $domain . ']' : '';
         }
@@ -386,24 +381,23 @@ class TreeController
      */
     protected function getDomainNameForPage(int $pageId): string
     {
-        if (!is_array($this->domains)) {
-            $this->domains = [];
-            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
-                ->getQueryBuilderForTable('sys_domain');
-            $result = $queryBuilder
-                ->select('domainName', 'pid')
-                ->from('sys_domain')
-                ->orderBy('sorting')
-                ->execute()
-                ->fetchAll();
-            foreach ($result as $domain) {
-                $domainPid = (int)$domain['pid'];
-                if (!isset($this->domains[$domainPid])) {
-                    $this->domains[$domainPid] = $domain['domainName'];
-                }
+        $domain = '';
+        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
+        try {
+            $site = $siteFinder->getSiteByRootPageId($pageId);
+            $domain = (string)$site->getBase();
+        } catch (SiteNotFoundException $e) {
+            // No site found, let's see if it is a legacy-pseudo-site
+            $pseudoSiteFinder = GeneralUtility::makeInstance(PseudoSiteFinder::class);
+            try {
+                $site = $pseudoSiteFinder->getSiteByRootPageId($pageId);
+                $domain = trim((string)$site->getBase(), '/');
+            } catch (SiteNotFoundException $e) {
+                // No pseudo-site found either
             }
         }
-        return $this->domains[$pageId] ?? '';
+
+        return $domain;
     }
 
     /**
index 27b843a..fc52ca8 100644 (file)
@@ -15,7 +15,9 @@ namespace TYPO3\CMS\Backend\Tree\View;
  */
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
-use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Exception\SiteNotFoundException;
+use TYPO3\CMS\Core\Site\PseudoSiteFinder;
+use TYPO3\CMS\Core\Site\SiteFinder;
 use TYPO3\CMS\Core\Type\Bitmask\Permission;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -153,23 +155,21 @@ class BrowseTreeView extends AbstractTreeView
         if (!empty($row['is_siteroot'])
             && $this->getBackendUser()->getTSConfig()['options.']['pageTree.']['showDomainNameWithTitle'] ?? false
         ) {
-            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_domain');
-            $row = $queryBuilder
-                ->select('domainName', 'sorting')
-                ->from('sys_domain')
-                ->where(
-                    $queryBuilder->expr()->eq(
-                        'pid',
-                        $queryBuilder->createNamedParameter($row['uid'], \PDO::PARAM_INT)
-                    )
-                )
-                ->orderBy('sorting')
-                ->setMaxResults(1)
-                ->execute()
-                ->fetch();
+            $pageId = (int)$row['uid'];
+            $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
+            try {
+                $site = $siteFinder->getSiteByRootPageId($pageId);
+                $title .= ' [' . (string)$site->getBase() . ']';
+            } catch (SiteNotFoundException $e) {
+                // No site found, let's see if it is a legacy-pseudo-site
+                $pseudoSiteFinder = GeneralUtility::makeInstance(PseudoSiteFinder::class);
 
-            if ($row !== false) {
-                $title = sprintf('%s [%s]', $title, htmlspecialchars($row['domainName']));
+                try {
+                    $site = $pseudoSiteFinder->getSiteByRootPageId($pageId);
+                    $title .= ' [' . trim((string)$site->getBase(), '/') . ']';
+                } catch (SiteNotFoundException $e) {
+                    // No pseudo-site found either
+                }
             }
         }
         return $title;
index a1f1f27..b581149 100644 (file)
@@ -160,8 +160,8 @@ class PseudoSiteFinder
     public function getSiteByPageId(int $pageId, array $rootLine = null): SiteInterface
     {
         $this->findAll();
-        if ($pageId === 0) {
-            return $this->pseudoSites[0];
+        if (isset($this->pseudoSites[$pageId])) {
+            return $this->pseudoSites[$pageId];
         }
         if (!is_array($rootLine)) {
             try {
@@ -179,6 +179,24 @@ class PseudoSiteFinder
     }
 
     /**
+     * Find a site by given root page id
+     *
+     * @param int $rootPageId the page ID (default language)
+     * @return SiteInterface
+     * @throws SiteNotFoundException
+     */
+    public function getSiteByRootPageId(int $rootPageId): SiteInterface
+    {
+        if (empty($this->pseudoSites)) {
+            $this->populate();
+        }
+        if (isset($this->pseudoSites[$rootPageId])) {
+            return $this->pseudoSites[$rootPageId];
+        }
+        throw new SiteNotFoundException('No pseudo-site found for root page id ' . $rootPageId, 1521668982);
+    }
+
+    /**
      * Loads all sites with a configuration, and takes their rootPageId.
      *
      * @return array