fix coding style: true is lower case
[phpmyadmin.git] / tbl_operations.php
blob6a1e4216c0204204d5acb5b706a46cc7fe6491e9
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();
22 /**
23 * Runs common work
25 require 'libraries/tbl_common.inc.php';
26 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
27 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
29 /**
30 * Gets relation settings
32 $cfgRelation = PMA_getRelationsParam();
34 /**
35 * Gets available MySQL charsets and storage engines
37 require_once 'libraries/mysql_charsets.inc.php';
38 require_once 'libraries/StorageEngine.class.php';
40 /**
41 * Class for partition management
43 require_once 'libraries/Partition.class.php';
45 // reselect current db (needed in some cases probably due to
46 // the calling of relation.lib.php)
47 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
49 /**
50 * Gets tables informations
52 require 'libraries/tbl_info.inc.php';
54 // define some variables here, for improved syntax in the conditionals
55 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
56 $is_aria = $is_pbxt = false;
57 // set initial value of these variables, based on the current table engine
58 list($is_myisam_or_aria, $is_innodb, $is_isam,
59 $is_berkeleydb, $is_aria, $is_pbxt
60 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
62 if ($is_aria) {
63 // the value for transactional can be implicit
64 // (no create option found, in this case it means 1)
65 // or explicit (option found with a value of 0 or 1)
66 // ($transactional may have been set by libraries/tbl_info.inc.php,
67 // from the $create_options)
68 $transactional = (isset($transactional) && $transactional == '0')
69 ? '0'
70 : '1';
71 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
74 $reread_info = false;
75 $table_alters = array();
77 /**
78 * If the table has to be moved to some other database
80 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
81 $_message = '';
82 include_once 'tbl_move_copy.php';
84 /**
85 * If the table has to be maintained
87 if (isset($_REQUEST['table_maintenance'])) {
88 include_once 'sql.php';
89 unset($result);
91 /**
92 * Updates table comment, type and options if required
94 if (isset($_REQUEST['submitoptions'])) {
95 $_message = '';
96 $warning_messages = array();
98 if (isset($_REQUEST['new_name'])) {
99 if ($pma_table->rename($_REQUEST['new_name'])) {
100 $_message .= $pma_table->getLastMessage();
101 $result = true;
102 $GLOBALS['table'] = $pma_table->getName();
103 $reread_info = true;
104 $reload = true;
105 } else {
106 $_message .= $pma_table->getLastError();
107 $result = false;
111 if (! empty($_REQUEST['new_tbl_storage_engine'])
112 && strtolower($_REQUEST['new_tbl_storage_engine']) !== strtolower($tbl_storage_engine)
114 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
115 // reset the globals for the new engine
116 list($is_myisam_or_aria, $is_innodb, $is_isam,
117 $is_berkeleydb, $is_aria, $is_pbxt
118 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
120 if ($is_aria) {
121 $transactional = (isset($transactional) && $transactional == '0')
122 ? '0'
123 : '1';
124 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
126 } else {
127 $new_tbl_storage_engine = '';
130 $table_alters = PMA_getTableAltersArray(
131 $is_myisam_or_aria, $is_isam, $pack_keys,
132 (empty($checksum) ? '0' : '1'),
133 $is_aria,
134 ((isset($page_checksum)) ? $page_checksum : ''),
135 (empty($delay_key_write) ? '0' : '1'),
136 $is_innodb, $is_pbxt, $row_format,
137 $new_tbl_storage_engine,
138 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
139 $tbl_collation
142 if (count($table_alters) > 0) {
143 $sql_query = 'ALTER TABLE '
144 . PMA_Util::backquote($GLOBALS['table']);
145 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
146 $sql_query .= ';';
147 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
148 $reread_info = true;
149 unset($table_alters);
150 $warning_messages = PMA_getWarningMessagesArray();
154 * Reordering the table has been requested by the user
156 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
157 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
158 } // end if
161 * A partition operation has been requested by the user
163 if (isset($_REQUEST['submit_partition'])
164 && ! empty($_REQUEST['partition_operation'])
166 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
167 } // end if
169 if ($reread_info) {
170 // to avoid showing the old value (for example the AUTO_INCREMENT) after
171 // a change, clear the cache
172 PMA_Table::$cache = array();
173 $page_checksum = $checksum = $delay_key_write = 0;
174 include 'libraries/tbl_info.inc.php';
176 unset($reread_info);
178 if (isset($result) && empty($message_to_show)) {
179 // set to success by default, because result set could be empty
180 // (for example, a table rename)
181 $_type = 'success';
182 if (empty($_message)) {
183 $_message = $result
184 ? PMA_Message::success(
185 __('Your SQL query has been executed successfully')
187 : PMA_Message::error(__('Error'));
188 // $result should exist, regardless of $_message
189 $_type = $result ? 'success' : 'error';
191 if (isset($GLOBALS['ajax_request'])
192 && $GLOBALS['ajax_request'] == true
194 $response = PMA_Response::getInstance();
195 $response->isSuccess($_message->isSuccess());
196 $response->addJSON('message', $_message);
197 $response->addJSON(
198 'sql_query', PMA_Util::getMessage(null, $sql_query)
200 exit;
203 if (! empty($warning_messages)) {
204 $_message = new PMA_Message;
205 $_message->addMessages($warning_messages);
206 $_message->isError(true);
207 if ($GLOBALS['ajax_request'] == true) {
208 $response = PMA_Response::getInstance();
209 $response->isSuccess(false);
210 $response->addJSON('message', $_message);
211 exit;
213 unset($warning_messages);
216 $response->addHTML(
217 PMA_Util::getMessage($_message, $sql_query, $_type)
219 unset($_message, $_type);
222 $url_params['goto']
223 = $url_params['back']
224 = 'tbl_operations.php';
227 * Get columns names
229 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
232 * Displays the page
234 $response->addHTML('<div id="boxContainer" data-box-width="300">');
237 * Order the table
239 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
242 * Move table
244 $response->addHTML(PMA_getHtmlForMoveTable());
246 if (strstr($show_comment, '; InnoDB free') === false) {
247 if (strstr($show_comment, 'InnoDB free') === false) {
248 // only user entered comment
249 $comment = $show_comment;
250 } else {
251 // here we have just InnoDB generated part
252 $comment = '';
254 } else {
255 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
256 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
259 // PACK_KEYS: MyISAM or ISAM
260 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
261 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
263 // Here should be version check for InnoDB, however it is supported
264 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
265 // check for version
267 $response->addHTML(
268 PMA_getTableOptionDiv(
269 $comment, $tbl_collation, $tbl_storage_engine,
270 $is_myisam_or_aria, $is_isam, $pack_keys,
271 $auto_increment,
272 (empty($delay_key_write) ? '0' : '1'),
273 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
274 ((isset($page_checksum)) ? $page_checksum : ''),
275 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ? '0' : '1')
280 * Copy table
282 $response->addHTML(PMA_getHtmlForCopytable());
285 * Table maintenance
287 $response->addHTML(
288 PMA_getHtmlForTableMaintenance(
289 $is_myisam_or_aria,
290 $is_innodb,
291 $is_berkeleydb,
292 $url_params
296 if (! (isset($db_is_information_schema) && $db_is_information_schema)) {
297 $truncate_table_url_params = array();
298 $drop_table_url_params = array();
300 if (! $tbl_is_view
301 && ! (isset($db_is_information_schema) && $db_is_information_schema)
303 $this_sql_query = 'TRUNCATE TABLE '
304 . PMA_Util::backquote($GLOBALS['table']);
305 $truncate_table_url_params = array_merge(
306 $url_params,
307 array(
308 'sql_query' => $this_sql_query,
309 'goto' => 'tbl_structure.php',
310 'reload' => '1',
311 'message_to_show' => sprintf(
312 __('Table %s has been emptied'),
313 htmlspecialchars($table)
318 if (! (isset($db_is_information_schema) && $db_is_information_schema)) {
319 $this_sql_query = 'DROP TABLE '
320 . PMA_Util::backquote($GLOBALS['table']);
321 $drop_table_url_params = array_merge(
322 $url_params,
323 array(
324 'sql_query' => $this_sql_query,
325 'goto' => 'db_operations.php',
326 'reload' => '1',
327 'purge' => '1',
328 'message_to_show' => sprintf(
329 ($tbl_is_view
330 ? __('View %s has been dropped')
331 : __('Table %s has been dropped')
333 htmlspecialchars($table)
335 // table name is needed to avoid running
336 // PMA_relationsCleanupDatabase() on the whole db later
337 'table' => $GLOBALS['table'],
341 $response->addHTML(
342 PMA_getHtmlForDeleteDataOrTable(
343 $truncate_table_url_params,
344 $drop_table_url_params
349 if (PMA_Partition::havePartitioning()) {
350 $partition_names = PMA_Partition::getPartitionNames($db, $table);
351 // show the Partition maintenance section only if we detect a partition
352 if (! is_null($partition_names[0])) {
353 $response->addHTML(
354 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
356 } // end if
357 } // end if
358 unset($partition_names);
360 // Referential integrity check
361 // The Referential integrity check was intended for the non-InnoDB
362 // tables for which the relations are defined in pmadb
363 // so I assume that if the current table is InnoDB, I don't display
364 // this choice (InnoDB maintains integrity by itself)
366 if ($cfgRelation['relwork'] && ! $is_innodb) {
367 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
368 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table']);
370 if ($foreign) {
371 $response->addHTML(
372 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
374 } // end if ($foreign)
376 } // end if (!empty($cfg['Server']['relation']))
378 $response->addHTML('</div>');