2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
9 use PhpMyAdmin\Message
;
10 use PhpMyAdmin\Partition
;
11 use PhpMyAdmin\Operations
;
12 use PhpMyAdmin\Relation
;
13 use PhpMyAdmin\Response
;
20 require_once 'libraries/common.inc.php';
23 * functions implementation for this script
25 require_once 'libraries/check_user_privileges.inc.php';
27 // lower_case_table_names=1 `DB` becomes `db`
28 $lowerCaseNames = $GLOBALS['dbi']->getLowerCaseNames() === '1';
30 if ($lowerCaseNames) {
31 $GLOBALS['table'] = mb_strtolower(
36 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
39 * Load JavaScript files
41 $response = Response
::getInstance();
42 $header = $response->getHeader();
43 $scripts = $header->getScripts();
44 $scripts->addFile('tbl_operations.js');
49 require '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();
57 $cfgRelation = $relation->getRelationsParam();
59 // reselect current db (needed in some cases probably due to
60 // the calling of PhpMyAdmin\Relation)
61 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
64 * Gets tables information
66 $pma_table = $GLOBALS['dbi']->getTable(
70 $reread_info = $pma_table->getStatusInfo(null, false);
71 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ?
true : false));
72 if ($pma_table->isView()) {
74 $tbl_storage_engine = __('View');
78 $tbl_storage_engine = $pma_table->getStorageEngine();
79 $show_comment = $pma_table->getComment();
81 $tbl_collation = $pma_table->getCollation();
82 $table_info_num_rows = $pma_table->getNumRows();
83 $row_format = $pma_table->getRowFormat();
84 $auto_increment = $pma_table->getAutoIncrement();
85 $create_options = $pma_table->getCreateOptions();
87 // set initial value of these variables, based on the current table engine
88 if ($pma_table->isEngine('ARIA')) {
89 // the value for transactional can be implicit
90 // (no create option found, in this case it means 1)
91 // or explicit (option found with a value of 0 or 1)
92 // ($create_options['transactional'] may have been set by Table class,
93 // from the $create_options)
94 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
97 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
100 $pma_table = $GLOBALS['dbi']->getTable(
104 $reread_info = false;
105 $table_alters = array();
107 $operations = new Operations();
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 'sql.php';
126 * Updates table comment, type and options if required
128 if (isset($_POST['submitoptions'])) {
130 $warning_messages = array();
132 if (isset($_POST['new_name'])) {
133 // lower_case_table_names=1 `DB` becomes `db`
134 if ($lowerCaseNames) {
135 $_POST['new_name'] = mb_strtolower(
139 // Get original names before rename operation
140 $oldTable = $pma_table->getName();
141 $oldDb = $pma_table->getDbName();
143 if ($pma_table->rename($_POST['new_name'])) {
144 if (isset($_POST['adjust_privileges'])
145 && ! empty($_POST['adjust_privileges'])
147 $operations->adjustPrivilegesRenameOrMoveTable(
148 $oldDb, $oldTable, $_POST['db'], $_POST['new_name']
152 // Reselect the original DB
153 $GLOBALS['db'] = $oldDb;
154 $GLOBALS['dbi']->selectDb($oldDb);
155 $_message .= $pma_table->getLastMessage();
157 $GLOBALS['table'] = $pma_table->getName();
161 $_message .= $pma_table->getLastError();
166 if (! empty($_POST['new_tbl_storage_engine'])
167 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
169 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
171 if ($pma_table->isEngine('ARIA')) {
172 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
175 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
178 $new_tbl_storage_engine = '';
181 $row_format = (isset($create_options['row_format']))
182 ?
$create_options['row_format']
183 : $pma_table->getRowFormat();
185 $table_alters = $operations->getTableAltersArray(
187 $create_options['pack_keys'],
188 (empty($create_options['checksum']) ?
'0' : '1'),
189 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
190 (empty($create_options['delay_key_write']) ?
'0' : '1'),
192 $new_tbl_storage_engine,
193 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
197 if (count($table_alters) > 0) {
198 $sql_query = 'ALTER TABLE '
199 . Util
::backquote($GLOBALS['table']);
200 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
202 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
204 unset($table_alters);
205 $warning_messages = $operations->getWarningMessagesArray();
208 if (isset($_POST['tbl_collation'])
209 && ! empty($_POST['tbl_collation'])
210 && isset($_POST['change_all_collations'])
211 && ! empty($_POST['change_all_collations'])
213 $operations->changeAllColumnsCollation(
214 $GLOBALS['db'], $GLOBALS['table'], $_POST['tbl_collation']
218 if (isset($_POST['tbl_collation']) && empty($_POST['tbl_collation'])) {
219 $response = Response
::getInstance();
220 if ($response->isAjax()) {
221 $response->setRequestStatus(false);
224 Message
::error(__('No collation provided.'))
231 * Reordering the table has been requested by the user
233 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
234 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
238 * A partition operation has been requested by the user
240 if (isset($_POST['submit_partition'])
241 && ! empty($_POST['partition_operation'])
243 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
247 // to avoid showing the old value (for example the AUTO_INCREMENT) after
248 // a change, clear the cache
249 $GLOBALS['dbi']->clearTableCache();
250 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
251 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
252 if ($pma_table->isView()) {
254 $tbl_storage_engine = __('View');
255 $show_comment = null;
257 $tbl_is_view = false;
258 $tbl_storage_engine = $pma_table->getStorageEngine();
259 $show_comment = $pma_table->getComment();
261 $tbl_collation = $pma_table->getCollation();
262 $table_info_num_rows = $pma_table->getNumRows();
263 $row_format = $pma_table->getRowFormat();
264 $auto_increment = $pma_table->getAutoIncrement();
265 $create_options = $pma_table->getCreateOptions();
269 if (isset($result) && empty($message_to_show)) {
270 if (empty($_message)) {
271 if (empty($sql_query)) {
272 $_message = Message
::success(__('No change'));
279 if ($response->isAjax()) {
280 $response->setRequestStatus($_message->isSuccess());
281 $response->addJSON('message', $_message);
282 if (!empty($sql_query)) {
284 'sql_query', Util
::getMessage(null, $sql_query)
291 ? Message
::success($_message)
292 : Message
::error($_message);
295 if (! empty($warning_messages)) {
296 $_message = new Message
;
297 $_message->addMessagesString($warning_messages);
298 $_message->isError(true);
299 if ($response->isAjax()) {
300 $response->setRequestStatus(false);
301 $response->addJSON('message', $_message);
302 if (!empty($sql_query)) {
304 'sql_query', Util
::getMessage(null, $sql_query)
309 unset($warning_messages);
312 if (empty($sql_query)) {
314 $_message->getDisplay()
318 Util
::getMessage($_message, $sql_query)
325 = $url_params['back']
326 = 'tbl_operations.php';
331 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
340 $hideOrderTable = false;
341 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
342 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
343 // InnoDB always orders table rows according to such an index if one is present.
344 if ($tbl_storage_engine == 'INNODB') {
345 $indexes = Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
346 foreach ($indexes as $name => $idx) {
347 if ($name == 'PRIMARY') {
348 $hideOrderTable = true;
350 } elseif (! $idx->getNonUnique()) {
352 foreach ($idx->getColumns() as $column) {
353 if ($column->getNull()) {
359 $hideOrderTable = true;
365 if (! $hideOrderTable) {
366 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
372 $response->addHTML($operations->getHtmlForMoveTable());
374 if (mb_strstr($show_comment, '; InnoDB free') === false) {
375 if (mb_strstr($show_comment, 'InnoDB free') === false) {
376 // only user entered comment
377 $comment = $show_comment;
379 // here we have just InnoDB generated part
383 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
384 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
387 // PACK_KEYS: MyISAM or ISAM
388 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
389 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
391 // Here should be version check for InnoDB, however it is supported
392 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
396 $operations->getTableOptionDiv(
397 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
398 $create_options['pack_keys'],
400 (empty($create_options['delay_key_write']) ?
'0' : '1'),
401 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
402 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
403 (empty($create_options['checksum']) ?
'0' : '1')
410 $response->addHTML($operations->getHtmlForCopytable());
416 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
419 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
420 $truncate_table_url_params = array();
421 $drop_table_url_params = array();
424 && ! (isset($db_is_system_schema) && $db_is_system_schema)
426 $this_sql_query = 'TRUNCATE TABLE '
427 . Util
::backquote($GLOBALS['table']);
428 $truncate_table_url_params = array_merge(
431 'sql_query' => $this_sql_query,
432 'goto' => 'tbl_structure.php',
434 'message_to_show' => sprintf(
435 __('Table %s has been emptied.'),
436 htmlspecialchars($table)
441 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
442 $this_sql_query = 'DROP TABLE '
443 . Util
::backquote($GLOBALS['table']);
444 $drop_table_url_params = array_merge(
447 'sql_query' => $this_sql_query,
448 'goto' => 'db_operations.php',
451 'message_to_show' => sprintf(
453 ?
__('View %s has been dropped.')
454 : __('Table %s has been dropped.')
456 htmlspecialchars($table)
458 // table name is needed to avoid running
459 // PhpMyAdmin\RelationCleanup::database() on the whole db later
460 'table' => $GLOBALS['table'],
465 $operations->getHtmlForDeleteDataOrTable(
466 $truncate_table_url_params,
467 $drop_table_url_params
472 if (Partition
::havePartitioning()) {
473 $partition_names = Partition
::getPartitionNames($db, $table);
474 // show the Partition maintenance section only if we detect a partition
475 if (! is_null($partition_names[0])) {
477 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
481 unset($partition_names);
483 // Referential integrity check
485 if ($cfgRelation['relwork']) {
486 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
487 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
489 if (! empty($foreign)) {
491 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
493 } // end if ($foreign)
495 } // end if (!empty($cfg['Server']['relation']))