Translation update done using Pootle.
[phpmyadmin/madhuracj.git] / db_operations.php
blobfa0f10894ce410b490ea5eda8a7932e9a2b63502
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 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);
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(
165 $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 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
197 // to remove stand-in VIEW that was created earlier
198 if (isset($GLOBALS['drop_if_exists'])) {
199 $temp_drop_if_exists = $GLOBALS['drop_if_exists'];
201 $GLOBALS['drop_if_exists'] = 'true';
203 foreach ($views as $view) {
204 if (! PMA_Table::moveCopy($db, $view, $newname, $view, 'structure', $move, 'db_copy')) {
205 $_error = true;
206 break;
209 unset($GLOBALS['drop_if_exists']);
210 if (isset($temp_drop_if_exists)) {
211 // restore previous value
212 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
213 unset($temp_drop_if_exists);
216 unset($view, $views);
218 // now that all tables exist, create all the accumulated constraints
219 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
220 PMA_DBI_select_db($newname);
221 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
222 PMA_DBI_query($one_query);
223 // and prepare to display them
224 $GLOBALS['sql_query'] .= "\n" . $one_query;
227 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
230 if (!PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
231 // here DELIMITER is not used because it's not part of the
232 // language; each statement is sent one by one
234 // to avoid selecting alternatively the current and new db
235 // we would need to modify the CREATE definitions to qualify
236 // the db name
237 $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddSlashes($db, true) . '\';');
238 if ($event_names) {
239 foreach ($event_names as $event_name) {
240 PMA_DBI_select_db($db);
241 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
242 // collect for later display
243 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
244 PMA_DBI_select_db($newname);
245 PMA_DBI_query($tmp_query);
250 // go back to current db, just in case
251 PMA_DBI_select_db($db);
253 // Duplicate the bookmarks for this db (done once for each db)
254 if (! $_error && $db != $newname) {
255 $get_fields = array('user', 'label', 'query');
256 $where_fields = array('dbase' => $db);
257 $new_fields = array('dbase' => $newname);
258 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
259 $where_fields, $new_fields);
262 if (! $_error && $move) {
264 * cleanup pmadb stuff for this db
266 include_once './libraries/relation_cleanup.lib.php';
267 PMA_relationsCleanupDatabase($db);
269 // if someday the RENAME DATABASE reappears, do not DROP
270 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
271 $sql_query .= "\n" . $local_query;
272 PMA_DBI_query($local_query);
274 $message = PMA_Message::success(__('Database %s has been renamed to %s'));
275 $message->addParam($db);
276 $message->addParam($newname);
277 } elseif (! $_error) {
278 $message = PMA_Message::success(__('Database %s has been copied to %s'));
279 $message->addParam($db);
280 $message->addParam($newname);
282 $reload = true;
284 /* Change database to be used */
285 if (! $_error && $move) {
286 $db = $newname;
287 } elseif (! $_error) {
288 if (isset($switch_to_new) && $switch_to_new == 'true') {
289 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
290 $db = $newname;
291 } else {
292 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
296 if ($_error && ! isset($message)) {
297 $message = PMA_Message::error();
302 * Database has been successfully renamed/moved. If in an Ajax request,
303 * generate the output with {@link PMA_ajaxResponse} and exit
305 if ( $GLOBALS['is_ajax_request'] == true) {
306 $extra_data['newname'] = $newname;
307 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
308 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
314 * Settings for relations stuff
317 $cfgRelation = PMA_getRelationsParam();
320 * Check if comments were updated
321 * (must be done before displaying the menu tabs)
323 if (isset($_REQUEST['comment'])) {
324 PMA_setDbComment($db, $comment);
328 * Prepares the tables list if the user where not redirected to this script
329 * because there is no table in the database ($is_info is true)
331 if (empty($is_info)) {
332 include './libraries/db_common.inc.php';
333 $url_query .= '&amp;goto=db_operations.php';
335 // Gets the database structure
336 $sub_part = '_structure';
337 include './libraries/db_info.inc.php';
338 echo "\n";
340 if (isset($message)) {
341 PMA_showMessage($message, $sql_query);
342 unset($message);
346 $db_collation = PMA_getDbCollation($db);
347 $is_information_schema = PMA_is_system_schema($db);
349 if (!$is_information_schema) {
350 if ($cfgRelation['commwork']) {
352 * database comment
355 <div class="operations_half_width">
356 <form method="post" action="db_operations.php">
357 <?php echo PMA_generate_common_hidden_inputs($db); ?>
358 <fieldset>
359 <legend>
360 <?php
361 if ($cfg['PropertiesIconic']) {
362 echo '<img class="icon ic_b_comment" src="themes/dot.gif" alt="" />';
364 echo __('Database comment: ');
366 </legend>
367 <input type="text" name="comment" class="textfield" size="30"
368 value="<?php
369 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
370 </fieldset>
371 <fieldset class="tblFooters">
372 <input type="submit" value="<?php echo __('Go'); ?>" />
373 </fieldset>
374 </form>
375 </div>
376 <?php
379 <div class="operations_half_width">
380 <?php include './libraries/display_create_table.lib.php'; ?>
381 </div>
382 <?php
384 * rename database
386 if ($db != 'mysql') {
388 <div class="operations_half_width">
389 <form id="rename_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
390 onsubmit="return emptyFormElements(this, 'newname')">
391 <?php
392 if (isset($db_collation)) {
393 echo '<input type="hidden" name="db_collation" value="' . $db_collation
394 .'" />' . "\n";
397 <input type="hidden" name="what" value="data" />
398 <input type="hidden" name="db_rename" value="true" />
399 <?php echo PMA_generate_common_hidden_inputs($db); ?>
400 <fieldset>
401 <legend>
402 <?php
403 if ($cfg['PropertiesIconic']) {
404 echo PMA_getImage('b_edit.png');
406 echo __('Rename database to') . ':';
408 </legend>
409 <input id="new_db_name" type="text" name="newname" size="30" class="textfield" value="" />
410 </fieldset>
411 <fieldset class="tblFooters">
412 <input id="rename_db_input" type="submit" value="<?php echo __('Go'); ?>" />
413 </fieldset>
414 </form>
415 </div>
416 <?php
417 } // end if
419 // Drop link if allowed
420 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
421 // Don't allow to easily drop mysql database, RFE #1327514.
422 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
423 && !$db_is_information_schema
424 && (PMA_DRIZZLE || $db != 'mysql')) {
426 <div class="operations_half_width">
427 <fieldset class="caution">
428 <legend><?php
429 if ($cfg['PropertiesIconic']) {
430 echo PMA_getImage('b_deltbl.png');
432 echo __('Remove database');
433 ?></legend>
435 <ul>
436 <?php
437 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
438 $this_url_params = array(
439 'sql_query' => $this_sql_query,
440 'back' => 'db_operations.php',
441 'goto' => 'main.php',
442 'reload' => '1',
443 'purge' => '1',
444 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
445 'db' => null,
448 <li><a href="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? 'id="drop_db_anchor"' : ''); ?>>
449 <?php echo __('Drop the database (DROP)'); ?></a>
450 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?>
451 </li>
452 </ul>
453 </fieldset>
454 </div>
455 <?php } ?>
456 <?php
458 * Copy database
461 <div class="operations_half_width clearfloat">
462 <form id="copy_db_form" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax" ' : ''); ?>method="post" action="db_operations.php"
463 onsubmit="return emptyFormElements(this, 'newname')">
464 <?php
465 if (isset($db_collation)) {
466 echo '<input type="hidden" name="db_collation" value="' . $db_collation
467 .'" />' . "\n";
469 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
470 echo PMA_generate_common_hidden_inputs($db);
472 <fieldset>
473 <legend>
474 <?php
475 if ($cfg['PropertiesIconic']) {
476 echo PMA_getImage('b_edit.png');
478 echo __('Copy database to') . ':';
479 $drop_clause = 'DROP TABLE / DROP VIEW';
481 </legend>
482 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
483 <?php
484 $choices = array(
485 'structure' => __('Structure only'),
486 'data' => __('Structure and data'),
487 'dataonly' => __('Data only'));
488 PMA_display_html_radio('what', $choices, 'data', true);
489 unset($choices);
491 <input type="checkbox" name="create_database_before_copying" value="1"
492 id="checkbox_create_database_before_copying"
493 checked="checked" />
494 <label for="checkbox_create_database_before_copying">
495 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
496 <input type="checkbox" name="drop_if_exists" value="true"
497 id="checkbox_drop" />
498 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
499 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
500 id="checkbox_auto_increment" />
501 <label for="checkbox_auto_increment">
502 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
503 <input type="checkbox" name="add_constraints" value="1"
504 id="checkbox_constraints" />
505 <label for="checkbox_constraints">
506 <?php echo __('Add constraints'); ?></label><br />
507 <?php
508 unset($drop_clause);
510 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
511 && $_COOKIE['pma_switch_to_new'] == 'true') {
512 $pma_switch_to_new = 'true';
515 <input type="checkbox" name="switch_to_new" value="true"
516 id="checkbox_switch"
517 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
519 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
520 </fieldset>
521 <fieldset class="tblFooters">
522 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
523 </fieldset>
524 </form>
525 </div>
526 <?php
529 * Change database charset
531 echo '<div class="operations_half_width"><form id="change_db_charset_form" ';
532 if ($GLOBALS['cfg']['AjaxEnable']) {
533 echo ' class="ajax" ';
535 echo 'method="post" action="./db_operations.php">'
536 . PMA_generate_common_hidden_inputs($db, $table)
537 . '<fieldset>' . "\n"
538 . ' <legend>';
539 if ($cfg['PropertiesIconic']) {
540 echo PMA_getImage('s_asci.png');
542 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
543 . ' </legend>' . "\n"
544 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
545 'db_collation', 'select_db_collation', $db_collation, false, 3)
546 . '</fieldset>'
547 . '<fieldset class="tblFooters">'
548 . ' <input type="submit" name="submitcollation"'
549 . ' value="' . __('Go') . '" />' . "\n"
550 . '</fieldset>' . "\n"
551 . '</form></div>' . "\n";
553 if ($num_tables > 0
554 && ! $cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
555 $message = PMA_Message::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
556 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
557 $message->addParam('</a>', false);
558 /* Show error if user has configured something, notice elsewhere */
559 if (!empty($cfg['Servers'][$server]['pmadb'])) {
560 $message->isError(true);
562 echo '<div class="operations_full_width">';
563 $message->display();
564 echo '</div>';
565 } // end if
566 } // end if (!$is_information_schema)
569 // not sure about displaying the PDF dialog in case db is information_schema
570 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
571 <!-- Work on PDF Pages -->
573 <?php
574 // We only show this if we find something in the new pdf_pages table
576 $test_query = '
577 SELECT *
578 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
579 WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'';
580 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
583 * Export Relational Schema View
585 echo '<div class="operations_full_width"><fieldset><a href="schema_edit.php?' . $url_query . '">';
586 if ($cfg['PropertiesIconic']) {
587 echo PMA_getImage('b_edit.png');
589 echo __('Edit or export relational schema') . '</a></fieldset></div>';
590 } // end if
593 * Displays the footer
595 require './libraries/footer.inc.php';