Translated using Weblate (Estonian)
[phpmyadmin.git] / tbl_operations.php
blobc79c70c068b17229133162bda64688850600fbcb
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 /**
22 require_once 'libraries/common.inc.php';
24 /**
25 * functions implementation for this script
27 require_once 'libraries/check_user_privileges.inc.php';
29 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
31 /**
32 * Load JavaScript files
34 $response = Response::getInstance();
35 $header = $response->getHeader();
36 $scripts = $header->getScripts();
37 $scripts->addFile('tbl_operations.js');
39 /**
40 * Runs common work
42 require 'libraries/tbl_common.inc.php';
43 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
44 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
46 /**
47 * Gets relation settings
49 $relation = new Relation();
50 $cfgRelation = $relation->getRelationsParam();
52 // reselect current db (needed in some cases probably due to
53 // the calling of PhpMyAdmin\Relation)
54 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
56 /**
57 * Gets tables information
59 $pma_table = $GLOBALS['dbi']->getTable(
60 $GLOBALS['db'],
61 $GLOBALS['table']
63 $reread_info = $pma_table->getStatusInfo(null, false);
64 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ? true : false));
65 if ($pma_table->isView()) {
66 $tbl_is_view = true;
67 $tbl_storage_engine = __('View');
68 $show_comment = null;
69 } else {
70 $tbl_is_view = false;
71 $tbl_storage_engine = $pma_table->getStorageEngine();
72 $show_comment = $pma_table->getComment();
74 $tbl_collation = $pma_table->getCollation();
75 $table_info_num_rows = $pma_table->getNumRows();
76 $row_format = $pma_table->getRowFormat();
77 $auto_increment = $pma_table->getAutoIncrement();
78 $create_options = $pma_table->getCreateOptions();
80 // set initial value of these variables, based on the current table engine
81 if ($pma_table->isEngine('ARIA')) {
82 // the value for transactional can be implicit
83 // (no create option found, in this case it means 1)
84 // or explicit (option found with a value of 0 or 1)
85 // ($create_options['transactional'] may have been set by Table class,
86 // from the $create_options)
87 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
88 ? '0'
89 : '1';
90 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
93 $pma_table = $GLOBALS['dbi']->getTable(
94 $GLOBALS['db'],
95 $GLOBALS['table']
97 $reread_info = false;
98 $table_alters = [];
100 $operations = new Operations();
103 * If the table has to be moved to some other database
105 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
106 //$_message = '';
107 $operations->moveOrCopyTable($db, $table);
108 // This was ended in an Ajax call
109 exit;
112 * If the table has to be maintained
114 if (isset($_REQUEST['table_maintenance'])) {
115 include_once 'sql.php';
116 unset($result);
119 * Updates table comment, type and options if required
121 if (isset($_REQUEST['submitoptions'])) {
122 $_message = '';
123 $warning_messages = [];
125 if (isset($_REQUEST['new_name'])) {
126 // Get original names before rename operation
127 $oldTable = $pma_table->getName();
128 $oldDb = $pma_table->getDbName();
130 if ($pma_table->rename($_REQUEST['new_name'])) {
131 if (isset($_REQUEST['adjust_privileges'])
132 && ! empty($_REQUEST['adjust_privileges'])
134 $operations->adjustPrivilegesRenameOrMoveTable(
135 $oldDb,
136 $oldTable,
137 $_REQUEST['db'],
138 $_REQUEST['new_name']
142 // Reselect the original DB
143 $GLOBALS['db'] = $oldDb;
144 $GLOBALS['dbi']->selectDb($oldDb);
145 $_message .= $pma_table->getLastMessage();
146 $result = true;
147 $GLOBALS['table'] = $pma_table->getName();
148 $reread_info = true;
149 $reload = true;
150 } else {
151 $_message .= $pma_table->getLastError();
152 $result = false;
156 if (! empty($_REQUEST['new_tbl_storage_engine'])
157 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
159 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
161 if ($pma_table->isEngine('ARIA')) {
162 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
163 ? '0'
164 : '1';
165 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
167 } else {
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(
176 $pma_table,
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'),
181 $row_format,
182 $new_tbl_storage_engine,
183 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
184 $tbl_collation
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);
191 $sql_query .= ';';
192 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
193 $reread_info = true;
194 unset($table_alters);
195 $warning_messages = $operations->getWarningMessagesArray();
198 if (isset($_REQUEST['tbl_collation'])
199 && ! empty($_REQUEST['tbl_collation'])
200 && isset($_REQUEST['change_all_collations'])
201 && ! empty($_REQUEST['change_all_collations'])
203 $operations->changeAllColumnsCollation(
204 $GLOBALS['db'],
205 $GLOBALS['table'],
206 $_REQUEST['tbl_collation']
211 * Reordering the table has been requested by the user
213 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
214 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
215 } // end if
218 * A partition operation has been requested by the user
220 if (isset($_REQUEST['submit_partition'])
221 && ! empty($_REQUEST['partition_operation'])
223 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
224 } // end if
226 if ($reread_info) {
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()) {
233 $tbl_is_view = true;
234 $tbl_storage_engine = __('View');
235 $show_comment = null;
236 } else {
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();
247 unset($reread_info);
249 if (isset($result) && empty($message_to_show)) {
250 if (empty($_message)) {
251 if (empty($sql_query)) {
252 $_message = Message::success(__('No change'));
253 } else {
254 $_message = $result
255 ? Message::success()
256 : Message::error();
259 if ($response->isAjax()) {
260 $response->setRequestStatus($_message->isSuccess());
261 $response->addJSON('message', $_message);
262 if (!empty($sql_query)) {
263 $response->addJSON(
264 'sql_query',
265 Util::getMessage(null, $sql_query)
268 exit;
270 } else {
271 $_message = $result
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)) {
284 $response->addJSON(
285 'sql_query',
286 Util::getMessage(null, $sql_query)
289 exit;
291 unset($warning_messages);
294 if (empty($sql_query)) {
295 $response->addHTML(
296 $_message->getDisplay()
298 } else {
299 $response->addHTML(
300 Util::getMessage($_message, $sql_query)
303 unset($_message);
306 $url_params['goto']
307 = $url_params['back']
308 = 'tbl_operations.php';
311 * Get columns names
313 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
316 * Displays the page
320 * Order the 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;
331 break;
332 } elseif (! $idx->getNonUnique()) {
333 $notNull = true;
334 foreach ($idx->getColumns() as $column) {
335 if ($column->getNull()) {
336 $notNull = false;
337 break;
340 if ($notNull) {
341 $hideOrderTable = true;
342 break;
347 if (! $hideOrderTable) {
348 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
352 * Move table
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;
360 } else {
361 // here we have just InnoDB generated part
362 $comment = '';
364 } else {
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
375 // check for version
377 $response->addHTML(
378 $operations->getTableOptionDiv(
379 $pma_table,
380 $comment,
381 $tbl_collation,
382 $tbl_storage_engine,
383 $create_options['pack_keys'],
384 $auto_increment,
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')
393 * Copy table
395 $response->addHTML($operations->getHtmlForCopytable());
398 * Table maintenance
400 $response->addHTML(
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 = [];
408 if (! $tbl_is_view
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(
414 $url_params,
416 'sql_query' => $this_sql_query,
417 'goto' => 'tbl_structure.php',
418 'reload' => '1',
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(
430 $url_params,
432 'sql_query' => $this_sql_query,
433 'goto' => 'db_operations.php',
434 'reload' => '1',
435 'purge' => '1',
436 'message_to_show' => sprintf(
437 ($tbl_is_view
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'],
449 $response->addHTML(
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])) {
461 $response->addHTML(
462 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
464 } // end if
465 } // end if
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)) {
479 $response->addHTML(
480 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
482 } // end if ($foreign)
483 } // end if (!empty($cfg['Server']['relation']))