‪TYPO3CMS  ‪main
ShortcutRecordsMigration.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 
23 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
26 
30 #[UpgradeWizard('shortcutRecordsMigration')]
32 {
33  private const ‪TABLE_NAME = 'sys_be_shortcuts';
34 
36  protected ?‪Router ‪$router = null;
37 
38  public function ‪getTitle(): string
39  {
40  return 'Migrate shortcut records to new format.';
41  }
42 
43  public function ‪getDescription(): string
44  {
45  return 'To support speaking urls in the backend, some fields need to be changed in sys_be_shortcuts.';
46  }
47 
48  public function ‪getPrerequisites(): array
49  {
50  return [
51  DatabaseUpdatedPrerequisite::class,
52  ];
53  }
54 
55  public function ‪updateNecessary(): bool
56  {
57  return $this->‪columnsExistInTable() && $this->‪hasRecordsToUpdate();
58  }
59 
60  public function ‪executeUpdate(): bool
61  {
62  $this->moduleProvider = GeneralUtility::makeInstance(ModuleProvider::class);
63  $this->router = GeneralUtility::makeInstance(Router::class);
64  $connection = $this->‪getConnectionPool()->getConnectionForTable(self::TABLE_NAME);
65 
66  foreach ($this->‪getRecordsToUpdate() as ‪$record) {
67  [$moduleName] = explode('|', (string)‪$record['module_name'], 2);
68 
69  if (!is_string($moduleName) || $moduleName === '') {
70  continue;
71  }
72 
73  if (($routeIdentifier = $this->‪getRouteIdentifierForModuleName($moduleName)) === '') {
74  continue;
75  }
76 
77  // Parse the url and reveal the arguments (query parameters)
78  $parsedUrl = parse_url((string)‪$record['url']) ?: [];
79  $arguments = [];
80  parse_str($parsedUrl['query'] ?? '', $arguments);
81 
82  // Unset not longer needed arguments
83  unset($arguments['route'], $arguments['returnUrl']);
84 
85  try {
86  $encodedArguments = json_encode($arguments, JSON_THROW_ON_ERROR) ?: '';
87  } catch (\JsonException $e) {
88  // Skip the row if arguments can not be encoded
89  continue;
90  }
91 
92  // Update the record - Note: The "old" values won't be unset
93  $connection->update(
94  self::TABLE_NAME,
95  ['route' => $routeIdentifier, 'arguments' => $encodedArguments],
96  ['uid' => (int)‪$record['uid']]
97  );
98  }
99 
100  return true;
101  }
102 
103  protected function ‪columnsExistInTable(): bool
104  {
105  $schemaManager = $this->‪getConnectionPool()->getConnectionForTable(self::TABLE_NAME)->createSchemaManager();
106 
107  $tableColumns = $schemaManager->listTableColumns(self::TABLE_NAME);
108 
109  foreach (['module_name', 'url', 'route', 'arguments'] as $column) {
110  if (!isset($tableColumns[$column])) {
111  return false;
112  }
113  }
114 
115  return true;
116  }
117 
118  protected function ‪hasRecordsToUpdate(): bool
119  {
120  return (bool)$this->‪getPreparedQueryBuilder()->count('uid')->executeQuery()->fetchOne();
121  }
122 
123  protected function ‪getRecordsToUpdate(): array
124  {
125  return $this->‪getPreparedQueryBuilder()->select(...['uid', 'module_name', 'url'])->executeQuery()->fetchAllAssociative();
126  }
127 
128  protected function ‪getPreparedQueryBuilder(): QueryBuilder
129  {
130  $queryBuilder = $this->‪getConnectionPool()->getQueryBuilderForTable(self::TABLE_NAME);
131  $queryBuilder->getRestrictions()->removeAll();
132  $queryBuilder
133  ->from(self::TABLE_NAME)
134  ->where(
135  $queryBuilder->expr()->neq('module_name', $queryBuilder->createNamedParameter('')),
136  $queryBuilder->expr()->and(
137  $queryBuilder->expr()->neq('url', $queryBuilder->createNamedParameter('')),
138  $queryBuilder->expr()->isNotNull('url')
139  ),
140  $queryBuilder->expr()->eq('route', $queryBuilder->createNamedParameter('')),
141  $queryBuilder->expr()->or(
142  $queryBuilder->expr()->eq('arguments', $queryBuilder->createNamedParameter('')),
143  $queryBuilder->expr()->isNull('arguments')
144  )
145  );
146 
147  return $queryBuilder;
148  }
149 
150  protected function ‪getRouteIdentifierForModuleName(string $moduleName): string
151  {
152  // Check static special cases first
153  switch ($moduleName) {
154  case 'xMOD_alt_doc.php':
155  return 'record_edit';
156  case 'file_edit':
157  case 'wizard_rte':
158  return $moduleName;
159  }
160 
161  if ($this->moduleProvider->isModuleRegistered($moduleName)) {
162  return $moduleName;
163  }
164 
165  $alias = $this->router->getRouteCollection()->getAliases()[$moduleName] ?? null;
166  if ($alias) {
167  return $alias->getId();
168  }
169  return '';
170  }
171 
173  {
174  return GeneralUtility::makeInstance(ConnectionPool::class);
175  }
176 }
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\TABLE_NAME
‪const TABLE_NAME
Definition: ShortcutRecordsMigration.php:33
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\hasRecordsToUpdate
‪hasRecordsToUpdate()
Definition: ShortcutRecordsMigration.php:118
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getPrerequisites
‪getPrerequisites()
Definition: ShortcutRecordsMigration.php:48
‪TYPO3\CMS\Install\Attribute\UpgradeWizard
Definition: UpgradeWizard.php:27
‪TYPO3\CMS\Backend\Module\ModuleProvider
Definition: ModuleProvider.php:29
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\$moduleProvider
‪ModuleProvider $moduleProvider
Definition: ShortcutRecordsMigration.php:35
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:16
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\updateNecessary
‪updateNecessary()
Definition: ShortcutRecordsMigration.php:55
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getPreparedQueryBuilder
‪getPreparedQueryBuilder()
Definition: ShortcutRecordsMigration.php:128
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getConnectionPool
‪getConnectionPool()
Definition: ShortcutRecordsMigration.php:172
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\columnsExistInTable
‪columnsExistInTable()
Definition: ShortcutRecordsMigration.php:103
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\$router
‪Router $router
Definition: ShortcutRecordsMigration.php:36
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\executeUpdate
‪executeUpdate()
Definition: ShortcutRecordsMigration.php:60
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration
Definition: ShortcutRecordsMigration.php:32
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getTitle
‪getTitle()
Definition: ShortcutRecordsMigration.php:38
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getDescription
‪getDescription()
Definition: ShortcutRecordsMigration.php:43
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:24
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Backend\Routing\Router
Definition: Router.php:39
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getRecordsToUpdate
‪getRecordsToUpdate()
Definition: ShortcutRecordsMigration.php:123
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getRouteIdentifierForModuleName
‪getRouteIdentifierForModuleName(string $moduleName)
Definition: ShortcutRecordsMigration.php:150