Refactored ConfigFile class so that it is no longer a singleton
[phpmyadmin.git] / tbl_operations.php
bloba078ab99f63325dda58db191a5e00d5a584ac899
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 $hideOrderTable = false;
240 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
241 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
242 // InnoDB always orders table rows according to such an index if one is present.
243 if ($tbl_storage_engine == 'INNODB') {
244 include_once 'libraries/Index.class.php';
245 $indexes = PMA_Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
246 foreach ($indexes as $name => $idx) {
247 if ($name == 'PRIMARY') {
248 $hideOrderTable = true;
249 break;
250 } elseif (! $idx->getNonUnique()) {
251 $notNull = true;
252 foreach ($idx->getColumns() as $column) {
253 if ($column->getNull()) {
254 $notNull = false;
255 break;
258 if ($notNull) {
259 $hideOrderTable = true;
260 break;
265 if (! $hideOrderTable) {
266 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
270 * Move table
272 $response->addHTML(PMA_getHtmlForMoveTable());
274 if (strstr($show_comment, '; InnoDB free') === false) {
275 if (strstr($show_comment, 'InnoDB free') === false) {
276 // only user entered comment
277 $comment = $show_comment;
278 } else {
279 // here we have just InnoDB generated part
280 $comment = '';
282 } else {
283 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
284 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
287 // PACK_KEYS: MyISAM or ISAM
288 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
289 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
291 // Here should be version check for InnoDB, however it is supported
292 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
293 // check for version
295 $response->addHTML(
296 PMA_getTableOptionDiv(
297 $comment, $tbl_collation, $tbl_storage_engine,
298 $is_myisam_or_aria, $is_isam, $pack_keys,
299 $auto_increment,
300 (empty($delay_key_write) ? '0' : '1'),
301 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
302 ((isset($page_checksum)) ? $page_checksum : ''),
303 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ? '0' : '1')
308 * Copy table
310 $response->addHTML(PMA_getHtmlForCopytable());
313 * Table maintenance
315 $response->addHTML(
316 PMA_getHtmlForTableMaintenance(
317 $is_myisam_or_aria,
318 $is_innodb,
319 $is_berkeleydb,
320 $url_params
324 if (! (isset($db_is_information_schema) && $db_is_information_schema)) {
325 $truncate_table_url_params = array();
326 $drop_table_url_params = array();
328 if (! $tbl_is_view
329 && ! (isset($db_is_information_schema) && $db_is_information_schema)
331 $this_sql_query = 'TRUNCATE TABLE '
332 . PMA_Util::backquote($GLOBALS['table']);
333 $truncate_table_url_params = array_merge(
334 $url_params,
335 array(
336 'sql_query' => $this_sql_query,
337 'goto' => 'tbl_structure.php',
338 'reload' => '1',
339 'message_to_show' => sprintf(
340 __('Table %s has been emptied'),
341 htmlspecialchars($table)
346 if (! (isset($db_is_information_schema) && $db_is_information_schema)) {
347 $this_sql_query = 'DROP TABLE '
348 . PMA_Util::backquote($GLOBALS['table']);
349 $drop_table_url_params = array_merge(
350 $url_params,
351 array(
352 'sql_query' => $this_sql_query,
353 'goto' => 'db_operations.php',
354 'reload' => '1',
355 'purge' => '1',
356 'message_to_show' => sprintf(
357 ($tbl_is_view
358 ? __('View %s has been dropped')
359 : __('Table %s has been dropped')
361 htmlspecialchars($table)
363 // table name is needed to avoid running
364 // PMA_relationsCleanupDatabase() on the whole db later
365 'table' => $GLOBALS['table'],
369 $response->addHTML(
370 PMA_getHtmlForDeleteDataOrTable(
371 $truncate_table_url_params,
372 $drop_table_url_params
377 if (PMA_Partition::havePartitioning()) {
378 $partition_names = PMA_Partition::getPartitionNames($db, $table);
379 // show the Partition maintenance section only if we detect a partition
380 if (! is_null($partition_names[0])) {
381 $response->addHTML(
382 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
384 } // end if
385 } // end if
386 unset($partition_names);
388 // Referential integrity check
389 // The Referential integrity check was intended for the non-InnoDB
390 // tables for which the relations are defined in pmadb
391 // so I assume that if the current table is InnoDB, I don't display
392 // this choice (InnoDB maintains integrity by itself)
394 if ($cfgRelation['relwork'] && ! $is_innodb) {
395 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
396 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table']);
398 if ($foreign) {
399 $response->addHTML(
400 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
402 } // end if ($foreign)
404 } // end if (!empty($cfg['Server']['relation']))
406 $response->addHTML('</div>');