InnoDB Migration and sql TEXT field standardization, take 3.
[openemr.git] / phpmyadmin / libraries / operations.lib.php
blob33e476693b51f7ef75f5b825a9390d7575dbbf11
1 <?php
3 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * set of functions with the operations section in pma
7 * @package PhpMyAdmin
8 */
10 if (! defined('PHPMYADMIN')) {
11 exit;
14 require_once 'libraries/Partition.class.php';
16 /**
17 * Get HTML output for database comment
19 * @param string $db database name
21 * @return string $html_output
23 function PMA_getHtmlForDatabaseComment($db)
25 $html_output = '<div class="operations_half_width">'
26 . '<form method="post" action="db_operations.php" id="formDatabaseComment">'
27 . PMA_URL_getHiddenInputs($db)
28 . '<fieldset>'
29 . '<legend>';
30 if (PMA_Util::showIcons('ActionLinksMode')) {
31 $html_output .= PMA_Util::getImage('b_comment.png') . '&nbsp;';
33 $html_output .= __('Database comment');
34 $html_output .= '</legend>';
35 $html_output .= '<input type="text" name="comment" '
36 . 'class="textfield" size="30"'
37 . 'value="' . htmlspecialchars(PMA_getDBComment($db)) . '" />'
38 . '</fieldset>';
39 $html_output .= '<fieldset class="tblFooters">'
40 . '<input type="submit" value="' . __('Go') . '" />'
41 . '</fieldset>'
42 . '</form>'
43 . '</div>';
45 return $html_output;
48 /**
49 * Get HTML output for rename database
51 * @param string $db database name
53 * @return string $html_output
55 function PMA_getHtmlForRenameDatabase($db)
57 $html_output = '<div class="operations_half_width">'
58 . '<form id="rename_db_form" '
59 . 'class="ajax" '
60 . 'method="post" action="db_operations.php" '
61 . 'onsubmit="return emptyCheckTheField(this, \'newname\')">';
62 if (isset($_REQUEST['db_collation'])) {
63 $html_output .= '<input type="hidden" name="db_collation" '
64 . 'value="' . $_REQUEST['db_collation']
65 . '" />' . "\n";
67 $html_output .= '<input type="hidden" name="what" value="data" />'
68 . '<input type="hidden" name="db_rename" value="true" />'
69 . PMA_URL_getHiddenInputs($db)
70 . '<fieldset>'
71 . '<legend>';
73 if (PMA_Util::showIcons('ActionLinksMode')) {
74 $html_output .= PMA_Util::getImage('b_edit.png') . '&nbsp;';
76 $html_output .= __('Rename database to')
77 . '</legend>';
79 $html_output .= '<input id="new_db_name" type="text" name="newname" '
80 . 'maxlength="64" size="30" class="textfield" required="required" '
81 . 'value="' . htmlspecialchars($db) . '"/>';
83 if (! PMA_DRIZZLE) {
84 if ($GLOBALS['db_priv'] && $GLOBALS['table_priv']
85 && $GLOBALS['col_priv'] && $GLOBALS['proc_priv']
86 && $GLOBALS['is_reload_priv']
87 ) {
88 $html_output .= '<input type="checkbox" name="adjust_privileges" '
89 . 'value="1" id="checkbox_adjust_privileges" checked="checked" />';
90 } else {
91 $html_output .= '<input type="checkbox" name="adjust_privileges" '
92 . 'value="1" id="checkbox_adjust_privileges" title="' . __(
93 'You don\'t have sufficient privileges to perform this '
94 . 'operation; Please refer to the documentation for more details'
96 . '" disabled/>';
99 $html_output .= '<label for="checkbox_adjust_privileges">'
100 . __('Adjust privileges') . PMA_Util::showDocu('faq', 'faq6-39')
101 . '</label><br />';
104 $html_output .= ''
105 . '</fieldset>'
106 . '<fieldset class="tblFooters">'
107 . '<input id="rename_db_input" type="submit" value="' . __('Go') . '" />'
108 . '</fieldset>'
109 . '</form>'
110 . '</div>';
112 return $html_output;
116 * Get HTML for database drop link
118 * @param string $db database name
120 * @return string $html_output
122 function PMA_getHtmlForDropDatabaseLink($db)
124 $this_sql_query = 'DROP DATABASE ' . PMA_Util::backquote($db);
125 $this_url_params = array(
126 'sql_query' => $this_sql_query,
127 'back' => 'db_operations.php',
128 'goto' => 'index.php',
129 'reload' => '1',
130 'purge' => '1',
131 'message_to_show' => sprintf(
132 __('Database %s has been dropped.'),
133 htmlspecialchars(PMA_Util::backquote($db))
135 'db' => null,
138 $html_output = '<div class="operations_half_width">'
139 . '<fieldset class="caution">';
140 $html_output .= '<legend>';
141 if (PMA_Util::showIcons('ActionLinksMode')) {
142 $html_output .= PMA_Util::getImage('b_deltbl.png') . '&nbsp';
144 $html_output .= __('Remove database')
145 . '</legend>';
146 $html_output .= '<ul>';
147 $html_output .= PMA_getDeleteDataOrTablelink(
148 $this_url_params,
149 'DROP_DATABASE',
150 __('Drop the database (DROP)'),
151 'drop_db_anchor'
153 $html_output .= '</ul></fieldset>'
154 . '</div>';
156 return $html_output;
160 * Get HTML snippet for copy database
162 * @param string $db database name
164 * @return string $html_output
166 function PMA_getHtmlForCopyDatabase($db)
168 $drop_clause = 'DROP TABLE / DROP VIEW';
169 $choices = array(
170 'structure' => __('Structure only'),
171 'data' => __('Structure and data'),
172 'dataonly' => __('Data only')
175 if (isset($_COOKIE)
176 && isset($_COOKIE['pma_switch_to_new'])
177 && $_COOKIE['pma_switch_to_new'] == 'true'
179 $pma_switch_to_new = 'true';
182 $html_output = '<div class="operations_half_width clearfloat">';
183 $html_output .= '<form id="copy_db_form" '
184 . 'class="ajax" '
185 . 'method="post" action="db_operations.php" '
186 . 'onsubmit="return emptyCheckTheField(this, \'newname\')">';
188 if (isset($_REQUEST['db_collation'])) {
189 $html_output .= '<input type="hidden" name="db_collation" '
190 . 'value="' . $_REQUEST['db_collation'] . '" />' . "\n";
192 $html_output .= '<input type="hidden" name="db_copy" value="true" />' . "\n"
193 . PMA_URL_getHiddenInputs($db);
194 $html_output .= '<fieldset>'
195 . '<legend>';
197 if (PMA_Util::showIcons('ActionLinksMode')) {
198 $html_output .= PMA_Util::getImage('b_edit.png') . '&nbsp';
200 $html_output .= __('Copy database to')
201 . '</legend>'
202 . '<input type="text" maxlength="64" name="newname" size="30" '
203 . 'class="textfield" value="' . htmlspecialchars($db) . '" '
204 . 'required="required" /><br />'
205 . PMA_Util::getRadioFields(
206 'what', $choices, 'data', true
208 $html_output .= '<br />';
209 $html_output .= '<input type="checkbox" name="create_database_before_copying" '
210 . 'value="1" id="checkbox_create_database_before_copying"'
211 . 'checked="checked" />';
212 $html_output .= '<label for="checkbox_create_database_before_copying">'
213 . __('CREATE DATABASE before copying') . '</label><br />';
214 $html_output .= '<input type="checkbox" name="drop_if_exists" value="true"'
215 . 'id="checkbox_drop" />';
216 $html_output .= '<label for="checkbox_drop">'
217 . sprintf(__('Add %s'), $drop_clause)
218 . '</label><br />';
219 $html_output .= '<input type="checkbox" name="sql_auto_increment" value="1" '
220 . 'checked="checked" id="checkbox_auto_increment" />';
221 $html_output .= '<label for="checkbox_auto_increment">'
222 . __('Add AUTO_INCREMENT value') . '</label><br />';
223 $html_output .= '<input type="checkbox" name="add_constraints" value="1"'
224 . 'id="checkbox_constraints" checked="checked"/>';
225 $html_output .= '<label for="checkbox_constraints">'
226 . __('Add constraints') . '</label><br />';
227 $html_output .= '<br />';
229 if (! PMA_DRIZZLE) {
230 if ($GLOBALS['db_priv'] && $GLOBALS['table_priv']
231 && $GLOBALS['col_priv'] && $GLOBALS['proc_priv']
232 && $GLOBALS['is_reload_priv']
234 $html_output .= '<input type="checkbox" name="adjust_privileges" '
235 . 'value="1" id="checkbox_privileges" checked="checked" />';
236 } else {
237 $html_output .= '<input type="checkbox" name="adjust_privileges" '
238 . 'value="1" id="checkbox_privileges" title="' . __(
239 'You don\'t have sufficient privileges to perform this '
240 . 'operation; Please refer to the documentation for more details'
242 . '" disabled/>';
244 $html_output .= '<label for="checkbox_privileges">'
245 . __('Adjust privileges') . PMA_Util::showDocu('faq', 'faq6-39')
246 . '</label><br />';
249 $html_output .= '<input type="checkbox" name="switch_to_new" value="true"'
250 . 'id="checkbox_switch"'
251 . ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true')
252 ? ' checked="checked"'
253 : '')
254 . '/>';
255 $html_output .= '<label for="checkbox_switch">'
256 . __('Switch to copied database') . '</label>'
257 . '</fieldset>';
258 $html_output .= '<fieldset class="tblFooters">'
259 . '<input type="submit" name="submit_copy" value="' . __('Go') . '" />'
260 . '</fieldset>'
261 . '</form>'
262 . '</div>';
264 return $html_output;
268 * Get HTML snippet for change database charset
270 * @param string $db database name
271 * @param string $table table name
273 * @return string $html_output
275 function PMA_getHtmlForChangeDatabaseCharset($db, $table)
277 $html_output = '<div class="operations_half_width">'
278 . '<form id="change_db_charset_form" ';
279 $html_output .= 'class="ajax" ';
280 $html_output .= 'method="post" action="db_operations.php">';
282 $html_output .= PMA_URL_getHiddenInputs($db, $table);
284 $html_output .= '<fieldset>' . "\n"
285 . ' <legend>';
286 if (PMA_Util::showIcons('ActionLinksMode')) {
287 $html_output .= PMA_Util::getImage('s_asci.png') . '&nbsp';
289 $html_output .= '<label for="select_db_collation">' . __('Collation')
290 . '</label>' . "\n"
291 . '</legend>' . "\n"
292 . PMA_generateCharsetDropdownBox(
293 PMA_CSDROPDOWN_COLLATION,
294 'db_collation',
295 'select_db_collation',
296 isset($_REQUEST['db_collation']) ? $_REQUEST['db_collation'] : '',
297 false
299 . '</fieldset>'
300 . '<fieldset class="tblFooters">'
301 . '<input type="submit" name="submitcollation"'
302 . ' value="' . __('Go') . '" />' . "\n"
303 . '</fieldset>' . "\n"
304 . '</form></div>' . "\n";
306 return $html_output;
310 * Run the Procedure definitions and function definitions
312 * to avoid selecting alternatively the current and new db
313 * we would need to modify the CREATE definitions to qualify
314 * the db name
316 * @param string $db database name
318 * @return void
320 function PMA_runProcedureAndFunctionDefinitions($db)
322 $procedure_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'PROCEDURE');
323 if ($procedure_names) {
324 foreach ($procedure_names as $procedure_name) {
325 $GLOBALS['dbi']->selectDb($db);
326 $tmp_query = $GLOBALS['dbi']->getDefinition(
327 $db, 'PROCEDURE', $procedure_name
329 // collect for later display
330 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
331 $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
332 $GLOBALS['dbi']->query($tmp_query);
336 $function_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'FUNCTION');
337 if ($function_names) {
338 foreach ($function_names as $function_name) {
339 $GLOBALS['dbi']->selectDb($db);
340 $tmp_query = $GLOBALS['dbi']->getDefinition(
341 $db, 'FUNCTION', $function_name
343 // collect for later display
344 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
345 $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
346 $GLOBALS['dbi']->query($tmp_query);
352 * Create database before copy
354 * @return void
356 function PMA_createDbBeforeCopy()
358 // lower_case_table_names=1 `DB` becomes `db`
359 if (! PMA_DRIZZLE) {
360 $lowerCaseTableNames = $GLOBALS['dbi']->fetchValue(
361 'SELECT @@lower_case_table_names'
363 if ($lowerCaseTableNames === '1') {
364 $_REQUEST['newname'] = /*overload*/mb_strtolower(
365 $_REQUEST['newname']
370 $local_query = 'CREATE DATABASE IF NOT EXISTS '
371 . PMA_Util::backquote($_REQUEST['newname']);
372 if (isset($_REQUEST['db_collation'])) {
373 $local_query .= ' DEFAULT'
374 . PMA_generateCharsetQueryPart($_REQUEST['db_collation']);
376 $local_query .= ';';
377 $GLOBALS['sql_query'] .= $local_query;
379 // save the original db name because Tracker.class.php which
380 // may be called under $GLOBALS['dbi']->query() changes $GLOBALS['db']
381 // for some statements, one of which being CREATE DATABASE
382 $original_db = $GLOBALS['db'];
383 $GLOBALS['dbi']->query($local_query);
384 $GLOBALS['db'] = $original_db;
386 // Set the SQL mode to NO_AUTO_VALUE_ON_ZERO to prevent MySQL from creating
387 // export statements it cannot import
388 if (! PMA_DRIZZLE) {
389 $sql_set_mode = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'";
390 $GLOBALS['dbi']->query($sql_set_mode);
393 // rebuild the database list because PMA_Table::moveCopy
394 // checks in this list if the target db exists
395 $GLOBALS['pma']->databases->build();
399 * Get views as an array and create SQL view stand-in
401 * @param array $tables_full array of all tables in given db or dbs
402 * @param ExportSql $export_sql_plugin export plugin instance
403 * @param string $db database name
405 * @return array $views
407 function PMA_getViewsAndCreateSqlViewStandIn(
408 $tables_full, $export_sql_plugin, $db
410 $views = array();
411 foreach ($tables_full as $each_table => $tmp) {
412 // to be able to rename a db containing views,
413 // first all the views are collected and a stand-in is created
414 // the real views are created after the tables
415 if ($GLOBALS['dbi']->getTable($db, $each_table)->isView()) {
417 // If view exists, and 'add drop view' is selected: Drop it!
418 if ($_REQUEST['what'] != 'nocopy'
419 && isset($_REQUEST['drop_if_exists'])
420 && $_REQUEST['drop_if_exists'] == 'true'
422 $drop_query = 'DROP VIEW IF EXISTS '
423 . PMA_Util::backquote($_REQUEST['newname']) . '.'
424 . PMA_Util::backquote($each_table);
425 $GLOBALS['dbi']->query($drop_query);
427 $GLOBALS['sql_query'] .= "\n" . $drop_query . ';';
430 $views[] = $each_table;
431 // Create stand-in definition to resolve view dependencies
432 $sql_view_standin = $export_sql_plugin->getTableDefStandIn(
433 $db, $each_table, "\n"
435 $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
436 $GLOBALS['dbi']->query($sql_view_standin);
437 $GLOBALS['sql_query'] .= "\n" . $sql_view_standin;
440 return $views;
444 * Get sql query for copy/rename table and boolean for whether copy/rename or not
446 * @param array $tables_full array of all tables in given db or dbs
447 * @param boolean $move whether database name is empty or not
448 * @param string $db database name
450 * @return array SQL queries for the constraints
452 function PMA_copyTables($tables_full, $move, $db)
454 $sqlContraints = array();
455 foreach ($tables_full as $each_table => $tmp) {
456 // skip the views; we have created stand-in definitions
457 if ($GLOBALS['dbi']->getTable($db, $each_table)->isView()) {
458 continue;
461 // value of $what for this table only
462 $this_what = $_REQUEST['what'];
464 // do not copy the data from a Merge table
465 // note: on the calling FORM, 'data' means 'structure and data'
466 if ($GLOBALS['dbi']->getTable($db, $each_table)->isMerge()) {
467 if ($this_what == 'data') {
468 $this_what = 'structure';
470 if ($this_what == 'dataonly') {
471 $this_what = 'nocopy';
475 if ($this_what != 'nocopy') {
476 // keep the triggers from the original db+table
477 // (third param is empty because delimiters are only intended
478 // for importing via the mysql client or our Import feature)
479 $triggers = $GLOBALS['dbi']->getTriggers($db, $each_table, '');
481 if (! PMA_Table::moveCopy(
482 $db, $each_table, $_REQUEST['newname'], $each_table,
483 (isset($this_what) ? $this_what : 'data'),
484 $move, 'db_copy'
485 )) {
486 $GLOBALS['_error'] = true;
487 break;
489 // apply the triggers to the destination db+table
490 if ($triggers) {
491 $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
492 foreach ($triggers as $trigger) {
493 $GLOBALS['dbi']->query($trigger['create']);
494 $GLOBALS['sql_query'] .= "\n" . $trigger['create'] . ';';
498 // this does not apply to a rename operation
499 if (isset($_REQUEST['add_constraints'])
500 && ! empty($GLOBALS['sql_constraints_query'])
502 $sqlContraints[] = $GLOBALS['sql_constraints_query'];
503 unset($GLOBALS['sql_constraints_query']);
507 return $sqlContraints;
511 * Run the EVENT definition for selected database
513 * to avoid selecting alternatively the current and new db
514 * we would need to modify the CREATE definitions to qualify
515 * the db name
517 * @param string $db database name
519 * @return void
521 function PMA_runEventDefinitionsForDb($db)
523 $event_names = $GLOBALS['dbi']->fetchResult(
524 'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \''
525 . PMA_Util::sqlAddSlashes($db, true) . '\';'
527 if ($event_names) {
528 foreach ($event_names as $event_name) {
529 $GLOBALS['dbi']->selectDb($db);
530 $tmp_query = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $event_name);
531 // collect for later display
532 $GLOBALS['sql_query'] .= "\n" . $tmp_query;
533 $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
534 $GLOBALS['dbi']->query($tmp_query);
540 * Handle the views, return the boolean value whether table rename/copy or not
542 * @param array $views views as an array
543 * @param boolean $move whether database name is empty or not
544 * @param string $db database name
546 * @return void
548 function PMA_handleTheViews($views, $move, $db)
550 // temporarily force to add DROP IF EXIST to CREATE VIEW query,
551 // to remove stand-in VIEW that was created earlier
552 // ( $_REQUEST['drop_if_exists'] is used in moveCopy() )
553 if (isset($_REQUEST['drop_if_exists'])) {
554 $temp_drop_if_exists = $_REQUEST['drop_if_exists'];
557 $_REQUEST['drop_if_exists'] = 'true';
558 foreach ($views as $view) {
559 $copying_succeeded = PMA_Table::moveCopy(
560 $db, $view, $_REQUEST['newname'], $view, 'structure', $move, 'db_copy'
562 if (! $copying_succeeded) {
563 $GLOBALS['_error'] = true;
564 break;
567 unset($_REQUEST['drop_if_exists']);
569 if (isset($temp_drop_if_exists)) {
570 // restore previous value
571 $_REQUEST['drop_if_exists'] = $temp_drop_if_exists;
576 * Adjust the privileges after Renaming the db
578 * @param string $oldDb Database name before renaming
579 * @param string $newname New Database name requested
581 * @return void
583 function PMA_AdjustPrivileges_moveDB($oldDb, $newname)
585 if (! PMA_DRIZZLE) {
586 if ($GLOBALS['db_priv'] && $GLOBALS['table_priv']
587 && $GLOBALS['col_priv'] && $GLOBALS['proc_priv']
588 && $GLOBALS['is_reload_priv']
590 $GLOBALS['dbi']->selectDb('mysql');
592 // For Db specific privileges
593 $query_db_specific = 'UPDATE ' . PMA_Util::backquote('db')
594 . 'SET Db = "' . $newname
595 . '" where Db = "' . $oldDb . '";';
596 $GLOBALS['dbi']->query($query_db_specific);
598 // For table specific privileges
599 $query_table_specific = 'UPDATE ' . PMA_Util::backquote('tables_priv')
600 . 'SET Db = "' . $newname
601 . '" where Db = "' . $oldDb . '";';
602 $GLOBALS['dbi']->query($query_table_specific);
604 // For column specific privileges
605 $query_col_specific = 'UPDATE ' . PMA_Util::backquote('columns_priv')
606 . 'SET Db = "' . $newname
607 . '" where Db = "' . $oldDb . '";';
608 $GLOBALS['dbi']->query($query_col_specific);
610 // For procedures specific privileges
611 $query_proc_specific = 'UPDATE ' . PMA_Util::backquote('procs_priv')
612 . 'SET Db = "' . $newname
613 . '" where Db = "' . $oldDb . '";';
614 $GLOBALS['dbi']->query($query_proc_specific);
616 // Finally FLUSH the new privileges
617 $flush_query = "FLUSH PRIVILEGES;";
618 $GLOBALS['dbi']->query($flush_query);
624 * Adjust the privileges after Copying the db
626 * @param string $oldDb Database name before copying
627 * @param string $newname New Database name requested
629 * @return void
631 function PMA_AdjustPrivileges_copyDB($oldDb, $newname)
633 if (! PMA_DRIZZLE) {
634 if ($GLOBALS['db_priv'] && $GLOBALS['table_priv']
635 && $GLOBALS['col_priv'] && $GLOBALS['proc_priv']
636 && $GLOBALS['is_reload_priv']
638 $GLOBALS['dbi']->selectDb('mysql');
640 $query_db_specific_old = 'SELECT * FROM '
641 . PMA_Util::backquote('db') . ' WHERE '
642 . 'Db = "' . $oldDb . '";';
644 $old_privs_db = $GLOBALS['dbi']->fetchResult($query_db_specific_old, 0);
646 foreach ($old_privs_db as $old_priv) {
647 $newDb_db_privs_query = 'INSERT INTO ' . PMA_Util::backquote('db')
648 . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
649 . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
650 . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
651 . $old_priv[7] . '", "' . $old_priv[8] . '", "' . $old_priv[9]
652 . '", "' . $old_priv[10] . '", "' . $old_priv[11] . '", "'
653 . $old_priv[12] . '", "' . $old_priv[13] . '", "' . $old_priv[14]
654 . '", "' . $old_priv[15] . '", "' . $old_priv[16] . '", "'
655 . $old_priv[17] . '", "' . $old_priv[18] . '", "' . $old_priv[19]
656 . '", "' . $old_priv[20] . '", "' . $old_priv[21] . '");';
658 $GLOBALS['dbi']->query($newDb_db_privs_query);
661 // For Table Specific privileges
662 $query_table_specific_old = 'SELECT * FROM '
663 . PMA_Util::backquote('tables_priv') . ' WHERE '
664 . 'Db = "' . $oldDb . '";';
666 $old_privs_table = $GLOBALS['dbi']->fetchResult(
667 $query_table_specific_old,
671 foreach ($old_privs_table as $old_priv) {
672 $newDb_table_privs_query = 'INSERT INTO ' . PMA_Util::backquote(
673 'tables_priv'
674 ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
675 . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
676 . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
677 . $old_priv[7] . '");';
679 $GLOBALS['dbi']->query($newDb_table_privs_query);
682 // For Column Specific privileges
683 $query_col_specific_old = 'SELECT * FROM '
684 . PMA_Util::backquote('columns_priv') . ' WHERE '
685 . 'Db = "' . $oldDb . '";';
687 $old_privs_col = $GLOBALS['dbi']->fetchResult(
688 $query_col_specific_old,
692 foreach ($old_privs_col as $old_priv) {
693 $newDb_col_privs_query = 'INSERT INTO ' . PMA_Util::backquote(
694 'columns_priv'
695 ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
696 . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
697 . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '");';
699 $GLOBALS['dbi']->query($newDb_col_privs_query);
702 // For Procedure Specific privileges
703 $query_proc_specific_old = 'SELECT * FROM '
704 . PMA_Util::backquote('procs_priv') . ' WHERE '
705 . 'Db = "' . $oldDb . '";';
707 $old_privs_proc = $GLOBALS['dbi']->fetchResult(
708 $query_proc_specific_old,
712 foreach ($old_privs_proc as $old_priv) {
713 $newDb_proc_privs_query = 'INSERT INTO ' . PMA_Util::backquote(
714 'procs_priv'
715 ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
716 . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
717 . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
718 . $old_priv[7] . '");';
720 $GLOBALS['dbi']->query($newDb_proc_privs_query);
723 // Finally FLUSH the new privileges
724 $flush_query = "FLUSH PRIVILEGES;";
725 $GLOBALS['dbi']->query($flush_query);
731 * Create all accumulated constraints
733 * @param array $sqlConstratints array of sql constraints for the database
735 * @return void
737 function PMA_createAllAccumulatedConstraints($sqlConstratints)
739 $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
740 foreach ($sqlConstratints as $one_query) {
741 $GLOBALS['dbi']->query($one_query);
742 // and prepare to display them
743 $GLOBALS['sql_query'] .= "\n" . $one_query;
748 * Duplicate the bookmarks for the db (done once for each db)
750 * @param boolean $_error whether table rename/copy or not
751 * @param string $db database name
753 * @return void
755 function PMA_duplicateBookmarks($_error, $db)
757 if (! $_error && $db != $_REQUEST['newname']) {
758 $get_fields = array('user', 'label', 'query');
759 $where_fields = array('dbase' => $db);
760 $new_fields = array('dbase' => $_REQUEST['newname']);
761 PMA_Table::duplicateInfo(
762 'bookmarkwork', 'bookmark', $get_fields,
763 $where_fields, $new_fields
769 * Get the HTML snippet for order the table
771 * @param array $columns columns array
773 * @return string $html_out
775 function PMA_getHtmlForOrderTheTable($columns)
777 $html_output = '<div class="operations_half_width">';
778 $html_output .= '<form method="post" id="alterTableOrderby" '
779 . 'action="tbl_operations.php">';
780 $html_output .= PMA_URL_getHiddenInputs(
781 $GLOBALS['db'], $GLOBALS['table']
783 $html_output .= '<fieldset id="fieldset_table_order">'
784 . '<legend>' . __('Alter table order by') . '</legend>'
785 . '<select name="order_field">';
787 foreach ($columns as $fieldname) {
788 $html_output .= '<option '
789 . 'value="' . htmlspecialchars($fieldname['Field']) . '">'
790 . htmlspecialchars($fieldname['Field']) . '</option>' . "\n";
792 $html_output .= '</select> ' . __('(singly)') . ' '
793 . '<br />'
794 . '<input id="order_order_asc" name="order_order"'
795 . ' type="radio" value="asc" checked="checked" />'
796 . '<label for="order_order_asc">' . __('Ascending') . '</label>'
797 . '<input id="order_order_desc" name="order_order"'
798 . ' type="radio" value="desc" />'
799 . '<label for="order_order_desc">' . __('Descending') . '</label>'
800 . '</fieldset>'
801 . '<fieldset class="tblFooters">'
802 . '<input type="hidden" name="submitorderby" value="1" />'
803 . '<input type="submit" value="' . __('Go') . '" />'
804 . '</fieldset>'
805 . '</form>'
806 . '</div>';
808 return $html_output;
812 * Get the HTML snippet for move table
814 * @return string $html_output
816 function PMA_getHtmlForMoveTable()
818 $html_output = '<div class="operations_half_width">';
819 $html_output .= '<form method="post" action="tbl_operations.php"'
820 . ' id="moveTableForm" class="ajax"'
821 . ' onsubmit="return emptyCheckTheField(this, \'new_name\')">'
822 . PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table']);
824 $html_output .= '<input type="hidden" name="reload" value="1" />'
825 . '<input type="hidden" name="what" value="data" />'
826 . '<fieldset id="fieldset_table_rename">';
828 $html_output .= '<legend>' . __('Move table to (database<b>.</b>table)')
829 . '</legend>';
831 if (count($GLOBALS['pma']->databases) > $GLOBALS['cfg']['MaxDbList']) {
832 $html_output .= '<input type="text" maxlength="100" size="30" '
833 . 'name="target_db" value="' . htmlspecialchars($GLOBALS['db'])
834 . '"/>';
835 } else {
836 $html_output .= '<select class="halfWidth" name="target_db">'
837 . $GLOBALS['pma']->databases->getHtmlOptions(true, false)
838 . '</select>';
840 $html_output .= '&nbsp;<strong>.</strong>&nbsp;';
841 $html_output .= '<input class="halfWidth" type="text" size="20" name="new_name"'
842 . ' maxlength="64" required="required" '
843 . 'value="' . htmlspecialchars($GLOBALS['table']) . '" /><br />';
845 // starting with MySQL 5.0.24, SHOW CREATE TABLE includes the AUTO_INCREMENT
846 // next value but users can decide if they want it or not for the operation
848 $html_output .= '<input type="checkbox" name="sql_auto_increment" '
849 . 'value="1" id="checkbox_auto_increment_mv" checked="checked" />'
850 . '<label for="checkbox_auto_increment_mv">'
851 . __('Add AUTO_INCREMENT value')
852 . '</label><br />';
854 if (! PMA_DRIZZLE) {
855 if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
856 && $GLOBALS['is_reload_priv']
858 $html_output .= '<input type="checkbox" name="adjust_privileges" '
859 . 'value="1" id="checkbox_privileges_tables_move" '
860 . 'checked="checked" />';
861 } else {
862 $html_output .= '<input type="checkbox" name="adjust_privileges" '
863 . 'value="1" id="checkbox_privileges_tables_move" title="' . __(
864 'You don\'t have sufficient privileges to perform this '
865 . 'operation; Please refer to the documentation for more details'
867 . '" disabled/>';
869 $html_output .= '<label for="checkbox_privileges_tables_move">'
870 . __('Adjust privileges') . PMA_Util::showDocu('faq', 'faq6-39')
871 . '</label><br />';
874 $html_output .= '</fieldset><fieldset class="tblFooters">'
875 . '<input type="submit" name="submit_move" value="' . __('Go') . '" />'
876 . '</fieldset>'
877 . '</form>'
878 . '</div>';
880 return $html_output;
884 * Get the HTML div for Table option
886 * @param string $comment Comment
887 * @param array $tbl_collation table collation
888 * @param string $tbl_storage_engine table storage engine
889 * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
890 * @param boolean $is_isam whether ISAM or not
891 * @param string $pack_keys pack keys
892 * @param string $auto_increment value of auto increment
893 * @param string $delay_key_write delay key write
894 * @param string $transactional value of transactional
895 * @param string $page_checksum value of page checksum
896 * @param boolean $is_innodb whether INNODB or not
897 * @param boolean $is_pbxt whether PBXT or not
898 * @param boolean $is_aria whether ARIA or not
899 * @param string $checksum the checksum
901 * @return string $html_output
903 function PMA_getTableOptionDiv($comment, $tbl_collation, $tbl_storage_engine,
904 $is_myisam_or_aria, $is_isam, $pack_keys, $auto_increment, $delay_key_write,
905 $transactional, $page_checksum, $is_innodb, $is_pbxt, $is_aria, $checksum
907 $html_output = '<div class="operations_half_width clearfloat">';
908 $html_output .= '<form method="post" action="tbl_operations.php"';
909 $html_output .= ' id="tableOptionsForm" class="ajax">';
910 $html_output .= PMA_URL_getHiddenInputs(
911 $GLOBALS['db'], $GLOBALS['table']
913 $html_output .= '<input type="hidden" name="reload" value="1" />';
915 $html_output .= PMA_getTableOptionFieldset(
916 $comment, $tbl_collation,
917 $tbl_storage_engine, $is_myisam_or_aria, $is_isam, $pack_keys,
918 $delay_key_write, $auto_increment, $transactional, $page_checksum,
919 $is_innodb, $is_pbxt, $is_aria, $checksum
922 $html_output .= '<fieldset class="tblFooters">'
923 . '<input type="hidden" name="submitoptions" value="1" />'
924 . '<input type="submit" value="' . __('Go') . '" />'
925 . '</fieldset>'
926 . '</form>'
927 . '</div>';
929 return $html_output;
933 * Get HTML for the rename table part of table options
935 * @return string $html_output
937 function PMA_getHtmlForRenameTable()
939 $html_output = '<tr><td class="vmiddle">' . __('Rename table to') . '</td>'
940 . '<td>'
941 . '<input type="text" size="20" name="new_name" maxlength="64" '
942 . 'value="' . htmlspecialchars($GLOBALS['table'])
943 . '" required="required" />'
944 . '</td></tr>'
945 . '<tr><td></td><td>';
947 if (! PMA_DRIZZLE) {
948 if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
949 && $GLOBALS['is_reload_priv']
951 $html_output .= '<input type="checkbox" name="adjust_privileges" '
952 . 'value="1" id="checkbox_privileges_table_options" '
953 . 'checked="checked" />';
954 } else {
955 $html_output .= '<input type="checkbox" name="adjust_privileges" '
956 . 'value="1" id="checkbox_privileges_table_options" title="' . __(
957 'You don\'t have sufficient privileges to perform this '
958 . 'operation; Please refer to the documentation for more details'
960 . '" disabled/>';
962 $html_output .= '<label for="checkbox_privileges_table_options">'
963 . __('Adjust privileges') . '&nbsp;'
964 . PMA_Util::showDocu('faq', 'faq6-39') . '</label>';
967 $html_output .= '</td></tr>';
968 return $html_output;
972 * Get HTML for the table comments part of table options
974 * @param string $current_value of the table comments
976 * @return string $html_output
978 function PMA_getHtmlForTableComments($current_value)
980 $commentLength = PMA_MYSQL_INT_VERSION >= 50503 ? 2048 : 60;
981 $html_output = '<tr><td class="vmiddle">' . __('Table comments') . '</td>'
982 . '<td><input type="text" name="comment" '
983 . 'maxlength="' . $commentLength . '" size="30"'
984 . 'value="' . htmlspecialchars($current_value) . '" />'
985 . '<input type="hidden" name="prev_comment" value="'
986 . htmlspecialchars($current_value) . '" />'
987 . '</td>'
988 . '</tr>';
990 return $html_output;
994 * Get HTML for the PACK KEYS part of table options
996 * @param string $current_value of the pack keys option
998 * @return string $html_output
1000 function PMA_getHtmlForPackKeys($current_value)
1002 $html_output = '<tr>'
1003 . '<td class="vmiddle"><label for="new_pack_keys">PACK_KEYS</label></td>'
1004 . '<td><select name="new_pack_keys" id="new_pack_keys">';
1006 $html_output .= '<option value="DEFAULT"';
1007 if ($current_value == 'DEFAULT') {
1008 $html_output .= 'selected="selected"';
1010 $html_output .= '>DEFAULT</option>
1011 <option value="0"';
1012 if ($current_value == '0') {
1013 $html_output .= 'selected="selected"';
1015 $html_output .= '>0</option>
1016 <option value="1" ';
1017 if ($current_value == '1') {
1018 $html_output .= 'selected="selected"';
1020 $html_output .= '>1</option>'
1021 . '</select>'
1022 . '</td>'
1023 . '</tr>';
1025 return $html_output;
1029 * Get HTML fieldset for Table option, it contains HTML table for options
1031 * @param string $comment Comment
1032 * @param array $tbl_collation table collation
1033 * @param string $tbl_storage_engine table storage engine
1034 * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
1035 * @param boolean $is_isam whether ISAM or not
1036 * @param string $pack_keys pack keys
1037 * @param string $delay_key_write delay key write
1038 * @param string $auto_increment value of auto increment
1039 * @param string $transactional value of transactional
1040 * @param string $page_checksum value of page checksum
1041 * @param boolean $is_innodb whether INNODB or not
1042 * @param boolean $is_pbxt whether PBXT or not
1043 * @param boolean $is_aria whether ARIA or not
1044 * @param string $checksum the checksum
1046 * @return string $html_output
1048 function PMA_getTableOptionFieldset($comment, $tbl_collation,
1049 $tbl_storage_engine, $is_myisam_or_aria, $is_isam, $pack_keys,
1050 $delay_key_write, $auto_increment, $transactional,
1051 $page_checksum, $is_innodb, $is_pbxt, $is_aria, $checksum
1053 $html_output = '<fieldset>'
1054 . '<legend>' . __('Table options') . '</legend>';
1056 $html_output .= '<table>';
1057 $html_output .= PMA_getHtmlForRenameTable();
1058 $html_output .= PMA_getHtmlForTableComments($comment);
1060 //Storage engine
1061 $html_output .= '<tr><td class="vmiddle">' . __('Storage Engine')
1062 . '&nbsp;' . PMA_Util::showMySQLDocu('Storage_engines')
1063 . '</td>'
1064 . '<td>'
1065 . PMA_StorageEngine::getHtmlSelect(
1066 'new_tbl_storage_engine', null, $tbl_storage_engine
1068 . '</td>'
1069 . '</tr>';
1071 //Table character set
1072 $html_output .= '<tr><td class="vmiddle">' . __('Collation') . '</td>'
1073 . '<td>'
1074 . PMA_generateCharsetDropdownBox(
1075 PMA_CSDROPDOWN_COLLATION,
1076 'tbl_collation', null, $tbl_collation, false
1078 . '</td>'
1079 . '</tr>';
1081 // Change all Column collations
1082 $html_output .= '<tr><td></td><td>'
1083 . '<input type="checkbox" name="change_all_collations" value="1" '
1084 . 'id="checkbox_change_all_collations" />'
1085 . '<label for="checkbox_change_all_collations">'
1086 . __('Change all column collations')
1087 . '</label>'
1088 . '</td></tr>';
1090 if ($is_myisam_or_aria || $is_isam) {
1091 $html_output .= PMA_getHtmlForPackKeys($pack_keys);
1092 } // end if (MYISAM|ISAM)
1094 if ($is_myisam_or_aria) {
1095 $html_output .= PMA_getHtmlForTableRow(
1096 'new_checksum',
1097 'CHECKSUM',
1098 $checksum
1101 $html_output .= PMA_getHtmlForTableRow(
1102 'new_delay_key_write',
1103 'DELAY_KEY_WRITE',
1104 $delay_key_write
1106 } // end if (MYISAM)
1108 if ($is_aria) {
1109 $html_output .= PMA_getHtmlForTableRow(
1110 'new_transactional',
1111 'TRANSACTIONAL',
1112 $transactional
1115 $html_output .= PMA_getHtmlForTableRow(
1116 'new_page_checksum',
1117 'PAGE_CHECKSUM',
1118 $page_checksum
1120 } // end if (ARIA)
1122 if (/*overload*/mb_strlen($auto_increment) > 0
1123 && ($is_myisam_or_aria || $is_innodb || $is_pbxt)
1125 $html_output .= '<tr><td class="vmiddle">'
1126 . '<label for="auto_increment_opt">AUTO_INCREMENT</label></td>'
1127 . '<td><input type="number" name="new_auto_increment" '
1128 . 'id="auto_increment_opt"'
1129 . 'value="' . $auto_increment . '" /></td>'
1130 . '</tr> ';
1131 } // end if (MYISAM|INNODB)
1133 $possible_row_formats = PMA_getPossibleRowFormat();
1135 // for MYISAM there is also COMPRESSED but it can be set only by the
1136 // myisampack utility, so don't offer here the choice because if we
1137 // try it inside an ALTER TABLE, MySQL (at least in 5.1.23-maria)
1138 // does not return a warning
1139 // (if the table was compressed, it can be seen on the Structure page)
1141 if (isset($possible_row_formats[$tbl_storage_engine])) {
1142 $current_row_format
1143 = /*overload*/mb_strtoupper($GLOBALS['showtable']['Row_format']);
1144 $html_output .= '<tr><td class="vmiddle">'
1145 . '<label for="new_row_format">ROW_FORMAT</label></td>'
1146 . '<td>';
1147 $html_output .= PMA_Util::getDropdown(
1148 'new_row_format', $possible_row_formats[$tbl_storage_engine],
1149 $current_row_format, 'new_row_format'
1151 $html_output .= '</td></tr>';
1153 $html_output .= '</table>'
1154 . '</fieldset>';
1156 return $html_output;
1160 * Get the common HTML table row (tr) for new_checksum, new_delay_key_write,
1161 * new_transactional and new_page_checksum
1163 * @param string $attribute class, name and id attribute
1164 * @param string $label label value
1165 * @param string $val checksum, delay_key_write, transactional, page_checksum
1167 * @return string $html_output
1169 function PMA_getHtmlForTableRow($attribute, $label, $val)
1171 return '<tr>'
1172 . '<td class="vmiddle">'
1173 . '<label for="' . $attribute . '">' . $label . '</label>'
1174 . '</td>'
1175 . '<td>'
1176 . '<input type="checkbox" name="' . $attribute . '" id="' . $attribute . '"'
1177 . ' value="1"' . ((!empty($val) && $val == 1) ? ' checked="checked"' : '')
1178 . '/>'
1179 . '</td>'
1180 . '</tr>';
1184 * Get array of possible row formats
1186 * @return array $possible_row_formats
1188 function PMA_getPossibleRowFormat()
1190 // the outer array is for engines, the inner array contains the dropdown
1191 // option values as keys then the dropdown option labels
1193 $possible_row_formats = array(
1194 'ARIA' => array(
1195 'FIXED' => 'FIXED',
1196 'DYNAMIC' => 'DYNAMIC',
1197 'PAGE' => 'PAGE'
1199 'MARIA' => array(
1200 'FIXED' => 'FIXED',
1201 'DYNAMIC' => 'DYNAMIC',
1202 'PAGE' => 'PAGE'
1204 'MYISAM' => array(
1205 'FIXED' => 'FIXED',
1206 'DYNAMIC' => 'DYNAMIC'
1208 'PBXT' => array(
1209 'FIXED' => 'FIXED',
1210 'DYNAMIC' => 'DYNAMIC'
1212 'INNODB' => array(
1213 'COMPACT' => 'COMPACT',
1214 'REDUNDANT' => 'REDUNDANT'
1218 /** @var PMA_StorageEngine_Innodb $innodbEnginePlugin */
1219 $innodbEnginePlugin = PMA_StorageEngine::getEngine('innodb');
1220 $innodbPluginVersion = $innodbEnginePlugin->getInnodbPluginVersion();
1221 if (!empty($innodbPluginVersion)) {
1222 $innodb_file_format = $innodbEnginePlugin->getInnodbFileFormat();
1223 } else {
1224 $innodb_file_format = '';
1226 if ('Barracuda' == $innodb_file_format
1227 && $innodbEnginePlugin->supportsFilePerTable()
1229 $possible_row_formats['INNODB']['DYNAMIC'] = 'DYNAMIC';
1230 $possible_row_formats['INNODB']['COMPRESSED'] = 'COMPRESSED';
1233 return $possible_row_formats;
1237 * Get HTML div for copy table
1239 * @return string $html_output
1241 function PMA_getHtmlForCopytable()
1243 $html_output = '<div class="operations_half_width">';
1244 $html_output .= '<form method="post" action="tbl_operations.php" '
1245 . 'name="copyTable" '
1246 . 'id="copyTable" '
1247 . ' class="ajax" '
1248 . 'onsubmit="return emptyCheckTheField(this, \'new_name\')">'
1249 . PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
1250 . '<input type="hidden" name="reload" value="1" />';
1252 $html_output .= '<fieldset>';
1253 $html_output .= '<legend>'
1254 . __('Copy table to (database<b>.</b>table)') . '</legend>';
1256 if (count($GLOBALS['pma']->databases) > $GLOBALS['cfg']['MaxDbList']) {
1257 $html_output .= '<input class="halfWidth" type="text" maxlength="100" '
1258 . 'size="30" name="target_db" '
1259 . 'value="' . htmlspecialchars($GLOBALS['db']) . '"/>';
1260 } else {
1261 $html_output .= '<select class="halfWidth" name="target_db">'
1262 . $GLOBALS['pma']->databases->getHtmlOptions(true, false)
1263 . '</select>';
1265 $html_output .= '&nbsp;<strong>.</strong>&nbsp;';
1266 $html_output .= '<input class="halfWidth" type="text" required="required" '
1267 . 'size="20" name="new_name" maxlength="64" '
1268 . 'value="' . htmlspecialchars($GLOBALS['table']) . '"/><br />';
1270 $choices = array(
1271 'structure' => __('Structure only'),
1272 'data' => __('Structure and data'),
1273 'dataonly' => __('Data only')
1276 $html_output .= PMA_Util::getRadioFields(
1277 'what', $choices, 'data', true
1279 $html_output .= '<br />';
1281 $html_output .= '<input type="checkbox" name="drop_if_exists" '
1282 . 'value="true" id="checkbox_drop" />'
1283 . '<label for="checkbox_drop">'
1284 . sprintf(__('Add %s'), 'DROP TABLE') . '</label><br />'
1285 . '<input type="checkbox" name="sql_auto_increment" '
1286 . 'value="1" id="checkbox_auto_increment_cp" />'
1287 . '<label for="checkbox_auto_increment_cp">'
1288 . __('Add AUTO_INCREMENT value') . '</label><br />';
1290 // display "Add constraints" choice only if there are
1291 // foreign keys
1292 if (PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'foreign')) {
1293 $html_output .= '<input type="checkbox" name="add_constraints" '
1294 . 'value="1" id="checkbox_constraints" checked="checked"/>';
1295 $html_output .= '<label for="checkbox_constraints">'
1296 . __('Add constraints') . '</label><br />';
1297 } // endif
1299 $html_output .= '<br />';
1301 if (! PMA_DRIZZLE) {
1302 if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
1303 && $GLOBALS['is_reload_priv']
1305 $html_output .= '<input type="checkbox" name="adjust_privileges" '
1306 . 'value="1" id="checkbox_adjust_privileges" checked="checked" />';
1307 } else {
1308 $html_output .= '<input type="checkbox" name="adjust_privileges" '
1309 . 'value="1" id="checkbox_adjust_privileges" title="' . __(
1310 'You don\'t have sufficient privileges to perform this '
1311 . 'operation; Please refer to the documentation for more details'
1313 . '" disabled/>';
1315 $html_output .= '<label for="checkbox_adjust_privileges">'
1316 . __('Adjust privileges') . PMA_Util::showDocu('faq', 'faq6-39')
1317 . '</label><br />';
1320 if (isset($_COOKIE['pma_switch_to_new'])
1321 && $_COOKIE['pma_switch_to_new'] == 'true'
1323 $pma_switch_to_new = 'true';
1326 $html_output .= '<input type="checkbox" name="switch_to_new" value="true"'
1327 . 'id="checkbox_switch"'
1328 . ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true')
1329 ? ' checked="checked"'
1330 : '' . '/>');
1331 $html_output .= '<label for="checkbox_switch">'
1332 . __('Switch to copied table') . '</label>'
1333 . '</fieldset>';
1335 $html_output .= '<fieldset class="tblFooters">'
1336 . '<input type="submit" name="submit_copy" value="' . __('Go') . '" />'
1337 . '</fieldset>'
1338 . '</form>'
1339 . '</div>';
1341 return $html_output;
1345 * Get HTML snippet for table maintenance
1347 * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
1348 * @param boolean $is_innodb whether innodb or not
1349 * @param boolean $is_berkeleydb whether berkeleydb or not
1350 * @param array $url_params array of URL parameters
1352 * @return string $html_output
1354 function PMA_getHtmlForTableMaintenance(
1355 $is_myisam_or_aria, $is_innodb, $is_berkeleydb, $url_params
1357 $html_output = '<div class="operations_half_width">';
1358 $html_output .= '<fieldset>'
1359 . '<legend>' . __('Table maintenance') . '</legend>';
1360 $html_output .= '<ul id="tbl_maintenance">';
1362 // Note: BERKELEY (BDB) is no longer supported, starting with MySQL 5.1
1363 $html_output .= PMA_getListofMaintainActionLink(
1364 $is_myisam_or_aria, $is_innodb, $url_params, $is_berkeleydb
1367 $html_output .= '</ul>'
1368 . '</fieldset>'
1369 . '</div>';
1371 return $html_output;
1375 * Get HTML 'li' having a link of maintain action
1377 * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
1378 * @param boolean $is_innodb whether innodb or not
1379 * @param array $url_params array of URL parameters
1380 * @param boolean $is_berkeleydb whether berkeleydb or not
1382 * @return string $html_output
1384 function PMA_getListofMaintainActionLink($is_myisam_or_aria,
1385 $is_innodb, $url_params, $is_berkeleydb
1387 $html_output = '';
1389 // analyze table
1390 if ($is_innodb || $is_myisam_or_aria || $is_berkeleydb) {
1391 $params = array(
1392 'sql_query' => 'ANALYZE TABLE '
1393 . PMA_Util::backquote($GLOBALS['table']),
1394 'table_maintenance' => 'Go',
1396 $html_output .= PMA_getMaintainActionlink(
1397 __('Analyze table'),
1398 $params,
1399 $url_params,
1400 'ANALYZE_TABLE'
1404 // check table
1405 if ($is_myisam_or_aria || $is_innodb) {
1406 $params = array(
1407 'sql_query' => 'CHECK TABLE '
1408 . PMA_Util::backquote($GLOBALS['table']),
1409 'table_maintenance' => 'Go',
1411 $html_output .= PMA_getMaintainActionlink(
1412 __('Check table'),
1413 $params,
1414 $url_params,
1415 'CHECK_TABLE'
1419 // checksum table
1420 if (! PMA_DRIZZLE) {
1421 $params = array(
1422 'sql_query' => 'CHECKSUM TABLE '
1423 . PMA_Util::backquote($GLOBALS['table']),
1424 'table_maintenance' => 'Go',
1426 $html_output .= PMA_getMaintainActionlink(
1427 __('Checksum table'),
1428 $params,
1429 $url_params,
1430 'CHECKSUM_TABLE'
1434 // defragment table
1435 if ($is_innodb) {
1436 $params = array(
1437 'sql_query' => 'ALTER TABLE '
1438 . PMA_Util::backquote($GLOBALS['table'])
1439 . ' ENGINE = InnoDB;'
1441 $html_output .= PMA_getMaintainActionlink(
1442 __('Defragment table'),
1443 $params,
1444 $url_params,
1445 'InnoDB_File_Defragmenting'
1449 // flush table
1450 $params = array(
1451 'sql_query' => 'FLUSH TABLE '
1452 . PMA_Util::backquote($GLOBALS['table']),
1453 'message_to_show' => sprintf(
1454 __('Table %s has been flushed.'),
1455 htmlspecialchars($GLOBALS['table'])
1457 'reload' => 1,
1459 $html_output .= PMA_getMaintainActionlink(
1460 __('Flush the table (FLUSH)'),
1461 $params,
1462 $url_params,
1463 'FLUSH'
1466 // optimize table
1467 if (($is_myisam_or_aria || $is_innodb || $is_berkeleydb)
1468 && !PMA_DRIZZLE
1470 $params = array(
1471 'sql_query' => 'OPTIMIZE TABLE '
1472 . PMA_Util::backquote($GLOBALS['table']),
1473 'table_maintenance' => 'Go',
1475 $html_output .= PMA_getMaintainActionlink(
1476 __('Optimize table'),
1477 $params,
1478 $url_params,
1479 'OPTIMIZE_TABLE'
1483 // repair table
1484 if ($is_myisam_or_aria && !PMA_DRIZZLE) {
1485 $params = array(
1486 'sql_query' => 'REPAIR TABLE '
1487 . PMA_Util::backquote($GLOBALS['table']),
1488 'table_maintenance' => 'Go',
1490 $html_output .= PMA_getMaintainActionlink(
1491 __('Repair table'),
1492 $params,
1493 $url_params,
1494 'REPAIR_TABLE'
1498 return $html_output;
1502 * Get maintain action HTML link
1504 * @param string $action_message action message
1505 * @param array $params url parameters array
1506 * @param array $url_params additional url parameters
1507 * @param string $link contains name of page/anchor that is being linked
1509 * @return string $html_output
1511 function PMA_getMaintainActionlink($action_message, $params, $url_params, $link)
1513 return '<li>'
1514 . '<a class="maintain_action ajax" '
1515 . 'href="sql.php'
1516 . PMA_URL_getCommon(array_merge($url_params, $params)) . '">'
1517 . $action_message
1518 . '</a>'
1519 . PMA_Util::showMySQLDocu($link)
1520 . '</li>';
1524 * Get HTML for Delete data or table (truncate table, drop table)
1526 * @param array $truncate_table_url_params url parameter array for truncate table
1527 * @param array $dropTableUrlParams url parameter array for drop table
1529 * @return string $html_output
1531 function PMA_getHtmlForDeleteDataOrTable(
1532 $truncate_table_url_params,
1533 $dropTableUrlParams
1535 $html_output = '<div class="operations_half_width">'
1536 . '<fieldset class="caution">'
1537 . '<legend>' . __('Delete data or table') . '</legend>';
1539 $html_output .= '<ul>';
1541 if (! empty($truncate_table_url_params)) {
1542 $html_output .= PMA_getDeleteDataOrTablelink(
1543 $truncate_table_url_params,
1544 'TRUNCATE_TABLE',
1545 __('Empty the table (TRUNCATE)'),
1546 'truncate_tbl_anchor'
1549 if (!empty($dropTableUrlParams)) {
1550 $html_output .= PMA_getDeleteDataOrTablelink(
1551 $dropTableUrlParams,
1552 'DROP_TABLE',
1553 __('Delete the table (DROP)'),
1554 'drop_tbl_anchor'
1557 $html_output .= '</ul></fieldset></div>';
1559 return $html_output;
1563 * Get the HTML link for Truncate table, Drop table and Drop db
1565 * @param array $url_params url parameter array for delete data or table
1566 * @param string $syntax TRUNCATE_TABLE or DROP_TABLE or DROP_DATABASE
1567 * @param string $link link to be shown
1568 * @param string $htmlId id of the link
1570 * @return String html output
1572 function PMA_getDeleteDataOrTablelink($url_params, $syntax, $link, $htmlId)
1574 return '<li><a '
1575 . 'href="sql.php' . PMA_URL_getCommon($url_params) . '"'
1576 . ' id="' . $htmlId . '" class="ajax">'
1577 . $link . '</a>'
1578 . PMA_Util::showMySQLDocu($syntax)
1579 . '</li>';
1583 * Get HTML snippet for partition maintenance
1585 * @param array $partition_names array of partition names for a specific db/table
1586 * @param array $url_params url parameters
1588 * @return string $html_output
1590 function PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
1592 $choices = array(
1593 'ANALYZE' => __('Analyze'),
1594 'CHECK' => __('Check'),
1595 'OPTIMIZE' => __('Optimize'),
1596 'REBUILD' => __('Rebuild'),
1597 'REPAIR' => __('Repair'),
1598 'TRUNCATE' => __('Truncate')
1601 $partition_method = PMA_Partition::getPartitionMethod(
1602 $GLOBALS['db'], $GLOBALS['table']
1604 // add COALESCE or DROP option to choices array depeding on Partition method
1605 if ($partition_method == 'RANGE' || $partition_method == 'LIST') {
1606 $choices['DROP'] = __('Drop');
1607 } else {
1608 $choices['COALESCE'] = __('Coalesce');
1611 $html_output = '<div class="operations_half_width">'
1612 . '<form id="partitionsForm" class="ajax" '
1613 . 'method="post" action="tbl_operations.php" >'
1614 . PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
1615 . '<fieldset>'
1616 . '<legend>'
1617 . __('Partition maintenance')
1618 . PMA_Util::showMySQLDocu('partitioning_maintenance')
1619 . '</legend>';
1621 $html_select = '<select id="partition_name" name="partition_name[]"'
1622 . ' multiple="multiple" required="required">' . "\n";
1623 $first = true;
1624 foreach ($partition_names as $one_partition) {
1625 $one_partition = htmlspecialchars($one_partition);
1626 $html_select .= '<option value="' . $one_partition . '"';
1627 if ($first) {
1628 $html_select .= ' selected="selected"';
1629 $first = false;
1631 $html_select .= '>' . $one_partition . '</option>' . "\n";
1633 $html_select .= '</select>' . "\n";
1634 $html_output .= sprintf(__('Partition %s'), $html_select);
1636 $html_output .= '<div class="clearfloat" />';
1637 $html_output .= PMA_Util::getRadioFields(
1638 'partition_operation', $choices, 'ANALYZE', false, true, 'floatleft'
1640 $this_url_params = array_merge(
1641 $url_params,
1642 array(
1643 'sql_query' => 'ALTER TABLE '
1644 . PMA_Util::backquote($GLOBALS['table'])
1645 . ' REMOVE PARTITIONING;'
1648 $html_output .= '<div class="clearfloat" /><br />';
1650 $html_output .= '<a href="sql.php'
1651 . PMA_URL_getCommon($this_url_params) . '">'
1652 . __('Remove partitioning') . '</a>';
1654 $html_output .= '</fieldset>'
1655 . '<fieldset class="tblFooters">'
1656 . '<input type="hidden" name="submit_partition" value="1">'
1657 . '<input type="submit" value="' . __('Go') . '" />'
1658 . '</fieldset>'
1659 . '</form>'
1660 . '</div>';
1662 return $html_output;
1666 * Get the HTML for Referential Integrity check
1668 * @param array $foreign all Relations to foreign tables for a given table
1669 * or optionally a given column in a table
1670 * @param array $url_params array of url parameters
1672 * @return string $html_output
1674 function PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
1676 $html_output = '<div class="operations_half_width">'
1677 . '<fieldset>'
1678 . '<legend>' . __('Check referential integrity:') . '</legend>';
1680 $html_output .= '<ul>';
1682 foreach ($foreign as $master => $arr) {
1683 $join_query = 'SELECT '
1684 . PMA_Util::backquote($GLOBALS['table']) . '.*'
1685 . ' FROM ' . PMA_Util::backquote($GLOBALS['table'])
1686 . ' LEFT JOIN '
1687 . PMA_Util::backquote($arr['foreign_db'])
1688 . '.'
1689 . PMA_Util::backquote($arr['foreign_table']);
1690 if ($arr['foreign_table'] == $GLOBALS['table']) {
1691 $foreign_table = $GLOBALS['table'] . '1';
1692 $join_query .= ' AS ' . PMA_Util::backquote($foreign_table);
1693 } else {
1694 $foreign_table = $arr['foreign_table'];
1696 $join_query .= ' ON '
1697 . PMA_Util::backquote($GLOBALS['table']) . '.'
1698 . PMA_Util::backquote($master)
1699 . ' = '
1700 . PMA_Util::backquote($arr['foreign_db'])
1701 . '.'
1702 . PMA_Util::backquote($foreign_table) . '.'
1703 . PMA_Util::backquote($arr['foreign_field'])
1704 . ' WHERE '
1705 . PMA_Util::backquote($arr['foreign_db'])
1706 . '.'
1707 . PMA_Util::backquote($foreign_table) . '.'
1708 . PMA_Util::backquote($arr['foreign_field'])
1709 . ' IS NULL AND '
1710 . PMA_Util::backquote($GLOBALS['table']) . '.'
1711 . PMA_Util::backquote($master)
1712 . ' IS NOT NULL';
1713 $this_url_params = array_merge(
1714 $url_params,
1715 array('sql_query' => $join_query)
1718 $html_output .= '<li>'
1719 . '<a href="sql.php'
1720 . PMA_URL_getCommon($this_url_params)
1721 . '">'
1722 . $master . '&nbsp;->&nbsp;' . $arr['foreign_db'] . '.'
1723 . $arr['foreign_table'] . '.' . $arr['foreign_field']
1724 . '</a></li>' . "\n";
1725 } // foreach $foreign
1726 $html_output .= '</ul></fieldset></div>';
1728 return $html_output;
1732 * Reorder table based on request params
1734 * @return array SQL query and result
1736 function PMA_getQueryAndResultForReorderingTable()
1738 $sql_query = 'ALTER TABLE '
1739 . PMA_Util::backquote($GLOBALS['table'])
1740 . ' ORDER BY '
1741 . PMA_Util::backquote(urldecode($_REQUEST['order_field']));
1742 if (isset($_REQUEST['order_order'])
1743 && $_REQUEST['order_order'] === 'desc'
1745 $sql_query .= ' DESC';
1747 $sql_query .= ';';
1748 $result = $GLOBALS['dbi']->query($sql_query);
1750 return array($sql_query, $result);
1754 * Get table alters array
1756 * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
1757 * @param boolean $is_isam whether ISAM or not
1758 * @param string $pack_keys pack keys
1759 * @param string $checksum value of checksum
1760 * @param boolean $is_aria whether ARIA or not
1761 * @param string $page_checksum value of page checksum
1762 * @param string $delay_key_write delay key write
1763 * @param boolean $is_innodb whether INNODB or not
1764 * @param boolean $is_pbxt whether PBXT or not
1765 * @param string $row_format row format
1766 * @param string $newTblStorageEngine table storage engine
1767 * @param string $transactional value of transactional
1768 * @param string $tbl_collation collation of the table
1770 * @return array $table_alters
1772 function PMA_getTableAltersArray($is_myisam_or_aria, $is_isam, $pack_keys,
1773 $checksum, $is_aria, $page_checksum, $delay_key_write, $is_innodb,
1774 $is_pbxt, $row_format, $newTblStorageEngine, $transactional, $tbl_collation
1776 global $auto_increment;
1778 $table_alters = array();
1780 if (isset($_REQUEST['comment'])
1781 && urldecode($_REQUEST['prev_comment']) !== $_REQUEST['comment']
1783 $table_alters[] = 'COMMENT = \''
1784 . PMA_Util::sqlAddSlashes($_REQUEST['comment']) . '\'';
1787 if (! empty($newTblStorageEngine)
1788 && /*overload*/mb_strtolower($newTblStorageEngine) !== /*overload*/mb_strtolower($GLOBALS['tbl_storage_engine'])
1790 $table_alters[] = 'ENGINE = ' . $newTblStorageEngine;
1792 if (! empty($_REQUEST['tbl_collation'])
1793 && $_REQUEST['tbl_collation'] !== $tbl_collation
1795 $table_alters[] = 'DEFAULT '
1796 . PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
1799 if (($is_myisam_or_aria || $is_isam)
1800 && isset($_REQUEST['new_pack_keys'])
1801 && $_REQUEST['new_pack_keys'] != (string)$pack_keys
1803 $table_alters[] = 'pack_keys = ' . $_REQUEST['new_pack_keys'];
1806 $_REQUEST['new_checksum'] = empty($_REQUEST['new_checksum']) ? '0' : '1';
1807 if ($is_myisam_or_aria
1808 && $_REQUEST['new_checksum'] !== $checksum
1810 $table_alters[] = 'checksum = ' . $_REQUEST['new_checksum'];
1813 $_REQUEST['new_transactional']
1814 = empty($_REQUEST['new_transactional']) ? '0' : '1';
1815 if ($is_aria
1816 && $_REQUEST['new_transactional'] !== $transactional
1818 $table_alters[] = 'TRANSACTIONAL = ' . $_REQUEST['new_transactional'];
1821 $_REQUEST['new_page_checksum']
1822 = empty($_REQUEST['new_page_checksum']) ? '0' : '1';
1823 if ($is_aria
1824 && $_REQUEST['new_page_checksum'] !== $page_checksum
1826 $table_alters[] = 'PAGE_CHECKSUM = ' . $_REQUEST['new_page_checksum'];
1829 $_REQUEST['new_delay_key_write']
1830 = empty($_REQUEST['new_delay_key_write']) ? '0' : '1';
1831 if ($is_myisam_or_aria
1832 && $_REQUEST['new_delay_key_write'] !== $delay_key_write
1834 $table_alters[] = 'delay_key_write = ' . $_REQUEST['new_delay_key_write'];
1837 if (($is_myisam_or_aria || $is_innodb || $is_pbxt)
1838 && ! empty($_REQUEST['new_auto_increment'])
1839 && (! isset($auto_increment)
1840 || $_REQUEST['new_auto_increment'] !== $auto_increment)
1842 $table_alters[] = 'auto_increment = '
1843 . PMA_Util::sqlAddSlashes($_REQUEST['new_auto_increment']);
1846 if (! empty($_REQUEST['new_row_format'])) {
1847 $newRowFormat = $_REQUEST['new_row_format'];
1848 $newRowFormatLower = /*overload*/mb_strtolower($newRowFormat);
1849 if (($is_myisam_or_aria || $is_innodb || $is_pbxt)
1850 && (!/*overload*/mb_strlen($row_format)
1851 || $newRowFormatLower !== /*overload*/mb_strtolower($row_format))
1853 $table_alters[] = 'ROW_FORMAT = '
1854 . PMA_Util::sqlAddSlashes($newRowFormat);
1858 return $table_alters;
1862 * set initial value of the set of variables, based on the current table engine
1864 * @param string $tbl_storage_engine table storage engine
1866 * @return array ($is_myisam_or_aria, $is_innodb, $is_isam,
1867 * $is_berkeleydb, $is_aria, $is_pbxt)
1869 function PMA_setGlobalVariablesForEngine($tbl_storage_engine)
1871 $upperTblStorEngine = $tbl_storage_engine;
1873 //Options that apply to MYISAM usually apply to ARIA
1874 $is_myisam_or_aria = ($upperTblStorEngine == 'MYISAM'
1875 || $upperTblStorEngine == 'ARIA'
1876 || $upperTblStorEngine == 'MARIA'
1878 $is_aria = ($upperTblStorEngine == 'ARIA');
1880 $is_isam = ($upperTblStorEngine == 'ISAM');
1881 $is_innodb = ($upperTblStorEngine == 'INNODB');
1882 $is_berkeleydb = ($upperTblStorEngine == 'BERKELEYDB');
1883 $is_pbxt = ($upperTblStorEngine == 'PBXT');
1885 return array(
1886 $is_myisam_or_aria, $is_innodb, $is_isam,
1887 $is_berkeleydb, $is_aria, $is_pbxt
1892 * Get warning messages array
1894 * @return array $warning_messages
1896 function PMA_getWarningMessagesArray()
1898 $warning_messages = array();
1899 foreach ($GLOBALS['dbi']->getWarnings() as $warning) {
1900 // In MariaDB 5.1.44, when altering a table from Maria to MyISAM
1901 // and if TRANSACTIONAL was set, the system reports an error;
1902 // I discussed with a Maria developer and he agrees that this
1903 // should not be reported with a Level of Error, so here
1904 // I just ignore it. But there are other 1478 messages
1905 // that it's better to show.
1906 if (! ($_REQUEST['new_tbl_storage_engine'] == 'MyISAM'
1907 && $warning['Code'] == '1478'
1908 && $warning['Level'] == 'Error')
1910 $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
1911 . ' ' . $warning['Message'];
1914 return $warning_messages;
1918 * Get SQL query and result after ran this SQL query for a partition operation
1919 * has been requested by the user
1921 * @return array $sql_query, $result
1923 function PMA_getQueryAndResultForPartition()
1925 $sql_query = 'ALTER TABLE '
1926 . PMA_Util::backquote($GLOBALS['table']) . ' '
1927 . $_REQUEST['partition_operation']
1928 . ' PARTITION ';
1930 if ($_REQUEST['partition_operation'] == 'COALESCE') {
1931 $sql_query .= count($_REQUEST['partition_name']);
1932 } else {
1933 $sql_query .= implode(', ', $_REQUEST['partition_name']) . ';';
1936 $result = $GLOBALS['dbi']->query($sql_query);
1938 return array($sql_query, $result);
1942 * Adjust the privileges after renaming/moving a table
1944 * @param string $oldDb Database name before table renaming/moving table
1945 * @param string $oldTable Table name before table renaming/moving table
1946 * @param string $newDb Database name after table renaming/ moving table
1947 * @param string $newTable Table name after table renaming/moving table
1949 * @return void
1951 function PMA_AdjustPrivileges_renameOrMoveTable($oldDb, $oldTable, $newDb, $newTable)
1953 if (! PMA_DRIZZLE) {
1954 if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
1955 && $GLOBALS['is_reload_priv']
1957 $GLOBALS['dbi']->selectDb('mysql');
1959 // For table specific privileges
1960 $query_table_specific = 'UPDATE ' . PMA_Util::backquote('tables_priv')
1961 . 'SET Db = "' . $newDb . '", Table_name = "' . $newTable
1962 . '" where Db = "' . $oldDb . '" AND Table_name = "' . $oldTable
1963 . '";';
1964 $GLOBALS['dbi']->query($query_table_specific);
1966 // For column specific privileges
1967 $query_col_specific = 'UPDATE ' . PMA_Util::backquote('columns_priv')
1968 . 'SET Db = "' . $newDb . '", Table_name = "' . $newTable
1969 . '" where Db = "' . $oldDb . '" AND Table_name = "' . $oldTable
1970 . '";';
1971 $GLOBALS['dbi']->query($query_col_specific);
1973 // Finally FLUSH the new privileges
1974 $flush_query = "FLUSH PRIVILEGES;";
1975 $GLOBALS['dbi']->query($flush_query);
1981 * Adjust the privileges after copying a table
1983 * @param string $oldDb Database name before table copying
1984 * @param string $oldTable Table name before table copying
1985 * @param string $newDb Database name after table copying
1986 * @param string $newTable Table name after table copying
1988 * @return void
1990 function PMA_AdjustPrivileges_copyTable($oldDb, $oldTable, $newDb, $newTable)
1992 if (! PMA_DRIZZLE) {
1993 if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
1994 && $GLOBALS['is_reload_priv']
1996 $GLOBALS['dbi']->selectDb('mysql');
1998 // For Table Specific privileges
1999 $query_table_specific_old = 'SELECT * FROM '
2000 . PMA_Util::backquote('tables_priv') . ' where '
2001 . 'Db = "' . $oldDb . '" AND Table_name = "' . $oldTable . '";';
2003 $old_privs_table = $GLOBALS['dbi']->fetchResult(
2004 $query_table_specific_old,
2008 foreach ($old_privs_table as $old_priv) {
2009 $newDb_table_privs_query = 'INSERT INTO '
2010 . PMA_Util::backquote('tables_priv') . ' VALUES("'
2011 . $old_priv[0] . '", "' . $newDb . '", "' . $old_priv[2] . '", "'
2012 . $newTable . '", "' . $old_priv[4] . '", "' . $old_priv[5]
2013 . '", "' . $old_priv[6] . '", "' . $old_priv[7] . '");';
2015 $GLOBALS['dbi']->query($newDb_table_privs_query);
2018 // For Column Specific privileges
2019 $query_col_specific_old = 'SELECT * FROM '
2020 . PMA_Util::backquote('columns_priv') . ' WHERE '
2021 . 'Db = "' . $oldDb . '" AND Table_name = "' . $oldTable . '";';
2023 $old_privs_col = $GLOBALS['dbi']->fetchResult(
2024 $query_col_specific_old,
2028 foreach ($old_privs_col as $old_priv) {
2029 $newDb_col_privs_query = 'INSERT INTO '
2030 . PMA_Util::backquote('columns_priv') . ' VALUES("'
2031 . $old_priv[0] . '", "' . $newDb . '", "' . $old_priv[2] . '", "'
2032 . $newTable . '", "' . $old_priv[4] . '", "' . $old_priv[5]
2033 . '", "' . $old_priv[6] . '");';
2035 $GLOBALS['dbi']->query($newDb_col_privs_query);
2038 // Finally FLUSH the new privileges
2039 $flush_query = "FLUSH PRIVILEGES;";
2040 $GLOBALS['dbi']->query($flush_query);
2046 * Change all collations and character sets of all columns in table
2048 * @param string $db Database name
2049 * @param string $table Table name
2050 * @param string $tbl_collation Collation Name
2052 * @return void
2054 function PMA_changeAllColumnsCollation($db, $table, $tbl_collation)
2056 $GLOBALS['dbi']->selectDb($db);
2058 $change_all_collations_query = 'ALTER TABLE '
2059 . PMA_Util::backquote($table)
2060 . ' CONVERT TO';
2062 list($charset) = explode('_', $tbl_collation);
2064 $change_all_collations_query .= ' CHARACTER SET ' . $charset
2065 . ($charset == $tbl_collation ? '' : ' COLLATE ' . $tbl_collation);
2067 $GLOBALS['dbi']->query($change_all_collations_query);
2071 * Move or copy a table
2073 * @param string $db current database name
2074 * @param string $table current table name
2076 * @return void
2078 function PMA_moveOrCopyTable($db, $table)
2081 * Selects the database to work with
2083 $GLOBALS['dbi']->selectDb($db);
2086 * $_REQUEST['target_db'] could be empty in case we came from an input field
2087 * (when there are many databases, no drop-down)
2089 if (empty($_REQUEST['target_db'])) {
2090 $_REQUEST['target_db'] = $db;
2094 * A target table name has been sent to this script -> do the work
2096 if (PMA_isValid($_REQUEST['new_name'])) {
2097 if ($db == $_REQUEST['target_db'] && $table == $_REQUEST['new_name']) {
2098 if (isset($_REQUEST['submit_move'])) {
2099 $message = PMA_Message::error(__('Can\'t move table to same one!'));
2100 } else {
2101 $message = PMA_Message::error(__('Can\'t copy table to same one!'));
2103 } else {
2104 PMA_Table::moveCopy(
2105 $db, $table, $_REQUEST['target_db'], $_REQUEST['new_name'],
2106 $_REQUEST['what'], isset($_REQUEST['submit_move']), 'one_table'
2109 if (isset($_REQUEST['adjust_privileges'])
2110 && ! empty($_REQUEST['adjust_privileges'])
2112 if (isset($_REQUEST['submit_move'])) {
2113 PMA_AdjustPrivileges_renameOrMoveTable(
2114 $db, $table, $_REQUEST['target_db'], $_REQUEST['new_name']
2116 } else {
2117 PMA_AdjustPrivileges_copyTable(
2118 $db, $table, $_REQUEST['target_db'], $_REQUEST['new_name']
2122 if (isset($_REQUEST['submit_move'])) {
2123 $message = PMA_Message::success(
2125 'Table %s has been moved to %s. Privileges have been '
2126 . 'adjusted.'
2129 } else {
2130 $message = PMA_Message::success(
2132 'Table %s has been copied to %s. Privileges have been '
2133 . 'adjusted.'
2138 } else {
2139 if (isset($_REQUEST['submit_move'])) {
2140 $message = PMA_Message::success(
2141 __('Table %s has been moved to %s.')
2143 } else {
2144 $message = PMA_Message::success(
2145 __('Table %s has been copied to %s.')
2150 $old = PMA_Util::backquote($db) . '.'
2151 . PMA_Util::backquote($table);
2152 $message->addParam($old);
2153 $new = PMA_Util::backquote($_REQUEST['target_db']) . '.'
2154 . PMA_Util::backquote($_REQUEST['new_name']);
2155 $message->addParam($new);
2157 /* Check: Work on new table or on old table? */
2158 if (isset($_REQUEST['submit_move'])
2159 || PMA_isValid($_REQUEST['switch_to_new'])
2163 } else {
2165 * No new name for the table!
2167 $message = PMA_Message::error(__('The table name is empty!'));
2170 if ($GLOBALS['is_ajax_request'] == true) {
2171 $response = PMA_Response::getInstance();
2172 $response->addJSON('message', $message);
2173 if ($message->isSuccess()) {
2174 $response->addJSON('db', $GLOBALS['db']);
2175 } else {
2176 $response->isSuccess(false);
2178 exit;