2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
8 declare(strict_types
=1);
11 use PhpMyAdmin\Message
;
12 use PhpMyAdmin\Partition
;
13 use PhpMyAdmin\Operations
;
14 use PhpMyAdmin\Relation
;
15 use PhpMyAdmin\Response
;
19 if (! defined('ROOT_PATH')) {
20 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
26 require_once ROOT_PATH
. 'libraries/common.inc.php';
29 * functions implementation for this script
31 require_once ROOT_PATH
. 'libraries/check_user_privileges.inc.php';
33 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
36 * Load JavaScript files
38 $response = Response
::getInstance();
39 $header = $response->getHeader();
40 $scripts = $header->getScripts();
41 $scripts->addFile('tbl_operations.js');
46 require ROOT_PATH
. 'libraries/tbl_common.inc.php';
47 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
48 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
51 * Gets relation settings
53 $relation = new Relation($GLOBALS['dbi']);
54 $operations = new Operations($GLOBALS['dbi'], $relation);
56 $cfgRelation = $relation->getRelationsParam();
58 // reselect current db (needed in some cases probably due to
59 // the calling of PhpMyAdmin\Relation)
60 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
63 * Gets tables information
65 $pma_table = $GLOBALS['dbi']->getTable(
69 $reread_info = $pma_table->getStatusInfo(null, false);
70 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ?
true : false));
71 if ($pma_table->isView()) {
73 $tbl_storage_engine = __('View');
77 $tbl_storage_engine = $pma_table->getStorageEngine();
78 $show_comment = $pma_table->getComment();
80 $tbl_collation = $pma_table->getCollation();
81 $table_info_num_rows = $pma_table->getNumRows();
82 $row_format = $pma_table->getRowFormat();
83 $auto_increment = $pma_table->getAutoIncrement();
84 $create_options = $pma_table->getCreateOptions();
86 // set initial value of these variables, based on the current table engine
87 if ($pma_table->isEngine('ARIA')) {
88 // the value for transactional can be implicit
89 // (no create option found, in this case it means 1)
90 // or explicit (option found with a value of 0 or 1)
91 // ($create_options['transactional'] may have been set by Table class,
92 // from the $create_options)
93 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
96 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
99 $pma_table = $GLOBALS['dbi']->getTable(
103 $reread_info = false;
107 * If the table has to be moved to some other database
109 if (isset($_POST['submit_move']) ||
isset($_POST['submit_copy'])) {
111 $operations->moveOrCopyTable($db, $table);
112 // This was ended in an Ajax call
116 * If the table has to be maintained
118 if (isset($_POST['table_maintenance'])) {
119 include_once ROOT_PATH
. 'sql.php';
123 * Updates table comment, type and options if required
125 if (isset($_POST['submitoptions'])) {
127 $warning_messages = [];
129 if (isset($_POST['new_name'])) {
130 // Get original names before rename operation
131 $oldTable = $pma_table->getName();
132 $oldDb = $pma_table->getDbName();
134 if ($pma_table->rename($_POST['new_name'])) {
135 if (isset($_POST['adjust_privileges'])
136 && ! empty($_POST['adjust_privileges'])
138 $operations->adjustPrivilegesRenameOrMoveTable(
146 // Reselect the original DB
147 $GLOBALS['db'] = $oldDb;
148 $GLOBALS['dbi']->selectDb($oldDb);
149 $_message .= $pma_table->getLastMessage();
151 $GLOBALS['table'] = $pma_table->getName();
155 $_message .= $pma_table->getLastError();
160 if (! empty($_POST['new_tbl_storage_engine'])
161 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
163 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
165 if ($pma_table->isEngine('ARIA')) {
166 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
169 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
172 $new_tbl_storage_engine = '';
175 $row_format = (isset($create_options['row_format']))
176 ?
$create_options['row_format']
177 : $pma_table->getRowFormat();
179 $table_alters = $operations->getTableAltersArray(
181 $create_options['pack_keys'],
182 (empty($create_options['checksum']) ?
'0' : '1'),
183 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
184 (empty($create_options['delay_key_write']) ?
'0' : '1'),
186 $new_tbl_storage_engine,
187 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
191 if (count($table_alters) > 0) {
192 $sql_query = 'ALTER TABLE '
193 . Util
::backquote($GLOBALS['table']);
194 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
196 $result = $GLOBALS['dbi']->query($sql_query) ?
true : false;
198 unset($table_alters);
199 $warning_messages = $operations->getWarningMessagesArray();
202 if (isset($_POST['tbl_collation'])
203 && ! empty($_POST['tbl_collation'])
204 && isset($_POST['change_all_collations'])
205 && ! empty($_POST['change_all_collations'])
207 $operations->changeAllColumnsCollation(
210 $_POST['tbl_collation']
215 * Reordering the table has been requested by the user
217 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
218 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
222 * A partition operation has been requested by the user
224 if (isset($_POST['submit_partition'])
225 && ! empty($_POST['partition_operation'])
227 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
231 // to avoid showing the old value (for example the AUTO_INCREMENT) after
232 // a change, clear the cache
233 $GLOBALS['dbi']->clearTableCache();
234 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
235 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
236 if ($pma_table->isView()) {
238 $tbl_storage_engine = __('View');
239 $show_comment = null;
241 $tbl_is_view = false;
242 $tbl_storage_engine = $pma_table->getStorageEngine();
243 $show_comment = $pma_table->getComment();
245 $tbl_collation = $pma_table->getCollation();
246 $table_info_num_rows = $pma_table->getNumRows();
247 $row_format = $pma_table->getRowFormat();
248 $auto_increment = $pma_table->getAutoIncrement();
249 $create_options = $pma_table->getCreateOptions();
253 if (isset($result) && empty($message_to_show)) {
254 if (empty($_message)) {
255 if (empty($sql_query)) {
256 $_message = Message
::success(__('No change'));
263 if ($response->isAjax()) {
264 $response->setRequestStatus($_message->isSuccess());
265 $response->addJSON('message', $_message);
266 if (! empty($sql_query)) {
269 Util
::getMessage(null, $sql_query)
276 ? Message
::success($_message)
277 : Message
::error($_message);
280 if (! empty($warning_messages)) {
281 $_message = new Message();
282 $_message->addMessagesString($warning_messages);
283 $_message->isError(true);
284 if ($response->isAjax()) {
285 $response->setRequestStatus(false);
286 $response->addJSON('message', $_message);
287 if (! empty($sql_query)) {
290 Util
::getMessage(null, $sql_query)
295 unset($warning_messages);
298 if (empty($sql_query)) {
300 $_message->getDisplay()
304 Util
::getMessage($_message, $sql_query)
311 = $url_params['back']
312 = 'tbl_operations.php';
317 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
326 $hideOrderTable = false;
327 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
328 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
329 // InnoDB always orders table rows according to such an index if one is present.
330 if ($tbl_storage_engine == 'INNODB') {
331 $indexes = Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
332 foreach ($indexes as $name => $idx) {
333 if ($name == 'PRIMARY') {
334 $hideOrderTable = true;
336 } elseif (! $idx->getNonUnique()) {
338 foreach ($idx->getColumns() as $column) {
339 if ($column->getNull()) {
345 $hideOrderTable = true;
351 if (! $hideOrderTable) {
352 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
358 $response->addHTML($operations->getHtmlForMoveTable());
360 if (mb_strstr($show_comment, '; InnoDB free') === false) {
361 if (mb_strstr($show_comment, 'InnoDB free') === false) {
362 // only user entered comment
363 $comment = $show_comment;
365 // here we have just InnoDB generated part
369 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
370 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
373 // PACK_KEYS: MyISAM or ISAM
374 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
375 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
377 // Here should be version check for InnoDB, however it is supported
378 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
382 $operations->getTableOptionDiv(
387 $create_options['pack_keys'],
389 (empty($create_options['delay_key_write']) ?
'0' : '1'),
390 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
391 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
392 (empty($create_options['checksum']) ?
'0' : '1')
399 $response->addHTML($operations->getHtmlForCopytable());
405 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
408 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
409 $truncate_table_url_params = [];
410 $drop_table_url_params = [];
413 && ! (isset($db_is_system_schema) && $db_is_system_schema)
415 $this_sql_query = 'TRUNCATE TABLE '
416 . Util
::backquote($GLOBALS['table']);
417 $truncate_table_url_params = array_merge(
420 'sql_query' => $this_sql_query,
421 'goto' => 'tbl_structure.php',
423 'message_to_show' => sprintf(
424 __('Table %s has been emptied.'),
425 htmlspecialchars($table)
430 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
431 $this_sql_query = 'DROP TABLE '
432 . Util
::backquote($GLOBALS['table']);
433 $drop_table_url_params = array_merge(
436 'sql_query' => $this_sql_query,
437 'goto' => 'db_operations.php',
440 'message_to_show' => sprintf(
442 ?
__('View %s has been dropped.')
443 : __('Table %s has been dropped.')
445 htmlspecialchars($table)
447 // table name is needed to avoid running
448 // PhpMyAdmin\RelationCleanup::database() on the whole db later
449 'table' => $GLOBALS['table'],
454 $operations->getHtmlForDeleteDataOrTable(
455 $truncate_table_url_params,
456 $drop_table_url_params
461 if (Partition
::havePartitioning()) {
462 $partition_names = Partition
::getPartitionNames($db, $table);
463 // show the Partition maintenance section only if we detect a partition
464 if (! is_null($partition_names[0])) {
466 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
470 unset($partition_names);
472 // Referential integrity check
473 // The Referential integrity check was intended for the non-InnoDB
474 // tables for which the relations are defined in pmadb
475 // so I assume that if the current table is InnoDB, I don't display
476 // this choice (InnoDB maintains integrity by itself)
478 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
479 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
480 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
482 if (! empty($foreign)) {
484 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
486 } // end if ($foreign)
487 } // end if (!empty($cfg['Server']['relation']))