‪TYPO3CMS  ‪main
SysRedirectRootPageMoveMigration.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 
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
25 
31 {
32  private const ‪TABLE_NAME = 'sys_redirect';
33 
34  private array ‪$rootPageIds;
35 
36  public function ‪__construct(
37  private ‪ConnectionPool $connectionPool,
38  private ‪SiteFinder $siteFinder
39  ) {
40  $this->rootPageIds = $this->‪getRootPageIds();
41  }
42 
43  public function ‪getTitle(): string
44  {
45  return 'Move "sys_redirect" records to site config root pages.';
46  }
47 
48  public function ‪hasPotentialUpdateForTable(string $tableName): bool
49  {
50  if ($tableName !== self::TABLE_NAME || !$this->‪sysRedirectsTableExists()) {
51  return false;
52  }
53  $rootPageIds = $this->‪getRootPageIds();
54  $queryBuilder = $this->‪getPreparedQueryBuilder();
55  $andWhereConditions = $queryBuilder->expr()->and($queryBuilder->expr()->neq('pid', $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)));
56  if (‪$rootPageIds !== []) {
57  $andWhereConditions = $andWhereConditions->with($queryBuilder->expr()->notIn('pid', $queryBuilder->createNamedParameter(‪$rootPageIds, ‪Connection::PARAM_INT_ARRAY)));
58  }
59  $numberOfNonRootPageRedirectRecords = (int)$queryBuilder
60  ->count('uid')
61  ->from(self::TABLE_NAME)
62  ->where($andWhereConditions)
63  ->executeQuery()
64  ->fetchOne();
65  return $numberOfNonRootPageRedirectRecords > 0;
66  }
67 
68  public function ‪updateTableRow(string $tableName, array $row): array
69  {
70  if ($tableName !== self::TABLE_NAME) {
71  return $row;
72  }
73  $pageId = (int)$row['pid'];
74  if ($pageId === 0 || in_array($pageId, $this->rootPageIds, true)) {
75  return $row;
76  }
77  try {
78  $rootPageId = $this->siteFinder->getSiteByPageId($pageId)->getRootPageId();
79  } catch (‪SiteNotFoundException) {
80  // Move redirects without proper site config root page to top tree page.
81  $rootPageId = 0;
82  }
83  if ($rootPageId !== $pageId) {
84  $row['pid'] = $rootPageId;
85  }
86  return $row;
87  }
88 
89  private function ‪getRootPageIds(): array
90  {
91  ‪$rootPageIds = [];
92  $sites = $this->siteFinder->getAllSites();
93  foreach ($sites as $site) {
94  if (!in_array($site->getRootPageId(), ‪$rootPageIds, true)) {
95  ‪$rootPageIds[] = $site->getRootPageId();
96  }
97  }
98  return ‪$rootPageIds;
99  }
100 
101  private function ‪sysRedirectsTableExists(): bool
102  {
103  $schemaManager = $this->connectionPool->getConnectionForTable(self::TABLE_NAME)->createSchemaManager();
104  return $schemaManager->tablesExist([self::TABLE_NAME]);
105  }
106 
107  private function ‪getPreparedQueryBuilder(): QueryBuilder
108  {
109  $queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLE_NAME);
110  $queryBuilder->getRestrictions()->removeAll();
111  return $queryBuilder;
112  }
113 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\getTitle
‪getTitle()
Definition: SysRedirectRootPageMoveMigration.php:43
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\sysRedirectsTableExists
‪sysRedirectsTableExists()
Definition: SysRedirectRootPageMoveMigration.php:101
‪TYPO3\CMS\Install\Updates\RowUpdater\RowUpdaterInterface
Definition: RowUpdaterInterface.php:24
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\getRootPageIds
‪getRootPageIds()
Definition: SysRedirectRootPageMoveMigration.php:89
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\$rootPageIds
‪array $rootPageIds
Definition: SysRedirectRootPageMoveMigration.php:34
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\hasPotentialUpdateForTable
‪hasPotentialUpdateForTable(string $tableName)
Definition: SysRedirectRootPageMoveMigration.php:48
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration
Definition: SysRedirectRootPageMoveMigration.php:31
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\TABLE_NAME
‪const TABLE_NAME
Definition: SysRedirectRootPageMoveMigration.php:32
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\updateTableRow
‪updateTableRow(string $tableName, array $row)
Definition: SysRedirectRootPageMoveMigration.php:68
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\getPreparedQueryBuilder
‪getPreparedQueryBuilder()
Definition: SysRedirectRootPageMoveMigration.php:107
‪TYPO3\CMS\Install\Updates\RowUpdater
Definition: RowUpdaterInterface.php:18
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Install\Updates\RowUpdater\SysRedirectRootPageMoveMigration\__construct
‪__construct(private ConnectionPool $connectionPool, private SiteFinder $siteFinder)
Definition: SysRedirectRootPageMoveMigration.php:36
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT_ARRAY
‪const PARAM_INT_ARRAY
Definition: Connection.php:72