Merge branch 'QA_3_3'
[phpmyadmin-themes.git] / db_operations.php
blobb5d7f5a353bd5296531131fa59e6f4e41aeefa07
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 ||
46 (isset($create_database_before_copying) && $create_database_before_copying)) {
47 // lower_case_table_names=1 `DB` becomes `db`
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 = strtolower($newname);
53 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
54 if (isset($db_collation)) {
55 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
57 $local_query .= ';';
58 $sql_query = $local_query;
59 // save the original db name because Tracker.class.php which
60 // may be called under PMA_DBI_query() changes $GLOBALS['db']
61 // for some statements, one of which being CREATE DATABASE
62 $original_db = $db;
63 PMA_DBI_query($local_query);
64 $db = $original_db;
65 unset($original_db);
67 // rebuild the database list because PMA_Table::moveCopy
68 // checks in this list if the target db exists
69 $GLOBALS['pma']->databases->build();
72 if (PMA_MYSQL_INT_VERSION >= 50000) {
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 if (isset($GLOBALS['add_constraints']) || $move) {
107 $GLOBALS['sql_constraints_query_full_db'] = array();
110 $tables_full = PMA_DBI_get_tables_full($db);
111 $views = array();
113 // remove all foreign key constraints, otherwise we can get errors
114 require_once './libraries/export/sql.php';
115 foreach ($tables_full as $each_table => $tmp) {
116 $sql_constraints = '';
117 $sql_drop_foreign_keys = '';
118 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
119 if ($move && ! empty($sql_drop_foreign_keys)) {
120 PMA_DBI_query($sql_drop_foreign_keys);
122 // keep the constraint we just dropped
123 if (! empty($sql_constraints)) {
124 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
127 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
130 foreach ($tables_full as $each_table => $tmp) {
131 // to be able to rename a db containing views, we
132 // first collect in $views all the views we find and we
133 // will handle them after the tables
135 * @todo support a view of a view
137 if (PMA_Table::isView($db, $each_table)) {
138 $views[] = $each_table;
139 continue;
142 $back = $sql_query;
143 $sql_query = '';
145 // value of $what for this table only
146 $this_what = $what;
148 // do not copy the data from a Merge table
149 // note: on the calling FORM, 'data' means 'structure and data'
150 if (PMA_Table::isMerge($db, $each_table)) {
151 if ($this_what == 'data') {
152 $this_what = 'structure';
154 if ($this_what == 'dataonly') {
155 $this_what = 'nocopy';
159 if ($this_what != 'nocopy') {
160 // keep the triggers from the original db+table
161 // (third param is empty because delimiters are only intended
162 // for importing via the mysql client or our Import feature)
163 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
165 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
166 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
168 $_error = true;
169 // $sql_query is filled by PMA_Table::moveCopy()
170 $sql_query = $back . $sql_query;
171 break;
173 // apply the triggers to the destination db+table
174 if ($triggers) {
175 PMA_DBI_select_db($newname);
176 foreach ($triggers as $trigger) {
177 PMA_DBI_query($trigger['create']);
179 unset($trigger);
181 unset($triggers);
183 // this does not apply to a rename operation
184 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
185 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
186 unset($GLOBALS['sql_constraints_query']);
189 // $sql_query is filled by PMA_Table::moveCopy()
190 $sql_query = $back . $sql_query;
191 } // end (foreach)
192 unset($each_table);
194 // handle the views
195 if (! $_error) {
196 foreach ($views as $view) {
197 if (! PMA_Table::moveCopy($db, $view, $newname, $view,
198 'structure', $move, 'db_copy')) {
199 $_error = true;
200 break;
204 unset($view, $views);
206 // now that all tables exist, create all the accumulated constraints
207 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
208 PMA_DBI_select_db($newname);
209 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
210 PMA_DBI_query($one_query);
211 // and prepare to display them
212 $GLOBALS['sql_query'] .= "\n" . $one_query;
215 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
218 if (PMA_MYSQL_INT_VERSION >= 50100) {
219 // here DELIMITER is not used because it's not part of the
220 // language; each statement is sent one by one
222 // to avoid selecting alternatively the current and new db
223 // we would need to modify the CREATE definitions to qualify
224 // the db name
225 $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddslashes($db,true) . '\';');
226 if ($event_names) {
227 foreach($event_names as $event_name) {
228 PMA_DBI_select_db($db);
229 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
230 // collect for later display
231 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
232 PMA_DBI_select_db($newname);
233 PMA_DBI_query($tmp_query);
237 // go back to current db, just in case
238 PMA_DBI_select_db($db);
240 // Duplicate the bookmarks for this db (done once for each db)
241 if (! $_error && $db != $newname) {
242 $get_fields = array('user', 'label', 'query');
243 $where_fields = array('dbase' => $db);
244 $new_fields = array('dbase' => $newname);
245 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
246 $where_fields, $new_fields);
249 if (! $_error && $move) {
251 * cleanup pmadb stuff for this db
253 require_once './libraries/relation_cleanup.lib.php';
254 PMA_relationsCleanupDatabase($db);
256 // if someday the RENAME DATABASE reappears, do not DROP
257 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
258 $sql_query .= "\n" . $local_query;
259 PMA_DBI_query($local_query);
261 $message = PMA_Message::success(__('Database %s has been renamed to %s'));
262 $message->addParam($db);
263 $message->addParam($newname);
264 } elseif (! $_error) {
265 $message = PMA_Message::success(__('Database %s has been copied to %s'));
266 $message->addParam($db);
267 $message->addParam($newname);
269 $reload = true;
271 /* Change database to be used */
272 if (! $_error && $move) {
273 $db = $newname;
274 } elseif (! $_error) {
275 if (isset($switch_to_new) && $switch_to_new == 'true') {
276 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
277 $db = $newname;
278 } else {
279 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
283 if ($_error && ! isset($message)) {
284 $message = PMA_Message::error();
289 * Database has been successfully renamed/moved. If in an Ajax request,
290 * generate the output with {@link PMA_ajaxResponse} and exit
292 if( $GLOBALS['is_ajax_request'] == true) {
293 $extra_data['newname'] = $newname;
294 $extra_data['sql_query'] = PMA_showMessage(NULL, $sql_query);
295 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
301 * Settings for relations stuff
304 $cfgRelation = PMA_getRelationsParam();
307 * Check if comments were updated
308 * (must be done before displaying the menu tabs)
310 if (isset($_REQUEST['comment'])) {
311 PMA_setDbComment($db, $comment);
315 * Prepares the tables list if the user where not redirected to this script
316 * because there is no table in the database ($is_info is true)
318 if (empty($is_info)) {
319 require './libraries/db_common.inc.php';
320 $url_query .= '&amp;goto=db_operations.php';
322 // Gets the database structure
323 $sub_part = '_structure';
324 require './libraries/db_info.inc.php';
325 echo "\n";
327 if (isset($message)) {
328 PMA_showMessage($message, $sql_query);
329 unset($message);
333 $db_collation = PMA_getDbCollation($db);
334 if ($db == 'information_schema') {
335 $is_information_schema = true;
336 } else {
337 $is_information_schema = false;
340 if (!$is_information_schema) {
342 require './libraries/display_create_table.lib.php';
344 if ($cfgRelation['commwork']) {
346 * database comment
349 <form method="post" action="db_operations.php">
350 <?php echo PMA_generate_common_hidden_inputs($db); ?>
351 <fieldset>
352 <legend>
353 <?php echo PMA_getIcon('b_comment.png', __('Database comment: '), false, true); ?>
354 </legend>
355 <input type="text" name="comment" class="textfield" size="30"
356 value="<?php
357 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
358 </fieldset>
359 <fieldset class="tblFooters">
360 <input type="submit" value="<?php echo __('Go'); ?>" />
361 </fieldset>
362 </form>
363 <?php
366 * rename database
368 if ($db != 'mysql') {
370 <form id="rename_db_form" method="post" action="db_operations.php"
371 onsubmit="return emptyFormElements(this, 'newname')">
372 <?php
373 if (isset($db_collation)) {
374 echo '<input type="hidden" name="db_collation" value="' . $db_collation
375 .'" />' . "\n";
378 <input type="hidden" name="what" value="data" />
379 <input type="hidden" name="db_rename" value="true" />
380 <?php echo PMA_generate_common_hidden_inputs($db); ?>
381 <fieldset>
382 <legend>
383 <?php
384 if ($cfg['PropertiesIconic']) {
385 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
386 .' alt="" width="16" height="16" />';
388 echo __('Rename database to') . ':';
390 </legend>
391 <input type="text" name="newname" size="30" class="textfield" value="" />
392 <?php
393 echo '(' . __('Command') . ': ';
395 * @todo (see explanations above in a previous todo)
397 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
398 // echo 'RENAME DATABASE';
399 //} else {
400 echo 'INSERT INTO ... SELECT';
402 echo ')'; ?>
403 </fieldset>
404 <fieldset class="tblFooters">
405 <input id="rename_db_input" type="submit" value="<?php echo __('Go'); ?>" />
406 </fieldset>
407 </form>
408 <?php
409 } // end if
411 // Drop link if allowed
412 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
413 // Don't allow to easily drop mysql database, RFE #1327514.
414 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase']) && ! $db_is_information_schema && ($db != 'mysql')) {
416 <fieldset class="caution">
417 <legend><?php
418 if ($cfg['PropertiesIconic']) {
419 echo '<img class="icon" src="' . $pmaThemeImage . 'b_deltbl.png"'
420 .' alt="" width="16" height="16" />';
422 echo __('Remove database');
423 ?></legend>
425 <ul>
426 <?php
427 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
428 $this_url_params = array(
429 'sql_query' => $this_sql_query,
430 'back' => 'db_operations.php',
431 'goto' => 'main.php',
432 'reload' => '1',
433 'purge' => '1',
434 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
435 'db' => NULL,
438 <li><a href="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" onclick="return confirmLinkDropDB(this, '<?php echo PMA_jsFormat($this_sql_query); ?>')">
439 <?php echo __('Drop the database (DROP)'); ?></a>
440 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?>
441 </li>
442 </ul>
443 </fieldset>
444 <?php } ?>
445 <?php
447 * Copy database
450 <form id="copy_db_form" method="post" action="db_operations.php"
451 onsubmit="return emptyFormElements(this, 'newname')">
452 <?php
453 if (isset($db_collation)) {
454 echo '<input type="hidden" name="db_collation" value="' . $db_collation
455 .'" />' . "\n";
457 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
458 echo PMA_generate_common_hidden_inputs($db);
460 <fieldset>
461 <legend>
462 <?php
463 if ($cfg['PropertiesIconic']) {
464 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
465 .' alt="" width="16" height="16" />';
467 echo __('Copy database to') . ':';
468 $drop_clause = 'DROP TABLE / DROP VIEW';
470 </legend>
471 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
472 <?php
473 $choices = array(
474 'structure' => __('Structure only'),
475 'data' => __('Structure and data'),
476 'dataonly' => __('Data only'));
477 PMA_display_html_radio('what', $choices, 'data', true);
478 unset($choices);
480 <input type="checkbox" name="create_database_before_copying" value="1"
481 id="checkbox_create_database_before_copying"
482 checked="checked" />
483 <label for="checkbox_create_database_before_copying">
484 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
485 <input type="checkbox" name="drop_if_exists" value="true"
486 id="checkbox_drop" />
487 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
488 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
489 id="checkbox_auto_increment" />
490 <label for="checkbox_auto_increment">
491 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
492 <input type="checkbox" name="add_constraints" value="1"
493 id="checkbox_constraints" />
494 <label for="checkbox_constraints">
495 <?php echo __('Add constraints'); ?></label><br />
496 <?php
497 unset($drop_clause);
499 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
500 && $_COOKIE['pma_switch_to_new'] == 'true') {
501 $pma_switch_to_new = 'true';
504 <input type="checkbox" name="switch_to_new" value="true"
505 id="checkbox_switch"
506 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
508 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
509 </fieldset>
510 <fieldset class="tblFooters">
511 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
512 </fieldset>
513 </form>
515 <?php
518 * Change database charset
520 echo '<form id="change_db_charset_form" method="post" action="./db_operations.php">' . "\n"
521 . PMA_generate_common_hidden_inputs($db, $table)
522 . '<fieldset>' . "\n"
523 . ' <legend>';
524 if ($cfg['PropertiesIconic']) {
525 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
526 .' alt="" width="16" height="16" />';
528 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
529 . ' </legend>' . "\n"
530 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
531 'db_collation', 'select_db_collation', $db_collation, false, 3)
532 . '</fieldset>'
533 . '<fieldset class="tblFooters">'
534 . ' <input type="submit" name="submitcollation"'
535 . ' value="' . __('Go') . '" />' . "\n"
536 . '</fieldset>' . "\n"
537 . '</form>' . "\n";
539 if ($num_tables > 0
540 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
541 $message = PMA_Message::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
542 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
543 $message->addParam('</a>', false);
544 /* Show error if user has configured something, notice elsewhere */
545 if (!empty($cfg['Servers'][$server]['pmadb'])) {
546 $message->isError(true);
548 $message->display();
549 } // end if
550 } // end if (!$is_information_schema)
553 // not sure about displaying the PDF dialog in case db is information_schema
554 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
555 <!-- Work on PDF Pages -->
557 <?php
558 // We only show this if we find something in the new pdf_pages table
560 $test_query = '
561 SELECT *
562 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
563 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
564 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
567 * Export Relational Schema View
569 echo '<fieldset><a href="schema_edit.php?' . $url_query . '">';
570 if ($cfg['PropertiesIconic']) {
571 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
572 .' alt="" width="16" height="16" />';
574 echo __('Edit or export relational schema') . '</a></fieldset>';
575 } // end if
578 * Displays the footer
580 require './libraries/footer.inc.php';