Skip to content

Commit

Permalink
refactor server_variables
Browse files Browse the repository at this point in the history
1. move functions to separated files
2. render HTML at once
3. refactor function name to PMA_getHtmlForSubPageHeader
  • Loading branch information
xmujay committed Aug 2, 2013
1 parent 21564e3 commit db39eeb
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 209 deletions.
8 changes: 6 additions & 2 deletions libraries/server_common.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
* 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');

Expand All @@ -45,7 +49,7 @@ function PMA_getSubPageHeader($type)
$html = '<h2>' . "\n"
. PMA_Util::getImage($header[$type]['icon'])
. ' ' . $header[$type]['text'] . "\n"
. '</h2>' . "\n";
. $link . '</h2>' . "\n";
return $html;
}

Expand Down
270 changes: 270 additions & 0 deletions libraries/server_variables.lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */

/**
* functions for displaying server variables
*
* @usedby server_variables.php
*
* @package PhpMyAdmin
*/
if (! defined('PHPMYADMIN')) {
exit;
}

/**
* Get Ajax return when $_REQUEST['type'] === 'getval'
*
* @return null
*/
function PMA_getAjaxReturnForGetVal()
{
global $VARIABLE_DOC_LINKS;
$response = PMA_Response::getInstance();

// 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]
);
}
}
/**
* 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 '<abbr title="'
. PMA_Util::formatNumber($value, 0) . '">'
. implode(' ', PMA_Util::formatByteDown($value, 3, 3))
. '</abbr>';
} 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 = '<a style="display: none;" href="#" class="editLink">';
$output .= PMA_Util::getIcon('b_edit.png', __('Edit')) . '</a>';
$output .= '<a style="display: none;" href="'
. $url . '" class="ajax saveLink">';
$output .= PMA_Util::getIcon('b_save.png', __('Save')) . '</a> ';
$output .= '<a style="display: none;" href="#" class="cancelLink">';
$output .= PMA_Util::getIcon('b_close.png', __('Cancel')) . '</a> ';
$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 = '<fieldset id="tableFilter">'
. '<legend>' . __('Filters') . '</legend>'
. '<div class="formelement">'
. '<label for="filterText">' . __('Containing the word:') . '</label>'
. '<input name="filterText" type="text" id="filterText"'
. ' style="vertical-align: baseline;" value="' . $value . '" />'
. '</div>'
. '</fieldset>';

$output .= '<div id="serverVariables" class="data filteredData noclick">'
. '<div class="var-header var-row">'
. '<div class="var-name">' . __('Variable') . '</div>'
. '<div class="var-value valueHeader">'
. __('Session value') . ' / ' . __('Global value')
. '</div>'
. '<div style="clear:both"></div>'
. '</div>';

$output .= PMA_getHtmlForServerVariablesItems();

$output .= '</div>';

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 .= '<div class="var-row' . $row_class . '">'
. '<div class="var-name">';

// To display variable documentation link
if (isset($VARIABLE_DOC_LINKS[$name])) {
$output .= '<span title="'
. htmlspecialchars(str_replace('_', ' ', $name)) . '">';
$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 .= '</a>';
$output .= '</span>';
} else {
$output .= htmlspecialchars(str_replace('_', ' ', $name));
}
$output .= '</div>'
. '<div class="var-value value'
. ($GLOBALS['dbi']->isSuperuser() ? ' editable' : '') . '">&nbsp;'
. PMA_formatVariable($name, $value)
. '</div>'
. '<div style="clear:both"></div>'
. '</div>';

if ($has_session_value) {
$output .= '<div class="var-row' . ($odd_row ? ' odd' : ' even') . '">'
. '<div class="var-name session">(' . __('Session value') . ')</div>'
. '<div class="var-value value">&nbsp;'
. PMA_formatVariable($name, $serverVarsSession[$name]) . '</div>'
. '<div style="clear:both"></div>'
. '</div>';
}

$odd_row = ! $odd_row;
}

return $output;
}
?>


2 changes: 1 addition & 1 deletion server_binlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

$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));

Expand Down
2 changes: 1 addition & 1 deletion server_collations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

$response = PMA_Response::getInstance();

$response->addHTML(PMA_getSubPageHeader('collations'));
$response->addHTML(PMA_getHtmlForSubPageHeader('collations'));
$response->addHTML(
PMA_getHtmlForCharsets(
$mysql_charsets,
Expand Down
3 changes: 2 additions & 1 deletion server_databases.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Server databases
*
* @package PhpMyAdmin
*/
Expand Down Expand Up @@ -135,7 +136,7 @@
* Displays the sub-page heading
*/
$header_type = $dbstats ? "database_statistics" : "databases";
$response->addHTML(PMA_getSubPageHeader($header_type));
$response->addHTML(PMA_getHtmlForSubPageHeader($header_type));

/**
* Displays For Create database.
Expand Down
2 changes: 1 addition & 1 deletion server_engines.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Displays the sub-page heading
*/
$response = PMA_Response::getInstance();
$response->addHTML(PMA_getSubPageHeader('engines'));
$response->addHTML(PMA_getHtmlForSubPageHeader('engines'));

/**
* start output
Expand Down
2 changes: 1 addition & 1 deletion server_plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
/**
* Displays the page
*/
$response->addHTML(PMA_getSubPageHeader('plugins'));
$response->addHTML(PMA_getHtmlForSubPageHeader('plugins'));
$response->addHTML(PMA_getPluginAndModuleInfo($plugins, $modules));

exit;
Expand Down
5 changes: 3 additions & 2 deletions server_replication.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Server replications
*
* @package PhpMyAdmin
*/
Expand All @@ -27,7 +28,7 @@
* 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;
Expand All @@ -46,7 +47,7 @@
* start output
*/
$response->addHTML('<div id="replication">');
$response->addHTML(PMA_getSubPageHeader('replication'));
$response->addHTML(PMA_getHtmlForSubPageHeader('replication'));

// Display error messages
$response->addHTML(PMA_getHtmlForErrorMessage());
Expand Down
Loading

0 comments on commit db39eeb

Please sign in to comment.