Join multiline strings.
[phpmyadmin/last10db.git] / db_operations.php
blob6aa9d20861f6a1b1dc712c3c4e0d0b1e4b725124
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 * @version $Id$
13 * @package phpMyAdmin
16 /**
17 * requirements
19 require_once './libraries/common.inc.php';
20 require_once './libraries/Table.class.php';
21 require_once './libraries/mysql_charsets.lib.php';
23 // add blobstreaming library functions
24 require_once "./libraries/blobstreaming.lib.php";
26 /**
27 * Rename/move or copy database
29 if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) {
31 if (! empty($db_rename)) {
32 $move = true;
33 } else {
34 $move = false;
37 if (!isset($newname) || !strlen($newname)) {
38 $message = PMA_Message::error('strDatabaseEmpty');
39 } else {
40 $sql_query = ''; // in case target db exists
41 $_error = false;
42 if ($move ||
43 (isset($create_database_before_copying) && $create_database_before_copying)) {
44 // 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 = strtolower($newname);
50 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
51 if (isset($db_collation)) {
52 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
54 $local_query .= ';';
55 $sql_query = $local_query;
56 PMA_DBI_query($local_query);
58 // rebuild the database list because PMA_Table::moveCopy
59 // checks in this list if the target db exists
60 $GLOBALS['pma']->databases->build();
63 if (isset($GLOBALS['add_constraints'])) {
64 $GLOBALS['sql_constraints_query_full_db'] = '';
67 $tables_full = PMA_DBI_get_tables_full($db);
68 $views = array();
69 foreach ($tables_full as $each_table => $tmp) {
70 // to be able to rename a db containing views, we
71 // first collect in $views all the views we find and we
72 // will handle them after the tables
73 /**
74 * @todo support a view of a view
76 if (PMA_Table::isView($db, $each_table)) {
77 $views[] = $each_table;
78 continue;
81 $back = $sql_query;
82 $sql_query = '';
84 // value of $what for this table only
85 $this_what = $what;
87 // do not copy the data from a Merge table
88 // note: on the calling FORM, 'data' means 'structure and data'
89 if ($tables_full[$each_table]['Engine'] == 'MRG_MyISAM') {
90 if ($this_what == 'data') {
91 $this_what = 'structure';
93 if ($this_what == 'dataonly') {
94 $this_what = 'nocopy';
98 if ($this_what != 'nocopy') {
99 // keep the triggers from the original db+table
100 // (third param is empty because delimiters are only intended
101 // for importing via the mysql client or our Import feature)
102 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
104 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
105 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
107 $_error = true;
108 // $sql_query is filled by PMA_Table::moveCopy()
109 $sql_query = $back . $sql_query;
110 break;
112 // apply the triggers to the destination db+table
113 if ($triggers) {
114 PMA_DBI_select_db($newname);
115 foreach ($triggers as $trigger) {
116 PMA_DBI_query($trigger['create']);
118 unset($trigger);
120 unset($triggers);
122 if (isset($GLOBALS['add_constraints'])) {
123 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
124 unset($GLOBALS['sql_constraints_query']);
127 // $sql_query is filled by PMA_Table::moveCopy()
128 $sql_query = $back . $sql_query;
129 } // end (foreach)
130 unset($each_table);
132 // handle the views
133 if (! $_error) {
134 foreach ($views as $view) {
135 if (! PMA_Table::moveCopy($db, $view, $newname, $view,
136 'structure', $move, 'db_copy')) {
137 $_error = true;
138 break;
142 unset($view, $views);
144 // now that all tables exist, create all the accumulated constraints
145 if (! $_error && isset($GLOBALS['add_constraints'])) {
147 * @todo this works with mysqli but not with mysql, because
148 * mysql extension does not accept more than one statement; maybe
149 * interface with the sql import plugin that handles statement delimiter
151 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
153 // and prepare to display them
154 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
155 unset($GLOBALS['sql_constraints_query_full_db']);
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
164 // the db name
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 // Duplicate the bookmarks for this db (done once for each db)
193 if (! $_error && $db != $newname) {
194 $get_fields = array('user', 'label', 'query');
195 $where_fields = array('dbase' => $db);
196 $new_fields = array('dbase' => $newname);
197 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
198 $where_fields, $new_fields);
201 if (! $_error && $move) {
203 * cleanup pmadb stuff for this db
205 require_once './libraries/relation_cleanup.lib.php';
206 PMA_relationsCleanupDatabase($db);
208 // if someday the RENAME DATABASE reappears, do not DROP
209 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
210 $sql_query .= "\n" . $local_query;
211 PMA_DBI_query($local_query);
213 $message = PMA_Message::success('strRenameDatabaseOK');
214 $message->addParam($db);
215 $message->addParam($newname);
216 } elseif (! $_error) {
217 $message = PMA_Message::success('strCopyDatabaseOK');
218 $message->addParam($db);
219 $message->addParam($newname);
221 $reload = true;
223 /* Change database to be used */
224 if (! $_error && $move) {
225 $db = $newname;
226 } elseif (! $_error) {
227 if (isset($switch_to_new) && $switch_to_new == 'true') {
228 PMA_setCookie('pma_switch_to_new', 'true');
229 $db = $newname;
230 } else {
231 PMA_setCookie('pma_switch_to_new', '');
235 if ($_error && ! isset($message)) {
236 $message = PMA_Message::error();
242 * Enable/Disable/Repair BLOB Repository Monitoring for current database
244 if (strlen($db) > 0 && !empty($db_blob_streaming_op))
246 // load PMA_Config
247 $PMA_Config = $_SESSION['PMA_Config'];
249 if (!empty($PMA_Config))
251 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
253 // if Blobstreaming plugins exist, begin checking for Blobstreaming tables
254 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
256 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
257 $bs_tables = $bs_tables[$db];
259 $oneBSTableExists = FALSE;
261 // check if at least one blobstreaming table exists
262 foreach ($bs_tables as $table_key=>$tbl)
263 if ($bs_tables[$table_key]['Exists'])
265 $oneBSTableExists = TRUE;
266 break;
269 switch ($db_blob_streaming_op)
271 // enable BLOB repository monitoring
272 case "enable":
273 // if blobstreaming tables do not exist, create them
274 if (!$oneBSTableExists)
275 PMA_BS_CreateTables($db);
276 break;
277 // disable BLOB repository monitoring
278 case "disable":
279 // if at least one blobstreaming table exists, execute drop
280 if ($oneBSTableExists)
281 PMA_BS_DropTables($db);
282 break;
283 // repair BLOB repository
284 case "repair":
285 // check if a blobstreaming table is missing
286 foreach ($bs_tables as $table_key=>$tbl)
287 if (!$bs_tables[$table_key]['Exists'])
289 PMA_DBI_select_db($db);
290 PMA_DBI_query(PMA_BS_GetTableStruct($table_key));
294 // refresh side menu
295 PMA_sendHeaderLocation($cfg['PmaAbsoluteUri'] . 'db_operations.php?' . PMA_generate_common_url ('','', '&') . (isset($db) ? '&db=' . urlencode($db) : '') . (isset($token) ? '&token=' . urlencode($token) : '') . (isset($goto) ? '&goto=' . urlencode($goto) : '') . 'reload=1&purge=1');
296 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
297 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
302 * Settings for relations stuff
305 require_once './libraries/relation.lib.php';
306 $cfgRelation = PMA_getRelationsParam();
309 * Check if comments were updated
310 * (must be done before displaying the menu tabs)
312 if (isset($_REQUEST['comment'])) {
313 PMA_setDbComment($db, $comment);
317 * Prepares the tables list if the user where not redirected to this script
318 * because there is no table in the database ($is_info is true)
320 if (empty($is_info)) {
321 require './libraries/db_common.inc.php';
322 $url_query .= '&amp;goto=db_operations.php';
324 // Gets the database structure
325 $sub_part = '_structure';
326 require './libraries/db_info.inc.php';
327 echo "\n";
329 if (isset($message)) {
330 PMA_showMessage($message, $sql_query);
331 unset($message);
335 $db_collation = PMA_getDbCollation($db);
336 if ($db == 'information_schema') {
337 $is_information_schema = true;
338 } else {
339 $is_information_schema = false;
342 if (!$is_information_schema) {
344 require './libraries/display_create_table.lib.php';
346 if ($cfgRelation['commwork']) {
348 * database comment
351 <form method="post" action="db_operations.php">
352 <?php echo PMA_generate_common_hidden_inputs($db); ?>
353 <fieldset>
354 <legend>
355 <?php echo PMA_getIcon('b_comment.png', $strDBComment, false, true); ?>
356 </legend>
357 <input type="text" name="comment" class="textfield" size="30"
358 value="<?php
359 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
360 <input type="submit" value="<?php echo $strGo; ?>" />
361 </fieldset>
362 </form>
363 <?php
366 * rename database
369 <form method="post" action="db_operations.php"
370 onsubmit="return emptyFormElements(this, 'newname')">
371 <?php
372 if (isset($db_collation)) {
373 echo '<input type="hidden" name="db_collation" value="' . $db_collation
374 .'" />' . "\n";
377 <input type="hidden" name="what" value="data" />
378 <input type="hidden" name="db_rename" value="true" />
379 <?php echo PMA_generate_common_hidden_inputs($db); ?>
380 <fieldset>
381 <legend>
382 <?php
383 if ($cfg['PropertiesIconic']) {
384 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
385 .' alt="" width="16" height="16" />';
387 echo $strDBRename . ':';
389 </legend>
390 <input type="text" name="newname" size="30" class="textfield" value="" />
391 <?php
392 echo '(' . $strCommand . ': ';
394 * @todo (see explanations above in a previous todo)
396 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
397 // echo 'RENAME DATABASE';
398 //} else {
399 echo 'INSERT INTO ... SELECT';
401 echo ')'; ?>
402 <input type="submit" value="<?php echo $strGo; ?>" onclick="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
403 </fieldset>
404 </form>
406 <?php
408 * Copy database
411 <form method="post" action="db_operations.php"
412 onsubmit="return emptyFormElements(this, 'newname')">
413 <?php
414 if (isset($db_collation)) {
415 echo '<input type="hidden" name="db_collation" value="' . $db_collation
416 .'" />' . "\n";
418 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
419 echo PMA_generate_common_hidden_inputs($db);
421 <fieldset>
422 <legend>
423 <?php
424 if ($cfg['PropertiesIconic']) {
425 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
426 .' alt="" width="16" height="16" />';
428 echo $strDBCopy . ':';
429 $drop_clause = 'DROP TABLE / DROP VIEW';
431 </legend>
432 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
433 <?php
434 $choices = array(
435 'structure' => $strStrucOnly,
436 'data' => $strStrucData,
437 'dataonly' => $strDataOnly);
438 PMA_display_html_radio('what', $choices, 'data', true);
439 unset($choices);
441 <input type="checkbox" name="create_database_before_copying" value="1"
442 id="checkbox_create_database_before_copying"
443 style="vertical-align: middle" checked="checked" />
444 <label for="checkbox_create_database_before_copying">
445 <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
446 <input type="checkbox" name="drop_if_exists" value="true"
447 id="checkbox_drop" style="vertical-align: middle" />
448 <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
449 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
450 id="checkbox_auto_increment" style="vertical-align: middle" />
451 <label for="checkbox_auto_increment">
452 <?php echo $strAddAutoIncrement; ?></label><br />
453 <input type="checkbox" name="add_constraints" value="1"
454 id="checkbox_constraints" style="vertical-align: middle" />
455 <label for="checkbox_constraints">
456 <?php echo $strAddConstraints; ?></label><br />
457 <?php
458 unset($drop_clause);
460 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
461 && $_COOKIE['pma_switch_to_new'] == 'true') {
462 $pma_switch_to_new = 'true';
465 <input type="checkbox" name="switch_to_new" value="true"
466 id="checkbox_switch"
467 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
468 style="vertical-align: middle" />
469 <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
470 </fieldset>
471 <fieldset class="tblFooters">
472 <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
473 </fieldset>
474 </form>
476 <?php
478 * BLOB streaming support
481 // load PMA_Config
482 $PMA_Config = $_SESSION['PMA_Config'];
484 // if all blobstreaming plugins exist, begin checking for blobstreaming tables
485 if (!empty($PMA_Config))
487 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
489 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
491 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
492 $bs_tables = $bs_tables[$db];
494 $oneBSTableExists = FALSE;
495 $allBSTablesExist = TRUE;
497 // first check that all blobstreaming tables do not exist
498 foreach ($bs_tables as $table_key=>$tbl)
499 if ($bs_tables[$table_key]['Exists'])
500 $oneBSTableExists = TRUE;
501 else
502 $allBSTablesExist = FALSE;
506 <form method="post" action="./db_operations.php">
507 <?php echo PMA_generate_common_hidden_inputs($db); ?>
508 <fieldset>
509 <legend>
510 <?php echo PMA_getIcon('b_edit.png', $strBLOBRepository, false, true); ?>
511 </legend>
513 <?php echo $strStatus; ?>:
515 <?php
517 // if the blobstreaming tables exist, provide option to disable the BLOB repository
518 if ($allBSTablesExist)
521 <?php echo $strBLOBRepositoryEnabled; ?>
522 </fieldset>
523 <fieldset class="tblFooters">
524 <input type="hidden" name="db_blob_streaming_op" value="disable" />
525 <input type="submit" onclick="return confirmDisableRepository('<?php echo $db; ?>');" value="<?php echo $strBLOBRepositoryDisable; ?>" />
526 </fieldset>
527 <?php
529 else
531 // if any of the blobstreaming tables are missing, provide option to repair the BLOB repository
532 if ($oneBSTableExists && !$allBSTablesExist)
535 <?php echo $strBLOBRepositoryDamaged; ?>
536 </fieldset>
537 <fieldset class="tblFooters">
538 <input type="hidden" name="db_blob_streaming_op" value="repair" />
539 <input type="submit" value="<?php echo $strBLOBRepositoryRepair; ?>" />
540 </fieldset>
541 <?php
543 // if none of the blobstreaming tables exist, provide option to enable BLOB repository
544 else
547 <?php echo $strBLOBRepositoryDisabled; ?>
548 </fieldset>
549 <fieldset class="tblFooters">
550 <input type="hidden" name="db_blob_streaming_op" value="enable" />
551 <input type="submit" value="<?php echo $strBLOBRepositoryEnable; ?>" />
552 </fieldset>
553 <?php
555 } // end if ($allBSTablesExist)
558 </form>
559 <?php
560 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
561 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
565 * Change database charset
567 echo '<form method="post" action="./db_operations.php">' . "\n"
568 . PMA_generate_common_hidden_inputs($db, $table)
569 . '<fieldset>' . "\n"
570 . ' <legend>';
571 if ($cfg['PropertiesIconic']) {
572 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
573 .' alt="" width="16" height="16" />';
575 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
576 . ' </legend>' . "\n"
577 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
578 'db_collation', 'select_db_collation', $db_collation, false, 3)
579 . ' <input type="submit" name="submitcollation"'
580 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
581 . '</fieldset>' . "\n"
582 . '</form>' . "\n";
584 if ($num_tables > 0
585 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
586 $message = PMA_Message::notice('strRelationNotWorking');
587 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
588 $message->addParam('</a>', false);
589 /* Show error if user has configured something, notice elsewhere */
590 if (!empty($cfg['Servers'][$server]['pmadb'])) {
591 $message->isError(true);
593 $message->display();
594 } // end if
595 } // end if (!$is_information_schema)
598 // not sure about displaying the PDF dialog in case db is information_schema
599 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
600 <!-- Work on PDF Pages -->
602 <?php
603 // We only show this if we find something in the new pdf_pages table
605 $test_query = '
606 SELECT *
607 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
608 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
609 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
611 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
612 <!-- PDF schema -->
613 <form method="post" action="pdf_schema.php">
614 <fieldset>
615 <legend>
616 <?php
617 echo PMA_generate_common_hidden_inputs($db);
618 if ($cfg['PropertiesIconic']) {
619 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
620 .' alt="" width="16" height="16" />';
622 echo $strDisplayPDF;
624 </legend>
625 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
626 <select name="pdf_page_number" id="pdf_page_number_opt">
627 <?php
628 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
629 echo ' <option value="' . $pages['page_nr'] . '">'
630 . $pages['page_nr'] . ': ' . htmlspecialchars($pages['page_descr']) . '</option>' . "\n";
631 } // end while
632 PMA_DBI_free_result($test_rs);
633 unset($test_rs);
635 </select><br />
637 <input type="checkbox" name="show_grid" id="show_grid_opt" />
638 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
639 <input type="checkbox" name="show_color" id="show_color_opt"
640 checked="checked" />
641 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
642 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
643 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
644 </label><br />
645 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
646 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
647 </label><br />
648 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
649 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
650 <input type="checkbox" name="show_keys" id="show_keys" />
651 <label for="show_keys"><?php echo $strShowKeys; ?></label><br />
653 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
654 <select name="orientation" id="orientation_opt">
655 <option value="L"><?php echo $strLandscape;?></option>
656 <option value="P"><?php echo $strPortrait;?></option>
657 </select><br />
659 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
660 <select name="paper" id="paper_opt">
661 <?php
662 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
663 echo '<option value="' . $val . '"';
664 if ($val == $cfg['PDFDefaultPageSize']) {
665 echo ' selected="selected"';
667 echo ' >' . $val . '</option>' . "\n";
670 </select>
671 </fieldset>
672 <fieldset class="tblFooters">
673 <input type="submit" value="<?php echo $strGo; ?>" />
674 </fieldset>
675 </form>
676 <?php
677 } // end if
678 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
679 if ($cfg['PropertiesIconic']) {
680 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
681 .' alt="" width="16" height="16" />';
683 echo $strEditPDFPages . '</a>';
684 } // end if
687 * Displays the footer
689 require_once './libraries/footer.inc.php';