From f46db43f40f47ac49e9417e64f5ab9565531d709 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Mon, 18 Dec 2017 21:11:14 +0100
Subject: [PATCH] [TASK] Decouple Indexed Search from TSFE-BE User

Indexed Search is using an instance of TSFE-BE
to fetch all subpages of a page, which can
easily be implemented itself.

The method is refactored and adapted to its needs
within AdministrationRepository of Indexed Search.

Resolves: #83382
Releases: master
Change-Id: Iaa71c4dc04d69c0ca0d7bb61f2f36f4a93a4c11d
Reviewed-on: https://review.typo3.org/55154
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
---
 .../Repository/AdministrationRepository.php   | 58 ++++++++++++++-----
 1 file changed, 44 insertions(+), 14 deletions(-)

diff --git a/typo3/sysext/indexed_search/Classes/Domain/Repository/AdministrationRepository.php b/typo3/sysext/indexed_search/Classes/Domain/Repository/AdministrationRepository.php
index 98f2f2266c5b..c76a49e77415 100644
--- a/typo3/sysext/indexed_search/Classes/Domain/Repository/AdministrationRepository.php
+++ b/typo3/sysext/indexed_search/Classes/Domain/Repository/AdministrationRepository.php
@@ -13,7 +13,6 @@ namespace TYPO3\CMS\IndexedSearch\Domain\Repository;
  *
  * The TYPO3 project - inspiring people to share!
  */
-use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
 use TYPO3\CMS\Backend\Tree\View\PageTreeView;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
@@ -22,6 +21,7 @@ use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\QueryHelper;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\DataHandling\DataHandler;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
@@ -420,7 +420,7 @@ class AdministrationRepository
                 $queryBuilder->expr()->in(
                     'pageid',
                     $queryBuilder->createNamedParameter(
-                        GeneralUtility::intExplode(',', $this->extGetTreeList((int)$pageUid, 100, 0, '1=1'), true),
+                        $this->extGetTreeList((int)$pageUid),
                         Connection::PARAM_INT_ARRAY
                     )
                 ),
@@ -641,23 +641,53 @@ class AdministrationRepository
      * The only pages excluded from the list are deleted pages.
      *
      * @param int $id page id
-     * @param int $depth to traverse down the page tree.
-     * @param int $begin is an optional integer that determines at which level in the tree to start collecting uid's. Zero means 'start right away', 1 = 'next level and out'
-     * @param string $perms_clause
-     * @return string Returns the list with a comma in the end + id itself
+     * @return array Returns an array with all page IDs
      */
-    protected function extGetTreeList($id, $depth, $begin = 0, $perms_clause)
+    protected function extGetTreeList(int $id): array
     {
-        $list = GeneralUtility::makeInstance(FrontendBackendUserAuthentication::class)
-            ->extGetTreeList($id, $depth, $begin, $perms_clause);
+        $pageIds = $this->getPageTreeIds($id, 100, 0);
+        $pageIds[] = $id;
+        return $pageIds;
+    }
 
-        if (empty($list)) {
-            $list = $id;
-        } else {
-            $list = rtrim($list, ',') . ',' . $id;
+    /**
+     * Generates a list of Page-uid's from $id. List does not include $id itself
+     * The only pages excluded from the list are deleted pages.
+     *
+     * @param int $id Start page id
+     * @param int $depth Depth to traverse down the page tree.
+     * @param int $begin Determines at which level in the tree to start collecting uid's. Zero means 'start right away', 1 = 'next level and out'
+     * @return array Returns the list of pages
+     */
+    protected function getPageTreeIds(int $id, int $depth, int $begin): array
+    {
+        if (!$id || $depth <= 0) {
+            return [];
         }
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('pages');
 
-        return $list;
+        $queryBuilder->getRestrictions()
+            ->removeAll()
+            ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+        $result = $queryBuilder
+            ->select('uid', 'title')
+            ->from('pages')
+            ->where(
+                $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT))
+            )
+            ->execute();
+
+        $pageIds = [];
+        while ($row = $result->fetch()) {
+            if ($begin <= 0) {
+                $pageIds[] = (int)$row['uid'];
+            }
+            if ($depth > 1) {
+                $pageIds[] = array_merge($pageIds, $this->getPageTreeIds((int)$row['uid'], $depth - 1, $begin - 1));
+            }
+        }
+        return $pageIds;
     }
 
     /**
-- 
GitLab