2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
8 use PMA\libraries\Partition
;
9 use PMA\libraries\Table
;
10 use PMA\libraries\Response
;
15 require_once 'libraries/common.inc.php';
18 * functions implementation for this script
20 require_once 'libraries/check_user_privileges.lib.php';
21 require_once 'libraries/operations.lib.php';
23 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
26 * Load JavaScript files
28 $response = Response
::getInstance();
29 $header = $response->getHeader();
30 $scripts = $header->getScripts();
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();
45 // reselect current db (needed in some cases probably due to
46 // the calling of relation.lib.php)
47 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
50 * Gets tables information
52 require 'libraries/tbl_info.inc.php';
54 // set initial value of these variables, based on the current table engine
55 if ($pma_table->isEngine('ARIA')) {
56 // the value for transactional can be implicit
57 // (no create option found, in this case it means 1)
58 // or explicit (option found with a value of 0 or 1)
59 // ($create_options['transactional'] may have been set by libraries/tbl_info.inc.php,
60 // from the $create_options)
61 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
64 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
68 $table_alters = array();
71 * If the table has to be moved to some other database
73 if (isset($_REQUEST['submit_move']) ||
isset($_REQUEST['submit_copy'])) {
75 PMA_moveOrCopyTable($db, $table);
76 // This was ended in an Ajax call
80 * If the table has to be maintained
82 if (isset($_REQUEST['table_maintenance'])) {
83 include_once 'sql.php';
87 * Updates table comment, type and options if required
89 if (isset($_REQUEST['submitoptions'])) {
91 $warning_messages = array();
93 if (isset($_REQUEST['new_name'])) {
94 // Get original names before rename operation
95 $oldTable = $pma_table->getName();
96 $oldDb = $pma_table->getDbName();
98 if ($pma_table->rename($_REQUEST['new_name'])) {
99 if (isset($_REQUEST['adjust_privileges'])
100 && ! empty($_REQUEST['adjust_privileges'])
102 PMA_AdjustPrivileges_renameOrMoveTable(
103 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
107 // Reselect the original DB
108 $GLOBALS['db'] = $oldDb;
109 $GLOBALS['dbi']->selectDb($oldDb);
111 $_message .= $pma_table->getLastMessage();
113 $GLOBALS['table'] = $pma_table->getName();
117 $_message .= $pma_table->getLastError();
122 if (! empty($_REQUEST['new_tbl_storage_engine'])
123 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
125 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
127 if ($pma_table->isEngine('ARIA')) {
128 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
131 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : '';
134 $new_tbl_storage_engine = '';
137 $row_format = (isset($create_options['row_format']))
138 ?
$create_options['row_format']
139 : $pma_table->getStatusInfo('ROW_FORMAT');
141 $table_alters = PMA_getTableAltersArray(
143 $create_options['pack_keys'],
144 (empty($create_options['checksum']) ?
'0' : '1'),
145 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
146 (empty($create_options['delay_key_write']) ?
'0' : '1'),
148 $new_tbl_storage_engine,
149 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
153 if (count($table_alters) > 0) {
154 $sql_query = 'ALTER TABLE '
155 . PMA\libraries\Util
::backquote($GLOBALS['table']);
156 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
158 $result .= $GLOBALS['dbi']->query($sql_query) ?
true : false;
160 unset($table_alters);
161 $warning_messages = PMA_getWarningMessagesArray();
164 if (isset($_REQUEST['tbl_collation'])
165 && ! empty($_REQUEST['tbl_collation'])
166 && isset($_REQUEST['change_all_collations'])
167 && ! empty($_REQUEST['change_all_collations'])
169 PMA_changeAllColumnsCollation(
170 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
175 * Reordering the table has been requested by the user
177 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
178 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
182 * A partition operation has been requested by the user
184 if (isset($_REQUEST['submit_partition'])
185 && ! empty($_REQUEST['partition_operation'])
187 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
191 // to avoid showing the old value (for example the AUTO_INCREMENT) after
192 // a change, clear the cache
193 $GLOBALS['dbi']->clearTableCache();
194 include 'libraries/tbl_info.inc.php';
198 if (isset($result) && empty($message_to_show)) {
199 if (empty($_message)) {
200 if (empty($sql_query)) {
201 $_message = PMA\libraries\Message
::success(__('No change'));
204 ? PMA\libraries\Message
::success()
205 : PMA\libraries\Message
::error();
208 if ($response->isAjax()) {
209 $response->setRequestStatus($_message->isSuccess());
210 $response->addJSON('message', $_message);
211 if (!empty($sql_query)) {
213 'sql_query', PMA\libraries\Util
::getMessage(null, $sql_query)
220 ? PMA\libraries\Message
::success($_message)
221 : PMA\libraries\Message
::error($_message);
224 if (! empty($warning_messages)) {
225 $_message = new PMA\libraries\Message
;
226 $_message->addMessagesString($warning_messages);
227 $_message->isError(true);
228 if ($response->isAjax()) {
229 $response->setRequestStatus(false);
230 $response->addJSON('message', $_message);
231 if (!empty($sql_query)) {
233 'sql_query', PMA\libraries\Util
::getMessage(null, $sql_query)
238 unset($warning_messages);
241 if (empty($sql_query)) {
243 $_message->getDisplay()
247 PMA\libraries\Util
::getMessage($_message, $sql_query)
254 = $url_params['back']
255 = 'tbl_operations.php';
260 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
265 $response->addHTML('<div id="boxContainer" data-box-width="300">');
270 $hideOrderTable = false;
271 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
272 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
273 // InnoDB always orders table rows according to such an index if one is present.
274 if ($tbl_storage_engine == 'INNODB') {
275 $indexes = PMA\libraries\Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
276 foreach ($indexes as $name => $idx) {
277 if ($name == 'PRIMARY') {
278 $hideOrderTable = true;
280 } elseif (! $idx->getNonUnique()) {
282 foreach ($idx->getColumns() as $column) {
283 if ($column->getNull()) {
289 $hideOrderTable = true;
295 if (! $hideOrderTable) {
296 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
302 $response->addHTML(PMA_getHtmlForMoveTable());
304 if (mb_strstr($show_comment, '; InnoDB free') === false) {
305 if (mb_strstr($show_comment, 'InnoDB free') === false) {
306 // only user entered comment
307 $comment = $show_comment;
309 // here we have just InnoDB generated part
313 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
314 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
317 // PACK_KEYS: MyISAM or ISAM
318 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
319 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
321 // Here should be version check for InnoDB, however it is supported
322 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
326 PMA_getTableOptionDiv(
327 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
328 $create_options['pack_keys'],
330 (empty($create_options['delay_key_write']) ?
'0' : '1'),
331 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
332 ((isset($create_options['page_checksum'])) ?
$create_options['page_checksum'] : ''),
333 (empty($create_options['checksum']) ?
'0' : '1')
340 $response->addHTML(PMA_getHtmlForCopytable());
346 PMA_getHtmlForTableMaintenance($pma_table, $url_params)
349 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
350 $truncate_table_url_params = array();
351 $drop_table_url_params = array();
354 && ! (isset($db_is_system_schema) && $db_is_system_schema)
356 $this_sql_query = 'TRUNCATE TABLE '
357 . PMA\libraries\Util
::backquote($GLOBALS['table']);
358 $truncate_table_url_params = array_merge(
361 'sql_query' => $this_sql_query,
362 'goto' => 'tbl_structure.php',
364 'message_to_show' => sprintf(
365 __('Table %s has been emptied.'),
366 htmlspecialchars($table)
371 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
372 $this_sql_query = 'DROP TABLE '
373 . PMA\libraries\Util
::backquote($GLOBALS['table']);
374 $drop_table_url_params = array_merge(
377 'sql_query' => $this_sql_query,
378 'goto' => 'db_operations.php',
381 'message_to_show' => sprintf(
383 ?
__('View %s has been dropped.')
384 : __('Table %s has been dropped.')
386 htmlspecialchars($table)
388 // table name is needed to avoid running
389 // PMA_relationsCleanupDatabase() on the whole db later
390 'table' => $GLOBALS['table'],
395 PMA_getHtmlForDeleteDataOrTable(
396 $truncate_table_url_params,
397 $drop_table_url_params
402 if (Partition
::havePartitioning()) {
403 $partition_names = Partition
::getPartitionNames($db, $table);
404 // show the Partition maintenance section only if we detect a partition
405 if (! is_null($partition_names[0])) {
407 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
411 unset($partition_names);
413 // Referential integrity check
414 // The Referential integrity check was intended for the non-InnoDB
415 // tables for which the relations are defined in pmadb
416 // so I assume that if the current table is InnoDB, I don't display
417 // this choice (InnoDB maintains integrity by itself)
419 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
420 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
421 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
423 if (! empty($foreign)) {
425 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
427 } // end if ($foreign)
429 } // end if (!empty($cfg['Server']['relation']))
431 $response->addHTML('</div>');