2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
10 * - viewing PDF schemas
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";
27 * Rename/move or copy database
29 if (strlen($db) && (! empty($db_rename) ||
! empty($db_copy))) {
31 if (! empty($db_rename)) {
37 if (!isset($newname) ||
!strlen($newname)) {
38 $message = PMA_Message
::error('strDatabaseEmpty');
40 $sql_query = ''; // in case target db exists
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);
55 $sql_query = $local_query;
56 PMA_DBI_query($local_query);
58 // rebuild the database list because PMA_Table::moveCopy
59 // checks in this list if the target db exists
60 $GLOBALS['pma']->databases
->build();
63 if (isset($GLOBALS['add_constraints']) ||
$move) {
64 $GLOBALS['sql_constraints_query_full_db'] = array();
67 $tables_full = PMA_DBI_get_tables_full($db);
70 // remove all foreign key constraints, otherwise we can get errors
71 require_once './libraries/export/sql.php';
72 foreach ($tables_full as $each_table => $tmp) {
73 $sql_constraints = '';
74 $sql_drop_foreign_keys = '';
75 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
76 if (! empty($sql_drop_foreign_keys)) {
77 PMA_DBI_query($sql_drop_foreign_keys);
79 // keep the constraint we just dropped
80 if (! empty($sql_constraints)) {
81 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
84 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
87 foreach ($tables_full as $each_table => $tmp) {
88 // to be able to rename a db containing views, we
89 // first collect in $views all the views we find and we
90 // will handle them after the tables
92 * @todo support a view of a view
94 if (PMA_Table
::isView($db, $each_table)) {
95 $views[] = $each_table;
102 // value of $what for this table only
105 // do not copy the data from a Merge table
106 // note: on the calling FORM, 'data' means 'structure and data'
107 if ($tables_full[$each_table]['Engine'] == 'MRG_MyISAM') {
108 if ($this_what == 'data') {
109 $this_what = 'structure';
111 if ($this_what == 'dataonly') {
112 $this_what = 'nocopy';
116 if ($this_what != 'nocopy') {
117 // keep the triggers from the original db+table
118 // (third param is empty because delimiters are only intended
119 // for importing via the mysql client or our Import feature)
120 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
122 if (! PMA_Table
::moveCopy($db, $each_table, $newname, $each_table,
123 isset($this_what) ?
$this_what : 'data', $move, 'db_copy'))
126 // $sql_query is filled by PMA_Table::moveCopy()
127 $sql_query = $back . $sql_query;
130 // apply the triggers to the destination db+table
132 PMA_DBI_select_db($newname);
133 foreach ($triggers as $trigger) {
134 PMA_DBI_query($trigger['create']);
140 // this does not apply to a rename operation
141 if (isset($GLOBALS['add_constraints'])) {
142 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
143 unset($GLOBALS['sql_constraints_query']);
146 // $sql_query is filled by PMA_Table::moveCopy()
147 $sql_query = $back . $sql_query;
153 foreach ($views as $view) {
154 if (! PMA_Table
::moveCopy($db, $view, $newname, $view,
155 'structure', $move, 'db_copy')) {
161 unset($view, $views);
163 // now that all tables exist, create all the accumulated constraints
164 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
165 PMA_DBI_select_db($newname);
166 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
167 PMA_DBI_query($one_query);
168 // and prepare to display them
169 $GLOBALS['sql_query'] .= "\n" . $one_query;
172 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
175 if (PMA_MYSQL_INT_VERSION
>= 50000) {
176 // here I don't use DELIMITER because it's not part of the
177 // language; I have to send each statement one by one
179 // to avoid selecting alternatively the current and new db
180 // we would need to modify the CREATE definitions to qualify
182 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
183 if ($procedure_names) {
184 foreach($procedure_names as $procedure_name) {
185 PMA_DBI_select_db($db);
186 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
187 // collect for later display
188 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
189 PMA_DBI_select_db($newname);
190 PMA_DBI_query($tmp_query);
194 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
195 if ($function_names) {
196 foreach($function_names as $function_name) {
197 PMA_DBI_select_db($db);
198 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
199 // collect for later display
200 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
201 PMA_DBI_select_db($newname);
202 PMA_DBI_query($tmp_query);
206 // go back to current db, just in case
207 PMA_DBI_select_db($db);
209 // Duplicate the bookmarks for this db (done once for each db)
210 if (! $_error && $db != $newname) {
211 $get_fields = array('user', 'label', 'query');
212 $where_fields = array('dbase' => $db);
213 $new_fields = array('dbase' => $newname);
214 PMA_Table
::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
215 $where_fields, $new_fields);
218 if (! $_error && $move) {
220 * cleanup pmadb stuff for this db
222 require_once './libraries/relation_cleanup.lib.php';
223 PMA_relationsCleanupDatabase($db);
225 // if someday the RENAME DATABASE reappears, do not DROP
226 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
227 $sql_query .= "\n" . $local_query;
228 PMA_DBI_query($local_query);
230 $message = PMA_Message
::success('strRenameDatabaseOK');
231 $message->addParam($db);
232 $message->addParam($newname);
233 } elseif (! $_error) {
234 $message = PMA_Message
::success('strCopyDatabaseOK');
235 $message->addParam($db);
236 $message->addParam($newname);
240 /* Change database to be used */
241 if (! $_error && $move) {
243 } elseif (! $_error) {
244 if (isset($switch_to_new) && $switch_to_new == 'true') {
245 PMA_setCookie('pma_switch_to_new', 'true');
248 PMA_setCookie('pma_switch_to_new', '');
252 if ($_error && ! isset($message)) {
253 $message = PMA_Message
::error();
259 * Enable/Disable/Repair BLOB Repository Monitoring for current database
261 if (strlen($db) > 0 && !empty($db_blob_streaming_op))
264 $PMA_Config = $_SESSION['PMA_Config'];
266 if (!empty($PMA_Config))
268 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
270 // if Blobstreaming plugins exist, begin checking for Blobstreaming tables
271 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
273 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
274 $bs_tables = $bs_tables[$db];
276 $oneBSTableExists = FALSE;
278 // check if at least one blobstreaming table exists
279 foreach ($bs_tables as $table_key=>$tbl)
280 if ($bs_tables[$table_key]['Exists'])
282 $oneBSTableExists = TRUE;
286 switch ($db_blob_streaming_op)
288 // enable BLOB repository monitoring
290 // if blobstreaming tables do not exist, create them
291 if (!$oneBSTableExists)
292 PMA_BS_CreateTables($db);
294 // disable BLOB repository monitoring
296 // if at least one blobstreaming table exists, execute drop
297 if ($oneBSTableExists)
298 PMA_BS_DropTables($db);
300 // repair BLOB repository
302 // check if a blobstreaming table is missing
303 foreach ($bs_tables as $table_key=>$tbl)
304 if (!$bs_tables[$table_key]['Exists'])
306 PMA_DBI_select_db($db);
307 PMA_DBI_query(PMA_BS_GetTableStruct($table_key));
312 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');
313 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
314 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
319 * Settings for relations stuff
322 require_once './libraries/relation.lib.php';
323 $cfgRelation = PMA_getRelationsParam();
326 * Check if comments were updated
327 * (must be done before displaying the menu tabs)
329 if (isset($_REQUEST['comment'])) {
330 PMA_setDbComment($db, $comment);
334 * Prepares the tables list if the user where not redirected to this script
335 * because there is no table in the database ($is_info is true)
337 if (empty($is_info)) {
338 require './libraries/db_common.inc.php';
339 $url_query .= '&goto=db_operations.php';
341 // Gets the database structure
342 $sub_part = '_structure';
343 require './libraries/db_info.inc.php';
346 if (isset($message)) {
347 PMA_showMessage($message, $sql_query);
352 $db_collation = PMA_getDbCollation($db);
353 if ($db == 'information_schema') {
354 $is_information_schema = true;
356 $is_information_schema = false;
359 if (!$is_information_schema) {
361 require './libraries/display_create_table.lib.php';
363 if ($cfgRelation['commwork']) {
368 <form method
="post" action
="db_operations.php">
369 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
372 <?php
echo PMA_getIcon('b_comment.png', $strDBComment, false, true); ?
>
374 <input type
="text" name
="comment" class="textfield" size
="30"
376 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
377 <input type
="submit" value
="<?php echo $strGo; ?>" />
386 <form method
="post" action
="db_operations.php"
387 onsubmit
="return emptyFormElements(this, 'newname')">
389 if (isset($db_collation)) {
390 echo '<input type="hidden" name="db_collation" value="' . $db_collation
394 <input type
="hidden" name
="what" value
="data" />
395 <input type
="hidden" name
="db_rename" value
="true" />
396 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
400 if ($cfg['PropertiesIconic']) {
401 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
402 .' alt="" width="16" height="16" />';
404 echo $strDBRename . ':';
407 <input type
="text" name
="newname" size
="30" class="textfield" value
="" />
409 echo '(' . $strCommand . ': ';
411 * @todo (see explanations above in a previous todo)
413 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
414 // echo 'RENAME DATABASE';
416 echo 'INSERT INTO ... SELECT';
419 <input type
="submit" value
="<?php echo $strGo; ?>" onclick
="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
428 <form method
="post" action
="db_operations.php"
429 onsubmit
="return emptyFormElements(this, 'newname')">
431 if (isset($db_collation)) {
432 echo '<input type="hidden" name="db_collation" value="' . $db_collation
435 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
436 echo PMA_generate_common_hidden_inputs($db);
441 if ($cfg['PropertiesIconic']) {
442 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
443 .' alt="" width="16" height="16" />';
445 echo $strDBCopy . ':';
446 $drop_clause = 'DROP TABLE / DROP VIEW';
449 <input type
="text" name
="newname" size
="30" class="textfield" value
="" /><br
/>
452 'structure' => $strStrucOnly,
453 'data' => $strStrucData,
454 'dataonly' => $strDataOnly);
455 PMA_display_html_radio('what', $choices, 'data', true);
458 <input type
="checkbox" name
="create_database_before_copying" value
="1"
459 id
="checkbox_create_database_before_copying"
460 style
="vertical-align: middle" checked
="checked" />
461 <label
for="checkbox_create_database_before_copying">
462 <?php
echo $strCreateDatabaseBeforeCopying; ?
></label
><br
/>
463 <input type
="checkbox" name
="drop_if_exists" value
="true"
464 id
="checkbox_drop" style
="vertical-align: middle" />
465 <label
for="checkbox_drop"><?php
echo sprintf($strAddClause, $drop_clause); ?
></label
><br
/>
466 <input type
="checkbox" name
="sql_auto_increment" value
="1" checked
="checked"
467 id
="checkbox_auto_increment" style
="vertical-align: middle" />
468 <label
for="checkbox_auto_increment">
469 <?php
echo $strAddAutoIncrement; ?
></label
><br
/>
470 <input type
="checkbox" name
="add_constraints" value
="1"
471 id
="checkbox_constraints" style
="vertical-align: middle" />
472 <label
for="checkbox_constraints">
473 <?php
echo $strAddConstraints; ?
></label
><br
/>
477 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
478 && $_COOKIE['pma_switch_to_new'] == 'true') {
479 $pma_switch_to_new = 'true';
482 <input type
="checkbox" name
="switch_to_new" value
="true"
484 <?php
echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ?
' checked="checked"' : ''); ?
>
485 style
="vertical-align: middle" />
486 <label
for="checkbox_switch"><?php
echo $strSwitchToDatabase; ?
></label
>
488 <fieldset
class="tblFooters">
489 <input type
="submit" name
="submit_copy" value
="<?php echo $strGo; ?>" />
495 * BLOB streaming support
499 $PMA_Config = $_SESSION['PMA_Config'];
501 // if all blobstreaming plugins exist, begin checking for blobstreaming tables
502 if (!empty($PMA_Config))
504 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
506 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
508 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
509 $bs_tables = $bs_tables[$db];
511 $oneBSTableExists = FALSE;
512 $allBSTablesExist = TRUE;
514 // first check that all blobstreaming tables do not exist
515 foreach ($bs_tables as $table_key=>$tbl)
516 if ($bs_tables[$table_key]['Exists'])
517 $oneBSTableExists = TRUE;
519 $allBSTablesExist = FALSE;
523 <form method
="post" action
="./db_operations.php">
524 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
527 <?php
echo PMA_getIcon('b_edit.png', $strBLOBRepository, false, true); ?
>
530 <?php
echo $strStatus; ?
>:
534 // if the blobstreaming tables exist, provide option to disable the BLOB repository
535 if ($allBSTablesExist)
538 <?php
echo $strBLOBRepositoryEnabled; ?
>
540 <fieldset
class="tblFooters">
541 <input type
="hidden" name
="db_blob_streaming_op" value
="disable" />
542 <input type
="submit" onclick
="return confirmDisableRepository('<?php echo $db; ?>');" value
="<?php echo $strBLOBRepositoryDisable; ?>" />
548 // if any of the blobstreaming tables are missing, provide option to repair the BLOB repository
549 if ($oneBSTableExists && !$allBSTablesExist)
552 <?php
echo $strBLOBRepositoryDamaged; ?
>
554 <fieldset
class="tblFooters">
555 <input type
="hidden" name
="db_blob_streaming_op" value
="repair" />
556 <input type
="submit" value
="<?php echo $strBLOBRepositoryRepair; ?>" />
560 // if none of the blobstreaming tables exist, provide option to enable BLOB repository
564 <?php
echo $strBLOBRepositoryDisabled; ?
>
566 <fieldset
class="tblFooters">
567 <input type
="hidden" name
="db_blob_streaming_op" value
="enable" />
568 <input type
="submit" value
="<?php echo $strBLOBRepositoryEnable; ?>" />
572 } // end if ($allBSTablesExist)
577 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
578 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
582 * Change database charset
584 echo '<form method="post" action="./db_operations.php">' . "\n"
585 . PMA_generate_common_hidden_inputs($db, $table)
586 . '<fieldset>' . "\n"
588 if ($cfg['PropertiesIconic']) {
589 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
590 .' alt="" width="16" height="16" />';
592 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
593 . ' </legend>' . "\n"
594 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION
,
595 'db_collation', 'select_db_collation', $db_collation, false, 3)
596 . ' <input type="submit" name="submitcollation"'
597 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
598 . '</fieldset>' . "\n"
602 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
603 $message = PMA_Message
::notice('strRelationNotWorking');
604 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
605 $message->addParam('</a>', false);
606 /* Show error if user has configured something, notice elsewhere */
607 if (!empty($cfg['Servers'][$server]['pmadb'])) {
608 $message->isError(true);
612 } // end if (!$is_information_schema)
615 // not sure about displaying the PDF dialog in case db is information_schema
616 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?
>
617 <!-- Work on PDF Pages
-->
620 // We only show this if we find something in the new pdf_pages table
624 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
625 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
626 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE
);
628 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?
>
630 <form method
="post" action
="pdf_schema.php">
634 echo PMA_generate_common_hidden_inputs($db);
635 if ($cfg['PropertiesIconic']) {
636 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
637 .' alt="" width="16" height="16" />';
642 <label
for="pdf_page_number_opt"><?php
echo $strPageNumber; ?
></label
>
643 <select name
="pdf_page_number" id
="pdf_page_number_opt">
645 while ($pages = @PMA_DBI_fetch_assoc
($test_rs)) {
646 echo ' <option value="' . $pages['page_nr'] . '">'
647 . $pages['page_nr'] . ': ' . htmlspecialchars($pages['page_descr']) . '</option>' . "\n";
649 PMA_DBI_free_result($test_rs);
654 <input type
="checkbox" name
="show_grid" id
="show_grid_opt" />
655 <label
for="show_grid_opt"><?php
echo $strShowGrid; ?
></label
><br
/>
656 <input type
="checkbox" name
="show_color" id
="show_color_opt"
658 <label
for="show_color_opt"><?php
echo $strShowColor; ?
></label
><br
/>
659 <input type
="checkbox" name
="show_table_dimension" id
="show_table_dim_opt" />
660 <label
for="show_table_dim_opt"><?php
echo $strShowTableDimension; ?
>
662 <input type
="checkbox" name
="all_tab_same_wide" id
="all_tab_same_wide" />
663 <label
for="all_tab_same_wide"><?php
echo $strAllTableSameWidth; ?
>
665 <input type
="checkbox" name
="with_doc" id
="with_doc" checked
="checked" />
666 <label
for="with_doc"><?php
echo $strDataDict; ?
></label
><br
/>
667 <input type
="checkbox" name
="show_keys" id
="show_keys" />
668 <label
for="show_keys"><?php
echo $strShowKeys; ?
></label
><br
/>
670 <label
for="orientation_opt"><?php
echo $strShowDatadictAs; ?
></label
>
671 <select name
="orientation" id
="orientation_opt">
672 <option value
="L"><?php
echo $strLandscape;?
></option
>
673 <option value
="P"><?php
echo $strPortrait;?
></option
>
676 <label
for="paper_opt"><?php
echo $strPaperSize; ?
></label
>
677 <select name
="paper" id
="paper_opt">
679 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
680 echo '<option value="' . $val . '"';
681 if ($val == $cfg['PDFDefaultPageSize']) {
682 echo ' selected="selected"';
684 echo ' >' . $val . '</option>' . "\n";
689 <fieldset
class="tblFooters">
690 <input type
="submit" value
="<?php echo $strGo; ?>" />
695 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
696 if ($cfg['PropertiesIconic']) {
697 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
698 .' alt="" width="16" height="16" />';
700 echo $strEditPDFPages . '</a>';
704 * Displays the footer
706 require_once './libraries/footer.inc.php';