Translated using Weblate (Croatian)
[phpmyadmin.git] / tbl_operations.php
blob55d9a22df516eb564a50fd35d6c9ae3d99f35eb6
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']
218 if (isset($_POST['tbl_collation']) && empty($_POST['tbl_collation'])) {
219 $response = Response::getInstance();
220 if ($response->isAjax()) {
221 $response->setRequestStatus(false);
222 $response->addJSON(
223 'message',
224 Message::error(__('No collation provided.'))
226 exit;
231 * Reordering the table has been requested by the user
233 if (isset($_POST['submitorderby']) && ! empty($_POST['order_field'])) {
234 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
235 } // end if
238 * A partition operation has been requested by the user
240 if (isset($_POST['submit_partition'])
241 && ! empty($_POST['partition_operation'])
243 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
244 } // end if
246 if ($reread_info) {
247 // to avoid showing the old value (for example the AUTO_INCREMENT) after
248 // a change, clear the cache
249 $GLOBALS['dbi']->clearTableCache();
250 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
251 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
252 if ($pma_table->isView()) {
253 $tbl_is_view = true;
254 $tbl_storage_engine = __('View');
255 $show_comment = null;
256 } else {
257 $tbl_is_view = false;
258 $tbl_storage_engine = $pma_table->getStorageEngine();
259 $show_comment = $pma_table->getComment();
261 $tbl_collation = $pma_table->getCollation();
262 $table_info_num_rows = $pma_table->getNumRows();
263 $row_format = $pma_table->getRowFormat();
264 $auto_increment = $pma_table->getAutoIncrement();
265 $create_options = $pma_table->getCreateOptions();
267 unset($reread_info);
269 if (isset($result) && empty($message_to_show)) {
270 if (empty($_message)) {
271 if (empty($sql_query)) {
272 $_message = Message::success(__('No change'));
273 } else {
274 $_message = $result
275 ? Message::success()
276 : Message::error();
279 if ($response->isAjax()) {
280 $response->setRequestStatus($_message->isSuccess());
281 $response->addJSON('message', $_message);
282 if (!empty($sql_query)) {
283 $response->addJSON(
284 'sql_query', Util::getMessage(null, $sql_query)
287 exit;
289 } else {
290 $_message = $result
291 ? Message::success($_message)
292 : Message::error($_message);
295 if (! empty($warning_messages)) {
296 $_message = new Message;
297 $_message->addMessagesString($warning_messages);
298 $_message->isError(true);
299 if ($response->isAjax()) {
300 $response->setRequestStatus(false);
301 $response->addJSON('message', $_message);
302 if (!empty($sql_query)) {
303 $response->addJSON(
304 'sql_query', Util::getMessage(null, $sql_query)
307 exit;
309 unset($warning_messages);
312 if (empty($sql_query)) {
313 $response->addHTML(
314 $_message->getDisplay()
316 } else {
317 $response->addHTML(
318 Util::getMessage($_message, $sql_query)
321 unset($_message);
324 $url_params['goto']
325 = $url_params['back']
326 = 'tbl_operations.php';
329 * Get columns names
331 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
334 * Displays the page
338 * Order the table
340 $hideOrderTable = false;
341 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
342 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
343 // InnoDB always orders table rows according to such an index if one is present.
344 if ($tbl_storage_engine == 'INNODB') {
345 $indexes = Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
346 foreach ($indexes as $name => $idx) {
347 if ($name == 'PRIMARY') {
348 $hideOrderTable = true;
349 break;
350 } elseif (! $idx->getNonUnique()) {
351 $notNull = true;
352 foreach ($idx->getColumns() as $column) {
353 if ($column->getNull()) {
354 $notNull = false;
355 break;
358 if ($notNull) {
359 $hideOrderTable = true;
360 break;
365 if (! $hideOrderTable) {
366 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
370 * Move table
372 $response->addHTML($operations->getHtmlForMoveTable());
374 if (mb_strstr($show_comment, '; InnoDB free') === false) {
375 if (mb_strstr($show_comment, 'InnoDB free') === false) {
376 // only user entered comment
377 $comment = $show_comment;
378 } else {
379 // here we have just InnoDB generated part
380 $comment = '';
382 } else {
383 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
384 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
387 // PACK_KEYS: MyISAM or ISAM
388 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
389 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
391 // Here should be version check for InnoDB, however it is supported
392 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
393 // check for version
395 $response->addHTML(
396 $operations->getTableOptionDiv(
397 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
398 $create_options['pack_keys'],
399 $auto_increment,
400 (empty($create_options['delay_key_write']) ? '0' : '1'),
401 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
402 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
403 (empty($create_options['checksum']) ? '0' : '1')
408 * Copy table
410 $response->addHTML($operations->getHtmlForCopytable());
413 * Table maintenance
415 $response->addHTML(
416 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
419 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
420 $truncate_table_url_params = array();
421 $drop_table_url_params = array();
423 if (! $tbl_is_view
424 && ! (isset($db_is_system_schema) && $db_is_system_schema)
426 $this_sql_query = 'TRUNCATE TABLE '
427 . Util::backquote($GLOBALS['table']);
428 $truncate_table_url_params = array_merge(
429 $url_params,
430 array(
431 'sql_query' => $this_sql_query,
432 'goto' => 'tbl_structure.php',
433 'reload' => '1',
434 'message_to_show' => sprintf(
435 __('Table %s has been emptied.'),
436 htmlspecialchars($table)
441 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
442 $this_sql_query = 'DROP TABLE '
443 . Util::backquote($GLOBALS['table']);
444 $drop_table_url_params = array_merge(
445 $url_params,
446 array(
447 'sql_query' => $this_sql_query,
448 'goto' => 'db_operations.php',
449 'reload' => '1',
450 'purge' => '1',
451 'message_to_show' => sprintf(
452 ($tbl_is_view
453 ? __('View %s has been dropped.')
454 : __('Table %s has been dropped.')
456 htmlspecialchars($table)
458 // table name is needed to avoid running
459 // PhpMyAdmin\RelationCleanup::database() on the whole db later
460 'table' => $GLOBALS['table'],
464 $response->addHTML(
465 $operations->getHtmlForDeleteDataOrTable(
466 $truncate_table_url_params,
467 $drop_table_url_params
472 if (Partition::havePartitioning()) {
473 $partition_names = Partition::getPartitionNames($db, $table);
474 // show the Partition maintenance section only if we detect a partition
475 if (! is_null($partition_names[0])) {
476 $response->addHTML(
477 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
479 } // end if
480 } // end if
481 unset($partition_names);
483 // Referential integrity check
485 if ($cfgRelation['relwork']) {
486 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
487 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
489 if (! empty($foreign)) {
490 $response->addHTML(
491 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
493 } // end if ($foreign)
495 } // end if (!empty($cfg['Server']['relation']))