Revert "[TASK] Avoid slow array functions in loops"
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / OptimizeDatabaseTableAdditionalFieldProvider.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\Connection;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Localization\LanguageService;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Scheduler\AbstractAdditionalFieldProvider;
23 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
24 use TYPO3\CMS\Scheduler\Task\Enumeration\Action;
25
26 /**
27 * Additional BE fields for optimize database table task.
28 * @internal This class is a specific scheduler task implementation is not considered part of the Public TYPO3 API.
29 */
30 class OptimizeDatabaseTableAdditionalFieldProvider extends AbstractAdditionalFieldProvider
31 {
32 /**
33 * @var string
34 */
35 protected $languageFile = 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf';
36
37 /**
38 * Add a multi select box with all available database tables.
39 *
40 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
41 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
42 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
43 * @return array Array containing all the information pertaining to the additional fields
44 */
45 public function getAdditionalFields(array &$taskInfo, $task, SchedulerModuleController $schedulerModule)
46 {
47 $currentSchedulerModuleAction = $schedulerModule->getCurrentAction();
48
49 // Initialize selected fields
50 if (empty($taskInfo['scheduler_optimizeDatabaseTables_selectedTables'])) {
51 $taskInfo['scheduler_optimizeDatabaseTables_selectedTables'] = [];
52 if ($currentSchedulerModuleAction->equals(Action::ADD)) {
53 // In case of new task, select no tables by default
54 $taskInfo['scheduler_optimizeDatabaseTables_selectedTables'] = [];
55 } elseif ($currentSchedulerModuleAction->equals(Action::EDIT)) {
56 // In case of editing the task, set to currently selected value
57 $taskInfo['scheduler_optimizeDatabaseTables_selectedTables'] = $task->selectedTables;
58 }
59 }
60 $fieldName = 'tx_scheduler[scheduler_optimizeDatabaseTables_selectedTables][]';
61 $fieldId = 'scheduler_optimizeDatabaseTables_selectedTables';
62 $fieldOptions = $this->getDatabaseTableOptions($taskInfo['scheduler_optimizeDatabaseTables_selectedTables']);
63 $fieldHtml = '<select class="form-control" name="' . $fieldName
64 . '" id="' . $fieldId
65 . '" class="from-control" size="10" multiple="multiple">'
66 . $fieldOptions
67 . '</select>';
68 $additionalFields[$fieldId] = [
69 'code' => $fieldHtml,
70 'label' => $this->languageFile . ':label.optimizeDatabaseTables.selectTables',
71 'cshKey' => '_MOD_system_txschedulerM1',
72 'cshLabel' => $fieldId,
73 ];
74
75 return $additionalFields;
76 }
77
78 /**
79 * Checks that all selected backends exist in available backend list
80 *
81 * @param array $submittedData Reference to the array containing the data submitted by the user
82 * @param SchedulerModuleController $schedulerModule Reference to the calling object (Scheduler's BE module)
83 * @return bool TRUE if validation was ok (or selected class is not relevant), FALSE otherwise
84 */
85 public function validateAdditionalFields(array &$submittedData, SchedulerModuleController $schedulerModule)
86 {
87 $validData = true;
88 $availableTables = $this->getOptimizableTables();
89 if (is_array($submittedData['scheduler_optimizeDatabaseTables_selectedTables'])) {
90 $invalidTables = array_diff(
91 $submittedData['scheduler_optimizeDatabaseTables_selectedTables'],
92 $availableTables
93 );
94 if (!empty($invalidTables)) {
95 $this->addMessage(
96 $this->getLanguageService()->sL($this->languageFile . ':msg.selectionOfNonExistingDatabaseTables'),
97 FlashMessage::ERROR
98 );
99 $validData = false;
100 }
101 } else {
102 $this->addMessage(
103 $this->getLanguageService()->sL($this->languageFile . ':msg.noDatabaseTablesSelected'),
104 FlashMessage::ERROR
105 );
106 $validData = false;
107 }
108
109 return $validData;
110 }
111
112 /**
113 * Save selected backends in task object
114 *
115 * @param array $submittedData Contains data submitted by the user
116 * @param AbstractTask $task Reference to the current task object
117 */
118 public function saveAdditionalFields(array $submittedData, AbstractTask $task)
119 {
120 $task->selectedTables = $submittedData['scheduler_optimizeDatabaseTables_selectedTables'];
121 }
122
123 /**
124 * Build select options of available backends and set currently selected backends
125 *
126 * @param array $selectedTables Selected backends
127 * @return string HTML of selectbox options
128 */
129 protected function getDatabaseTableOptions(array $selectedTables)
130 {
131 $options = [];
132 $availableTables = $this->getOptimizableTables();
133 foreach ($availableTables as $tableName) {
134 $selected = in_array($tableName, $selectedTables, true) ? ' selected="selected"' : '';
135 $options[] = '<option value="' . $tableName . '"' . $selected . '>' . $tableName . '</option>';
136 }
137
138 return implode('', $options);
139 }
140
141 /**
142 * Get all tables that are capable of optimization
143 *
144 * @return array Names of table that can be optimized.
145 */
146 protected function getOptimizableTables()
147 {
148 $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
149 $defaultConnection = $connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
150
151 // Retrieve all optimizable tables for the default connection
152 $optimizableTables = $this->getOptimizableTablesForConnection($defaultConnection);
153
154 // Retrieve additional optimizable tables that have been remapped to a different connection
155 $tableMap = $GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'] ?? [];
156 if ($tableMap) {
157 // Remove all remapped tables from the list of optimizable tables
158 // These tables will be rechecked and possibly re-added to the list
159 // of optimizable tables. This ensures that no orphaned table from
160 // the default connection gets mistakenly labeled as optimizable.
161 $optimizableTables = array_diff($optimizableTables, array_keys($tableMap));
162
163 // Walk each connection and check all tables that have been
164 // remapped to it for optimization support.
165 $connectionNames = array_keys(array_flip($tableMap));
166 foreach ($connectionNames as $connectionName) {
167 $connection = $connectionPool->getConnectionByName($connectionName);
168 $tablesOnConnection = array_keys(array_filter(
169 $tableMap,
170 function ($value) use ($connectionName) {
171 return $value === $connectionName;
172 }
173 ));
174 $tables = $this->getOptimizableTablesForConnection($connection, $tablesOnConnection);
175 $optimizableTables = array_merge($optimizableTables, $tables);
176 }
177 }
178
179 sort($optimizableTables);
180
181 return $optimizableTables;
182 }
183
184 /**
185 * Retrieve all optimizable tables for a connection, optionally restricted to the subset
186 * of table names in the $tableNames array.
187 *
188 * @param Connection $connection
189 * @param array $tableNames
190 * @return array
191 */
192 protected function getOptimizableTablesForConnection(Connection $connection, array $tableNames = []): array
193 {
194 // Return empty list if the database platform is not MySQL
195 if (strpos($connection->getServerVersion(), 'MySQL') !== 0) {
196 return [];
197 }
198
199 // Retrieve all tables from the MySQL informaation schema that have an engine type
200 // that supports the OPTIMIZE TABLE command.
201 $queryBuilder = $connection->createQueryBuilder();
202 $queryBuilder->select('TABLE_NAME AS Table', 'ENGINE AS Engine')
203 ->from('information_schema.TABLES')
204 ->where(
205 $queryBuilder->expr()->eq(
206 'TABLE_TYPE',
207 $queryBuilder->createNamedParameter('BASE TABLE', \PDO::PARAM_STR)
208 ),
209 $queryBuilder->expr()->in(
210 'ENGINE',
211 $queryBuilder->createNamedParameter(['InnoDB', 'MyISAM', 'ARCHIVE'], Connection::PARAM_STR_ARRAY)
212 ),
213 $queryBuilder->expr()->eq(
214 'TABLE_SCHEMA',
215 $queryBuilder->createNamedParameter($connection->getDatabase(), \PDO::PARAM_STR)
216 )
217 );
218
219 if (!empty($tableNames)) {
220 $queryBuilder->andWhere(
221 $queryBuilder->expr()->in(
222 'TABLE_NAME',
223 $queryBuilder->createNamedParameter($tableNames, \PDO::PARAM_STR)
224 )
225 );
226 }
227
228 $tables = $queryBuilder->execute()->fetchAll();
229
230 return array_column($tables, 'Table');
231 }
232
233 /**
234 * @return LanguageService|null
235 */
236 protected function getLanguageService(): ?LanguageService
237 {
238 return $GLOBALS['LANG'] ?? null;
239 }
240 }