Merge branch 'QA_4_3'
[phpmyadmin.git] / tbl_operations.php
blob6a4773ed72a2ad84f46bdcf1d3b933179469e5ed
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Various table operations
6 * @package PhpMyAdmin
7 */
9 /**
12 require_once 'libraries/common.inc.php';
14 /**
15 * functions implementation for this script
17 require_once 'libraries/operations.lib.php';
19 $pma_table = new PMA_Table($GLOBALS['table'], $GLOBALS['db']);
20 $response = PMA_Response::getInstance();
21 /**
22 * Runs common work
24 require 'libraries/tbl_common.inc.php';
25 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
26 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
28 /**
29 * Gets relation settings
31 $cfgRelation = PMA_getRelationsParam();
33 /**
34 * Gets available MySQL charsets and storage engines
36 require_once 'libraries/mysql_charsets.inc.php';
37 require_once 'libraries/StorageEngine.class.php';
39 /**
40 * Class for partition management
42 require_once 'libraries/Partition.class.php';
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 informations
51 require 'libraries/tbl_info.inc.php';
53 // define some variables here, for improved syntax in the conditionals
54 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
55 $is_aria = $is_pbxt = false;
56 // set initial value of these variables, based on the current table engine
57 list($is_myisam_or_aria, $is_innodb, $is_isam,
58 $is_berkeleydb, $is_aria, $is_pbxt
59 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
61 if ($is_aria) {
62 // the value for transactional can be implicit
63 // (no create option found, in this case it means 1)
64 // or explicit (option found with a value of 0 or 1)
65 // ($transactional may have been set by libraries/tbl_info.inc.php,
66 // from the $create_options)
67 $transactional = (isset($transactional) && $transactional == '0')
68 ? '0'
69 : '1';
70 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
73 $reread_info = false;
74 $table_alters = array();
76 /** @var PMA_String $pmaString */
77 $pmaString = $GLOBALS['PMA_String'];
79 /**
80 * If the table has to be moved to some other database
82 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
83 //$_message = '';
84 PMA_moveOrCopyTable($db, $table);
85 // This was ended in an Ajax call
86 exit;
88 /**
89 * If the table has to be maintained
91 if (isset($_REQUEST['table_maintenance'])) {
92 include_once 'sql.php';
93 unset($result);
95 /**
96 * Updates table comment, type and options if required
98 if (isset($_REQUEST['submitoptions'])) {
99 $_message = '';
100 $warning_messages = array();
102 if (isset($_REQUEST['new_name'])) {
103 if ($pma_table->rename($_REQUEST['new_name'])) {
104 $_message .= $pma_table->getLastMessage();
105 $result = true;
106 $GLOBALS['table'] = $pma_table->getName();
107 $reread_info = true;
108 $reload = true;
109 } else {
110 $_message .= $pma_table->getLastError();
111 $result = false;
115 if (! empty($_REQUEST['new_tbl_storage_engine'])
116 && /*overload*/mb_strtolower($_REQUEST['new_tbl_storage_engine']) !== /*overload*/mb_strtolower($tbl_storage_engine)
118 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
119 // reset the globals for the new engine
120 list($is_myisam_or_aria, $is_innodb, $is_isam,
121 $is_berkeleydb, $is_aria, $is_pbxt
122 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
124 if ($is_aria) {
125 $transactional = (isset($transactional) && $transactional == '0')
126 ? '0'
127 : '1';
128 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
130 } else {
131 $new_tbl_storage_engine = '';
134 $table_alters = PMA_getTableAltersArray(
135 $is_myisam_or_aria, $is_isam, $pack_keys,
136 (empty($checksum) ? '0' : '1'),
137 $is_aria,
138 ((isset($page_checksum)) ? $page_checksum : ''),
139 (empty($delay_key_write) ? '0' : '1'),
140 $is_innodb, $is_pbxt, $row_format,
141 $new_tbl_storage_engine,
142 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
143 $tbl_collation
146 if (count($table_alters) > 0) {
147 $sql_query = 'ALTER TABLE '
148 . PMA_Util::backquote($GLOBALS['table']);
149 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
150 $sql_query .= ';';
151 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
152 $reread_info = true;
153 unset($table_alters);
154 $warning_messages = PMA_getWarningMessagesArray();
158 * Reordering the table has been requested by the user
160 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
161 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
162 } // end if
165 * A partition operation has been requested by the user
167 $sql_query = '';
169 if (isset($_REQUEST['submit_partition'])
170 && ! empty($_REQUEST['partition_operation'])
172 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
173 } // end if
175 if ($reread_info) {
176 // to avoid showing the old value (for example the AUTO_INCREMENT) after
177 // a change, clear the cache
178 PMA_Table::$cache = array();
179 $page_checksum = $checksum = $delay_key_write = 0;
180 include 'libraries/tbl_info.inc.php';
182 unset($reread_info);
184 if (isset($result) && empty($message_to_show)) {
185 // set to success by default, because result set could be empty
186 // (for example, a table rename)
187 $_type = 'success';
188 if (empty($_message)) {
189 $_message = $result
190 ? PMA_Message::success(
191 __('Your SQL query has been executed successfully.')
193 : PMA_Message::error(__('Error'));
194 // $result should exist, regardless of $_message
195 $_type = $result ? 'success' : 'error';
197 if (isset($GLOBALS['ajax_request'])
198 && $GLOBALS['ajax_request'] == true
200 $response = PMA_Response::getInstance();
201 $response->isSuccess($_message->isSuccess());
202 $response->addJSON('message', $_message);
203 $response->addJSON(
204 'sql_query', PMA_Util::getMessage(null, $sql_query)
206 exit;
209 if (! empty($warning_messages)) {
210 $_message = new PMA_Message;
211 $_message->addMessages($warning_messages);
212 $_message->isError(true);
213 if ($GLOBALS['ajax_request'] == true) {
214 $response = PMA_Response::getInstance();
215 $response->isSuccess(false);
216 $response->addJSON('message', $_message);
217 exit;
219 unset($warning_messages);
222 $response->addHTML(
223 PMA_Util::getMessage($_message, $sql_query, $_type)
225 unset($_message, $_type);
228 $url_params['goto']
229 = $url_params['back']
230 = 'tbl_operations.php';
233 * Get columns names
235 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
238 * Displays the page
240 $response->addHTML('<div id="boxContainer" data-box-width="300">');
243 * Order the table
245 $hideOrderTable = false;
246 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
247 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
248 // InnoDB always orders table rows according to such an index if one is present.
249 if ($tbl_storage_engine == 'INNODB') {
250 include_once 'libraries/Index.class.php';
251 $indexes = PMA_Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
252 foreach ($indexes as $name => $idx) {
253 if ($name == 'PRIMARY') {
254 $hideOrderTable = true;
255 break;
256 } elseif (! $idx->getNonUnique()) {
257 $notNull = true;
258 foreach ($idx->getColumns() as $column) {
259 if ($column->getNull()) {
260 $notNull = false;
261 break;
264 if ($notNull) {
265 $hideOrderTable = true;
266 break;
271 if (! $hideOrderTable) {
272 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
276 * Move table
278 $response->addHTML(PMA_getHtmlForMoveTable());
280 if (/*overload*/mb_strstr($show_comment, '; InnoDB free') === false) {
281 if (/*overload*/mb_strstr($show_comment, 'InnoDB free') === false) {
282 // only user entered comment
283 $comment = $show_comment;
284 } else {
285 // here we have just InnoDB generated part
286 $comment = '';
288 } else {
289 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
290 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
293 // PACK_KEYS: MyISAM or ISAM
294 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
295 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
297 // Here should be version check for InnoDB, however it is supported
298 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
299 // check for version
301 $response->addHTML(
302 PMA_getTableOptionDiv(
303 $comment, $tbl_collation, $tbl_storage_engine,
304 $is_myisam_or_aria, $is_isam, $pack_keys,
305 $auto_increment,
306 (empty($delay_key_write) ? '0' : '1'),
307 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
308 ((isset($page_checksum)) ? $page_checksum : ''),
309 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ? '0' : '1')
314 * Copy table
316 $response->addHTML(PMA_getHtmlForCopytable());
319 * Table maintenance
321 $response->addHTML(
322 PMA_getHtmlForTableMaintenance(
323 $is_myisam_or_aria,
324 $is_innodb,
325 $is_berkeleydb,
326 $url_params
330 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
331 $truncate_table_url_params = array();
332 $drop_table_url_params = array();
334 if (! $tbl_is_view
335 && ! (isset($db_is_system_schema) && $db_is_system_schema)
337 $this_sql_query = 'TRUNCATE TABLE '
338 . PMA_Util::backquote($GLOBALS['table']);
339 $truncate_table_url_params = array_merge(
340 $url_params,
341 array(
342 'sql_query' => $this_sql_query,
343 'goto' => 'tbl_structure.php',
344 'reload' => '1',
345 'message_to_show' => sprintf(
346 __('Table %s has been emptied.'),
347 htmlspecialchars($table)
352 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
353 $this_sql_query = 'DROP TABLE '
354 . PMA_Util::backquote($GLOBALS['table']);
355 $drop_table_url_params = array_merge(
356 $url_params,
357 array(
358 'sql_query' => $this_sql_query,
359 'goto' => 'db_operations.php',
360 'reload' => '1',
361 'purge' => '1',
362 'message_to_show' => sprintf(
363 ($tbl_is_view
364 ? __('View %s has been dropped.')
365 : __('Table %s has been dropped.')
367 htmlspecialchars($table)
369 // table name is needed to avoid running
370 // PMA_relationsCleanupDatabase() on the whole db later
371 'table' => $GLOBALS['table'],
375 $response->addHTML(
376 PMA_getHtmlForDeleteDataOrTable(
377 $truncate_table_url_params,
378 $drop_table_url_params
383 if (PMA_Partition::havePartitioning()) {
384 $partition_names = PMA_Partition::getPartitionNames($db, $table);
385 // show the Partition maintenance section only if we detect a partition
386 if (! is_null($partition_names[0])) {
387 $response->addHTML(
388 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
390 } // end if
391 } // end if
392 unset($partition_names);
394 // Referential integrity check
395 // The Referential integrity check was intended for the non-InnoDB
396 // tables for which the relations are defined in pmadb
397 // so I assume that if the current table is InnoDB, I don't display
398 // this choice (InnoDB maintains integrity by itself)
400 if ($cfgRelation['relwork'] && ! $is_innodb) {
401 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
402 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
404 if ($foreign) {
405 $response->addHTML(
406 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
408 } // end if ($foreign)
410 } // end if (!empty($cfg['Server']['relation']))
412 $response->addHTML('</div>');