2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
8 declare(strict_types
=1);
10 use PhpMyAdmin\CheckUserPrivileges
;
12 use PhpMyAdmin\Message
;
13 use PhpMyAdmin\Partition
;
14 use PhpMyAdmin\Operations
;
15 use PhpMyAdmin\Relation
;
16 use PhpMyAdmin\Response
;
20 if (! defined('ROOT_PATH')) {
21 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
24 require_once ROOT_PATH
. 'libraries/common.inc.php';
26 $checkUserPrivileges = new CheckUserPrivileges($GLOBALS['dbi']);
27 $checkUserPrivileges->getPrivileges();
29 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
32 * Load JavaScript files
34 $response = Response
::getInstance();
35 $header = $response->getHeader();
36 $scripts = $header->getScripts();
37 $scripts->addFile('tbl_operations.js');
42 require ROOT_PATH
. 'libraries/tbl_common.inc.php';
43 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
44 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
47 * Gets relation settings
49 $relation = new Relation($GLOBALS['dbi']);
50 $operations = new Operations($GLOBALS['dbi'], $relation);
52 $cfgRelation = $relation->getRelationsParam();
54 // reselect current db (needed in some cases probably due to
55 // the calling of PhpMyAdmin\Relation)
56 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
59 * Gets tables information
61 $pma_table = $GLOBALS['dbi']->getTable(
65 $reread_info = $pma_table->getStatusInfo(null, false);
66 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ?
true : false));
67 if ($pma_table->isView()) {
69 $tbl_storage_engine = __('View');
73 $tbl_storage_engine = $pma_table->getStorageEngine();
74 $show_comment = $pma_table->getComment();
76 $tbl_collation = $pma_table->getCollation();
77 $table_info_num_rows = $pma_table->getNumRows();
78 $row_format = $pma_table->getRowFormat();
79 $auto_increment = $pma_table->getAutoIncrement();
80 $create_options = $pma_table->getCreateOptions();
82 // set initial value of these variables, based on the current table engine
83 if ($pma_table->isEngine('ARIA')) {
84 // the value for transactional can be implicit
85 // (no create option found, in this case it means 1)
86 // or explicit (option found with a value of 0 or 1)
87 // ($create_options['transactional'] may have been set by Table class,
88 // from the $create_options)
89 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
92 $create_options['page_checksum'] = isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : '';
95 $pma_table = $GLOBALS['dbi']->getTable(
103 * If the table has to be moved to some other database
105 if (isset($_POST['submit_move']) ||
isset($_POST['submit_copy'])) {
107 $operations->moveOrCopyTable($db, $table);
108 // This was ended in an Ajax call
112 * If the table has to be maintained
114 if (isset($_POST['table_maintenance'])) {
115 include_once ROOT_PATH
. 'sql.php';
119 * Updates table comment, type and options if required
121 if (isset($_POST['submitoptions'])) {
123 $warning_messages = [];
125 if (isset($_POST['new_name'])) {
126 // Get original names before rename operation
127 $oldTable = $pma_table->getName();
128 $oldDb = $pma_table->getDbName();
130 if ($pma_table->rename($_POST['new_name'])) {
131 if (isset($_POST['adjust_privileges'])
132 && ! empty($_POST['adjust_privileges'])
134 $operations->adjustPrivilegesRenameOrMoveTable(
142 // Reselect the original DB
143 $GLOBALS['db'] = $oldDb;
144 $GLOBALS['dbi']->selectDb($oldDb);
145 $_message .= $pma_table->getLastMessage();
147 $GLOBALS['table'] = $pma_table->getName();
151 $_message .= $pma_table->getLastError();
156 if (! empty($_POST['new_tbl_storage_engine'])
157 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
159 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
161 if ($pma_table->isEngine('ARIA')) {
162 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
165 $create_options['page_checksum'] = isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : '';
168 $new_tbl_storage_engine = '';
171 $row_format = isset($create_options['row_format'])
172 ?
$create_options['row_format']
173 : $pma_table->getRowFormat();
175 $table_alters = $operations->getTableAltersArray(
177 $create_options['pack_keys'],
178 (empty($create_options['checksum']) ?
'0' : '1'),
179 (isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : ''),
180 (empty($create_options['delay_key_write']) ?
'0' : '1'),
182 $new_tbl_storage_engine,
183 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
187 if (count($table_alters) > 0) {
188 $sql_query = 'ALTER TABLE '
189 . Util
::backquote($GLOBALS['table']);
190 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
192 $result = $GLOBALS['dbi']->query($sql_query) ?
true : false;
194 unset($table_alters);
195 $warning_messages = $operations->getWarningMessagesArray();
198 if (isset($_POST['tbl_collation'])
199 && ! empty($_POST['tbl_collation'])
200 && isset($_POST['change_all_collations'])
201 && ! empty($_POST['change_all_collations'])
203 $operations->changeAllColumnsCollation(
206 $_POST['tbl_collation']
211 * Reordering the table has been requested by the user
213 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
214 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
218 * A partition operation has been requested by the user
220 if (isset($_POST['submit_partition'])
221 && ! empty($_POST['partition_operation'])
223 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
227 // to avoid showing the old value (for example the AUTO_INCREMENT) after
228 // a change, clear the cache
229 $GLOBALS['dbi']->clearTableCache();
230 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
231 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
232 if ($pma_table->isView()) {
234 $tbl_storage_engine = __('View');
235 $show_comment = null;
237 $tbl_is_view = false;
238 $tbl_storage_engine = $pma_table->getStorageEngine();
239 $show_comment = $pma_table->getComment();
241 $tbl_collation = $pma_table->getCollation();
242 $table_info_num_rows = $pma_table->getNumRows();
243 $row_format = $pma_table->getRowFormat();
244 $auto_increment = $pma_table->getAutoIncrement();
245 $create_options = $pma_table->getCreateOptions();
249 if (isset($result) && empty($message_to_show)) {
250 if (empty($_message)) {
251 if (empty($sql_query)) {
252 $_message = Message
::success(__('No change'));
259 if ($response->isAjax()) {
260 $response->setRequestStatus($_message->isSuccess());
261 $response->addJSON('message', $_message);
262 if (! empty($sql_query)) {
265 Util
::getMessage(null, $sql_query)
272 ? Message
::success($_message)
273 : Message
::error($_message);
276 if (! empty($warning_messages)) {
277 $_message = new Message();
278 $_message->addMessagesString($warning_messages);
279 $_message->isError(true);
280 if ($response->isAjax()) {
281 $response->setRequestStatus(false);
282 $response->addJSON('message', $_message);
283 if (! empty($sql_query)) {
286 Util
::getMessage(null, $sql_query)
291 unset($warning_messages);
294 if (empty($sql_query)) {
296 $_message->getDisplay()
300 Util
::getMessage($_message, $sql_query)
307 = $url_params['back']
308 = 'tbl_operations.php';
313 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
322 $hideOrderTable = false;
323 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
324 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
325 // InnoDB always orders table rows according to such an index if one is present.
326 if ($tbl_storage_engine == 'INNODB') {
327 $indexes = Index
::getFromTable($GLOBALS['table'], $GLOBALS['db']);
328 foreach ($indexes as $name => $idx) {
329 if ($name == 'PRIMARY') {
330 $hideOrderTable = true;
332 } elseif (! $idx->getNonUnique()) {
334 foreach ($idx->getColumns() as $column) {
335 if ($column->getNull()) {
341 $hideOrderTable = true;
347 if (! $hideOrderTable) {
348 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
354 $response->addHTML($operations->getHtmlForMoveTable());
356 if (mb_strstr($show_comment, '; InnoDB free') === false) {
357 if (mb_strstr($show_comment, 'InnoDB free') === false) {
358 // only user entered comment
359 $comment = $show_comment;
361 // here we have just InnoDB generated part
365 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
366 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
369 // PACK_KEYS: MyISAM or ISAM
370 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
371 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
373 // Here should be version check for InnoDB, however it is supported
374 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
378 $operations->getTableOptionDiv(
383 $create_options['pack_keys'],
385 (empty($create_options['delay_key_write']) ?
'0' : '1'),
386 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
387 (isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : ''),
388 (empty($create_options['checksum']) ?
'0' : '1')
395 $response->addHTML($operations->getHtmlForCopytable());
401 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
404 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
405 $truncate_table_url_params = [];
406 $drop_table_url_params = [];
409 && ! (isset($db_is_system_schema) && $db_is_system_schema)
411 $this_sql_query = 'TRUNCATE TABLE '
412 . Util
::backquote($GLOBALS['table']);
413 $truncate_table_url_params = array_merge(
416 'sql_query' => $this_sql_query,
417 'goto' => 'tbl_structure.php',
419 'message_to_show' => sprintf(
420 __('Table %s has been emptied.'),
421 htmlspecialchars($table)
426 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
427 $this_sql_query = 'DROP TABLE '
428 . Util
::backquote($GLOBALS['table']);
429 $drop_table_url_params = array_merge(
432 'sql_query' => $this_sql_query,
433 'goto' => 'db_operations.php',
436 'message_to_show' => sprintf(
438 ?
__('View %s has been dropped.')
439 : __('Table %s has been dropped.')
441 htmlspecialchars($table)
443 // table name is needed to avoid running
444 // PhpMyAdmin\RelationCleanup::database() on the whole db later
445 'table' => $GLOBALS['table'],
450 $operations->getHtmlForDeleteDataOrTable(
451 $truncate_table_url_params,
452 $drop_table_url_params
457 if (Partition
::havePartitioning()) {
458 $partition_names = Partition
::getPartitionNames($db, $table);
459 // show the Partition maintenance section only if we detect a partition
460 if (! is_null($partition_names[0])) {
462 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
466 unset($partition_names);
468 // Referential integrity check
469 // The Referential integrity check was intended for the non-InnoDB
470 // tables for which the relations are defined in pmadb
471 // so I assume that if the current table is InnoDB, I don't display
472 // this choice (InnoDB maintains integrity by itself)
474 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
475 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
476 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
478 if (! empty($foreign)) {
480 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
482 } // end if ($foreign)
483 } // end if (!empty($cfg['Server']['relation']))