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';
22 // add blobstreaming library functions
23 require_once "./libraries/blobstreaming.lib.php";
26 * Rename/move or copy database
28 if (strlen($db) && (! empty($db_rename) ||
! empty($db_copy))) {
30 if (! empty($db_rename)) {
36 if (!isset($newname) ||
!strlen($newname)) {
37 $message = PMA_Message
::error('strDatabaseEmpty');
39 $sql_query = ''; // in case target db exists
42 (isset($create_database_before_copying) && $create_database_before_copying)) {
43 // lower_case_table_names=1 `DB` becomes `db`
44 $lower_case_table_names = PMA_DBI_fetch_value('SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1);
45 if ($lower_case_table_names === '1') {
46 $newname = strtolower($newname);
49 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
50 if (isset($db_collation)) {
51 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
54 $sql_query = $local_query;
55 PMA_DBI_query($local_query);
57 // rebuild the database list because PMA_Table::moveCopy
58 // checks in this list if the target db exists
59 $GLOBALS['pma']->databases
->build();
62 if (isset($GLOBALS['add_constraints'])) {
63 $GLOBALS['sql_constraints_query_full_db'] = '';
66 $tables_full = PMA_DBI_get_tables_full($db);
68 foreach ($tables_full as $each_table => $tmp) {
69 // to be able to rename a db containing views, we
70 // first collect in $views all the views we find and we
71 // will handle them after the tables
73 * @todo support a view of a view
74 * @todo support triggers
76 if (PMA_Table
::isView($db, $each_table)) {
77 $views[] = $each_table;
84 // value of $what for this table only
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 if (! PMA_Table
::moveCopy($db, $each_table, $newname, $each_table,
100 isset($this_what) ?
$this_what : 'data', $move, 'db_copy'))
103 // $sql_query is filled by PMA_Table::moveCopy()
104 $sql_query = $back . $sql_query;
107 if (isset($GLOBALS['add_constraints'])) {
108 $GLOBALS['sql_constraints_query_full_db'] .= $GLOBALS['sql_constraints_query'];
109 unset($GLOBALS['sql_constraints_query']);
112 // $sql_query is filled by PMA_Table::moveCopy()
113 $sql_query = $back . $sql_query;
119 foreach ($views as $view) {
120 if (! PMA_Table
::moveCopy($db, $view, $newname, $view,
121 'structure', $move, 'db_copy')) {
127 unset($view, $views);
129 // now that all tables exist, create all the accumulated constraints
130 if (! $_error && isset($GLOBALS['add_constraints'])) {
132 * @todo this works with mysqli but not with mysql, because
133 * mysql extension does not accept more than one statement; maybe
134 * interface with the sql import plugin that handles statement delimiter
136 PMA_DBI_query($GLOBALS['sql_constraints_query_full_db']);
138 // and prepare to display them
139 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['sql_constraints_query_full_db'];
140 unset($GLOBALS['sql_constraints_query_full_db']);
143 if (PMA_MYSQL_INT_VERSION
>= 50000) {
144 // here I don't use DELIMITER because it's not part of the
145 // language; I have to send each statement one by one
147 // to avoid selecting alternatively the current and new db
148 // we would need to modify the CREATE definitions to qualify
150 $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
151 if ($procedure_names) {
152 foreach($procedure_names as $procedure_name) {
153 PMA_DBI_select_db($db);
154 $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name);
155 // collect for later display
156 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
157 PMA_DBI_select_db($newname);
158 PMA_DBI_query($tmp_query);
162 $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
163 if ($function_names) {
164 foreach($function_names as $function_name) {
165 PMA_DBI_select_db($db);
166 $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name);
167 // collect for later display
168 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
169 PMA_DBI_select_db($newname);
170 PMA_DBI_query($tmp_query);
174 // go back to current db, just in case
175 PMA_DBI_select_db($db);
177 // Duplicate the bookmarks for this db (done once for each db)
178 if (! $_error && $db != $newname) {
179 $get_fields = array('user', 'label', 'query');
180 $where_fields = array('dbase' => $db);
181 $new_fields = array('dbase' => $newname);
182 PMA_Table
::duplicateInfo('bookmarkwork', 'bookmark', $get_fields,
183 $where_fields, $new_fields);
186 if (! $_error && $move) {
187 // cleanup pmadb stuff for this db
188 require_once './libraries/relation_cleanup.lib.php';
189 PMA_relationsCleanupDatabase($db);
191 // if someday the RENAME DATABASE reappears, do not DROP
192 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
193 $sql_query .= "\n" . $local_query;
194 PMA_DBI_query($local_query);
196 $message = PMA_Message
::success('strRenameDatabaseOK');
197 $message->addParam($db);
198 $message->addParam($newname);
199 } elseif (! $_error) {
200 $message = PMA_Message
::success('strCopyDatabaseOK');
201 $message->addParam($db);
202 $message->addParam($newname);
206 /* Change database to be used */
207 if (! $_error && $move) {
209 } elseif (! $_error) {
210 if (isset($switch_to_new) && $switch_to_new == 'true') {
211 PMA_setCookie('pma_switch_to_new', 'true');
214 PMA_setCookie('pma_switch_to_new', '');
218 if ($_error && ! isset($message)) {
219 $message = PMA_Message
::error();
225 * Enable/Disable/Repair BLOB Repository Monitoring for current database
227 if (strlen($db) > 0 && !empty($db_blob_streaming_op))
230 $PMA_Config = $_SESSION['PMA_Config'];
232 if (!empty($PMA_Config))
234 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
236 // if Blobstreaming plugins exist, begin checking for Blobstreaming tables
237 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
239 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
240 $bs_tables = $bs_tables[$db];
242 $oneBSTableExists = FALSE;
244 // check if at least one blobstreaming table exists
245 foreach ($bs_tables as $table_key=>$tbl)
246 if ($bs_tables[$table_key]['Exists'])
248 $oneBSTableExists = TRUE;
252 switch ($db_blob_streaming_op)
254 // enable BLOB repository monitoring
256 // if blobstreaming tables do not exist, create them
257 if (!$oneBSTableExists)
258 PMA_BS_CreateTables($db);
260 // disable BLOB repository monitoring
262 // if at least one blobstreaming table exists, execute drop
263 if ($oneBSTableExists)
264 PMA_BS_DropTables($db);
266 // repair BLOB repository
268 // check if a blobstreaming table is missing
269 foreach ($bs_tables as $table_key=>$tbl)
270 if (!$bs_tables[$table_key]['Exists'])
272 PMA_DBI_select_db($db);
273 PMA_DBI_query(PMA_BS_GetTableStruct($table_key));
278 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');
279 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
280 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
285 * Settings for relations stuff
288 require_once './libraries/relation.lib.php';
289 $cfgRelation = PMA_getRelationsParam();
292 * Check if comments were updated
293 * (must be done before displaying the menu tabs)
295 if (isset($_REQUEST['comment'])) {
296 PMA_setDbComment($db, $comment);
300 * Prepares the tables list if the user where not redirected to this script
301 * because there is no table in the database ($is_info is true)
303 if (empty($is_info)) {
304 require './libraries/db_common.inc.php';
305 $url_query .= '&goto=db_operations.php';
307 // Gets the database structure
308 $sub_part = '_structure';
309 require './libraries/db_info.inc.php';
312 if (isset($message)) {
313 PMA_showMessage($message, $sql_query);
318 $db_collation = PMA_getDbCollation($db);
319 if ($db == 'information_schema') {
320 $is_information_schema = true;
322 $is_information_schema = false;
325 if (!$is_information_schema) {
327 require './libraries/display_create_table.lib.php';
329 if ($cfgRelation['commwork']) {
334 <form method
="post" action
="db_operations.php">
335 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
338 <?php
echo PMA_getIcon('b_comment.png', $strDBComment, false, true); ?
>
340 <input type
="text" name
="comment" class="textfield" size
="30"
342 echo htmlspecialchars(PMA_getDBComment($db)); ?>" />
343 <input type
="submit" value
="<?php echo $strGo; ?>" />
352 <form method
="post" action
="db_operations.php"
353 onsubmit
="return emptyFormElements(this, 'newname')">
355 if (isset($db_collation)) {
356 echo '<input type="hidden" name="db_collation" value="' . $db_collation
360 <input type
="hidden" name
="what" value
="data" />
361 <input type
="hidden" name
="db_rename" value
="true" />
362 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
366 if ($cfg['PropertiesIconic']) {
367 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
368 .' alt="" width="16" height="16" />';
370 echo $strDBRename . ':';
373 <input type
="text" name
="newname" size
="30" class="textfield" value
="" />
375 echo '(' . $strCommand . ': ';
377 * @todo (see explanations above in a previous todo)
379 //if (PMA_MYSQL_INT_VERSION >= XYYZZ) {
380 // echo 'RENAME DATABASE';
382 echo 'INSERT INTO ... SELECT';
385 <input type
="submit" value
="<?php echo $strGo; ?>" onclick
="return confirmLink(this, 'CREATE DATABASE ... <?php echo $strAndThen; ?> DROP DATABASE <?php echo PMA_jsFormat($db); ?>')" />
394 <form method
="post" action
="db_operations.php"
395 onsubmit
="return emptyFormElements(this, 'newname')">
397 if (isset($db_collation)) {
398 echo '<input type="hidden" name="db_collation" value="' . $db_collation
401 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
402 echo PMA_generate_common_hidden_inputs($db);
407 if ($cfg['PropertiesIconic']) {
408 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
409 .' alt="" width="16" height="16" />';
411 echo $strDBCopy . ':';
412 $drop_clause = 'DROP TABLE / DROP VIEW';
415 <input type
="text" name
="newname" size
="30" class="textfield" value
="" /><br
/>
418 'structure' => $strStrucOnly,
419 'data' => $strStrucData,
420 'dataonly' => $strDataOnly);
421 PMA_generate_html_radio('what', $choices, 'data', true);
424 <input type
="checkbox" name
="create_database_before_copying" value
="1"
425 id
="checkbox_create_database_before_copying"
426 style
="vertical-align: middle" checked
="checked" />
427 <label
for="checkbox_create_database_before_copying">
428 <?php
echo $strCreateDatabaseBeforeCopying; ?
></label
><br
/>
429 <input type
="checkbox" name
="drop_if_exists" value
="true"
430 id
="checkbox_drop" style
="vertical-align: middle" />
431 <label
for="checkbox_drop"><?php
echo sprintf($strAddClause, $drop_clause); ?
></label
><br
/>
432 <input type
="checkbox" name
="sql_auto_increment" value
="1"
433 id
="checkbox_auto_increment" style
="vertical-align: middle" />
434 <label
for="checkbox_auto_increment">
435 <?php
echo $strAddAutoIncrement; ?
></label
><br
/>
436 <input type
="checkbox" name
="add_constraints" value
="1"
437 id
="checkbox_constraints" style
="vertical-align: middle" />
438 <label
for="checkbox_constraints">
439 <?php
echo $strAddConstraints; ?
></label
><br
/>
443 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
444 && $_COOKIE['pma_switch_to_new'] == 'true') {
445 $pma_switch_to_new = 'true';
448 <input type
="checkbox" name
="switch_to_new" value
="true"
450 <?php
echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ?
' checked="checked"' : ''); ?
>
451 style
="vertical-align: middle" />
452 <label
for="checkbox_switch"><?php
echo $strSwitchToDatabase; ?
></label
>
454 <fieldset
class="tblFooters">
455 <input type
="submit" name
="submit_copy" value
="<?php echo $strGo; ?>" />
461 * BLOB streaming support
465 $PMA_Config = $_SESSION['PMA_Config'];
467 // if all blobstreaming plugins exist, begin checking for blobstreaming tables
468 if (!empty($PMA_Config))
470 if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
472 if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
474 $bs_tables = $PMA_Config->get('BLOBSTREAMABLE_DATABASES');
475 $bs_tables = $bs_tables[$db];
477 $oneBSTableExists = FALSE;
478 $allBSTablesExist = TRUE;
480 // first check that all blobstreaming tables do not exist
481 foreach ($bs_tables as $table_key=>$tbl)
482 if ($bs_tables[$table_key]['Exists'])
483 $oneBSTableExists = TRUE;
485 $allBSTablesExist = FALSE;
489 <form method
="post" action
="./db_operations.php">
490 <?php
echo PMA_generate_common_hidden_inputs($db); ?
>
493 <?php
echo PMA_getIcon('b_edit.png', $strBLOBRepository, false, true); ?
>
496 <?php
echo $strBLOBRepositoryStatus; ?
>:
500 // if the blobstreaming tables exist, provide option to disable the BLOB repository
501 if ($allBSTablesExist)
504 <?php
echo $strBLOBRepositoryEnabled; ?
>
506 <fieldset
class="tblFooters">
507 <input type
="hidden" name
="db_blob_streaming_op" value
="disable" />
508 <input type
="submit" onclick
="return confirmDisableRepository('<?php echo $db; ?>');" value
="<?php echo $strBLOBRepositoryDisable; ?>" />
514 // if any of the blobstreaming tables are missing, provide option to repair the BLOB repository
515 if ($oneBSTableExists && !$allBSTablesExist)
518 <?php
echo $strBLOBRepositoryDamaged; ?
>
520 <fieldset
class="tblFooters">
521 <input type
="hidden" name
="db_blob_streaming_op" value
="repair" />
522 <input type
="submit" value
="<?php echo $strBLOBRepositoryRepair; ?>" />
526 // if none of the blobstreaming tables exist, provide option to enable BLOB repository
530 <?php
echo $strBLOBRepositoryDisabled; ?
>
532 <fieldset
class="tblFooters">
533 <input type
="hidden" name
="db_blob_streaming_op" value
="enable" />
534 <input type
="submit" value
="<?php echo $strBLOBRepositoryEnable; ?>" />
538 } // end if ($allBSTablesExist)
543 } // end if ($PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST'))
544 } // end if ($PMA_Config->get('PBXT_NAME') !== strtolower($db))
548 * Change database charset
550 echo '<form method="post" action="./db_operations.php">' . "\n"
551 . PMA_generate_common_hidden_inputs($db, $table)
552 . '<fieldset>' . "\n"
554 if ($cfg['PropertiesIconic']) {
555 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
556 .' alt="" width="16" height="16" />';
558 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
559 . ' </legend>' . "\n"
560 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION
,
561 'db_collation', 'select_db_collation', $db_collation, false, 3)
562 . ' <input type="submit" name="submitcollation"'
563 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
564 . '</fieldset>' . "\n"
568 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == false) {
569 $message = PMA_Message
::notice('strRelationNotWorking');
570 $message->addParam('<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">', false);
571 $message->addParam('</a>', false);
572 /* Show error if user has configured something, notice elsewhere */
573 if (!empty($cfg['Servers'][$server]['pmadb'])) {
574 $message->isError(true);
578 } // end if (!$is_information_schema)
581 // not sure about displaying the PDF dialog in case db is information_schema
582 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?
>
583 <!-- Work on PDF Pages
-->
586 // We only show this if we find something in the new pdf_pages table
590 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
591 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
592 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE
);
594 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?
>
596 <form method
="post" action
="pdf_schema.php">
600 echo PMA_generate_common_hidden_inputs($db);
601 if ($cfg['PropertiesIconic']) {
602 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
603 .' alt="" width="16" height="16" />';
608 <label
for="pdf_page_number_opt"><?php
echo $strPageNumber; ?
></label
>
609 <select name
="pdf_page_number" id
="pdf_page_number_opt">
611 while ($pages = @PMA_DBI_fetch_assoc
($test_rs)) {
612 echo ' <option value="' . $pages['page_nr'] . '">'
613 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
615 PMA_DBI_free_result($test_rs);
620 <input type
="checkbox" name
="show_grid" id
="show_grid_opt" />
621 <label
for="show_grid_opt"><?php
echo $strShowGrid; ?
></label
><br
/>
622 <input type
="checkbox" name
="show_color" id
="show_color_opt"
624 <label
for="show_color_opt"><?php
echo $strShowColor; ?
></label
><br
/>
625 <input type
="checkbox" name
="show_table_dimension" id
="show_table_dim_opt" />
626 <label
for="show_table_dim_opt"><?php
echo $strShowTableDimension; ?
>
628 <input type
="checkbox" name
="all_tab_same_wide" id
="all_tab_same_wide" />
629 <label
for="all_tab_same_wide"><?php
echo $strAllTableSameWidth; ?
>
631 <input type
="checkbox" name
="with_doc" id
="with_doc" checked
="checked" />
632 <label
for="with_doc"><?php
echo $strDataDict; ?
></label
><br
/>
634 <label
for="orientation_opt"><?php
echo $strShowDatadictAs; ?
></label
>
635 <select name
="orientation" id
="orientation_opt">
636 <option value
="L"><?php
echo $strLandscape;?
></option
>
637 <option value
="P"><?php
echo $strPortrait;?
></option
>
640 <label
for="paper_opt"><?php
echo $strPaperSize; ?
></label
>
641 <select name
="paper" id
="paper_opt">
643 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
644 echo '<option value="' . $val . '"';
645 if ($val == $cfg['PDFDefaultPageSize']) {
646 echo ' selected="selected"';
648 echo ' >' . $val . '</option>' . "\n";
653 <fieldset
class="tblFooters">
654 <input type
="submit" value
="<?php echo $strGo; ?>" />
659 echo '<br /><a href="pdf_pages.php?' . $url_query . '">';
660 if ($cfg['PropertiesIconic']) {
661 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
662 .' alt="" width="16" height="16" />';
664 echo $strEditPDFPages . '</a>';
668 * Displays the footer
670 require_once './libraries/footer.inc.php';