Translated using Weblate (German)
[phpmyadmin.git] / tbl_operations.php
blob3208a20e541fd965f97e658e26129ad2f7411691
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Various table operations
6 * @package PhpMyAdmin
7 */
8 use PhpMyAdmin\Partition;
9 use PhpMyAdmin\Table;
10 use PhpMyAdmin\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 $pma_table = $GLOBALS['dbi']->getTable(
53 $GLOBALS['db'],
54 $GLOBALS['table']
56 $reread_info = $pma_table->getStatusInfo(null, false);
57 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ? true : false));
58 if ($pma_table->isView()) {
59 $tbl_is_view = true;
60 $tbl_storage_engine = __('View');
61 $show_comment = null;
62 } else {
63 $tbl_is_view = false;
64 $tbl_storage_engine = $pma_table->getStorageEngine();
65 $show_comment = $pma_table->getComment();
67 $tbl_collation = $pma_table->getCollation();
68 $table_info_num_rows = $pma_table->getNumRows();
69 $row_format = $pma_table->getRowFormat();
70 $auto_increment = $pma_table->getAutoIncrement();
71 $create_options = $pma_table->getCreateOptions();
73 // set initial value of these variables, based on the current table engine
74 if ($pma_table->isEngine('ARIA')) {
75 // the value for transactional can be implicit
76 // (no create option found, in this case it means 1)
77 // or explicit (option found with a value of 0 or 1)
78 // ($create_options['transactional'] may have been set by Table class,
79 // from the $create_options)
80 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
81 ? '0'
82 : '1';
83 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
86 $pma_table = $GLOBALS['dbi']->getTable(
87 $GLOBALS['db'],
88 $GLOBALS['table']
90 $reread_info = false;
91 $table_alters = array();
93 /**
94 * If the table has to be moved to some other database
96 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
97 //$_message = '';
98 PMA_moveOrCopyTable($db, $table);
99 // This was ended in an Ajax call
100 exit;
103 * If the table has to be maintained
105 if (isset($_REQUEST['table_maintenance'])) {
106 include_once 'sql.php';
107 unset($result);
110 * Updates table comment, type and options if required
112 if (isset($_REQUEST['submitoptions'])) {
113 $_message = '';
114 $warning_messages = array();
116 if (isset($_REQUEST['new_name'])) {
117 // Get original names before rename operation
118 $oldTable = $pma_table->getName();
119 $oldDb = $pma_table->getDbName();
121 if ($pma_table->rename($_REQUEST['new_name'])) {
122 if (isset($_REQUEST['adjust_privileges'])
123 && ! empty($_REQUEST['adjust_privileges'])
125 PMA_AdjustPrivileges_renameOrMoveTable(
126 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
130 // Reselect the original DB
131 $GLOBALS['db'] = $oldDb;
132 $GLOBALS['dbi']->selectDb($oldDb);
133 $_message .= $pma_table->getLastMessage();
134 $result = true;
135 $GLOBALS['table'] = $pma_table->getName();
136 $reread_info = true;
137 $reload = true;
138 } else {
139 $_message .= $pma_table->getLastError();
140 $result = false;
144 if (! empty($_REQUEST['new_tbl_storage_engine'])
145 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
147 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
149 if ($pma_table->isEngine('ARIA')) {
150 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
151 ? '0'
152 : '1';
153 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
155 } else {
156 $new_tbl_storage_engine = '';
159 $row_format = (isset($create_options['row_format']))
160 ? $create_options['row_format']
161 : $pma_table->getRowFormat();
163 $table_alters = PMA_getTableAltersArray(
164 $pma_table,
165 $create_options['pack_keys'],
166 (empty($create_options['checksum']) ? '0' : '1'),
167 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
168 (empty($create_options['delay_key_write']) ? '0' : '1'),
169 $row_format,
170 $new_tbl_storage_engine,
171 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
172 $tbl_collation
175 if (count($table_alters) > 0) {
176 $sql_query = 'ALTER TABLE '
177 . PhpMyAdmin\Util::backquote($GLOBALS['table']);
178 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
179 $sql_query .= ';';
180 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
181 $reread_info = true;
182 unset($table_alters);
183 $warning_messages = PMA_getWarningMessagesArray();
186 if (isset($_REQUEST['tbl_collation'])
187 && ! empty($_REQUEST['tbl_collation'])
188 && isset($_REQUEST['change_all_collations'])
189 && ! empty($_REQUEST['change_all_collations'])
191 PMA_changeAllColumnsCollation(
192 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
197 * Reordering the table has been requested by the user
199 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
200 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
201 } // end if
204 * A partition operation has been requested by the user
206 if (isset($_REQUEST['submit_partition'])
207 && ! empty($_REQUEST['partition_operation'])
209 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
210 } // end if
212 if ($reread_info) {
213 // to avoid showing the old value (for example the AUTO_INCREMENT) after
214 // a change, clear the cache
215 $GLOBALS['dbi']->clearTableCache();
216 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
217 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
218 if ($pma_table->isView()) {
219 $tbl_is_view = true;
220 $tbl_storage_engine = __('View');
221 $show_comment = null;
222 } else {
223 $tbl_is_view = false;
224 $tbl_storage_engine = $pma_table->getStorageEngine();
225 $show_comment = $pma_table->getComment();
227 $tbl_collation = $pma_table->getCollation();
228 $table_info_num_rows = $pma_table->getNumRows();
229 $row_format = $pma_table->getRowFormat();
230 $auto_increment = $pma_table->getAutoIncrement();
231 $create_options = $pma_table->getCreateOptions();
233 unset($reread_info);
235 if (isset($result) && empty($message_to_show)) {
236 if (empty($_message)) {
237 if (empty($sql_query)) {
238 $_message = PhpMyAdmin\Message::success(__('No change'));
239 } else {
240 $_message = $result
241 ? PhpMyAdmin\Message::success()
242 : PhpMyAdmin\Message::error();
245 if ($response->isAjax()) {
246 $response->setRequestStatus($_message->isSuccess());
247 $response->addJSON('message', $_message);
248 if (!empty($sql_query)) {
249 $response->addJSON(
250 'sql_query', PhpMyAdmin\Util::getMessage(null, $sql_query)
253 exit;
255 } else {
256 $_message = $result
257 ? PhpMyAdmin\Message::success($_message)
258 : PhpMyAdmin\Message::error($_message);
261 if (! empty($warning_messages)) {
262 $_message = new PhpMyAdmin\Message;
263 $_message->addMessagesString($warning_messages);
264 $_message->isError(true);
265 if ($response->isAjax()) {
266 $response->setRequestStatus(false);
267 $response->addJSON('message', $_message);
268 if (!empty($sql_query)) {
269 $response->addJSON(
270 'sql_query', PhpMyAdmin\Util::getMessage(null, $sql_query)
273 exit;
275 unset($warning_messages);
278 if (empty($sql_query)) {
279 $response->addHTML(
280 $_message->getDisplay()
282 } else {
283 $response->addHTML(
284 PhpMyAdmin\Util::getMessage($_message, $sql_query)
287 unset($_message);
290 $url_params['goto']
291 = $url_params['back']
292 = 'tbl_operations.php';
295 * Get columns names
297 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
300 * Displays the page
304 * Order the table
306 $hideOrderTable = false;
307 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
308 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
309 // InnoDB always orders table rows according to such an index if one is present.
310 if ($tbl_storage_engine == 'INNODB') {
311 $indexes = PhpMyAdmin\Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
312 foreach ($indexes as $name => $idx) {
313 if ($name == 'PRIMARY') {
314 $hideOrderTable = true;
315 break;
316 } elseif (! $idx->getNonUnique()) {
317 $notNull = true;
318 foreach ($idx->getColumns() as $column) {
319 if ($column->getNull()) {
320 $notNull = false;
321 break;
324 if ($notNull) {
325 $hideOrderTable = true;
326 break;
331 if (! $hideOrderTable) {
332 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
336 * Move table
338 $response->addHTML(PMA_getHtmlForMoveTable());
340 if (mb_strstr($show_comment, '; InnoDB free') === false) {
341 if (mb_strstr($show_comment, 'InnoDB free') === false) {
342 // only user entered comment
343 $comment = $show_comment;
344 } else {
345 // here we have just InnoDB generated part
346 $comment = '';
348 } else {
349 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
350 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
353 // PACK_KEYS: MyISAM or ISAM
354 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
355 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
357 // Here should be version check for InnoDB, however it is supported
358 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
359 // check for version
361 $response->addHTML(
362 PMA_getTableOptionDiv(
363 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
364 $create_options['pack_keys'],
365 $auto_increment,
366 (empty($create_options['delay_key_write']) ? '0' : '1'),
367 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
368 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
369 (empty($create_options['checksum']) ? '0' : '1')
374 * Copy table
376 $response->addHTML(PMA_getHtmlForCopytable());
379 * Table maintenance
381 $response->addHTML(
382 PMA_getHtmlForTableMaintenance($pma_table, $url_params)
385 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
386 $truncate_table_url_params = array();
387 $drop_table_url_params = array();
389 if (! $tbl_is_view
390 && ! (isset($db_is_system_schema) && $db_is_system_schema)
392 $this_sql_query = 'TRUNCATE TABLE '
393 . PhpMyAdmin\Util::backquote($GLOBALS['table']);
394 $truncate_table_url_params = array_merge(
395 $url_params,
396 array(
397 'sql_query' => $this_sql_query,
398 'goto' => 'tbl_structure.php',
399 'reload' => '1',
400 'message_to_show' => sprintf(
401 __('Table %s has been emptied.'),
402 htmlspecialchars($table)
407 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
408 $this_sql_query = 'DROP TABLE '
409 . PhpMyAdmin\Util::backquote($GLOBALS['table']);
410 $drop_table_url_params = array_merge(
411 $url_params,
412 array(
413 'sql_query' => $this_sql_query,
414 'goto' => 'db_operations.php',
415 'reload' => '1',
416 'purge' => '1',
417 'message_to_show' => sprintf(
418 ($tbl_is_view
419 ? __('View %s has been dropped.')
420 : __('Table %s has been dropped.')
422 htmlspecialchars($table)
424 // table name is needed to avoid running
425 // PMA_relationsCleanupDatabase() on the whole db later
426 'table' => $GLOBALS['table'],
430 $response->addHTML(
431 PMA_getHtmlForDeleteDataOrTable(
432 $truncate_table_url_params,
433 $drop_table_url_params
438 if (Partition::havePartitioning()) {
439 $partition_names = Partition::getPartitionNames($db, $table);
440 // show the Partition maintenance section only if we detect a partition
441 if (! is_null($partition_names[0])) {
442 $response->addHTML(
443 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
445 } // end if
446 } // end if
447 unset($partition_names);
449 // Referential integrity check
450 // The Referential integrity check was intended for the non-InnoDB
451 // tables for which the relations are defined in pmadb
452 // so I assume that if the current table is InnoDB, I don't display
453 // this choice (InnoDB maintains integrity by itself)
455 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
456 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
457 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
459 if (! empty($foreign)) {
460 $response->addHTML(
461 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
463 } // end if ($foreign)
465 } // end if (!empty($cfg['Server']['relation']))