Translated using Weblate (French)
[phpmyadmin.git] / tbl_operations.php
blobf0568bd55dfd2ae1cb5703b095cfb5a506afb34f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Various table operations
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 use PhpMyAdmin\CheckUserPrivileges;
11 use PhpMyAdmin\DatabaseInterface;
12 use PhpMyAdmin\Index;
13 use PhpMyAdmin\Message;
14 use PhpMyAdmin\Operations;
15 use PhpMyAdmin\Partition;
16 use PhpMyAdmin\Relation;
17 use PhpMyAdmin\Response;
18 use PhpMyAdmin\Table;
19 use PhpMyAdmin\Util;
21 if (! defined('ROOT_PATH')) {
22 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
25 global $url_query;
27 require_once ROOT_PATH . 'libraries/common.inc.php';
29 /** @var Response $response */
30 $response = $containerBuilder->get(Response::class);
32 /** @var DatabaseInterface $dbi */
33 $dbi = $containerBuilder->get(DatabaseInterface::class);
35 /** @var string $db */
36 $db = $containerBuilder->getParameter('db');
38 /** @var string $table */
39 $table = $containerBuilder->getParameter('table');
41 /** @var CheckUserPrivileges $checkUserPrivileges */
42 $checkUserPrivileges = $containerBuilder->get('check_user_privileges');
43 $checkUserPrivileges->getPrivileges();
45 // lower_case_table_names=1 `DB` becomes `db`
46 $lowerCaseNames = $dbi->getLowerCaseNames() === '1';
48 if ($lowerCaseNames) {
49 $table = mb_strtolower($table);
52 $pma_table = new Table($table, $db);
54 $header = $response->getHeader();
55 $scripts = $header->getScripts();
56 $scripts->addFile('table/operations.js');
58 /**
59 * Runs common work
61 require ROOT_PATH . 'libraries/tbl_common.inc.php';
62 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
63 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
65 /**
66 * Gets relation settings
68 /** @var Relation $relation */
69 $relation = $containerBuilder->get('relation');
70 $cfgRelation = $relation->getRelationsParam();
72 /** @var Operations $operations */
73 $operations = $containerBuilder->get('operations');
75 // reselect current db (needed in some cases probably due to
76 // the calling of PhpMyAdmin\Relation)
77 $dbi->selectDb($db);
79 /**
80 * Gets tables information
82 $pma_table = $dbi->getTable(
83 $db,
84 $table
86 $reread_info = $pma_table->getStatusInfo(null, false);
87 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ? true : false));
88 if ($pma_table->isView()) {
89 $tbl_is_view = true;
90 $tbl_storage_engine = __('View');
91 $show_comment = null;
92 } else {
93 $tbl_is_view = false;
94 $tbl_storage_engine = $pma_table->getStorageEngine();
95 $show_comment = $pma_table->getComment();
97 $tbl_collation = $pma_table->getCollation();
98 $table_info_num_rows = $pma_table->getNumRows();
99 $row_format = $pma_table->getRowFormat();
100 $auto_increment = $pma_table->getAutoIncrement();
101 $create_options = $pma_table->getCreateOptions();
103 // set initial value of these variables, based on the current table engine
104 if ($pma_table->isEngine('ARIA')) {
105 // the value for transactional can be implicit
106 // (no create option found, in this case it means 1)
107 // or explicit (option found with a value of 0 or 1)
108 // ($create_options['transactional'] may have been set by Table class,
109 // from the $create_options)
110 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
111 ? '0'
112 : '1';
113 $create_options['page_checksum'] = isset($create_options['page_checksum']) ? $create_options['page_checksum'] : '';
116 $pma_table = $dbi->getTable(
117 $db,
118 $table
120 $reread_info = false;
121 $table_alters = [];
124 * If the table has to be moved to some other database
126 if (isset($_POST['submit_move']) || isset($_POST['submit_copy'])) {
127 //$_message = '';
128 $operations->moveOrCopyTable($db, $table);
129 // This was ended in an Ajax call
130 exit;
133 * If the table has to be maintained
135 if (isset($_POST['table_maintenance'])) {
136 include_once ROOT_PATH . 'sql.php';
137 unset($result);
140 * Updates table comment, type and options if required
142 if (isset($_POST['submitoptions'])) {
143 $_message = '';
144 $warning_messages = [];
146 if (isset($_POST['new_name'])) {
147 // lower_case_table_names=1 `DB` becomes `db`
148 if ($lowerCaseNames) {
149 $_POST['new_name'] = mb_strtolower(
150 $_POST['new_name']
153 // Get original names before rename operation
154 $oldTable = $pma_table->getName();
155 $oldDb = $pma_table->getDbName();
157 if ($pma_table->rename($_POST['new_name'])) {
158 if (isset($_POST['adjust_privileges'])
159 && ! empty($_POST['adjust_privileges'])
161 $operations->adjustPrivilegesRenameOrMoveTable(
162 $oldDb,
163 $oldTable,
164 $_POST['db'],
165 $_POST['new_name']
169 // Reselect the original DB
170 $db = $oldDb;
171 $dbi->selectDb($oldDb);
172 $_message .= $pma_table->getLastMessage();
173 $result = true;
174 $table = $pma_table->getName();
175 $reread_info = true;
176 $reload = true;
177 } else {
178 $_message .= $pma_table->getLastError();
179 $result = false;
183 if (! empty($_POST['new_tbl_storage_engine'])
184 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
186 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
188 if ($pma_table->isEngine('ARIA')) {
189 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
190 ? '0'
191 : '1';
192 $create_options['page_checksum'] = isset($create_options['page_checksum']) ? $create_options['page_checksum'] : '';
194 } else {
195 $new_tbl_storage_engine = '';
198 $row_format = isset($create_options['row_format'])
199 ? $create_options['row_format']
200 : $pma_table->getRowFormat();
202 $table_alters = $operations->getTableAltersArray(
203 $pma_table,
204 $create_options['pack_keys'],
205 (empty($create_options['checksum']) ? '0' : '1'),
206 (isset($create_options['page_checksum']) ? $create_options['page_checksum'] : ''),
207 (empty($create_options['delay_key_write']) ? '0' : '1'),
208 $row_format,
209 $new_tbl_storage_engine,
210 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
211 $tbl_collation
214 if (count($table_alters) > 0) {
215 $sql_query = 'ALTER TABLE '
216 . Util::backquote($table);
217 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
218 $sql_query .= ';';
219 $result = $dbi->query($sql_query) ? true : false;
220 $reread_info = true;
221 unset($table_alters);
222 $warning_messages = $operations->getWarningMessagesArray();
225 if (isset($_POST['tbl_collation'])
226 && ! empty($_POST['tbl_collation'])
227 && isset($_POST['change_all_collations'])
228 && ! empty($_POST['change_all_collations'])
230 $operations->changeAllColumnsCollation(
231 $db,
232 $table,
233 $_POST['tbl_collation']
237 if (isset($_POST['tbl_collation']) && empty($_POST['tbl_collation'])) {
238 $response = Response::getInstance();
239 if ($response->isAjax()) {
240 $response->setRequestStatus(false);
241 $response->addJSON(
242 'message',
243 Message::error(__('No collation provided.'))
245 exit;
250 * Reordering the table has been requested by the user
252 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
253 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
254 } // end if
257 * A partition operation has been requested by the user
259 if (isset($_POST['submit_partition'])
260 && ! empty($_POST['partition_operation'])
262 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
263 } // end if
265 if ($reread_info) {
266 // to avoid showing the old value (for example the AUTO_INCREMENT) after
267 // a change, clear the cache
268 $dbi->clearTableCache();
269 $dbi->selectDb($db);
270 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
271 if ($pma_table->isView()) {
272 $tbl_is_view = true;
273 $tbl_storage_engine = __('View');
274 $show_comment = null;
275 } else {
276 $tbl_is_view = false;
277 $tbl_storage_engine = $pma_table->getStorageEngine();
278 $show_comment = $pma_table->getComment();
280 $tbl_collation = $pma_table->getCollation();
281 $table_info_num_rows = $pma_table->getNumRows();
282 $row_format = $pma_table->getRowFormat();
283 $auto_increment = $pma_table->getAutoIncrement();
284 $create_options = $pma_table->getCreateOptions();
286 unset($reread_info);
288 if (isset($result) && empty($message_to_show)) {
289 if (empty($_message)) {
290 if (empty($sql_query)) {
291 $_message = Message::success(__('No change'));
292 } else {
293 $_message = $result
294 ? Message::success()
295 : Message::error();
298 if ($response->isAjax()) {
299 $response->setRequestStatus($_message->isSuccess());
300 $response->addJSON('message', $_message);
301 if (! empty($sql_query)) {
302 $response->addJSON(
303 'sql_query',
304 Util::getMessage(null, $sql_query)
307 exit;
309 } else {
310 $_message = $result
311 ? Message::success($_message)
312 : Message::error($_message);
315 if (! empty($warning_messages)) {
316 $_message = new Message();
317 $_message->addMessagesString($warning_messages);
318 $_message->isError(true);
319 if ($response->isAjax()) {
320 $response->setRequestStatus(false);
321 $response->addJSON('message', $_message);
322 if (! empty($sql_query)) {
323 $response->addJSON(
324 'sql_query',
325 Util::getMessage(null, $sql_query)
328 exit;
330 unset($warning_messages);
333 if (empty($sql_query)) {
334 $response->addHTML(
335 $_message->getDisplay()
337 } else {
338 $response->addHTML(
339 Util::getMessage($_message, $sql_query)
342 unset($_message);
345 $url_params['goto']
346 = $url_params['back']
347 = 'tbl_operations.php';
350 * Get columns names
352 $columns = $dbi->getColumns($db, $table);
355 * Displays the page
359 * Order the table
361 $hideOrderTable = false;
362 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
363 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
364 // InnoDB always orders table rows according to such an index if one is present.
365 if ($tbl_storage_engine == 'INNODB') {
366 $indexes = Index::getFromTable($table, $db);
367 foreach ($indexes as $name => $idx) {
368 if ($name == 'PRIMARY') {
369 $hideOrderTable = true;
370 break;
371 } elseif (! $idx->getNonUnique()) {
372 $notNull = true;
373 foreach ($idx->getColumns() as $column) {
374 if ($column->getNull()) {
375 $notNull = false;
376 break;
379 if ($notNull) {
380 $hideOrderTable = true;
381 break;
386 if (! $hideOrderTable) {
387 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
391 * Move table
393 $response->addHTML($operations->getHtmlForMoveTable());
395 if (mb_strstr($show_comment, '; InnoDB free') === false) {
396 if (mb_strstr($show_comment, 'InnoDB free') === false) {
397 // only user entered comment
398 $comment = $show_comment;
399 } else {
400 // here we have just InnoDB generated part
401 $comment = '';
403 } else {
404 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
405 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
408 // PACK_KEYS: MyISAM or ISAM
409 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
410 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
412 // Here should be version check for InnoDB, however it is supported
413 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
414 // check for version
416 $response->addHTML(
417 $operations->getTableOptionDiv(
418 $pma_table,
419 $comment,
420 $tbl_collation,
421 $tbl_storage_engine,
422 $create_options['pack_keys'],
423 $auto_increment,
424 (empty($create_options['delay_key_write']) ? '0' : '1'),
425 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
426 (isset($create_options['page_checksum']) ? $create_options['page_checksum'] : ''),
427 (empty($create_options['checksum']) ? '0' : '1')
432 * Copy table
434 $response->addHTML($operations->getHtmlForCopytable());
437 * Table maintenance
439 $response->addHTML(
440 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
443 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
444 $truncate_table_url_params = [];
445 $drop_table_url_params = [];
447 if (! $tbl_is_view
448 && ! (isset($db_is_system_schema) && $db_is_system_schema)
450 $this_sql_query = 'TRUNCATE TABLE '
451 . Util::backquote($table);
452 $truncate_table_url_params = array_merge(
453 $url_params,
455 'sql_query' => $this_sql_query,
456 'goto' => 'tbl_structure.php',
457 'reload' => '1',
458 'message_to_show' => sprintf(
459 __('Table %s has been emptied.'),
460 htmlspecialchars($table)
465 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
466 $this_sql_query = 'DROP TABLE '
467 . Util::backquote($table);
468 $drop_table_url_params = array_merge(
469 $url_params,
471 'sql_query' => $this_sql_query,
472 'goto' => 'db_operations.php',
473 'reload' => '1',
474 'purge' => '1',
475 'message_to_show' => sprintf(
476 ($tbl_is_view
477 ? __('View %s has been dropped.')
478 : __('Table %s has been dropped.')
480 htmlspecialchars($table)
482 // table name is needed to avoid running
483 // PhpMyAdmin\RelationCleanup::database() on the whole db later
484 'table' => $table,
488 $response->addHTML(
489 $operations->getHtmlForDeleteDataOrTable(
490 $truncate_table_url_params,
491 $drop_table_url_params
496 if (Partition::havePartitioning()) {
497 $partition_names = Partition::getPartitionNames($db, $table);
498 // show the Partition maintenance section only if we detect a partition
499 if ($partition_names[0] !== null) {
500 $response->addHTML(
501 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
503 } // end if
504 } // end if
505 unset($partition_names);
507 // Referential integrity check
509 if ($cfgRelation['relwork']) {
510 $dbi->selectDb($db);
511 $foreign = $relation->getForeigners($db, $table, '', 'internal');
513 if (! empty($foreign)) {
514 $response->addHTML(
515 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
517 } // end if ($foreign)
518 } // end if (!empty($cfg['Server']['relation']))