Rename left.php to navigation.php and modify all calls to that. Only config variables...
[phpmyadmin/last10db.git] / db_operations.php
blob2f690df9c84045397c328bd04b2df60a6a0ca054
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * handles miscellaneous db operations:
7 * - move/rename
8 * - copy
9 * - changing collation
10 * - changing comment
11 * - adding tables
12 * - viewing PDF schemas
15 /**
16 * requirements
18 require_once './libraries/common.lib.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 (isset($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 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
42 if (isset($db_collation)) {
43 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
45 $local_query .= ';';
46 $sql_query = $local_query;
47 PMA_DBI_query($local_query);
50 if (isset($GLOBALS['add_constraints'])) {
51 $GLOBALS['sql_constraints_query_full_db'] = '';
54 $tables_full = PMA_DBI_get_tables_full($db);
55 foreach ($tables_full as $table => $tmp) {
56 $back = $sql_query;
57 $sql_query = '';
59 // value of $what for this table only
60 $this_what = $what;
62 if (!isset($tables_full[$table]['Engine'])) {
63 $tables_full[$table]['Engine'] = $tables_full[$table]['Type'];
65 // do not copy the data from a Merge table
66 // note: on the calling FORM, 'data' means 'structure and data'
67 if ($tables_full[$table]['Engine'] == 'MRG_MyISAM') {
68 if ($this_what == 'data') {
69 $this_what = 'structure';
71 if ($this_what == 'dataonly') {
72 $this_what = 'nocopy';
76 if ($this_what != 'nocopy') {
77 PMA_Table::moveCopy($db, $table, $newname, $table,
78 isset($this_what) ? $this_what : 'data', $move, 'db_copy');
79 if (isset($GLOBALS['add_constraints'])) {
80 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
81 unset($GLOBALS['sql_constraints_query']);
85 $sql_query = $back . $sql_query;
87 unset($table);
89 // now that all tables exist, create all the accumulated constraints
90 if (isset($GLOBALS['add_constraints'])) {
91 /**
92 * @todo this works with mysqli but not with mysql, because
93 * mysql extension does not accept more than one statement; maybe
94 * interface with the sql import plugin that handles statement delimiter
96 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
98 // and prepare to display them
99 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
100 unset($GLOBALS['sql_constraints_query_full_db']);
103 // Duplicate the bookmarks for this db (done once for each db)
104 if ($db != $newname) {
105 $get_fields = array('user', 'label', 'query');
106 $where_fields = array('dbase' => $db);
107 $new_fields = array('dbase' => $newname);
108 PMA_Table::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
109 $where_fields, $new_fields);
112 if ($move) {
113 // cleanup pmadb stuff for this db
114 require_once './libraries/relation_cleanup.lib.php';
115 PMA_relationsCleanupDatabase($db);
117 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
118 $sql_query .= "\n" . $local_query;
119 PMA_DBI_query($local_query);
120 $message = sprintf($strRenameDatabaseOK, htmlspecialchars($db),
121 htmlspecialchars($newname));
122 } else {
123 $message = sprintf($strCopyDatabaseOK, htmlspecialchars($db),
124 htmlspecialchars($newname));
126 $reload = true;
128 /* Change database to be used */
129 if ($move) {
130 $db = $newname;
131 } else {
132 if (isset($switch_to_new) && $switch_to_new == 'true') {
133 PMA_setCookie('pma_switch_to_new', 'true');
134 $db = $newname;
135 } else {
136 PMA_setCookie('pma_switch_to_new', '');
142 * Settings for relations stuff
145 require_once './libraries/relation.lib.php';
146 $cfgRelation = PMA_getRelationsParam();
149 * Check if comments were updated
150 * (must be done before displaying the menu tabs)
152 if ($cfgRelation['commwork'] && isset($db_comment) && $db_comment == 'true') {
153 PMA_SetComment($db, '', '(db_comment)', $comment);
157 * Prepares the tables list if the user where not redirected to this script
158 * because there is no table in the database ($is_info is true)
160 if (empty($is_info)) {
161 require './libraries/db_details_common.inc.php';
162 $url_query .= '&amp;goto=db_operations.php';
164 // Gets the database structure
165 $sub_part = '_structure';
166 require './libraries/db_details_db_info.inc.php';
167 echo "\n";
170 if (PMA_MYSQL_INT_VERSION >= 40101) {
171 $db_collation = PMA_getDbCollation($db);
173 if (PMA_MYSQL_INT_VERSION < 50002
174 || (PMA_MYSQL_INT_VERSION >= 50002 && $db != 'information_schema')) {
175 $is_information_schema = false;
176 } else {
177 $is_information_schema = true;
180 if (!$is_information_schema) {
182 require './libraries/display_create_table.lib.php';
184 if ($cfgRelation['commwork']) {
186 * database comment
189 <form method="post" action="db_operations.php">
190 <?php echo PMA_generate_common_hidden_inputs($db); ?>
191 <input type="hidden" name="db_comment" value="true" />
192 <fieldset>
193 <legend>
194 <?php
195 if ($cfg['PropertiesIconic']) {
196 echo '<img class="icon" src="' . $pmaThemeImage . 'b_comment.png"'
197 .' alt="" border="0" width="16" height="16" hspace="2" align="middle" />';
199 echo $strDBComment;
200 $comment = PMA_getComments($db);
202 </legend>
203 <input type="text" name="comment" class="textfield" size="30"
204 value="<?php
205 echo (isset($comment) && is_array($comment)
206 ? htmlspecialchars(implode(' ', $comment))
207 : ''); ?>" />
208 <input type="submit" value="<?php echo $strGo; ?>" />
209 </fieldset>
210 </form>
211 <?php
214 * rename database
217 <form method="post" action="db_operations.php"
218 onsubmit="return emptyFormElements(this, 'newname')">
219 <input type="hidden" name="what" value="data" />
220 <input type="hidden" name="db_rename" value="true" />
221 <?php echo PMA_generate_common_hidden_inputs($db); ?>
222 <fieldset>
223 <legend>
224 <?php
225 if ($cfg['PropertiesIconic']) {
226 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
227 .' alt="" width="16" height="16" />';
229 echo $strDBRename . ':';
231 </legend>
232 <input type="text" name="newname" size="30" class="textfield" value="" />
233 <input type="submit" value="<?php echo $strGo; ?>" />
234 </fieldset>
235 </form>
237 <?php
239 * Copy database
242 <form method="post" action="db_operations.php"
243 onsubmit="return emptyFormElements(this, 'newname')">
244 <?php
245 if (isset($db_collation)) {
246 echo '<input type="hidden" name="db_collation" value="' . $db_collation
247 .'" />' . "\n";
249 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
250 echo PMA_generate_common_hidden_inputs($db);
252 <fieldset>
253 <legend>
254 <?php
255 if ($cfg['PropertiesIconic']) {
256 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
257 .' alt="" width="16" height="16" />';
259 echo $strDBCopy . ':';
260 if (PMA_MYSQL_INT_VERSION >= 50000) {
261 $drop_clause = 'DROP TABLE / DROP VIEW';
262 } else {
263 $drop_clause = 'DROP TABLE';
266 </legend>
267 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
268 <input type="radio" name="what" value="structure"
269 id="radio_copy_structure" style="vertical-align: middle" />
270 <label for="radio_copy_structure"><?php echo $strStrucOnly; ?></label><br />
271 <input type="radio" name="what" value="data" id="radio_copy_data"
272 checked="checked" style="vertical-align: middle" />
273 <label for="radio_copy_data"><?php echo $strStrucData; ?></label><br />
274 <input type="radio" name="what" value="dataonly"
275 id="radio_copy_dataonly" style="vertical-align: middle" />
276 <label for="radio_copy_dataonly"><?php echo $strDataOnly; ?></label><br />
278 <input type="checkbox" name="create_database_before_copying" value="1"
279 id="checkbox_create_database_before_copying"
280 style="vertical-align: middle" checked="checked" />
281 <label for="checkbox_create_database_before_copying">
282 <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
283 <input type="checkbox" name="drop_if_exists" value="true"
284 id="checkbox_drop" style="vertical-align: middle" />
285 <label for="checkbox_drop"><?php echo sprintf($strAddClause, $drop_clause); ?></label><br />
286 <input type="checkbox" name="sql_auto_increment" value="1"
287 id="checkbox_auto_increment" style="vertical-align: middle" />
288 <label for="checkbox_auto_increment">
289 <?php echo $strAddAutoIncrement; ?></label><br />
290 <input type="checkbox" name="add_constraints" value="1"
291 id="checkbox_constraints" style="vertical-align: middle" />
292 <label for="checkbox_constraints">
293 <?php echo $strAddConstraints; ?></label><br />
294 <?php
295 unset($drop_clause);
297 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
298 && $_COOKIE['pma_switch_to_new'] == 'true') {
299 $pma_switch_to_new = 'true';
302 <input type="checkbox" name="switch_to_new" value="true"
303 id="checkbox_switch"
304 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
305 style="vertical-align: middle" />
306 <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
307 </fieldset>
308 <fieldset class="tblFooters">
309 <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
310 </fieldset>
311 </form>
313 <?php
315 * Change database charset
317 if (PMA_MYSQL_INT_VERSION >= 40101) {
318 // MySQL supports setting default charsets / collations for databases since
319 // version 4.1.1.
320 echo '<form method="post" action="./db_operations.php">' . "\n"
321 . PMA_generate_common_hidden_inputs($db, isset($table) ? $table : '')
322 . '<fieldset>' . "\n"
323 . ' <legend>';
324 if ($cfg['PropertiesIconic']) {
325 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
326 .' alt="" width="16" height="16" />';
328 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
329 . ' </legend>' . "\n"
330 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
331 'db_collation', 'select_db_collation', $db_collation, false, 3)
332 . ' <input type="submit" name="submitcollation"'
333 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
334 . '</fieldset>' . "\n"
335 . '</form>' . "\n";
338 if ($num_tables > 0
339 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
340 echo '<div class="error"><h1>' . $strError . '</h1>'
341 . sprintf($strRelationNotWorking,
342 '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">',
343 '</a>')
344 . '</div>';
345 } // end if
346 } // end if (!$is_information_schema)
349 // not sure about leaving the PDF dialog for information_schema
350 if ($num_tables > 0 && isset($table)) {
351 $takeaway = $url_query . '&amp;table=' . urlencode($table);
352 } else {
353 $takeaway = '';
356 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
357 <!-- Work on PDF Pages -->
359 <?php
360 // We only show this if we find something in the new pdf_pages table
362 $test_query = '
363 SELECT *
364 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
365 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
366 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
368 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
369 <!-- PDF schema -->
370 <form method="post" action="pdf_schema.php">
371 <fieldset>
372 <legend>
373 <?php
374 echo PMA_generate_common_hidden_inputs($db);
375 if ($cfg['PropertiesIconic']) {
376 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
377 .' alt="" width="16" height="16" />';
379 echo $strDisplayPDF;
381 </legend>
382 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
383 <select name="pdf_page_number" id="pdf_page_number_opt">
384 <?php
385 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
386 echo ' <option value="' . $pages['page_nr'] . '">'
387 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
388 } // end while
389 PMA_DBI_free_result($test_rs);
390 unset($test_rs);
392 </select><br />
394 <input type="checkbox" name="show_grid" id="show_grid_opt" />
395 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
396 <input type="checkbox" name="show_color" id="show_color_opt"
397 checked="checked" />
398 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
399 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
400 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
401 </label><br />
402 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
403 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
404 </label><br />
405 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
406 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
408 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
409 <select name="orientation" id="orientation_opt">
410 <option value="L"><?php echo $strLandscape;?></option>
411 <option value="P"><?php echo $strPortrait;?></option>
412 </select><br />
414 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
415 <select name="paper" id="paper_opt">
416 <?php
417 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
418 echo '<option value="' . $val . '"';
419 if ($val == $cfg['PDFDefaultPageSize']) {
420 echo ' selected="selected"';
422 echo ' >' . $val . '</option>' . "\n";
425 </select>
426 </fieldset>
427 <fieldset class="tblFooters">
428 <input type="submit" value="<?php echo $strGo; ?>" />
429 </fieldset>
430 </form>
431 <?php
432 } // end if
434 <ul>
435 <li>
436 <?php
437 echo '<a href="pdf_pages.php?' . $takeaway . '">';
438 if ($cfg['PropertiesIconic']) {
439 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
440 .' alt="" width="16" height="16" />';
442 echo $strEditPDFPages . '</a>';
444 </li>
445 </ul>
446 <?php
447 } // end if
449 if ($num_tables > 0
450 && $cfgRelation['relwork'] && $cfgRelation['commwork']
451 && isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir'])) {
453 * import docSQL files
455 echo '<ul>' . "\n"
456 .'<li><a href="db_details_importdocsql.php?' . $takeaway . '">' . "\n";
457 if ($cfg['PropertiesIconic']) {
458 echo '<img class="icon" src="' . $pmaThemeImage . 'b_docsql.png"'
459 .' alt="" width="16" height="16" />';
461 echo $strImportDocSQL . '</a></li>' . "\n"
462 .'</ul>';
466 * Displays the footer
468 require_once './libraries/footer.inc.php';