2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
10 * - viewing PDF schemas
18 require_once './libraries/common.inc.php';
19 require_once './libraries/mysql_charsets.lib.php';
21 // add a javascript file for jQuery functions to handle Ajax actions
23 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.16.custom.js';
24 $GLOBALS['js_include'][] = 'db_operations.js';
27 * Rename/move or copy database
29 if (strlen($db) && (! empty($db_rename) ||
! empty($db_copy))) {
31 if (! empty($db_rename)) {
37 if (! isset($newname) ||
! strlen($newname)) {
38 $message = PMA_Message
::error(__('The database name is empty!'));
40 $sql_query = ''; // in case target db exists
42 if ($move ||
(isset($create_database_before_copying) && $create_database_before_copying)) {
43 // lower_case_table_names=1 `DB` becomes `db`
45 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
46 if ($lower_case_table_names === '1') {
47 $newname = PMA_strtolower($newname);
51 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
52 if (isset($db_collation)) {
53 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
56 $sql_query = $local_query;
57 // save the original db name because Tracker.class.php which
58 // may be called under PMA_DBI_query() changes $GLOBALS['db']
59 // for some statements, one of which being CREATE DATABASE
61 PMA_DBI_query($local_query);
65 // rebuild the database list because PMA_Table::moveCopy
66 // checks in this list if the target db exists
67 $GLOBALS['pma']->databases
->build();
70 // here I don't use DELIMITER because it's not part of the
71 // language; I have to send each statement one by one
73 // to avoid selecting alternatively the current and new db
74 // we would need to modify the CREATE definitions to qualify
76 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
77 if ($procedure_names) {
78 foreach ($procedure_names as $procedure_name) {
79 PMA_DBI_select_db($db);
80 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
81 // collect for later display
82 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
83 PMA_DBI_select_db($newname);
84 PMA_DBI_query($tmp_query);
88 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
89 if ($function_names) {
90 foreach ($function_names as $function_name) {
91 PMA_DBI_select_db($db);
92 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
93 // collect for later display
94 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
95 PMA_DBI_select_db($newname);
96 PMA_DBI_query($tmp_query);
100 // go back to current db, just in case
101 PMA_DBI_select_db($db);
103 $GLOBALS['sql_constraints_query_full_db'] = array();
105 $tables_full = PMA_DBI_get_tables_full($db);
108 // remove all foreign key constraints, otherwise we can get errors
109 include_once './libraries/export/sql.php';
110 foreach ($tables_full as $each_table => $tmp) {
111 $sql_constraints = '';
112 $sql_drop_foreign_keys = '';
113 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
114 if ($move && ! empty($sql_drop_foreign_keys)) {
115 PMA_DBI_query($sql_drop_foreign_keys);
117 // keep the constraint we just dropped
118 if (! empty($sql_constraints)) {
119 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
122 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
124 foreach ($tables_full as $each_table => $tmp) {
125 // to be able to rename a db containing views,
126 // first all the views are collected and a stand-in is created
127 // the real views are created after the tables
128 if (PMA_Table
::isView($db, $each_table)) {
129 $views[] = $each_table;
130 // Create stand-in definition to resolve view dependencies
131 $sql_view_standin = PMA_getTableDefStandIn($db, $each_table, "\n");
132 PMA_DBI_select_db($newname);
133 PMA_DBI_query($sql_view_standin);
134 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin;
138 foreach ($tables_full as $each_table => $tmp) {
139 // skip the views; we have creted stand-in definitions
140 if (PMA_Table
::isView($db, $each_table)) {
146 // value of $what for this table only
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(
167 $db, $each_table, $newname, $each_table,
168 isset($this_what) ?
$this_what : 'data', $move, 'db_copy')
171 // $sql_query is filled by PMA_Table::moveCopy()
172 $sql_query = $back . $sql_query;
175 // apply the triggers to the destination db+table
177 PMA_DBI_select_db($newname);
178 foreach ($triggers as $trigger) {
179 PMA_DBI_query($trigger['create']);
180 $GLOBALS['sql_query'] .= "\n" . $trigger['create'] . ';';
186 // this does not apply to a rename operation
187 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
188 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
189 unset($GLOBALS['sql_constraints_query']);
192 // $sql_query is filled by PMA_Table::moveCopy()
193 $sql_query = $back . $sql_query;
199 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
200 // to remove stand-in VIEW that was created earlier
201 if (isset($GLOBALS['drop_if_exists'])) {
202 $temp_drop_if_exists = $GLOBALS['drop_if_exists'];
204 $GLOBALS['drop_if_exists'] = 'true';
206 foreach ($views as $view) {
207 if (! PMA_Table
::moveCopy($db, $view, $newname, $view, 'structure', $move, 'db_copy')) {
212 unset($GLOBALS['drop_if_exists']);
213 if (isset($temp_drop_if_exists)) {
214 // restore previous value
215 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
216 unset($temp_drop_if_exists);
219 unset($view, $views);
221 // now that all tables exist, create all the accumulated constraints
222 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
223 PMA_DBI_select_db($newname);
224 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
225 PMA_DBI_query($one_query);
226 // and prepare to display them
227 $GLOBALS['sql_query'] .= "\n" . $one_query;
230 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
233 if (!PMA_DRIZZLE
&& PMA_MYSQL_INT_VERSION
>= 50100) {
234 // here DELIMITER is not used because it's not part of the
235 // language; each statement is sent one by one
237 // to avoid selecting alternatively the current and new db
238 // we would need to modify the CREATE definitions to qualify
240 $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddSlashes($db, true) . '\';');
242 foreach ($event_names as $event_name) {
243 PMA_DBI_select_db($db);
244 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
245 // collect for later display
246 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
247 PMA_DBI_select_db($newname);
248 PMA_DBI_query($tmp_query);
253 // go back to current db, just in case
254 PMA_DBI_select_db($db);
256 // Duplicate the bookmarks for this db (done once for each db)
257 if (! $_error && $db != $newname) {
258 $get_fields = array('user', 'label', 'query');
259 $where_fields = array('dbase' => $db);
260 $new_fields = array('dbase' => $newname);
261 PMA_Table
::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
262 $where_fields, $new_fields);
265 if (! $_error && $move) {
267 * cleanup pmadb stuff for this db
269 include_once './libraries/relation_cleanup.lib.php';
270 PMA_relationsCleanupDatabase($db);
272 // if someday the RENAME DATABASE reappears, do not DROP
273 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
274 $sql_query .= "\n" . $local_query;
275 PMA_DBI_query($local_query);
277 $message = PMA_Message
::success(__('Database %s has been renamed to %s'));
278 $message->addParam($db);
279 $message->addParam($newname);
280 } elseif (! $_error) {
281 $message = PMA_Message
::success(__('Database %s has been copied to %s'));
282 $message->addParam($db);
283 $message->addParam($newname);
287 /* Change database to be used */
288 if (! $_error && $move) {
290 } elseif (! $_error) {
291 if (isset($switch_to_new) && $switch_to_new == 'true') {
292 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
295 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
299 if ($_error && ! isset($message)) {
300 $message = PMA_Message
::error();
305 * Database has been successfully renamed/moved. If in an Ajax request,
306 * generate the output with {@link PMA_ajaxResponse} and exit
308 if ( $GLOBALS['is_ajax_request'] == true) {
309 $extra_data['newname'] = $newname;
310 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
311 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
317 * Settings for relations stuff
320 $cfgRelation = PMA_getRelationsParam();
323 * Check if comments were updated
324 * (must be done before displaying the menu tabs)
326 if (isset($_REQUEST['comment'])) {
327 PMA_setDbComment($db, $comment);
331 * Prepares the tables list if the user where not redirected to this script
332 * because there is no table in the database ($is_info is true)
334 if (empty($is_info)) {
335 include './libraries/db_common.inc.php';
336 $url_query .= '&goto=db_operations.php';
338 // Gets the database structure
339 $sub_part = '_structure';
340 include './libraries/db_info.inc.php';
343 if (isset($message)) {
344 PMA_showMessage($message, $sql_query);
349 $db_collation = PMA_getDbCollation($db);
350 $is_information_schema = PMA_is_system_schema($db);
352 if (!$is_information_schema) {
353 if ($cfgRelation['commwork']) {
358 <div
class="operations_half_width">
359 <form method
="post" action
="db_operations.php">
360 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
364 if ($cfg['PropertiesIconic']) {
365 echo '<img class="icon ic_b_comment" src="themes/dot.gif" alt="" />';
367 echo __('Database comment: ');
370 <input type
="text" name
="comment" class="textfield" size
="30"
372 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
374 <fieldset
class="tblFooters">
375 <input type
="submit" value
="<?php echo __('Go'); ?>" />
382 <div
class="operations_half_width">
383 <?php
include './libraries/display_create_table.lib.php'; ?
>
389 if ($db != 'mysql') {
391 <div
class="operations_half_width">
392 <form id
="rename_db_form" <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax" ' : ''); ?
>method
="post" action
="db_operations.php"
393 onsubmit
="return emptyFormElements(this, 'newname')">
395 if (isset($db_collation)) {
396 echo '<input type="hidden" name="db_collation" value="' . $db_collation
400 <input type
="hidden" name
="what" value
="data" />
401 <input type
="hidden" name
="db_rename" value
="true" />
402 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
406 if ($cfg['PropertiesIconic']) {
407 echo PMA_getImage('b_edit.png');
409 echo __('Rename database to') . ':';
412 <input id
="new_db_name" type
="text" name
="newname" size
="30" class="textfield" value
="" />
414 <fieldset
class="tblFooters">
415 <input id
="rename_db_input" type
="submit" value
="<?php echo __('Go'); ?>" />
422 // Drop link if allowed
423 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
424 // Don't allow to easily drop mysql database, RFE #1327514.
425 if (($is_superuser ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
426 && !$db_is_information_schema
427 && (PMA_DRIZZLE ||
$db != 'mysql')) {
429 <div
class="operations_half_width">
430 <fieldset
class="caution">
432 if ($cfg['PropertiesIconic']) {
433 echo PMA_getImage('b_deltbl.png');
435 echo __('Remove database');
440 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
441 $this_url_params = array(
442 'sql_query' => $this_sql_query,
443 'back' => 'db_operations.php',
444 'goto' => 'main.php',
447 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
451 <li
><a href
="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
'id="drop_db_anchor"' : ''); ?
>>
452 <?php
echo __('Drop the database (DROP)'); ?
></a
>
453 <?php
echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?
>
464 <div
class="operations_half_width clearfloat">
465 <form id
="copy_db_form" <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax" ' : ''); ?
>method
="post" action
="db_operations.php"
466 onsubmit
="return emptyFormElements(this, 'newname')">
468 if (isset($db_collation)) {
469 echo '<input type="hidden" name="db_collation" value="' . $db_collation
472 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
473 echo PMA_generate_common_hidden_inputs($db);
478 if ($cfg['PropertiesIconic']) {
479 echo PMA_getImage('b_edit.png');
481 echo __('Copy database to') . ':';
482 $drop_clause = 'DROP TABLE / DROP VIEW';
485 <input type
="text" name
="newname" size
="30" class="textfield" value
="" /><br
/>
488 'structure' => __('Structure only'),
489 'data' => __('Structure and data'),
490 'dataonly' => __('Data only'));
491 PMA_display_html_radio('what', $choices, 'data', true);
494 <input type
="checkbox" name
="create_database_before_copying" value
="1"
495 id
="checkbox_create_database_before_copying"
497 <label
for="checkbox_create_database_before_copying">
498 <?php
echo __('CREATE DATABASE before copying'); ?
></label
><br
/>
499 <input type
="checkbox" name
="drop_if_exists" value
="true"
500 id
="checkbox_drop" />
501 <label
for="checkbox_drop"><?php
echo sprintf(__('Add %s'), $drop_clause); ?
></label
><br
/>
502 <input type
="checkbox" name
="sql_auto_increment" value
="1" checked
="checked"
503 id
="checkbox_auto_increment" />
504 <label
for="checkbox_auto_increment">
505 <?php
echo __('Add AUTO_INCREMENT value'); ?
></label
><br
/>
506 <input type
="checkbox" name
="add_constraints" value
="1"
507 id
="checkbox_constraints" />
508 <label
for="checkbox_constraints">
509 <?php
echo __('Add constraints'); ?
></label
><br
/>
513 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
514 && $_COOKIE['pma_switch_to_new'] == 'true') {
515 $pma_switch_to_new = 'true';
518 <input type
="checkbox" name
="switch_to_new" value
="true"
520 <?php
echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ?
' checked="checked"' : ''); ?
>
522 <label
for="checkbox_switch"><?php
echo __('Switch to copied database'); ?
></label
>
524 <fieldset
class="tblFooters">
525 <input type
="submit" name
="submit_copy" value
="<?php echo __('Go'); ?>" />
532 * Change database charset
534 echo '<div class="operations_half_width"><form id="change_db_charset_form" ';
535 if ($GLOBALS['cfg']['AjaxEnable']) {
536 echo ' class="ajax" ';
538 echo 'method="post" action="./db_operations.php">'
539 . PMA_generate_common_hidden_inputs($db, $table)
540 . '<fieldset>' . "\n"
542 if ($cfg['PropertiesIconic']) {
543 echo PMA_getImage('s_asci.png');
545 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
546 . ' </legend>' . "\n"
547 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION
,
548 'db_collation', 'select_db_collation', $db_collation, false, 3)
550 . '<fieldset class="tblFooters">'
551 . ' <input type="submit" name="submitcollation"'
552 . ' value="' . __('Go') . '" />' . "\n"
553 . '</fieldset>' . "\n"
554 . '</form></div>' . "\n";
557 && ! $cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
558 $message = PMA_Message
::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
559 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
560 $message->addParam('</a>', false);
561 /* Show error if user has configured something, notice elsewhere */
562 if (!empty($cfg['Servers'][$server]['pmadb'])) {
563 $message->isError(true);
565 echo '<div class="operations_full_width">';
569 } // end if (!$is_information_schema)
572 // not sure about displaying the PDF dialog in case db is information_schema
573 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?
>
574 <!-- Work on PDF Pages
-->
577 // We only show this if we find something in the new pdf_pages table
581 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
582 WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\'';
583 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE
);
586 * Export Relational Schema View
588 echo '<div class="operations_full_width"><fieldset><a href="schema_edit.php?' . $url_query . '">';
589 if ($cfg['PropertiesIconic']) {
590 echo PMA_getImage('b_edit.png');
592 echo __('Edit or export relational schema') . '</a></fieldset></div>';
596 * Displays the footer
598 require './libraries/footer.inc.php';