Translated using Weblate (Finnish)
[phpmyadmin.git] / tbl_operations.php
blobd45697c195bb76c1c73de27af649244def9831bd
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 // ($transactional may have been set by libraries/tbl_info.inc.php,
63 // from the $create_options)
64 $transactional = (isset($transactional) && $transactional == '0')
65 ? '0'
66 : '1';
67 $page_checksum = (isset($page_checksum)) ? $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 $transactional = (isset($transactional) && $transactional == '0')
136 ? '0'
137 : '1';
138 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
140 } else {
141 $new_tbl_storage_engine = '';
144 $table_alters = PMA_getTableAltersArray(
145 $is_myisam_or_aria, $is_isam, $pack_keys,
146 (empty($checksum) ? '0' : '1'),
147 $is_aria,
148 ((isset($page_checksum)) ? $page_checksum : ''),
149 (empty($delay_key_write) ? '0' : '1'),
150 $is_innodb, $is_pbxt, $row_format,
151 $new_tbl_storage_engine,
152 ((isset($transactional) && $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 $page_checksum = $checksum = $delay_key_write = 0;
198 include 'libraries/tbl_info.inc.php';
200 unset($reread_info);
202 if (isset($result) && empty($message_to_show)) {
203 if (empty($_message)) {
204 if (empty($sql_query)) {
205 $_message = PMA\libraries\Message::success(__('No change'));
206 } else {
207 $_message = $result
208 ? PMA\libraries\Message::success()
209 : PMA\libraries\Message::error();
212 if (isset($GLOBALS['ajax_request'])
213 && $GLOBALS['ajax_request'] == true
215 $response = PMA\libraries\Response::getInstance();
216 $response->setRequestStatus($_message->isSuccess());
217 $response->addJSON('message', $_message);
218 if (!empty($sql_query)) {
219 $response->addJSON(
220 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
223 exit;
226 if (! empty($warning_messages)) {
227 $_message = new PMA\libraries\Message;
228 $_message->addMessagesString($warning_messages);
229 $_message->isError(true);
230 if (isset($GLOBALS['ajax_request'])
231 && $GLOBALS['ajax_request'] == true
233 $response = PMA\libraries\Response::getInstance();
234 $response->setRequestStatus(false);
235 $response->addJSON('message', $_message);
236 if (!empty($sql_query)) {
237 $response->addJSON(
238 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
241 exit;
243 unset($warning_messages);
246 if (empty($sql_query)) {
247 $response->addHTML(
248 $_message->getDisplay()
250 } else {
251 $response->addHTML(
252 PMA\libraries\Util::getMessage($_message, $sql_query)
255 unset($_message);
258 $url_params['goto']
259 = $url_params['back']
260 = 'tbl_operations.php';
263 * Get columns names
265 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
268 * Displays the page
270 $response->addHTML('<div id="boxContainer" data-box-width="300">');
273 * Order the table
275 $hideOrderTable = false;
276 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
277 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
278 // InnoDB always orders table rows according to such an index if one is present.
279 if ($tbl_storage_engine == 'INNODB') {
280 $indexes = PMA\libraries\Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
281 foreach ($indexes as $name => $idx) {
282 if ($name == 'PRIMARY') {
283 $hideOrderTable = true;
284 break;
285 } elseif (! $idx->getNonUnique()) {
286 $notNull = true;
287 foreach ($idx->getColumns() as $column) {
288 if ($column->getNull()) {
289 $notNull = false;
290 break;
293 if ($notNull) {
294 $hideOrderTable = true;
295 break;
300 if (! $hideOrderTable) {
301 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
305 * Move table
307 $response->addHTML(PMA_getHtmlForMoveTable());
309 if (mb_strstr($show_comment, '; InnoDB free') === false) {
310 if (mb_strstr($show_comment, 'InnoDB free') === false) {
311 // only user entered comment
312 $comment = $show_comment;
313 } else {
314 // here we have just InnoDB generated part
315 $comment = '';
317 } else {
318 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
319 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
322 // PACK_KEYS: MyISAM or ISAM
323 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
324 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
326 // Here should be version check for InnoDB, however it is supported
327 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
328 // check for version
330 $response->addHTML(
331 PMA_getTableOptionDiv(
332 $comment, $tbl_collation, $tbl_storage_engine,
333 $is_myisam_or_aria, $is_isam, $pack_keys,
334 $auto_increment,
335 (empty($delay_key_write) ? '0' : '1'),
336 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
337 ((isset($page_checksum)) ? $page_checksum : ''),
338 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ? '0' : '1')
343 * Copy table
345 $response->addHTML(PMA_getHtmlForCopytable());
348 * Table maintenance
350 $response->addHTML(
351 PMA_getHtmlForTableMaintenance(
352 $is_myisam_or_aria,
353 $is_innodb,
354 $is_berkeleydb,
355 $url_params
359 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
360 $truncate_table_url_params = array();
361 $drop_table_url_params = array();
363 if (! $tbl_is_view
364 && ! (isset($db_is_system_schema) && $db_is_system_schema)
366 $this_sql_query = 'TRUNCATE TABLE '
367 . PMA\libraries\Util::backquote($GLOBALS['table']);
368 $truncate_table_url_params = array_merge(
369 $url_params,
370 array(
371 'sql_query' => $this_sql_query,
372 'goto' => 'tbl_structure.php',
373 'reload' => '1',
374 'message_to_show' => sprintf(
375 __('Table %s has been emptied.'),
376 htmlspecialchars($table)
381 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
382 $this_sql_query = 'DROP TABLE '
383 . PMA\libraries\Util::backquote($GLOBALS['table']);
384 $drop_table_url_params = array_merge(
385 $url_params,
386 array(
387 'sql_query' => $this_sql_query,
388 'goto' => 'db_operations.php',
389 'reload' => '1',
390 'purge' => '1',
391 'message_to_show' => sprintf(
392 ($tbl_is_view
393 ? __('View %s has been dropped.')
394 : __('Table %s has been dropped.')
396 htmlspecialchars($table)
398 // table name is needed to avoid running
399 // PMA_relationsCleanupDatabase() on the whole db later
400 'table' => $GLOBALS['table'],
404 $response->addHTML(
405 PMA_getHtmlForDeleteDataOrTable(
406 $truncate_table_url_params,
407 $drop_table_url_params
412 if (Partition::havePartitioning()) {
413 $partition_names = Partition::getPartitionNames($db, $table);
414 // show the Partition maintenance section only if we detect a partition
415 if (! is_null($partition_names[0])) {
416 $response->addHTML(
417 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
419 } // end if
420 } // end if
421 unset($partition_names);
423 // Referential integrity check
424 // The Referential integrity check was intended for the non-InnoDB
425 // tables for which the relations are defined in pmadb
426 // so I assume that if the current table is InnoDB, I don't display
427 // this choice (InnoDB maintains integrity by itself)
429 if ($cfgRelation['relwork'] && ! $is_innodb) {
430 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
431 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
433 if (! empty($foreign)) {
434 $response->addHTML(
435 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
437 } // end if ($foreign)
439 } // end if (!empty($cfg['Server']['relation']))
441 $response->addHTML('</div>');