2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
10 * - viewing PDF schemas
18 require_once './libraries/common.inc.php';
19 require_once './libraries/Table.class.php';
20 require_once './libraries/mysql_charsets.lib.php';
23 * Rename/move or copy database
25 if (strlen($db) && (! empty($db_rename) ||
! empty($db_copy))) {
27 if (! empty($db_rename)) {
33 if (!isset($newname) ||
!strlen($newname)) {
34 $message = PMA_Message
::error('strDatabaseEmpty');
36 $sql_query = ''; // in case target db exists
39 (isset($create_database_before_copying) && $create_database_before_copying)) {
41 * @todo activate this with the correct version of MySQL
42 * when they fix the problem when the db contains a VIEW
43 * (problem exists in 5.1.20)
44 * also, in 6.0.0 when the db contains a Falcon table,
45 * renaming it results in a unusable db!
47 //if (PMA_MYSQL_INT_VERSION >= 50107) {
48 // $local_query = 'RENAME DATABASE ' . PMA_backquote($db) . ' TO ' . PMA_backquote($newname) . ';';
49 // $sql_query = $local_query;
50 // PMA_DBI_query($local_query);
54 // lower_case_table_names=1 `DB` becomes `db`
55 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
56 if ($lower_case_table_names === '1') {
57 $newname = strtolower($newname);
60 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
61 if (isset($db_collation)) {
62 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
65 $sql_query = $local_query;
66 PMA_DBI_query($local_query);
68 // rebuild the database list because PMA_Table::moveCopy
69 // checks in this list if the target db exists
70 $GLOBALS['PMA_List_Database']->build();
73 if (isset($GLOBALS['add_constraints'])) {
74 $GLOBALS['sql_constraints_query_full_db'] = '';
77 $tables_full = PMA_DBI_get_tables_full($db);
79 foreach ($tables_full as $each_table => $tmp) {
80 // to be able to rename a db containing views, we
81 // first collect in $views all the views we find and we
82 // will handle them after the tables
84 * @todo support a view of a view
86 if (PMA_Table
::isView($db, $each_table)) {
87 $views[] = $each_table;
94 // value of $what for this table only
97 // do not copy the data from a Merge table
98 // note: on the calling FORM, 'data' means 'structure and data'
99 if ($tables_full[$each_table]['Engine'] == 'MRG_MyISAM') {
100 if ($this_what == 'data') {
101 $this_what = 'structure';
103 if ($this_what == 'dataonly') {
104 $this_what = 'nocopy';
108 if ($this_what != 'nocopy') {
109 if (! PMA_Table
::moveCopy($db, $each_table, $newname, $each_table,
110 isset($this_what) ?
$this_what : 'data', $move, 'db_copy'))
113 // $sql_query is filled by PMA_Table::moveCopy()
114 $sql_query = $back . $sql_query;
117 if (isset($GLOBALS['add_constraints'])) {
118 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
119 unset($GLOBALS['sql_constraints_query']);
122 // $sql_query is filled by PMA_Table::moveCopy()
123 $sql_query = $back . $sql_query;
129 foreach ($views as $view) {
130 if (! PMA_Table
::moveCopy($db, $view, $newname, $view,
131 'structure', $move, 'db_copy')) {
137 unset($view, $views);
139 // now that all tables exist, create all the accumulated constraints
140 if (! $_error && isset($GLOBALS['add_constraints'])) {
142 * @todo this works with mysqli but not with mysql, because
143 * mysql extension does not accept more than one statement; maybe
144 * interface with the sql import plugin that handles statement delimiter
146 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
148 // and prepare to display them
149 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
150 unset($GLOBALS['sql_constraints_query_full_db']);
152 // see the previous todo
153 // } // end else MySQL < 50107
155 // Duplicate the bookmarks for this db (done once for each db)
156 if (! $_error && $db != $newname) {
157 $get_fields = array('user', 'label', 'query');
158 $where_fields = array('dbase' => $db);
159 $new_fields = array('dbase' => $newname);
160 PMA_Table
::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
161 $where_fields, $new_fields);
164 if (! $_error && $move) {
165 // cleanup pmadb stuff for this db
166 require_once './libraries/relation_cleanup.lib.php';
167 PMA_relationsCleanupDatabase($db);
169 if (PMA_MYSQL_INT_VERSION
< 50107) {
170 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
171 $sql_query .= "\n" . $local_query;
172 PMA_DBI_query($local_query);
174 $message = PMA_Message
::success('strRenameDatabaseOK');
175 $message->addParam($db);
176 $message->addParam($newname);
177 } elseif (! $_error) {
178 $message = PMA_Message
::success('strCopyDatabaseOK');
179 $message->addParam($db);
180 $message->addParam($newname);
184 /* Change database to be used */
185 if (! $_error && $move) {
187 } elseif (! $_error) {
188 if (isset($switch_to_new) && $switch_to_new == 'true') {
189 PMA_setCookie('pma_switch_to_new', 'true');
192 PMA_setCookie('pma_switch_to_new', '');
196 if ($_error && ! isset($message)) {
197 $message = PMA_Message
::error();
202 * Settings for relations stuff
205 require_once './libraries/relation.lib.php';
206 $cfgRelation = PMA_getRelationsParam();
209 * Check if comments were updated
210 * (must be done before displaying the menu tabs)
212 if (isset($_REQUEST['comment'])) {
213 PMA_setDbComment($db, $comment);
217 * Prepares the tables list if the user where not redirected to this script
218 * because there is no table in the database ($is_info is true)
220 if (empty($is_info)) {
221 require './libraries/db_common.inc.php';
222 $url_query .= '&goto=db_operations.php';
224 // Gets the database structure
225 $sub_part = '_structure';
226 require './libraries/db_info.inc.php';
229 if (isset($message)) {
230 PMA_showMessage($message, $sql_query);
235 $db_collation = PMA_getDbCollation($db);
236 if ($db == 'information_schema') {
237 $is_information_schema = true;
239 $is_information_schema = false;
242 if (!$is_information_schema) {
244 require './libraries/display_create_table.lib.php';
246 if ($cfgRelation['commwork']) {
251 <form method
="post" action
="db_operations.php">
252 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
255 <?php
echo PMA_getIcon('b_comment.png', $strDBComment, false, true); ?
>
257 <input type
="text" name
="comment" class="textfield" size
="30"
259 echo htmlspecialchars(PMA_getDbComment($db)); ?>" />
260 <input type
="submit" value
="<?php echo $strGo; ?>" />
269 <form method
="post" action
="db_operations.php"
270 onsubmit
="return emptyFormElements(this, 'newname')">
272 if (isset($db_collation)) {
273 echo '<input type="hidden" name="db_collation" value="' . $db_collation
277 <input type
="hidden" name
="what" value
="data" />
278 <input type
="hidden" name
="db_rename" value
="true" />
279 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
283 if ($cfg['PropertiesIconic']) {
284 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
285 .' alt="" width="16" height="16" />';
287 echo $strDBRename . ':';
290 <input type
="text" name
="newname" size
="30" class="textfield" value
="" />
292 echo '(' . $strCommand . ': ';
294 * @todo (see explanations above in a previous todo)
296 //if (PMA_MYSQL_INT_VERSION >= 50107) {
297 // echo 'RENAME DATABASE';
299 echo 'INSERT INTO ... SELECT';
302 <input type
="submit" value
="<?php echo $strGo; ?>" onclick
="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
311 <form method
="post" action
="db_operations.php"
312 onsubmit
="return emptyFormElements(this, 'newname')">
314 if (isset($db_collation)) {
315 echo '<input type="hidden" name="db_collation" value="' . $db_collation
318 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
319 echo PMA_generate_common_hidden_inputs($db);
324 if ($cfg['PropertiesIconic']) {
325 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
326 .' alt="" width="16" height="16" />';
328 echo $strDBCopy . ':';
329 $drop_clause = 'DROP TABLE / DROP VIEW';
332 <input type
="text" name
="newname" size
="30" class="textfield" value
="" /><br
/>
335 'structure' => $strStrucOnly,
336 'data' => $strStrucData,
337 'dataonly' => $strDataOnly);
338 PMA_generate_html_radio('what', $choices, 'data', true);
341 <input type
="checkbox" name
="create_database_before_copying" value
="1"
342 id
="checkbox_create_database_before_copying"
343 style
="vertical-align: middle" checked
="checked" />
344 <label
for="checkbox_create_database_before_copying">
345 <?php
echo $strCreateDatabaseBeforeCopying; ?
></label
><br
/>
346 <input type
="checkbox" name
="drop_if_exists" value
="true"
347 id
="checkbox_drop" style
="vertical-align: middle" />
348 <label
for="checkbox_drop"><?php
echo sprintf($strAddClause, $drop_clause); ?
></label
><br
/>
349 <input type
="checkbox" name
="sql_auto_increment" value
="1"
350 id
="checkbox_auto_increment" style
="vertical-align: middle" />
351 <label
for="checkbox_auto_increment">
352 <?php
echo $strAddAutoIncrement; ?
></label
><br
/>
353 <input type
="checkbox" name
="add_constraints" value
="1"
354 id
="checkbox_constraints" style
="vertical-align: middle" />
355 <label
for="checkbox_constraints">
356 <?php
echo $strAddConstraints; ?
></label
><br
/>
360 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
361 && $_COOKIE['pma_switch_to_new'] == 'true') {
362 $pma_switch_to_new = 'true';
365 <input type
="checkbox" name
="switch_to_new" value
="true"
367 <?php
echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ?
' checked="checked"' : ''); ?
>
368 style
="vertical-align: middle" />
369 <label
for="checkbox_switch"><?php
echo $strSwitchToDatabase; ?
></label
>
371 <fieldset
class="tblFooters">
372 <input type
="submit" name
="submit_copy" value
="<?php echo $strGo; ?>" />
378 * Change database charset
380 echo '<form method="post" action="./db_operations.php">' . "\n"
381 . PMA_generate_common_hidden_inputs($db, $table)
382 . '<fieldset>' . "\n"
384 if ($cfg['PropertiesIconic']) {
385 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
386 .' alt="" width="16" height="16" />';
388 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
389 . ' </legend>' . "\n"
390 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION
,
391 'db_collation', 'select_db_collation', $db_collation, false, 3)
392 . ' <input type="submit" name="submitcollation"'
393 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
394 . '</fieldset>' . "\n"
398 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
399 $message = PMA_Message
::notice('strRelationNotWorking');
400 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
401 $message->addParam('</a>', false);
402 /* Show error if user has configured something, notice elsewhere */
403 if (!empty($cfg['Servers'][$server]['pmadb'])) {
404 $message->isError(true);
408 } // end if (!$is_information_schema)
411 // not sure about displaying the PDF dialog in case db is information_schema
412 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?
>
413 <!-- Work on PDF Pages
-->
416 // We only show this if we find something in the new pdf_pages table
420 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
421 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
422 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE
);
424 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?
>
426 <form method
="post" action
="pdf_schema.php">
430 echo PMA_generate_common_hidden_inputs($db);
431 if ($cfg['PropertiesIconic']) {
432 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
433 .' alt="" width="16" height="16" />';
438 <label
for="pdf_page_number_opt"><?php
echo $strPageNumber; ?
></label
>
439 <select name
="pdf_page_number" id
="pdf_page_number_opt">
441 while ($pages = @PMA_DBI_fetch_assoc
($test_rs)) {
442 echo ' <option value="' . $pages['page_nr'] . '">'
443 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
445 PMA_DBI_free_result($test_rs);
450 <input type
="checkbox" name
="show_grid" id
="show_grid_opt" />
451 <label
for="show_grid_opt"><?php
echo $strShowGrid; ?
></label
><br
/>
452 <input type
="checkbox" name
="show_color" id
="show_color_opt"
454 <label
for="show_color_opt"><?php
echo $strShowColor; ?
></label
><br
/>
455 <input type
="checkbox" name
="show_table_dimension" id
="show_table_dim_opt" />
456 <label
for="show_table_dim_opt"><?php
echo $strShowTableDimension; ?
>
458 <input type
="checkbox" name
="all_tab_same_wide" id
="all_tab_same_wide" />
459 <label
for="all_tab_same_wide"><?php
echo $strAllTableSameWidth; ?
>
461 <input type
="checkbox" name
="with_doc" id
="with_doc" checked
="checked" />
462 <label
for="with_doc"><?php
echo $strDataDict; ?
></label
><br
/>
464 <label
for="orientation_opt"><?php
echo $strShowDatadictAs; ?
></label
>
465 <select name
="orientation" id
="orientation_opt">
466 <option value
="L"><?php
echo $strLandscape;?
></option
>
467 <option value
="P"><?php
echo $strPortrait;?
></option
>
470 <label
for="paper_opt"><?php
echo $strPaperSize; ?
></label
>
471 <select name
="paper" id
="paper_opt">
473 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
474 echo '<option value="' . $val . '"';
475 if ($val == $cfg['PDFDefaultPageSize']) {
476 echo ' selected="selected"';
478 echo ' >' . $val . '</option>' . "\n";
483 <fieldset
class="tblFooters">
484 <input type
="submit" value
="<?php echo $strGo; ?>" />
489 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
490 if ($cfg['PropertiesIconic']) {
491 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
492 .' alt="" width="16" height="16" />';
494 echo $strEditPDFPages . '</a>';
498 * Displays the footer
500 require_once './libraries/footer.inc.php';