2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Various table operations
8 declare(strict_types
=1);
10 use PhpMyAdmin\CheckUserPrivileges
;
11 use PhpMyAdmin\DatabaseInterface
;
13 use PhpMyAdmin\Message
;
14 use PhpMyAdmin\Operations
;
15 use PhpMyAdmin\Partition
;
16 use PhpMyAdmin\Relation
;
17 use PhpMyAdmin\Response
;
21 if (! defined('ROOT_PATH')) {
22 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
27 require_once ROOT_PATH
. 'libraries/common.inc.php';
29 /** @var Response $response */
30 $response = $containerBuilder->get(Response
::class);
32 /** @var DatabaseInterface $dbi */
33 $dbi = $containerBuilder->get(DatabaseInterface
::class);
35 /** @var string $db */
36 $db = $containerBuilder->getParameter('db');
38 /** @var string $table */
39 $table = $containerBuilder->getParameter('table');
41 /** @var CheckUserPrivileges $checkUserPrivileges */
42 $checkUserPrivileges = $containerBuilder->get('check_user_privileges');
43 $checkUserPrivileges->getPrivileges();
45 // lower_case_table_names=1 `DB` becomes `db`
46 $lowerCaseNames = $dbi->getLowerCaseNames() === '1';
48 if ($lowerCaseNames) {
49 $table = mb_strtolower($table);
52 $pma_table = new Table($table, $db);
54 $header = $response->getHeader();
55 $scripts = $header->getScripts();
56 $scripts->addFile('table/operations.js');
61 require ROOT_PATH
. 'libraries/tbl_common.inc.php';
62 $url_query .= '&goto=tbl_operations.php&back=tbl_operations.php';
63 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
66 * Gets relation settings
68 /** @var Relation $relation */
69 $relation = $containerBuilder->get('relation');
70 $cfgRelation = $relation->getRelationsParam();
72 /** @var Operations $operations */
73 $operations = $containerBuilder->get('operations');
75 // reselect current db (needed in some cases probably due to
76 // the calling of PhpMyAdmin\Relation)
80 * Gets tables information
82 $pma_table = $dbi->getTable(
86 $reread_info = $pma_table->getStatusInfo(null, false);
87 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ?
true : false));
88 if ($pma_table->isView()) {
90 $tbl_storage_engine = __('View');
94 $tbl_storage_engine = $pma_table->getStorageEngine();
95 $show_comment = $pma_table->getComment();
97 $tbl_collation = $pma_table->getCollation();
98 $table_info_num_rows = $pma_table->getNumRows();
99 $row_format = $pma_table->getRowFormat();
100 $auto_increment = $pma_table->getAutoIncrement();
101 $create_options = $pma_table->getCreateOptions();
103 // set initial value of these variables, based on the current table engine
104 if ($pma_table->isEngine('ARIA')) {
105 // the value for transactional can be implicit
106 // (no create option found, in this case it means 1)
107 // or explicit (option found with a value of 0 or 1)
108 // ($create_options['transactional'] may have been set by Table class,
109 // from the $create_options)
110 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
113 $create_options['page_checksum'] = isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : '';
116 $pma_table = $dbi->getTable(
120 $reread_info = false;
124 * If the table has to be moved to some other database
126 if (isset($_POST['submit_move']) ||
isset($_POST['submit_copy'])) {
128 $operations->moveOrCopyTable($db, $table);
129 // This was ended in an Ajax call
133 * If the table has to be maintained
135 if (isset($_POST['table_maintenance'])) {
136 include_once ROOT_PATH
. 'sql.php';
140 * Updates table comment, type and options if required
142 if (isset($_POST['submitoptions'])) {
144 $warning_messages = [];
146 if (isset($_POST['new_name'])) {
147 // lower_case_table_names=1 `DB` becomes `db`
148 if ($lowerCaseNames) {
149 $_POST['new_name'] = mb_strtolower(
153 // Get original names before rename operation
154 $oldTable = $pma_table->getName();
155 $oldDb = $pma_table->getDbName();
157 if ($pma_table->rename($_POST['new_name'])) {
158 if (isset($_POST['adjust_privileges'])
159 && ! empty($_POST['adjust_privileges'])
161 $operations->adjustPrivilegesRenameOrMoveTable(
169 // Reselect the original DB
171 $dbi->selectDb($oldDb);
172 $_message .= $pma_table->getLastMessage();
174 $table = $pma_table->getName();
178 $_message .= $pma_table->getLastError();
183 if (! empty($_POST['new_tbl_storage_engine'])
184 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
186 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
188 if ($pma_table->isEngine('ARIA')) {
189 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
192 $create_options['page_checksum'] = isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : '';
195 $new_tbl_storage_engine = '';
198 $row_format = isset($create_options['row_format'])
199 ?
$create_options['row_format']
200 : $pma_table->getRowFormat();
202 $table_alters = $operations->getTableAltersArray(
204 $create_options['pack_keys'],
205 (empty($create_options['checksum']) ?
'0' : '1'),
206 (isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : ''),
207 (empty($create_options['delay_key_write']) ?
'0' : '1'),
209 $new_tbl_storage_engine,
210 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
214 if (count($table_alters) > 0) {
215 $sql_query = 'ALTER TABLE '
216 . Util
::backquote($table);
217 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
219 $result = $dbi->query($sql_query) ?
true : false;
221 unset($table_alters);
222 $warning_messages = $operations->getWarningMessagesArray();
225 if (isset($_POST['tbl_collation'])
226 && ! empty($_POST['tbl_collation'])
227 && isset($_POST['change_all_collations'])
228 && ! empty($_POST['change_all_collations'])
230 $operations->changeAllColumnsCollation(
233 $_POST['tbl_collation']
237 if (isset($_POST['tbl_collation']) && empty($_POST['tbl_collation'])) {
238 $response = Response
::getInstance();
239 if ($response->isAjax()) {
240 $response->setRequestStatus(false);
243 Message
::error(__('No collation provided.'))
250 * Reordering the table has been requested by the user
252 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
253 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
257 * A partition operation has been requested by the user
259 if (isset($_POST['submit_partition'])
260 && ! empty($_POST['partition_operation'])
262 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
266 // to avoid showing the old value (for example the AUTO_INCREMENT) after
267 // a change, clear the cache
268 $dbi->clearTableCache();
270 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
271 if ($pma_table->isView()) {
273 $tbl_storage_engine = __('View');
274 $show_comment = null;
276 $tbl_is_view = false;
277 $tbl_storage_engine = $pma_table->getStorageEngine();
278 $show_comment = $pma_table->getComment();
280 $tbl_collation = $pma_table->getCollation();
281 $table_info_num_rows = $pma_table->getNumRows();
282 $row_format = $pma_table->getRowFormat();
283 $auto_increment = $pma_table->getAutoIncrement();
284 $create_options = $pma_table->getCreateOptions();
288 if (isset($result) && empty($message_to_show)) {
289 if (empty($_message)) {
290 if (empty($sql_query)) {
291 $_message = Message
::success(__('No change'));
298 if ($response->isAjax()) {
299 $response->setRequestStatus($_message->isSuccess());
300 $response->addJSON('message', $_message);
301 if (! empty($sql_query)) {
304 Util
::getMessage(null, $sql_query)
311 ? Message
::success($_message)
312 : Message
::error($_message);
315 if (! empty($warning_messages)) {
316 $_message = new Message();
317 $_message->addMessagesString($warning_messages);
318 $_message->isError(true);
319 if ($response->isAjax()) {
320 $response->setRequestStatus(false);
321 $response->addJSON('message', $_message);
322 if (! empty($sql_query)) {
325 Util
::getMessage(null, $sql_query)
330 unset($warning_messages);
333 if (empty($sql_query)) {
335 $_message->getDisplay()
339 Util
::getMessage($_message, $sql_query)
346 = $url_params['back']
347 = 'tbl_operations.php';
352 $columns = $dbi->getColumns($db, $table);
361 $hideOrderTable = false;
362 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
363 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
364 // InnoDB always orders table rows according to such an index if one is present.
365 if ($tbl_storage_engine == 'INNODB') {
366 $indexes = Index
::getFromTable($table, $db);
367 foreach ($indexes as $name => $idx) {
368 if ($name == 'PRIMARY') {
369 $hideOrderTable = true;
371 } elseif (! $idx->getNonUnique()) {
373 foreach ($idx->getColumns() as $column) {
374 if ($column->getNull()) {
380 $hideOrderTable = true;
386 if (! $hideOrderTable) {
387 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
393 $response->addHTML($operations->getHtmlForMoveTable());
395 if (mb_strstr($show_comment, '; InnoDB free') === false) {
396 if (mb_strstr($show_comment, 'InnoDB free') === false) {
397 // only user entered comment
398 $comment = $show_comment;
400 // here we have just InnoDB generated part
404 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
405 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
408 // PACK_KEYS: MyISAM or ISAM
409 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
410 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
412 // Here should be version check for InnoDB, however it is supported
413 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
417 $operations->getTableOptionDiv(
422 $create_options['pack_keys'],
424 (empty($create_options['delay_key_write']) ?
'0' : '1'),
425 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ?
'0' : '1'),
426 (isset($create_options['page_checksum']) ?
$create_options['page_checksum'] : ''),
427 (empty($create_options['checksum']) ?
'0' : '1')
434 $response->addHTML($operations->getHtmlForCopytable());
440 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
443 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
444 $truncate_table_url_params = [];
445 $drop_table_url_params = [];
448 && ! (isset($db_is_system_schema) && $db_is_system_schema)
450 $this_sql_query = 'TRUNCATE TABLE '
451 . Util
::backquote($table);
452 $truncate_table_url_params = array_merge(
455 'sql_query' => $this_sql_query,
456 'goto' => 'tbl_structure.php',
458 'message_to_show' => sprintf(
459 __('Table %s has been emptied.'),
460 htmlspecialchars($table)
465 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
466 $this_sql_query = 'DROP TABLE '
467 . Util
::backquote($table);
468 $drop_table_url_params = array_merge(
471 'sql_query' => $this_sql_query,
472 'goto' => 'db_operations.php',
475 'message_to_show' => sprintf(
477 ?
__('View %s has been dropped.')
478 : __('Table %s has been dropped.')
480 htmlspecialchars($table)
482 // table name is needed to avoid running
483 // PhpMyAdmin\RelationCleanup::database() on the whole db later
489 $operations->getHtmlForDeleteDataOrTable(
490 $truncate_table_url_params,
491 $drop_table_url_params
496 if (Partition
::havePartitioning()) {
497 $partition_names = Partition
::getPartitionNames($db, $table);
498 // show the Partition maintenance section only if we detect a partition
499 if ($partition_names[0] !== null) {
501 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
505 unset($partition_names);
507 // Referential integrity check
509 if ($cfgRelation['relwork']) {
511 $foreign = $relation->getForeigners($db, $table, '', 'internal');
513 if (! empty($foreign)) {
515 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
517 } // end if ($foreign)
518 } // end if (!empty($cfg['Server']['relation']))