Translation is already loaded by libraries/select_lang.lib.php.
[phpmyadmin/last10db.git] / db_operations.php
blob517db1098592968793e14f99bfb1c978fe72f774
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$
15 /**
16 * requirements
18 require_once './libraries/common.inc.php';
19 require_once './libraries/Table.class.php';
20 require_once './libraries/mysql_charsets.lib.php';
22 /**
23 * Rename/move or copy database
25 if (strlen($db) && (! empty($db_rename) || ! empty($db_copy))) {
27 if (! empty($db_rename)) {
28 $move = true;
29 } else {
30 $move = false;
33 if (!isset($newname) || !strlen($newname)) {
34 $message = PMA_Message::error('strDatabaseEmpty');
35 } else {
36 $sql_query = ''; // in case target db exists
37 $_error = false;
38 if ($move ||
39 (isset($create_database_before_copying) && $create_database_before_copying)) {
40 // lower_case_table_names=1 `DB` becomes `db`
41 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
42 if ($lower_case_table_names === '1') {
43 $newname = strtolower($newname);
46 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
47 if (isset($db_collation)) {
48 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
50 $local_query .= ';';
51 $sql_query = $local_query;
52 PMA_DBI_query($local_query);
54 // rebuild the database list because PMA_Table::moveCopy
55 // checks in this list if the target db exists
56 $GLOBALS['pma']->databases->build();
59 if (isset($GLOBALS['add_constraints'])) {
60 $GLOBALS['sql_constraints_query_full_db'] = '';
63 $tables_full = PMA_DBI_get_tables_full($db);
64 $views = array();
65 foreach ($tables_full as $each_table => $tmp) {
66 // to be able to rename a db containing views, we
67 // first collect in $views all the views we find and we
68 // will handle them after the tables
69 /**
70 * @todo support a view of a view
71 * @todo support triggers
73 if (PMA_Table::isView($db, $each_table)) {
74 $views[] = $each_table;
75 continue;
78 $back = $sql_query;
79 $sql_query = '';
81 // value of $what for this table only
82 $this_what = $what;
84 // do not copy the data from a Merge table
85 // note: on the calling FORM, 'data' means 'structure and data'
86 if ($tables_full[$each_table]['Engine'] == 'MRG_MyISAM') {
87 if ($this_what == 'data') {
88 $this_what = 'structure';
90 if ($this_what == 'dataonly') {
91 $this_what = 'nocopy';
95 if ($this_what != 'nocopy') {
96 if (! PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
97 isset($this_what) ? $this_what : 'data', $move, 'db_copy'))
99 $_error = true;
100 // $sql_query is filled by PMA_Table::moveCopy()
101 $sql_query = $back . $sql_query;
102 break;
104 if (isset($GLOBALS['add_constraints'])) {
105 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
106 unset($GLOBALS['sql_constraints_query']);
109 // $sql_query is filled by PMA_Table::moveCopy()
110 $sql_query = $back . $sql_query;
111 } // end (foreach)
112 unset($each_table);
114 // handle the views
115 if (! $_error) {
116 foreach ($views as $view) {
117 if (! PMA_Table::moveCopy($db, $view, $newname, $view,
118 'structure', $move, 'db_copy')) {
119 $_error = true;
120 break;
124 unset($view, $views);
126 // now that all tables exist, create all the accumulated constraints
127 if (! $_error && isset($GLOBALS['add_constraints'])) {
129 * @todo this works with mysqli but not with mysql, because
130 * mysql extension does not accept more than one statement; maybe
131 * interface with the sql import plugin that handles statement delimiter
133 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
135 // and prepare to display them
136 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
137 unset($GLOBALS['sql_constraints_query_full_db']);
140 if (PMA_MYSQL_INT_VERSION >= 50000) {
141 // here I don't use DELIMITER because it's not part of the
142 // language; I have to send each statement one by one
144 // to avoid selecting alternatively the current and new db
145 // we would need to modify the CREATE definitions to qualify
146 // the db name
147 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
148 if ($procedure_names) {
149 foreach($procedure_names as $procedure_name) {
150 PMA_DBI_select_db($db);
151 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
152 // collect for later display
153 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
154 PMA_DBI_select_db($newname);
155 PMA_DBI_query($tmp_query);
159 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
160 if ($function_names) {
161 foreach($function_names as $function_name) {
162 PMA_DBI_select_db($db);
163 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
164 // collect for later display
165 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
166 PMA_DBI_select_db($newname);
167 PMA_DBI_query($tmp_query);
171 // go back to current db, just in case
172 PMA_DBI_select_db($db);
174 // Duplicate the bookmarks for this db (done once for each db)
175 if (! $_error && $db != $newname) {
176 $get_fields = array('user', 'label', 'query');
177 $where_fields = array('dbase' => $db);
178 $new_fields = array('dbase' => $newname);
179 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
180 $where_fields, $new_fields);
183 if (! $_error && $move) {
184 // cleanup pmadb stuff for this db
185 require_once './libraries/relation_cleanup.lib.php';
186 PMA_relationsCleanupDatabase($db);
188 // if someday the RENAME DATABASE reappears, do not DROP
189 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
190 $sql_query .= "\n" . $local_query;
191 PMA_DBI_query($local_query);
193 $message = PMA_Message::success('strRenameDatabaseOK');
194 $message->addParam($db);
195 $message->addParam($newname);
196 } elseif (! $_error) {
197 $message = PMA_Message::success('strCopyDatabaseOK');
198 $message->addParam($db);
199 $message->addParam($newname);
201 $reload = true;
203 /* Change database to be used */
204 if (! $_error && $move) {
205 $db = $newname;
206 } elseif (! $_error) {
207 if (isset($switch_to_new) && $switch_to_new == 'true') {
208 PMA_setCookie('pma_switch_to_new', 'true');
209 $db = $newname;
210 } else {
211 PMA_setCookie('pma_switch_to_new', '');
215 if ($_error && ! isset($message)) {
216 $message = PMA_Message::error();
221 * Settings for relations stuff
224 require_once './libraries/relation.lib.php';
225 $cfgRelation = PMA_getRelationsParam();
228 * Check if comments were updated
229 * (must be done before displaying the menu tabs)
231 if (isset($_REQUEST['comment'])) {
232 PMA_setDbComment($db, $comment);
236 * Prepares the tables list if the user where not redirected to this script
237 * because there is no table in the database ($is_info is true)
239 if (empty($is_info)) {
240 require './libraries/db_common.inc.php';
241 $url_query .= '&amp;goto=db_operations.php';
243 // Gets the database structure
244 $sub_part = '_structure';
245 require './libraries/db_info.inc.php';
246 echo "\n";
248 if (isset($message)) {
249 PMA_showMessage($message, $sql_query);
250 unset($message);
254 $db_collation = PMA_getDbCollation($db);
255 if ($db == 'information_schema') {
256 $is_information_schema = true;
257 } else {
258 $is_information_schema = false;
261 if (!$is_information_schema) {
263 require './libraries/display_create_table.lib.php';
265 if ($cfgRelation['commwork']) {
267 * database comment
270 <form method="post" action="db_operations.php">
271 <?php echo PMA_generate_common_hidden_inputs($db); ?>
272 <fieldset>
273 <legend>
274 <?php echo PMA_getIcon('b_comment.png', $strDBComment, false, true); ?>
275 </legend>
276 <input type="text" name="comment" class="textfield" size="30"
277 value="<?php
278 echo htmlspecialchars(PMA_getDbComment($db)); ?>" />
279 <input type="submit" value="<?php echo $strGo; ?>" />
280 </fieldset>
281 </form>
282 <?php
285 * rename database
288 <form method="post" action="db_operations.php"
289 onsubmit="return emptyFormElements(this, 'newname')">
290 <?php
291 if (isset($db_collation)) {
292 echo '<input type="hidden" name="db_collation" value="' . $db_collation
293 .'" />' . "\n";
296 <input type="hidden" name="what" value="data" />
297 <input type="hidden" name="db_rename" value="true" />
298 <?php echo PMA_generate_common_hidden_inputs($db); ?>
299 <fieldset>
300 <legend>
301 <?php
302 if ($cfg['PropertiesIconic']) {
303 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
304 .' alt="" width="16" height="16" />';
306 echo $strDBRename . ':';
308 </legend>
309 <input type="text" name="newname" size="30" class="textfield" value="" />
310 <?php
311 echo '(' . $strCommand . ': ';
313 * @todo (see explanations above in a previous todo)
315 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
316 // echo 'RENAME DATABASE';
317 //} else {
318 echo 'INSERT INTO ... SELECT';
320 echo ')'; ?>
321 <input type="submit" value="<?php echo $strGo; ?>" onclick="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
322 </fieldset>
323 </form>
325 <?php
327 * Copy database
330 <form method="post" action="db_operations.php"
331 onsubmit="return emptyFormElements(this, 'newname')">
332 <?php
333 if (isset($db_collation)) {
334 echo '<input type="hidden" name="db_collation" value="' . $db_collation
335 .'" />' . "\n";
337 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
338 echo PMA_generate_common_hidden_inputs($db);
340 <fieldset>
341 <legend>
342 <?php
343 if ($cfg['PropertiesIconic']) {
344 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
345 .' alt="" width="16" height="16" />';
347 echo $strDBCopy . ':';
348 $drop_clause = 'DROP TABLE / DROP VIEW';
350 </legend>
351 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
352 <?php
353 $choices = array(
354 'structure' => $strStrucOnly,
355 'data' => $strStrucData,
356 'dataonly' => $strDataOnly);
357 PMA_generate_html_radio('what', $choices, 'data', true);
358 unset($choices);
360 <input type="checkbox" name="create_database_before_copying" value="1"
361 id="checkbox_create_database_before_copying"
362 style="vertical-align: middle" checked="checked" />
363 <label for="checkbox_create_database_before_copying">
364 <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
365 <input type="checkbox" name="drop_if_exists" value="true"
366 id="checkbox_drop" style="vertical-align: middle" />
367 <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
368 <input type="checkbox" name="sql_auto_increment" value="1"
369 id="checkbox_auto_increment" style="vertical-align: middle" />
370 <label for="checkbox_auto_increment">
371 <?php echo $strAddAutoIncrement; ?></label><br />
372 <input type="checkbox" name="add_constraints" value="1"
373 id="checkbox_constraints" style="vertical-align: middle" />
374 <label for="checkbox_constraints">
375 <?php echo $strAddConstraints; ?></label><br />
376 <?php
377 unset($drop_clause);
379 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
380 && $_COOKIE['pma_switch_to_new'] == 'true') {
381 $pma_switch_to_new = 'true';
384 <input type="checkbox" name="switch_to_new" value="true"
385 id="checkbox_switch"
386 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
387 style="vertical-align: middle" />
388 <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
389 </fieldset>
390 <fieldset class="tblFooters">
391 <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
392 </fieldset>
393 </form>
395 <?php
397 * Change database charset
399 echo '<form method="post" action="./db_operations.php">' . "\n"
400 . PMA_generate_common_hidden_inputs($db, $table)
401 . '<fieldset>' . "\n"
402 . ' <legend>';
403 if ($cfg['PropertiesIconic']) {
404 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
405 .' alt="" width="16" height="16" />';
407 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
408 . ' </legend>' . "\n"
409 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
410 'db_collation', 'select_db_collation', $db_collation, false, 3)
411 . ' <input type="submit" name="submitcollation"'
412 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
413 . '</fieldset>' . "\n"
414 . '</form>' . "\n";
416 if ($num_tables > 0
417 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
418 $message = PMA_Message::notice('strRelationNotWorking');
419 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
420 $message->addParam('</a>', false);
421 /* Show error if user has configured something, notice elsewhere */
422 if (!empty($cfg['Servers'][$server]['pmadb'])) {
423 $message->isError(true);
425 $message->display();
426 } // end if
427 } // end if (!$is_information_schema)
430 // not sure about displaying the PDF dialog in case db is information_schema
431 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
432 <!-- Work on PDF Pages -->
434 <?php
435 // We only show this if we find something in the new pdf_pages table
437 $test_query = '
438 SELECT *
439 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
440 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
441 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
443 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
444 <!-- PDF schema -->
445 <form method="post" action="pdf_schema.php">
446 <fieldset>
447 <legend>
448 <?php
449 echo PMA_generate_common_hidden_inputs($db);
450 if ($cfg['PropertiesIconic']) {
451 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
452 .' alt="" width="16" height="16" />';
454 echo $strDisplayPDF;
456 </legend>
457 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
458 <select name="pdf_page_number" id="pdf_page_number_opt">
459 <?php
460 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
461 echo ' <option value="' . $pages['page_nr'] . '">'
462 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
463 } // end while
464 PMA_DBI_free_result($test_rs);
465 unset($test_rs);
467 </select><br />
469 <input type="checkbox" name="show_grid" id="show_grid_opt" />
470 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
471 <input type="checkbox" name="show_color" id="show_color_opt"
472 checked="checked" />
473 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
474 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
475 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
476 </label><br />
477 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
478 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
479 </label><br />
480 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
481 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
483 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
484 <select name="orientation" id="orientation_opt">
485 <option value="L"><?php echo $strLandscape;?></option>
486 <option value="P"><?php echo $strPortrait;?></option>
487 </select><br />
489 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
490 <select name="paper" id="paper_opt">
491 <?php
492 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
493 echo '<option value="' . $val . '"';
494 if ($val == $cfg['PDFDefaultPageSize']) {
495 echo ' selected="selected"';
497 echo ' >' . $val . '</option>' . "\n";
500 </select>
501 </fieldset>
502 <fieldset class="tblFooters">
503 <input type="submit" value="<?php echo $strGo; ?>" />
504 </fieldset>
505 </form>
506 <?php
507 } // end if
508 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
509 if ($cfg['PropertiesIconic']) {
510 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
511 .' alt="" width="16" height="16" />';
513 echo $strEditPDFPages . '</a>';
514 } // end if
517 * Displays the footer
519 require_once './libraries/footer.inc.php';