2.11.6
[phpmyadmin/arisferyanto.git] / db_operations.php
blobebac542afb8ad05155ea9ff69bc22162de3e142f
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) &&
26 ((isset($db_rename) && $db_rename == 'true') ||
27 (isset($db_copy) && $db_copy == 'true'))) {
29 if (isset($db_rename) && $db_rename == 'true') {
30 $move = true;
31 } else {
32 $move = false;
35 if (!isset($newname) || !strlen($newname)) {
36 $message = $strDatabaseEmpty;
37 } else {
38 $sql_query = ''; // in case target db exists
39 if ($move ||
40 (isset($create_database_before_copying) && $create_database_before_copying)) {
41 // lower_case_table_names=1 `DB` becomes `db`
42 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
43 if ($lower_case_table_names === '1') {
44 $newname = strtolower($newname);
47 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
48 if (isset($db_collation)) {
49 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
51 $local_query .= ';';
52 $sql_query = $local_query;
53 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_List_Database']->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 if (!isset($tables_full[$each_table]['Engine'])) {
85 $tables_full[$each_table]['Engine'] = $tables_full[$each_table]['Type'];
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 PMA_Table::moveCopy($db, $each_table, $newname, $each_table,
100 isset($this_what) ? $this_what : 'data', $move, 'db_copy');
101 if (isset($GLOBALS['add_constraints'])) {
102 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
103 unset($GLOBALS['sql_constraints_query']);
107 $sql_query = $back . $sql_query;
108 } // end (foreach)
109 unset($each_table);
111 // handle the views
112 foreach ($views as $view) {
113 PMA_Table::moveCopy($db, $view, $newname, $view,
114 'structure', $move, 'db_copy');
116 unset($view, $views);
118 // now that all tables exist, create all the accumulated constraints
119 if (isset($GLOBALS['add_constraints'])) {
121 * @todo this works with mysqli but not with mysql, because
122 * mysql extension does not accept more than one statement; maybe
123 * interface with the sql import plugin that handles statement delimiter
125 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
127 // and prepare to display them
128 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
129 unset($GLOBALS['sql_constraints_query_full_db']);
132 if (PMA_MYSQL_INT_VERSION >= 50000) {
133 // here I don't use DELIMITER because it's not part of the
134 // language; I have to send each statement one by one
136 // to avoid selecting alternatively the current and new db
137 // we would need to modify the CREATE definitions to qualify
138 // the db name
139 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
140 if ($procedure_names) {
141 foreach($procedure_names as $procedure_name) {
142 PMA_DBI_select_db($db);
143 $tmp_query = PMA_DBI_get_procedure_or_function_def($db, 'PROCEDURE', $procedure_name);
144 // collect for later display
145 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
146 PMA_DBI_select_db($newname);
147 PMA_DBI_query($tmp_query);
151 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
152 if ($function_names) {
153 foreach($function_names as $function_name) {
154 PMA_DBI_select_db($db);
155 $tmp_query = PMA_DBI_get_procedure_or_function_def($db, 'FUNCTION', $function_name);
156 // collect for later display
157 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
158 PMA_DBI_select_db($newname);
159 PMA_DBI_query($tmp_query);
163 // go back to current db, just in case
164 PMA_DBI_select_db($db);
166 // Duplicate the bookmarks for this db (done once for each db)
167 if ($db != $newname) {
168 $get_fields = array('user', 'label', 'query');
169 $where_fields = array('dbase' => $db);
170 $new_fields = array('dbase' => $newname);
171 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
172 $where_fields, $new_fields);
175 if ($move) {
176 // cleanup pmadb stuff for this db
177 require_once './libraries/relation_cleanup.lib.php';
178 PMA_relationsCleanupDatabase($db);
180 // if someday the RENAME DATABASE reappears, do not DROP
181 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
182 $sql_query .= "\n" . $local_query;
183 PMA_DBI_query($local_query);
185 $message = sprintf($strRenameDatabaseOK, htmlspecialchars($db),
186 htmlspecialchars($newname));
187 } else {
188 $message = sprintf($strCopyDatabaseOK, htmlspecialchars($db),
189 htmlspecialchars($newname));
191 $reload = true;
193 /* Change database to be used */
194 if ($move) {
195 $db = $newname;
196 } else {
197 if (isset($switch_to_new) && $switch_to_new == 'true') {
198 PMA_setCookie('pma_switch_to_new', 'true');
199 $db = $newname;
200 } else {
201 PMA_setCookie('pma_switch_to_new', '');
207 * Settings for relations stuff
210 require_once './libraries/relation.lib.php';
211 $cfgRelation = PMA_getRelationsParam();
214 * Check if comments were updated
215 * (must be done before displaying the menu tabs)
217 if ($cfgRelation['commwork'] && isset($db_comment) && $db_comment == 'true') {
218 PMA_SetComment($db, '', '(db_comment)', $comment);
222 * Prepares the tables list if the user where not redirected to this script
223 * because there is no table in the database ($is_info is true)
225 if (empty($is_info)) {
226 require './libraries/db_common.inc.php';
227 $url_query .= '&amp;goto=db_operations.php';
229 // Gets the database structure
230 $sub_part = '_structure';
231 require './libraries/db_info.inc.php';
232 echo "\n";
235 if (PMA_MYSQL_INT_VERSION >= 40101) {
236 $db_collation = PMA_getDbCollation($db);
238 if (PMA_MYSQL_INT_VERSION < 50002
239 || (PMA_MYSQL_INT_VERSION >= 50002 && $db != 'information_schema')) {
240 $is_information_schema = false;
241 } else {
242 $is_information_schema = true;
245 if (!$is_information_schema) {
247 require './libraries/display_create_table.lib.php';
249 if ($cfgRelation['commwork']) {
251 * database comment
254 <form method="post" action="db_operations.php">
255 <?php echo PMA_generate_common_hidden_inputs($db); ?>
256 <input type="hidden" name="db_comment" value="true" />
257 <fieldset>
258 <legend>
259 <?php
260 if ($cfg['PropertiesIconic']) {
261 echo '<img class="icon" src="' . $pmaThemeImage . 'b_comment.png"'
262 .' alt="" border="0" width="16" height="16" hspace="2" align="middle" />';
264 echo $strDBComment;
265 $comment = PMA_getComments($db);
267 </legend>
268 <input type="text" name="comment" class="textfield" size="30"
269 value="<?php
270 echo (isset($comment) && is_array($comment)
271 ? htmlspecialchars(implode(' ', $comment))
272 : ''); ?>" />
273 <input type="submit" value="<?php echo $strGo; ?>" />
274 </fieldset>
275 </form>
276 <?php
279 * rename database
282 <form method="post" action="db_operations.php"
283 onsubmit="return emptyFormElements(this, 'newname')">
284 <?php
285 if (isset($db_collation)) {
286 echo '<input type="hidden" name="db_collation" value="' . $db_collation
287 .'" />' . "\n";
290 <input type="hidden" name="what" value="data" />
291 <input type="hidden" name="db_rename" value="true" />
292 <?php echo PMA_generate_common_hidden_inputs($db); ?>
293 <fieldset>
294 <legend>
295 <?php
296 if ($cfg['PropertiesIconic']) {
297 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
298 .' alt="" width="16" height="16" />';
300 echo $strDBRename . ':';
302 </legend>
303 <input type="text" name="newname" size="30" class="textfield" value="" />
304 <?php
305 echo '(' . $strCommand . ': ';
307 * @todo (see explanations above in a previous todo)
309 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
310 // echo 'RENAME DATABASE';
311 //} else {
312 echo 'INSERT INTO ... SELECT';
314 echo ')'; ?>
315 <input type="submit" value="<?php echo $strGo; ?>" onclick="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
316 </fieldset>
317 </form>
319 <?php
321 * Copy database
324 <form method="post" action="db_operations.php"
325 onsubmit="return emptyFormElements(this, 'newname')">
326 <?php
327 if (isset($db_collation)) {
328 echo '<input type="hidden" name="db_collation" value="' . $db_collation
329 .'" />' . "\n";
331 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
332 echo PMA_generate_common_hidden_inputs($db);
334 <fieldset>
335 <legend>
336 <?php
337 if ($cfg['PropertiesIconic']) {
338 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
339 .' alt="" width="16" height="16" />';
341 echo $strDBCopy . ':';
342 if (PMA_MYSQL_INT_VERSION >= 50000) {
343 $drop_clause = 'DROP TABLE / DROP VIEW';
344 } else {
345 $drop_clause = 'DROP TABLE';
348 </legend>
349 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
350 <input type="radio" name="what" value="structure"
351 id="radio_copy_structure" style="vertical-align: middle" />
352 <label for="radio_copy_structure"><?php echo $strStrucOnly; ?></label><br />
353 <input type="radio" name="what" value="data" id="radio_copy_data"
354 checked="checked" style="vertical-align: middle" />
355 <label for="radio_copy_data"><?php echo $strStrucData; ?></label><br />
356 <input type="radio" name="what" value="dataonly"
357 id="radio_copy_dataonly" style="vertical-align: middle" />
358 <label for="radio_copy_dataonly"><?php echo $strDataOnly; ?></label><br />
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 if (PMA_MYSQL_INT_VERSION >= 40101) {
400 // MySQL supports setting default charsets / collations for databases since
401 // version 4.1.1.
402 echo '<form method="post" action="./db_operations.php">' . "\n"
403 . PMA_generate_common_hidden_inputs($db, $table)
404 . '<fieldset>' . "\n"
405 . ' <legend>';
406 if ($cfg['PropertiesIconic']) {
407 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
408 .' alt="" width="16" height="16" />';
410 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
411 . ' </legend>' . "\n"
412 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
413 'db_collation', 'select_db_collation', $db_collation, false, 3)
414 . ' <input type="submit" name="submitcollation"'
415 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
416 . '</fieldset>' . "\n"
417 . '</form>' . "\n";
420 if ($num_tables > 0
421 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
422 /* Show error if user has configured something, notice elsewhere */
423 if (!empty($cfg['Servers'][$server]['pmadb'])) {
424 echo '<div class="error"><h1>' . $strError . '</h1>';
425 } else {
426 echo '<div class="notice">';
428 printf($strRelationNotWorking, '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', '</a>');
429 echo '</div>';
430 } // end if
431 } // end if (!$is_information_schema)
434 // not sure about displaying the PDF dialog in case db is information_schema
435 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
436 <!-- Work on PDF Pages -->
438 <?php
439 // We only show this if we find something in the new pdf_pages table
441 $test_query = '
442 SELECT *
443 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
444 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
445 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
447 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
448 <!-- PDF schema -->
449 <form method="post" action="pdf_schema.php">
450 <fieldset>
451 <legend>
452 <?php
453 echo PMA_generate_common_hidden_inputs($db);
454 if ($cfg['PropertiesIconic']) {
455 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
456 .' alt="" width="16" height="16" />';
458 echo $strDisplayPDF;
460 </legend>
461 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
462 <select name="pdf_page_number" id="pdf_page_number_opt">
463 <?php
464 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
465 echo ' <option value="' . $pages['page_nr'] . '">'
466 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
467 } // end while
468 PMA_DBI_free_result($test_rs);
469 unset($test_rs);
471 </select><br />
473 <input type="checkbox" name="show_grid" id="show_grid_opt" />
474 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
475 <input type="checkbox" name="show_color" id="show_color_opt"
476 checked="checked" />
477 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
478 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
479 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
480 </label><br />
481 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
482 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
483 </label><br />
484 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
485 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
487 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
488 <select name="orientation" id="orientation_opt">
489 <option value="L"><?php echo $strLandscape;?></option>
490 <option value="P"><?php echo $strPortrait;?></option>
491 </select><br />
493 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
494 <select name="paper" id="paper_opt">
495 <?php
496 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
497 echo '<option value="' . $val . '"';
498 if ($val == $cfg['PDFDefaultPageSize']) {
499 echo ' selected="selected"';
501 echo ' >' . $val . '</option>' . "\n";
504 </select>
505 </fieldset>
506 <fieldset class="tblFooters">
507 <input type="submit" value="<?php echo $strGo; ?>" />
508 </fieldset>
509 </form>
510 <?php
511 } // end if
512 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
513 if ($cfg['PropertiesIconic']) {
514 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
515 .' alt="" width="16" height="16" />';
517 echo $strEditPDFPages . '</a>';
518 } // end if
521 * Displays the footer
523 require_once './libraries/footer.inc.php';