Translated using Weblate (Danish)
[phpmyadmin.git] / tbl_operations.php
blobd930576d191d36a67797ab96044103f49bc848e3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Various table operations
6 * @package PhpMyAdmin
7 */
8 use PMA\libraries\Partition;
9 use PMA\libraries\Table;
10 use PMA\libraries\Response;
12 /**
15 require_once 'libraries/common.inc.php';
17 /**
18 * functions implementation for this script
20 require_once 'libraries/check_user_privileges.lib.php';
21 require_once 'libraries/operations.lib.php';
23 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
25 /**
26 * Load JavaScript files
28 $response = Response::getInstance();
29 $header = $response->getHeader();
30 $scripts = $header->getScripts();
31 $scripts->addFile('tbl_operations.js');
33 /**
34 * Runs common work
36 require 'libraries/tbl_common.inc.php';
37 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
38 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
40 /**
41 * Gets relation settings
43 $cfgRelation = PMA_getRelationsParam();
45 // reselect current db (needed in some cases probably due to
46 // the calling of relation.lib.php)
47 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
49 /**
50 * Gets tables information
52 require 'libraries/tbl_info.inc.php';
54 // set initial value of these variables, based on the current table engine
55 if ($pma_table->isEngine('ARIA')) {
56 // the value for transactional can be implicit
57 // (no create option found, in this case it means 1)
58 // or explicit (option found with a value of 0 or 1)
59 // ($create_options['transactional'] may have been set by libraries/tbl_info.inc.php,
60 // from the $create_options)
61 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
62 ? '0'
63 : '1';
64 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
67 $reread_info = false;
68 $table_alters = array();
70 /**
71 * If the table has to be moved to some other database
73 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
74 //$_message = '';
75 PMA_moveOrCopyTable($db, $table);
76 // This was ended in an Ajax call
77 exit;
79 /**
80 * If the table has to be maintained
82 if (isset($_REQUEST['table_maintenance'])) {
83 include_once 'sql.php';
84 unset($result);
86 /**
87 * Updates table comment, type and options if required
89 if (isset($_REQUEST['submitoptions'])) {
90 $_message = '';
91 $warning_messages = array();
93 if (isset($_REQUEST['new_name'])) {
94 // Get original names before rename operation
95 $oldTable = $pma_table->getName();
96 $oldDb = $pma_table->getDbName();
98 if ($pma_table->rename($_REQUEST['new_name'])) {
99 if (isset($_REQUEST['adjust_privileges'])
100 && ! empty($_REQUEST['adjust_privileges'])
102 PMA_AdjustPrivileges_renameOrMoveTable(
103 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
107 // Reselect the original DB
108 $GLOBALS['db'] = $oldDb;
109 $GLOBALS['dbi']->selectDb($oldDb);
111 $_message .= $pma_table->getLastMessage();
112 $result = true;
113 $GLOBALS['table'] = $pma_table->getName();
114 $reread_info = true;
115 $reload = true;
116 } else {
117 $_message .= $pma_table->getLastError();
118 $result = false;
122 if (! empty($_REQUEST['new_tbl_storage_engine'])
123 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
125 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
127 if ($pma_table->isEngine('ARIA')) {
128 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
129 ? '0'
130 : '1';
131 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
133 } else {
134 $new_tbl_storage_engine = '';
137 $row_format = (isset($create_options['row_format']))
138 ? $create_options['row_format']
139 : $pma_table->getStatusInfo('ROW_FORMAT');
141 $table_alters = PMA_getTableAltersArray(
142 $pma_table,
143 $create_options['pack_keys'],
144 (empty($create_options['checksum']) ? '0' : '1'),
145 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
146 (empty($create_options['delay_key_write']) ? '0' : '1'),
147 $row_format,
148 $new_tbl_storage_engine,
149 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
150 $tbl_collation
153 if (count($table_alters) > 0) {
154 $sql_query = 'ALTER TABLE '
155 . PMA\libraries\Util::backquote($GLOBALS['table']);
156 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
157 $sql_query .= ';';
158 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
159 $reread_info = true;
160 unset($table_alters);
161 $warning_messages = PMA_getWarningMessagesArray();
164 if (isset($_REQUEST['tbl_collation'])
165 && ! empty($_REQUEST['tbl_collation'])
166 && isset($_REQUEST['change_all_collations'])
167 && ! empty($_REQUEST['change_all_collations'])
169 PMA_changeAllColumnsCollation(
170 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
175 * Reordering the table has been requested by the user
177 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
178 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
179 } // end if
182 * A partition operation has been requested by the user
184 if (isset($_REQUEST['submit_partition'])
185 && ! empty($_REQUEST['partition_operation'])
187 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
188 } // end if
190 if ($reread_info) {
191 // to avoid showing the old value (for example the AUTO_INCREMENT) after
192 // a change, clear the cache
193 $GLOBALS['dbi']->clearTableCache();
194 include 'libraries/tbl_info.inc.php';
196 unset($reread_info);
198 if (isset($result) && empty($message_to_show)) {
199 if (empty($_message)) {
200 if (empty($sql_query)) {
201 $_message = PMA\libraries\Message::success(__('No change'));
202 } else {
203 $_message = $result
204 ? PMA\libraries\Message::success()
205 : PMA\libraries\Message::error();
208 if ($response->isAjax()) {
209 $response->setRequestStatus($_message->isSuccess());
210 $response->addJSON('message', $_message);
211 if (!empty($sql_query)) {
212 $response->addJSON(
213 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
216 exit;
218 } else {
219 $_message = $result
220 ? PMA\libraries\Message::success($_message)
221 : PMA\libraries\Message::error($_message);
224 if (! empty($warning_messages)) {
225 $_message = new PMA\libraries\Message;
226 $_message->addMessagesString($warning_messages);
227 $_message->isError(true);
228 if ($response->isAjax()) {
229 $response->setRequestStatus(false);
230 $response->addJSON('message', $_message);
231 if (!empty($sql_query)) {
232 $response->addJSON(
233 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
236 exit;
238 unset($warning_messages);
241 if (empty($sql_query)) {
242 $response->addHTML(
243 $_message->getDisplay()
245 } else {
246 $response->addHTML(
247 PMA\libraries\Util::getMessage($_message, $sql_query)
250 unset($_message);
253 $url_params['goto']
254 = $url_params['back']
255 = 'tbl_operations.php';
258 * Get columns names
260 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
263 * Displays the page
265 $response->addHTML('<div id="boxContainer" data-box-width="300">');
268 * Order the table
270 $hideOrderTable = false;
271 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
272 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
273 // InnoDB always orders table rows according to such an index if one is present.
274 if ($tbl_storage_engine == 'INNODB') {
275 $indexes = PMA\libraries\Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
276 foreach ($indexes as $name => $idx) {
277 if ($name == 'PRIMARY') {
278 $hideOrderTable = true;
279 break;
280 } elseif (! $idx->getNonUnique()) {
281 $notNull = true;
282 foreach ($idx->getColumns() as $column) {
283 if ($column->getNull()) {
284 $notNull = false;
285 break;
288 if ($notNull) {
289 $hideOrderTable = true;
290 break;
295 if (! $hideOrderTable) {
296 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
300 * Move table
302 $response->addHTML(PMA_getHtmlForMoveTable());
304 if (mb_strstr($show_comment, '; InnoDB free') === false) {
305 if (mb_strstr($show_comment, 'InnoDB free') === false) {
306 // only user entered comment
307 $comment = $show_comment;
308 } else {
309 // here we have just InnoDB generated part
310 $comment = '';
312 } else {
313 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
314 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
317 // PACK_KEYS: MyISAM or ISAM
318 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
319 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
321 // Here should be version check for InnoDB, however it is supported
322 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
323 // check for version
325 $response->addHTML(
326 PMA_getTableOptionDiv(
327 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
328 $create_options['pack_keys'],
329 $auto_increment,
330 (empty($create_options['delay_key_write']) ? '0' : '1'),
331 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
332 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
333 (empty($create_options['checksum']) ? '0' : '1')
338 * Copy table
340 $response->addHTML(PMA_getHtmlForCopytable());
343 * Table maintenance
345 $response->addHTML(
346 PMA_getHtmlForTableMaintenance($pma_table, $url_params)
349 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
350 $truncate_table_url_params = array();
351 $drop_table_url_params = array();
353 if (! $tbl_is_view
354 && ! (isset($db_is_system_schema) && $db_is_system_schema)
356 $this_sql_query = 'TRUNCATE TABLE '
357 . PMA\libraries\Util::backquote($GLOBALS['table']);
358 $truncate_table_url_params = array_merge(
359 $url_params,
360 array(
361 'sql_query' => $this_sql_query,
362 'goto' => 'tbl_structure.php',
363 'reload' => '1',
364 'message_to_show' => sprintf(
365 __('Table %s has been emptied.'),
366 htmlspecialchars($table)
371 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
372 $this_sql_query = 'DROP TABLE '
373 . PMA\libraries\Util::backquote($GLOBALS['table']);
374 $drop_table_url_params = array_merge(
375 $url_params,
376 array(
377 'sql_query' => $this_sql_query,
378 'goto' => 'db_operations.php',
379 'reload' => '1',
380 'purge' => '1',
381 'message_to_show' => sprintf(
382 ($tbl_is_view
383 ? __('View %s has been dropped.')
384 : __('Table %s has been dropped.')
386 htmlspecialchars($table)
388 // table name is needed to avoid running
389 // PMA_relationsCleanupDatabase() on the whole db later
390 'table' => $GLOBALS['table'],
394 $response->addHTML(
395 PMA_getHtmlForDeleteDataOrTable(
396 $truncate_table_url_params,
397 $drop_table_url_params
402 if (Partition::havePartitioning()) {
403 $partition_names = Partition::getPartitionNames($db, $table);
404 // show the Partition maintenance section only if we detect a partition
405 if (! is_null($partition_names[0])) {
406 $response->addHTML(
407 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
409 } // end if
410 } // end if
411 unset($partition_names);
413 // Referential integrity check
414 // The Referential integrity check was intended for the non-InnoDB
415 // tables for which the relations are defined in pmadb
416 // so I assume that if the current table is InnoDB, I don't display
417 // this choice (InnoDB maintains integrity by itself)
419 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
420 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
421 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
423 if (! empty($foreign)) {
424 $response->addHTML(
425 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
427 } // end if ($foreign)
429 } // end if (!empty($cfg['Server']['relation']))
431 $response->addHTML('</div>');