Merge remote-tracking branch 'origin/QA_4_4' into QA_4_4
[phpmyadmin.git] / tbl_operations.php
blob803abbd1b9622d31317af43a970021378d7075b6
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']);
21 /**
22 * Load JavaScript files
24 $response = PMA_Response::getInstance();
25 $header = $response->getHeader();
26 $scripts = $header->getScripts();
27 $scripts->addFile('functions.js');
28 $scripts->addFile('tbl_operations.js');
30 /**
31 * Runs common work
33 require 'libraries/tbl_common.inc.php';
34 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
35 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
37 /**
38 * Gets relation settings
40 $cfgRelation = PMA_getRelationsParam();
42 /**
43 * Gets available MySQL charsets and storage engines
45 require_once 'libraries/mysql_charsets.inc.php';
46 require_once 'libraries/StorageEngine.class.php';
48 /**
49 * Class for partition management
51 require_once 'libraries/Partition.class.php';
53 // reselect current db (needed in some cases probably due to
54 // the calling of relation.lib.php)
55 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
57 /**
58 * Gets tables informations
60 require 'libraries/tbl_info.inc.php';
62 // define some variables here, for improved syntax in the conditionals
63 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
64 $is_aria = $is_pbxt = false;
65 // set initial value of these variables, based on the current table engine
66 list($is_myisam_or_aria, $is_innodb, $is_isam,
67 $is_berkeleydb, $is_aria, $is_pbxt
68 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
70 if ($is_aria) {
71 // the value for transactional can be implicit
72 // (no create option found, in this case it means 1)
73 // or explicit (option found with a value of 0 or 1)
74 // ($transactional may have been set by libraries/tbl_info.inc.php,
75 // from the $create_options)
76 $transactional = (isset($transactional) && $transactional == '0')
77 ? '0'
78 : '1';
79 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
82 $reread_info = false;
83 $table_alters = array();
85 /** @var PMA_String $pmaString */
86 $pmaString = $GLOBALS['PMA_String'];
88 /**
89 * If the table has to be moved to some other database
91 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
92 //$_message = '';
93 PMA_moveOrCopyTable($db, $table);
94 // This was ended in an Ajax call
95 exit;
97 /**
98 * If the table has to be maintained
100 if (isset($_REQUEST['table_maintenance'])) {
101 include_once 'sql.php';
102 unset($result);
105 * Updates table comment, type and options if required
107 if (isset($_REQUEST['submitoptions'])) {
108 $_message = '';
109 $warning_messages = array();
111 if (isset($_REQUEST['new_name'])) {
112 if ($pma_table->rename($_REQUEST['new_name'])) {
113 $_message .= $pma_table->getLastMessage();
114 $result = true;
115 $GLOBALS['table'] = $pma_table->getName();
116 $reread_info = true;
117 $reload = true;
118 } else {
119 $_message .= $pma_table->getLastError();
120 $result = false;
124 if (! empty($_REQUEST['new_tbl_storage_engine'])
125 && /*overload*/mb_strtolower($_REQUEST['new_tbl_storage_engine']) !== /*overload*/mb_strtolower($tbl_storage_engine)
127 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
128 // reset the globals for the new engine
129 list($is_myisam_or_aria, $is_innodb, $is_isam,
130 $is_berkeleydb, $is_aria, $is_pbxt
131 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
133 if ($is_aria) {
134 $transactional = (isset($transactional) && $transactional == '0')
135 ? '0'
136 : '1';
137 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
139 } else {
140 $new_tbl_storage_engine = '';
143 $table_alters = PMA_getTableAltersArray(
144 $is_myisam_or_aria, $is_isam, $pack_keys,
145 (empty($checksum) ? '0' : '1'),
146 $is_aria,
147 ((isset($page_checksum)) ? $page_checksum : ''),
148 (empty($delay_key_write) ? '0' : '1'),
149 $is_innodb, $is_pbxt, $row_format,
150 $new_tbl_storage_engine,
151 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
152 $tbl_collation
155 if (count($table_alters) > 0) {
156 $sql_query = 'ALTER TABLE '
157 . PMA_Util::backquote($GLOBALS['table']);
158 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
159 $sql_query .= ';';
160 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
161 $reread_info = true;
162 unset($table_alters);
163 $warning_messages = PMA_getWarningMessagesArray();
167 * Reordering the table has been requested by the user
169 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
170 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
171 } // end if
174 * A partition operation has been requested by the user
176 $sql_query = '';
178 if (isset($_REQUEST['submit_partition'])
179 && ! empty($_REQUEST['partition_operation'])
181 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
182 } // end if
184 if ($reread_info) {
185 // to avoid showing the old value (for example the AUTO_INCREMENT) after
186 // a change, clear the cache
187 PMA_Table::$cache = array();
188 $page_checksum = $checksum = $delay_key_write = 0;
189 include 'libraries/tbl_info.inc.php';
191 unset($reread_info);
193 if (isset($result) && empty($message_to_show)) {
194 // set to success by default, because result set could be empty
195 // (for example, a table rename)
196 $_type = 'success';
197 if (empty($_message)) {
198 $_message = $result
199 ? PMA_Message::success(
200 __('Your SQL query has been executed successfully.')
202 : PMA_Message::error(__('Error'));
203 // $result should exist, regardless of $_message
204 $_type = $result ? 'success' : 'error';
206 if (isset($GLOBALS['ajax_request'])
207 && $GLOBALS['ajax_request'] == true
209 $response = PMA_Response::getInstance();
210 $response->isSuccess($_message->isSuccess());
211 $response->addJSON('message', $_message);
212 $response->addJSON(
213 'sql_query', PMA_Util::getMessage(null, $sql_query)
215 exit;
218 if (! empty($warning_messages)) {
219 $_message = new PMA_Message;
220 $_message->addMessages($warning_messages);
221 $_message->isError(true);
222 if ($GLOBALS['ajax_request'] == true) {
223 $response = PMA_Response::getInstance();
224 $response->isSuccess(false);
225 $response->addJSON('message', $_message);
226 exit;
228 unset($warning_messages);
231 $response->addHTML(
232 PMA_Util::getMessage($_message, $sql_query, $_type)
234 unset($_message, $_type);
237 $url_params['goto']
238 = $url_params['back']
239 = 'tbl_operations.php';
242 * Get columns names
244 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
247 * Displays the page
249 $response->addHTML('<div id="boxContainer" data-box-width="300">');
252 * Order the table
254 $hideOrderTable = false;
255 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
256 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
257 // InnoDB always orders table rows according to such an index if one is present.
258 if ($tbl_storage_engine == 'INNODB') {
259 include_once 'libraries/Index.class.php';
260 $indexes = PMA_Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
261 foreach ($indexes as $name => $idx) {
262 if ($name == 'PRIMARY') {
263 $hideOrderTable = true;
264 break;
265 } elseif (! $idx->getNonUnique()) {
266 $notNull = true;
267 foreach ($idx->getColumns() as $column) {
268 if ($column->getNull()) {
269 $notNull = false;
270 break;
273 if ($notNull) {
274 $hideOrderTable = true;
275 break;
280 if (! $hideOrderTable) {
281 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
285 * Move table
287 $response->addHTML(PMA_getHtmlForMoveTable());
289 if (/*overload*/mb_strstr($show_comment, '; InnoDB free') === false) {
290 if (/*overload*/mb_strstr($show_comment, 'InnoDB free') === false) {
291 // only user entered comment
292 $comment = $show_comment;
293 } else {
294 // here we have just InnoDB generated part
295 $comment = '';
297 } else {
298 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
299 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
302 // PACK_KEYS: MyISAM or ISAM
303 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
304 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
306 // Here should be version check for InnoDB, however it is supported
307 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
308 // check for version
310 $response->addHTML(
311 PMA_getTableOptionDiv(
312 $comment, $tbl_collation, $tbl_storage_engine,
313 $is_myisam_or_aria, $is_isam, $pack_keys,
314 $auto_increment,
315 (empty($delay_key_write) ? '0' : '1'),
316 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
317 ((isset($page_checksum)) ? $page_checksum : ''),
318 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ? '0' : '1')
323 * Copy table
325 $response->addHTML(PMA_getHtmlForCopytable());
328 * Table maintenance
330 $response->addHTML(
331 PMA_getHtmlForTableMaintenance(
332 $is_myisam_or_aria,
333 $is_innodb,
334 $is_berkeleydb,
335 $url_params
339 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
340 $truncate_table_url_params = array();
341 $drop_table_url_params = array();
343 if (! $tbl_is_view
344 && ! (isset($db_is_system_schema) && $db_is_system_schema)
346 $this_sql_query = 'TRUNCATE TABLE '
347 . PMA_Util::backquote($GLOBALS['table']);
348 $truncate_table_url_params = array_merge(
349 $url_params,
350 array(
351 'sql_query' => $this_sql_query,
352 'goto' => 'tbl_structure.php',
353 'reload' => '1',
354 'message_to_show' => sprintf(
355 __('Table %s has been emptied.'),
356 htmlspecialchars($table)
361 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
362 $this_sql_query = 'DROP TABLE '
363 . PMA_Util::backquote($GLOBALS['table']);
364 $drop_table_url_params = array_merge(
365 $url_params,
366 array(
367 'sql_query' => $this_sql_query,
368 'goto' => 'db_operations.php',
369 'reload' => '1',
370 'purge' => '1',
371 'message_to_show' => sprintf(
372 ($tbl_is_view
373 ? __('View %s has been dropped.')
374 : __('Table %s has been dropped.')
376 htmlspecialchars($table)
378 // table name is needed to avoid running
379 // PMA_relationsCleanupDatabase() on the whole db later
380 'table' => $GLOBALS['table'],
384 $response->addHTML(
385 PMA_getHtmlForDeleteDataOrTable(
386 $truncate_table_url_params,
387 $drop_table_url_params
392 if (PMA_Partition::havePartitioning()) {
393 $partition_names = PMA_Partition::getPartitionNames($db, $table);
394 // show the Partition maintenance section only if we detect a partition
395 if (! is_null($partition_names[0])) {
396 $response->addHTML(
397 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
399 } // end if
400 } // end if
401 unset($partition_names);
403 // Referential integrity check
404 // The Referential integrity check was intended for the non-InnoDB
405 // tables for which the relations are defined in pmadb
406 // so I assume that if the current table is InnoDB, I don't display
407 // this choice (InnoDB maintains integrity by itself)
409 if ($cfgRelation['relwork'] && ! $is_innodb) {
410 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
411 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
413 if ($foreign) {
414 $response->addHTML(
415 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
417 } // end if ($foreign)
419 } // end if (!empty($cfg['Server']['relation']))
421 $response->addHTML('</div>');