Translated using Weblate (Slovenian)
[phpmyadmin.git] / tbl_operations.php
blob3c3754d7f4a638c441a8f786cdff280fdd730a6c
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;
11 /**
14 require_once 'libraries/common.inc.php';
16 /**
17 * functions implementation for this script
19 require_once 'libraries/check_user_privileges.lib.php';
20 require_once 'libraries/operations.lib.php';
22 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
24 /**
25 * Load JavaScript files
27 $response = PMA\libraries\Response::getInstance();
28 $header = $response->getHeader();
29 $scripts = $header->getScripts();
30 $scripts->addFile('tbl_operations.js');
32 /**
33 * Runs common work
35 require 'libraries/tbl_common.inc.php';
36 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
37 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
39 /**
40 * Gets relation settings
42 $cfgRelation = PMA_getRelationsParam();
44 /**
45 * Gets available MySQL charsets and storage engines
47 require_once 'libraries/mysql_charsets.inc.php';
49 // reselect current db (needed in some cases probably due to
50 // the calling of relation.lib.php)
51 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
53 /**
54 * Gets tables information
56 require 'libraries/tbl_info.inc.php';
58 // set initial value of these variables, based on the current table engine
59 list($is_myisam_or_aria, $is_innodb, $is_isam,
60 $is_berkeleydb, $is_aria, $is_pbxt
61 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
63 if ($is_aria) {
64 // the value for transactional can be implicit
65 // (no create option found, in this case it means 1)
66 // or explicit (option found with a value of 0 or 1)
67 // ($create_options['transactional'] may have been set by libraries/tbl_info.inc.php,
68 // from the $create_options)
69 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
70 ? '0'
71 : '1';
72 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
75 $reread_info = false;
76 $table_alters = array();
78 /**
79 * If the table has to be moved to some other database
81 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
82 //$_message = '';
83 PMA_moveOrCopyTable($db, $table);
84 // This was ended in an Ajax call
85 exit;
87 /**
88 * If the table has to be maintained
90 if (isset($_REQUEST['table_maintenance'])) {
91 include_once 'sql.php';
92 unset($result);
94 /**
95 * Updates table comment, type and options if required
97 if (isset($_REQUEST['submitoptions'])) {
98 $_message = '';
99 $warning_messages = array();
101 if (isset($_REQUEST['new_name'])) {
102 // Get original names before rename operation
103 $oldTable = $pma_table->getName();
104 $oldDb = $pma_table->getDbName();
106 if ($pma_table->rename($_REQUEST['new_name'])) {
107 if (isset($_REQUEST['adjust_privileges'])
108 && ! empty($_REQUEST['adjust_privileges'])
110 PMA_AdjustPrivileges_renameOrMoveTable(
111 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
115 // Reselect the original DB
116 $GLOBALS['db'] = $oldDb;
117 $GLOBALS['dbi']->selectDb($oldDb);
119 $_message .= $pma_table->getLastMessage();
120 $result = true;
121 $GLOBALS['table'] = $pma_table->getName();
122 $reread_info = true;
123 $reload = true;
124 } else {
125 $_message .= $pma_table->getLastError();
126 $result = false;
130 if (! empty($_REQUEST['new_tbl_storage_engine'])
131 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
133 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
134 // reset the globals for the new engine
135 list($is_myisam_or_aria, $is_innodb, $is_isam,
136 $is_berkeleydb, $is_aria, $is_pbxt
137 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
139 if ($is_aria) {
140 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
141 ? '0'
142 : '1';
143 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
145 } else {
146 $new_tbl_storage_engine = '';
149 $table_alters = PMA_getTableAltersArray(
150 $is_myisam_or_aria, $is_isam, $create_options['pack_keys'],
151 (empty($create_options['checksum']) ? '0' : '1'),
152 $is_aria,
153 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
154 (empty($create_options['delay_key_write']) ? '0' : '1'),
155 $is_innodb, $is_pbxt, $create_options['row_format'],
156 $new_tbl_storage_engine,
157 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
158 $tbl_collation
161 if (count($table_alters) > 0) {
162 $sql_query = 'ALTER TABLE '
163 . PMA\libraries\Util::backquote($GLOBALS['table']);
164 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
165 $sql_query .= ';';
166 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
167 $reread_info = true;
168 unset($table_alters);
169 $warning_messages = PMA_getWarningMessagesArray();
172 if (isset($_REQUEST['tbl_collation'])
173 && ! empty($_REQUEST['tbl_collation'])
174 && isset($_REQUEST['change_all_collations'])
175 && ! empty($_REQUEST['change_all_collations'])
177 PMA_changeAllColumnsCollation(
178 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
183 * Reordering the table has been requested by the user
185 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
186 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
187 } // end if
190 * A partition operation has been requested by the user
192 if (isset($_REQUEST['submit_partition'])
193 && ! empty($_REQUEST['partition_operation'])
195 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
196 } // end if
198 if ($reread_info) {
199 // to avoid showing the old value (for example the AUTO_INCREMENT) after
200 // a change, clear the cache
201 $GLOBALS['dbi']->clearTableCache();
202 include 'libraries/tbl_info.inc.php';
204 unset($reread_info);
206 if (isset($result) && empty($message_to_show)) {
207 if (empty($_message)) {
208 if (empty($sql_query)) {
209 $_message = PMA\libraries\Message::success(__('No change'));
210 } else {
211 $_message = $result
212 ? PMA\libraries\Message::success()
213 : PMA\libraries\Message::error();
216 if (isset($GLOBALS['ajax_request'])
217 && $GLOBALS['ajax_request'] == true
219 $response = PMA\libraries\Response::getInstance();
220 $response->setRequestStatus($_message->isSuccess());
221 $response->addJSON('message', $_message);
222 if (!empty($sql_query)) {
223 $response->addJSON(
224 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
227 exit;
229 } else {
230 $_message = $result
231 ? PMA\libraries\Message::success($_message)
232 : PMA\libraries\Message::error($_message);
235 if (! empty($warning_messages)) {
236 $_message = new PMA\libraries\Message;
237 $_message->addMessages($warning_messages);
238 $_message->isError(true);
239 if (isset($GLOBALS['ajax_request'])
240 && $GLOBALS['ajax_request'] == true
242 $response = PMA\libraries\Response::getInstance();
243 $response->setRequestStatus(false);
244 $response->addJSON('message', $_message);
245 if (!empty($sql_query)) {
246 $response->addJSON(
247 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
250 exit;
252 unset($warning_messages);
255 if (empty($sql_query)) {
256 $response->addHTML(
257 $_message->getDisplay()
259 } else {
260 $response->addHTML(
261 PMA\libraries\Util::getMessage($_message, $sql_query)
264 unset($_message);
267 $url_params['goto']
268 = $url_params['back']
269 = 'tbl_operations.php';
272 * Get columns names
274 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
277 * Displays the page
279 $response->addHTML('<div id="boxContainer" data-box-width="300">');
282 * Order the table
284 $hideOrderTable = false;
285 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
286 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
287 // InnoDB always orders table rows according to such an index if one is present.
288 if ($tbl_storage_engine == 'INNODB') {
289 $indexes = PMA\libraries\Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
290 foreach ($indexes as $name => $idx) {
291 if ($name == 'PRIMARY') {
292 $hideOrderTable = true;
293 break;
294 } elseif (! $idx->getNonUnique()) {
295 $notNull = true;
296 foreach ($idx->getColumns() as $column) {
297 if ($column->getNull()) {
298 $notNull = false;
299 break;
302 if ($notNull) {
303 $hideOrderTable = true;
304 break;
309 if (! $hideOrderTable) {
310 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
314 * Move table
316 $response->addHTML(PMA_getHtmlForMoveTable());
318 if (mb_strstr($show_comment, '; InnoDB free') === false) {
319 if (mb_strstr($show_comment, 'InnoDB free') === false) {
320 // only user entered comment
321 $comment = $show_comment;
322 } else {
323 // here we have just InnoDB generated part
324 $comment = '';
326 } else {
327 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
328 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
331 // PACK_KEYS: MyISAM or ISAM
332 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
333 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
335 // Here should be version check for InnoDB, however it is supported
336 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
337 // check for version
339 $response->addHTML(
340 PMA_getTableOptionDiv(
341 $comment, $tbl_collation, $tbl_storage_engine,
342 $is_myisam_or_aria, $is_isam, $create_options['pack_keys'],
343 $auto_increment,
344 (empty($create_options['delay_key_write']) ? '0' : '1'),
345 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
346 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
347 $is_innodb, $is_pbxt, $is_aria, (empty($create_options['checksum']) ? '0' : '1')
352 * Copy table
354 $response->addHTML(PMA_getHtmlForCopytable());
357 * Table maintenance
359 $response->addHTML(
360 PMA_getHtmlForTableMaintenance(
361 $is_myisam_or_aria,
362 $is_innodb,
363 $is_berkeleydb,
364 $url_params
368 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
369 $truncate_table_url_params = array();
370 $drop_table_url_params = array();
372 if (! $tbl_is_view
373 && ! (isset($db_is_system_schema) && $db_is_system_schema)
375 $this_sql_query = 'TRUNCATE TABLE '
376 . PMA\libraries\Util::backquote($GLOBALS['table']);
377 $truncate_table_url_params = array_merge(
378 $url_params,
379 array(
380 'sql_query' => $this_sql_query,
381 'goto' => 'tbl_structure.php',
382 'reload' => '1',
383 'message_to_show' => sprintf(
384 __('Table %s has been emptied.'),
385 htmlspecialchars($table)
390 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
391 $this_sql_query = 'DROP TABLE '
392 . PMA\libraries\Util::backquote($GLOBALS['table']);
393 $drop_table_url_params = array_merge(
394 $url_params,
395 array(
396 'sql_query' => $this_sql_query,
397 'goto' => 'db_operations.php',
398 'reload' => '1',
399 'purge' => '1',
400 'message_to_show' => sprintf(
401 ($tbl_is_view
402 ? __('View %s has been dropped.')
403 : __('Table %s has been dropped.')
405 htmlspecialchars($table)
407 // table name is needed to avoid running
408 // PMA_relationsCleanupDatabase() on the whole db later
409 'table' => $GLOBALS['table'],
413 $response->addHTML(
414 PMA_getHtmlForDeleteDataOrTable(
415 $truncate_table_url_params,
416 $drop_table_url_params
421 if (Partition::havePartitioning()) {
422 $partition_names = Partition::getPartitionNames($db, $table);
423 // show the Partition maintenance section only if we detect a partition
424 if (! is_null($partition_names[0])) {
425 $response->addHTML(
426 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
428 } // end if
429 } // end if
430 unset($partition_names);
432 // Referential integrity check
433 // The Referential integrity check was intended for the non-InnoDB
434 // tables for which the relations are defined in pmadb
435 // so I assume that if the current table is InnoDB, I don't display
436 // this choice (InnoDB maintains integrity by itself)
438 if ($cfgRelation['relwork'] && ! $is_innodb) {
439 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
440 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
442 if (! empty($foreign)) {
443 $response->addHTML(
444 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
446 } // end if ($foreign)
448 } // end if (!empty($cfg['Server']['relation']))
450 $response->addHTML('</div>');