Translated using Weblate (Portuguese (Brazil))
[phpmyadmin.git] / tbl_operations.php
blob4ff5ab9d6941a4461dcd77a213ec8e4b48f19a23
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Various table operations
6 * @package PhpMyAdmin
7 */
8 use PhpMyAdmin\Index;
9 use PhpMyAdmin\Message;
10 use PhpMyAdmin\Partition;
11 use PhpMyAdmin\Operations;
12 use PhpMyAdmin\Relation;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Table;
15 use PhpMyAdmin\Util;
17 /**
20 require_once 'libraries/common.inc.php';
22 /**
23 * functions implementation for this script
25 require_once 'libraries/check_user_privileges.inc.php';
27 // lower_case_table_names=1 `DB` becomes `db`
28 $lowerCaseNames = $GLOBALS['dbi']->getLowerCaseNames() === '1';
30 if ($lowerCaseNames) {
31 $GLOBALS['table'] = mb_strtolower(
32 $GLOBALS['table']
36 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
38 /**
39 * Load JavaScript files
41 $response = Response::getInstance();
42 $header = $response->getHeader();
43 $scripts = $header->getScripts();
44 $scripts->addFile('tbl_operations.js');
46 /**
47 * Runs common work
49 require 'libraries/tbl_common.inc.php';
50 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
51 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
53 /**
54 * Gets relation settings
56 $relation = new Relation();
57 $cfgRelation = $relation->getRelationsParam();
59 // reselect current db (needed in some cases probably due to
60 // the calling of PhpMyAdmin\Relation)
61 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
63 /**
64 * Gets tables information
66 $pma_table = $GLOBALS['dbi']->getTable(
67 $GLOBALS['db'],
68 $GLOBALS['table']
70 $reread_info = $pma_table->getStatusInfo(null, false);
71 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ? true : false));
72 if ($pma_table->isView()) {
73 $tbl_is_view = true;
74 $tbl_storage_engine = __('View');
75 $show_comment = null;
76 } else {
77 $tbl_is_view = false;
78 $tbl_storage_engine = $pma_table->getStorageEngine();
79 $show_comment = $pma_table->getComment();
81 $tbl_collation = $pma_table->getCollation();
82 $table_info_num_rows = $pma_table->getNumRows();
83 $row_format = $pma_table->getRowFormat();
84 $auto_increment = $pma_table->getAutoIncrement();
85 $create_options = $pma_table->getCreateOptions();
87 // set initial value of these variables, based on the current table engine
88 if ($pma_table->isEngine('ARIA')) {
89 // the value for transactional can be implicit
90 // (no create option found, in this case it means 1)
91 // or explicit (option found with a value of 0 or 1)
92 // ($create_options['transactional'] may have been set by Table class,
93 // from the $create_options)
94 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
95 ? '0'
96 : '1';
97 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
100 $pma_table = $GLOBALS['dbi']->getTable(
101 $GLOBALS['db'],
102 $GLOBALS['table']
104 $reread_info = false;
105 $table_alters = array();
107 $operations = new Operations();
110 * If the table has to be moved to some other database
112 if (isset($_POST['submit_move']) || isset($_POST['submit_copy'])) {
113 //$_message = '';
114 $operations->moveOrCopyTable($db, $table);
115 // This was ended in an Ajax call
116 exit;
119 * If the table has to be maintained
121 if (isset($_POST['table_maintenance'])) {
122 include_once 'sql.php';
123 unset($result);
126 * Updates table comment, type and options if required
128 if (isset($_POST['submitoptions'])) {
129 $_message = '';
130 $warning_messages = array();
132 if (isset($_POST['new_name'])) {
133 // lower_case_table_names=1 `DB` becomes `db`
134 if ($lowerCaseNames) {
135 $_POST['new_name'] = mb_strtolower(
136 $_POST['new_name']
139 // Get original names before rename operation
140 $oldTable = $pma_table->getName();
141 $oldDb = $pma_table->getDbName();
143 if ($pma_table->rename($_POST['new_name'])) {
144 if (isset($_POST['adjust_privileges'])
145 && ! empty($_POST['adjust_privileges'])
147 $operations->adjustPrivilegesRenameOrMoveTable(
148 $oldDb, $oldTable, $_POST['db'], $_POST['new_name']
152 // Reselect the original DB
153 $GLOBALS['db'] = $oldDb;
154 $GLOBALS['dbi']->selectDb($oldDb);
155 $_message .= $pma_table->getLastMessage();
156 $result = true;
157 $GLOBALS['table'] = $pma_table->getName();
158 $reread_info = true;
159 $reload = true;
160 } else {
161 $_message .= $pma_table->getLastError();
162 $result = false;
166 if (! empty($_POST['new_tbl_storage_engine'])
167 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
169 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
171 if ($pma_table->isEngine('ARIA')) {
172 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
173 ? '0'
174 : '1';
175 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
177 } else {
178 $new_tbl_storage_engine = '';
181 $row_format = (isset($create_options['row_format']))
182 ? $create_options['row_format']
183 : $pma_table->getRowFormat();
185 $table_alters = $operations->getTableAltersArray(
186 $pma_table,
187 $create_options['pack_keys'],
188 (empty($create_options['checksum']) ? '0' : '1'),
189 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
190 (empty($create_options['delay_key_write']) ? '0' : '1'),
191 $row_format,
192 $new_tbl_storage_engine,
193 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
194 $tbl_collation
197 if (count($table_alters) > 0) {
198 $sql_query = 'ALTER TABLE '
199 . Util::backquote($GLOBALS['table']);
200 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
201 $sql_query .= ';';
202 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
203 $reread_info = true;
204 unset($table_alters);
205 $warning_messages = $operations->getWarningMessagesArray();
208 if (isset($_POST['tbl_collation'])
209 && ! empty($_POST['tbl_collation'])
210 && isset($_POST['change_all_collations'])
211 && ! empty($_POST['change_all_collations'])
213 $operations->changeAllColumnsCollation(
214 $GLOBALS['db'], $GLOBALS['table'], $_POST['tbl_collation']
219 * Reordering the table has been requested by the user
221 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
222 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
223 } // end if
226 * A partition operation has been requested by the user
228 if (isset($_POST['submit_partition'])
229 && ! empty($_POST['partition_operation'])
231 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
232 } // end if
234 if ($reread_info) {
235 // to avoid showing the old value (for example the AUTO_INCREMENT) after
236 // a change, clear the cache
237 $GLOBALS['dbi']->clearTableCache();
238 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
239 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
240 if ($pma_table->isView()) {
241 $tbl_is_view = true;
242 $tbl_storage_engine = __('View');
243 $show_comment = null;
244 } else {
245 $tbl_is_view = false;
246 $tbl_storage_engine = $pma_table->getStorageEngine();
247 $show_comment = $pma_table->getComment();
249 $tbl_collation = $pma_table->getCollation();
250 $table_info_num_rows = $pma_table->getNumRows();
251 $row_format = $pma_table->getRowFormat();
252 $auto_increment = $pma_table->getAutoIncrement();
253 $create_options = $pma_table->getCreateOptions();
255 unset($reread_info);
257 if (isset($result) && empty($message_to_show)) {
258 if (empty($_message)) {
259 if (empty($sql_query)) {
260 $_message = Message::success(__('No change'));
261 } else {
262 $_message = $result
263 ? Message::success()
264 : Message::error();
267 if ($response->isAjax()) {
268 $response->setRequestStatus($_message->isSuccess());
269 $response->addJSON('message', $_message);
270 if (!empty($sql_query)) {
271 $response->addJSON(
272 'sql_query', Util::getMessage(null, $sql_query)
275 exit;
277 } else {
278 $_message = $result
279 ? Message::success($_message)
280 : Message::error($_message);
283 if (! empty($warning_messages)) {
284 $_message = new Message;
285 $_message->addMessagesString($warning_messages);
286 $_message->isError(true);
287 if ($response->isAjax()) {
288 $response->setRequestStatus(false);
289 $response->addJSON('message', $_message);
290 if (!empty($sql_query)) {
291 $response->addJSON(
292 'sql_query', Util::getMessage(null, $sql_query)
295 exit;
297 unset($warning_messages);
300 if (empty($sql_query)) {
301 $response->addHTML(
302 $_message->getDisplay()
304 } else {
305 $response->addHTML(
306 Util::getMessage($_message, $sql_query)
309 unset($_message);
312 $url_params['goto']
313 = $url_params['back']
314 = 'tbl_operations.php';
317 * Get columns names
319 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
322 * Displays the page
326 * Order the table
328 $hideOrderTable = false;
329 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
330 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
331 // InnoDB always orders table rows according to such an index if one is present.
332 if ($tbl_storage_engine == 'INNODB') {
333 $indexes = Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
334 foreach ($indexes as $name => $idx) {
335 if ($name == 'PRIMARY') {
336 $hideOrderTable = true;
337 break;
338 } elseif (! $idx->getNonUnique()) {
339 $notNull = true;
340 foreach ($idx->getColumns() as $column) {
341 if ($column->getNull()) {
342 $notNull = false;
343 break;
346 if ($notNull) {
347 $hideOrderTable = true;
348 break;
353 if (! $hideOrderTable) {
354 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
358 * Move table
360 $response->addHTML($operations->getHtmlForMoveTable());
362 if (mb_strstr($show_comment, '; InnoDB free') === false) {
363 if (mb_strstr($show_comment, 'InnoDB free') === false) {
364 // only user entered comment
365 $comment = $show_comment;
366 } else {
367 // here we have just InnoDB generated part
368 $comment = '';
370 } else {
371 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
372 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
375 // PACK_KEYS: MyISAM or ISAM
376 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
377 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
379 // Here should be version check for InnoDB, however it is supported
380 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
381 // check for version
383 $response->addHTML(
384 $operations->getTableOptionDiv(
385 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
386 $create_options['pack_keys'],
387 $auto_increment,
388 (empty($create_options['delay_key_write']) ? '0' : '1'),
389 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
390 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
391 (empty($create_options['checksum']) ? '0' : '1')
396 * Copy table
398 $response->addHTML($operations->getHtmlForCopytable());
401 * Table maintenance
403 $response->addHTML(
404 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
407 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
408 $truncate_table_url_params = array();
409 $drop_table_url_params = array();
411 if (! $tbl_is_view
412 && ! (isset($db_is_system_schema) && $db_is_system_schema)
414 $this_sql_query = 'TRUNCATE TABLE '
415 . Util::backquote($GLOBALS['table']);
416 $truncate_table_url_params = array_merge(
417 $url_params,
418 array(
419 'sql_query' => $this_sql_query,
420 'goto' => 'tbl_structure.php',
421 'reload' => '1',
422 'message_to_show' => sprintf(
423 __('Table %s has been emptied.'),
424 htmlspecialchars($table)
429 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
430 $this_sql_query = 'DROP TABLE '
431 . Util::backquote($GLOBALS['table']);
432 $drop_table_url_params = array_merge(
433 $url_params,
434 array(
435 'sql_query' => $this_sql_query,
436 'goto' => 'db_operations.php',
437 'reload' => '1',
438 'purge' => '1',
439 'message_to_show' => sprintf(
440 ($tbl_is_view
441 ? __('View %s has been dropped.')
442 : __('Table %s has been dropped.')
444 htmlspecialchars($table)
446 // table name is needed to avoid running
447 // PhpMyAdmin\RelationCleanup::database() on the whole db later
448 'table' => $GLOBALS['table'],
452 $response->addHTML(
453 $operations->getHtmlForDeleteDataOrTable(
454 $truncate_table_url_params,
455 $drop_table_url_params
460 if (Partition::havePartitioning()) {
461 $partition_names = Partition::getPartitionNames($db, $table);
462 // show the Partition maintenance section only if we detect a partition
463 if (! is_null($partition_names[0])) {
464 $response->addHTML(
465 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
467 } // end if
468 } // end if
469 unset($partition_names);
471 // Referential integrity check
472 // The Referential integrity check was intended for the non-InnoDB
473 // tables for which the relations are defined in pmadb
474 // so I assume that if the current table is InnoDB, I don't display
475 // this choice (InnoDB maintains integrity by itself)
477 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
478 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
479 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
481 if (! empty($foreign)) {
482 $response->addHTML(
483 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
485 } // end if ($foreign)
487 } // end if (!empty($cfg['Server']['relation']))