From db39eeb4230e703bc579a431bf10685fd65a1f10 Mon Sep 17 00:00:00 2001 From: xmujay Date: Fri, 2 Aug 2013 20:05:27 +0800 Subject: [PATCH] refactor server_variables 1. move functions to separated files 2. render HTML at once 3. refactor function name to PMA_getHtmlForSubPageHeader --- libraries/server_common.lib.php | 8 +- libraries/server_variables.lib.php | 270 ++++++++++++++++++++++ server_binlog.php | 2 +- server_collations.php | 2 +- server_databases.php | 3 +- server_engines.php | 2 +- server_plugins.php | 2 +- server_replication.php | 5 +- server_variables.php | 206 +---------------- test/libraries/PMA_server_common_test.php | 8 +- 10 files changed, 299 insertions(+), 209 deletions(-) create mode 100644 libraries/server_variables.lib.php diff --git a/libraries/server_common.lib.php b/libraries/server_common.lib.php index 540a14982b..07f1bb2a1e 100644 --- a/libraries/server_common.lib.php +++ b/libraries/server_common.lib.php @@ -13,14 +13,18 @@ if (! defined('PHPMYADMIN')) { * Returns the html for the sub-page heading * * @param string $type Sub page type + * @param string $link Link to the official MySQL documentation * * @return string */ -function PMA_getSubPageHeader($type) +function PMA_getHtmlForSubPageHeader($type, $link='') { //array contains Sub page icon and text $header = array(); + $header['variables']['icon'] = 's_vars.png'; + $header['variables']['text'] = __('Server variables and settings'); + $header['engines']['icon'] = 'b_engine.png'; $header['engines']['text'] = __('Storage Engines'); @@ -45,7 +49,7 @@ function PMA_getSubPageHeader($type) $html = '

' . "\n" . PMA_Util::getImage($header[$type]['icon']) . ' ' . $header[$type]['text'] . "\n" - . '

' . "\n"; + . $link . '' . "\n"; return $html; } diff --git a/libraries/server_variables.lib.php b/libraries/server_variables.lib.php new file mode 100644 index 0000000000..80a4672a2c --- /dev/null +++ b/libraries/server_variables.lib.php @@ -0,0 +1,270 @@ +fetchSingleRow( + 'SHOW GLOBAL VARIABLES WHERE Variable_name="' + . PMA_Util::sqlAddSlashes($_REQUEST['varName']) . '";', + 'NUM' + ); + if (isset($VARIABLE_DOC_LINKS[$_REQUEST['varName']][3]) + && $VARIABLE_DOC_LINKS[$_REQUEST['varName']][3] == 'byte' + ) { + $response->addJSON( + 'message', + implode( + ' ', PMA_Util::formatByteDown($varValue[1], 3, 3) + ) + ); + } else { + $response->addJSON( + 'message', + $varValue[1] + ); + } +} +/** + * Get Ajax return when $_REQUEST['type'] === 'setval' + * + * @return null + */ +function PMA_getAjaxReturnForSetVal() +{ + global $VARIABLE_DOC_LINKS; + $response = PMA_Response::getInstance(); + + $value = $_REQUEST['varValue']; + $matches = array(); + + if (isset($VARIABLE_DOC_LINKS[$_REQUEST['varName']][3]) + && $VARIABLE_DOC_LINKS[$_REQUEST['varName']][3] == 'byte' + && preg_match( + '/^\s*(\d+(\.\d+)?)\s*(mb|kb|mib|kib|gb|gib)\s*$/i', + $value, + $matches + ) + ) { + $exp = array( + 'kb' => 1, + 'kib' => 1, + 'mb' => 2, + 'mib' => 2, + 'gb' => 3, + 'gib' => 3 + ); + $value = floatval($matches[1]) * PMA_Util::pow( + 1024, + $exp[strtolower($matches[3])] + ); + } else { + $value = PMA_Util::sqlAddSlashes($value); + } + + if (! is_numeric($value)) { + $value="'" . $value . "'"; + } + + if (! preg_match("/[^a-zA-Z0-9_]+/", $_REQUEST['varName']) + && $GLOBALS['dbi']->query( + 'SET GLOBAL ' . $_REQUEST['varName'] . ' = ' . $value + ) + ) { + // Some values are rounded down etc. + $varValue = $GLOBALS['dbi']->fetchSingleRow( + 'SHOW GLOBAL VARIABLES WHERE Variable_name="' + . PMA_Util::sqlAddSlashes($_REQUEST['varName']) + . '";', 'NUM' + ); + $response->addJSON( + 'variable', + PMA_formatVariable($_REQUEST['varName'], $varValue[1]) + ); + } else { + $response->isSuccess(false); + $response->addJSON( + 'error', + __('Setting variable failed') + ); + } +} + +/** + * Format Variable + * + * @param string $name variable name + * @param numeric $value variable value + * + * @return formatted string + */ +function PMA_formatVariable($name, $value) +{ + global $VARIABLE_DOC_LINKS; + + if (is_numeric($value)) { + if (isset($VARIABLE_DOC_LINKS[$name][3]) + && $VARIABLE_DOC_LINKS[$name][3]=='byte' + ) { + return '' + . implode(' ', PMA_Util::formatByteDown($value, 3, 3)) + . ''; + } else { + return PMA_Util::formatNumber($value, 0); + } + } + return htmlspecialchars($value); +} + +/** + * Prints link templates + * + * @return string + */ +function PMA_getHtmlForLinkTemplates() +{ + $url = htmlspecialchars('server_variables.php?' . PMA_generate_common_url()); + $output = ''; + $output .= ' '; + $output .= ' '; + $output .= PMA_Util::getImage( + 'b_help.png', + __('Documentation'), + array( + 'style' => 'display:none', + 'id' => 'docImage' + ) + ); + + return $output; +} + +/** + * Prints Html for Server Variables + * + * @return string + */ +function PMA_getHtmlForServerVariables() +{ + $value = ! empty($_REQUEST['filter']) + ? htmlspecialchars($_REQUEST['filter']) + : ''; + $output = '
' + . '' . __('Filters') . '' + . '
' + . '' + . '' + . '
' + . '
'; + + $output .= '
' + . '
' + . '
' . __('Variable') . '
' + . '
' + . __('Session value') . ' / ' . __('Global value') + . '
' + . '
' + . '
'; + + $output .= PMA_getHtmlForServerVariablesItems(); + + $output .= '
'; + + return $output; +} + + +/** + * Prints Html for Server Variables Items + * + * @return string + */ +function PMA_getHtmlForServerVariablesItems() +{ + global $VARIABLE_DOC_LINKS; + /** + * Sends the queries and buffers the results + */ + $serverVarsSession + = $GLOBALS['dbi']->fetchResult('SHOW SESSION VARIABLES;', 0, 1); + $serverVars = $GLOBALS['dbi']->fetchResult('SHOW GLOBAL VARIABLES;', 0, 1); + + $output = ''; + $odd_row = true; + foreach ($serverVars as $name => $value) { + $has_session_value = isset($serverVarsSession[$name]) + && $serverVarsSession[$name] != $value; + $row_class = ($odd_row ? ' odd' : ' even') + . ($has_session_value ? ' diffSession' : ''); + + $output .= '
' + . '
'; + + // To display variable documentation link + if (isset($VARIABLE_DOC_LINKS[$name])) { + $output .= ''; + $output .= PMA_Util::showMySQLDocu( + $VARIABLE_DOC_LINKS[$name][1], + $VARIABLE_DOC_LINKS[$name][1], + false, + $VARIABLE_DOC_LINKS[$name][2] . '_' . $VARIABLE_DOC_LINKS[$name][0], + true + ); + $output .= htmlspecialchars(str_replace('_', ' ', $name)); + $output .= ''; + $output .= ''; + } else { + $output .= htmlspecialchars(str_replace('_', ' ', $name)); + } + $output .= '
' + . '
 ' + . PMA_formatVariable($name, $value) + . '
' + . '
' + . '
'; + + if ($has_session_value) { + $output .= '
' + . '
(' . __('Session value') . ')
' + . '
 ' + . PMA_formatVariable($name, $serverVarsSession[$name]) . '
' + . '
' + . '
'; + } + + $odd_row = ! $odd_row; + } + + return $output; +} +?> + + diff --git a/server_binlog.php b/server_binlog.php index 45d08494fa..8f3b79902b 100644 --- a/server_binlog.php +++ b/server_binlog.php @@ -45,7 +45,7 @@ if (!empty($_REQUEST['dontlimitchars'])) { $response = PMA_Response::getInstance(); -$response->addHTML(PMA_getSubPageHeader('binlog')); +$response->addHTML(PMA_getHtmlForSubPageHeader('binlog')); $response->addHTML(PMA_getLogSelector($binary_logs, $url_params)); $response->addHTML(PMA_getLogInfo($binary_logs, $url_params)); diff --git a/server_collations.php b/server_collations.php index 872ceca68b..d547901740 100644 --- a/server_collations.php +++ b/server_collations.php @@ -24,7 +24,7 @@ require_once 'libraries/mysql_charsets.inc.php'; $response = PMA_Response::getInstance(); -$response->addHTML(PMA_getSubPageHeader('collations')); +$response->addHTML(PMA_getHtmlForSubPageHeader('collations')); $response->addHTML( PMA_getHtmlForCharsets( $mysql_charsets, diff --git a/server_databases.php b/server_databases.php index 7120e9d514..762e859d31 100644 --- a/server_databases.php +++ b/server_databases.php @@ -1,6 +1,7 @@ addHTML(PMA_getSubPageHeader($header_type)); +$response->addHTML(PMA_getHtmlForSubPageHeader($header_type)); /** * Displays For Create database. diff --git a/server_engines.php b/server_engines.php index a987f8d61e..65808e945f 100644 --- a/server_engines.php +++ b/server_engines.php @@ -22,7 +22,7 @@ require 'libraries/server_engines.lib.php'; * Displays the sub-page heading */ $response = PMA_Response::getInstance(); -$response->addHTML(PMA_getSubPageHeader('engines')); +$response->addHTML(PMA_getHtmlForSubPageHeader('engines')); /** * start output diff --git a/server_plugins.php b/server_plugins.php index 2a77f0dcd1..ca53ef3a3c 100644 --- a/server_plugins.php +++ b/server_plugins.php @@ -51,7 +51,7 @@ ksort($plugins); /** * Displays the page */ -$response->addHTML(PMA_getSubPageHeader('plugins')); +$response->addHTML(PMA_getHtmlForSubPageHeader('plugins')); $response->addHTML(PMA_getPluginAndModuleInfo($plugins, $modules)); exit; diff --git a/server_replication.php b/server_replication.php index 31a9d6e1a8..9f1f2ae6f7 100644 --- a/server_replication.php +++ b/server_replication.php @@ -1,6 +1,7 @@ addFile('replication.js'); * Checks if the user is allowed to do what he tries to... */ if (! $is_superuser) { - $html = PMA_getSubPageHeader('replication'); + $html = PMA_getHtmlForSubPageHeader('replication'); $html .= PMA_Message::error(__('No Privileges'))->getDisplay(); $response->addHTML($html); exit; @@ -46,7 +47,7 @@ PMA_handleControlRequest(); * start output */ $response->addHTML('
'); -$response->addHTML(PMA_getSubPageHeader('replication')); +$response->addHTML(PMA_getHtmlForSubPageHeader('replication')); // Display error messages $response->addHTML(PMA_getHtmlForErrorMessage()); diff --git a/server_variables.php b/server_variables.php index 50356392ac..b796c1b064 100644 --- a/server_variables.php +++ b/server_variables.php @@ -7,6 +7,7 @@ */ require_once 'libraries/common.inc.php'; +require_once 'libraries/server_variables.lib.php'; $response = PMA_Response::getInstance(); $header = $response->getHeader(); @@ -28,85 +29,11 @@ require 'libraries/server_variables_doc.php'; */ if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) { - $response = PMA_Response::getInstance(); - if (isset($_REQUEST['type'])) { if ($_REQUEST['type'] === 'getval') { - // Send with correct charset - header('Content-Type: text/html; charset=UTF-8'); - $varValue = $GLOBALS['dbi']->fetchSingleRow( - 'SHOW GLOBAL VARIABLES WHERE Variable_name="' - . PMA_Util::sqlAddSlashes($_REQUEST['varName']) . '";', - 'NUM' - ); - if (isset($VARIABLE_DOC_LINKS[$_REQUEST['varName']][3]) - && $VARIABLE_DOC_LINKS[$_REQUEST['varName']][3] == 'byte' - ) { - $response->addJSON( - 'message', - implode( - ' ', PMA_Util::formatByteDown($varValue[1], 3, 3) - ) - ); - } else { - $response->addJSON( - 'message', - $varValue[1] - ); - } + PMA_getAjaxReturnForGetVal(); } else if ($_REQUEST['type'] === 'setval') { - $value = $_REQUEST['varValue']; - - if (isset($VARIABLE_DOC_LINKS[$_REQUEST['varName']][3]) - && $VARIABLE_DOC_LINKS[$_REQUEST['varName']][3] == 'byte' - && preg_match( - '/^\s*(\d+(\.\d+)?)\s*(mb|kb|mib|kib|gb|gib)\s*$/i', - $value, - $matches - ) - ) { - $exp = array( - 'kb' => 1, - 'kib' => 1, - 'mb' => 2, - 'mib' => 2, - 'gb' => 3, - 'gib' => 3 - ); - $value = floatval($matches[1]) * PMA_Util::pow( - 1024, - $exp[strtolower($matches[3])] - ); - } else { - $value = PMA_Util::sqlAddSlashes($value); - } - - if (! is_numeric($value)) { - $value="'" . $value . "'"; - } - - if (! preg_match("/[^a-zA-Z0-9_]+/", $_REQUEST['varName']) - && $GLOBALS['dbi']->query( - 'SET GLOBAL ' . $_REQUEST['varName'] . ' = ' . $value - ) - ) { - // Some values are rounded down etc. - $varValue = $GLOBALS['dbi']->fetchSingleRow( - 'SHOW GLOBAL VARIABLES WHERE Variable_name="' - . PMA_Util::sqlAddSlashes($_REQUEST['varName']) - . '";', 'NUM' - ); - $response->addJSON( - 'variable', - PMA_formatVariable($_REQUEST['varName'], $varValue[1]) - ); - } else { - $response->isSuccess(false); - $response->addJSON( - 'error', - __('Setting variable failed') - ); - } + PMA_getAjaxReturnForSetVal(); } exit; } @@ -115,134 +42,21 @@ if (isset($_REQUEST['ajax_request']) && $_REQUEST['ajax_request'] == true) { /** * Displays the sub-page heading */ -$output = '

' . PMA_Util::getImage('s_vars.png') - . '' . __('Server variables and settings') . "\n" - . PMA_Util::showMySQLDocu( - 'server_system_variables', 'server_system_variables' - ) - . '

' . "\n"; +$doc_link = PMA_Util::showMySQLDocu( + 'server_system_variables', 'server_system_variables' +); +$response->addHtml(PMA_getHtmlForSubPageHeader('variables', $doc_link)); /** * Link templates */ -$url = htmlspecialchars('server_variables.php?' . PMA_generate_common_url()); -$output .= ''; -$output .= ' '; -$output .= ' '; -$output .= PMA_Util::getImage( - 'b_help.png', - __('Documentation'), - array( - 'style' => 'display:none', - 'id' => 'docImage' - ) -); - -/** - * Sends the queries and buffers the results - */ -$serverVarsSession = $GLOBALS['dbi']->fetchResult('SHOW SESSION VARIABLES;', 0, 1); -$serverVars = $GLOBALS['dbi']->fetchResult('SHOW GLOBAL VARIABLES;', 0, 1); - +$response->addHtml(PMA_getHtmlForLinkTemplates()); /** * Displays the page */ -$value = ! empty($_REQUEST['filter']) ? htmlspecialchars($_REQUEST['filter']) : ''; -$output .= '
' - . '' . __('Filters') . '' - . '
' - . '' - . '' - . '
' - . '
'; +$response->addHtml(PMA_getHtmlForServerVariables()); -$output .= '
' - . '
' - . '
' . __('Variable') . '
' - . '
' - . __('Session value') . ' / ' . __('Global value') - . '
' - . '
' - . '
'; - -$odd_row = true; -foreach ($serverVars as $name => $value) { - $has_session_value = isset($serverVarsSession[$name]) - && $serverVarsSession[$name] != $value; - $row_class = ($odd_row ? ' odd' : ' even') - . ($has_session_value ? ' diffSession' : ''); - - $output .= '
' - . '
'; - - // To display variable documentation link - if (isset($VARIABLE_DOC_LINKS[$name])) { - $output .= ''; - $output .= PMA_Util::showMySQLDocu( - $VARIABLE_DOC_LINKS[$name][1], - $VARIABLE_DOC_LINKS[$name][1], - false, - $VARIABLE_DOC_LINKS[$name][2] . '_' . $VARIABLE_DOC_LINKS[$name][0], - true - ); - $output .= htmlspecialchars(str_replace('_', ' ', $name)); - $output .= ''; - $output .= ''; - } else { - $output .= htmlspecialchars(str_replace('_', ' ', $name)); - } - $output .= '
' - . '
 ' - . PMA_formatVariable($name, $value) - . '
' - . '
' - . '
'; - - if ($has_session_value) { - $output .= '
' - . '
(' . __('Session value') . ')
' - . '
 ' - . PMA_formatVariable($name, $serverVarsSession[$name]) . '
' - . '
' - . '
'; - } - - $odd_row = ! $odd_row; -} -$output .= '
'; - -$response->addHtml($output); - -/** - * Format Variable - * - * @param string $name variable name - * @param numeric $value variable value - * - * @return formatted string - */ -function PMA_formatVariable($name, $value) -{ - global $VARIABLE_DOC_LINKS; - - if (is_numeric($value)) { - if (isset($VARIABLE_DOC_LINKS[$name][3]) - && $VARIABLE_DOC_LINKS[$name][3]=='byte' - ) { - return '' - . implode(' ', PMA_Util::formatByteDown($value, 3, 3)) - . ''; - } else { - return PMA_Util::formatNumber($value, 0); - } - } - return htmlspecialchars($value); -} +exit; ?> diff --git a/test/libraries/PMA_server_common_test.php b/test/libraries/PMA_server_common_test.php index 1c39b31dca..3dc47b83a3 100644 --- a/test/libraries/PMA_server_common_test.php +++ b/test/libraries/PMA_server_common_test.php @@ -35,14 +35,14 @@ class PMA_ServerCommon_Test extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getSubPageHeader + * Test for PMA_getHtmlForSubPageHeader * * @return void */ public function testPMAGetSubPageHeader() { //server_engines - $html = PMA_getSubPageHeader("engines"); + $html = PMA_getHtmlForSubPageHeader("engines"); $this->assertContains( '', $html @@ -53,7 +53,7 @@ class PMA_ServerCommon_Test extends PHPUnit_Framework_TestCase ); //server_databases - $html = PMA_getSubPageHeader("databases"); + $html = PMA_getHtmlForSubPageHeader("databases"); $this->assertContains( '', $html @@ -64,7 +64,7 @@ class PMA_ServerCommon_Test extends PHPUnit_Framework_TestCase ); //server_replication - $html = PMA_getSubPageHeader("replication"); + $html = PMA_getHtmlForSubPageHeader("replication"); $replication_img = ''; $this->assertContains(