2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
6 * - copy database to a new one
7 * - copy tables within the database
11 * - viewing PDF schemas
19 require_once './libraries/common.inc.php';
20 require_once './libraries/mysql_charsets.lib.php';
22 // add blobstreaming library functions
23 require_once "./libraries/blobstreaming.lib.php";
25 // add a javascript file for jQuery functions to handle Ajax actions
27 $GLOBALS['js_include'][] = 'jquery/jquery-ui-1.8.custom.js';
28 $GLOBALS['js_include'][] = 'db_operations.js';
32 * Rename/move or copy database
34 if (strlen($db) && (! empty($table_copy)||
! empty($db_rename) ||
! empty($db_copy))) {
36 if (! empty($db_rename)) {
41 if(isset($table_copy)){
43 //@TODO: the label show not display centered-align in ajax mode
44 //@TODO: write a introduction for user to understand this function
45 if(!PMA_isValid($src)){
46 $message = PMA_Message
::error(__('The From name is empty!'));
60 $srcTables = PMA_DBI_get_tables($db);
61 if (version_compare(PHP_VERSION
, '5.3.0', 'lt')){
62 $dstTables=preg_replace($pattern,$replace,$srcTables);
63 foreach($dstTables as $key=>$dstTable){
64 if($dstTable==$srcTables[$key]){
65 unset($dstTable[$key]);
71 * preg_filter — Perform a regular expression search and replace
73 $dstTables=preg_filter($pattern,$replace,$srcTables);
76 $message=new PMA_Message();
77 $matchedCount=count($dstTables);
78 if(!isset($replace_existed_table)){
79 foreach($dstTables as $key=>$dstTable){
80 if(!(array_search($dstTable,$srcTables)===false)){
81 unset($dstTables[$key]);
82 $msg.=__("Table %s already exists!").'<br/>';
83 $message->addParam($dstTable,true);
85 }//to avoid create and copy data to existed tables
89 if(empty($dstTables)){
91 $message->setMessage($msg);
93 $message->setMessage(__('Table not found'));
97 if(isset($replace_existed_table)){
98 if (isset($GLOBALS['drop_if_exists'])) {
99 $temp_drop_if_exists = $GLOBALS['drop_if_exists'];
101 $GLOBALS['drop_if_exists'] = 'true';
103 foreach($dstTables as $key=>$dstTable){
104 $success=PMA_Table
::moveCopy($db, $srcTables[$key], $db, $dstTable,
105 'data', false, 'db_copy');
106 $msg.='Copy Table %s to %s'.':'.($success?
__('OK'):__('Error'))."<br>";
107 $message->addParam($srcTables[$key],true);
108 $message->addParam($dstTable,true);
110 $message->setMessage($msg);
111 if(isset($replace_existed_table)){
112 unset($GLOBALS['drop_if_exists']);
113 if (isset($temp_drop_if_exists)) {
114 // restore previous value
115 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
116 unset($temp_drop_if_exists);
121 $GLOBALS['pma']->databases
->build();
123 //print_r($messge->getParams());
124 //@todo i18n,ajax-client side
126 }else if (!isset($newname) ||
!strlen($newname)) {
127 $message = PMA_Message
::error(__('The database name is empty!'));
129 $sql_query = ''; // in case target db exists
132 (isset($create_database_before_copying) && $create_database_before_copying)) {
133 // lower_case_table_names=1 `DB` becomes `db`
134 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
135 if ($lower_case_table_names === '1') {
136 $newname = strtolower($newname);
139 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
140 if (isset($db_collation)) {
141 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
144 $sql_query = $local_query;
145 // save the original db name because Tracker.class.php which
146 // may be called under PMA_DBI_query() changes $GLOBALS['db']
147 // for some statements, one of which being CREATE DATABASE
149 PMA_DBI_query($local_query);
153 // rebuild the database list because PMA_Table::moveCopy
154 // checks in this list if the target db exists
155 $GLOBALS['pma']->databases
->build();
158 if (PMA_MYSQL_INT_VERSION
>= 50000) {
159 // here I don't use DELIMITER because it's not part of the
160 // language; I have to send each statement one by one
162 // to avoid selecting alternatively the current and new db
163 // we would need to modify the CREATE definitions to qualify
165 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
166 if ($procedure_names) {
167 foreach($procedure_names as $procedure_name) {
168 PMA_DBI_select_db($db);
169 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
170 // collect for later display
171 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
172 PMA_DBI_select_db($newname);
173 PMA_DBI_query($tmp_query);
177 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
178 if ($function_names) {
179 foreach($function_names as $function_name) {
180 PMA_DBI_select_db($db);
181 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
182 // collect for later display
183 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
184 PMA_DBI_select_db($newname);
185 PMA_DBI_query($tmp_query);
189 // go back to current db, just in case
190 PMA_DBI_select_db($db);
192 $GLOBALS['sql_constraints_query_full_db'] = array();
194 $tables_full = PMA_DBI_get_tables_full($db);
197 // remove all foreign key constraints, otherwise we can get errors
198 require_once './libraries/export/sql.php';
199 foreach ($tables_full as $each_table => $tmp) {
200 $sql_constraints = '';
201 $sql_drop_foreign_keys = '';
202 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
203 if ($move && ! empty($sql_drop_foreign_keys)) {
204 PMA_DBI_query($sql_drop_foreign_keys);
206 // keep the constraint we just dropped
207 if (! empty($sql_constraints)) {
208 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
211 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
214 foreach ($tables_full as $each_table => $tmp) {
215 // to be able to rename a db containing views,
216 // first all the views are collected and a stand-in is created
217 // the real views are created after the tables
218 if (PMA_Table
::isView($db, $each_table)) {
219 $views[] = $each_table;
220 // Create stand-in definition to resolve view dependencies
221 $sql_view_standin = PMA_getTableDefStandIn($db, $each_table, "\n");
222 PMA_DBI_query($sql_view_standin);
223 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin . ';';
230 // value of $what for this table only
233 // do not copy the data from a Merge table
234 // note: on the calling FORM, 'data' means 'structure and data'
235 if (PMA_Table
::isMerge($db, $each_table)) {
236 if ($this_what == 'data') {
237 $this_what = 'structure';
239 if ($this_what == 'dataonly') {
240 $this_what = 'nocopy';
244 if ($this_what != 'nocopy') {
245 // keep the triggers from the original db+table
246 // (third param is empty because delimiters are only intended
247 // for importing via the mysql client or our Import feature)
248 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
250 if (! PMA_Table
::moveCopy($db, $each_table, $newname, $each_table,
251 isset($this_what) ?
$this_what : 'data', $move, 'db_copy'))
254 // $sql_query is filled by PMA_Table::moveCopy()
255 $sql_query = $back . $sql_query;
258 // apply the triggers to the destination db+table
260 PMA_DBI_select_db($newname);
261 foreach ($triggers as $trigger) {
262 PMA_DBI_query($trigger['create']);
268 // this does not apply to a rename operation
269 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
270 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
271 unset($GLOBALS['sql_constraints_query']);
274 // $sql_query is filled by PMA_Table::moveCopy()
275 $sql_query = $back . $sql_query;
281 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
282 // to remove stand-in VIEW that was created earlier
283 if (isset($GLOBALS['drop_if_exists'])) {
284 $temp_drop_if_exists = $GLOBALS['drop_if_exists'];
286 $GLOBALS['drop_if_exists'] = 'true';
288 foreach ($views as $view) {
289 if (! PMA_Table
::moveCopy($db, $view, $newname, $view, 'structure', $move, 'db_copy')) {
294 unset($GLOBALS['drop_if_exists']);
295 if (isset($temp_drop_if_exists)) {
296 // restore previous value
297 $GLOBALS['drop_if_exists'] = $temp_drop_if_exists;
298 unset($temp_drop_if_exists);
301 unset($view, $views);
303 // now that all tables exist, create all the accumulated constraints
304 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
305 PMA_DBI_select_db($newname);
306 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
307 PMA_DBI_query($one_query);
308 // and prepare to display them
309 $GLOBALS['sql_query'] .= "\n" . $one_query;
312 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
315 if (PMA_MYSQL_INT_VERSION
>= 50100) {
316 // here DELIMITER is not used because it's not part of the
317 // language; each statement is sent one by one
319 // to avoid selecting alternatively the current and new db
320 // we would need to modify the CREATE definitions to qualify
322 $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddslashes($db,true) . '\';');
324 foreach($event_names as $event_name) {
325 PMA_DBI_select_db($db);
326 $tmp_query = PMA_DBI_get_definition($db, 'EVENT', $event_name);
327 // collect for later display
328 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
329 PMA_DBI_select_db($newname);
330 PMA_DBI_query($tmp_query);
334 // go back to current db, just in case
335 PMA_DBI_select_db($db);
337 // Duplicate the bookmarks for this db (done once for each db)
338 if (! $_error && $db != $newname) {
339 $get_fields = array('user', 'label', 'query');
340 $where_fields = array('dbase' => $db);
341 $new_fields = array('dbase' => $newname);
342 PMA_Table
::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
343 $where_fields, $new_fields);
346 if (! $_error && $move) {
348 * cleanup pmadb stuff for this db
350 require_once './libraries/relation_cleanup.lib.php';
351 PMA_relationsCleanupDatabase($db);
353 // if someday the RENAME DATABASE reappears, do not DROP
354 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
355 $sql_query .= "\n" . $local_query;
356 PMA_DBI_query($local_query);
358 $message = PMA_Message
::success(__('Database %s has been renamed to %s'));
359 $message->addParam($db);
360 $message->addParam($newname);
361 } elseif (! $_error) {
362 $message = PMA_Message
::success(__('Database %s has been copied to %s'));
363 $message->addParam($db);
364 $message->addParam($newname);
368 /* Change database to be used */
369 if (! $_error && $move) {
371 } elseif (! $_error) {
372 if (isset($switch_to_new) && $switch_to_new == 'true') {
373 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
376 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
380 if ($_error && ! isset($message)) {
381 $message = PMA_Message
::error();
386 * Database has been successfully renamed/moved. If in an Ajax request,
387 * generate the output with {@link PMA_ajaxResponse} and exit
389 if( $GLOBALS['is_ajax_request'] == true) {
391 $extra_data['newname'] = $newname;
392 $extra_data['sql_query'] = PMA_showMessage(NULL, $sql_query);
393 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
399 * Settings for relations stuff
402 $cfgRelation = PMA_getRelationsParam();
405 * Check if comments were updated
406 * (must be done before displaying the menu tabs)
408 if (isset($_REQUEST['comment'])) {
409 PMA_setDbComment($db, $comment);
413 * Prepares the tables list if the user where not redirected to this script
414 * because there is no table in the database ($is_info is true)
416 if (empty($is_info)) {
417 require './libraries/db_common.inc.php';
418 $url_query .= '&goto=db_operations.php';
420 // Gets the database structure
421 $sub_part = '_structure';
422 require './libraries/db_info.inc.php';
425 if (isset($message)) {
426 PMA_showMessage($message, $sql_query);
431 $db_collation = PMA_getDbCollation($db);
432 if ($db == 'information_schema') {
433 $is_information_schema = true;
435 $is_information_schema = false;
438 if (!$is_information_schema) {
440 require './libraries/display_create_table.lib.php';
442 if ($cfgRelation['commwork']) {
447 <form method
="post" action
="db_operations.php">
448 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
451 <?php
echo PMA_getIcon('b_comment.png', __('Database comment: '), false, true); ?
>
453 <input type
="text" name
="comment" class="textfield" size
="30"
455 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
457 <fieldset
class="tblFooters">
458 <input type
="submit" value
="<?php echo __('Go'); ?>" />
466 if ($db != 'mysql') {
468 <form id
="rename_db_form" <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax" ' : ''); ?
>method
="post" action
="db_operations.php"
469 onsubmit
="return emptyFormElements(this, 'newname')">
471 if (isset($db_collation)) {
472 echo '<input type="hidden" name="db_collation" value="' . $db_collation
476 <input type
="hidden" name
="what" value
="data" />
477 <input type
="hidden" name
="db_rename" value
="true" />
478 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
482 if ($cfg['PropertiesIconic']) {
483 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
484 .' alt="" width="16" height="16" />';
486 echo __('Rename database to') . ':';
489 <input id
="new_db_name" type
="text" name
="newname" size
="30" class="textfield" value
="" />
491 echo '(' . __('Command') . ': ';
493 * @todo (see explanations above in a previous todo)
495 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
496 // echo 'RENAME DATABASE';
498 echo 'INSERT INTO ... SELECT';
502 <fieldset
class="tblFooters">
503 <input id
="rename_db_input" type
="submit" value
="<?php echo __('Go'); ?>" />
509 // Drop link if allowed
510 // Don't even try to drop information_schema. You won't be able to. Believe me. You won't.
511 // Don't allow to easily drop mysql database, RFE #1327514.
512 if (($is_superuser ||
$GLOBALS['cfg']['AllowUserDropDatabase']) && ! $db_is_information_schema && ($db != 'mysql')) {
514 <fieldset
class="caution">
516 if ($cfg['PropertiesIconic']) {
517 echo '<img class="icon" src="' . $pmaThemeImage . 'b_deltbl.png"'
518 .' alt="" width="16" height="16" />';
520 echo __('Remove database');
525 $this_sql_query = 'DROP DATABASE ' . PMA_backquote($GLOBALS['db']);
526 $this_url_params = array(
527 'sql_query' => $this_sql_query,
528 'back' => 'db_operations.php',
529 'goto' => 'main.php',
532 'message_to_show' => sprintf(__('Database %s has been dropped.'), htmlspecialchars(PMA_backquote($db))),
536 <li
><a href
="sql.php<?php echo PMA_generate_common_url($this_url_params); ?>" <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
'id="drop_db_anchor"' : ''); ?
>>
537 <?php
echo __('Drop the database (DROP)'); ?
></a
>
538 <?php
echo PMA_showMySQLDocu('SQL-Syntax', 'DROP_DATABASE'); ?
>
548 <form id
="copy_db_form" <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax" ' : ''); ?
>method
="post" action
="db_operations.php"
549 onsubmit
="return emptyFormElements(this, 'newname')">
551 if (isset($db_collation)) {
552 echo '<input type="hidden" name="db_collation" value="' . $db_collation
555 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
556 echo PMA_generate_common_hidden_inputs($db);
561 if ($cfg['PropertiesIconic']) {
562 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
563 .' alt="" width="16" height="16" />';
565 echo __('Copy database to') . ':';
566 $drop_clause = 'DROP TABLE / DROP VIEW';
569 <input type
="text" name
="newname" size
="30" class="textfield" value
="" /><br
/>
572 'structure' => __('Structure only'),
573 'data' => __('Structure and data'),
574 'dataonly' => __('Data only'));
575 PMA_display_html_radio('what', $choices, 'data', true);
578 <input type
="checkbox" name
="create_database_before_copying" value
="1"
579 id
="checkbox_create_database_before_copying"
581 <label
for="checkbox_create_database_before_copying">
582 <?php
echo __('CREATE DATABASE before copying'); ?
></label
><br
/>
583 <input type
="checkbox" name
="drop_if_exists" value
="true"
584 id
="checkbox_drop" />
585 <label
for="checkbox_drop"><?php
echo sprintf(__('Add %s'), $drop_clause); ?
></label
><br
/>
586 <input type
="checkbox" name
="sql_auto_increment" value
="1" checked
="checked"
587 id
="checkbox_auto_increment" />
588 <label
for="checkbox_auto_increment">
589 <?php
echo __('Add AUTO_INCREMENT value'); ?
></label
><br
/>
590 <input type
="checkbox" name
="add_constraints" value
="1"
591 id
="checkbox_constraints" />
592 <label
for="checkbox_constraints">
593 <?php
echo __('Add constraints'); ?
></label
><br
/>
597 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
598 && $_COOKIE['pma_switch_to_new'] == 'true') {
599 $pma_switch_to_new = 'true';
602 <input type
="checkbox" name
="switch_to_new" value
="true"
604 <?php
echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ?
' checked="checked"' : ''); ?
>
606 <label
for="checkbox_switch"><?php
echo __('Switch to copied database'); ?
></label
>
608 <fieldset
class="tblFooters">
609 <input type
="submit" name
="submit_copy" value
="<?php echo __('Go'); ?>" />
621 <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax" ' : ''); ?
>
622 action
="db_operations.php"
624 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
625 <input type
="hidden" name
="table_copy" value
="true">
628 <?php
echo PMA_getIcon('b_edit.png', __('Copy Tables Within Current Database:'), false, true); ?
>
631 <div
class="formelement">
632 <?php
echo __('From'); ?
>:
633 <input type
="text" name
="src" maxlength
="64" size
="30" />
635 <div
class="formelement">
636 <?php
echo __('To'); ?
>:
637 <input type
="text" name
="dst" maxlength
="64" size
="30" />
639 <div
class="clearfloat"></div
>
642 'prefix' => __('By Prefix'),
643 'suffix' => __('By Suffix'),
644 'regex' => __('By Regular Expression'));
645 PMA_display_html_radio('type', $choices, 'prefix',true,true,'');
648 'nocopy'=> __('Do Not Replace Replicated Tables'),
649 'all' => __('Replace Replicated Tables'),
650 'dataonly' => __('Replace Replicated Tables(data only)')
652 PMA_display_html_checkbox('replace_existed_table', __('Replace Existed Tables'),false,false);
656 <fieldset
class="tblFooters">
657 <input type
="submit" value
="<?php echo __('Go'); ?>" />
665 * Change database charset
667 echo '<form id="change_db_charset_form" ';
668 if ($GLOBALS['cfg']['AjaxEnable']) {
669 echo ' class="ajax" ';
671 echo 'method="post" action="./db_operations.php">'
672 . PMA_generate_common_hidden_inputs($db, $table)
673 . '<fieldset>' . "\n"
675 if ($cfg['PropertiesIconic']) {
676 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
677 .' alt="" width="16" height="16" />';
679 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
680 . ' </legend>' . "\n"
681 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION
,
682 'db_collation', 'select_db_collation', $db_collation, false, 3)
684 . '<fieldset class="tblFooters">'
685 . ' <input type="submit" name="submitcollation"'
686 . ' value="' . __('Go') . '" />' . "\n"
687 . '</fieldset>' . "\n"
691 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
692 $message = PMA_Message
::notice(__('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.'));
693 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
694 $message->addParam('</a>', false);
695 /* Show error if user has configured something, notice elsewhere */
696 if (!empty($cfg['Servers'][$server]['pmadb'])) {
697 $message->isError(true);
701 } // end if (!$is_information_schema)
704 // not sure about displaying the PDF dialog in case db is information_schema
705 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?
>
706 <!-- Work on PDF Pages
-->
709 // We only show this if we find something in the new pdf_pages table
713 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
714 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
715 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE
);
718 * Export Relational Schema View
720 echo '<fieldset><a href="schema_edit.php?' . $url_query . '">';
721 if ($cfg['PropertiesIconic']) {
722 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
723 .' alt="" width="16" height="16" />';
725 echo __('Edit or export relational schema') . '</a></fieldset>';
729 * Displays the footer
731 require './libraries/footer.inc.php';