‪TYPO3CMS  11.5
ShortcutRecordsMigration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
7 /*
8  * This file is part of the TYPO3 CMS project.
9  *
10  * It is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License, either version 2
12  * of the License, or any later version.
13  *
14  * For the full copyright and license information, please read the
15  * LICENSE.txt file that was distributed with this source code.
16  *
17  * The TYPO3 project - inspiring people to share!
18  */
19 
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
24 
29 {
30  private const ‪TABLE_NAME = 'sys_be_shortcuts';
31 
32  protected ?iterable ‪$routes = null;
33 
34  public function ‪getIdentifier(): string
35  {
36  return 'shortcutRecordsMigration';
37  }
38 
39  public function ‪getTitle(): string
40  {
41  return 'Migrate shortcut records to new format.';
42  }
43 
44  public function ‪getDescription(): string
45  {
46  return 'To support speaking urls in the backend, some fields need to be changed in sys_be_shortcuts.';
47  }
48 
49  public function ‪getPrerequisites(): array
50  {
51  return [
52  DatabaseUpdatedPrerequisite::class,
53  ];
54  }
55 
56  public function ‪updateNecessary(): bool
57  {
58  return $this->‪columnsExistInTable() && $this->‪hasRecordsToUpdate();
59  }
60 
61  public function ‪executeUpdate(): bool
62  {
63  $this->routes = GeneralUtility::makeInstance(Router::class)->getRoutes();
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()->andX(
137  $queryBuilder->expr()->neq('url', $queryBuilder->createNamedParameter('')),
138  $queryBuilder->expr()->isNotNull('url')
139  ),
140  $queryBuilder->expr()->eq('route', $queryBuilder->createNamedParameter('')),
141  $queryBuilder->expr()->orX(
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  // Get identifier from module configuration
162  $routeIdentifier = ‪$GLOBALS['TBE_MODULES']['_configuration'][$moduleName]['id'] ?? $moduleName;
163 
164  // Check if a route with the identifier exist
165  if (isset($this->routes[$routeIdentifier])
166  && $this->routes[$routeIdentifier]->hasOption('moduleName')
167  && $this->routes[$routeIdentifier]->getOption('moduleName') === $moduleName
168  ) {
169  return $routeIdentifier;
170  }
171 
172  // If the defined route identifier can't be fetched, try from the other side
173  // by iterating over the routes to match a route by the defined module name
174  foreach ($this->routes as $identifier => $route) {
175  if ($route->hasOption('moduleName') && $route->getOption('moduleName') === $moduleName) {
176  return $routeIdentifier;
177  }
178  }
179 
180  return '';
181  }
182 
184  {
185  return GeneralUtility::makeInstance(ConnectionPool::class);
186  }
187 }
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\TABLE_NAME
‪const TABLE_NAME
Definition: ShortcutRecordsMigration.php:30
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getIdentifier
‪getIdentifier()
Definition: ShortcutRecordsMigration.php:34
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\hasRecordsToUpdate
‪hasRecordsToUpdate()
Definition: ShortcutRecordsMigration.php:118
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getPrerequisites
‪getPrerequisites()
Definition: ShortcutRecordsMigration.php:49
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:16
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\updateNecessary
‪updateNecessary()
Definition: ShortcutRecordsMigration.php:56
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getPreparedQueryBuilder
‪getPreparedQueryBuilder()
Definition: ShortcutRecordsMigration.php:128
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getConnectionPool
‪getConnectionPool()
Definition: ShortcutRecordsMigration.php:183
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\columnsExistInTable
‪columnsExistInTable()
Definition: ShortcutRecordsMigration.php:103
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\$routes
‪iterable $routes
Definition: ShortcutRecordsMigration.php:32
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\executeUpdate
‪executeUpdate()
Definition: ShortcutRecordsMigration.php:61
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration
Definition: ShortcutRecordsMigration.php:29
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getTitle
‪getTitle()
Definition: ShortcutRecordsMigration.php:39
‪TYPO3\CMS\Install\Updates\ShortcutRecordsMigration\getDescription
‪getDescription()
Definition: ShortcutRecordsMigration.php:44
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪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