Translation update done using Pootle.
[phpmyadmin/ammar_yasir.git] / db_operations.php
blobc9aa565c7ea3f3280096de11a96c7acda02c16ce
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,
132 // first all the views are collected and a stand-in is created
133 // the real views are created after the tables
134 if (PMA_Table::isView($db, $each_table)) {
135 $views[] = $each_table;
136 // Create stand-in definition to resolve view dependencies
137 $sql_view_standin = PMA_getTableDefStandIn($db, $each_table, "\n");
138 PMA_DBI_query($sql_view_standin);
139 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin . ';';
140 continue;
143 $back = $sql_query;
144 $sql_query = '';
146 // value of $what for this table only
147 $this_what = $what;
149 // do not copy the data from a Merge table
150 // note: on the calling FORM, 'data' means 'structure and data'
151 if (PMA_Table::isMerge($db, $each_table)) {
152 if ($this_what == 'data') {
153 $this_what = 'structure';
155 if ($this_what == 'dataonly') {
156 $this_what = 'nocopy';
160 if ($this_what != 'nocopy') {
161 // keep the triggers from the original db+table
162 // (third param is empty because delimiters are only intended
163 // for importing via the mysql client or our Import feature)
164 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
166 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
167 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
169 $_error = true;
170 // $sql_query is filled by PMA_Table::moveCopy()
171 $sql_query = $back . $sql_query;
172 break;
174 // apply the triggers to the destination db+table
175 if ($triggers) {
176 PMA_DBI_select_db($newname);
177 foreach ($triggers as $trigger) {
178 PMA_DBI_query($trigger['create']);
180 unset($trigger);
182 unset($triggers);
184 // this does not apply to a rename operation
185 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
186 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
187 unset($GLOBALS['sql_constraints_query']);
190 // $sql_query is filled by PMA_Table::moveCopy()
191 $sql_query = $back . $sql_query;
192 } // end (foreach)
193 unset($each_table);
195 // handle the views
196 if (! $_error) {
197 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
198 // to remove stand-in VIEW that was created earlier
199 $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 // restore previous value
209 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
211 unset($view, $views);
213 // now that all tables exist, create all the accumulated constraints
214 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
215 PMA_DBI_select_db($newname);
216 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
217 PMA_DBI_query($one_query);
218 // and prepare to display them
219 $GLOBALS['sql_query'] .= "\n" . $one_query;
222 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
225 if (PMA_MYSQL_INT_VERSION >= 50100) {
226 // here DELIMITER is not used because it's not part of the
227 // language; each statement is sent one by one
229 // to avoid selecting alternatively the current and new db
230 // we would need to modify the CREATE definitions to qualify
231 // the db name
232 $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddslashes($db,true) . '\';');
233 if ($event_names) {
234 foreach($event_names as $event_name) {
235 PMA_DBI_select_db($db);
236 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
237 // collect for later display
238 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
239 PMA_DBI_select_db($newname);
240 PMA_DBI_query($tmp_query);
244 // go back to current db, just in case
245 PMA_DBI_select_db($db);
247 // Duplicate the bookmarks for this db (done once for each db)
248 if (! $_error && $db != $newname) {
249 $get_fields = array('user', 'label', 'query');
250 $where_fields = array('dbase' => $db);
251 $new_fields = array('dbase' => $newname);
252 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
253 $where_fields, $new_fields);
256 if (! $_error && $move) {
258 * cleanup pmadb stuff for this db
260 require_once './libraries/relation_cleanup.lib.php';
261 PMA_relationsCleanupDatabase($db);
263 // if someday the RENAME DATABASE reappears, do not DROP
264 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
265 $sql_query .= "\n" . $local_query;
266 PMA_DBI_query($local_query);
268 $message = PMA_Message::success(__('Database %s has been renamed to %s'));
269 $message->addParam($db);
270 $message->addParam($newname);
271 } elseif (! $_error) {
272 $message = PMA_Message::success(__('Database %s has been copied to %s'));
273 $message->addParam($db);
274 $message->addParam($newname);
276 $reload = true;
278 /* Change database to be used */
279 if (! $_error && $move) {
280 $db = $newname;
281 } elseif (! $_error) {
282 if (isset($switch_to_new) && $switch_to_new == 'true') {
283 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
284 $db = $newname;
285 } else {
286 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
290 if ($_error && ! isset($message)) {
291 $message = PMA_Message::error();
296 * Database has been successfully renamed/moved. If in an Ajax request,
297 * generate the output with {@link PMA_ajaxResponse} and exit
299 if( $GLOBALS['is_ajax_request'] == true) {
300 $extra_data['newname'] = $newname;
301 $extra_data['sql_query'] = PMA_showMessage(NULL, $sql_query);
302 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
308 * Settings for relations stuff
311 $cfgRelation = PMA_getRelationsParam();
314 * Check if comments were updated
315 * (must be done before displaying the menu tabs)
317 if (isset($_REQUEST['comment'])) {
318 PMA_setDbComment($db, $comment);
322 * Prepares the tables list if the user where not redirected to this script
323 * because there is no table in the database ($is_info is true)
325 if (empty($is_info)) {
326 require './libraries/db_common.inc.php';
327 $url_query .= '&amp;goto=db_operations.php';
329 // Gets the database structure
330 $sub_part = '_structure';
331 require './libraries/db_info.inc.php';
332 echo "\n";
334 if (isset($message)) {
335 PMA_showMessage($message, $sql_query);
336 unset($message);
340 $db_collation = PMA_getDbCollation($db);
341 if ($db == 'information_schema') {
342 $is_information_schema = true;
343 } else {
344 $is_information_schema = false;
347 if (!$is_information_schema) {
349 require './libraries/display_create_table.lib.php';
351 if ($cfgRelation['commwork']) {
353 * database comment
356 <form method="post" action="db_operations.php">
357 <?php echo PMA_generate_common_hidden_inputs($db); ?>
358 <fieldset>
359 <legend>
360 <?php echo PMA_getIcon('b_comment.png', __('Database comment: '), false, true); ?>
361 </legend>
362 <input type="text" name="comment" class="textfield" size="30"
363 value="<?php
364 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
365 </fieldset>
366 <fieldset class="tblFooters">
367 <input type="submit" value="<?php echo __('Go'); ?>" />
368 </fieldset>
369 </form>
370 <?php
373 * rename database
375 if ($db != 'mysql') {
377 <form id="rename_db_form" method="post" action="db_operations.php"
378 onsubmit="return emptyFormElements(this, 'newname')">
379 <?php
380 if (isset($db_collation)) {
381 echo '<input type="hidden" name="db_collation" value="' . $db_collation
382 .'" />' . "\n";
385 <input type="hidden" name="what" value="data" />
386 <input type="hidden" name="db_rename" value="true" />
387 <?php echo PMA_generate_common_hidden_inputs($db); ?>
388 <fieldset>
389 <legend>
390 <?php
391 if ($cfg['PropertiesIconic']) {
392 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
393 .' alt="" width="16" height="16" />';
395 echo __('Rename database to') . ':';
397 </legend>
398 <input type="text" name="newname" size="30" class="textfield" value="" />
399 <?php
400 echo '(' . __('Command') . ': ';
402 * @todo (see explanations above in a previous todo)
404 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
405 // echo 'RENAME DATABASE';
406 //} else {
407 echo 'INSERT INTO ... SELECT';
409 echo ')'; ?>
410 </fieldset>
411 <fieldset class="tblFooters">
412 <input id="rename_db_input" type="submit" value="<?php echo __('Go'); ?>" />
413 </fieldset>
414 </form>
415 <?php
416 } // end if
418 // Drop link if allowed
419 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
420 // Don't allow to easily drop mysql database, RFE #1327514.
421 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase']) && ! $db_is_information_schema && ($db != 'mysql')) {
423 <fieldset class="caution">
424 <legend><?php
425 if ($cfg['PropertiesIconic']) {
426 echo '<img class="icon" src="' . $pmaThemeImage . 'b_deltbl.png"'
427 .' alt="" width="16" height="16" />';
429 echo __('Remove database');
430 ?></legend>
432 <ul>
433 <?php
434 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
435 $this_url_params = array(
436 'sql_query' => $this_sql_query,
437 'back' => 'db_operations.php',
438 'goto' => 'main.php',
439 'reload' => '1',
440 'purge' => '1',
441 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
442 'db' => NULL,
445 <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); ?>')">
446 <?php echo __('Drop the database (DROP)'); ?></a>
447 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?>
448 </li>
449 </ul>
450 </fieldset>
451 <?php } ?>
452 <?php
454 * Copy database
457 <form id="copy_db_form" method="post" action="db_operations.php"
458 onsubmit="return emptyFormElements(this, 'newname')">
459 <?php
460 if (isset($db_collation)) {
461 echo '<input type="hidden" name="db_collation" value="' . $db_collation
462 .'" />' . "\n";
464 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
465 echo PMA_generate_common_hidden_inputs($db);
467 <fieldset>
468 <legend>
469 <?php
470 if ($cfg['PropertiesIconic']) {
471 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
472 .' alt="" width="16" height="16" />';
474 echo __('Copy database to') . ':';
475 $drop_clause = 'DROP TABLE / DROP VIEW';
477 </legend>
478 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
479 <?php
480 $choices = array(
481 'structure' => __('Structure only'),
482 'data' => __('Structure and data'),
483 'dataonly' => __('Data only'));
484 PMA_display_html_radio('what', $choices, 'data', true);
485 unset($choices);
487 <input type="checkbox" name="create_database_before_copying" value="1"
488 id="checkbox_create_database_before_copying"
489 checked="checked" />
490 <label for="checkbox_create_database_before_copying">
491 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
492 <input type="checkbox" name="drop_if_exists" value="true"
493 id="checkbox_drop" />
494 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
495 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
496 id="checkbox_auto_increment" />
497 <label for="checkbox_auto_increment">
498 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
499 <input type="checkbox" name="add_constraints" value="1"
500 id="checkbox_constraints" />
501 <label for="checkbox_constraints">
502 <?php echo __('Add constraints'); ?></label><br />
503 <?php
504 unset($drop_clause);
506 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
507 && $_COOKIE['pma_switch_to_new'] == 'true') {
508 $pma_switch_to_new = 'true';
511 <input type="checkbox" name="switch_to_new" value="true"
512 id="checkbox_switch"
513 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
515 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
516 </fieldset>
517 <fieldset class="tblFooters">
518 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
519 </fieldset>
520 </form>
522 <?php
525 * Change database charset
527 echo '<form id="change_db_charset_form" method="post" action="./db_operations.php">' . "\n"
528 . PMA_generate_common_hidden_inputs($db, $table)
529 . '<fieldset>' . "\n"
530 . ' <legend>';
531 if ($cfg['PropertiesIconic']) {
532 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
533 .' alt="" width="16" height="16" />';
535 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
536 . ' </legend>' . "\n"
537 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
538 'db_collation', 'select_db_collation', $db_collation, false, 3)
539 . '</fieldset>'
540 . '<fieldset class="tblFooters">'
541 . ' <input type="submit" name="submitcollation"'
542 . ' value="' . __('Go') . '" />' . "\n"
543 . '</fieldset>' . "\n"
544 . '</form>' . "\n";
546 if ($num_tables > 0
547 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
548 $message = PMA_Message::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
549 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
550 $message->addParam('</a>', false);
551 /* Show error if user has configured something, notice elsewhere */
552 if (!empty($cfg['Servers'][$server]['pmadb'])) {
553 $message->isError(true);
555 $message->display();
556 } // end if
557 } // end if (!$is_information_schema)
560 // not sure about displaying the PDF dialog in case db is information_schema
561 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
562 <!-- Work on PDF Pages -->
564 <?php
565 // We only show this if we find something in the new pdf_pages table
567 $test_query = '
568 SELECT *
569 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
570 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
571 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
574 * Export Relational Schema View
576 echo '<fieldset><a href="schema_edit.php?' . $url_query . '">';
577 if ($cfg['PropertiesIconic']) {
578 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
579 .' alt="" width="16" height="16" />';
581 echo __('Edit or export relational schema') . '</a></fieldset>';
582 } // end if
585 * Displays the footer
587 require './libraries/footer.inc.php';