Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / db_operations.php
blob4e17da8e355e65ab29d00e399b45a32c8af50cfc
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * handles miscellaneous db operations:
5 * - move/rename
6 * - copy
7 * - changing collation
8 * - changing comment
9 * - adding tables
10 * - viewing PDF schemas
12 * @package phpMyAdmin
15 /**
16 * requirements
18 require_once './libraries/common.inc.php';
19 require_once './libraries/mysql_charsets.lib.php';
21 // add blobstreaming library functions
22 require_once "./libraries/blobstreaming.lib.php";
24 // add a javascript file for jQuery functions to handle Ajax actions
25 // also add jQueryUI
26 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
27 $GLOBALS['js_include'][] = 'db_operations.js';
29 /**
30 * Rename/move or copy database
32 if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) {
34 if (! empty($db_rename)) {
35 $move = true;
36 } else {
37 $move = false;
40 if (! isset($newname) || ! strlen($newname)) {
41 $message = PMA_Message::error(__('The database name is empty!'));
42 } else {
43 $sql_query = ''; // in case target db exists
44 $_error = false;
45 if ($move || (isset($create_database_before_copying) && $create_database_before_copying)) {
46 // lower_case_table_names=1 `DB` becomes `db`
47 if (!PMA_DRIZZLE) {
48 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
49 if ($lower_case_table_names === '1') {
50 $newname = PMA_strtolower($newname);
54 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
55 if (isset($db_collation)) {
56 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
58 $local_query .= ';';
59 $sql_query = $local_query;
60 // save the original db name because Tracker.class.php which
61 // may be called under PMA_DBI_query() changes $GLOBALS['db']
62 // for some statements, one of which being CREATE DATABASE
63 $original_db = $db;
64 PMA_DBI_query($local_query);
65 $db = $original_db;
66 unset($original_db);
68 // rebuild the database list because PMA_Table::moveCopy
69 // checks in this list if the target db exists
70 $GLOBALS['pma']->databases->build();
73 // here I don't use DELIMITER because it's not part of the
74 // language; I have to send each statement one by one
76 // to avoid selecting alternatively the current and new db
77 // we would need to modify the CREATE definitions to qualify
78 // the db name
79 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
80 if ($procedure_names) {
81 foreach($procedure_names as $procedure_name) {
82 PMA_DBI_select_db($db);
83 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
84 // collect for later display
85 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
86 PMA_DBI_select_db($newname);
87 PMA_DBI_query($tmp_query);
91 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
92 if ($function_names) {
93 foreach($function_names as $function_name) {
94 PMA_DBI_select_db($db);
95 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
96 // collect for later display
97 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
98 PMA_DBI_select_db($newname);
99 PMA_DBI_query($tmp_query);
103 // go back to current db, just in case
104 PMA_DBI_select_db($db);
106 $GLOBALS['sql_constraints_query_full_db'] = array();
108 $tables_full = PMA_DBI_get_tables_full($db);
109 $views = array();
111 // remove all foreign key constraints, otherwise we can get errors
112 require_once './libraries/export/sql.php';
113 foreach ($tables_full as $each_table => $tmp) {
114 $sql_constraints = '';
115 $sql_drop_foreign_keys = '';
116 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
117 if ($move && ! empty($sql_drop_foreign_keys)) {
118 PMA_DBI_query($sql_drop_foreign_keys);
120 // keep the constraint we just dropped
121 if (! empty($sql_constraints)) {
122 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
125 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
128 foreach ($tables_full as $each_table => $tmp) {
129 // to be able to rename a db containing views,
130 // first all the views are collected and a stand-in is created
131 // the real views are created after the tables
132 if (PMA_Table::isView($db, $each_table)) {
133 $views[] = $each_table;
134 // Create stand-in definition to resolve view dependencies
135 $sql_view_standin = PMA_getTableDefStandIn($db, $each_table, "\n");
136 PMA_DBI_query($sql_view_standin);
137 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin . ';';
138 continue;
141 $back = $sql_query;
142 $sql_query = '';
144 // value of $what for this table only
145 $this_what = $what;
147 // do not copy the data from a Merge table
148 // note: on the calling FORM, 'data' means 'structure and data'
149 if (PMA_Table::isMerge($db, $each_table)) {
150 if ($this_what == 'data') {
151 $this_what = 'structure';
153 if ($this_what == 'dataonly') {
154 $this_what = 'nocopy';
158 if ($this_what != 'nocopy') {
159 // keep the triggers from the original db+table
160 // (third param is empty because delimiters are only intended
161 // for importing via the mysql client or our Import feature)
162 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
164 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
165 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
167 $_error = true;
168 // $sql_query is filled by PMA_Table::moveCopy()
169 $sql_query = $back . $sql_query;
170 break;
172 // apply the triggers to the destination db+table
173 if ($triggers) {
174 PMA_DBI_select_db($newname);
175 foreach ($triggers as $trigger) {
176 PMA_DBI_query($trigger['create']);
178 unset($trigger);
180 unset($triggers);
182 // this does not apply to a rename operation
183 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
184 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
185 unset($GLOBALS['sql_constraints_query']);
188 // $sql_query is filled by PMA_Table::moveCopy()
189 $sql_query = $back . $sql_query;
190 } // end (foreach)
191 unset($each_table);
193 // handle the views
194 if (! $_error) {
195 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
196 // to remove stand-in VIEW that was created earlier
197 if (isset($GLOBALS['drop_if_exists'])) {
198 $temp_drop_if_exists = $GLOBALS['drop_if_exists'];
200 $GLOBALS['drop_if_exists'] = 'true';
202 foreach ($views as $view) {
203 if (! PMA_Table::moveCopy($db, $view, $newname, $view, 'structure', $move, 'db_copy')) {
204 $_error = true;
205 break;
208 unset($GLOBALS['drop_if_exists']);
209 if (isset($temp_drop_if_exists)) {
210 // restore previous value
211 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
212 unset($temp_drop_if_exists);
215 unset($view, $views);
217 // now that all tables exist, create all the accumulated constraints
218 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
219 PMA_DBI_select_db($newname);
220 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
221 PMA_DBI_query($one_query);
222 // and prepare to display them
223 $GLOBALS['sql_query'] .= "\n" . $one_query;
226 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
229 if (!PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
230 // here DELIMITER is not used because it's not part of the
231 // language; each statement is sent one by one
233 // to avoid selecting alternatively the current and new db
234 // we would need to modify the CREATE definitions to qualify
235 // the db name
236 $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddSlashes($db,true) . '\';');
237 if ($event_names) {
238 foreach($event_names as $event_name) {
239 PMA_DBI_select_db($db);
240 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
241 // collect for later display
242 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
243 PMA_DBI_select_db($newname);
244 PMA_DBI_query($tmp_query);
249 // go back to current db, just in case
250 PMA_DBI_select_db($db);
252 // Duplicate the bookmarks for this db (done once for each db)
253 if (! $_error && $db != $newname) {
254 $get_fields = array('user', 'label', 'query');
255 $where_fields = array('dbase' => $db);
256 $new_fields = array('dbase' => $newname);
257 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
258 $where_fields, $new_fields);
261 if (! $_error && $move) {
263 * cleanup pmadb stuff for this db
265 require_once './libraries/relation_cleanup.lib.php';
266 PMA_relationsCleanupDatabase($db);
268 // if someday the RENAME DATABASE reappears, do not DROP
269 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
270 $sql_query .= "\n" . $local_query;
271 PMA_DBI_query($local_query);
273 $message = PMA_Message::success(__('Database %s has been renamed to %s'));
274 $message->addParam($db);
275 $message->addParam($newname);
276 } elseif (! $_error) {
277 $message = PMA_Message::success(__('Database %s has been copied to %s'));
278 $message->addParam($db);
279 $message->addParam($newname);
281 $reload = true;
283 /* Change database to be used */
284 if (! $_error && $move) {
285 $db = $newname;
286 } elseif (! $_error) {
287 if (isset($switch_to_new) && $switch_to_new == 'true') {
288 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
289 $db = $newname;
290 } else {
291 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
295 if ($_error && ! isset($message)) {
296 $message = PMA_Message::error();
301 * Database has been successfully renamed/moved. If in an Ajax request,
302 * generate the output with {@link PMA_ajaxResponse} and exit
304 if( $GLOBALS['is_ajax_request'] == true) {
305 $extra_data['newname'] = $newname;
306 $extra_data['sql_query'] = PMA_showMessage(NULL, $sql_query);
307 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
313 * Settings for relations stuff
316 $cfgRelation = PMA_getRelationsParam();
319 * Check if comments were updated
320 * (must be done before displaying the menu tabs)
322 if (isset($_REQUEST['comment'])) {
323 PMA_setDbComment($db, $comment);
327 * Prepares the tables list if the user where not redirected to this script
328 * because there is no table in the database ($is_info is true)
330 if (empty($is_info)) {
331 require './libraries/db_common.inc.php';
332 $url_query .= '&amp;goto=db_operations.php';
334 // Gets the database structure
335 $sub_part = '_structure';
336 require './libraries/db_info.inc.php';
337 echo "\n";
339 if (isset($message)) {
340 PMA_showMessage($message, $sql_query);
341 unset($message);
345 $db_collation = PMA_getDbCollation($db);
346 if (strtolower($db) == 'information_schema' || (PMA_DRIZZLE && strtolower($db) == 'data_dictionary')) {
347 $is_information_schema = true;
348 } else {
349 $is_information_schema = false;
352 if (!$is_information_schema) {
353 if ($cfgRelation['commwork']) {
355 * database comment
358 <div class="operations_half_width">
359 <form method="post" action="db_operations.php">
360 <?php echo PMA_generate_common_hidden_inputs($db); ?>
361 <fieldset>
362 <legend>
363 <?php echo PMA_getIcon('b_comment.png', __('Database comment: '), false, true); ?>
364 </legend>
365 <input type="text" name="comment" class="textfield" size="30"
366 value="<?php
367 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
368 </fieldset>
369 <fieldset class="tblFooters">
370 <input type="submit" value="<?php echo __('Go'); ?>" />
371 </fieldset>
372 </form>
373 </div>
374 <?php
377 <div class="operations_half_width">
378 <?php require './libraries/display_create_table.lib.php'; ?>
379 </div>
380 <?php
382 * rename database
384 if ($db != 'mysql') {
386 <div class="operations_half_width">
387 <form id="rename_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
388 onsubmit="return emptyFormElements(this, 'newname')">
389 <?php
390 if (isset($db_collation)) {
391 echo '<input type="hidden" name="db_collation" value="' . $db_collation
392 .'" />' . "\n";
395 <input type="hidden" name="what" value="data" />
396 <input type="hidden" name="db_rename" value="true" />
397 <?php echo PMA_generate_common_hidden_inputs($db); ?>
398 <fieldset>
399 <legend>
400 <?php
401 if ($cfg['PropertiesIconic']) {
402 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
403 .' alt="" width="16" height="16" />';
405 echo __('Rename database to') . ':';
407 </legend>
408 <input id="new_db_name" type="text" name="newname" size="30" class="textfield" value="" />
409 <?php
410 echo '(' . __('Command') . ': ';
412 * @todo (see explanations above in a previous todo)
414 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
415 // echo 'RENAME DATABASE';
416 //} else {
417 echo 'INSERT INTO ... SELECT';
419 echo ')'; ?>
420 </fieldset>
421 <fieldset class="tblFooters">
422 <input id="rename_db_input" type="submit" value="<?php echo __('Go'); ?>" />
423 </fieldset>
424 </form>
425 </div>
426 <?php
427 } // end if
429 // Drop link if allowed
430 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
431 // Don't allow to easily drop mysql database, RFE #1327514.
432 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
433 && !$db_is_information_schema
434 && (PMA_DRIZZLE || $db != 'mysql')) {
436 <div class="operations_half_width">
437 <fieldset class="caution">
438 <legend><?php
439 if ($cfg['PropertiesIconic']) {
440 echo '<img class="icon" src="' . $pmaThemeImage . 'b_deltbl.png"'
441 .' alt="" width="16" height="16" />';
443 echo __('Remove database');
444 ?></legend>
446 <ul>
447 <?php
448 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
449 $this_url_params = array(
450 'sql_query' => $this_sql_query,
451 'back' => 'db_operations.php',
452 'goto' => 'main.php',
453 'reload' => '1',
454 'purge' => '1',
455 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
456 'db' => NULL,
459 <li><a href="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? 'id="drop_db_anchor"' : ''); ?>>
460 <?php echo __('Drop the database (DROP)'); ?></a>
461 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?>
462 </li>
463 </ul>
464 </fieldset>
465 </div>
466 <?php } ?>
467 <?php
469 * Copy database
472 <div class="operations_half_width clearfloat">
473 <form id="copy_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
474 onsubmit="return emptyFormElements(this, 'newname')">
475 <?php
476 if (isset($db_collation)) {
477 echo '<input type="hidden" name="db_collation" value="' . $db_collation
478 .'" />' . "\n";
480 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
481 echo PMA_generate_common_hidden_inputs($db);
483 <fieldset>
484 <legend>
485 <?php
486 if ($cfg['PropertiesIconic']) {
487 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
488 .' alt="" width="16" height="16" />';
490 echo __('Copy database to') . ':';
491 $drop_clause = 'DROP TABLE / DROP VIEW';
493 </legend>
494 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
495 <?php
496 $choices = array(
497 'structure' => __('Structure only'),
498 'data' => __('Structure and data'),
499 'dataonly' => __('Data only'));
500 PMA_display_html_radio('what', $choices, 'data', true);
501 unset($choices);
503 <input type="checkbox" name="create_database_before_copying" value="1"
504 id="checkbox_create_database_before_copying"
505 checked="checked" />
506 <label for="checkbox_create_database_before_copying">
507 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
508 <input type="checkbox" name="drop_if_exists" value="true"
509 id="checkbox_drop" />
510 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
511 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
512 id="checkbox_auto_increment" />
513 <label for="checkbox_auto_increment">
514 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
515 <input type="checkbox" name="add_constraints" value="1"
516 id="checkbox_constraints" />
517 <label for="checkbox_constraints">
518 <?php echo __('Add constraints'); ?></label><br />
519 <?php
520 unset($drop_clause);
522 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
523 && $_COOKIE['pma_switch_to_new'] == 'true') {
524 $pma_switch_to_new = 'true';
527 <input type="checkbox" name="switch_to_new" value="true"
528 id="checkbox_switch"
529 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
531 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
532 </fieldset>
533 <fieldset class="tblFooters">
534 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
535 </fieldset>
536 </form>
537 </div>
538 <?php
541 * Change database charset
543 echo '<div class="operations_half_width"><form id="change_db_charset_form" ';
544 if ($GLOBALS['cfg']['AjaxEnable']) {
545 echo ' class="ajax" ';
547 echo 'method="post" action="./db_operations.php">'
548 . PMA_generate_common_hidden_inputs($db, $table)
549 . '<fieldset>' . "\n"
550 . ' <legend>';
551 if ($cfg['PropertiesIconic']) {
552 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
553 .' alt="" width="16" height="16" />';
555 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
556 . ' </legend>' . "\n"
557 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
558 'db_collation', 'select_db_collation', $db_collation, false, 3)
559 . '</fieldset>'
560 . '<fieldset class="tblFooters">'
561 . ' <input type="submit" name="submitcollation"'
562 . ' value="' . __('Go') . '" />' . "\n"
563 . '</fieldset>' . "\n"
564 . '</form></div>' . "\n";
566 if ($num_tables > 0
567 && ! $cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
568 $message = PMA_Message::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
569 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
570 $message->addParam('</a>', false);
571 /* Show error if user has configured something, notice elsewhere */
572 if (!empty($cfg['Servers'][$server]['pmadb'])) {
573 $message->isError(true);
575 echo '<div class="operations_full_width">';
576 $message->display();
577 echo '</div>';
578 } // end if
579 } // end if (!$is_information_schema)
582 // not sure about displaying the PDF dialog in case db is information_schema
583 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
584 <!-- Work on PDF Pages -->
586 <?php
587 // We only show this if we find something in the new pdf_pages table
589 $test_query = '
590 SELECT *
591 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
592 WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'';
593 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
596 * Export Relational Schema View
598 echo '<div class="operations_full_width"><fieldset><a href="schema_edit.php?' . $url_query . '">';
599 if ($cfg['PropertiesIconic']) {
600 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
601 .' alt="" width="16" height="16" />';
603 echo __('Edit or export relational schema') . '</a></fieldset></div>';
604 } // end if
607 * Displays the footer
609 require './libraries/footer.inc.php';