Translated using Weblate (Slovenian)
[phpmyadmin.git] / tbl_operations.php
blob2057c9e7b7b860faa396f00f9df2d64ad79d6561
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 // reselect current db (needed in some cases probably due to
45 // the calling of relation.lib.php)
46 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
48 /**
49 * Gets tables information
51 require 'libraries/tbl_info.inc.php';
53 // set initial value of these variables, based on the current table engine
54 list($is_myisam_or_aria, $is_innodb, $is_isam,
55 $is_berkeleydb, $is_aria, $is_pbxt
56 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
58 if ($is_aria) {
59 // the value for transactional can be implicit
60 // (no create option found, in this case it means 1)
61 // or explicit (option found with a value of 0 or 1)
62 // ($create_options['transactional'] may have been set by libraries/tbl_info.inc.php,
63 // from the $create_options)
64 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
65 ? '0'
66 : '1';
67 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
70 $reread_info = false;
71 $table_alters = array();
73 /**
74 * If the table has to be moved to some other database
76 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
77 //$_message = '';
78 PMA_moveOrCopyTable($db, $table);
79 // This was ended in an Ajax call
80 exit;
82 /**
83 * If the table has to be maintained
85 if (isset($_REQUEST['table_maintenance'])) {
86 include_once 'sql.php';
87 unset($result);
89 /**
90 * Updates table comment, type and options if required
92 if (isset($_REQUEST['submitoptions'])) {
93 $_message = '';
94 $warning_messages = array();
96 if (isset($_REQUEST['new_name'])) {
97 // Get original names before rename operation
98 $oldTable = $pma_table->getName();
99 $oldDb = $pma_table->getDbName();
101 if ($pma_table->rename($_REQUEST['new_name'])) {
102 if (isset($_REQUEST['adjust_privileges'])
103 && ! empty($_REQUEST['adjust_privileges'])
105 PMA_AdjustPrivileges_renameOrMoveTable(
106 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
110 // Reselect the original DB
111 $GLOBALS['db'] = $oldDb;
112 $GLOBALS['dbi']->selectDb($oldDb);
114 $_message .= $pma_table->getLastMessage();
115 $result = true;
116 $GLOBALS['table'] = $pma_table->getName();
117 $reread_info = true;
118 $reload = true;
119 } else {
120 $_message .= $pma_table->getLastError();
121 $result = false;
125 if (! empty($_REQUEST['new_tbl_storage_engine'])
126 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
128 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
129 // reset the globals for the new engine
130 list($is_myisam_or_aria, $is_innodb, $is_isam,
131 $is_berkeleydb, $is_aria, $is_pbxt
132 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
134 if ($is_aria) {
135 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
136 ? '0'
137 : '1';
138 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
140 } else {
141 $new_tbl_storage_engine = '';
144 $table_alters = PMA_getTableAltersArray(
145 $is_myisam_or_aria, $is_isam, $create_options['pack_keys'],
146 (empty($create_options['checksum']) ? '0' : '1'),
147 $is_aria,
148 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
149 (empty($create_options['delay_key_write']) ? '0' : '1'),
150 $is_innodb, $is_pbxt, $create_options['row_format'],
151 $new_tbl_storage_engine,
152 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
153 $tbl_collation
156 if (count($table_alters) > 0) {
157 $sql_query = 'ALTER TABLE '
158 . PMA\libraries\Util::backquote($GLOBALS['table']);
159 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
160 $sql_query .= ';';
161 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
162 $reread_info = true;
163 unset($table_alters);
164 $warning_messages = PMA_getWarningMessagesArray();
167 if (isset($_REQUEST['tbl_collation'])
168 && ! empty($_REQUEST['tbl_collation'])
169 && isset($_REQUEST['change_all_collations'])
170 && ! empty($_REQUEST['change_all_collations'])
172 PMA_changeAllColumnsCollation(
173 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
178 * Reordering the table has been requested by the user
180 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
181 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
182 } // end if
185 * A partition operation has been requested by the user
187 if (isset($_REQUEST['submit_partition'])
188 && ! empty($_REQUEST['partition_operation'])
190 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
191 } // end if
193 if ($reread_info) {
194 // to avoid showing the old value (for example the AUTO_INCREMENT) after
195 // a change, clear the cache
196 $GLOBALS['dbi']->clearTableCache();
197 include 'libraries/tbl_info.inc.php';
199 unset($reread_info);
201 if (isset($result) && empty($message_to_show)) {
202 if (empty($_message)) {
203 if (empty($sql_query)) {
204 $_message = PMA\libraries\Message::success(__('No change'));
205 } else {
206 $_message = $result
207 ? PMA\libraries\Message::success()
208 : PMA\libraries\Message::error();
211 if (isset($GLOBALS['ajax_request'])
212 && $GLOBALS['ajax_request'] == true
214 $response = PMA\libraries\Response::getInstance();
215 $response->setRequestStatus($_message->isSuccess());
216 $response->addJSON('message', $_message);
217 if (!empty($sql_query)) {
218 $response->addJSON(
219 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
222 exit;
224 } else {
225 $_message = $result
226 ? PMA\libraries\Message::success($_message)
227 : PMA\libraries\Message::error($_message);
230 if (! empty($warning_messages)) {
231 $_message = new PMA\libraries\Message;
232 $_message->addMessagesString($warning_messages);
233 $_message->isError(true);
234 if (isset($GLOBALS['ajax_request'])
235 && $GLOBALS['ajax_request'] == true
237 $response = PMA\libraries\Response::getInstance();
238 $response->setRequestStatus(false);
239 $response->addJSON('message', $_message);
240 if (!empty($sql_query)) {
241 $response->addJSON(
242 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
245 exit;
247 unset($warning_messages);
250 if (empty($sql_query)) {
251 $response->addHTML(
252 $_message->getDisplay()
254 } else {
255 $response->addHTML(
256 PMA\libraries\Util::getMessage($_message, $sql_query)
259 unset($_message);
262 $url_params['goto']
263 = $url_params['back']
264 = 'tbl_operations.php';
267 * Get columns names
269 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
272 * Displays the page
274 $response->addHTML('<div id="boxContainer" data-box-width="300">');
277 * Order the table
279 $hideOrderTable = false;
280 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
281 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
282 // InnoDB always orders table rows according to such an index if one is present.
283 if ($tbl_storage_engine == 'INNODB') {
284 $indexes = PMA\libraries\Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
285 foreach ($indexes as $name => $idx) {
286 if ($name == 'PRIMARY') {
287 $hideOrderTable = true;
288 break;
289 } elseif (! $idx->getNonUnique()) {
290 $notNull = true;
291 foreach ($idx->getColumns() as $column) {
292 if ($column->getNull()) {
293 $notNull = false;
294 break;
297 if ($notNull) {
298 $hideOrderTable = true;
299 break;
304 if (! $hideOrderTable) {
305 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
309 * Move table
311 $response->addHTML(PMA_getHtmlForMoveTable());
313 if (mb_strstr($show_comment, '; InnoDB free') === false) {
314 if (mb_strstr($show_comment, 'InnoDB free') === false) {
315 // only user entered comment
316 $comment = $show_comment;
317 } else {
318 // here we have just InnoDB generated part
319 $comment = '';
321 } else {
322 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
323 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
326 // PACK_KEYS: MyISAM or ISAM
327 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
328 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
330 // Here should be version check for InnoDB, however it is supported
331 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
332 // check for version
334 $response->addHTML(
335 PMA_getTableOptionDiv(
336 $comment, $tbl_collation, $tbl_storage_engine,
337 $is_myisam_or_aria, $is_isam, $create_options['pack_keys'],
338 $auto_increment,
339 (empty($create_options['delay_key_write']) ? '0' : '1'),
340 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
341 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
342 $is_innodb, $is_pbxt, $is_aria, (empty($create_options['checksum']) ? '0' : '1')
347 * Copy table
349 $response->addHTML(PMA_getHtmlForCopytable());
352 * Table maintenance
354 $response->addHTML(
355 PMA_getHtmlForTableMaintenance(
356 $is_myisam_or_aria,
357 $is_innodb,
358 $is_berkeleydb,
359 $url_params
363 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
364 $truncate_table_url_params = array();
365 $drop_table_url_params = array();
367 if (! $tbl_is_view
368 && ! (isset($db_is_system_schema) && $db_is_system_schema)
370 $this_sql_query = 'TRUNCATE TABLE '
371 . PMA\libraries\Util::backquote($GLOBALS['table']);
372 $truncate_table_url_params = array_merge(
373 $url_params,
374 array(
375 'sql_query' => $this_sql_query,
376 'goto' => 'tbl_structure.php',
377 'reload' => '1',
378 'message_to_show' => sprintf(
379 __('Table %s has been emptied.'),
380 htmlspecialchars($table)
385 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
386 $this_sql_query = 'DROP TABLE '
387 . PMA\libraries\Util::backquote($GLOBALS['table']);
388 $drop_table_url_params = array_merge(
389 $url_params,
390 array(
391 'sql_query' => $this_sql_query,
392 'goto' => 'db_operations.php',
393 'reload' => '1',
394 'purge' => '1',
395 'message_to_show' => sprintf(
396 ($tbl_is_view
397 ? __('View %s has been dropped.')
398 : __('Table %s has been dropped.')
400 htmlspecialchars($table)
402 // table name is needed to avoid running
403 // PMA_relationsCleanupDatabase() on the whole db later
404 'table' => $GLOBALS['table'],
408 $response->addHTML(
409 PMA_getHtmlForDeleteDataOrTable(
410 $truncate_table_url_params,
411 $drop_table_url_params
416 if (Partition::havePartitioning()) {
417 $partition_names = Partition::getPartitionNames($db, $table);
418 // show the Partition maintenance section only if we detect a partition
419 if (! is_null($partition_names[0])) {
420 $response->addHTML(
421 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
423 } // end if
424 } // end if
425 unset($partition_names);
427 // Referential integrity check
428 // The Referential integrity check was intended for the non-InnoDB
429 // tables for which the relations are defined in pmadb
430 // so I assume that if the current table is InnoDB, I don't display
431 // this choice (InnoDB maintains integrity by itself)
433 if ($cfgRelation['relwork'] && ! $is_innodb) {
434 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
435 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
437 if (! empty($foreign)) {
438 $response->addHTML(
439 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
441 } // end if ($foreign)
443 } // end if (!empty($cfg['Server']['relation']))
445 $response->addHTML('</div>');