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.lib.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 $cfgRelation = Relation
::getRelationsParam();
49 // reselect current db (needed in some cases probably due to
50 // the calling of PhpMyAdmin\Relation)
51 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
54 * Gets tables information
56 $pma_table = $GLOBALS['dbi']->getTable(
60 $reread_info = $pma_table->getStatusInfo(null, false);
61 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ?
true : false));
62 if ($pma_table->isView()) {
64 $tbl_storage_engine = __('View');
68 $tbl_storage_engine = $pma_table->getStorageEngine();
69 $show_comment = $pma_table->getComment();
71 $tbl_collation = $pma_table->getCollation();
72 $table_info_num_rows = $pma_table->getNumRows();
73 $row_format = $pma_table->getRowFormat();
74 $auto_increment = $pma_table->getAutoIncrement();
75 $create_options = $pma_table->getCreateOptions();
77 // set initial value of these variables, based on the current table engine
78 if ($pma_table->isEngine('ARIA')) {
79 // the value for transactional can be implicit
80 // (no create option found, in this case it means 1)
81 // or explicit (option found with a value of 0 or 1)
82 // ($create_options['transactional'] may have been set by Table class,
83 // from the $create_options)
84 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
87 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
90 $pma_table = $GLOBALS['dbi']->getTable(
95 $table_alters = array();
98 * If the table has to be moved to some other database
100 if (isset($_REQUEST['submit_move']) ||
isset($_REQUEST['submit_copy'])) {
102 Operations
::moveOrCopyTable($db, $table);
103 // This was ended in an Ajax call
107 * If the table has to be maintained
109 if (isset($_REQUEST['table_maintenance'])) {
110 include_once 'sql.php';
114 * Updates table comment, type and options if required
116 if (isset($_REQUEST['submitoptions'])) {
118 $warning_messages = array();
120 if (isset($_REQUEST['new_name'])) {
121 // Get original names before rename operation
122 $oldTable = $pma_table->getName();
123 $oldDb = $pma_table->getDbName();
125 if ($pma_table->rename($_REQUEST['new_name'])) {
126 if (isset($_REQUEST['adjust_privileges'])
127 && ! empty($_REQUEST['adjust_privileges'])
129 Operations
::adjustPrivilegesRenameOrMoveTable(
130 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
134 // Reselect the original DB
135 $GLOBALS['db'] = $oldDb;
136 $GLOBALS['dbi']->selectDb($oldDb);
137 $_message .= $pma_table->getLastMessage();
139 $GLOBALS['table'] = $pma_table->getName();
143 $_message .= $pma_table->getLastError();
148 if (! empty($_REQUEST['new_tbl_storage_engine'])
149 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
151 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
153 if ($pma_table->isEngine('ARIA')) {
154 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
157 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
160 $new_tbl_storage_engine = '';
163 $row_format = (isset($create_options['row_format']))
164 ?
$create_options['row_format']
165 : $pma_table->getRowFormat();
167 $table_alters = Operations
::getTableAltersArray(
169 $create_options['pack_keys'],
170 (empty($create_options['checksum']) ?
'0' : '1'),
171 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
172 (empty($create_options['delay_key_write']) ?
'0' : '1'),
174 $new_tbl_storage_engine,
175 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
179 if (count($table_alters) > 0) {
180 $sql_query = 'ALTER TABLE '
181 . Util
::backquote($GLOBALS['table']);
182 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
184 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
186 unset($table_alters);
187 $warning_messages = Operations
::getWarningMessagesArray();
190 if (isset($_REQUEST['tbl_collation'])
191 && ! empty($_REQUEST['tbl_collation'])
192 && isset($_REQUEST['change_all_collations'])
193 && ! empty($_REQUEST['change_all_collations'])
195 Operations
::changeAllColumnsCollation(
196 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
201 * Reordering the table has been requested by the user
203 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
204 list($sql_query, $result) = Operations
::getQueryAndResultForReorderingTable();
208 * A partition operation has been requested by the user
210 if (isset($_REQUEST['submit_partition'])
211 && ! empty($_REQUEST['partition_operation'])
213 list($sql_query, $result) = Operations
::getQueryAndResultForPartition();
217 // to avoid showing the old value (for example the AUTO_INCREMENT) after
218 // a change, clear the cache
219 $GLOBALS['dbi']->clearTableCache();
220 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
221 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
222 if ($pma_table->isView()) {
224 $tbl_storage_engine = __('View');
225 $show_comment = null;
227 $tbl_is_view = false;
228 $tbl_storage_engine = $pma_table->getStorageEngine();
229 $show_comment = $pma_table->getComment();
231 $tbl_collation = $pma_table->getCollation();
232 $table_info_num_rows = $pma_table->getNumRows();
233 $row_format = $pma_table->getRowFormat();
234 $auto_increment = $pma_table->getAutoIncrement();
235 $create_options = $pma_table->getCreateOptions();
239 if (isset($result) && empty($message_to_show)) {
240 if (empty($_message)) {
241 if (empty($sql_query)) {
242 $_message = Message
::success(__('No change'));
249 if ($response->isAjax()) {
250 $response->setRequestStatus($_message->isSuccess());
251 $response->addJSON('message', $_message);
252 if (!empty($sql_query)) {
254 'sql_query', Util
::getMessage(null, $sql_query)
261 ? Message
::success($_message)
262 : Message
::error($_message);
265 if (! empty($warning_messages)) {
266 $_message = new Message
;
267 $_message->addMessagesString($warning_messages);
268 $_message->isError(true);
269 if ($response->isAjax()) {
270 $response->setRequestStatus(false);
271 $response->addJSON('message', $_message);
272 if (!empty($sql_query)) {
274 'sql_query', Util
::getMessage(null, $sql_query)
279 unset($warning_messages);
282 if (empty($sql_query)) {
284 $_message->getDisplay()
288 Util
::getMessage($_message, $sql_query)
295 = $url_params['back']
296 = 'tbl_operations.php';
301 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
310 $hideOrderTable = false;
311 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
312 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
313 // InnoDB always orders table rows according to such an index if one is present.
314 if ($tbl_storage_engine == 'INNODB') {
315 $indexes = Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
316 foreach ($indexes as $name => $idx) {
317 if ($name == 'PRIMARY') {
318 $hideOrderTable = true;
320 } elseif (! $idx->getNonUnique()) {
322 foreach ($idx->getColumns() as $column) {
323 if ($column->getNull()) {
329 $hideOrderTable = true;
335 if (! $hideOrderTable) {
336 $response->addHTML(Operations
::getHtmlForOrderTheTable($columns));
342 $response->addHTML(Operations
::getHtmlForMoveTable());
344 if (mb_strstr($show_comment, '; InnoDB free') === false) {
345 if (mb_strstr($show_comment, 'InnoDB free') === false) {
346 // only user entered comment
347 $comment = $show_comment;
349 // here we have just InnoDB generated part
353 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
354 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
357 // PACK_KEYS: MyISAM or ISAM
358 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
359 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
361 // Here should be version check for InnoDB, however it is supported
362 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
366 Operations
::getTableOptionDiv(
367 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
368 $create_options['pack_keys'],
370 (empty($create_options['delay_key_write']) ?
'0' : '1'),
371 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
372 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
373 (empty($create_options['checksum']) ?
'0' : '1')
380 $response->addHTML(Operations
::getHtmlForCopytable());
386 Operations
::getHtmlForTableMaintenance($pma_table, $url_params)
389 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
390 $truncate_table_url_params = array();
391 $drop_table_url_params = array();
394 && ! (isset($db_is_system_schema) && $db_is_system_schema)
396 $this_sql_query = 'TRUNCATE TABLE '
397 . Util
::backquote($GLOBALS['table']);
398 $truncate_table_url_params = array_merge(
401 'sql_query' => $this_sql_query,
402 'goto' => 'tbl_structure.php',
404 'message_to_show' => sprintf(
405 __('Table %s has been emptied.'),
406 htmlspecialchars($table)
411 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
412 $this_sql_query = 'DROP TABLE '
413 . Util
::backquote($GLOBALS['table']);
414 $drop_table_url_params = array_merge(
417 'sql_query' => $this_sql_query,
418 'goto' => 'db_operations.php',
421 'message_to_show' => sprintf(
423 ?
__('View %s has been dropped.')
424 : __('Table %s has been dropped.')
426 htmlspecialchars($table)
428 // table name is needed to avoid running
429 // PMA_relationsCleanupDatabase() on the whole db later
430 'table' => $GLOBALS['table'],
435 Operations
::getHtmlForDeleteDataOrTable(
436 $truncate_table_url_params,
437 $drop_table_url_params
442 if (Partition
::havePartitioning()) {
443 $partition_names = Partition
::getPartitionNames($db, $table);
444 // show the Partition maintenance section only if we detect a partition
445 if (! is_null($partition_names[0])) {
447 Operations
::getHtmlForPartitionMaintenance($partition_names, $url_params)
451 unset($partition_names);
453 // Referential integrity check
454 // The Referential integrity check was intended for the non-InnoDB
455 // tables for which the relations are defined in pmadb
456 // so I assume that if the current table is InnoDB, I don't display
457 // this choice (InnoDB maintains integrity by itself)
459 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
460 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
461 $foreign = Relation
::getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
463 if (! empty($foreign)) {
465 Operations
::getHtmlForReferentialIntegrityCheck($foreign, $url_params)
467 } // end if ($foreign)
469 } // end if (!empty($cfg['Server']['relation']))