Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / tbl_operations.php
blob9587491ead049ffd18f0e3b47fd11591c5d8e209
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 if ($pma_table->isEngine('ARIA')) {
55 // the value for transactional can be implicit
56 // (no create option found, in this case it means 1)
57 // or explicit (option found with a value of 0 or 1)
58 // ($create_options['transactional'] may have been set by libraries/tbl_info.inc.php,
59 // from the $create_options)
60 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
61 ? '0'
62 : '1';
63 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
66 $reread_info = false;
67 $table_alters = array();
69 /**
70 * If the table has to be moved to some other database
72 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
73 //$_message = '';
74 PMA_moveOrCopyTable($db, $table);
75 // This was ended in an Ajax call
76 exit;
78 /**
79 * If the table has to be maintained
81 if (isset($_REQUEST['table_maintenance'])) {
82 include_once 'sql.php';
83 unset($result);
85 /**
86 * Updates table comment, type and options if required
88 if (isset($_REQUEST['submitoptions'])) {
89 $_message = '';
90 $warning_messages = array();
92 if (isset($_REQUEST['new_name'])) {
93 // Get original names before rename operation
94 $oldTable = $pma_table->getName();
95 $oldDb = $pma_table->getDbName();
97 if ($pma_table->rename($_REQUEST['new_name'])) {
98 if (isset($_REQUEST['adjust_privileges'])
99 && ! empty($_REQUEST['adjust_privileges'])
101 PMA_AdjustPrivileges_renameOrMoveTable(
102 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
106 // Reselect the original DB
107 $GLOBALS['db'] = $oldDb;
108 $GLOBALS['dbi']->selectDb($oldDb);
110 $_message .= $pma_table->getLastMessage();
111 $result = true;
112 $GLOBALS['table'] = $pma_table->getName();
113 $reread_info = true;
114 $reload = true;
115 } else {
116 $_message .= $pma_table->getLastError();
117 $result = false;
121 if (! empty($_REQUEST['new_tbl_storage_engine'])
122 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
124 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
126 if ($pma_table->isEngine('ARIA')) {
127 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
128 ? '0'
129 : '1';
130 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
132 } else {
133 $new_tbl_storage_engine = '';
136 $table_alters = PMA_getTableAltersArray(
137 $pma_table,
138 $create_options['pack_keys'],
139 (empty($create_options['checksum']) ? '0' : '1'),
140 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
141 (empty($create_options['delay_key_write']) ? '0' : '1'),
142 $create_options['row_format'],
143 $new_tbl_storage_engine,
144 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
145 $tbl_collation
148 if (count($table_alters) > 0) {
149 $sql_query = 'ALTER TABLE '
150 . PMA\libraries\Util::backquote($GLOBALS['table']);
151 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
152 $sql_query .= ';';
153 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
154 $reread_info = true;
155 unset($table_alters);
156 $warning_messages = PMA_getWarningMessagesArray();
159 if (isset($_REQUEST['tbl_collation'])
160 && ! empty($_REQUEST['tbl_collation'])
161 && isset($_REQUEST['change_all_collations'])
162 && ! empty($_REQUEST['change_all_collations'])
164 PMA_changeAllColumnsCollation(
165 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
170 * Reordering the table has been requested by the user
172 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
173 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
174 } // end if
177 * A partition operation has been requested by the user
179 if (isset($_REQUEST['submit_partition'])
180 && ! empty($_REQUEST['partition_operation'])
182 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
183 } // end if
185 if ($reread_info) {
186 // to avoid showing the old value (for example the AUTO_INCREMENT) after
187 // a change, clear the cache
188 $GLOBALS['dbi']->clearTableCache();
189 include 'libraries/tbl_info.inc.php';
191 unset($reread_info);
193 if (isset($result) && empty($message_to_show)) {
194 if (empty($_message)) {
195 if (empty($sql_query)) {
196 $_message = PMA\libraries\Message::success(__('No change'));
197 } else {
198 $_message = $result
199 ? PMA\libraries\Message::success()
200 : PMA\libraries\Message::error();
203 if (isset($GLOBALS['ajax_request'])
204 && $GLOBALS['ajax_request'] == true
206 $response = PMA\libraries\Response::getInstance();
207 $response->setRequestStatus($_message->isSuccess());
208 $response->addJSON('message', $_message);
209 if (!empty($sql_query)) {
210 $response->addJSON(
211 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
214 exit;
216 } else {
217 $_message = $result
218 ? PMA\libraries\Message::success($_message)
219 : PMA\libraries\Message::error($_message);
222 if (! empty($warning_messages)) {
223 $_message = new PMA\libraries\Message;
224 $_message->addMessagesString($warning_messages);
225 $_message->isError(true);
226 if (isset($GLOBALS['ajax_request'])
227 && $GLOBALS['ajax_request'] == true
229 $response = PMA\libraries\Response::getInstance();
230 $response->setRequestStatus(false);
231 $response->addJSON('message', $_message);
232 if (!empty($sql_query)) {
233 $response->addJSON(
234 'sql_query', PMA\libraries\Util::getMessage(null, $sql_query)
237 exit;
239 unset($warning_messages);
242 if (empty($sql_query)) {
243 $response->addHTML(
244 $_message->getDisplay()
246 } else {
247 $response->addHTML(
248 PMA\libraries\Util::getMessage($_message, $sql_query)
251 unset($_message);
254 $url_params['goto']
255 = $url_params['back']
256 = 'tbl_operations.php';
259 * Get columns names
261 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
264 * Displays the page
266 $response->addHTML('<div id="boxContainer" data-box-width="300">');
269 * Order the table
271 $hideOrderTable = false;
272 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
273 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
274 // InnoDB always orders table rows according to such an index if one is present.
275 if ($tbl_storage_engine == 'INNODB') {
276 $indexes = PMA\libraries\Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
277 foreach ($indexes as $name => $idx) {
278 if ($name == 'PRIMARY') {
279 $hideOrderTable = true;
280 break;
281 } elseif (! $idx->getNonUnique()) {
282 $notNull = true;
283 foreach ($idx->getColumns() as $column) {
284 if ($column->getNull()) {
285 $notNull = false;
286 break;
289 if ($notNull) {
290 $hideOrderTable = true;
291 break;
296 if (! $hideOrderTable) {
297 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
301 * Move table
303 $response->addHTML(PMA_getHtmlForMoveTable());
305 if (mb_strstr($show_comment, '; InnoDB free') === false) {
306 if (mb_strstr($show_comment, 'InnoDB free') === false) {
307 // only user entered comment
308 $comment = $show_comment;
309 } else {
310 // here we have just InnoDB generated part
311 $comment = '';
313 } else {
314 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
315 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
318 // PACK_KEYS: MyISAM or ISAM
319 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
320 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
322 // Here should be version check for InnoDB, however it is supported
323 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
324 // check for version
326 $response->addHTML(
327 PMA_getTableOptionDiv(
328 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
329 $create_options['pack_keys'],
330 $auto_increment,
331 (empty($create_options['delay_key_write']) ? '0' : '1'),
332 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
333 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
334 (empty($create_options['checksum']) ? '0' : '1')
339 * Copy table
341 $response->addHTML(PMA_getHtmlForCopytable());
344 * Table maintenance
346 $response->addHTML(
347 PMA_getHtmlForTableMaintenance($pma_table, $url_params)
350 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
351 $truncate_table_url_params = array();
352 $drop_table_url_params = array();
354 if (! $tbl_is_view
355 && ! (isset($db_is_system_schema) && $db_is_system_schema)
357 $this_sql_query = 'TRUNCATE TABLE '
358 . PMA\libraries\Util::backquote($GLOBALS['table']);
359 $truncate_table_url_params = array_merge(
360 $url_params,
361 array(
362 'sql_query' => $this_sql_query,
363 'goto' => 'tbl_structure.php',
364 'reload' => '1',
365 'message_to_show' => sprintf(
366 __('Table %s has been emptied.'),
367 htmlspecialchars($table)
372 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
373 $this_sql_query = 'DROP TABLE '
374 . PMA\libraries\Util::backquote($GLOBALS['table']);
375 $drop_table_url_params = array_merge(
376 $url_params,
377 array(
378 'sql_query' => $this_sql_query,
379 'goto' => 'db_operations.php',
380 'reload' => '1',
381 'purge' => '1',
382 'message_to_show' => sprintf(
383 ($tbl_is_view
384 ? __('View %s has been dropped.')
385 : __('Table %s has been dropped.')
387 htmlspecialchars($table)
389 // table name is needed to avoid running
390 // PMA_relationsCleanupDatabase() on the whole db later
391 'table' => $GLOBALS['table'],
395 $response->addHTML(
396 PMA_getHtmlForDeleteDataOrTable(
397 $truncate_table_url_params,
398 $drop_table_url_params
403 if (Partition::havePartitioning()) {
404 $partition_names = Partition::getPartitionNames($db, $table);
405 // show the Partition maintenance section only if we detect a partition
406 if (! is_null($partition_names[0])) {
407 $response->addHTML(
408 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
410 } // end if
411 } // end if
412 unset($partition_names);
414 // Referential integrity check
415 // The Referential integrity check was intended for the non-InnoDB
416 // tables for which the relations are defined in pmadb
417 // so I assume that if the current table is InnoDB, I don't display
418 // this choice (InnoDB maintains integrity by itself)
420 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
421 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
422 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
424 if (! empty($foreign)) {
425 $response->addHTML(
426 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
428 } // end if ($foreign)
430 } // end if (!empty($cfg['Server']['relation']))
432 $response->addHTML('</div>');