From 11ff8cee1191fae7f283e2ef438c90a5d4b47e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 11 Sep 2017 20:11:26 -0300 Subject: [PATCH] Refactor browse_foreigners functions to static methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- browse_foreigners.php | 7 +- libraries/browse_foreigners.lib.php | 341 ----------------- libraries/classes/BrowseForeigners.php | 352 ++++++++++++++++++ .../BrowseForeignersTest.php} | 60 ++- 4 files changed, 385 insertions(+), 375 deletions(-) delete mode 100644 libraries/browse_foreigners.lib.php create mode 100644 libraries/classes/BrowseForeigners.php rename test/{libraries/PMA_browse_foreigners_test.php => classes/BrowseForeignersTest.php} (79%) diff --git a/browse_foreigners.php b/browse_foreigners.php index b7c194f2e7..2a4dd24566 100644 --- a/browse_foreigners.php +++ b/browse_foreigners.php @@ -5,12 +5,13 @@ * * @package PhpMyAdmin */ + +use PhpMyAdmin\BrowseForeigners; use PhpMyAdmin\Relation; use PhpMyAdmin\Response; use PhpMyAdmin\Util; require_once 'libraries/common.inc.php'; -require_once 'libraries/browse_foreigners.lib.php'; /** * Sets globals from $_REQUEST @@ -39,7 +40,7 @@ $header->setBodyId('body_browse_foreigners'); */ $foreigners = Relation::getForeigners($db, $table); -$foreign_limit = PMA_getForeignLimit( +$foreign_limit = BrowseForeigners::getForeignLimit( isset($_REQUEST['foreign_showAll']) ? $_REQUEST['foreign_showAll'] : null ); @@ -53,7 +54,7 @@ $foreignData = Relation::getForeignData( ); // HTML output -$html = PMA_getHtmlForRelationalFieldSelection( +$html = BrowseForeigners::getHtmlForRelationalFieldSelection( $db, $table, $_REQUEST['field'], $foreignData, isset($fieldkey) ? $fieldkey : null, isset($data) ? $data : null diff --git a/libraries/browse_foreigners.lib.php b/libraries/browse_foreigners.lib.php deleted file mode 100644 index cc24abdced..0000000000 --- a/libraries/browse_foreigners.lib.php +++ /dev/null @@ -1,341 +0,0 @@ - 0 - && $horizontal_count > $GLOBALS['cfg']['RepeatCells'] - ) { - $output .= $header; - $horizontal_count = 0; - } - - // key names and descriptions for the left section, - // sorted by key names - $leftKeyname = $keys[$indexByKeyname]; - list( - $leftDescription, - $leftDescriptionTitle - ) = PMA_getDescriptionAndTitle($descriptions[$indexByKeyname]); - - // key names and descriptions for the right section, - // sorted by descriptions - $rightKeyname = $keys[$indexByDescription]; - list( - $rightDescription, - $rightDescriptionTitle - ) = PMA_getDescriptionAndTitle($descriptions[$indexByDescription]); - - $indexByDescription++; - - if (! empty($current_value)) { - $rightKeynameIsSelected = $rightKeyname == $current_value; - $leftKeynameIsSelected = $leftKeyname == $current_value; - } - - $output .= ''; - - $output .= PMA_getHtmlForColumnElement( - 'class="nowrap"', $leftKeynameIsSelected, - $leftKeyname, $leftDescription, - $leftDescriptionTitle - ); - - $output .= PMA_getHtmlForColumnElement( - '', $leftKeynameIsSelected, $leftKeyname, - $leftDescription, $leftDescriptionTitle - ); - - $output .= '' - . ''; - - $output .= PMA_getHtmlForColumnElement( - '', $rightKeynameIsSelected, $rightKeyname, - $rightDescription, $rightDescriptionTitle - ); - - $output .= PMA_getHtmlForColumnElement( - 'class="nowrap"', $rightKeynameIsSelected, - $rightKeyname, $rightDescription, - $rightDescriptionTitle - ); - $output .= ''; - - return array($output, $horizontal_count, $indexByDescription); -} - -/** - * Function to get html for relational field selection - * - * @param string $db current database - * @param string $table current table - * @param string $field field - * @param array $foreignData foreign column data - * @param string $fieldkey field key - * @param string $current_value current columns's value - * - * @return string - */ -function PMA_getHtmlForRelationalFieldSelection($db, $table, $field, $foreignData, - $fieldkey, $current_value -) { - $gotopage = PMA_getHtmlForGotoPage($foreignData); - $showall = PMA_getHtmlForShowAll($foreignData); - - $output = '
' - . '
' - . Url::getHiddenInputs($db, $table) - . '' - . ''; - - if (isset($_REQUEST['rownumber'])) { - $output .= ''; - } - $filter_value = (isset($_REQUEST['foreign_filter']) - ? htmlspecialchars($_REQUEST['foreign_filter']) - : ''); - $output .= '' - . '' - . '' - . '' - . '' - . '' . $gotopage . '' - . '' . $showall . '' - . '
' - . '
'; - - $output .= ''; - - if (!is_array($foreignData['disp_row'])) { - $output .= '' - . '
'; - - return $output; - } - - $header = ' - ' . __('Keyname') . ' - ' . __('Description') . ' - - ' . __('Description') . ' - ' . __('Keyname') . ' - '; - - $output .= '' . $header . '' . "\n" - . '' . $header . '' . "\n" - . '' . "\n"; - - $descriptions = array(); - $keys = array(); - foreach ($foreignData['disp_row'] as $relrow) { - if ($foreignData['foreign_display'] != false) { - $descriptions[] = $relrow[$foreignData['foreign_display']]; - } else { - $descriptions[] = ''; - } - - $keys[] = $relrow[$foreignData['foreign_field']]; - } - - asort($keys); - - $horizontal_count = 0; - $indexByDescription = 0; - - foreach ($keys as $indexByKeyname => $value) { - list( - $html, - $horizontal_count, - $indexByDescription - ) = PMA_getHtmlForOneKey( - $horizontal_count, $header, $keys, $indexByKeyname, - $descriptions, $indexByDescription, $current_value - ); - $output .= $html; - } - - $output .= '' - . ''; - - return $output; -} - -/** - * Get the description (possibly truncated) and the title - * - * @param string $description the key name's description - * - * @return array the new description and title - */ -function PMA_getDescriptionAndTitle($description) -{ - $limitChars = $GLOBALS['cfg']['LimitChars']; - if (mb_strlen($description) <= $limitChars) { - $description = htmlspecialchars( - $description - ); - $descriptionTitle = ''; - } else { - $descriptionTitle = htmlspecialchars( - $description - ); - $description = htmlspecialchars( - mb_substr( - $description, 0, $limitChars - ) - . '...' - ); - } - return array($description, $descriptionTitle); -} - -/** - * Function to get html for each column element - * - * @param string $cssClass class="nowrap" or '' - * @param bool $isSelected whether current equals form's value - * @param string $keyname current key - * @param string $description current value - * @param string $title current title - * - * @return string - */ -function PMA_getHtmlForColumnElement($cssClass, $isSelected, $keyname, - $description, $title -) { - $keyname = htmlspecialchars($keyname); - $output = '' : '') - . ''; - if ($cssClass !== '') { - $output .= $keyname; - } else { - $output .= $description; - } - - $output .= '' . ($isSelected ? '' : '') . ''; - - return $output; -} - -/** - * Function to get html for show all case - * - * @param array $foreignData foreign data - * - * @return string - */ -function PMA_getHtmlForShowAll($foreignData) -{ - $showall = ''; - if (is_array($foreignData['disp_row'])) { - if ($GLOBALS['cfg']['ShowAll'] - && ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) - ) { - $showall = ''; - } - } - - return $showall; -} - -/** - * Function to get html for the goto page option - * - * @param array $foreignData foreign data - * - * @return string - */ -function PMA_getHtmlForGotoPage($foreignData) -{ - $gotopage = ''; - isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0; - if (!is_array($foreignData['disp_row'])) { - return $gotopage; - } - - $session_max_rows = $GLOBALS['cfg']['MaxRows']; - $pageNow = @floor($pos / $session_max_rows) + 1; - $nbTotalPage = @ceil($foreignData['the_total'] / $session_max_rows); - - if ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) { - $gotopage = PhpMyAdmin\Util::pageselector( - 'pos', - $session_max_rows, - $pageNow, - $nbTotalPage, - 200, - 5, - 5, - 20, - 10, - __('Page number:') - ); - } - - return $gotopage; -} - -/** - * Function to get foreign limit - * - * @param string $foreign_showAll foreign navigation - * - * @return string - */ -function PMA_getForeignLimit($foreign_showAll) -{ - if (isset($foreign_showAll) && $foreign_showAll == __('Show all')) { - return null; - } - isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0; - return 'LIMIT ' . $pos . ', ' . intval($GLOBALS['cfg']['MaxRows']) . ' '; -} diff --git a/libraries/classes/BrowseForeigners.php b/libraries/classes/BrowseForeigners.php new file mode 100644 index 0000000000..a24dea96ec --- /dev/null +++ b/libraries/classes/BrowseForeigners.php @@ -0,0 +1,352 @@ + 0 + && $horizontal_count > $GLOBALS['cfg']['RepeatCells'] + ) { + $output .= $header; + $horizontal_count = 0; + } + + // key names and descriptions for the left section, + // sorted by key names + $leftKeyname = $keys[$indexByKeyname]; + list( + $leftDescription, + $leftDescriptionTitle + ) = self::getDescriptionAndTitle($descriptions[$indexByKeyname]); + + // key names and descriptions for the right section, + // sorted by descriptions + $rightKeyname = $keys[$indexByDescription]; + list( + $rightDescription, + $rightDescriptionTitle + ) = self::getDescriptionAndTitle($descriptions[$indexByDescription]); + + $indexByDescription++; + + if (! empty($current_value)) { + $rightKeynameIsSelected = $rightKeyname == $current_value; + $leftKeynameIsSelected = $leftKeyname == $current_value; + } + + $output .= ''; + + $output .= self::getHtmlForColumnElement( + 'class="nowrap"', $leftKeynameIsSelected, + $leftKeyname, $leftDescription, + $leftDescriptionTitle + ); + + $output .= self::getHtmlForColumnElement( + '', $leftKeynameIsSelected, $leftKeyname, + $leftDescription, $leftDescriptionTitle + ); + + $output .= '' + . ''; + + $output .= self::getHtmlForColumnElement( + '', $rightKeynameIsSelected, $rightKeyname, + $rightDescription, $rightDescriptionTitle + ); + + $output .= self::getHtmlForColumnElement( + 'class="nowrap"', $rightKeynameIsSelected, + $rightKeyname, $rightDescription, + $rightDescriptionTitle + ); + $output .= ''; + + return array($output, $horizontal_count, $indexByDescription); + } + + /** + * Function to get html for relational field selection + * + * @param string $db current database + * @param string $table current table + * @param string $field field + * @param array $foreignData foreign column data + * @param string $fieldkey field key + * @param string $current_value current columns's value + * + * @return string + */ + public static function getHtmlForRelationalFieldSelection($db, $table, $field, $foreignData, + $fieldkey, $current_value + ) { + $gotopage = self::getHtmlForGotoPage($foreignData); + $showall = self::getHtmlForShowAll($foreignData); + + $output = '
' + . '
' + . Url::getHiddenInputs($db, $table) + . '' + . ''; + + if (isset($_REQUEST['rownumber'])) { + $output .= ''; + } + $filter_value = (isset($_REQUEST['foreign_filter']) + ? htmlspecialchars($_REQUEST['foreign_filter']) + : ''); + $output .= '' + . '' + . '' + . '' + . '' + . '' . $gotopage . '' + . '' . $showall . '' + . '
' + . '
'; + + $output .= ''; + + if (!is_array($foreignData['disp_row'])) { + $output .= '' + . '
'; + + return $output; + } + + $header = ' + ' . __('Keyname') . ' + ' . __('Description') . ' + + ' . __('Description') . ' + ' . __('Keyname') . ' + '; + + $output .= '' . $header . '' . "\n" + . '' . $header . '' . "\n" + . '' . "\n"; + + $descriptions = array(); + $keys = array(); + foreach ($foreignData['disp_row'] as $relrow) { + if ($foreignData['foreign_display'] != false) { + $descriptions[] = $relrow[$foreignData['foreign_display']]; + } else { + $descriptions[] = ''; + } + + $keys[] = $relrow[$foreignData['foreign_field']]; + } + + asort($keys); + + $horizontal_count = 0; + $indexByDescription = 0; + + foreach ($keys as $indexByKeyname => $value) { + list( + $html, + $horizontal_count, + $indexByDescription + ) = self::getHtmlForOneKey( + $horizontal_count, $header, $keys, $indexByKeyname, + $descriptions, $indexByDescription, $current_value + ); + $output .= $html; + } + + $output .= '' + . ''; + + return $output; + } + + /** + * Get the description (possibly truncated) and the title + * + * @param string $description the key name's description + * + * @return array the new description and title + */ + public static function getDescriptionAndTitle($description) + { + $limitChars = $GLOBALS['cfg']['LimitChars']; + if (mb_strlen($description) <= $limitChars) { + $description = htmlspecialchars( + $description + ); + $descriptionTitle = ''; + } else { + $descriptionTitle = htmlspecialchars( + $description + ); + $description = htmlspecialchars( + mb_substr( + $description, 0, $limitChars + ) + . '...' + ); + } + return array($description, $descriptionTitle); + } + + /** + * Function to get html for each column element + * + * @param string $cssClass class="nowrap" or '' + * @param bool $isSelected whether current equals form's value + * @param string $keyname current key + * @param string $description current value + * @param string $title current title + * + * @return string + */ + public static function getHtmlForColumnElement($cssClass, $isSelected, $keyname, + $description, $title + ) { + $keyname = htmlspecialchars($keyname); + $output = '' : '') + . ''; + if ($cssClass !== '') { + $output .= $keyname; + } else { + $output .= $description; + } + + $output .= '' . ($isSelected ? '' : '') . ''; + + return $output; + } + + /** + * Function to get html for show all case + * + * @param array $foreignData foreign data + * + * @return string + */ + public static function getHtmlForShowAll($foreignData) + { + $showall = ''; + if (is_array($foreignData['disp_row'])) { + if ($GLOBALS['cfg']['ShowAll'] + && ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) + ) { + $showall = ''; + } + } + + return $showall; + } + + /** + * Function to get html for the goto page option + * + * @param array $foreignData foreign data + * + * @return string + */ + public static function getHtmlForGotoPage($foreignData) + { + $gotopage = ''; + isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0; + if (!is_array($foreignData['disp_row'])) { + return $gotopage; + } + + $session_max_rows = $GLOBALS['cfg']['MaxRows']; + $pageNow = @floor($pos / $session_max_rows) + 1; + $nbTotalPage = @ceil($foreignData['the_total'] / $session_max_rows); + + if ($foreignData['the_total'] > $GLOBALS['cfg']['MaxRows']) { + $gotopage = Util::pageselector( + 'pos', + $session_max_rows, + $pageNow, + $nbTotalPage, + 200, + 5, + 5, + 20, + 10, + __('Page number:') + ); + } + + return $gotopage; + } + + /** + * Function to get foreign limit + * + * @param string $foreign_showAll foreign navigation + * + * @return string + */ + public static function getForeignLimit($foreign_showAll) + { + if (isset($foreign_showAll) && $foreign_showAll == __('Show all')) { + return null; + } + isset($_REQUEST['pos']) ? $pos = $_REQUEST['pos'] : $pos = 0; + return 'LIMIT ' . $pos . ', ' . intval($GLOBALS['cfg']['MaxRows']) . ' '; + } +} diff --git a/test/libraries/PMA_browse_foreigners_test.php b/test/classes/BrowseForeignersTest.php similarity index 79% rename from test/libraries/PMA_browse_foreigners_test.php rename to test/classes/BrowseForeignersTest.php index 3d1917b331..999adada4e 100644 --- a/test/libraries/PMA_browse_foreigners_test.php +++ b/test/classes/BrowseForeignersTest.php @@ -1,22 +1,20 @@ assertNull( - PMA_getForeignLimit('Show all') + BrowseForeigners::getForeignLimit('Show all') ); $this->assertEquals( 'LIMIT 0, 25 ', - PMA_getForeignLimit(null) + BrowseForeigners::getForeignLimit(null) ); $_REQUEST['pos'] = 10; $this->assertEquals( 'LIMIT 10, 25 ', - PMA_getForeignLimit(null) + BrowseForeigners::getForeignLimit(null) ); $GLOBALS['cfg']['MaxRows'] = 50; $this->assertEquals( 'LIMIT 10, 50 ', - PMA_getForeignLimit(null) + BrowseForeigners::getForeignLimit(null) ); $this->assertEquals( 'LIMIT 10, 50 ', - PMA_getForeignLimit('xyz') + BrowseForeigners::getForeignLimit('xyz') ); } /** - * Test for PMA_getHtmlForShowAll + * Test for BrowseForeigners::getHtmlForShowAll * * @return void */ @@ -73,7 +71,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase { $this->assertEquals( '', - PMA_getHtmlForShowAll(null) + BrowseForeigners::getHtmlForShowAll(null) ); $foreignData = array(); @@ -82,7 +80,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $this->assertEquals( '', - PMA_getHtmlForShowAll($foreignData) + BrowseForeigners::getHtmlForShowAll($foreignData) ); $GLOBALS['cfg']['ShowAll'] = true; @@ -90,7 +88,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $this->assertEquals( '', - PMA_getHtmlForShowAll($foreignData) + BrowseForeigners::getHtmlForShowAll($foreignData) ); $foreignData['the_total'] = 30; @@ -99,12 +97,12 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase '', - PMA_getHtmlForShowAll($foreignData) + BrowseForeigners::getHtmlForShowAll($foreignData) ); } /** - * Test for PMA_getHtmlForGotoPage + * Test for BrowseForeigners::getHtmlForGotoPage * * @return void */ @@ -112,7 +110,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase { $this->assertEquals( '', - PMA_getHtmlForGotoPage(null) + BrowseForeigners::getHtmlForGotoPage(null) ); $_REQUEST['pos'] = 15; @@ -122,11 +120,11 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $this->assertEquals( '', - PMA_getHtmlForGotoPage($foreignData) + BrowseForeigners::getHtmlForGotoPage($foreignData) ); $foreignData['the_total'] = 30; - $result = PMA_getHtmlForGotoPage($foreignData); + $result = BrowseForeigners::getHtmlForGotoPage($foreignData); $this->assertStringStartsWith( 'Page number:', @@ -156,7 +154,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getHtmlForColumnElement + * Test for BrowseForeigners::getHtmlForColumnElement * * @return void */ @@ -167,7 +165,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $keyname = ''; $description = 'foo'; $title = ''; - $result = PMA_getHtmlForColumnElement( + $result = BrowseForeigners::getHtmlForColumnElement( $cssClass, $isSelected, $keyname, $description, $title ); @@ -187,7 +185,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $isSelected = true; $keyname = 'bar'; $title = 'foo'; - $result = PMA_getHtmlForColumnElement( + $result = BrowseForeigners::getHtmlForColumnElement( $cssClass, $isSelected, $keyname, $description, $title ); @@ -210,7 +208,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase } /** - * Test for PMA_getDescriptionAndTitle + * Test for BrowseForeigners::getDescriptionAndTitle * * @return void */ @@ -221,19 +219,19 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $this->assertEquals( array('foobar<baz', ''), - PMA_getDescriptionAndTitle($desc) + BrowseForeigners::getDescriptionAndTitle($desc) ); $GLOBALS['cfg']['LimitChars'] = 5; $this->assertEquals( array('fooba...', 'foobar<baz'), - PMA_getDescriptionAndTitle($desc) + BrowseForeigners::getDescriptionAndTitle($desc) ); } /** - * Test for PMA_getHtmlForRelationalFieldSelection + * Test for BrowseForeigners::getHtmlForRelationalFieldSelection * * @return void */ @@ -248,7 +246,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $current_value = ''; $_REQUEST['rownumber'] = 1; $_REQUEST['foreign_filter'] = '5'; - $result = PMA_getHtmlForRelationalFieldSelection( + $result = BrowseForeigners::getHtmlForRelationalFieldSelection( $db, $table, $field, $foreignData, $fieldkey, $current_value ); @@ -315,7 +313,7 @@ class PMA_BrowseForeignersTest extends PHPUnit_Framework_TestCase $foreignData['disp_row'] = array(); $foreignData['the_total'] = 5; $GLOBALS['cfg']['ShowAll'] = false; - $result = PMA_getHtmlForRelationalFieldSelection( + $result = BrowseForeigners::getHtmlForRelationalFieldSelection( $db, $table, $field, $foreignData, $fieldkey, $current_value );