Translation update done using Pootle.
[phpmyadmin/dkf.git] / db_operations.php
blob40ec65272a1c1e528dca15edbc964228f0bfc209
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(__('The database name is empty!'));
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 // save the original db name because Tracker.class.php which
57 // may be called under PMA_DBI_query() changes $GLOBALS['db']
58 // for some statements, one of which being CREATE DATABASE
59 $original_db = $db;
60 PMA_DBI_query($local_query);
61 $db = $original_db;
62 unset($original_db);
64 // rebuild the database list because PMA_Table::moveCopy
65 // checks in this list if the target db exists
66 $GLOBALS['pma']->databases->build();
69 if (isset($GLOBALS['add_constraints']) || $move) {
70 $GLOBALS['sql_constraints_query_full_db'] = array();
73 $tables_full = PMA_DBI_get_tables_full($db);
74 $views = array();
76 // remove all foreign key constraints, otherwise we can get errors
77 require_once './libraries/export/sql.php';
78 foreach ($tables_full as $each_table => $tmp) {
79 $sql_constraints = '';
80 $sql_drop_foreign_keys = '';
81 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
82 if ($move && ! empty($sql_drop_foreign_keys)) {
83 PMA_DBI_query($sql_drop_foreign_keys);
85 // keep the constraint we just dropped
86 if (! empty($sql_constraints)) {
87 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
90 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
93 foreach ($tables_full as $each_table => $tmp) {
94 // to be able to rename a db containing views, we
95 // first collect in $views all the views we find and we
96 // will handle them after the tables
97 /**
98 * @todo support a view of a view
100 if (PMA_Table::isView($db, $each_table)) {
101 $views[] = $each_table;
102 continue;
105 $back = $sql_query;
106 $sql_query = '';
108 // value of $what for this table only
109 $this_what = $what;
111 // do not copy the data from a Merge table
112 // note: on the calling FORM, 'data' means 'structure and data'
113 if (PMA_Table::isMerge($db, $each_table)) {
114 if ($this_what == 'data') {
115 $this_what = 'structure';
117 if ($this_what == 'dataonly') {
118 $this_what = 'nocopy';
122 if ($this_what != 'nocopy') {
123 // keep the triggers from the original db+table
124 // (third param is empty because delimiters are only intended
125 // for importing via the mysql client or our Import feature)
126 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
128 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
129 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
131 $_error = true;
132 // $sql_query is filled by PMA_Table::moveCopy()
133 $sql_query = $back . $sql_query;
134 break;
136 // apply the triggers to the destination db+table
137 if ($triggers) {
138 PMA_DBI_select_db($newname);
139 foreach ($triggers as $trigger) {
140 PMA_DBI_query($trigger['create']);
142 unset($trigger);
144 unset($triggers);
146 // this does not apply to a rename operation
147 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
148 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
149 unset($GLOBALS['sql_constraints_query']);
152 // $sql_query is filled by PMA_Table::moveCopy()
153 $sql_query = $back . $sql_query;
154 } // end (foreach)
155 unset($each_table);
157 // handle the views
158 if (! $_error) {
159 foreach ($views as $view) {
160 if (! PMA_Table::moveCopy($db, $view, $newname, $view,
161 'structure', $move, 'db_copy')) {
162 $_error = true;
163 break;
167 unset($view, $views);
169 // now that all tables exist, create all the accumulated constraints
170 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
171 PMA_DBI_select_db($newname);
172 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
173 PMA_DBI_query($one_query);
174 // and prepare to display them
175 $GLOBALS['sql_query'] .= "\n" . $one_query;
178 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
181 if (PMA_MYSQL_INT_VERSION >= 50000) {
182 // here I don't use DELIMITER because it's not part of the
183 // language; I have to send each statement one by one
185 // to avoid selecting alternatively the current and new db
186 // we would need to modify the CREATE definitions to qualify
187 // the db name
188 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
189 if ($procedure_names) {
190 foreach($procedure_names as $procedure_name) {
191 PMA_DBI_select_db($db);
192 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
193 // collect for later display
194 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
195 PMA_DBI_select_db($newname);
196 PMA_DBI_query($tmp_query);
200 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
201 if ($function_names) {
202 foreach($function_names as $function_name) {
203 PMA_DBI_select_db($db);
204 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
205 // collect for later display
206 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
207 PMA_DBI_select_db($newname);
208 PMA_DBI_query($tmp_query);
212 // go back to current db, just in case
213 PMA_DBI_select_db($db);
215 // Duplicate the bookmarks for this db (done once for each db)
216 if (! $_error && $db != $newname) {
217 $get_fields = array('user', 'label', 'query');
218 $where_fields = array('dbase' => $db);
219 $new_fields = array('dbase' => $newname);
220 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
221 $where_fields, $new_fields);
224 if (! $_error && $move) {
226 * cleanup pmadb stuff for this db
228 require_once './libraries/relation_cleanup.lib.php';
229 PMA_relationsCleanupDatabase($db);
231 // if someday the RENAME DATABASE reappears, do not DROP
232 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
233 $sql_query .= "\n" . $local_query;
234 PMA_DBI_query($local_query);
236 $message = PMA_Message::success(__('Database %s has been renamed to %s'));
237 $message->addParam($db);
238 $message->addParam($newname);
239 } elseif (! $_error) {
240 $message = PMA_Message::success(__('Database %s has been copied to %s'));
241 $message->addParam($db);
242 $message->addParam($newname);
244 $reload = true;
246 /* Change database to be used */
247 if (! $_error && $move) {
248 $db = $newname;
249 } elseif (! $_error) {
250 if (isset($switch_to_new) && $switch_to_new == 'true') {
251 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
252 $db = $newname;
253 } else {
254 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
258 if ($_error && ! isset($message)) {
259 $message = PMA_Message::error();
265 * Enable/Disable/Repair BLOB Repository Monitoring for current database
267 if (strlen($db) > 0 && !empty($db_blob_streaming_op))
269 // load PMA_Config
270 $PMA_Config = $GLOBALS['PMA_Config'];
272 if (!empty($PMA_Config))
274 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
276 // if Blobstreaming plugins exist, begin checking for Blobstreaming tables
277 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
279 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
280 $bs_tables = $bs_tables[$db];
282 $oneBSTableExists = FALSE;
284 // check if at least one blobstreaming table exists
285 foreach ($bs_tables as $table_key=>$tbl)
286 if ($bs_tables[$table_key]['Exists'])
288 $oneBSTableExists = TRUE;
289 break;
292 switch ($db_blob_streaming_op)
294 // enable BLOB repository monitoring
295 case "enable":
296 // if blobstreaming tables do not exist, create them
297 if (!$oneBSTableExists)
298 PMA_BS_CreateTables($db);
299 break;
300 // disable BLOB repository monitoring
301 case "disable":
302 // if at least one blobstreaming table exists, execute drop
303 if ($oneBSTableExists)
304 PMA_BS_DropTables($db);
305 break;
306 // repair BLOB repository
307 case "repair":
308 // check if a blobstreaming table is missing
309 foreach ($bs_tables as $table_key=>$tbl)
310 if (!$bs_tables[$table_key]['Exists'])
312 PMA_DBI_select_db($db);
313 PMA_DBI_query(PMA_BS_GetTableStruct($table_key));
317 // refresh side menu
318 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');
319 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
320 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
325 * Settings for relations stuff
328 require_once './libraries/relation.lib.php';
329 $cfgRelation = PMA_getRelationsParam();
332 * Check if comments were updated
333 * (must be done before displaying the menu tabs)
335 if (isset($_REQUEST['comment'])) {
336 PMA_setDbComment($db, $comment);
340 * Prepares the tables list if the user where not redirected to this script
341 * because there is no table in the database ($is_info is true)
343 if (empty($is_info)) {
344 require './libraries/db_common.inc.php';
345 $url_query .= '&amp;goto=db_operations.php';
347 // Gets the database structure
348 $sub_part = '_structure';
349 require './libraries/db_info.inc.php';
350 echo "\n";
352 if (isset($message)) {
353 PMA_showMessage($message, $sql_query);
354 unset($message);
358 $db_collation = PMA_getDbCollation($db);
359 if ($db == 'information_schema') {
360 $is_information_schema = true;
361 } else {
362 $is_information_schema = false;
365 if (!$is_information_schema) {
367 require './libraries/display_create_table.lib.php';
369 if ($cfgRelation['commwork']) {
371 * database comment
374 <form method="post" action="db_operations.php">
375 <?php echo PMA_generate_common_hidden_inputs($db); ?>
376 <fieldset>
377 <legend>
378 <?php echo PMA_getIcon('b_comment.png', __('Database comment: '), false, true); ?>
379 </legend>
380 <input type="text" name="comment" class="textfield" size="30"
381 value="<?php
382 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
383 </fieldset>
384 <fieldset class="tblFooters">
385 <input type="submit" value="<?php echo __('Go'); ?>" />
386 </fieldset>
387 </form>
388 <?php
391 * rename database
394 <form method="post" action="db_operations.php"
395 onsubmit="return emptyFormElements(this, 'newname')">
396 <?php
397 if (isset($db_collation)) {
398 echo '<input type="hidden" name="db_collation" value="' . $db_collation
399 .'" />' . "\n";
402 <input type="hidden" name="what" value="data" />
403 <input type="hidden" name="db_rename" value="true" />
404 <?php echo PMA_generate_common_hidden_inputs($db); ?>
405 <fieldset>
406 <legend>
407 <?php
408 if ($cfg['PropertiesIconic']) {
409 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
410 .' alt="" width="16" height="16" />';
412 echo __('Rename database to') . ':';
414 </legend>
415 <input type="text" name="newname" size="30" class="textfield" value="" />
416 <?php
417 echo '(' . __('Command') . ': ';
419 * @todo (see explanations above in a previous todo)
421 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
422 // echo 'RENAME DATABASE';
423 //} else {
424 echo 'INSERT INTO ... SELECT';
426 echo ')'; ?>
427 </fieldset>
428 <fieldset class="tblFooters">
429 <input type="submit" value="<?php echo __('Go'); ?>" onclick="return confirmLink(this, 'CREATE DATABASE ... <?php echo __('and then'); ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
430 </fieldset>
431 </form>
433 <?php
435 * Copy database
438 <form method="post" action="db_operations.php"
439 onsubmit="return emptyFormElements(this, 'newname')">
440 <?php
441 if (isset($db_collation)) {
442 echo '<input type="hidden" name="db_collation" value="' . $db_collation
443 .'" />' . "\n";
445 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
446 echo PMA_generate_common_hidden_inputs($db);
448 <fieldset>
449 <legend>
450 <?php
451 if ($cfg['PropertiesIconic']) {
452 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
453 .' alt="" width="16" height="16" />';
455 echo __('Copy database to') . ':';
456 $drop_clause = 'DROP TABLE / DROP VIEW';
458 </legend>
459 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
460 <?php
461 $choices = array(
462 'structure' => __('Structure only'),
463 'data' => __('Structure and data'),
464 'dataonly' => __('Data only'));
465 PMA_display_html_radio('what', $choices, 'data', true);
466 unset($choices);
468 <input type="checkbox" name="create_database_before_copying" value="1"
469 id="checkbox_create_database_before_copying"
470 checked="checked" />
471 <label for="checkbox_create_database_before_copying">
472 <?php echo __('CREATE DATABASE before copying'); ?></label><br />
473 <input type="checkbox" name="drop_if_exists" value="true"
474 id="checkbox_drop" />
475 <label for="checkbox_drop"><?php echo sprintf(__('Add %s'), $drop_clause); ?></label><br />
476 <input type="checkbox" name="sql_auto_increment" value="1" checked="checked"
477 id="checkbox_auto_increment" />
478 <label for="checkbox_auto_increment">
479 <?php echo __('Add AUTO_INCREMENT value'); ?></label><br />
480 <input type="checkbox" name="add_constraints" value="1"
481 id="checkbox_constraints" />
482 <label for="checkbox_constraints">
483 <?php echo __('Add constraints'); ?></label><br />
484 <?php
485 unset($drop_clause);
487 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
488 && $_COOKIE['pma_switch_to_new'] == 'true') {
489 $pma_switch_to_new = 'true';
492 <input type="checkbox" name="switch_to_new" value="true"
493 id="checkbox_switch"
494 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
496 <label for="checkbox_switch"><?php echo __('Switch to copied database'); ?></label>
497 </fieldset>
498 <fieldset class="tblFooters">
499 <input type="submit" name="submit_copy" value="<?php echo __('Go'); ?>" />
500 </fieldset>
501 </form>
503 <?php
505 * BLOB streaming support
508 // load PMA_Config
509 $PMA_Config = $GLOBALS['PMA_Config'];
511 // if all blobstreaming plugins exist, begin checking for blobstreaming tables
512 if (!empty($PMA_Config))
514 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
516 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
518 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
519 $bs_tables = $bs_tables[$db];
521 $oneBSTableExists = FALSE;
522 $allBSTablesExist = TRUE;
524 // first check that all blobstreaming tables do not exist
525 foreach ($bs_tables as $table_key=>$tbl)
526 if ($bs_tables[$table_key]['Exists'])
527 $oneBSTableExists = TRUE;
528 else
529 $allBSTablesExist = FALSE;
533 <form method="post" action="./db_operations.php">
534 <?php echo PMA_generate_common_hidden_inputs($db); ?>
535 <fieldset>
536 <legend>
537 <?php echo PMA_getIcon('b_edit.png', __('BLOB Repository'), false, true); ?>
538 </legend>
540 <?php echo __('Status'); ?>:
542 <?php
544 // if the blobstreaming tables exist, provide option to disable the BLOB repository
545 if ($allBSTablesExist)
548 <?php echo _pgettext('BLOB repository', 'Enabled'); ?>
549 </fieldset>
550 <fieldset class="tblFooters">
551 <input type="hidden" name="db_blob_streaming_op" value="disable" />
552 <input type="submit" onclick="return confirmDisableRepository('<?php echo $db; ?>');" value="<?php echo __('Disable'); ?>" />
553 </fieldset>
554 <?php
556 else
558 // if any of the blobstreaming tables are missing, provide option to repair the BLOB repository
559 if ($oneBSTableExists && !$allBSTablesExist)
562 <?php echo __('Damaged'); ?>
563 </fieldset>
564 <fieldset class="tblFooters">
565 <input type="hidden" name="db_blob_streaming_op" value="repair" />
566 <input type="submit" value="<?php echo _pgettext('BLOB repository', 'Repair'); ?>" />
567 </fieldset>
568 <?php
570 // if none of the blobstreaming tables exist, provide option to enable BLOB repository
571 else
574 <?php echo _pgettext('BLOB repository', 'Disabled'); ?>
575 </fieldset>
576 <fieldset class="tblFooters">
577 <input type="hidden" name="db_blob_streaming_op" value="enable" />
578 <input type="submit" value="<?php echo __('Enable'); ?>" />
579 </fieldset>
580 <?php
582 } // end if ($allBSTablesExist)
585 </form>
586 <?php
587 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
588 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
592 * Change database charset
594 echo '<form method="post" action="./db_operations.php">' . "\n"
595 . PMA_generate_common_hidden_inputs($db, $table)
596 . '<fieldset>' . "\n"
597 . ' <legend>';
598 if ($cfg['PropertiesIconic']) {
599 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
600 .' alt="" width="16" height="16" />';
602 echo ' <label for="select_db_collation">' . __('Collation') . ':</label>' . "\n"
603 . ' </legend>' . "\n"
604 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
605 'db_collation', 'select_db_collation', $db_collation, false, 3)
606 . '</fieldset>'
607 . '<fieldset class="tblFooters">'
608 . ' <input type="submit" name="submitcollation"'
609 . ' value="' . __('Go') . '" />' . "\n"
610 . '</fieldset>' . "\n"
611 . '</form>' . "\n";
613 if ($num_tables > 0
614 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
615 $message = PMA_Message::notice(__('The additional features for working with linked tables have been deactivated. To find out why click %shere%s.'));
616 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
617 $message->addParam('</a>', false);
618 /* Show error if user has configured something, notice elsewhere */
619 if (!empty($cfg['Servers'][$server]['pmadb'])) {
620 $message->isError(true);
622 $message->display();
623 } // end if
624 } // end if (!$is_information_schema)
627 // not sure about displaying the PDF dialog in case db is information_schema
628 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
629 <!-- Work on PDF Pages -->
631 <?php
632 // We only show this if we find something in the new pdf_pages table
634 $test_query = '
635 SELECT *
636 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
637 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
638 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE);
640 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
641 include('./libraries/display_pdf_schema.lib.php');
642 } // end if
643 echo '<fieldset><a href="pdf_pages.php?' . $url_query . '">';
644 if ($cfg['PropertiesIconic']) {
645 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
646 .' alt="" width="16" height="16" />';
648 echo __('Edit PDF Pages') . '</a></fieldset>';
649 } // end if
652 * Displays the footer
654 require_once './libraries/footer.inc.php';