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