‪TYPO3CMS  10.4
CanonicalGenerator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
27 
34 {
39 
43  protected ‪$pageRepository;
44 
48  protected ‪$eventDispatcher;
49 
50  public function ‪__construct(‪TypoScriptFrontendController ‪$typoScriptFrontendController = null, EventDispatcherInterface ‪$eventDispatcher = null)
51  {
52  $this->eventDispatcher = ‪$eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
53  $this->typoScriptFrontendController = ‪$typoScriptFrontendController ?? $this->‪getTypoScriptFrontendController();
54  $this->pageRepository = GeneralUtility::makeInstance(PageRepository::class);
55  }
56 
57  public function ‪generate(): string
58  {
59  $event = $this->eventDispatcher->dispatch(new ‪ModifyUrlForCanonicalTagEvent(''));
60  $href = $event->getUrl();
61 
62  if (empty($href) && (int)$this->typoScriptFrontendController->page['no_index'] === 1) {
63  return '';
64  }
65 
66  if (empty($href)) {
67  // 1) Check if page show content from other page
68  $href = $this->‪checkContentFromPid();
69  }
70  if (empty($href)) {
71  // 2) Check if page has canonical URL set
72  $href = $this->‪checkForCanonicalLink();
73  }
74  if (empty($href)) {
75  // 3) Fallback, create canonical URL
76  $href = $this->‪checkDefaultCanonical();
77  }
78 
79  if (!empty($href)) {
80  $canonical = '<link ' . GeneralUtility::implodeAttributes([
81  'rel' => 'canonical',
82  'href' => $href
83  ], true) . '/>' . LF;
84  $this->typoScriptFrontendController->additionalHeaderData[] = $canonical;
85  return $canonical;
86  }
87  return '';
88  }
89 
93  protected function ‪checkForCanonicalLink(): string
94  {
95  if (!empty($this->typoScriptFrontendController->page['canonical_link'])) {
96  return $this->typoScriptFrontendController->cObj->typoLink_URL([
97  'parameter' => $this->typoScriptFrontendController->page['canonical_link'],
98  'forceAbsoluteUrl' => true,
99  ]);
100  }
101  return '';
102  }
103 
107  protected function ‪checkContentFromPid(): string
108  {
109  if (!empty($this->typoScriptFrontendController->page['content_from_pid'])) {
110  $parameter = (int)$this->typoScriptFrontendController->page['content_from_pid'];
111  ‪if ($parameter > 0) {
112  $targetPage = $this->pageRepository->getPage($parameter, true);
113  if (!empty($targetPage['canonical_link'])) {
114  $parameter = $targetPage['canonical_link'];
115  }
116  return $this->typoScriptFrontendController->cObj->typoLink_URL([
117  'parameter' => $parameter,
118  'forceAbsoluteUrl' => true,
119  ]);
120  }
121  }
122  return '';
123  }
124 
128  protected function ‪checkDefaultCanonical(): string
129  {
130  // We should only create a canonical link to the target, if the target is within a valid site root
131  $inSiteRoot = $this->‪isPageWithinSiteRoot((int)$this->typoScriptFrontendController->id);
132  if (!$inSiteRoot) {
133  return '';
134  }
135 
136  // Temporarily remove current mountpoint information as we want to have the
137  // URL of the target page and not of the page within the mountpoint if the
138  // current page is a mountpoint.
139  $previousMp = $this->typoScriptFrontendController->MP;
140  $this->typoScriptFrontendController->MP = '';
141 
142  $link = $this->typoScriptFrontendController->cObj->typoLink_URL([
143  'parameter' => $this->typoScriptFrontendController->id . ',' . $this->typoScriptFrontendController->type,
144  'forceAbsoluteUrl' => true,
145  'addQueryString' => true,
146  'addQueryString.' => [
147  'method' => 'GET',
148  'exclude' => implode(
149  ',',
151  (int)$this->typoScriptFrontendController->id,
152  (array)‪$GLOBALS['TYPO3_CONF_VARS']['FE']['additionalCanonicalizedUrlParameters']
153  )
154  )
155  ]
156  ]);
157  $this->typoScriptFrontendController->MP = $previousMp;
158  return $link;
159  }
160 
161  protected function ‪isPageWithinSiteRoot(int $id): bool
162  {
163  $rootline = GeneralUtility::makeInstance(RootlineUtility::class, $id)->get();
164  foreach ($rootline as $page) {
165  if ($page['is_siteroot']) {
166  return true;
167  }
168  }
169  return false;
170  }
171 
175  protected function ‪getTypoScriptFrontendController(): TypoScriptFrontendController
176  {
177  return ‪$GLOBALS['TSFE'];
178  }
179 }
‪if
‪if(PHP_SAPI !=='cli')
Definition: splitAcceptanceTests.php:33
‪TYPO3\CMS\Core\Utility\RootlineUtility
Definition: RootlineUtility.php:39
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\$pageRepository
‪PageRepository $pageRepository
Definition: CanonicalGenerator.php:41
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: CanonicalGenerator.php:45
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\__construct
‪__construct(TypoScriptFrontendController $typoScriptFrontendController=null, EventDispatcherInterface $eventDispatcher=null)
Definition: CanonicalGenerator.php:47
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\generate
‪generate()
Definition: CanonicalGenerator.php:54
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\isPageWithinSiteRoot
‪isPageWithinSiteRoot(int $id)
Definition: CanonicalGenerator.php:158
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\checkContentFromPid
‪string checkContentFromPid()
Definition: CanonicalGenerator.php:104
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator
Definition: CanonicalGenerator.php:34
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\checkDefaultCanonical
‪string checkDefaultCanonical()
Definition: CanonicalGenerator.php:125
‪TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent
Definition: ModifyUrlForCanonicalTagEvent.php:24
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Utility\CanonicalizationUtility
Definition: CanonicalizationUtility.php:26
‪TYPO3\CMS\Seo\Canonical
Definition: CanonicalGenerator.php:18
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\checkForCanonicalLink
‪string checkForCanonicalLink()
Definition: CanonicalGenerator.php:90
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\getTypoScriptFrontendController
‪TypoScriptFrontendController getTypoScriptFrontendController()
Definition: CanonicalGenerator.php:172
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:52
‪TYPO3\CMS\Frontend\Utility\CanonicalizationUtility\getParamsToExcludeForCanonicalizedUrl
‪static array getParamsToExcludeForCanonicalizedUrl(int $pageId, array $additionalCanonicalizedUrlParameters=[])
Definition: CanonicalizationUtility.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Seo\Canonical\CanonicalGenerator\$typoScriptFrontendController
‪TypoScriptFrontendController $typoScriptFrontendController
Definition: CanonicalGenerator.php:37