[TASK] Cleanup unused PageUriBuilder options 95/58095/3
authorBenni Mack <benni@typo3.org>
Fri, 31 Aug 2018 07:29:35 +0000 (09:29 +0200)
committerJigal van Hemert <jigal.van.hemert@typo3.org>
Sun, 2 Sep 2018 11:47:51 +0000 (13:47 +0200)
Due to the refactoring of the typolink method, the PageUriBuilder
is now only used for links to pages with a site configuration, so it
can be cleaned up.

This a first step in getting the PageUriBuilder API in shape.

As this class is new, and the options and the class is not public
API yet, this removal does not need any docs.

Resolves: #86052
Releases: master
Change-Id: I56205f4e3fec0e68bd317a45ae1d860f8d84a799
Reviewed-on: https://review.typo3.org/58095
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/core/Classes/Error/PageErrorHandler/PageContentErrorHandler.php
typo3/sysext/core/Classes/Routing/PageUriBuilder.php

index 1625810..ea337b2 100644 (file)
@@ -2663,7 +2663,7 @@ class BackendUtility
                 $additionalQueryParams = [];
                 parse_str($additionalGetVars, $additionalQueryParams);
                 $uriBuilder = GeneralUtility::makeInstance(PageUriBuilder::class);
-                $previewUrl = (string)$uriBuilder->buildUri($pageUid, $additionalQueryParams, $anchorSection, ['rootLine' => $rootLine], $uriBuilder::ABSOLUTE_URL);
+                $previewUrl = (string)$uriBuilder->buildUri($pageUid, $additionalQueryParams, $anchorSection, ['site' => $site, 'rootLine' => $rootLine], $uriBuilder::ABSOLUTE_URL);
             } catch (SiteNotFoundException $e) {
                 $previewUrl = self::createPreviewUrl($pageUid, $rootLine, $anchorSection, $additionalGetVars, $viewScript);
             }
index ae236b7..1cfc13f 100644 (file)
@@ -21,7 +21,6 @@ use Psr\Http\Message\ServerRequestInterface;
 use TYPO3\CMS\Core\Http\HtmlResponse;
 use TYPO3\CMS\Core\LinkHandling\LinkService;
 use TYPO3\CMS\Core\Routing\PageUriBuilder;
-use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
@@ -88,17 +87,12 @@ class PageContentErrorHandler implements PageErrorHandlerInterface
         }
 
         // Build Url
-        $languageUid = null;
-        $siteLanguage = $request->getAttribute('language');
-        if ($siteLanguage instanceof SiteLanguage) {
-            $languageUid = $siteLanguage->getLanguageId();
-        }
         $uriBuilder = GeneralUtility::makeInstance(PageUriBuilder::class);
         return (string)$uriBuilder->buildUri(
             (int)$urlParams['pageuid'],
             [],
             null,
-            ['language' => $languageUid],
+            ['language' => $request->getAttribute('language', null)],
             PageUriBuilder::ABSOLUTE_URL
         );
     }
index 4d39382..9a379a9 100644 (file)
@@ -73,64 +73,64 @@ class PageUriBuilder implements SingletonInterface
     public function buildUri(int $pageId, array $queryParameters = [], string $fragment = null, array $options = [], string $referenceType = self::ABSOLUTE_PATH): UriInterface
     {
         // Resolve site
-        $site = null;
+        $site = $options['site'] ?? null;
         $siteLanguage = null;
         $languageOption = $options['language'] ?? null;
         $languageQueryParameter = isset($queryParameters['L']) ? (int)$queryParameters['L'] : null;
 
-        if (isset($options['site']) && $options['site'] instanceof Site) {
-            $site = $options['site'];
+        if (!($site instanceof Site)) {
+            try {
+                $site = $this->siteFinder->getSiteByPageId($pageId, $options['rootLine'] ?? null);
+            } catch (SiteNotFoundException $e) {
+                // no site found, must be an pseudo site site
+            }
         }
-        if (isset($options['language'])) {
-            if ($options['language'] instanceof SiteLanguage) {
-                $siteLanguage = $options['language'];
-                $languageOption = $siteLanguage->getLanguageId();
+        if ($languageOption) {
+            if ($languageOption instanceof SiteLanguage) {
+                $siteLanguage = $languageOption;
+                $languageOption = $languageOption->getLanguageId();
             } else {
                 $languageOption = (int)$languageOption;
             }
         }
         $languageId = $languageOption ?? $languageQueryParameter ?? null;
 
-        // alternative page ID - Used to set as alias as well
-        $alternativePageId = $options['alternativePageId'] ?? $pageId;
-        if (!($site instanceof Site)) {
-            try {
-                $site = $this->siteFinder->getSiteByPageId($pageId, $options['rootLine'] ?? null);
-                if ($site) {
-                    // Resolve language (based on the options / query parameters, and remove it from GET variables,
-                    // as the language is determined by the language path
+        // Resolve language (based on the options / query parameters, and remove it
+        // from GET variables, as the language is determined by the language path
+        if ($site) {
+            if ($languageId !== null) {
+                try {
+                    $siteLanguage = $site->getLanguageById($languageId);
                     unset($queryParameters['L']);
-                    $siteLanguage = $site->getLanguageById($languageId ?? 0);
+                } catch (\InvalidArgumentException $e) {
+                    // No Language found, so do fallback linking
                 }
-            } catch (SiteNotFoundException | \InvalidArgumentException $e) {
+            } else {
+                $siteLanguage = $site->getDefaultLanguage();
+                unset($queryParameters['L']);
             }
         }
 
         // If something is found, use /en/?id=123&additionalParams
         // Only if a language is configured for the site, build a URL with a site prefix / base
         if ($site && $siteLanguage) {
-            unset($options['legacyUrlPrefix']);
             // Ensure to fetch the path segment / slug if it exists
+            $pageRecord = BackendUtility::getRecord('pages', $pageId);
             if ($siteLanguage->getLanguageId() > 0) {
                 $pageLocalizations = BackendUtility::getRecordLocalization('pages', $pageId, $siteLanguage->getLanguageId());
-                $pageRecord = $pageLocalizations[0] ?? false;
-            } else {
-                $pageRecord = BackendUtility::getRecord('pages', $pageId);
+                $pageRecord = $pageLocalizations[0] ?? $pageRecord;
             }
             $prefix = (string)$siteLanguage->getBase();
             if (!empty($pageRecord['slug'] ?? '')) {
                 $prefix = rtrim($prefix, '/') . '/' . ltrim($pageRecord['slug'], '/');
             } else {
-                $prefix .= '?id=' . $alternativePageId;
+                $prefix .= '?id=' . $pageId;
             }
         } else {
             // If nothing is found, use index.php?id=123&additionalParams
             // This usually kicks in with "PseudoSites" where no language object can be determined.
-            $prefix = $options['legacyUrlPrefix'] ?? null;
-            if ($prefix === null) {
-                $prefix = $referenceType === self::ABSOLUTE_URL ? GeneralUtility::getIndpEnv('TYPO3_SITE_URL') : '';
-            }
-            $prefix .= 'index.php?id=' . $alternativePageId;
+            $prefix = $referenceType === self::ABSOLUTE_URL ? GeneralUtility::getIndpEnv('TYPO3_SITE_URL') : '';
+            $prefix .= 'index.php?id=' . $pageId;
             if ($languageId !== null) {
                 $queryParameters['L'] = $languageId;
             }
@@ -150,7 +150,7 @@ class PageUriBuilder implements SingletonInterface
         if ($fragment) {
             $uri = $uri->withFragment($fragment);
         }
-        if ($referenceType === self::ABSOLUTE_PATH && !isset($options['legacyUrlPrefix'])) {
+        if ($referenceType === self::ABSOLUTE_PATH) {
             $uri = $uri->withScheme('')->withHost('')->withPort(null);
         }
         return $uri;