Translated using Weblate (Slovenian)
[phpmyadmin.git] / tbl_operations.php
blob1d2ccc0d08d664090e48ff876d97354495de7ef7
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\CheckUserPrivileges;
11 use PhpMyAdmin\DatabaseInterface;
12 use PhpMyAdmin\Di\Container;
13 use PhpMyAdmin\Index;
14 use PhpMyAdmin\Message;
15 use PhpMyAdmin\Partition;
16 use PhpMyAdmin\Operations;
17 use PhpMyAdmin\Relation;
18 use PhpMyAdmin\Response;
19 use PhpMyAdmin\Table;
20 use PhpMyAdmin\Util;
22 if (! defined('ROOT_PATH')) {
23 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
26 require_once ROOT_PATH . 'libraries/common.inc.php';
28 $container = Container::getDefaultContainer();
29 $container->set(Response::class, Response::getInstance());
31 /** @var Response $response */
32 $response = $container->get(Response::class);
34 /** @var DatabaseInterface $dbi */
35 $dbi = $container->get(DatabaseInterface::class);
37 $checkUserPrivileges = new CheckUserPrivileges($dbi);
38 $checkUserPrivileges->getPrivileges();
40 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
42 $header = $response->getHeader();
43 $scripts = $header->getScripts();
44 $scripts->addFile('tbl_operations.js');
46 /**
47 * Runs common work
49 require ROOT_PATH . '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($dbi);
57 $operations = new Operations($dbi, $relation);
59 $cfgRelation = $relation->getRelationsParam();
61 // reselect current db (needed in some cases probably due to
62 // the calling of PhpMyAdmin\Relation)
63 $dbi->selectDb($GLOBALS['db']);
65 /**
66 * Gets tables information
68 $pma_table = $dbi->getTable(
69 $GLOBALS['db'],
70 $GLOBALS['table']
72 $reread_info = $pma_table->getStatusInfo(null, false);
73 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ? true : false));
74 if ($pma_table->isView()) {
75 $tbl_is_view = true;
76 $tbl_storage_engine = __('View');
77 $show_comment = null;
78 } else {
79 $tbl_is_view = false;
80 $tbl_storage_engine = $pma_table->getStorageEngine();
81 $show_comment = $pma_table->getComment();
83 $tbl_collation = $pma_table->getCollation();
84 $table_info_num_rows = $pma_table->getNumRows();
85 $row_format = $pma_table->getRowFormat();
86 $auto_increment = $pma_table->getAutoIncrement();
87 $create_options = $pma_table->getCreateOptions();
89 // set initial value of these variables, based on the current table engine
90 if ($pma_table->isEngine('ARIA')) {
91 // the value for transactional can be implicit
92 // (no create option found, in this case it means 1)
93 // or explicit (option found with a value of 0 or 1)
94 // ($create_options['transactional'] may have been set by Table class,
95 // from the $create_options)
96 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
97 ? '0'
98 : '1';
99 $create_options['page_checksum'] = isset($create_options['page_checksum']) ? $create_options['page_checksum'] : '';
102 $pma_table = $dbi->getTable(
103 $GLOBALS['db'],
104 $GLOBALS['table']
106 $reread_info = false;
107 $table_alters = [];
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 ROOT_PATH . 'sql.php';
123 unset($result);
126 * Updates table comment, type and options if required
128 if (isset($_POST['submitoptions'])) {
129 $_message = '';
130 $warning_messages = [];
132 if (isset($_POST['new_name'])) {
133 // Get original names before rename operation
134 $oldTable = $pma_table->getName();
135 $oldDb = $pma_table->getDbName();
137 if ($pma_table->rename($_POST['new_name'])) {
138 if (isset($_POST['adjust_privileges'])
139 && ! empty($_POST['adjust_privileges'])
141 $operations->adjustPrivilegesRenameOrMoveTable(
142 $oldDb,
143 $oldTable,
144 $_POST['db'],
145 $_POST['new_name']
149 // Reselect the original DB
150 $GLOBALS['db'] = $oldDb;
151 $dbi->selectDb($oldDb);
152 $_message .= $pma_table->getLastMessage();
153 $result = true;
154 $GLOBALS['table'] = $pma_table->getName();
155 $reread_info = true;
156 $reload = true;
157 } else {
158 $_message .= $pma_table->getLastError();
159 $result = false;
163 if (! empty($_POST['new_tbl_storage_engine'])
164 && mb_strtoupper($_POST['new_tbl_storage_engine']) !== $tbl_storage_engine
166 $new_tbl_storage_engine = mb_strtoupper($_POST['new_tbl_storage_engine']);
168 if ($pma_table->isEngine('ARIA')) {
169 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
170 ? '0'
171 : '1';
172 $create_options['page_checksum'] = isset($create_options['page_checksum']) ? $create_options['page_checksum'] : '';
174 } else {
175 $new_tbl_storage_engine = '';
178 $row_format = isset($create_options['row_format'])
179 ? $create_options['row_format']
180 : $pma_table->getRowFormat();
182 $table_alters = $operations->getTableAltersArray(
183 $pma_table,
184 $create_options['pack_keys'],
185 (empty($create_options['checksum']) ? '0' : '1'),
186 (isset($create_options['page_checksum']) ? $create_options['page_checksum'] : ''),
187 (empty($create_options['delay_key_write']) ? '0' : '1'),
188 $row_format,
189 $new_tbl_storage_engine,
190 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
191 $tbl_collation
194 if (count($table_alters) > 0) {
195 $sql_query = 'ALTER TABLE '
196 . Util::backquote($GLOBALS['table']);
197 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
198 $sql_query .= ';';
199 $result = $dbi->query($sql_query) ? true : false;
200 $reread_info = true;
201 unset($table_alters);
202 $warning_messages = $operations->getWarningMessagesArray();
205 if (isset($_POST['tbl_collation'])
206 && ! empty($_POST['tbl_collation'])
207 && isset($_POST['change_all_collations'])
208 && ! empty($_POST['change_all_collations'])
210 $operations->changeAllColumnsCollation(
211 $GLOBALS['db'],
212 $GLOBALS['table'],
213 $_POST['tbl_collation']
218 * Reordering the table has been requested by the user
220 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
221 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
222 } // end if
225 * A partition operation has been requested by the user
227 if (isset($_POST['submit_partition'])
228 && ! empty($_POST['partition_operation'])
230 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
231 } // end if
233 if ($reread_info) {
234 // to avoid showing the old value (for example the AUTO_INCREMENT) after
235 // a change, clear the cache
236 $dbi->clearTableCache();
237 $dbi->selectDb($GLOBALS['db']);
238 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
239 if ($pma_table->isView()) {
240 $tbl_is_view = true;
241 $tbl_storage_engine = __('View');
242 $show_comment = null;
243 } else {
244 $tbl_is_view = false;
245 $tbl_storage_engine = $pma_table->getStorageEngine();
246 $show_comment = $pma_table->getComment();
248 $tbl_collation = $pma_table->getCollation();
249 $table_info_num_rows = $pma_table->getNumRows();
250 $row_format = $pma_table->getRowFormat();
251 $auto_increment = $pma_table->getAutoIncrement();
252 $create_options = $pma_table->getCreateOptions();
254 unset($reread_info);
256 if (isset($result) && empty($message_to_show)) {
257 if (empty($_message)) {
258 if (empty($sql_query)) {
259 $_message = Message::success(__('No change'));
260 } else {
261 $_message = $result
262 ? Message::success()
263 : Message::error();
266 if ($response->isAjax()) {
267 $response->setRequestStatus($_message->isSuccess());
268 $response->addJSON('message', $_message);
269 if (! empty($sql_query)) {
270 $response->addJSON(
271 'sql_query',
272 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',
293 Util::getMessage(null, $sql_query)
296 exit;
298 unset($warning_messages);
301 if (empty($sql_query)) {
302 $response->addHTML(
303 $_message->getDisplay()
305 } else {
306 $response->addHTML(
307 Util::getMessage($_message, $sql_query)
310 unset($_message);
313 $url_params['goto']
314 = $url_params['back']
315 = 'tbl_operations.php';
318 * Get columns names
320 $columns = $dbi->getColumns($GLOBALS['db'], $GLOBALS['table']);
323 * Displays the page
327 * Order the table
329 $hideOrderTable = false;
330 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
331 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
332 // InnoDB always orders table rows according to such an index if one is present.
333 if ($tbl_storage_engine == 'INNODB') {
334 $indexes = Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
335 foreach ($indexes as $name => $idx) {
336 if ($name == 'PRIMARY') {
337 $hideOrderTable = true;
338 break;
339 } elseif (! $idx->getNonUnique()) {
340 $notNull = true;
341 foreach ($idx->getColumns() as $column) {
342 if ($column->getNull()) {
343 $notNull = false;
344 break;
347 if ($notNull) {
348 $hideOrderTable = true;
349 break;
354 if (! $hideOrderTable) {
355 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
359 * Move table
361 $response->addHTML($operations->getHtmlForMoveTable());
363 if (mb_strstr($show_comment, '; InnoDB free') === false) {
364 if (mb_strstr($show_comment, 'InnoDB free') === false) {
365 // only user entered comment
366 $comment = $show_comment;
367 } else {
368 // here we have just InnoDB generated part
369 $comment = '';
371 } else {
372 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
373 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
376 // PACK_KEYS: MyISAM or ISAM
377 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
378 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
380 // Here should be version check for InnoDB, however it is supported
381 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
382 // check for version
384 $response->addHTML(
385 $operations->getTableOptionDiv(
386 $pma_table,
387 $comment,
388 $tbl_collation,
389 $tbl_storage_engine,
390 $create_options['pack_keys'],
391 $auto_increment,
392 (empty($create_options['delay_key_write']) ? '0' : '1'),
393 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
394 (isset($create_options['page_checksum']) ? $create_options['page_checksum'] : ''),
395 (empty($create_options['checksum']) ? '0' : '1')
400 * Copy table
402 $response->addHTML($operations->getHtmlForCopytable());
405 * Table maintenance
407 $response->addHTML(
408 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
411 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
412 $truncate_table_url_params = [];
413 $drop_table_url_params = [];
415 if (! $tbl_is_view
416 && ! (isset($db_is_system_schema) && $db_is_system_schema)
418 $this_sql_query = 'TRUNCATE TABLE '
419 . Util::backquote($GLOBALS['table']);
420 $truncate_table_url_params = array_merge(
421 $url_params,
423 'sql_query' => $this_sql_query,
424 'goto' => 'tbl_structure.php',
425 'reload' => '1',
426 'message_to_show' => sprintf(
427 __('Table %s has been emptied.'),
428 htmlspecialchars($table)
433 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
434 $this_sql_query = 'DROP TABLE '
435 . Util::backquote($GLOBALS['table']);
436 $drop_table_url_params = array_merge(
437 $url_params,
439 'sql_query' => $this_sql_query,
440 'goto' => 'db_operations.php',
441 'reload' => '1',
442 'purge' => '1',
443 'message_to_show' => sprintf(
444 ($tbl_is_view
445 ? __('View %s has been dropped.')
446 : __('Table %s has been dropped.')
448 htmlspecialchars($table)
450 // table name is needed to avoid running
451 // PhpMyAdmin\RelationCleanup::database() on the whole db later
452 'table' => $GLOBALS['table'],
456 $response->addHTML(
457 $operations->getHtmlForDeleteDataOrTable(
458 $truncate_table_url_params,
459 $drop_table_url_params
464 if (Partition::havePartitioning()) {
465 $partition_names = Partition::getPartitionNames($db, $table);
466 // show the Partition maintenance section only if we detect a partition
467 if (! is_null($partition_names[0])) {
468 $response->addHTML(
469 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
471 } // end if
472 } // end if
473 unset($partition_names);
475 // Referential integrity check
476 // The Referential integrity check was intended for the non-InnoDB
477 // tables for which the relations are defined in pmadb
478 // so I assume that if the current table is InnoDB, I don't display
479 // this choice (InnoDB maintains integrity by itself)
481 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
482 $dbi->selectDb($GLOBALS['db']);
483 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
485 if (! empty($foreign)) {
486 $response->addHTML(
487 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
489 } // end if ($foreign)
490 } // end if (!empty($cfg['Server']['relation']))