2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
8 use PMA\libraries\Partition
;
9 use PMA\libraries\Table
;
14 require_once 'libraries/common.inc.php';
17 * functions implementation for this script
19 require_once 'libraries/check_user_privileges.lib.php';
20 require_once 'libraries/operations.lib.php';
22 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
25 * Load JavaScript files
27 $response = PMA\libraries\Response
::getInstance();
28 $header = $response->getHeader();
29 $scripts = $header->getScripts();
30 $scripts->addFile('functions.js');
31 $scripts->addFile('tbl_operations.js');
36 require 'libraries/tbl_common.inc.php';
37 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
38 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
41 * Gets relation settings
43 $cfgRelation = PMA_getRelationsParam();
46 * Gets available MySQL charsets and storage engines
48 require_once 'libraries/mysql_charsets.inc.php';
50 // reselect current db (needed in some cases probably due to
51 // the calling of relation.lib.php)
52 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
55 * Gets tables information
57 require 'libraries/tbl_info.inc.php';
59 // set initial value of these variables, based on the current table engine
60 list($is_myisam_or_aria, $is_innodb, $is_isam,
61 $is_berkeleydb, $is_aria, $is_pbxt
62 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
65 // the value for transactional can be implicit
66 // (no create option found, in this case it means 1)
67 // or explicit (option found with a value of 0 or 1)
68 // ($transactional may have been set by libraries/tbl_info.inc.php,
69 // from the $create_options)
70 $transactional = (isset($transactional) && $transactional == '0')
73 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
77 $table_alters = array();
80 * If the table has to be moved to some other database
82 if (isset($_REQUEST['submit_move']) ||
isset($_REQUEST['submit_copy'])) {
84 PMA_moveOrCopyTable($db, $table);
85 // This was ended in an Ajax call
89 * If the table has to be maintained
91 if (isset($_REQUEST['table_maintenance'])) {
92 include_once 'sql.php';
96 * Updates table comment, type and options if required
98 if (isset($_REQUEST['submitoptions'])) {
100 $warning_messages = array();
102 if (isset($_REQUEST['new_name'])) {
103 // Get original names before rename operation
104 $oldTable = $pma_table->getName();
105 $oldDb = $pma_table->getDbName();
107 if ($pma_table->rename($_REQUEST['new_name'])) {
108 if (isset($_REQUEST['adjust_privileges'])
109 && ! empty($_REQUEST['adjust_privileges'])
111 PMA_AdjustPrivileges_renameOrMoveTable(
112 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
116 // Reselect the original DB
117 $GLOBALS['db'] = $oldDb;
118 $GLOBALS['dbi']->selectDb($oldDb);
120 $_message .= $pma_table->getLastMessage();
122 $GLOBALS['table'] = $pma_table->getName();
126 $_message .= $pma_table->getLastError();
131 if (! empty($_REQUEST['new_tbl_storage_engine'])
132 && mb_strtolower($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
134 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
135 // reset the globals for the new engine
136 list($is_myisam_or_aria, $is_innodb, $is_isam,
137 $is_berkeleydb, $is_aria, $is_pbxt
138 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
141 $transactional = (isset($transactional) && $transactional == '0')
144 $page_checksum = (isset($page_checksum)) ?
$page_checksum : '';
147 $new_tbl_storage_engine = '';
150 $table_alters = PMA_getTableAltersArray(
151 $is_myisam_or_aria, $is_isam, $pack_keys,
152 (empty($checksum) ?
'0' : '1'),
154 ((isset($page_checksum)) ?
$page_checksum : ''),
155 (empty($delay_key_write) ?
'0' : '1'),
156 $is_innodb, $is_pbxt, $row_format,
157 $new_tbl_storage_engine,
158 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
162 if (count($table_alters) > 0) {
163 $sql_query = 'ALTER TABLE '
164 . PMA\libraries\Util
::backquote($GLOBALS['table']);
165 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
167 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
169 unset($table_alters);
170 $warning_messages = PMA_getWarningMessagesArray();
173 if (isset($_REQUEST['tbl_collation'])
174 && ! empty($_REQUEST['tbl_collation'])
175 && isset($_REQUEST['change_all_collations'])
176 && ! empty($_REQUEST['change_all_collations'])
178 PMA_changeAllColumnsCollation(
179 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
184 * Reordering the table has been requested by the user
186 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
187 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
191 * A partition operation has been requested by the user
193 if (isset($_REQUEST['submit_partition'])
194 && ! empty($_REQUEST['partition_operation'])
196 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
200 // to avoid showing the old value (for example the AUTO_INCREMENT) after
201 // a change, clear the cache
202 $GLOBALS['dbi']->clearTableCache();
203 $page_checksum = $checksum = $delay_key_write = 0;
204 include 'libraries/tbl_info.inc.php';
208 if (isset($result) && empty($message_to_show)) {
209 if (empty($_message)) {
210 if (empty($sql_query)) {
211 $_message = PMA\libraries\Message
::success(__('No change'));
214 ? PMA\libraries\Message
::success()
215 : PMA\libraries\Message
::error();
218 if (isset($GLOBALS['ajax_request'])
219 && $GLOBALS['ajax_request'] == true
221 $response = PMA\libraries\Response
::getInstance();
222 $response->setRequestStatus($_message->isSuccess());
223 $response->addJSON('message', $_message);
224 if (!empty($sql_query)) {
226 'sql_query', PMA\libraries\Util
::getMessage(null, $sql_query)
232 if (! empty($warning_messages)) {
233 $_message = new PMA\libraries\Message
;
234 $_message->addMessages($warning_messages);
235 $_message->isError(true);
236 if (isset($GLOBALS['ajax_request'])
237 && $GLOBALS['ajax_request'] == true
239 $response = PMA\libraries\Response
::getInstance();
240 $response->setRequestStatus(false);
241 $response->addJSON('message', $_message);
242 if (!empty($sql_query)) {
244 'sql_query', PMA\libraries\Util
::getMessage(null, $sql_query)
249 unset($warning_messages);
252 if (empty($sql_query)) {
254 $_message->getDisplay()
258 PMA\libraries\Util
::getMessage($_message, $sql_query)
265 = $url_params['back']
266 = 'tbl_operations.php';
271 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
276 $response->addHTML('<div id="boxContainer" data-box-width="300">');
281 $hideOrderTable = false;
282 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
283 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
284 // InnoDB always orders table rows according to such an index if one is present.
285 if ($tbl_storage_engine == 'INNODB') {
286 $indexes = PMA\libraries\Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
287 foreach ($indexes as $name => $idx) {
288 if ($name == 'PRIMARY') {
289 $hideOrderTable = true;
291 } elseif (! $idx->getNonUnique()) {
293 foreach ($idx->getColumns() as $column) {
294 if ($column->getNull()) {
300 $hideOrderTable = true;
306 if (! $hideOrderTable) {
307 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
313 $response->addHTML(PMA_getHtmlForMoveTable());
315 if (mb_strstr($show_comment, '; InnoDB free') === false) {
316 if (mb_strstr($show_comment, 'InnoDB free') === false) {
317 // only user entered comment
318 $comment = $show_comment;
320 // here we have just InnoDB generated part
324 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
325 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
328 // PACK_KEYS: MyISAM or ISAM
329 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
330 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
332 // Here should be version check for InnoDB, however it is supported
333 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
337 PMA_getTableOptionDiv(
338 $comment, $tbl_collation, $tbl_storage_engine,
339 $is_myisam_or_aria, $is_isam, $pack_keys,
341 (empty($delay_key_write) ?
'0' : '1'),
342 ((isset($transactional) && $transactional == '0') ?
'0' : '1'),
343 ((isset($page_checksum)) ?
$page_checksum : ''),
344 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ?
'0' : '1')
351 $response->addHTML(PMA_getHtmlForCopytable());
357 PMA_getHtmlForTableMaintenance(
365 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
366 $truncate_table_url_params = array();
367 $drop_table_url_params = array();
370 && ! (isset($db_is_system_schema) && $db_is_system_schema)
372 $this_sql_query = 'TRUNCATE TABLE '
373 . PMA\libraries\Util
::backquote($GLOBALS['table']);
374 $truncate_table_url_params = array_merge(
377 'sql_query' => $this_sql_query,
378 'goto' => 'tbl_structure.php',
380 'message_to_show' => sprintf(
381 __('Table %s has been emptied.'),
382 htmlspecialchars($table)
387 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
388 $this_sql_query = 'DROP TABLE '
389 . PMA\libraries\Util
::backquote($GLOBALS['table']);
390 $drop_table_url_params = array_merge(
393 'sql_query' => $this_sql_query,
394 'goto' => 'db_operations.php',
397 'message_to_show' => sprintf(
399 ?
__('View %s has been dropped.')
400 : __('Table %s has been dropped.')
402 htmlspecialchars($table)
404 // table name is needed to avoid running
405 // PMA_relationsCleanupDatabase() on the whole db later
406 'table' => $GLOBALS['table'],
411 PMA_getHtmlForDeleteDataOrTable(
412 $truncate_table_url_params,
413 $drop_table_url_params
418 if (Partition
::havePartitioning()) {
419 $partition_names = Partition
::getPartitionNames($db, $table);
420 // show the Partition maintenance section only if we detect a partition
421 if (! is_null($partition_names[0])) {
423 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
427 unset($partition_names);
429 // Referential integrity check
430 // The Referential integrity check was intended for the non-InnoDB
431 // tables for which the relations are defined in pmadb
432 // so I assume that if the current table is InnoDB, I don't display
433 // this choice (InnoDB maintains integrity by itself)
435 if ($cfgRelation['relwork'] && ! $is_innodb) {
436 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
437 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
439 if (! empty($foreign)) {
441 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
443 } // end if ($foreign)
445 } // end if (!empty($cfg['Server']['relation']))
447 $response->addHTML('</div>');