‪TYPO3CMS  9.5
PseudoSiteFinder.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
4 namespace ‪TYPO3\CMS\Core\Site;
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
33 
39 {
43  protected ‪$cacheIdentifier = 'pseudo-sites';
44 
48  protected ‪$cache;
49 
53  protected ‪$pseudoSites = [];
54 
55  public function ‪__construct()
56  {
57  $this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_core');
58  }
59 
64  protected function ‪populate(bool $allowCaching = true)
65  {
66  $data = $this->cache->get($this->cacheIdentifier);
67  if (empty($data) || $allowCaching === false) {
68  $allLanguages = $this->‪getAllLanguageRecords();
69  $groupedDomains = GeneralUtility::makeInstance(LegacyDomainResolver::class)->getGroupedDomainsPerPage();
70  $availablePages = $this->‪getAllRootPagesWithoutSiteConfiguration();
71  $this->cache->set($this->cacheIdentifier, json_encode([$allLanguages, $groupedDomains, $availablePages]));
72  } else {
73  // Due to the nature of PhpFrontend, the `<?php` and `#` wraps have to be removed
74  $data = preg_replace('/^<\?php\s*|\s*#$/', '', $data);
75  list($allLanguages, $groupedDomains, $availablePages) = json_decode($data, true);
76  }
77 
78  $this->pseudoSites = [];
79  foreach ($availablePages as $row) {
80  $rootPageId = (int)$row['uid'];
81  $site = new ‪PseudoSite($rootPageId, [
82  'domains' => $groupedDomains[$rootPageId] ?? [],
83  'languages' => $allLanguages
84  ]);
85  unset($groupedDomains[$rootPageId]);
86  $this->pseudoSites[$rootPageId] = $site;
87  }
88 
89  // Now add the records where there is a sys_domain record but not configured as root page
90  foreach ($groupedDomains as $rootPageId => $domainRecords) {
91  $site = new ‪PseudoSite((int)$rootPageId, [
92  'domains' => $domainRecords,
93  'languages' => $allLanguages
94  ]);
95  $this->pseudoSites[(int)$rootPageId] = $site;
96  }
97 
98  // Now lets an empty Pseudo-Site for visiting things on pid=0
99  $this->pseudoSites[0] = new ‪NullSite($allLanguages);
100  }
101 
107  public function ‪findAll(): array
108  {
109  if (empty($this->pseudoSites)) {
110  $this->‪populate();
111  }
112  return ‪$this->pseudoSites;
113  }
114 
120  public function ‪refresh()
121  {
122  $this->‪populate(false);
123  }
124 
130  protected function ‪getAllLanguageRecords(): array
131  {
132  $languageRecords = [
133  0 => [
134  'languageId' => 0,
135  'title' => 'Default',
136  'flag' => '',
137  ],
138  ];
139 
140  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_language');
141  $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
142  $statement = $queryBuilder
143  ->select('*')
144  ->from('sys_language')
145  ->orderBy('sorting')
146  ->execute();
147  while ($row = $statement->fetch()) {
148  $uid = (int)$row['uid'];
149  $languageRecords[$uid] = [
150  'languageId' => $uid,
151  'title' => $row['title'],
152  'iso-639-1' => $row['language_isocode'] ?? '',
153  'flag' => 'flags-' . $row['flag'],
154  'enabled' => !$row['hidden'],
155  ];
156  }
157 
158  return $languageRecords;
159  }
160 
171  public function ‪getSiteByPageId(int $pageId, array $rootLine = null): ‪SiteInterface
172  {
173  $this->‪findAll();
174  if (isset($this->pseudoSites[$pageId])) {
175  return $this->pseudoSites[$pageId];
176  }
177  if (!is_array($rootLine)) {
178  try {
179  $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId)->get();
180  } catch (‪PageNotFoundException $e) {
181  $rootLine = [];
182  }
183  }
184  foreach ($rootLine as $pageInRootLine) {
185  if (isset($this->pseudoSites[(int)$pageInRootLine['uid']])) {
186  return $this->pseudoSites[(int)$pageInRootLine['uid']];
187  }
188  }
189  throw new ‪SiteNotFoundException('No pseudo-site found in root line of page ' . $pageId, 1534710048);
190  }
191 
199  public function ‪getSiteByRootPageId(int $rootPageId): ‪SiteInterface
200  {
201  if (empty($this->pseudoSites)) {
202  $this->‪populate();
203  }
204  if (isset($this->pseudoSites[$rootPageId])) {
205  return $this->pseudoSites[$rootPageId];
206  }
207  throw new ‪SiteNotFoundException('No pseudo-site found for root page id ' . $rootPageId, 1521668982);
208  }
209 
215  protected function ‪getExistingSiteConfigurationRootPageIds(): array
216  {
217  $usedPageIds = [];
218  ‪$finder = GeneralUtility::makeInstance(SiteFinder::class);
219  $sites = ‪$finder->getAllSites();
220  foreach ($sites as $site) {
221  $usedPageIds[] = $site->getRootPageId();
222  }
223  return $usedPageIds;
224  }
225 
230  protected function ‪getAllRootPagesWithoutSiteConfiguration(): array
231  {
232  $usedPageIds = $this->‪getExistingSiteConfigurationRootPageIds();
233  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
234  $queryBuilder->getRestrictions()->removeAll()
235  ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
236  ->add(GeneralUtility::makeInstance(FrontendWorkspaceRestriction::class, 0, false));
237  $queryBuilder
238  ->select('uid')
239  ->from('pages')
240  ->where(
241  $queryBuilder->expr()->eq('sys_language_uid', 0),
242  $queryBuilder->expr()->orX(
243  $queryBuilder->expr()->eq('pid', 0),
244  $queryBuilder->expr()->eq('is_siteroot', 1)
245  )
246  )
247  ->orderBy('pid')
248  ->addOrderBy('sorting');
249 
250  if (!empty($usedPageIds)) {
251  $queryBuilder->andWhere($queryBuilder->expr()->notIn('uid', $usedPageIds));
252  }
253  $availablePages = $queryBuilder->execute()->fetchAll();
254  return is_array($availablePages) ? $availablePages : [];
255  }
256 }
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\__construct
‪__construct()
Definition: PseudoSiteFinder.php:52
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\$pseudoSites
‪PseudoSite[] $pseudoSites
Definition: PseudoSiteFinder.php:50
‪TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction
Definition: HiddenRestriction.php:25
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:25
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\$cacheIdentifier
‪string $cacheIdentifier
Definition: PseudoSiteFinder.php:42
‪$finder
‪$finder
Definition: annotationChecker.php:102
‪TYPO3\CMS\Core\Utility\RootlineUtility
Definition: RootlineUtility.php:36
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\getSiteByRootPageId
‪SiteInterface getSiteByRootPageId(int $rootPageId)
Definition: PseudoSiteFinder.php:196
‪TYPO3\CMS\Core\Site\Entity\NullSite
Definition: NullSite.php:31
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\findAll
‪PseudoSite[] findAll()
Definition: PseudoSiteFinder.php:104
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\$cache
‪FrontendInterface $cache
Definition: PseudoSiteFinder.php:46
‪TYPO3\CMS\Core\Database\Query\Restriction\FrontendWorkspaceRestriction
Definition: FrontendWorkspaceRestriction.php:28
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\getSiteByPageId
‪SiteInterface getSiteByPageId(int $pageId, array $rootLine=null)
Definition: PseudoSiteFinder.php:168
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\populate
‪populate(bool $allowCaching=true)
Definition: PseudoSiteFinder.php:61
‪TYPO3\CMS\Frontend\Compatibility\LegacyDomainResolver
Definition: LegacyDomainResolver.php:37
‪TYPO3\CMS\Core\Site
‪TYPO3\CMS\Core\Site\PseudoSiteFinder
Definition: PseudoSiteFinder.php:39
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:34
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\getExistingSiteConfigurationRootPageIds
‪array getExistingSiteConfigurationRootPageIds()
Definition: PseudoSiteFinder.php:212
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:21
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:26
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\refresh
‪refresh()
Definition: PseudoSiteFinder.php:117
‪TYPO3\CMS\Core\Site\Entity\PseudoSite
Definition: PseudoSite.php:28
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\getAllLanguageRecords
‪array getAllLanguageRecords()
Definition: PseudoSiteFinder.php:127
‪TYPO3\CMS\Core\Exception\Page\PageNotFoundException
Definition: PageNotFoundException.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Site\PseudoSiteFinder\getAllRootPagesWithoutSiteConfiguration
‪array getAllRootPagesWithoutSiteConfiguration()
Definition: PseudoSiteFinder.php:227