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 // save the original db name because Tracker.class.php which
57 // may be called under PMA_DBI_query() changes $GLOBALS['db']
58 // for some statements, one of which being CREATE DATABASE
60 PMA_DBI_query($local_query);
64 // rebuild the database list because PMA_Table::moveCopy
65 // checks in this list if the target db exists
66 $GLOBALS['pma']->databases
->build();
69 if (isset($GLOBALS['add_constraints']) ||
$move) {
70 $GLOBALS['sql_constraints_query_full_db'] = array();
73 $tables_full = PMA_DBI_get_tables_full($db);
76 // remove all foreign key constraints, otherwise we can get errors
77 require_once './libraries/export/sql.php';
78 foreach ($tables_full as $each_table => $tmp) {
79 $sql_constraints = '';
80 $sql_drop_foreign_keys = '';
81 $sql_structure = PMA_getTableDef($db, $each_table, "\n", '', false, false);
82 if ($move && ! empty($sql_drop_foreign_keys)) {
83 PMA_DBI_query($sql_drop_foreign_keys);
85 // keep the constraint we just dropped
86 if (! empty($sql_constraints)) {
87 $GLOBALS['sql_constraints_query_full_db'][] = $sql_constraints;
90 unset($sql_constraints, $sql_drop_foreign_keys, $sql_structure);
93 foreach ($tables_full as $each_table => $tmp) {
94 // to be able to rename a db containing views, we
95 // first collect in $views all the views we find and we
96 // will handle them after the tables
98 * @todo support a view of a view
100 if (PMA_Table
::isView($db, $each_table)) {
101 $views[] = $each_table;
108 // value of $what for this table only
111 // do not copy the data from a Merge table
112 // note: on the calling FORM, 'data' means 'structure and data'
113 if (PMA_Table
::isMerge($db, $each_table)) {
114 if ($this_what == 'data') {
115 $this_what = 'structure';
117 if ($this_what == 'dataonly') {
118 $this_what = 'nocopy';
122 if ($this_what != 'nocopy') {
123 // keep the triggers from the original db+table
124 // (third param is empty because delimiters are only intended
125 // for importing via the mysql client or our Import feature)
126 $triggers = PMA_DBI_get_triggers($db, $each_table, '');
128 if (! PMA_Table
::moveCopy($db, $each_table, $newname, $each_table,
129 isset($this_what) ?
$this_what : 'data', $move, 'db_copy'))
132 // $sql_query is filled by PMA_Table::moveCopy()
133 $sql_query = $back . $sql_query;
136 // apply the triggers to the destination db+table
138 PMA_DBI_select_db($newname);
139 foreach ($triggers as $trigger) {
140 PMA_DBI_query($trigger['create']);
146 // this does not apply to a rename operation
147 if (isset($GLOBALS['add_constraints']) && !empty($GLOBALS['sql_constraints_query'])) {
148 $GLOBALS['sql_constraints_query_full_db'][] = $GLOBALS['sql_constraints_query'];
149 unset($GLOBALS['sql_constraints_query']);
152 // $sql_query is filled by PMA_Table::moveCopy()
153 $sql_query = $back . $sql_query;
159 foreach ($views as $view) {
160 if (! PMA_Table
::moveCopy($db, $view, $newname, $view,
161 'structure', $move, 'db_copy')) {
167 unset($view, $views);
169 // now that all tables exist, create all the accumulated constraints
170 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
171 PMA_DBI_select_db($newname);
172 foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
173 PMA_DBI_query($one_query);
174 // and prepare to display them
175 $GLOBALS['sql_query'] .= "\n" . $one_query;
178 unset($GLOBALS['sql_constraints_query_full_db'], $one_query);
181 if (PMA_MYSQL_INT_VERSION
>= 50000) {
182 // here I don't use DELIMITER because it's not part of the
183 // language; I have to send each statement one by one
185 // to avoid selecting alternatively the current and new db
186 // we would need to modify the CREATE definitions to qualify
188 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
189 if ($procedure_names) {
190 foreach($procedure_names as $procedure_name) {
191 PMA_DBI_select_db($db);
192 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
193 // collect for later display
194 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
195 PMA_DBI_select_db($newname);
196 PMA_DBI_query($tmp_query);
200 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
201 if ($function_names) {
202 foreach($function_names as $function_name) {
203 PMA_DBI_select_db($db);
204 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
205 // collect for later display
206 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
207 PMA_DBI_select_db($newname);
208 PMA_DBI_query($tmp_query);
212 // go back to current db, just in case
213 PMA_DBI_select_db($db);
215 // Duplicate the bookmarks for this db (done once for each db)
216 if (! $_error && $db != $newname) {
217 $get_fields = array('user', 'label', 'query');
218 $where_fields = array('dbase' => $db);
219 $new_fields = array('dbase' => $newname);
220 PMA_Table
::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
221 $where_fields, $new_fields);
224 if (! $_error && $move) {
226 * cleanup pmadb stuff for this db
228 require_once './libraries/relation_cleanup.lib.php';
229 PMA_relationsCleanupDatabase($db);
231 // if someday the RENAME DATABASE reappears, do not DROP
232 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
233 $sql_query .= "\n" . $local_query;
234 PMA_DBI_query($local_query);
236 $message = PMA_Message
::success('strRenameDatabaseOK');
237 $message->addParam($db);
238 $message->addParam($newname);
239 } elseif (! $_error) {
240 $message = PMA_Message
::success('strCopyDatabaseOK');
241 $message->addParam($db);
242 $message->addParam($newname);
246 /* Change database to be used */
247 if (! $_error && $move) {
249 } elseif (! $_error) {
250 if (isset($switch_to_new) && $switch_to_new == 'true') {
251 PMA_setCookie('pma_switch_to_new', 'true');
254 PMA_setCookie('pma_switch_to_new', '');
258 if ($_error && ! isset($message)) {
259 $message = PMA_Message
::error();
265 * Enable/Disable/Repair BLOB Repository Monitoring for current database
267 if (strlen($db) > 0 && !empty($db_blob_streaming_op))
270 $PMA_Config = $_SESSION['PMA_Config'];
272 if (!empty($PMA_Config))
274 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
276 // if Blobstreaming plugins exist, begin checking for Blobstreaming tables
277 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
279 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
280 $bs_tables = $bs_tables[$db];
282 $oneBSTableExists = FALSE;
284 // check if at least one blobstreaming table exists
285 foreach ($bs_tables as $table_key=>$tbl)
286 if ($bs_tables[$table_key]['Exists'])
288 $oneBSTableExists = TRUE;
292 switch ($db_blob_streaming_op)
294 // enable BLOB repository monitoring
296 // if blobstreaming tables do not exist, create them
297 if (!$oneBSTableExists)
298 PMA_BS_CreateTables($db);
300 // disable BLOB repository monitoring
302 // if at least one blobstreaming table exists, execute drop
303 if ($oneBSTableExists)
304 PMA_BS_DropTables($db);
306 // repair BLOB repository
308 // check if a blobstreaming table is missing
309 foreach ($bs_tables as $table_key=>$tbl)
310 if (!$bs_tables[$table_key]['Exists'])
312 PMA_DBI_select_db($db);
313 PMA_DBI_query(PMA_BS_GetTableStruct($table_key));
318 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');
319 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
320 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
325 * Settings for relations stuff
328 require_once './libraries/relation.lib.php';
329 $cfgRelation = PMA_getRelationsParam();
332 * Check if comments were updated
333 * (must be done before displaying the menu tabs)
335 if (isset($_REQUEST['comment'])) {
336 PMA_setDbComment($db, $comment);
340 * Prepares the tables list if the user where not redirected to this script
341 * because there is no table in the database ($is_info is true)
343 if (empty($is_info)) {
344 require './libraries/db_common.inc.php';
345 $url_query .= '&goto=db_operations.php';
347 // Gets the database structure
348 $sub_part = '_structure';
349 require './libraries/db_info.inc.php';
352 if (isset($message)) {
353 PMA_showMessage($message, $sql_query);
358 $db_collation = PMA_getDbCollation($db);
359 if ($db == 'information_schema') {
360 $is_information_schema = true;
362 $is_information_schema = false;
365 if (!$is_information_schema) {
367 require './libraries/display_create_table.lib.php';
369 if ($cfgRelation['commwork']) {
374 <form method
="post" action
="db_operations.php">
375 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
378 <?php
echo PMA_getIcon('b_comment.png', $strDBComment, false, true); ?
>
380 <input type
="text" name
="comment" class="textfield" size
="30"
382 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
383 <input type
="submit" value
="<?php echo $strGo; ?>" />
392 <form method
="post" action
="db_operations.php"
393 onsubmit
="return emptyFormElements(this, 'newname')">
395 if (isset($db_collation)) {
396 echo '<input type="hidden" name="db_collation" value="' . $db_collation
400 <input type
="hidden" name
="what" value
="data" />
401 <input type
="hidden" name
="db_rename" value
="true" />
402 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
406 if ($cfg['PropertiesIconic']) {
407 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
408 .' alt="" width="16" height="16" />';
410 echo $strDBRename . ':';
413 <input type
="text" name
="newname" size
="30" class="textfield" value
="" />
415 echo '(' . $strCommand . ': ';
417 * @todo (see explanations above in a previous todo)
419 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
420 // echo 'RENAME DATABASE';
422 echo 'INSERT INTO ... SELECT';
425 <input type
="submit" value
="<?php echo $strGo; ?>" onclick
="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
434 <form method
="post" action
="db_operations.php"
435 onsubmit
="return emptyFormElements(this, 'newname')">
437 if (isset($db_collation)) {
438 echo '<input type="hidden" name="db_collation" value="' . $db_collation
441 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
442 echo PMA_generate_common_hidden_inputs($db);
447 if ($cfg['PropertiesIconic']) {
448 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
449 .' alt="" width="16" height="16" />';
451 echo $strDBCopy . ':';
452 $drop_clause = 'DROP TABLE / DROP VIEW';
455 <input type
="text" name
="newname" size
="30" class="textfield" value
="" /><br
/>
458 'structure' => $strStrucOnly,
459 'data' => $strStrucData,
460 'dataonly' => $strDataOnly);
461 PMA_display_html_radio('what', $choices, 'data', true);
464 <input type
="checkbox" name
="create_database_before_copying" value
="1"
465 id
="checkbox_create_database_before_copying"
466 style
="vertical-align: middle" checked
="checked" />
467 <label
for="checkbox_create_database_before_copying">
468 <?php
echo $strCreateDatabaseBeforeCopying; ?
></label
><br
/>
469 <input type
="checkbox" name
="drop_if_exists" value
="true"
470 id
="checkbox_drop" style
="vertical-align: middle" />
471 <label
for="checkbox_drop"><?php
echo sprintf($strAddClause, $drop_clause); ?
></label
><br
/>
472 <input type
="checkbox" name
="sql_auto_increment" value
="1" checked
="checked"
473 id
="checkbox_auto_increment" style
="vertical-align: middle" />
474 <label
for="checkbox_auto_increment">
475 <?php
echo $strAddAutoIncrement; ?
></label
><br
/>
476 <input type
="checkbox" name
="add_constraints" value
="1"
477 id
="checkbox_constraints" style
="vertical-align: middle" />
478 <label
for="checkbox_constraints">
479 <?php
echo $strAddConstraints; ?
></label
><br
/>
483 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
484 && $_COOKIE['pma_switch_to_new'] == 'true') {
485 $pma_switch_to_new = 'true';
488 <input type
="checkbox" name
="switch_to_new" value
="true"
490 <?php
echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ?
' checked="checked"' : ''); ?
>
491 style
="vertical-align: middle" />
492 <label
for="checkbox_switch"><?php
echo $strSwitchToDatabase; ?
></label
>
494 <fieldset
class="tblFooters">
495 <input type
="submit" name
="submit_copy" value
="<?php echo $strGo; ?>" />
501 * BLOB streaming support
505 $PMA_Config = $_SESSION['PMA_Config'];
507 // if all blobstreaming plugins exist, begin checking for blobstreaming tables
508 if (!empty($PMA_Config))
510 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
512 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
514 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
515 $bs_tables = $bs_tables[$db];
517 $oneBSTableExists = FALSE;
518 $allBSTablesExist = TRUE;
520 // first check that all blobstreaming tables do not exist
521 foreach ($bs_tables as $table_key=>$tbl)
522 if ($bs_tables[$table_key]['Exists'])
523 $oneBSTableExists = TRUE;
525 $allBSTablesExist = FALSE;
529 <form method
="post" action
="./db_operations.php">
530 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
533 <?php
echo PMA_getIcon('b_edit.png', $strBLOBRepository, false, true); ?
>
536 <?php
echo $strStatus; ?
>:
540 // if the blobstreaming tables exist, provide option to disable the BLOB repository
541 if ($allBSTablesExist)
544 <?php
echo $strBLOBRepositoryEnabled; ?
>
546 <fieldset
class="tblFooters">
547 <input type
="hidden" name
="db_blob_streaming_op" value
="disable" />
548 <input type
="submit" onclick
="return confirmDisableRepository('<?php echo $db; ?>');" value
="<?php echo $strBLOBRepositoryDisable; ?>" />
554 // if any of the blobstreaming tables are missing, provide option to repair the BLOB repository
555 if ($oneBSTableExists && !$allBSTablesExist)
558 <?php
echo $strBLOBRepositoryDamaged; ?
>
560 <fieldset
class="tblFooters">
561 <input type
="hidden" name
="db_blob_streaming_op" value
="repair" />
562 <input type
="submit" value
="<?php echo $strBLOBRepositoryRepair; ?>" />
566 // if none of the blobstreaming tables exist, provide option to enable BLOB repository
570 <?php
echo $strBLOBRepositoryDisabled; ?
>
572 <fieldset
class="tblFooters">
573 <input type
="hidden" name
="db_blob_streaming_op" value
="enable" />
574 <input type
="submit" value
="<?php echo $strBLOBRepositoryEnable; ?>" />
578 } // end if ($allBSTablesExist)
583 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
584 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
588 * Change database charset
590 echo '<form method="post" action="./db_operations.php">' . "\n"
591 . PMA_generate_common_hidden_inputs($db, $table)
592 . '<fieldset>' . "\n"
594 if ($cfg['PropertiesIconic']) {
595 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
596 .' alt="" width="16" height="16" />';
598 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
599 . ' </legend>' . "\n"
600 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION
,
601 'db_collation', 'select_db_collation', $db_collation, false, 3)
602 . ' <input type="submit" name="submitcollation"'
603 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
604 . '</fieldset>' . "\n"
608 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
609 $message = PMA_Message
::notice('strRelationNotWorking');
610 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
611 $message->addParam('</a>', false);
612 /* Show error if user has configured something, notice elsewhere */
613 if (!empty($cfg['Servers'][$server]['pmadb'])) {
614 $message->isError(true);
618 } // end if (!$is_information_schema)
621 // not sure about displaying the PDF dialog in case db is information_schema
622 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?
>
623 <!-- Work on PDF Pages
-->
626 // We only show this if we find something in the new pdf_pages table
630 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
631 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
632 $test_rs = PMA_query_as_controluser($test_query, null, PMA_DBI_QUERY_STORE
);
634 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?
>
636 <form method
="post" action
="pdf_schema.php">
640 echo PMA_generate_common_hidden_inputs($db);
641 if ($cfg['PropertiesIconic']) {
642 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
643 .' alt="" width="16" height="16" />';
648 <label
for="pdf_page_number_opt"><?php
echo $strPageNumber; ?
></label
>
649 <select name
="pdf_page_number" id
="pdf_page_number_opt">
651 while ($pages = @PMA_DBI_fetch_assoc
($test_rs)) {
652 echo ' <option value="' . $pages['page_nr'] . '">'
653 . $pages['page_nr'] . ': ' . htmlspecialchars($pages['page_descr']) . '</option>' . "\n";
655 PMA_DBI_free_result($test_rs);
660 <input type
="checkbox" name
="show_grid" id
="show_grid_opt" />
661 <label
for="show_grid_opt"><?php
echo $strShowGrid; ?
></label
><br
/>
662 <input type
="checkbox" name
="show_color" id
="show_color_opt"
664 <label
for="show_color_opt"><?php
echo $strShowColor; ?
></label
><br
/>
665 <input type
="checkbox" name
="show_table_dimension" id
="show_table_dim_opt" />
666 <label
for="show_table_dim_opt"><?php
echo $strShowTableDimension; ?
>
668 <input type
="checkbox" name
="all_tab_same_wide" id
="all_tab_same_wide" />
669 <label
for="all_tab_same_wide"><?php
echo $strAllTableSameWidth; ?
>
671 <input type
="checkbox" name
="with_doc" id
="with_doc" checked
="checked" />
672 <label
for="with_doc"><?php
echo $strDataDict; ?
></label
><br
/>
673 <input type
="checkbox" name
="show_keys" id
="show_keys" />
674 <label
for="show_keys"><?php
echo $strShowKeys; ?
></label
><br
/>
676 <label
for="orientation_opt"><?php
echo $strShowDatadictAs; ?
></label
>
677 <select name
="orientation" id
="orientation_opt">
678 <option value
="L"><?php
echo $strLandscape;?
></option
>
679 <option value
="P"><?php
echo $strPortrait;?
></option
>
682 <label
for="paper_opt"><?php
echo $strPaperSize; ?
></label
>
683 <select name
="paper" id
="paper_opt">
685 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
686 echo '<option value="' . $val . '"';
687 if ($val == $cfg['PDFDefaultPageSize']) {
688 echo ' selected="selected"';
690 echo ' >' . $val . '</option>' . "\n";
695 <fieldset
class="tblFooters">
696 <input type
="submit" value
="<?php echo $strGo; ?>" />
701 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
702 if ($cfg['PropertiesIconic']) {
703 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
704 .' alt="" width="16" height="16" />';
706 echo $strEditPDFPages . '</a>';
710 * Displays the footer
712 require_once './libraries/footer.inc.php';