‪TYPO3CMS  ‪main
DataHandlerCacheFlushingHook.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 
25 
31 {
41  public function ‪rebuildRedirectCacheIfNecessary(array $parameters, ‪DataHandler $dataHandler): void
42  {
43  if (
44  ($parameters['table'] ?? false) !== 'sys_redirect'
45  || !($parameters['uid'] ?? false)
46  || (
47  !isset($dataHandler->datamap['sys_redirect'])
48  && !isset($dataHandler->cmdmap['sys_redirect'][(int)$parameters['uid']])
49  )
50  ) {
51  return;
52  }
53 
54  $redirectCacheService = GeneralUtility::makeInstance(RedirectCacheService::class);
55  $sourceHosts = [];
56  if (isset($dataHandler->‪getHistoryRecords()['sys_redirect:' . (int)$parameters['uid']]['oldRecord']['source_host'])) {
57  $sourceHosts[] = $dataHandler->‪getHistoryRecords()['sys_redirect:' . (int)$parameters['uid']]['oldRecord']['source_host'];
58  }
59  if (isset($dataHandler->‪getHistoryRecords()['sys_redirect:' . (int)$parameters['uid']]['newRecord']['source_host'])) {
60  $sourceHosts[] = $dataHandler->‪getHistoryRecords()['sys_redirect:' . (int)$parameters['uid']]['newRecord']['source_host'];
61  }
62  // only do record lookup for delete cmd, otherwise we cannot get old and new source_host,
63  // thus rebuildAll() should be executed as a safety net anyway.
64  if ($sourceHosts === [] && isset($dataHandler->cmdmap['sys_redirect'][(int)$parameters['uid']])) {
65  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_redirect');
66  $queryBuilder->getRestrictions()->removeAll();
67  $row = $queryBuilder
68  ->select('source_host')
69  ->from('sys_redirect')
70  ->where(
71  $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($parameters['uid'], ‪Connection::PARAM_INT))
72  )
73  ->executeQuery()
74  ->fetchAssociative();
75 
76  if (isset($row['source_host'])) {
77  $sourceHosts[] = $row['source_host'] ?: '*';
78  }
79  }
80 
81  // rebuild only specific source_host redirect caches
82  if ($sourceHosts !== []) {
83  foreach (array_unique($sourceHosts) as $sourceHost) {
84  $redirectCacheService->rebuildForHost($sourceHost);
85  }
86  return;
87  }
88 
89  // Hopefully we get distinct source_host before. However, rebuild all redirect caches as a safety fallback.
90  $redirectCacheService->rebuildAll();
91  }
92 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Redirects\Hooks\DataHandlerCacheFlushingHook\rebuildRedirectCacheIfNecessary
‪rebuildRedirectCacheIfNecessary(array $parameters, DataHandler $dataHandler)
Definition: DataHandlerCacheFlushingHook.php:41
‪TYPO3\CMS\Redirects\Hooks
Definition: DataHandlerCacheFlushingHook.php:18
‪TYPO3\CMS\Redirects\Service\RedirectCacheService
Definition: RedirectCacheService.php:34
‪TYPO3\CMS\Core\DataHandling\DataHandler\getHistoryRecords
‪getHistoryRecords()
Definition: DataHandler.php:9705
‪TYPO3\CMS\Redirects\Hooks\DataHandlerCacheFlushingHook
Definition: DataHandlerCacheFlushingHook.php:31
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52