From 0874a9cbbc1942f7c15217b41cda1a12191e5346 Mon Sep 17 00:00:00 2001 From: xmujay Date: Sun, 11 Aug 2013 17:53:17 +0800 Subject: [PATCH] refactor mult_submits --- libraries/mult_submits.inc.php | 141 ++++++-------------------- libraries/mult_submits.lib.php | 177 +++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+), 110 deletions(-) create mode 100644 libraries/mult_submits.lib.php diff --git a/libraries/mult_submits.inc.php b/libraries/mult_submits.inc.php index a3fc79c8bb..b1d1b5c2d8 100644 --- a/libraries/mult_submits.inc.php +++ b/libraries/mult_submits.inc.php @@ -12,6 +12,7 @@ if (! defined('PHPMYADMIN')) { require_once 'libraries/transformations.lib.php'; require_once 'libraries/bookmark.lib.php'; require_once 'libraries/sql.lib.php'; +require_once 'libraries/mult_submits.lib.php'; $request_params = array( 'clause_is_unique', @@ -95,18 +96,7 @@ if (! empty($submit_mult) break; case 'primary': // Gets table primary key - $GLOBALS['dbi']->selectDb($db); - $result = $GLOBALS['dbi']->query( - 'SHOW KEYS FROM ' . PMA_Util::backquote($table) . ';' - ); - $primary = ''; - while ($row = $GLOBALS['dbi']->fetchAssoc($result)) { - // Backups the list of primary keys - if ($row['Key_name'] == 'PRIMARY') { - $primary .= $row['Column_name'] . ', '; - } - } // end while - $GLOBALS['dbi']->freeResult($result); + $primary = PMA_getKeyForTablePrimary($db, $table); if (empty($primary)) { // no primary key, so we can safely create new unset($submit_mult); @@ -181,7 +171,8 @@ if (!empty($submit_mult) && !empty($what)) { foreach ($selected as $idx => $sval) { switch ($what) { case 'row_delete': - $full_query .= 'DELETE FROM ' . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table) + $full_query .= 'DELETE FROM ' . PMA_Util::backquote($db) + . '.' . PMA_Util::backquote($table) . ' WHERE ' . urldecode($sval) . ' LIMIT 1' . ';
'; break; @@ -285,97 +276,11 @@ if (!empty($submit_mult) && !empty($what)) { } } if ($what == 'replace_prefix_tbl' || $what == 'copy_tbl_change_prefix') { - echo '
'; - echo PMA_URL_getHiddenInputs($_url_params); - echo '
'; - echo ''; - if ($what == 'replace_prefix_tbl') { - echo __('Replace table prefix:'); - } else { - echo __('Copy table with prefix:'); - } - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
' . __('From') . ''; - echo ''; - echo '
' . __('To') . ''; - echo ''; - echo '
'; - echo '
'; - echo '
'; - echo ''; - echo ''; - echo '
'; - echo '
'; + echo PMA_getHtmlForReplacePrefixTable($what, $action, $_url_params); } elseif ($what == 'add_prefix_tbl') { - echo '
'; - echo PMA_URL_getHiddenInputs($_url_params); - echo '
'; - echo '' . __('Add table prefix:') . ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo '
' . __('Add prefix') . ''; - echo ''; - echo '
'; - echo '
'; - echo '
'; - echo ''; - echo ''; - echo '
'; - echo '
'; + echo PMA_getHtmlForAddPrefixTable($action, $_url_params); } else { - echo '
'; - echo ''; - if ($what == 'drop_db') { - echo __('You are about to DESTROY a complete database!') . ' '; - } - echo __('Do you really want to execute the following query?'); - echo ''; - echo '' . $full_query . ''; - echo '
'; - echo '
'; - echo '
'; - echo PMA_URL_getHiddenInputs($_url_params); - // Display option to disable foreign key checks while dropping tables - if ($what == 'drop_tbl') { - echo '
'; - echo ''; - echo __('Foreign key check:'); - echo ''; - echo ''; - echo 'fetchValue('SHOW VARIABLES LIKE \'foreign_key_checks\';', 0, 1) == 'ON'; - if ($default_fk_check_value) { - echo ' checked="checked"'; - } - echo '/>'; - echo ''; - echo ($default_fk_check_value) ? __('(Enabled)') : __('(Disabled)'); - echo ''; - echo '
'; - } - echo ''; - echo ''; - echo '
'; - - echo '
'; - echo PMA_URL_getHiddenInputs($_url_params); - echo ''; - echo ''; - echo '
'; - echo '
'; + echo PMA_getHtmlForOtherActions($what, $action, $_url_params, $full_query); } exit; @@ -479,37 +384,51 @@ if (!empty($submit_mult) && !empty($what)) { case 'drop_fld': PMA_relationsCleanupColumn($db, $table, $selected[$i]); - $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_Util::backquote($table) : ',') + $sql_query .= (empty($sql_query) + ? 'ALTER TABLE ' . PMA_Util::backquote($table) + : ',') . ' DROP ' . PMA_Util::backquote($selected[$i]) . (($i == $selected_cnt-1) ? ';' : ''); break; case 'primary_fld': - $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_Util::backquote($table) . (empty($primary) ? '' : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' : ', ') + $sql_query .= (empty($sql_query) + ? 'ALTER TABLE ' . PMA_Util::backquote($table) . (empty($primary) + ? '' + : ' DROP PRIMARY KEY,') . ' ADD PRIMARY KEY( ' + : ', ') . PMA_Util::backquote($selected[$i]) . (($i == $selected_cnt-1) ? ');' : ''); break; case 'index_fld': - $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD INDEX( ' : ', ') + $sql_query .= (empty($sql_query) + ? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD INDEX( ' + : ', ') . PMA_Util::backquote($selected[$i]) . (($i == $selected_cnt-1) ? ');' : ''); break; case 'unique_fld': - $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD UNIQUE( ' : ', ') + $sql_query .= (empty($sql_query) + ? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD UNIQUE( ' + : ', ') . PMA_Util::backquote($selected[$i]) . (($i == $selected_cnt-1) ? ');' : ''); break; case 'spatial_fld': - $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD SPATIAL( ' : ', ') + $sql_query .= (empty($sql_query) + ? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD SPATIAL( ' + : ', ') . PMA_Util::backquote($selected[$i]) . (($i == $selected_cnt-1) ? ');' : ''); break; case 'fulltext_fld': - $sql_query .= (empty($sql_query) ? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD FULLTEXT( ' : ', ') + $sql_query .= (empty($sql_query) + ? 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ADD FULLTEXT( ' + : ', ') . PMA_Util::backquote($selected[$i]) . (($i == $selected_cnt-1) ? ');' : ''); break; @@ -578,7 +497,9 @@ if (!empty($submit_mult) && !empty($what)) { } // end for if ($query_type == 'drop_tbl') { - $default_fk_check_value = $GLOBALS['dbi']->fetchValue('SHOW VARIABLES LIKE \'foreign_key_checks\';', 0, 1) == 'ON'; + $default_fk_check_value = $GLOBALS['dbi']->fetchValue( + 'SHOW VARIABLES LIKE \'foreign_key_checks\';', 0, 1 + ) == 'ON'; if (!empty($sql_query)) { $sql_query .= ';'; } elseif (!empty($sql_query_views)) { @@ -591,7 +512,7 @@ if (!empty($submit_mult) && !empty($what)) { /** * Parse and analyze the query */ - require_once 'libraries/parse_analyze.inc.php'; + include_once 'libraries/parse_analyze.inc.php'; PMA_executeQueryAndSendQueryResponse( $analyzed_sql_results, false, $db, $table, null, null, null, diff --git a/libraries/mult_submits.lib.php b/libraries/mult_submits.lib.php new file mode 100644 index 0000000000..139e74e3e0 --- /dev/null +++ b/libraries/mult_submits.lib.php @@ -0,0 +1,177 @@ +selectDb($db); + $result = $GLOBALS['dbi']->query( + 'SHOW KEYS FROM ' . PMA_Util::backquote($table) . ';' + ); + $primary = ''; + while ($row = $GLOBALS['dbi']->fetchAssoc($result)) { + // Backups the list of primary keys + if ($row['Key_name'] == 'PRIMARY') { + $primary .= $row['Column_name'] . ', '; + } + } // end while + $GLOBALS['dbi']->freeResult($result); + + return $primary; +} + +/** + * Gets HTML for replace_prefix_tbl or copy_tbl_change_prefix + * + * @param string $what mult_submit type + * @param string $action action type + * @param array $_url_params URL params + * + * @return string + */ +function PMA_getHtmlForReplacePrefixTable($what, $action, $_url_params) +{ + $html = '
'; + $html .= PMA_URL_getHiddenInputs($_url_params); + $html .= '
'; + $html .= ''; + if ($what == 'replace_prefix_tbl') { + $html .= __('Replace table prefix:'); + } else { + $html .= __('Copy table with prefix:'); + } + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= '
' . __('From') . ''; + $html .= ''; + $html .= '
' . __('To') . ''; + $html .= ''; + $html .= '
'; + $html .= '
'; + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= '
'; + $html .= '
'; + + return $html; +} + +/** + * Gets HTML for add_prefix_tbl + * + * @param string $action action type + * @param array $_url_params URL params + * + * @return string + */ +function PMA_getHtmlForAddPrefixTable($action, $_url_params) +{ + $html = '
'; + $html .= PMA_URL_getHiddenInputs($_url_params); + $html .= '
'; + $html .= '' . __('Add table prefix:') . ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= '
' . __('Add prefix') . ''; + $html .= ''; + $html .= '
'; + $html .= '
'; + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= '
'; + $html .= '
'; + + return $html; +} + +/** + * Gets HTML for other mult_submits actions + * + * @param string $what mult_submit type + * @param string $action action type + * @param array $_url_params URL params + * @param array $full_query full sql query string + * + * @return string + */ +function PMA_getHtmlForOtherActions($what, $action, $_url_params, $full_query) +{ + $html = '
'; + $html .= ''; + if ($what == 'drop_db') { + $html .= __('You are about to DESTROY a complete database!') . ' '; + } + $html .= __('Do you really want to execute the following query?'); + $html .= ''; + $html .= '' . $full_query . ''; + $html .= '
'; + $html .= '
'; + $html .= '
'; + $html .= PMA_URL_getHiddenInputs($_url_params); + // Display option to disable foreign key checks while dropping tables + if ($what == 'drop_tbl') { + $html .= '
'; + $html .= ''; + $html .= __('Foreign key check:'); + $html .= ''; + $html .= ''; + $html .= 'fetchValue( + 'SHOW VARIABLES LIKE \'foreign_key_checks\';', 0, 1 + ) == 'ON'; + if ($default_fk_check_value) { + $html .= ' checked="checked"'; + } + $html .= '/>'; + $html .= ''; + $html .= ($default_fk_check_value) ? __('(Enabled)') : __('(Disabled)'); + $html .= ''; + $html .= '
'; + } + $html .= ''; + $html .= ''; + $html .= '
'; + + $html .= '
'; + $html .= PMA_URL_getHiddenInputs($_url_params); + $html .= ''; + $html .= ''; + $html .= '
'; + $html .= '
'; + + return $html; +} + +?>