Translated using Weblate (Norwegian Bokmål)
[phpmyadmin.git] / tbl_operations.php
blob95189677b8e3e7dfcef6c4c6cefb1eba901a80ba
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 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
29 /**
30 * Load JavaScript files
32 $response = Response::getInstance();
33 $header = $response->getHeader();
34 $scripts = $header->getScripts();
35 $scripts->addFile('tbl_operations.js');
37 /**
38 * Runs common work
40 require 'libraries/tbl_common.inc.php';
41 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
42 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
44 /**
45 * Gets relation settings
47 $relation = new Relation();
48 $cfgRelation = $relation->getRelationsParam();
50 // reselect current db (needed in some cases probably due to
51 // the calling of PhpMyAdmin\Relation)
52 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
54 /**
55 * Gets tables information
57 $pma_table = $GLOBALS['dbi']->getTable(
58 $GLOBALS['db'],
59 $GLOBALS['table']
61 $reread_info = $pma_table->getStatusInfo(null, false);
62 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ? true : false));
63 if ($pma_table->isView()) {
64 $tbl_is_view = true;
65 $tbl_storage_engine = __('View');
66 $show_comment = null;
67 } else {
68 $tbl_is_view = false;
69 $tbl_storage_engine = $pma_table->getStorageEngine();
70 $show_comment = $pma_table->getComment();
72 $tbl_collation = $pma_table->getCollation();
73 $table_info_num_rows = $pma_table->getNumRows();
74 $row_format = $pma_table->getRowFormat();
75 $auto_increment = $pma_table->getAutoIncrement();
76 $create_options = $pma_table->getCreateOptions();
78 // set initial value of these variables, based on the current table engine
79 if ($pma_table->isEngine('ARIA')) {
80 // the value for transactional can be implicit
81 // (no create option found, in this case it means 1)
82 // or explicit (option found with a value of 0 or 1)
83 // ($create_options['transactional'] may have been set by Table class,
84 // from the $create_options)
85 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
86 ? '0'
87 : '1';
88 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
91 $pma_table = $GLOBALS['dbi']->getTable(
92 $GLOBALS['db'],
93 $GLOBALS['table']
95 $reread_info = false;
96 $table_alters = array();
98 $operations = new Operations();
101 * If the table has to be moved to some other database
103 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
104 //$_message = '';
105 $operations->moveOrCopyTable($db, $table);
106 // This was ended in an Ajax call
107 exit;
110 * If the table has to be maintained
112 if (isset($_REQUEST['table_maintenance'])) {
113 include_once 'sql.php';
114 unset($result);
117 * Updates table comment, type and options if required
119 if (isset($_REQUEST['submitoptions'])) {
120 $_message = '';
121 $warning_messages = array();
123 if (isset($_REQUEST['new_name'])) {
124 // Get original names before rename operation
125 $oldTable = $pma_table->getName();
126 $oldDb = $pma_table->getDbName();
128 if ($pma_table->rename($_REQUEST['new_name'])) {
129 if (isset($_REQUEST['adjust_privileges'])
130 && ! empty($_REQUEST['adjust_privileges'])
132 $operations->adjustPrivilegesRenameOrMoveTable(
133 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
137 // Reselect the original DB
138 $GLOBALS['db'] = $oldDb;
139 $GLOBALS['dbi']->selectDb($oldDb);
140 $_message .= $pma_table->getLastMessage();
141 $result = true;
142 $GLOBALS['table'] = $pma_table->getName();
143 $reread_info = true;
144 $reload = true;
145 } else {
146 $_message .= $pma_table->getLastError();
147 $result = false;
151 if (! empty($_REQUEST['new_tbl_storage_engine'])
152 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
154 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
156 if ($pma_table->isEngine('ARIA')) {
157 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
158 ? '0'
159 : '1';
160 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
162 } else {
163 $new_tbl_storage_engine = '';
166 $row_format = (isset($create_options['row_format']))
167 ? $create_options['row_format']
168 : $pma_table->getRowFormat();
170 $table_alters = $operations->getTableAltersArray(
171 $pma_table,
172 $create_options['pack_keys'],
173 (empty($create_options['checksum']) ? '0' : '1'),
174 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
175 (empty($create_options['delay_key_write']) ? '0' : '1'),
176 $row_format,
177 $new_tbl_storage_engine,
178 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
179 $tbl_collation
182 if (count($table_alters) > 0) {
183 $sql_query = 'ALTER TABLE '
184 . Util::backquote($GLOBALS['table']);
185 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
186 $sql_query .= ';';
187 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
188 $reread_info = true;
189 unset($table_alters);
190 $warning_messages = $operations->getWarningMessagesArray();
193 if (isset($_REQUEST['tbl_collation'])
194 && ! empty($_REQUEST['tbl_collation'])
195 && isset($_REQUEST['change_all_collations'])
196 && ! empty($_REQUEST['change_all_collations'])
198 $operations->changeAllColumnsCollation(
199 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
204 * Reordering the table has been requested by the user
206 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
207 list($sql_query, $result) = $operations->getQueryAndResultForReorderingTable();
208 } // end if
211 * A partition operation has been requested by the user
213 if (isset($_REQUEST['submit_partition'])
214 && ! empty($_REQUEST['partition_operation'])
216 list($sql_query, $result) = $operations->getQueryAndResultForPartition();
217 } // end if
219 if ($reread_info) {
220 // to avoid showing the old value (for example the AUTO_INCREMENT) after
221 // a change, clear the cache
222 $GLOBALS['dbi']->clearTableCache();
223 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
224 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
225 if ($pma_table->isView()) {
226 $tbl_is_view = true;
227 $tbl_storage_engine = __('View');
228 $show_comment = null;
229 } else {
230 $tbl_is_view = false;
231 $tbl_storage_engine = $pma_table->getStorageEngine();
232 $show_comment = $pma_table->getComment();
234 $tbl_collation = $pma_table->getCollation();
235 $table_info_num_rows = $pma_table->getNumRows();
236 $row_format = $pma_table->getRowFormat();
237 $auto_increment = $pma_table->getAutoIncrement();
238 $create_options = $pma_table->getCreateOptions();
240 unset($reread_info);
242 if (isset($result) && empty($message_to_show)) {
243 if (empty($_message)) {
244 if (empty($sql_query)) {
245 $_message = Message::success(__('No change'));
246 } else {
247 $_message = $result
248 ? Message::success()
249 : Message::error();
252 if ($response->isAjax()) {
253 $response->setRequestStatus($_message->isSuccess());
254 $response->addJSON('message', $_message);
255 if (!empty($sql_query)) {
256 $response->addJSON(
257 'sql_query', Util::getMessage(null, $sql_query)
260 exit;
262 } else {
263 $_message = $result
264 ? Message::success($_message)
265 : Message::error($_message);
268 if (! empty($warning_messages)) {
269 $_message = new Message;
270 $_message->addMessagesString($warning_messages);
271 $_message->isError(true);
272 if ($response->isAjax()) {
273 $response->setRequestStatus(false);
274 $response->addJSON('message', $_message);
275 if (!empty($sql_query)) {
276 $response->addJSON(
277 'sql_query', Util::getMessage(null, $sql_query)
280 exit;
282 unset($warning_messages);
285 if (empty($sql_query)) {
286 $response->addHTML(
287 $_message->getDisplay()
289 } else {
290 $response->addHTML(
291 Util::getMessage($_message, $sql_query)
294 unset($_message);
297 $url_params['goto']
298 = $url_params['back']
299 = 'tbl_operations.php';
302 * Get columns names
304 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
307 * Displays the page
311 * Order the table
313 $hideOrderTable = false;
314 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
315 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
316 // InnoDB always orders table rows according to such an index if one is present.
317 if ($tbl_storage_engine == 'INNODB') {
318 $indexes = Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
319 foreach ($indexes as $name => $idx) {
320 if ($name == 'PRIMARY') {
321 $hideOrderTable = true;
322 break;
323 } elseif (! $idx->getNonUnique()) {
324 $notNull = true;
325 foreach ($idx->getColumns() as $column) {
326 if ($column->getNull()) {
327 $notNull = false;
328 break;
331 if ($notNull) {
332 $hideOrderTable = true;
333 break;
338 if (! $hideOrderTable) {
339 $response->addHTML($operations->getHtmlForOrderTheTable($columns));
343 * Move table
345 $response->addHTML($operations->getHtmlForMoveTable());
347 if (mb_strstr($show_comment, '; InnoDB free') === false) {
348 if (mb_strstr($show_comment, 'InnoDB free') === false) {
349 // only user entered comment
350 $comment = $show_comment;
351 } else {
352 // here we have just InnoDB generated part
353 $comment = '';
355 } else {
356 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
357 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
360 // PACK_KEYS: MyISAM or ISAM
361 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
362 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
364 // Here should be version check for InnoDB, however it is supported
365 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
366 // check for version
368 $response->addHTML(
369 $operations->getTableOptionDiv(
370 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
371 $create_options['pack_keys'],
372 $auto_increment,
373 (empty($create_options['delay_key_write']) ? '0' : '1'),
374 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
375 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
376 (empty($create_options['checksum']) ? '0' : '1')
381 * Copy table
383 $response->addHTML($operations->getHtmlForCopytable());
386 * Table maintenance
388 $response->addHTML(
389 $operations->getHtmlForTableMaintenance($pma_table, $url_params)
392 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
393 $truncate_table_url_params = array();
394 $drop_table_url_params = array();
396 if (! $tbl_is_view
397 && ! (isset($db_is_system_schema) && $db_is_system_schema)
399 $this_sql_query = 'TRUNCATE TABLE '
400 . Util::backquote($GLOBALS['table']);
401 $truncate_table_url_params = array_merge(
402 $url_params,
403 array(
404 'sql_query' => $this_sql_query,
405 'goto' => 'tbl_structure.php',
406 'reload' => '1',
407 'message_to_show' => sprintf(
408 __('Table %s has been emptied.'),
409 htmlspecialchars($table)
414 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
415 $this_sql_query = 'DROP TABLE '
416 . Util::backquote($GLOBALS['table']);
417 $drop_table_url_params = array_merge(
418 $url_params,
419 array(
420 'sql_query' => $this_sql_query,
421 'goto' => 'db_operations.php',
422 'reload' => '1',
423 'purge' => '1',
424 'message_to_show' => sprintf(
425 ($tbl_is_view
426 ? __('View %s has been dropped.')
427 : __('Table %s has been dropped.')
429 htmlspecialchars($table)
431 // table name is needed to avoid running
432 // PhpMyAdmin\RelationCleanup::database() on the whole db later
433 'table' => $GLOBALS['table'],
437 $response->addHTML(
438 $operations->getHtmlForDeleteDataOrTable(
439 $truncate_table_url_params,
440 $drop_table_url_params
445 if (Partition::havePartitioning()) {
446 $partition_names = Partition::getPartitionNames($db, $table);
447 // show the Partition maintenance section only if we detect a partition
448 if (! is_null($partition_names[0])) {
449 $response->addHTML(
450 $operations->getHtmlForPartitionMaintenance($partition_names, $url_params)
452 } // end if
453 } // end if
454 unset($partition_names);
456 // Referential integrity check
457 // The Referential integrity check was intended for the non-InnoDB
458 // tables for which the relations are defined in pmadb
459 // so I assume that if the current table is InnoDB, I don't display
460 // this choice (InnoDB maintains integrity by itself)
462 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
463 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
464 $foreign = $relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
466 if (! empty($foreign)) {
467 $response->addHTML(
468 $operations->getHtmlForReferentialIntegrityCheck($foreign, $url_params)
470 } // end if ($foreign)
472 } // end if (!empty($cfg['Server']['relation']))