2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
8 declare(strict_types
=1);
10 use PhpMyAdmin\CheckUserPrivileges
;
11 use PhpMyAdmin\DatabaseInterface
;
12 use PhpMyAdmin\Di\Container
;
14 use PhpMyAdmin\Message
;
15 use PhpMyAdmin\Partition
;
16 use PhpMyAdmin\Operations
;
17 use PhpMyAdmin\Relation
;
18 use PhpMyAdmin\Response
;
22 if (! defined('ROOT_PATH')) {
23 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
26 require_once ROOT_PATH
. 'libraries/common.inc.php';
28 $container = Container
::getDefaultContainer();
29 $container->set(Response
::class, Response
::getInstance());
31 /** @var Response $response */
32 $response = $container->get(Response
::class);
34 /** @var DatabaseInterface $dbi */
35 $dbi = $container->get(DatabaseInterface
::class);
37 $checkUserPrivileges = new CheckUserPrivileges($dbi);
38 $checkUserPrivileges->getPrivileges();
40 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
42 $header = $response->getHeader();
43 $scripts = $header->getScripts();
44 $scripts->addFile('tbl_operations.js');
49 require ROOT_PATH
. 'libraries/tbl_common.inc.php';
50 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
51 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
54 * Gets relation settings
56 $relation = new Relation($dbi);
57 $operations = new Operations($dbi, $relation);
59 $cfgRelation = $relation->getRelationsParam();
61 // reselect current db (needed in some cases probably due to
62 // the calling of PhpMyAdmin\Relation)
63 $dbi->selectDb($GLOBALS['db']);
66 * Gets tables information
68 $pma_table = $dbi->getTable(
72 $reread_info = $pma_table->getStatusInfo(null, false);
73 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ?
true : false));
74 if ($pma_table->isView()) {
76 $tbl_storage_engine = __('View');
80 $tbl_storage_engine = $pma_table->getStorageEngine();
81 $show_comment = $pma_table->getComment();
83 $tbl_collation = $pma_table->getCollation();
84 $table_info_num_rows = $pma_table->getNumRows();
85 $row_format = $pma_table->getRowFormat();
86 $auto_increment = $pma_table->getAutoIncrement();
87 $create_options = $pma_table->getCreateOptions();
89 // set initial value of these variables, based on the current table engine
90 if ($pma_table->isEngine('ARIA')) {
91 // the value for transactional can be implicit
92 // (no create option found, in this case it means 1)
93 // or explicit (option found with a value of 0 or 1)
94 // ($create_options['transactional'] may have been set by Table class,
95 // from the $create_options)
96 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
99 $create_options['page_checksum'] = isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : '';
102 $pma_table = $dbi->getTable(
106 $reread_info = false;
110 * If the table has to be moved to some other database
112 if (isset($_POST['submit_move']) ||
isset($_POST['submit_copy'])) {
114 $operations->moveOrCopyTable($db, $table);
115 // This was ended in an Ajax call
119 * If the table has to be maintained
121 if (isset($_POST['table_maintenance'])) {
122 include_once ROOT_PATH
. 'sql.php';
126 * Updates table comment, type and options if required
128 if (isset($_POST['submitoptions'])) {
130 $warning_messages = [];
132 if (isset($_POST['new_name'])) {
133 // Get original names before rename operation
134 $oldTable = $pma_table->getName();
135 $oldDb = $pma_table->getDbName();
137 if ($pma_table->rename($_POST['new_name'])) {
138 if (isset($_POST['adjust_privileges'])
139 && ! empty($_POST['adjust_privileges'])
141 $operations->adjustPrivilegesRenameOrMoveTable(
149 // Reselect the original DB
150 $GLOBALS['db'] = $oldDb;
151 $dbi->selectDb($oldDb);
152 $_message .= $pma_table->getLastMessage();
154 $GLOBALS['table'] = $pma_table->getName();
158 $_message .= $pma_table->getLastError();
163 if (! empty($_POST['new_tbl_storage_engine'])
164 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
166 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
168 if ($pma_table->isEngine('ARIA')) {
169 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
172 $create_options['page_checksum'] = isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : '';
175 $new_tbl_storage_engine = '';
178 $row_format = isset($create_options['row_format'])
179 ?
$create_options['row_format']
180 : $pma_table->getRowFormat();
182 $table_alters = $operations->getTableAltersArray(
184 $create_options['pack_keys'],
185 (empty($create_options['checksum']) ?
'0' : '1'),
186 (isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : ''),
187 (empty($create_options['delay_key_write']) ?
'0' : '1'),
189 $new_tbl_storage_engine,
190 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
194 if (count($table_alters) > 0) {
195 $sql_query = 'ALTER TABLE '
196 . Util
::backquote($GLOBALS['table']);
197 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
199 $result = $dbi->query($sql_query) ?
true : false;
201 unset($table_alters);
202 $warning_messages = $operations->getWarningMessagesArray();
205 if (isset($_POST['tbl_collation'])
206 && ! empty($_POST['tbl_collation'])
207 && isset($_POST['change_all_collations'])
208 && ! empty($_POST['change_all_collations'])
210 $operations->changeAllColumnsCollation(
213 $_POST['tbl_collation']
218 * Reordering the table has been requested by the user
220 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
221 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
225 * A partition operation has been requested by the user
227 if (isset($_POST['submit_partition'])
228 && ! empty($_POST['partition_operation'])
230 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
234 // to avoid showing the old value (for example the AUTO_INCREMENT) after
235 // a change, clear the cache
236 $dbi->clearTableCache();
237 $dbi->selectDb($GLOBALS['db']);
238 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
239 if ($pma_table->isView()) {
241 $tbl_storage_engine = __('View');
242 $show_comment = null;
244 $tbl_is_view = false;
245 $tbl_storage_engine = $pma_table->getStorageEngine();
246 $show_comment = $pma_table->getComment();
248 $tbl_collation = $pma_table->getCollation();
249 $table_info_num_rows = $pma_table->getNumRows();
250 $row_format = $pma_table->getRowFormat();
251 $auto_increment = $pma_table->getAutoIncrement();
252 $create_options = $pma_table->getCreateOptions();
256 if (isset($result) && empty($message_to_show)) {
257 if (empty($_message)) {
258 if (empty($sql_query)) {
259 $_message = Message
::success(__('No change'));
266 if ($response->isAjax()) {
267 $response->setRequestStatus($_message->isSuccess());
268 $response->addJSON('message', $_message);
269 if (! empty($sql_query)) {
272 Util
::getMessage(null, $sql_query)
279 ? Message
::success($_message)
280 : Message
::error($_message);
283 if (! empty($warning_messages)) {
284 $_message = new Message();
285 $_message->addMessagesString($warning_messages);
286 $_message->isError(true);
287 if ($response->isAjax()) {
288 $response->setRequestStatus(false);
289 $response->addJSON('message', $_message);
290 if (! empty($sql_query)) {
293 Util
::getMessage(null, $sql_query)
298 unset($warning_messages);
301 if (empty($sql_query)) {
303 $_message->getDisplay()
307 Util
::getMessage($_message, $sql_query)
314 = $url_params['back']
315 = 'tbl_operations.php';
320 $columns = $dbi->getColumns($GLOBALS['db'], $GLOBALS['table']);
329 $hideOrderTable = false;
330 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
331 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
332 // InnoDB always orders table rows according to such an index if one is present.
333 if ($tbl_storage_engine == 'INNODB') {
334 $indexes = Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
335 foreach ($indexes as $name => $idx) {
336 if ($name == 'PRIMARY') {
337 $hideOrderTable = true;
339 } elseif (! $idx->getNonUnique()) {
341 foreach ($idx->getColumns() as $column) {
342 if ($column->getNull()) {
348 $hideOrderTable = true;
354 if (! $hideOrderTable) {
355 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
361 $response->addHTML($operations->getHtmlForMoveTable());
363 if (mb_strstr($show_comment, '; InnoDB free') === false) {
364 if (mb_strstr($show_comment, 'InnoDB free') === false) {
365 // only user entered comment
366 $comment = $show_comment;
368 // here we have just InnoDB generated part
372 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
373 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
376 // PACK_KEYS: MyISAM or ISAM
377 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
378 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
380 // Here should be version check for InnoDB, however it is supported
381 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
385 $operations->getTableOptionDiv(
390 $create_options['pack_keys'],
392 (empty($create_options['delay_key_write']) ?
'0' : '1'),
393 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
394 (isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : ''),
395 (empty($create_options['checksum']) ?
'0' : '1')
402 $response->addHTML($operations->getHtmlForCopytable());
408 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
411 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
412 $truncate_table_url_params = [];
413 $drop_table_url_params = [];
416 && ! (isset($db_is_system_schema) && $db_is_system_schema)
418 $this_sql_query = 'TRUNCATE TABLE '
419 . Util
::backquote($GLOBALS['table']);
420 $truncate_table_url_params = array_merge(
423 'sql_query' => $this_sql_query,
424 'goto' => 'tbl_structure.php',
426 'message_to_show' => sprintf(
427 __('Table %s has been emptied.'),
428 htmlspecialchars($table)
433 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
434 $this_sql_query = 'DROP TABLE '
435 . Util
::backquote($GLOBALS['table']);
436 $drop_table_url_params = array_merge(
439 'sql_query' => $this_sql_query,
440 'goto' => 'db_operations.php',
443 'message_to_show' => sprintf(
445 ?
__('View %s has been dropped.')
446 : __('Table %s has been dropped.')
448 htmlspecialchars($table)
450 // table name is needed to avoid running
451 // PhpMyAdmin\RelationCleanup::database() on the whole db later
452 'table' => $GLOBALS['table'],
457 $operations->getHtmlForDeleteDataOrTable(
458 $truncate_table_url_params,
459 $drop_table_url_params
464 if (Partition
::havePartitioning()) {
465 $partition_names = Partition
::getPartitionNames($db, $table);
466 // show the Partition maintenance section only if we detect a partition
467 if (! is_null($partition_names[0])) {
469 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
473 unset($partition_names);
475 // Referential integrity check
476 // The Referential integrity check was intended for the non-InnoDB
477 // tables for which the relations are defined in pmadb
478 // so I assume that if the current table is InnoDB, I don't display
479 // this choice (InnoDB maintains integrity by itself)
481 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
482 $dbi->selectDb($GLOBALS['db']);
483 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
485 if (! empty($foreign)) {
487 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
489 } // end if ($foreign)
490 } // end if (!empty($cfg['Server']['relation']))