Translated using Weblate (Japanese)
[phpmyadmin.git] / tbl_operations.php
blobf8bb73ba094077ae97fa3601d7ff38f5e879ca5a
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']
238 * Reordering the table has been requested by the user
240 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
241 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
242 } // end if
245 * A partition operation has been requested by the user
247 if (isset($_POST['submit_partition'])
248 && ! empty($_POST['partition_operation'])
250 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
251 } // end if
253 if ($reread_info) {
254 // to avoid showing the old value (for example the AUTO_INCREMENT) after
255 // a change, clear the cache
256 $dbi->clearTableCache();
257 $dbi->selectDb($db);
258 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
259 if ($pma_table->isView()) {
260 $tbl_is_view = true;
261 $tbl_storage_engine = __('View');
262 $show_comment = null;
263 } else {
264 $tbl_is_view = false;
265 $tbl_storage_engine = $pma_table->getStorageEngine();
266 $show_comment = $pma_table->getComment();
268 $tbl_collation = $pma_table->getCollation();
269 $table_info_num_rows = $pma_table->getNumRows();
270 $row_format = $pma_table->getRowFormat();
271 $auto_increment = $pma_table->getAutoIncrement();
272 $create_options = $pma_table->getCreateOptions();
274 unset($reread_info);
276 if (isset($result) && empty($message_to_show)) {
277 if (empty($_message)) {
278 if (empty($sql_query)) {
279 $_message = Message::success(__('No change'));
280 } else {
281 $_message = $result
282 ? Message::success()
283 : Message::error();
286 if ($response->isAjax()) {
287 $response->setRequestStatus($_message->isSuccess());
288 $response->addJSON('message', $_message);
289 if (! empty($sql_query)) {
290 $response->addJSON(
291 'sql_query',
292 Util::getMessage(null, $sql_query)
295 exit;
297 } else {
298 $_message = $result
299 ? Message::success($_message)
300 : Message::error($_message);
303 if (! empty($warning_messages)) {
304 $_message = new Message();
305 $_message->addMessagesString($warning_messages);
306 $_message->isError(true);
307 if ($response->isAjax()) {
308 $response->setRequestStatus(false);
309 $response->addJSON('message', $_message);
310 if (! empty($sql_query)) {
311 $response->addJSON(
312 'sql_query',
313 Util::getMessage(null, $sql_query)
316 exit;
318 unset($warning_messages);
321 if (empty($sql_query)) {
322 $response->addHTML(
323 $_message->getDisplay()
325 } else {
326 $response->addHTML(
327 Util::getMessage($_message, $sql_query)
330 unset($_message);
333 $url_params['goto']
334 = $url_params['back']
335 = 'tbl_operations.php';
338 * Get columns names
340 $columns = $dbi->getColumns($db, $table);
343 * Displays the page
347 * Order the table
349 $hideOrderTable = false;
350 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
351 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
352 // InnoDB always orders table rows according to such an index if one is present.
353 if ($tbl_storage_engine == 'INNODB') {
354 $indexes = Index::getFromTable($table, $db);
355 foreach ($indexes as $name => $idx) {
356 if ($name == 'PRIMARY') {
357 $hideOrderTable = true;
358 break;
359 } elseif (! $idx->getNonUnique()) {
360 $notNull = true;
361 foreach ($idx->getColumns() as $column) {
362 if ($column->getNull()) {
363 $notNull = false;
364 break;
367 if ($notNull) {
368 $hideOrderTable = true;
369 break;
374 if (! $hideOrderTable) {
375 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
379 * Move table
381 $response->addHTML($operations->getHtmlForMoveTable());
383 if (mb_strstr($show_comment, '; InnoDB free') === false) {
384 if (mb_strstr($show_comment, 'InnoDB free') === false) {
385 // only user entered comment
386 $comment = $show_comment;
387 } else {
388 // here we have just InnoDB generated part
389 $comment = '';
391 } else {
392 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
393 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
396 // PACK_KEYS: MyISAM or ISAM
397 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
398 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
400 // Here should be version check for InnoDB, however it is supported
401 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
402 // check for version
404 $response->addHTML(
405 $operations->getTableOptionDiv(
406 $pma_table,
407 $comment,
408 $tbl_collation,
409 $tbl_storage_engine,
410 $create_options['pack_keys'],
411 $auto_increment,
412 (empty($create_options['delay_key_write']) ? '0' : '1'),
413 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
414 (isset($create_options['page_checksum']) ? $create_options['page_checksum'] : ''),
415 (empty($create_options['checksum']) ? '0' : '1')
420 * Copy table
422 $response->addHTML($operations->getHtmlForCopytable());
425 * Table maintenance
427 $response->addHTML(
428 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
431 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
432 $truncate_table_url_params = [];
433 $drop_table_url_params = [];
435 if (! $tbl_is_view
436 && ! (isset($db_is_system_schema) && $db_is_system_schema)
438 $this_sql_query = 'TRUNCATE TABLE '
439 . Util::backquote($table);
440 $truncate_table_url_params = array_merge(
441 $url_params,
443 'sql_query' => $this_sql_query,
444 'goto' => 'tbl_structure.php',
445 'reload' => '1',
446 'message_to_show' => sprintf(
447 __('Table %s has been emptied.'),
448 htmlspecialchars($table)
453 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
454 $this_sql_query = 'DROP TABLE '
455 . Util::backquote($table);
456 $drop_table_url_params = array_merge(
457 $url_params,
459 'sql_query' => $this_sql_query,
460 'goto' => 'db_operations.php',
461 'reload' => '1',
462 'purge' => '1',
463 'message_to_show' => sprintf(
464 ($tbl_is_view
465 ? __('View %s has been dropped.')
466 : __('Table %s has been dropped.')
468 htmlspecialchars($table)
470 // table name is needed to avoid running
471 // PhpMyAdmin\RelationCleanup::database() on the whole db later
472 'table' => $table,
476 $response->addHTML(
477 $operations->getHtmlForDeleteDataOrTable(
478 $truncate_table_url_params,
479 $drop_table_url_params
484 if (Partition::havePartitioning()) {
485 $partition_names = Partition::getPartitionNames($db, $table);
486 // show the Partition maintenance section only if we detect a partition
487 if ($partition_names[0] !== null) {
488 $response->addHTML(
489 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
491 } // end if
492 } // end if
493 unset($partition_names);
495 // Referential integrity check
496 // The Referential integrity check was intended for the non-InnoDB
497 // tables for which the relations are defined in pmadb
498 // so I assume that if the current table is InnoDB, I don't display
499 // this choice (InnoDB maintains integrity by itself)
501 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
502 $dbi->selectDb($db);
503 $foreign = $relation->getForeigners($db, $table, '', 'internal');
505 if (! empty($foreign)) {
506 $response->addHTML(
507 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
509 } // end if ($foreign)
510 } // end if (!empty($cfg['Server']['relation']))