Translated using Weblate (Slovenian)
[phpmyadmin.git] / tbl_operations.php
blob1996e085ddd8c063c3876c0d02b9d8b4ecd555d7
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 $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 . PMA\libraries\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 = PMA\libraries\Message::success(__('No change'));
239 } else {
240 $_message = $result
241 ? PMA\libraries\Message::success()
242 : PMA\libraries\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', PMA\libraries\Util::getMessage(null, $sql_query)
253 exit;
255 } else {
256 $_message = $result
257 ? PMA\libraries\Message::success($_message)
258 : PMA\libraries\Message::error($_message);
261 if (! empty($warning_messages)) {
262 $_message = new PMA\libraries\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', PMA\libraries\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 PMA\libraries\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
302 $response->addHTML('<div id="boxContainer" data-box-width="300">');
305 * Order the table
307 $hideOrderTable = false;
308 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
309 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
310 // InnoDB always orders table rows according to such an index if one is present.
311 if ($tbl_storage_engine == 'INNODB') {
312 $indexes = PMA\libraries\Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
313 foreach ($indexes as $name => $idx) {
314 if ($name == 'PRIMARY') {
315 $hideOrderTable = true;
316 break;
317 } elseif (! $idx->getNonUnique()) {
318 $notNull = true;
319 foreach ($idx->getColumns() as $column) {
320 if ($column->getNull()) {
321 $notNull = false;
322 break;
325 if ($notNull) {
326 $hideOrderTable = true;
327 break;
332 if (! $hideOrderTable) {
333 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
337 * Move table
339 $response->addHTML(PMA_getHtmlForMoveTable());
341 if (mb_strstr($show_comment, '; InnoDB free') === false) {
342 if (mb_strstr($show_comment, 'InnoDB free') === false) {
343 // only user entered comment
344 $comment = $show_comment;
345 } else {
346 // here we have just InnoDB generated part
347 $comment = '';
349 } else {
350 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
351 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
354 // PACK_KEYS: MyISAM or ISAM
355 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
356 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
358 // Here should be version check for InnoDB, however it is supported
359 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
360 // check for version
362 $response->addHTML(
363 PMA_getTableOptionDiv(
364 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
365 $create_options['pack_keys'],
366 $auto_increment,
367 (empty($create_options['delay_key_write']) ? '0' : '1'),
368 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
369 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
370 (empty($create_options['checksum']) ? '0' : '1')
375 * Copy table
377 $response->addHTML(PMA_getHtmlForCopytable());
380 * Table maintenance
382 $response->addHTML(
383 PMA_getHtmlForTableMaintenance($pma_table, $url_params)
386 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
387 $truncate_table_url_params = array();
388 $drop_table_url_params = array();
390 if (! $tbl_is_view
391 && ! (isset($db_is_system_schema) && $db_is_system_schema)
393 $this_sql_query = 'TRUNCATE TABLE '
394 . PMA\libraries\Util::backquote($GLOBALS['table']);
395 $truncate_table_url_params = array_merge(
396 $url_params,
397 array(
398 'sql_query' => $this_sql_query,
399 'goto' => 'tbl_structure.php',
400 'reload' => '1',
401 'message_to_show' => sprintf(
402 __('Table %s has been emptied.'),
403 htmlspecialchars($table)
408 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
409 $this_sql_query = 'DROP TABLE '
410 . PMA\libraries\Util::backquote($GLOBALS['table']);
411 $drop_table_url_params = array_merge(
412 $url_params,
413 array(
414 'sql_query' => $this_sql_query,
415 'goto' => 'db_operations.php',
416 'reload' => '1',
417 'purge' => '1',
418 'message_to_show' => sprintf(
419 ($tbl_is_view
420 ? __('View %s has been dropped.')
421 : __('Table %s has been dropped.')
423 htmlspecialchars($table)
425 // table name is needed to avoid running
426 // PMA_relationsCleanupDatabase() on the whole db later
427 'table' => $GLOBALS['table'],
431 $response->addHTML(
432 PMA_getHtmlForDeleteDataOrTable(
433 $truncate_table_url_params,
434 $drop_table_url_params
439 if (Partition::havePartitioning()) {
440 $partition_names = Partition::getPartitionNames($db, $table);
441 // show the Partition maintenance section only if we detect a partition
442 if (! is_null($partition_names[0])) {
443 $response->addHTML(
444 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
446 } // end if
447 } // end if
448 unset($partition_names);
450 // Referential integrity check
451 // The Referential integrity check was intended for the non-InnoDB
452 // tables for which the relations are defined in pmadb
453 // so I assume that if the current table is InnoDB, I don't display
454 // this choice (InnoDB maintains integrity by itself)
456 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
457 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
458 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
460 if (! empty($foreign)) {
461 $response->addHTML(
462 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
464 } // end if ($foreign)
466 } // end if (!empty($cfg['Server']['relation']))
468 $response->addHTML('</div>');