Translated using Weblate (Estonian)
[phpmyadmin.git] / tbl_operations.php
blob8bc2e4968d5678dd9723ad6eb18d99f3330e6736
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Various table operations
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 use PhpMyAdmin\Index;
11 use PhpMyAdmin\Message;
12 use PhpMyAdmin\Partition;
13 use PhpMyAdmin\Operations;
14 use PhpMyAdmin\Relation;
15 use PhpMyAdmin\Response;
16 use PhpMyAdmin\Table;
17 use PhpMyAdmin\Util;
19 if (! defined('ROOT_PATH')) {
20 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
23 /**
26 require_once ROOT_PATH . 'libraries/common.inc.php';
28 /**
29 * functions implementation for this script
31 require_once ROOT_PATH . 'libraries/check_user_privileges.inc.php';
33 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
35 /**
36 * Load JavaScript files
38 $response = Response::getInstance();
39 $header = $response->getHeader();
40 $scripts = $header->getScripts();
41 $scripts->addFile('tbl_operations.js');
43 /**
44 * Runs common work
46 require ROOT_PATH . 'libraries/tbl_common.inc.php';
47 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
48 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
50 /**
51 * Gets relation settings
53 $relation = new Relation($GLOBALS['dbi']);
54 $operations = new Operations($GLOBALS['dbi'], $relation);
56 $cfgRelation = $relation->getRelationsParam();
58 // reselect current db (needed in some cases probably due to
59 // the calling of PhpMyAdmin\Relation)
60 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
62 /**
63 * Gets tables information
65 $pma_table = $GLOBALS['dbi']->getTable(
66 $GLOBALS['db'],
67 $GLOBALS['table']
69 $reread_info = $pma_table->getStatusInfo(null, false);
70 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ? true : false));
71 if ($pma_table->isView()) {
72 $tbl_is_view = true;
73 $tbl_storage_engine = __('View');
74 $show_comment = null;
75 } else {
76 $tbl_is_view = false;
77 $tbl_storage_engine = $pma_table->getStorageEngine();
78 $show_comment = $pma_table->getComment();
80 $tbl_collation = $pma_table->getCollation();
81 $table_info_num_rows = $pma_table->getNumRows();
82 $row_format = $pma_table->getRowFormat();
83 $auto_increment = $pma_table->getAutoIncrement();
84 $create_options = $pma_table->getCreateOptions();
86 // set initial value of these variables, based on the current table engine
87 if ($pma_table->isEngine('ARIA')) {
88 // the value for transactional can be implicit
89 // (no create option found, in this case it means 1)
90 // or explicit (option found with a value of 0 or 1)
91 // ($create_options['transactional'] may have been set by Table class,
92 // from the $create_options)
93 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
94 ? '0'
95 : '1';
96 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
99 $pma_table = $GLOBALS['dbi']->getTable(
100 $GLOBALS['db'],
101 $GLOBALS['table']
103 $reread_info = false;
104 $table_alters = [];
107 * If the table has to be moved to some other database
109 if (isset($_POST['submit_move']) || isset($_POST['submit_copy'])) {
110 //$_message = '';
111 $operations->moveOrCopyTable($db, $table);
112 // This was ended in an Ajax call
113 exit;
116 * If the table has to be maintained
118 if (isset($_POST['table_maintenance'])) {
119 include_once ROOT_PATH . 'sql.php';
120 unset($result);
123 * Updates table comment, type and options if required
125 if (isset($_POST['submitoptions'])) {
126 $_message = '';
127 $warning_messages = [];
129 if (isset($_POST['new_name'])) {
130 // Get original names before rename operation
131 $oldTable = $pma_table->getName();
132 $oldDb = $pma_table->getDbName();
134 if ($pma_table->rename($_POST['new_name'])) {
135 if (isset($_POST['adjust_privileges'])
136 && ! empty($_POST['adjust_privileges'])
138 $operations->adjustPrivilegesRenameOrMoveTable(
139 $oldDb,
140 $oldTable,
141 $_POST['db'],
142 $_POST['new_name']
146 // Reselect the original DB
147 $GLOBALS['db'] = $oldDb;
148 $GLOBALS['dbi']->selectDb($oldDb);
149 $_message .= $pma_table->getLastMessage();
150 $result = true;
151 $GLOBALS['table'] = $pma_table->getName();
152 $reread_info = true;
153 $reload = true;
154 } else {
155 $_message .= $pma_table->getLastError();
156 $result = false;
160 if (! empty($_POST['new_tbl_storage_engine'])
161 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
163 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
165 if ($pma_table->isEngine('ARIA')) {
166 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
167 ? '0'
168 : '1';
169 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
171 } else {
172 $new_tbl_storage_engine = '';
175 $row_format = (isset($create_options['row_format']))
176 ? $create_options['row_format']
177 : $pma_table->getRowFormat();
179 $table_alters = $operations->getTableAltersArray(
180 $pma_table,
181 $create_options['pack_keys'],
182 (empty($create_options['checksum']) ? '0' : '1'),
183 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
184 (empty($create_options['delay_key_write']) ? '0' : '1'),
185 $row_format,
186 $new_tbl_storage_engine,
187 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
188 $tbl_collation
191 if (count($table_alters) > 0) {
192 $sql_query = 'ALTER TABLE '
193 . Util::backquote($GLOBALS['table']);
194 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
195 $sql_query .= ';';
196 $result = $GLOBALS['dbi']->query($sql_query) ? true : false;
197 $reread_info = true;
198 unset($table_alters);
199 $warning_messages = $operations->getWarningMessagesArray();
202 if (isset($_POST['tbl_collation'])
203 && ! empty($_POST['tbl_collation'])
204 && isset($_POST['change_all_collations'])
205 && ! empty($_POST['change_all_collations'])
207 $operations->changeAllColumnsCollation(
208 $GLOBALS['db'],
209 $GLOBALS['table'],
210 $_POST['tbl_collation']
215 * Reordering the table has been requested by the user
217 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
218 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
219 } // end if
222 * A partition operation has been requested by the user
224 if (isset($_POST['submit_partition'])
225 && ! empty($_POST['partition_operation'])
227 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
228 } // end if
230 if ($reread_info) {
231 // to avoid showing the old value (for example the AUTO_INCREMENT) after
232 // a change, clear the cache
233 $GLOBALS['dbi']->clearTableCache();
234 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
235 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
236 if ($pma_table->isView()) {
237 $tbl_is_view = true;
238 $tbl_storage_engine = __('View');
239 $show_comment = null;
240 } else {
241 $tbl_is_view = false;
242 $tbl_storage_engine = $pma_table->getStorageEngine();
243 $show_comment = $pma_table->getComment();
245 $tbl_collation = $pma_table->getCollation();
246 $table_info_num_rows = $pma_table->getNumRows();
247 $row_format = $pma_table->getRowFormat();
248 $auto_increment = $pma_table->getAutoIncrement();
249 $create_options = $pma_table->getCreateOptions();
251 unset($reread_info);
253 if (isset($result) && empty($message_to_show)) {
254 if (empty($_message)) {
255 if (empty($sql_query)) {
256 $_message = Message::success(__('No change'));
257 } else {
258 $_message = $result
259 ? Message::success()
260 : Message::error();
263 if ($response->isAjax()) {
264 $response->setRequestStatus($_message->isSuccess());
265 $response->addJSON('message', $_message);
266 if (! empty($sql_query)) {
267 $response->addJSON(
268 'sql_query',
269 Util::getMessage(null, $sql_query)
272 exit;
274 } else {
275 $_message = $result
276 ? Message::success($_message)
277 : Message::error($_message);
280 if (! empty($warning_messages)) {
281 $_message = new Message();
282 $_message->addMessagesString($warning_messages);
283 $_message->isError(true);
284 if ($response->isAjax()) {
285 $response->setRequestStatus(false);
286 $response->addJSON('message', $_message);
287 if (! empty($sql_query)) {
288 $response->addJSON(
289 'sql_query',
290 Util::getMessage(null, $sql_query)
293 exit;
295 unset($warning_messages);
298 if (empty($sql_query)) {
299 $response->addHTML(
300 $_message->getDisplay()
302 } else {
303 $response->addHTML(
304 Util::getMessage($_message, $sql_query)
307 unset($_message);
310 $url_params['goto']
311 = $url_params['back']
312 = 'tbl_operations.php';
315 * Get columns names
317 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
320 * Displays the page
324 * Order the table
326 $hideOrderTable = false;
327 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
328 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
329 // InnoDB always orders table rows according to such an index if one is present.
330 if ($tbl_storage_engine == 'INNODB') {
331 $indexes = Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
332 foreach ($indexes as $name => $idx) {
333 if ($name == 'PRIMARY') {
334 $hideOrderTable = true;
335 break;
336 } elseif (! $idx->getNonUnique()) {
337 $notNull = true;
338 foreach ($idx->getColumns() as $column) {
339 if ($column->getNull()) {
340 $notNull = false;
341 break;
344 if ($notNull) {
345 $hideOrderTable = true;
346 break;
351 if (! $hideOrderTable) {
352 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
356 * Move table
358 $response->addHTML($operations->getHtmlForMoveTable());
360 if (mb_strstr($show_comment, '; InnoDB free') === false) {
361 if (mb_strstr($show_comment, 'InnoDB free') === false) {
362 // only user entered comment
363 $comment = $show_comment;
364 } else {
365 // here we have just InnoDB generated part
366 $comment = '';
368 } else {
369 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
370 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
373 // PACK_KEYS: MyISAM or ISAM
374 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
375 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
377 // Here should be version check for InnoDB, however it is supported
378 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
379 // check for version
381 $response->addHTML(
382 $operations->getTableOptionDiv(
383 $pma_table,
384 $comment,
385 $tbl_collation,
386 $tbl_storage_engine,
387 $create_options['pack_keys'],
388 $auto_increment,
389 (empty($create_options['delay_key_write']) ? '0' : '1'),
390 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
391 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
392 (empty($create_options['checksum']) ? '0' : '1')
397 * Copy table
399 $response->addHTML($operations->getHtmlForCopytable());
402 * Table maintenance
404 $response->addHTML(
405 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
408 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
409 $truncate_table_url_params = [];
410 $drop_table_url_params = [];
412 if (! $tbl_is_view
413 && ! (isset($db_is_system_schema) && $db_is_system_schema)
415 $this_sql_query = 'TRUNCATE TABLE '
416 . Util::backquote($GLOBALS['table']);
417 $truncate_table_url_params = array_merge(
418 $url_params,
420 'sql_query' => $this_sql_query,
421 'goto' => 'tbl_structure.php',
422 'reload' => '1',
423 'message_to_show' => sprintf(
424 __('Table %s has been emptied.'),
425 htmlspecialchars($table)
430 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
431 $this_sql_query = 'DROP TABLE '
432 . Util::backquote($GLOBALS['table']);
433 $drop_table_url_params = array_merge(
434 $url_params,
436 'sql_query' => $this_sql_query,
437 'goto' => 'db_operations.php',
438 'reload' => '1',
439 'purge' => '1',
440 'message_to_show' => sprintf(
441 ($tbl_is_view
442 ? __('View %s has been dropped.')
443 : __('Table %s has been dropped.')
445 htmlspecialchars($table)
447 // table name is needed to avoid running
448 // PhpMyAdmin\RelationCleanup::database() on the whole db later
449 'table' => $GLOBALS['table'],
453 $response->addHTML(
454 $operations->getHtmlForDeleteDataOrTable(
455 $truncate_table_url_params,
456 $drop_table_url_params
461 if (Partition::havePartitioning()) {
462 $partition_names = Partition::getPartitionNames($db, $table);
463 // show the Partition maintenance section only if we detect a partition
464 if (! is_null($partition_names[0])) {
465 $response->addHTML(
466 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
468 } // end if
469 } // end if
470 unset($partition_names);
472 // Referential integrity check
473 // The Referential integrity check was intended for the non-InnoDB
474 // tables for which the relations are defined in pmadb
475 // so I assume that if the current table is InnoDB, I don't display
476 // this choice (InnoDB maintains integrity by itself)
478 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
479 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
480 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
482 if (! empty($foreign)) {
483 $response->addHTML(
484 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
486 } // end if ($foreign)
487 } // end if (!empty($cfg['Server']['relation']))