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 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
30 * Load JavaScript files
32 $response = Response
::getInstance();
33 $header = $response->getHeader();
34 $scripts = $header->getScripts();
35 $scripts->addFile('tbl_operations.js');
40 require 'libraries/tbl_common.inc.php';
41 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
42 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
45 * Gets relation settings
47 $relation = new Relation();
48 $cfgRelation = $relation->getRelationsParam();
50 // reselect current db (needed in some cases probably due to
51 // the calling of PhpMyAdmin\Relation)
52 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
55 * Gets tables information
57 $pma_table = $GLOBALS['dbi']->getTable(
61 $reread_info = $pma_table->getStatusInfo(null, false);
62 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ?
true : false));
63 if ($pma_table->isView()) {
65 $tbl_storage_engine = __('View');
69 $tbl_storage_engine = $pma_table->getStorageEngine();
70 $show_comment = $pma_table->getComment();
72 $tbl_collation = $pma_table->getCollation();
73 $table_info_num_rows = $pma_table->getNumRows();
74 $row_format = $pma_table->getRowFormat();
75 $auto_increment = $pma_table->getAutoIncrement();
76 $create_options = $pma_table->getCreateOptions();
78 // set initial value of these variables, based on the current table engine
79 if ($pma_table->isEngine('ARIA')) {
80 // the value for transactional can be implicit
81 // (no create option found, in this case it means 1)
82 // or explicit (option found with a value of 0 or 1)
83 // ($create_options['transactional'] may have been set by Table class,
84 // from the $create_options)
85 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
88 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
91 $pma_table = $GLOBALS['dbi']->getTable(
96 $table_alters = array();
98 $operations = new Operations();
101 * If the table has to be moved to some other database
103 if (isset($_POST['submit_move']) ||
isset($_POST['submit_copy'])) {
105 $operations->moveOrCopyTable($db, $table);
106 // This was ended in an Ajax call
110 * If the table has to be maintained
112 if (isset($_POST['table_maintenance'])) {
113 include_once 'sql.php';
117 * Updates table comment, type and options if required
119 if (isset($_POST['submitoptions'])) {
121 $warning_messages = array();
123 if (isset($_POST['new_name'])) {
124 // Get original names before rename operation
125 $oldTable = $pma_table->getName();
126 $oldDb = $pma_table->getDbName();
128 if ($pma_table->rename($_POST['new_name'])) {
129 if (isset($_POST['adjust_privileges'])
130 && ! empty($_POST['adjust_privileges'])
132 $operations->adjustPrivilegesRenameOrMoveTable(
133 $oldDb, $oldTable, $_POST['db'], $_POST['new_name']
137 // Reselect the original DB
138 $GLOBALS['db'] = $oldDb;
139 $GLOBALS['dbi']->selectDb($oldDb);
140 $_message .= $pma_table->getLastMessage();
142 $GLOBALS['table'] = $pma_table->getName();
146 $_message .= $pma_table->getLastError();
151 if (! empty($_POST['new_tbl_storage_engine'])
152 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
154 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
156 if ($pma_table->isEngine('ARIA')) {
157 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
160 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
163 $new_tbl_storage_engine = '';
166 $row_format = (isset($create_options['row_format']))
167 ?
$create_options['row_format']
168 : $pma_table->getRowFormat();
170 $table_alters = $operations->getTableAltersArray(
172 $create_options['pack_keys'],
173 (empty($create_options['checksum']) ?
'0' : '1'),
174 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
175 (empty($create_options['delay_key_write']) ?
'0' : '1'),
177 $new_tbl_storage_engine,
178 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
182 if (count($table_alters) > 0) {
183 $sql_query = 'ALTER TABLE '
184 . Util
::backquote($GLOBALS['table']);
185 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
187 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
189 unset($table_alters);
190 $warning_messages = $operations->getWarningMessagesArray();
193 if (isset($_POST['tbl_collation'])
194 && ! empty($_POST['tbl_collation'])
195 && isset($_POST['change_all_collations'])
196 && ! empty($_POST['change_all_collations'])
198 $operations->changeAllColumnsCollation(
199 $GLOBALS['db'], $GLOBALS['table'], $_POST['tbl_collation']
204 * Reordering the table has been requested by the user
206 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
207 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
211 * A partition operation has been requested by the user
213 if (isset($_POST['submit_partition'])
214 && ! empty($_POST['partition_operation'])
216 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
220 // to avoid showing the old value (for example the AUTO_INCREMENT) after
221 // a change, clear the cache
222 $GLOBALS['dbi']->clearTableCache();
223 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
224 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
225 if ($pma_table->isView()) {
227 $tbl_storage_engine = __('View');
228 $show_comment = null;
230 $tbl_is_view = false;
231 $tbl_storage_engine = $pma_table->getStorageEngine();
232 $show_comment = $pma_table->getComment();
234 $tbl_collation = $pma_table->getCollation();
235 $table_info_num_rows = $pma_table->getNumRows();
236 $row_format = $pma_table->getRowFormat();
237 $auto_increment = $pma_table->getAutoIncrement();
238 $create_options = $pma_table->getCreateOptions();
242 if (isset($result) && empty($message_to_show)) {
243 if (empty($_message)) {
244 if (empty($sql_query)) {
245 $_message = Message
::success(__('No change'));
252 if ($response->isAjax()) {
253 $response->setRequestStatus($_message->isSuccess());
254 $response->addJSON('message', $_message);
255 if (!empty($sql_query)) {
257 'sql_query', Util
::getMessage(null, $sql_query)
264 ? Message
::success($_message)
265 : Message
::error($_message);
268 if (! empty($warning_messages)) {
269 $_message = new Message
;
270 $_message->addMessagesString($warning_messages);
271 $_message->isError(true);
272 if ($response->isAjax()) {
273 $response->setRequestStatus(false);
274 $response->addJSON('message', $_message);
275 if (!empty($sql_query)) {
277 'sql_query', Util
::getMessage(null, $sql_query)
282 unset($warning_messages);
285 if (empty($sql_query)) {
287 $_message->getDisplay()
291 Util
::getMessage($_message, $sql_query)
298 = $url_params['back']
299 = 'tbl_operations.php';
304 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
313 $hideOrderTable = false;
314 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
315 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
316 // InnoDB always orders table rows according to such an index if one is present.
317 if ($tbl_storage_engine == 'INNODB') {
318 $indexes = Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
319 foreach ($indexes as $name => $idx) {
320 if ($name == 'PRIMARY') {
321 $hideOrderTable = true;
323 } elseif (! $idx->getNonUnique()) {
325 foreach ($idx->getColumns() as $column) {
326 if ($column->getNull()) {
332 $hideOrderTable = true;
338 if (! $hideOrderTable) {
339 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
345 $response->addHTML($operations->getHtmlForMoveTable());
347 if (mb_strstr($show_comment, '; InnoDB free') === false) {
348 if (mb_strstr($show_comment, 'InnoDB free') === false) {
349 // only user entered comment
350 $comment = $show_comment;
352 // here we have just InnoDB generated part
356 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
357 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
360 // PACK_KEYS: MyISAM or ISAM
361 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
362 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
364 // Here should be version check for InnoDB, however it is supported
365 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
369 $operations->getTableOptionDiv(
370 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
371 $create_options['pack_keys'],
373 (empty($create_options['delay_key_write']) ?
'0' : '1'),
374 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
375 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
376 (empty($create_options['checksum']) ?
'0' : '1')
383 $response->addHTML($operations->getHtmlForCopytable());
389 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
392 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
393 $truncate_table_url_params = array();
394 $drop_table_url_params = array();
397 && ! (isset($db_is_system_schema) && $db_is_system_schema)
399 $this_sql_query = 'TRUNCATE TABLE '
400 . Util
::backquote($GLOBALS['table']);
401 $truncate_table_url_params = array_merge(
404 'sql_query' => $this_sql_query,
405 'goto' => 'tbl_structure.php',
407 'message_to_show' => sprintf(
408 __('Table %s has been emptied.'),
409 htmlspecialchars($table)
414 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
415 $this_sql_query = 'DROP TABLE '
416 . Util
::backquote($GLOBALS['table']);
417 $drop_table_url_params = array_merge(
420 'sql_query' => $this_sql_query,
421 'goto' => 'db_operations.php',
424 'message_to_show' => sprintf(
426 ?
__('View %s has been dropped.')
427 : __('Table %s has been dropped.')
429 htmlspecialchars($table)
431 // table name is needed to avoid running
432 // PhpMyAdmin\RelationCleanup::database() on the whole db later
433 'table' => $GLOBALS['table'],
438 $operations->getHtmlForDeleteDataOrTable(
439 $truncate_table_url_params,
440 $drop_table_url_params
445 if (Partition
::havePartitioning()) {
446 $partition_names = Partition
::getPartitionNames($db, $table);
447 // show the Partition maintenance section only if we detect a partition
448 if (! is_null($partition_names[0])) {
450 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
454 unset($partition_names);
456 // Referential integrity check
457 // The Referential integrity check was intended for the non-InnoDB
458 // tables for which the relations are defined in pmadb
459 // so I assume that if the current table is InnoDB, I don't display
460 // this choice (InnoDB maintains integrity by itself)
462 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
463 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
464 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
466 if (! empty($foreign)) {
468 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
470 } // end if ($foreign)
472 } // end if (!empty($cfg['Server']['relation']))