Translation update done using Pootle.
[phpmyadmin/madhuracj.git] / db_operations.php
blobb20c2f6ced6d5f18db81773c4b660a7f098806b3
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.16.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 include_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);
127 foreach ($tables_full as $each_table => $tmp) {
128 // to be able to rename a db containing views,
129 // first all the views are collected and a stand-in is created
130 // the real views are created after the tables
131 if (PMA_Table::isView($db, $each_table)) {
132 $views[] = $each_table;
133 // Create stand-in definition to resolve view dependencies
134 $sql_view_standin = PMA_getTableDefStandIn($db, $each_table, "\n");
135 PMA_DBI_select_db($newname);
136 PMA_DBI_query($sql_view_standin);
137 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin . ';';
141 foreach ($tables_full as $each_table => $tmp) {
142 // skip the views; we have creted stand-in definitions
143 if (PMA_Table::isView($db, $each_table)) {
144 continue;
146 $back = $sql_query;
147 $sql_query = '';
149 // value of $what for this table only
150 $this_what = $what;
152 // do not copy the data from a Merge table
153 // note: on the calling FORM, 'data' means 'structure and data'
154 if (PMA_Table::isMerge($db, $each_table)) {
155 if ($this_what == 'data') {
156 $this_what = 'structure';
158 if ($this_what == 'dataonly') {
159 $this_what = 'nocopy';
163 if ($this_what != 'nocopy') {
164 // keep the triggers from the original db+table
165 // (third param is empty because delimiters are only intended
166 // for importing via the mysql client or our Import feature)
167 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
169 if (! PMA_Table::moveCopy(
170 $db, $each_table, $newname, $each_table,
171 isset($this_what) ? $this_what : 'data', $move, 'db_copy')
173 $_error = true;
174 // $sql_query is filled by PMA_Table::moveCopy()
175 $sql_query = $back . $sql_query;
176 break;
178 // apply the triggers to the destination db+table
179 if ($triggers) {
180 PMA_DBI_select_db($newname);
181 foreach ($triggers as $trigger) {
182 PMA_DBI_query($trigger['create']);
184 unset($trigger);
186 unset($triggers);
188 // this does not apply to a rename operation
189 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
190 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
191 unset($GLOBALS['sql_constraints_query']);
194 // $sql_query is filled by PMA_Table::moveCopy()
195 $sql_query = $back . $sql_query;
196 } // end (foreach)
197 unset($each_table);
199 // handle the views
200 if (! $_error) {
201 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
202 // to remove stand-in VIEW that was created earlier
203 if (isset($GLOBALS['drop_if_exists'])) {
204 $temp_drop_if_exists = $GLOBALS['drop_if_exists'];
206 $GLOBALS['drop_if_exists'] = 'true';
208 foreach ($views as $view) {
209 if (! PMA_Table::moveCopy($db, $view, $newname, $view, 'structure', $move, 'db_copy')) {
210 $_error = true;
211 break;
214 unset($GLOBALS['drop_if_exists']);
215 if (isset($temp_drop_if_exists)) {
216 // restore previous value
217 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
218 unset($temp_drop_if_exists);
221 unset($view, $views);
223 // now that all tables exist, create all the accumulated constraints
224 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
225 PMA_DBI_select_db($newname);
226 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
227 PMA_DBI_query($one_query);
228 // and prepare to display them
229 $GLOBALS['sql_query'] .= "\n" . $one_query;
232 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
235 if (!PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
236 // here DELIMITER is not used because it's not part of the
237 // language; each statement is sent one by one
239 // to avoid selecting alternatively the current and new db
240 // we would need to modify the CREATE definitions to qualify
241 // the db name
242 $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddSlashes($db, true) . '\';');
243 if ($event_names) {
244 foreach ($event_names as $event_name) {
245 PMA_DBI_select_db($db);
246 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
247 // collect for later display
248 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
249 PMA_DBI_select_db($newname);
250 PMA_DBI_query($tmp_query);
255 // go back to current db, just in case
256 PMA_DBI_select_db($db);
258 // Duplicate the bookmarks for this db (done once for each db)
259 if (! $_error && $db != $newname) {
260 $get_fields = array('user', 'label', 'query');
261 $where_fields = array('dbase' => $db);
262 $new_fields = array('dbase' => $newname);
263 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
264 $where_fields, $new_fields);
267 if (! $_error && $move) {
269 * cleanup pmadb stuff for this db
271 include_once './libraries/relation_cleanup.lib.php';
272 PMA_relationsCleanupDatabase($db);
274 // if someday the RENAME DATABASE reappears, do not DROP
275 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
276 $sql_query .= "\n" . $local_query;
277 PMA_DBI_query($local_query);
279 $message = PMA_Message::success(__('Database %s has been renamed to %s'));
280 $message->addParam($db);
281 $message->addParam($newname);
282 } elseif (! $_error) {
283 $message = PMA_Message::success(__('Database %s has been copied to %s'));
284 $message->addParam($db);
285 $message->addParam($newname);
287 $reload = true;
289 /* Change database to be used */
290 if (! $_error && $move) {
291 $db = $newname;
292 } elseif (! $_error) {
293 if (isset($switch_to_new) && $switch_to_new == 'true') {
294 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
295 $db = $newname;
296 } else {
297 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
301 if ($_error && ! isset($message)) {
302 $message = PMA_Message::error();
307 * Database has been successfully renamed/moved. If in an Ajax request,
308 * generate the output with {@link PMA_ajaxResponse} and exit
310 if ( $GLOBALS['is_ajax_request'] == true) {
311 $extra_data['newname'] = $newname;
312 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
313 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
319 * Settings for relations stuff
322 $cfgRelation = PMA_getRelationsParam();
325 * Check if comments were updated
326 * (must be done before displaying the menu tabs)
328 if (isset($_REQUEST['comment'])) {
329 PMA_setDbComment($db, $comment);
333 * Prepares the tables list if the user where not redirected to this script
334 * because there is no table in the database ($is_info is true)
336 if (empty($is_info)) {
337 include './libraries/db_common.inc.php';
338 $url_query .= '&amp;goto=db_operations.php';
340 // Gets the database structure
341 $sub_part = '_structure';
342 include './libraries/db_info.inc.php';
343 echo "\n";
345 if (isset($message)) {
346 PMA_showMessage($message, $sql_query);
347 unset($message);
351 $db_collation = PMA_getDbCollation($db);
352 $is_information_schema = PMA_is_system_schema($db);
354 if (!$is_information_schema) {
355 if ($cfgRelation['commwork']) {
357 * database comment
360 <div class="operations_half_width">
361 <form method="post" action="db_operations.php">
362 <?php echo PMA_generate_common_hidden_inputs($db); ?>
363 <fieldset>
364 <legend>
365 <?php
366 if ($cfg['PropertiesIconic']) {
367 echo '<img class="icon ic_b_comment" src="themes/dot.gif" alt="" />';
369 echo __('Database comment: ');
371 </legend>
372 <input type="text" name="comment" class="textfield" size="30"
373 value="<?php
374 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
375 </fieldset>
376 <fieldset class="tblFooters">
377 <input type="submit" value="<?php echo __('Go'); ?>" />
378 </fieldset>
379 </form>
380 </div>
381 <?php
384 <div class="operations_half_width">
385 <?php include './libraries/display_create_table.lib.php'; ?>
386 </div>
387 <?php
389 * rename database
391 if ($db != 'mysql') {
393 <div class="operations_half_width">
394 <form id="rename_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
395 onsubmit="return emptyFormElements(this, 'newname')">
396 <?php
397 if (isset($db_collation)) {
398 echo '<input type="hidden" name="db_collation" value="' . $db_collation
399 .'" />' . "\n";
402 <input type="hidden" name="what" value="data" />
403 <input type="hidden" name="db_rename" value="true" />
404 <?php echo PMA_generate_common_hidden_inputs($db); ?>
405 <fieldset>
406 <legend>
407 <?php
408 if ($cfg['PropertiesIconic']) {
409 echo PMA_getImage('b_edit.png');
411 echo __('Rename database to') . ':';
413 </legend>
414 <input id="new_db_name" type="text" name="newname" size="30" class="textfield" value="" />
415 </fieldset>
416 <fieldset class="tblFooters">
417 <input id="rename_db_input" type="submit" value="<?php echo __('Go'); ?>" />
418 </fieldset>
419 </form>
420 </div>
421 <?php
422 } // end if
424 // Drop link if allowed
425 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
426 // Don't allow to easily drop mysql database, RFE #1327514.
427 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
428 && !$db_is_information_schema
429 && (PMA_DRIZZLE || $db != 'mysql')) {
431 <div class="operations_half_width">
432 <fieldset class="caution">
433 <legend><?php
434 if ($cfg['PropertiesIconic']) {
435 echo PMA_getImage('b_deltbl.png');
437 echo __('Remove database');
438 ?></legend>
440 <ul>
441 <?php
442 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
443 $this_url_params = array(
444 'sql_query' => $this_sql_query,
445 'back' => 'db_operations.php',
446 'goto' => 'main.php',
447 'reload' => '1',
448 'purge' => '1',
449 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
450 'db' => null,
453 <li><a href="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? 'id="drop_db_anchor"' : ''); ?>>
454 <?php echo __('Drop the database (DROP)'); ?></a>
455 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?>
456 </li>
457 </ul>
458 </fieldset>
459 </div>
460 <?php } ?>
461 <?php
463 * Copy database
466 <div class="operations_half_width clearfloat">
467 <form id="copy_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
468 onsubmit="return emptyFormElements(this, 'newname')">
469 <?php
470 if (isset($db_collation)) {
471 echo '<input type="hidden" name="db_collation" value="' . $db_collation
472 .'" />' . "\n";
474 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
475 echo PMA_generate_common_hidden_inputs($db);
477 <fieldset>
478 <legend>
479 <?php
480 if ($cfg['PropertiesIconic']) {
481 echo PMA_getImage('b_edit.png');
483 echo __('Copy database to') . ':';
484 $drop_clause = 'DROP TABLE / DROP VIEW';
486 </legend>
487 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
488 <?php
489 $choices = array(
490 'structure' => __('Structure only'),
491 'data' => __('Structure and data'),
492 'dataonly' => __('Data only'));
493 PMA_display_html_radio('what', $choices, 'data', true);
494 unset($choices);
496 <input type="checkbox" name="create_database_before_copying" value="1"
497 id="checkbox_create_database_before_copying"
498 checked="checked" />
499 <label for="checkbox_create_database_before_copying">
500 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
501 <input type="checkbox" name="drop_if_exists" value="true"
502 id="checkbox_drop" />
503 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
504 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
505 id="checkbox_auto_increment" />
506 <label for="checkbox_auto_increment">
507 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
508 <input type="checkbox" name="add_constraints" value="1"
509 id="checkbox_constraints" />
510 <label for="checkbox_constraints">
511 <?php echo __('Add constraints'); ?></label><br />
512 <?php
513 unset($drop_clause);
515 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
516 && $_COOKIE['pma_switch_to_new'] == 'true') {
517 $pma_switch_to_new = 'true';
520 <input type="checkbox" name="switch_to_new" value="true"
521 id="checkbox_switch"
522 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
524 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
525 </fieldset>
526 <fieldset class="tblFooters">
527 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
528 </fieldset>
529 </form>
530 </div>
531 <?php
534 * Change database charset
536 echo '<div class="operations_half_width"><form id="change_db_charset_form" ';
537 if ($GLOBALS['cfg']['AjaxEnable']) {
538 echo ' class="ajax" ';
540 echo 'method="post" action="./db_operations.php">'
541 . PMA_generate_common_hidden_inputs($db, $table)
542 . '<fieldset>' . "\n"
543 . ' <legend>';
544 if ($cfg['PropertiesIconic']) {
545 echo PMA_getImage('s_asci.png');
547 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
548 . ' </legend>' . "\n"
549 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
550 'db_collation', 'select_db_collation', $db_collation, false, 3)
551 . '</fieldset>'
552 . '<fieldset class="tblFooters">'
553 . ' <input type="submit" name="submitcollation"'
554 . ' value="' . __('Go') . '" />' . "\n"
555 . '</fieldset>' . "\n"
556 . '</form></div>' . "\n";
558 if ($num_tables > 0
559 && ! $cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
560 $message = PMA_Message::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
561 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
562 $message->addParam('</a>', false);
563 /* Show error if user has configured something, notice elsewhere */
564 if (!empty($cfg['Servers'][$server]['pmadb'])) {
565 $message->isError(true);
567 echo '<div class="operations_full_width">';
568 $message->display();
569 echo '</div>';
570 } // end if
571 } // end if (!$is_information_schema)
574 // not sure about displaying the PDF dialog in case db is information_schema
575 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
576 <!-- Work on PDF Pages -->
578 <?php
579 // We only show this if we find something in the new pdf_pages table
581 $test_query = '
582 SELECT *
583 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
584 WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'';
585 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
588 * Export Relational Schema View
590 echo '<div class="operations_full_width"><fieldset><a href="schema_edit.php?' . $url_query . '">';
591 if ($cfg['PropertiesIconic']) {
592 echo PMA_getImage('b_edit.png');
594 echo __('Edit or export relational schema') . '</a></fieldset></div>';
595 } // end if
598 * Displays the footer
600 require './libraries/footer.inc.php';