‪TYPO3CMS  ‪main
RedirectCacheService.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 
26 
34 {
38  protected ‪$cache;
39 
40  public function ‪__construct(‪CacheManager $cacheManager = null)
41  {
42  $cacheManager = $cacheManager ?? GeneralUtility::makeInstance(CacheManager::class);
43  $this->cache = $cacheManager->getCache('pages');
44  }
45 
49  public function ‪getRedirects(string $sourceHost): array
50  {
51  $redirects = $this->cache->get($this->‪buildCacheIdentifier($sourceHost));
52  // empty array is considered as valid cache, so we need to check for array type here.
53  if (!is_array($redirects)) {
54  $redirects = $this->‪rebuildForHost($sourceHost);
55  }
56  return $redirects;
57  }
58 
63  public function ‪rebuildForHost(string $sourceHost): array
64  {
65  $redirects = [];
66  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_redirect');
67  $queryBuilder->getRestrictions()->removeAll()
68  ->add(GeneralUtility::makeInstance(HiddenRestriction::class))
69  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
70  $queryBuilder
71  ->select('*')
72  ->from('sys_redirect');
73 
74  if ($sourceHost === '' || $sourceHost === '*') {
75  $queryBuilder->where(
76  $queryBuilder->expr()->or(
77  $queryBuilder->expr()->eq('source_host', $queryBuilder->createNamedParameter('')),
78  $queryBuilder->expr()->eq('source_host', $queryBuilder->createNamedParameter('*')),
79  )
80  );
81  } else {
82  $queryBuilder->where(
83  $queryBuilder->expr()->in('source_host', $queryBuilder->createNamedParameter($sourceHost))
84  );
85  }
86 
87  // Ensure we have redirects which respect query parameters first, paired with a
88  // cross dbms deterministic sorting criteria (`uid`) as last criteria.
89  $queryBuilder
90  ->orderBy('respect_query_parameters', 'desc')
91  ->addOrderBy('uid', 'asc');
92 
93  $statement = $queryBuilder->executeQuery();
94  while ($row = $statement->fetchAssociative()) {
95  // Field "description" is not needed for FE redirect handling. Don't add it to cache.
96  unset($row['description']);
97  if ($row['is_regexp'] && $row['respect_query_parameters']) {
98  $redirects['regexp_query_parameters'][$row['source_path']][$row['uid']] = $row;
99  } elseif ($row['is_regexp'] && !$row['respect_query_parameters']) {
100  $redirects['regexp_flat'][$row['source_path']][$row['uid']] = $row;
101  } elseif ($row['respect_query_parameters']) {
102  $redirects['respect_query_parameters'][$row['source_path']][$row['uid']] = $row;
103  } else {
104  $redirects['flat'][rtrim($row['source_path'], '/') . '/'][$row['uid']] = $row;
105  }
106  }
107  $this->cache->set($this->‪buildCacheIdentifier($sourceHost), $redirects);
108  return $redirects;
109  }
110 
114  public function ‪rebuildAll(): void
115  {
116  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_redirect');
117  // remove all restriction, as we need to retrieve the source host even for hidden or deleted redirects.
118  $queryBuilder->getRestrictions()->removeAll();
119  $resultSet = $queryBuilder
120  ->select('source_host')
121  ->distinct()
122  ->from('sys_redirect')
123  ->executeQuery();
124  while ($row = $resultSet->fetchAssociative()) {
125  $this->‪rebuildForHost($row['source_host'] ?? '*');
126  }
127  }
128 
129  private function ‪buildCacheIdentifier(string $sourceHost): string
130  {
131  return 'redirects_' . sha1($sourceHost);
132  }
133 }
‪TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction
Definition: HiddenRestriction.php:27
‪TYPO3\CMS\Redirects\Service\RedirectCacheService\rebuildForHost
‪rebuildForHost(string $sourceHost)
Definition: RedirectCacheService.php:62
‪TYPO3\CMS\Redirects\Service\RedirectCacheService\getRedirects
‪getRedirects(string $sourceHost)
Definition: RedirectCacheService.php:48
‪TYPO3\CMS\Redirects\Service\RedirectCacheService\__construct
‪__construct(CacheManager $cacheManager=null)
Definition: RedirectCacheService.php:39
‪TYPO3\CMS\Redirects\Service\RedirectCacheService\rebuildAll
‪rebuildAll()
Definition: RedirectCacheService.php:113
‪TYPO3\CMS\Redirects\Service\RedirectCacheService
Definition: RedirectCacheService.php:34
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Redirects\Service\RedirectCacheService\$cache
‪FrontendInterface $cache
Definition: RedirectCacheService.php:37
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Redirects\Service
Definition: IntegrityService.php:18
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Redirects\Service\RedirectCacheService\buildCacheIdentifier
‪buildCacheIdentifier(string $sourceHost)
Definition: RedirectCacheService.php:128