ef28cd951cd385483bd6d14d92618a35ecb6be29
[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[] = $tables;
176 }
177
178 $optimizableTables = array_merge(...$optimizableTables);
179 }
180
181 sort($optimizableTables);
182
183 return $optimizableTables;
184 }
185
186 /**
187 * Retrieve all optimizable tables for a connection, optionally restricted to the subset
188 * of table names in the $tableNames array.
189 *
190 * @param Connection $connection
191 * @param array $tableNames
192 * @return array
193 */
194 protected function getOptimizableTablesForConnection(Connection $connection, array $tableNames = []): array
195 {
196 // Return empty list if the database platform is not MySQL
197 if (strpos($connection->getServerVersion(), 'MySQL') !== 0) {
198 return [];
199 }
200
201 // Retrieve all tables from the MySQL informaation schema that have an engine type
202 // that supports the OPTIMIZE TABLE command.
203 $queryBuilder = $connection->createQueryBuilder();
204 $queryBuilder->select('TABLE_NAME AS Table', 'ENGINE AS Engine')
205 ->from('information_schema.TABLES')
206 ->where(
207 $queryBuilder->expr()->eq(
208 'TABLE_TYPE',
209 $queryBuilder->createNamedParameter('BASE TABLE', \PDO::PARAM_STR)
210 ),
211 $queryBuilder->expr()->in(
212 'ENGINE',
213 $queryBuilder->createNamedParameter(['InnoDB', 'MyISAM', 'ARCHIVE'], Connection::PARAM_STR_ARRAY)
214 ),
215 $queryBuilder->expr()->eq(
216 'TABLE_SCHEMA',
217 $queryBuilder->createNamedParameter($connection->getDatabase(), \PDO::PARAM_STR)
218 )
219 );
220
221 if (!empty($tableNames)) {
222 $queryBuilder->andWhere(
223 $queryBuilder->expr()->in(
224 'TABLE_NAME',
225 $queryBuilder->createNamedParameter($tableNames, \PDO::PARAM_STR)
226 )
227 );
228 }
229
230 $tables = $queryBuilder->execute()->fetchAll();
231
232 return array_column($tables, 'Table');
233 }
234
235 /**
236 * @return LanguageService|null
237 */
238 protected function getLanguageService(): ?LanguageService
239 {
240 return $GLOBALS['LANG'] ?? null;
241 }
242 }