From 77318c0d02176cd99f9c7ef4a219899c0ed0dfb2 Mon Sep 17 00:00:00 2001 From: xmujay Date: Fri, 7 Jun 2013 00:22:48 +0800 Subject: [PATCH 1/3] refactor server_binlog.php: 1. split to functions 2. move function to server_bin_log.lib.php --- libraries/server_bin_log.lib.php | 240 +++++++++++++++++++++++++++++++ server_binlog.php | 198 ++----------------------- 2 files changed, 248 insertions(+), 190 deletions(-) create mode 100644 libraries/server_bin_log.lib.php diff --git a/libraries/server_bin_log.lib.php b/libraries/server_bin_log.lib.php new file mode 100644 index 0000000000..cbedea43f7 --- /dev/null +++ b/libraries/server_bin_log.lib.php @@ -0,0 +1,240 @@ +' . "\n" + . PMA_Util::getImage('s_tbl.png') + . ' ' . __('Binary log') . "\n" + . '' . "\n"; + return $html; +} + +/** + * Returns the html for log selector. + * + * @param Array $binary_logs Binary Logs + * + * @param Array $url_params links parameters + * + * @return string + */ +function PMA_getLogSelector($binary_logs, $url_params) +{ + $html = ""; + if (count($binary_logs) > 1) { + $html .= '
'; + $html .= PMA_generate_common_hidden_inputs($url_params); + $html .= '
'; + $html .= __('Select binary log to view'); + $html .= ''; + $html .= '
'; + $html .= '
'; + } + + return $html; +} + +/** + * Returns the html for binary log information. + * + * @param Array $binary_logs Binary Logs + * + * @param Array $url_params links parameters + * + * @return string + */ +function PMA_getLogInfo($binary_logs, $url_params) +{ + /** + * Need to find the real end of rows? + */ + if (! isset($_REQUEST['pos'])) { + $pos = 0; + } else { + /* We need this to be a integer */ + $pos = (int) $_REQUEST['pos']; + } + + $sql_query = 'SHOW BINLOG EVENTS'; + if (! empty($_REQUEST['log'])) { + $sql_query .= ' IN \'' . $_REQUEST['log'] . '\''; + } + if ($GLOBALS['cfg']['MaxRows'] !== 'all') { + $sql_query .= ' LIMIT ' . $pos . ', ' . (int) $GLOBALS['cfg']['MaxRows']; + } + + /** + * Sends the query + */ + $result = $GLOBALS['dbi']->query($sql_query); + + /** + * prepare some vars for displaying the result table + */ + // Gets the list of fields properties + if (isset($result) && $result) { + $num_rows = $GLOBALS['dbi']->numRows($result); + } else { + $num_rows = 0; + } + + if (empty($_REQUEST['dontlimitchars'])) { + $dontlimitchars = false; + } else { + $dontlimitchars = true; + $url_params['dontlimitchars'] = 1; + } + + //html output + $html = PMA_Util::getMessage(PMA_Message::success(), $sql_query); + $html .= '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . ''; + + $odd_row = true; + while ($value = $GLOBALS['dbi']->fetchAssoc($result)) { + if (! $dontlimitchars + && PMA_strlen($value['Info']) > $GLOBALS['cfg']['LimitChars'] + ) { + $value['Info'] = PMA_substr( + $value['Info'], 0, $GLOBALS['cfg']['LimitChars'] + ) . '...'; + } + + $html .= '' + . '' + . '' + . '' + . '' + . '' + . '' + . ''; + + $odd_row = !$odd_row; + } + $html .= '' + . '
'; + + // we do not now how much rows are in the binlog + // so we can just force 'NEXT' button + if ($pos > 0) { + $this_url_params = $url_params; + if ($pos > $GLOBALS['cfg']['MaxRows']) { + $this_url_params['pos'] = $pos - $GLOBALS['cfg']['MaxRows']; + } + + $html .= ''; + } else { + $html .= '>' . _pgettext('Previous page', 'Previous'); + } // end if... else... + $html .= ' < - '; + } + + $this_url_params = $url_params; + if ($pos > 0) { + $this_url_params['pos'] = $pos; + } + if ($dontlimitchars) { + unset($this_url_params['dontlimitchars']); + $tempTitle = __('Truncate Shown Queries'); + $tempImgMode = 'partial'; + } else { + $this_url_params['dontlimitchars'] = 1; + $tempTitle = __('Show Full Queries'); + $tempImgMode = 'full'; + } + $html .= '' + . ''; + + // we do not now how much rows are in the binlog + // so we can just force 'NEXT' button + if ($num_rows >= $GLOBALS['cfg']['MaxRows']) { + $this_url_params = $url_params; + $this_url_params['pos'] = $pos + $GLOBALS['cfg']['MaxRows']; + $html .= ' - '; + } else { + $html .= '>' . _pgettext('Next page', 'Next'); + } // end if... else... + $html .= ' > '; + } + + $html .= '
' . __('Log name') . '' . __('Position') . '' . __('Event type') . '' . __('Server ID') . '' . __('Original position') . '' . __('Information') . '
 ' . $value['Log_name'] . '  ' . $value['Pos'] . '  ' . $value['Event_type'] . '  ' . $value['Server_id'] . '  ' + . (isset($value['Orig_log_pos']) + ? $value['Orig_log_pos'] : $value['End_log_pos']) + . ' 
 ' . htmlspecialchars($value['Info'])
+	        . ' 
'; + + return $html; +} + +?> \ No newline at end of file diff --git a/server_binlog.php b/server_binlog.php index b02520b686..8a38fdb578 100644 --- a/server_binlog.php +++ b/server_binlog.php @@ -16,18 +16,8 @@ require_once 'libraries/common.inc.php'; */ require_once 'libraries/server_common.inc.php'; -$response = PMA_Response::getInstance(); -$url_params = array(); +require_once 'libraries/server_bin_log.lib.php'; -/** - * Need to find the real end of rows? - */ -if (! isset($_REQUEST['pos'])) { - $pos = 0; -} else { - /* We need this to be a integer */ - $pos = (int) $_REQUEST['pos']; -} if (! isset($_REQUEST['log']) || ! array_key_exists($_REQUEST['log'], $binary_logs) @@ -37,188 +27,16 @@ if (! isset($_REQUEST['log']) $url_params['log'] = $_REQUEST['log']; } -$sql_query = 'SHOW BINLOG EVENTS'; -if (! empty($_REQUEST['log'])) { - $sql_query .= ' IN \'' . $_REQUEST['log'] . '\''; -} -if ($GLOBALS['cfg']['MaxRows'] !== 'all') { - $sql_query .= ' LIMIT ' . $pos . ', ' . (int) $GLOBALS['cfg']['MaxRows']; -} - -/** - * Sends the query - */ -$result = $GLOBALS['dbi']->query($sql_query); - -/** - * prepare some vars for displaying the result table - */ -// Gets the list of fields properties -if (isset($result) && $result) { - $num_rows = $GLOBALS['dbi']->numRows($result); -} else { - $num_rows = 0; -} - -if (empty($_REQUEST['dontlimitchars'])) { - $dontlimitchars = false; -} else { - $dontlimitchars = true; +if (!empty($_REQUEST['dontlimitchars'])) { $url_params['dontlimitchars'] = 1; } -/** - * Displays the sub-page heading - */ -$html = '

' . "\n" - . PMA_Util::getImage('s_tbl.png') - . ' ' . __('Binary log') . "\n" - . '

' . "\n"; +$response = PMA_Response::getInstance(); -/** - * Display log selector. - */ -if (count($binary_logs) > 1) { - $html .= '
'; - $html .= PMA_generate_common_hidden_inputs($url_params); - $html .= '
'; - $html .= __('Select binary log to view'); - $html .= ''; - $html .= '
'; - $html .= '
'; -} +$response->addHTML(PMA_getSubPageHeader()); +$response->addHTML(PMA_getLogSelector($binary_logs, $url_params)); +$response->addHTML(PMA_getLogInfo($binary_logs, $url_params)); -$html .= PMA_Util::getMessage(PMA_Message::success()); +exit; -/** - * Displays the page - */ -$html .= '' - . '' - . '' - . '' - . '' - . '' - . '' - . '' - . '' - . '' - . '' - . '' - . '' - . '' - . ''; - -$odd_row = true; -while ($value = $GLOBALS['dbi']->fetchAssoc($result)) { - if (! $dontlimitchars - && PMA_strlen($value['Info']) > $GLOBALS['cfg']['LimitChars'] - ) { - $value['Info'] = PMA_substr( - $value['Info'], 0, $GLOBALS['cfg']['LimitChars'] - ) . '...'; - } - - $html .= '' - . '' - . '' - . '' - . '' - . '' - . '' - . ''; - - $odd_row = !$odd_row; -} -$html .= '' - . '
'; - -// we do not now how much rows are in the binlog -// so we can just force 'NEXT' button -if ($pos > 0) { - $this_url_params = $url_params; - if ($pos > $GLOBALS['cfg']['MaxRows']) { - $this_url_params['pos'] = $pos - $GLOBALS['cfg']['MaxRows']; - } - - $html .= ''; - } else { - $html .= '>' . _pgettext('Previous page', 'Previous'); - } // end if... else... - $html .= ' < - '; -} - -$this_url_params = $url_params; -if ($pos > 0) { - $this_url_params['pos'] = $pos; -} -if ($dontlimitchars) { - unset($this_url_params['dontlimitchars']); - $tempTitle = __('Truncate Shown Queries'); - $tempImgMode = 'partial'; -} else { - $this_url_params['dontlimitchars'] = 1; - $tempTitle = __('Show Full Queries'); - $tempImgMode = 'full'; -} -$html .= '' - . ''; - -// we do not now how much rows are in the binlog -// so we can just force 'NEXT' button -if ($num_rows >= $GLOBALS['cfg']['MaxRows']) { - $this_url_params = $url_params; - $this_url_params['pos'] = $pos + $GLOBALS['cfg']['MaxRows']; - $html .= ' - '; - } else { - $html .= '>' . _pgettext('Next page', 'Next'); - } // end if... else... - $html .= ' > '; -} - -$html .= '
' . __('Log name') . '' . __('Position') . '' . __('Event type') . '' . __('Server ID') . '' . __('Original position') . '' . __('Information') . '
 ' . $value['Log_name'] . '  ' . $value['Pos'] . '  ' . $value['Event_type'] . '  ' . $value['Server_id'] . '  ' - . (isset($value['Orig_log_pos']) - ? $value['Orig_log_pos'] : $value['End_log_pos']) - . ' 
 ' . htmlspecialchars($value['Info'])
-        . ' 
'; - -$response->addHTML($html); +?> From 222c432fbbaf01f12be5c8a970656dad0dd8c975 Mon Sep 17 00:00:00 2001 From: xmujay Date: Fri, 7 Jun 2013 00:26:54 +0800 Subject: [PATCH 2/3] fix code indention --- libraries/server_bin_log.lib.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/server_bin_log.lib.php b/libraries/server_bin_log.lib.php index cbedea43f7..72ddc8256c 100644 --- a/libraries/server_bin_log.lib.php +++ b/libraries/server_bin_log.lib.php @@ -39,7 +39,7 @@ function PMA_getSubPageHeader() */ function PMA_getLogSelector($binary_logs, $url_params) { - $html = ""; + $html = ""; if (count($binary_logs) > 1) { $html .= '
'; $html .= PMA_generate_common_hidden_inputs($url_params); @@ -134,8 +134,8 @@ function PMA_getLogInfo($binary_logs, $url_params) $url_params['dontlimitchars'] = 1; } - //html output - $html = PMA_Util::getMessage(PMA_Message::success(), $sql_query); + //html output + $html = PMA_Util::getMessage(PMA_Message::success(), $sql_query); $html .= '' . '' . '' @@ -237,4 +237,4 @@ function PMA_getLogInfo($binary_logs, $url_params) return $html; } -?> \ No newline at end of file +?> From dc2f33ffc086a3f9a825570486ed22c8ac24dfe2 Mon Sep 17 00:00:00 2001 From: xmujay Date: Fri, 7 Jun 2013 11:46:17 +0800 Subject: [PATCH 3/3] split navigation and item render from long function --- libraries/server_bin_log.lib.php | 86 +++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 24 deletions(-) diff --git a/libraries/server_bin_log.lib.php b/libraries/server_bin_log.lib.php index 72ddc8256c..4707441090 100644 --- a/libraries/server_bin_log.lib.php +++ b/libraries/server_bin_log.lib.php @@ -31,23 +31,23 @@ function PMA_getSubPageHeader() /** * Returns the html for log selector. * - * @param Array $binary_logs Binary Logs + * @param Array $binary_log_file_names Binary logs file names * * @param Array $url_params links parameters * * @return string */ -function PMA_getLogSelector($binary_logs, $url_params) +function PMA_getLogSelector($binary_log_file_names, $url_params) { $html = ""; - if (count($binary_logs) > 1) { + if (count($binary_log_file_names) > 1) { $html .= ''; $html .= PMA_generate_common_hidden_inputs($url_params); $html .= '
'; $html .= __('Select binary log to view'); $html .= '
' . '' . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . '' + . ''; + + $html .= PMA_getAllLogItemInfo($result, $dontlimitchars); + + $html .= '' + . '
'; + + $html .= PMA_getNavigationRow($url_params, $pos, $num_rows, $dontlimitchars); - // we do not now how much rows are in the binlog + $html .= '
' . __('Log name') . '' . __('Position') . '' . __('Event type') . '' . __('Server ID') . '' . __('Original position') . '' . __('Information') . '
'; + + return $html; +} + +/** + * Returns the html for Navigation Row. + * + * @param Array $url_params Links parameters + * + * @param int $pos Position to display + * + * @param int $num_rows Number of results row + * + * @param bool $dontlimitchars Whether limit chars + * + * @return string + */ +function PMA_getNavigationRow($url_params, $pos, $num_rows, $dontlimitchars) +{ + $html = ""; + // we do not know how much rows are in the binlog // so we can just force 'NEXT' button if ($pos > 0) { $this_url_params = $url_params; @@ -193,19 +232,21 @@ function PMA_getLogInfo($binary_logs, $url_params) $html .= ' > '; } - $html .= '' - . '' - . '' - . '' . __('Log name') . '' - . '' . __('Position') . '' - . '' . __('Event type') . '' - . '' . __('Server ID') . '' - . '' . __('Original position') . '' - . '' . __('Information') . '' - . '' - . '' - . ''; - + return $html; +} + +/** + * Returns the html for all binary log items. + * + * @param resource $result MySQL Query result + * + * @param bool $dontlimitchars Whether limit chars + * + * @return string + */ +function PMA_getAllLogItemInfo($result, $dontlimitchars) +{ + $html = ""; $odd_row = true; while ($value = $GLOBALS['dbi']->fetchAssoc($result)) { if (! $dontlimitchars @@ -231,9 +272,6 @@ function PMA_getLogInfo($binary_logs, $url_params) $odd_row = !$odd_row; } - $html .= '' - . ''; - return $html; }