From 3010b27babd7bbcd46a1af3e4061dfcd9a7e3b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 15 Dec 2017 11:41:53 -0200 Subject: [PATCH 1/5] Use template for _getResultsRow method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Database/Search.php | 61 +++++---------------- templates/database/search/results_row.twig | 42 +++++++++++++++ test/classes/Database/SearchTest.php | 62 ++++++++++------------ 3 files changed, 81 insertions(+), 84 deletions(-) create mode 100644 templates/database/search/results_row.twig diff --git a/libraries/classes/Database/Search.php b/libraries/classes/Database/Search.php index a7c088936e..d99b017fb4 100644 --- a/libraries/classes/Database/Search.php +++ b/libraries/classes/Database/Search.php @@ -8,6 +8,7 @@ namespace PhpMyAdmin\Database; use PhpMyAdmin\Sanitize; +use PhpMyAdmin\Template; use PhpMyAdmin\Url; use PhpMyAdmin\Util; @@ -281,7 +282,7 @@ class Search $res_cnt = intval($GLOBALS['dbi']->fetchValue($newsearchsqls['select_count'])); $num_search_result_total += $res_cnt; // Gets the result row's HTML for a table - $html_output .= $this->_getResultsRow( + $html_output .= $this->getResultsRow( $each_table, $newsearchsqls, $res_cnt ); } // end for @@ -306,58 +307,20 @@ class Search * Provides search results row with browse/delete links. * (for a table) * - * @param string $each_table One of the tables on which search was performed - * @param array $newsearchsqls Contains SQL queries - * @param integer $res_cnt Number of results found + * @param string $table One of the tables on which search was performed + * @param array $newSearchSqls Contains SQL queries + * @param integer $resultCount Number of results found * * @return string HTML row */ - private function _getResultsRow($each_table, array $newsearchsqls, $res_cnt) + private function getResultsRow($table, array $newSearchSqls, $resultCount) { - $this_url_params = array( - 'db' => $GLOBALS['db'], - 'table' => $each_table, - 'goto' => 'db_sql.php', - 'pos' => 0, - 'is_js_confirmed' => 0, - ); - // Start forming search results row - $html_output = ''; - // Displays results count for a table - $html_output .= ''; - $html_output .= sprintf( - _ngettext( - '%1$s match in %2$s', - '%1$s matches in %2$s', $res_cnt - ), - $res_cnt, htmlspecialchars($each_table) - ); - $html_output .= ''; - // Displays browse/delete link if result count > 0 - if ($res_cnt > 0) { - $this_url_params['db'] = htmlspecialchars($GLOBALS['db']); - $this_url_params['table'] = htmlspecialchars($each_table); - $browse_result_path = 'sql.php' . Url::getCommon($this_url_params); - $html_output .= '' - . __('Browse') . ''; - - $delete_result_path = $browse_result_path; - $html_output .= '' - . __('Delete') . ''; - } else { - $html_output .= ' ' - . ' '; - }// end if else - $html_output .= ''; - return $html_output; + return Template::get('database/search/results_row')->render([ + 'result_count' => $resultCount, + 'new_search_sqls' => $newSearchSqls, + 'db' => $GLOBALS['db'], + 'table' => $table, + ]); } /** diff --git a/templates/database/search/results_row.twig b/templates/database/search/results_row.twig new file mode 100644 index 0000000000..20c114fea1 --- /dev/null +++ b/templates/database/search/results_row.twig @@ -0,0 +1,42 @@ + + + {% set result_message %} + {% trans %} + %1$s match in %2$s + {% plural result_count %} + %1$s matches in %2$s + {% endtrans %} + {% endset %} + {{ result_message|format(result_count, table)|raw }} + + {% if result_count > 0 %} + {% set url_params = { + 'db': db, + 'table': table, + 'goto': 'db_sql.php', + 'pos': 0, + 'is_js_confirmed': 0 + } %} + + + {% trans 'Browse' %} + + + + + {% trans 'Delete' %} + + + {% else %} + + + {% endif %} + diff --git a/test/classes/Database/SearchTest.php b/test/classes/Database/SearchTest.php index d2da90c801..afb68cbb5e 100644 --- a/test/classes/Database/SearchTest.php +++ b/test/classes/Database/SearchTest.php @@ -171,27 +171,23 @@ class SearchTest extends PmaTestCase } /** - * Test for _getResultsRow + * Test for getResultsRow * - * @param string $each_table Tables on which search is to be performed - * @param array $newsearchsqls Contains SQL queries - * @param string $output Expected HTML output + * @param string $table Tables on which search is to be performed + * @param array $newSearchSqls Contains SQL queries + * @param string $expected Expected HTML output * * @return void * * @dataProvider providerForTestGetResultsRow */ - public function testGetResultsRow( - $each_table, $newsearchsqls, $output - ) { - - $this->assertEquals( - $output, - $this->_callProtectedFunction( - '_getResultsRow', - array($each_table, $newsearchsqls, 2) - ) + public function testGetResultsRow($table, $newSearchSqls, $needle) + { + $haystack = $this->_callProtectedFunction( + 'getResultsRow', + array($table, $newSearchSqls, 2) ); + $this->assertContains($needle, $haystack); } /** @@ -204,27 +200,23 @@ class SearchTest extends PmaTestCase return array( array( 'table1', - array( - 'SELECT * FROM `pma`.`table1` WHERE FALSE', - 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` WHERE FALSE', - 'select_count' => 2, - 'select_columns' => 'column1', - 'delete' => 'column2' - ), - '2 matches in table1' - . 'Browse' - . 'Delete' + array('select_columns' => 'column1', 'delete' => 'column2'), + '2 matches in table1' + ), + array( + 'table1', + array('select_columns' => 'column1', 'delete' => 'column2'), + 'data-table-name="table1"' + ), + array( + 'table1', + array('select_columns' => 'column1', 'delete' => 'column2'), + 'data-browse-sql="column1"' + ), + array( + 'table1', + array('select_columns' => 'column1', 'delete' => 'column2'), + 'data-delete-sql="column2"' ) ); } From 0e82a5e83e60ec3ace291e78c5f6eb977b1ef0fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 15 Dec 2017 15:44:36 -0200 Subject: [PATCH 2/5] Use template for getResultDivs method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Database/Search.php | 19 +----------- templates/database/search/result_divs.twig | 13 ++++++++ test/classes/Database/SearchTest.php | 36 ++++++++++++++-------- 3 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 templates/database/search/result_divs.twig diff --git a/libraries/classes/Database/Search.php b/libraries/classes/Database/Search.php index d99b017fb4..0e6fcc3ab9 100644 --- a/libraries/classes/Database/Search.php +++ b/libraries/classes/Database/Search.php @@ -433,23 +433,6 @@ class Search */ public function getResultDivs() { - $html_output = ''; - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - // div for browsing results - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= ''; - $html_output .= ''; - return $html_output; + return Template::get('database/search/result_divs')->render(); } } diff --git a/templates/database/search/result_divs.twig b/templates/database/search/result_divs.twig new file mode 100644 index 0000000000..9f5f19ffdd --- /dev/null +++ b/templates/database/search/result_divs.twig @@ -0,0 +1,13 @@ +{# These two table-image and table-link elements display the table name in browse search results #} +
+ +
+{# Div for browsing results #} +
+ {# This browse-results div is used to load the browse and delete results in the db search #} +
+
+ {# This sqlqueryform div is used to load the delete form in the db search #} +
+{# Toggle query box link #} + diff --git a/test/classes/Database/SearchTest.php b/test/classes/Database/SearchTest.php index afb68cbb5e..67ddb48194 100644 --- a/test/classes/Database/SearchTest.php +++ b/test/classes/Database/SearchTest.php @@ -241,19 +241,29 @@ class SearchTest extends PmaTestCase */ public function testGetResultDivs() { - $this->assertEquals( - '
' - . '
' - . '

' - . '
' - . '', - $this->_callProtectedFunction( - 'getResultDivs', - array() - ) + $actual = $this->_callProtectedFunction( + 'getResultDivs', + array() + ); + $this->assertContains( + '
assertContains( + 'assertContains( + '
assertContains( + '
assertContains( + ' Date: Fri, 15 Dec 2017 16:01:57 -0200 Subject: [PATCH 3/5] Remove underscores from methods and properties names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Database/Search.php | 111 ++++++++++++++------------ test/classes/Database/SearchTest.php | 13 ++- 2 files changed, 65 insertions(+), 59 deletions(-) diff --git a/libraries/classes/Database/Search.php b/libraries/classes/Database/Search.php index 0e6fcc3ab9..3dd78afbfe 100644 --- a/libraries/classes/Database/Search.php +++ b/libraries/classes/Database/Search.php @@ -25,56 +25,63 @@ class Search * @access private * @var string */ - private $_db; + private $db; + /** * Table Names * * @access private * @var array */ - private $_tables_names_only; + private $tablesNamesOnly; + /** * Type of search * * @access private * @var array */ - private $_searchTypes; + private $searchTypes; + /** * Already set search type * * @access private * @var integer */ - private $_criteriaSearchType; + private $criteriaSearchType; + /** * Already set search type's description * * @access private * @var string */ - private $_searchTypeDescription; + private $searchTypeDescription; + /** * Search string/regexp * * @access private * @var string */ - private $_criteriaSearchString; + private $criteriaSearchString; + /** * Criteria Tables to search in * * @access private * @var array */ - private $_criteriaTables; + private $criteriaTables; + /** * Restrict the search to this column * * @access private * @var string */ - private $_criteriaColumnName; + private $criteriaColumnName; /** * Public Constructor @@ -83,8 +90,8 @@ class Search */ public function __construct($db) { - $this->_db = $db; - $this->_searchTypes = array( + $this->db = $db; + $this->searchTypes = array( '1' => __('at least one of the words'), '2' => __('all of the words'), '3' => __('the exact phrase as substring'), @@ -92,7 +99,7 @@ class Search '5' => __('as regular expression'), ); // Sets criteria parameters - $this->_setSearchParams(); + $this->setSearchParams(); } /** @@ -100,51 +107,51 @@ class Search * * @return void */ - private function _setSearchParams() + private function setSearchParams() { - $this->_tables_names_only = $GLOBALS['dbi']->getTables($this->_db); + $this->tablesNamesOnly = $GLOBALS['dbi']->getTables($this->db); if (empty($_REQUEST['criteriaSearchType']) || ! is_string($_REQUEST['criteriaSearchType']) || ! array_key_exists( $_REQUEST['criteriaSearchType'], - $this->_searchTypes + $this->searchTypes ) ) { - $this->_criteriaSearchType = 1; + $this->criteriaSearchType = 1; unset($_REQUEST['submit_search']); } else { - $this->_criteriaSearchType = (int) $_REQUEST['criteriaSearchType']; - $this->_searchTypeDescription - = $this->_searchTypes[$_REQUEST['criteriaSearchType']]; + $this->criteriaSearchType = (int) $_REQUEST['criteriaSearchType']; + $this->searchTypeDescription + = $this->searchTypes[$_REQUEST['criteriaSearchType']]; } if (empty($_REQUEST['criteriaSearchString']) || ! is_string($_REQUEST['criteriaSearchString']) ) { - $this->_criteriaSearchString = ''; + $this->criteriaSearchString = ''; unset($_REQUEST['submit_search']); } else { - $this->_criteriaSearchString = $_REQUEST['criteriaSearchString']; + $this->criteriaSearchString = $_REQUEST['criteriaSearchString']; } - $this->_criteriaTables = array(); + $this->criteriaTables = array(); if (empty($_REQUEST['criteriaTables']) || ! is_array($_REQUEST['criteriaTables']) ) { unset($_REQUEST['submit_search']); } else { - $this->_criteriaTables = array_intersect( - $_REQUEST['criteriaTables'], $this->_tables_names_only + $this->criteriaTables = array_intersect( + $_REQUEST['criteriaTables'], $this->tablesNamesOnly ); } if (empty($_REQUEST['criteriaColumnName']) || ! is_string($_REQUEST['criteriaColumnName']) ) { - unset($this->_criteriaColumnName); + unset($this->criteriaColumnName); } else { - $this->_criteriaColumnName = $GLOBALS['dbi']->escapeString( + $this->criteriaColumnName = $GLOBALS['dbi']->escapeString( $_REQUEST['criteriaColumnName'] ); } @@ -166,7 +173,7 @@ class Search * count * strlen */ - private function _getSearchSqls($table) + private function getSearchSqls($table) { // Statement types $sqlstr_select = 'SELECT'; @@ -176,7 +183,7 @@ class Search . Util::backquote($GLOBALS['db']) . '.' . Util::backquote($table); // Gets where clause for the query - $where_clause = $this->_getWhereClause($table); + $where_clause = $this->getWhereClause($table); // Builds complete queries $sql = array(); $sql['select_columns'] = $sqlstr_select . ' * ' . $sqlstr_from @@ -197,22 +204,22 @@ class Search * * @return string The generated where clause */ - private function _getWhereClause($table) + private function getWhereClause($table) { // Columns to select $allColumns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $table); $likeClauses = array(); // Based on search type, decide like/regex & '%'/'' - $like_or_regex = (($this->_criteriaSearchType == 5) ? 'REGEXP' : 'LIKE'); - $automatic_wildcard = (($this->_criteriaSearchType < 4) ? '%' : ''); + $like_or_regex = (($this->criteriaSearchType == 5) ? 'REGEXP' : 'LIKE'); + $automatic_wildcard = (($this->criteriaSearchType < 4) ? '%' : ''); // For "as regular expression" (search option 5), LIKE won't be used // Usage example: If user is searching for a literal $ in a regexp search, // he should enter \$ as the value. $criteriaSearchStringEscaped = $GLOBALS['dbi']->escapeString( - $this->_criteriaSearchString + $this->criteriaSearchString ); // Extract search words or pattern - $search_words = (($this->_criteriaSearchType > 2) + $search_words = (($this->criteriaSearchType > 2) ? array($criteriaSearchStringEscaped) : explode(' ', $criteriaSearchStringEscaped)); @@ -224,9 +231,9 @@ class Search $likeClausesPerColumn = array(); // for each column in the table foreach ($allColumns as $column) { - if (! isset($this->_criteriaColumnName) - || strlen($this->_criteriaColumnName) === 0 - || $column['Field'] == $this->_criteriaColumnName + if (! isset($this->criteriaColumnName) + || strlen($this->criteriaColumnName) === 0 + || $column['Field'] == $this->criteriaColumnName ) { $column = 'CONVERT(' . Util::backquote($column['Field']) . ' USING utf8)'; @@ -241,7 +248,7 @@ class Search } } // end for // Use 'OR' if 'at least one word' is to be searched, else use 'AND' - $implode_str = ($this->_criteriaSearchType == 1 ? ' OR ' : ' AND '); + $implode_str = ($this->criteriaSearchType == 1 ? ' OR ' : ' AND '); if (empty($likeClauses)) { // this could happen when the "inside column" does not exist // in any selected tables @@ -268,16 +275,16 @@ class Search . '' . sprintf( __('Search results for "%s" %s:'), - htmlspecialchars($this->_criteriaSearchString), - $this->_searchTypeDescription + htmlspecialchars($this->criteriaSearchString), + $this->searchTypeDescription ) . ''; $num_search_result_total = 0; // For each table selected as search criteria - foreach ($this->_criteriaTables as $each_table) { + foreach ($this->criteriaTables as $each_table) { // Gets the SQL statements - $newsearchsqls = $this->_getSearchSqls($each_table); + $newsearchsqls = $this->getSearchSqls($each_table); // Executes the "COUNT" statement $res_cnt = intval($GLOBALS['dbi']->fetchValue($newsearchsqls['select_count'])); $num_search_result_total += $res_cnt; @@ -288,7 +295,7 @@ class Search } // end for $html_output .= ''; // Displays total number of matches - if (count($this->_criteriaTables) > 1) { + if (count($this->criteriaTables) > 1) { $html_output .= '

'; $html_output .= sprintf( _ngettext( @@ -345,7 +352,7 @@ class Search . ''; $html_output .= ''; + . ' value="' . htmlspecialchars($this->criteriaSearchString) . '" />'; $html_output .= ''; $html_output .= ''; // choices for types of search @@ -353,24 +360,24 @@ class Search $html_output .= '' . __('Find:') . ''; $html_output .= ''; $choices = array( - '1' => $this->_searchTypes[1] . ' ' + '1' => $this->searchTypes[1] . ' ' . Util::showHint( __('Words are separated by a space character (" ").') ), - '2' => $this->_searchTypes[2] . ' ' + '2' => $this->searchTypes[2] . ' ' . Util::showHint( __('Words are separated by a space character (" ").') ), - '3' => $this->_searchTypes[3], - '4' => $this->_searchTypes[4], - '5' => $this->_searchTypes[5] . ' ' + '3' => $this->searchTypes[3], + '4' => $this->searchTypes[4], + '5' => $this->searchTypes[5] . ' ' . Util::showMySQLDocu('Regexp') ); // 4th parameter set to true to add line breaks // 5th parameter set to false to avoid htmlspecialchars() escaping // in the label since we have some HTML in some labels $html_output .= Util::getRadioFields( - 'criteriaSearchType', $choices, $this->_criteriaSearchType, true, false + 'criteriaSearchType', $choices, $this->criteriaSearchType, true, false ); $html_output .= ''; // displays table names as select options @@ -379,8 +386,8 @@ class Search $html_output .= ''; $html_output .= ''; $html_output .= ''; diff --git a/test/classes/Database/SearchTest.php b/test/classes/Database/SearchTest.php index 67ddb48194..da8a00b329 100644 --- a/test/classes/Database/SearchTest.php +++ b/test/classes/Database/SearchTest.php @@ -76,7 +76,7 @@ class SearchTest extends PmaTestCase * * @return the output from the protected method. */ - private function _callProtectedFunction($name, $params) + private function callProtectedFunction($name, $params) { $class = new ReflectionClass(Search::class); $method = $class->getMethod($name); @@ -97,8 +97,8 @@ class SearchTest extends PmaTestCase $this->object = new Search('pma_test'); $this->assertEquals( $expected, - $this->_callProtectedFunction( - '_getWhereClause', + $this->callProtectedFunction( + 'getWhereClause', array('table1') ) ); @@ -149,8 +149,8 @@ class SearchTest extends PmaTestCase 'WHERE FALSE', 'delete' => 'DELETE FROM `pma`.`table1` WHERE FALSE' ), - $this->_callProtectedFunction( - '_getSearchSqls', + $this->callProtectedFunction( + 'getSearchSqls', array('table1') ) ); @@ -241,7 +241,7 @@ class SearchTest extends PmaTestCase */ public function testGetResultDivs() { - $actual = $this->_callProtectedFunction( + $actual = $this->callProtectedFunction( 'getResultDivs', array() ); @@ -266,5 +266,4 @@ class SearchTest extends PmaTestCase $actual ); } - } From aa48445e9a3601750956bfaba2f31b5b39cb9ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 15 Dec 2017 18:44:28 -0200 Subject: [PATCH 4/5] Use template for getSelectionForm method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Database/Search.php | 93 +++---------------- templates/database/search/selection_form.twig | 70 ++++++++++++++ 2 files changed, 81 insertions(+), 82 deletions(-) create mode 100644 templates/database/search/selection_form.twig diff --git a/libraries/classes/Database/Search.php b/libraries/classes/Database/Search.php index 3dd78afbfe..baf7b4f30e 100644 --- a/libraries/classes/Database/Search.php +++ b/libraries/classes/Database/Search.php @@ -337,28 +337,6 @@ class Search */ public function getSelectionForm() { - $html_output = ''; - $html_output .= '

'; - $html_output .= Url::getHiddenInputs($GLOBALS['db']); - $html_output .= '
'; - // set legend caption - $html_output .= '' . __('Search in database') . ''; - $html_output .= ''; - // inputbox for search phrase - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - // choices for types of search - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - // displays table names as select options - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - // Displays 'select all' and 'unselect all' links - $alter_select = '' - . __('Select all') . '  / '; - $alter_select .= '' - . __('Unselect all') . ''; - $html_output .= ''; - // Inputbox for column name entry - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= '
' . __('Words or values to search for (wildcard: "%"):') - . ''; - $html_output .= '
' . __('Find:') . ''; $choices = array( '1' => $this->searchTypes[1] . ' ' . Util::showHint( @@ -370,67 +348,18 @@ class Search ), '3' => $this->searchTypes[3], '4' => $this->searchTypes[4], - '5' => $this->searchTypes[5] . ' ' - . Util::showMySQLDocu('Regexp') + '5' => $this->searchTypes[5] . ' ' . Util::showMySQLDocu('Regexp') ); - // 4th parameter set to true to add line breaks - // 5th parameter set to false to avoid htmlspecialchars() escaping - // in the label since we have some HTML in some labels - $html_output .= Util::getRadioFields( - 'criteriaSearchType', $choices, $this->criteriaSearchType, true, false - ); - $html_output .= '
' . __('Inside tables:') . ''; - $html_output .= ''; - $html_output .= '
' - . $alter_select . '
' . __('Inside column:') . '
'; - $html_output .= '
'; - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= '
'; - $html_output .= '
' - . '
'; - - return $html_output; + return Template::get('database/search/selection_form')->render([ + 'db' => $this->db, + 'choices' => $choices, + 'criteria_search_string' => $this->criteriaSearchString, + 'criteria_search_type' => $this->criteriaSearchType, + 'criteria_tables' => $this->criteriaTables, + 'tables_names_only' => $this->tablesNamesOnly, + 'criteria_column_name' => isset($this->criteriaColumnName) + ? $this->criteriaColumnName : null, + ]); } /** diff --git a/templates/database/search/selection_form.twig b/templates/database/search/selection_form.twig new file mode 100644 index 0000000000..5ae4256b53 --- /dev/null +++ b/templates/database/search/selection_form.twig @@ -0,0 +1,70 @@ + +
+ {{ Url_getHiddenInputs(db) }} +
+ {% trans 'Search in database' %} + + + + + + + + + + + + + + + + + {# Inputbox for column name entry #} + + + + +
+ {% trans 'Words or values to search for (wildcard: "%"):' %} + + +
{% trans 'Find:' %} + {# 4th parameter set to true to add line breaks #} + {# 5th parameter set to false to avoid htmlspecialchars() escaping + in the label since we have some HTML in some labels #} + {{ Util_getRadioFields( + 'criteriaSearchType', + choices, + criteria_search_type, + true, + false + ) }} +
{% trans 'Inside tables:' %} + +
+ + {% trans 'Select all' %} + / + + {% trans 'Unselect all' %} + +
{% trans 'Inside column:' %} + +
+
+
+ +
+
+
+ +
From f9e4a1c6af071c8c97be18697ac7ac94a233938a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 15 Dec 2017 19:26:56 -0200 Subject: [PATCH 5/5] Use template for getSearchResults method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maurício Meneghini Fauth --- libraries/classes/Database/Search.php | 75 ++++++---------------- templates/database/search/results.twig | 62 ++++++++++++++++++ templates/database/search/results_row.twig | 42 ------------ test/classes/Database/SearchTest.php | 56 +--------------- 4 files changed, 84 insertions(+), 151 deletions(-) create mode 100644 templates/database/search/results.twig delete mode 100644 templates/database/search/results_row.twig diff --git a/libraries/classes/Database/Search.php b/libraries/classes/Database/Search.php index baf7b4f30e..151fb12351 100644 --- a/libraries/classes/Database/Search.php +++ b/libraries/classes/Database/Search.php @@ -7,9 +7,7 @@ */ namespace PhpMyAdmin\Database; -use PhpMyAdmin\Sanitize; use PhpMyAdmin\Template; -use PhpMyAdmin\Url; use PhpMyAdmin\Util; /** @@ -268,65 +266,32 @@ class Search */ public function getSearchResults() { - $html_output = ''; - // Displays search string - $html_output .= '
' - . '' - . ''; - - $num_search_result_total = 0; + $resultTotal = 0; + $rows = []; // For each table selected as search criteria - foreach ($this->criteriaTables as $each_table) { + foreach ($this->criteriaTables as $eachTable) { // Gets the SQL statements - $newsearchsqls = $this->getSearchSqls($each_table); + $newSearchSqls = $this->getSearchSqls($eachTable); // Executes the "COUNT" statement - $res_cnt = intval($GLOBALS['dbi']->fetchValue($newsearchsqls['select_count'])); - $num_search_result_total += $res_cnt; + $resultCount = intval($GLOBALS['dbi']->fetchValue( + $newSearchSqls['select_count'] + )); + $resultTotal += $resultCount; // Gets the result row's HTML for a table - $html_output .= $this->getResultsRow( - $each_table, $newsearchsqls, $res_cnt - ); - } // end for - $html_output .= '
' - . sprintf( - __('Search results for "%s" %s:'), - htmlspecialchars($this->criteriaSearchString), - $this->searchTypeDescription - ) - . '
'; - // Displays total number of matches - if (count($this->criteriaTables) > 1) { - $html_output .= '

'; - $html_output .= sprintf( - _ngettext( - 'Total: %s match', - 'Total: %s matches', - $num_search_result_total - ), - $num_search_result_total - ); - $html_output .= '

'; + $rows[] = [ + 'table' => $eachTable, + 'new_search_sqls' => $newSearchSqls, + 'result_count' => $resultCount, + ]; } - return $html_output; - } - /** - * Provides search results row with browse/delete links. - * (for a table) - * - * @param string $table One of the tables on which search was performed - * @param array $newSearchSqls Contains SQL queries - * @param integer $resultCount Number of results found - * - * @return string HTML row - */ - private function getResultsRow($table, array $newSearchSqls, $resultCount) - { - return Template::get('database/search/results_row')->render([ - 'result_count' => $resultCount, - 'new_search_sqls' => $newSearchSqls, - 'db' => $GLOBALS['db'], - 'table' => $table, + return Template::get('database/search/results')->render([ + 'db' => $this->db, + 'rows' => $rows, + 'result_total' => $resultTotal, + 'criteria_tables' => $this->criteriaTables, + 'criteria_search_string' => $this->criteriaSearchString, + 'search_type_description' => $this->searchTypeDescription, ]); } diff --git a/templates/database/search/results.twig b/templates/database/search/results.twig new file mode 100644 index 0000000000..efe5529615 --- /dev/null +++ b/templates/database/search/results.twig @@ -0,0 +1,62 @@ + + + {% for row in rows %} + + + {% if row.result_count > 0 %} + {% set url_params = { + 'db': db, + 'table': row.table, + 'goto': 'db_sql.php', + 'pos': 0, + 'is_js_confirmed': 0 + } %} + + + {% else %} + + + {% endif %} + + {% endfor %} +
+ {{ 'Search results for "%s" %s:'|format( + criteria_search_string, + search_type_description + )|raw }} +
+ {% set result_message %} + {% trans %} + %1$s match in %2$s + {% plural row.result_count %} + %1$s matches in %2$s + {% endtrans %} + {% endset %} + {{ result_message|format(row.result_count, row.table)|raw }} + + + {% trans 'Browse' %} + + + + {% trans 'Delete' %} + +
+ +{% if criteria_tables|length > 1 %} +

+ {% trans %} + Total: {{ count }} match + {% plural result_total %} + Total: {{ count }} matches + {% endtrans %} +

+{% endif %} diff --git a/templates/database/search/results_row.twig b/templates/database/search/results_row.twig deleted file mode 100644 index 20c114fea1..0000000000 --- a/templates/database/search/results_row.twig +++ /dev/null @@ -1,42 +0,0 @@ - - - {% set result_message %} - {% trans %} - %1$s match in %2$s - {% plural result_count %} - %1$s matches in %2$s - {% endtrans %} - {% endset %} - {{ result_message|format(result_count, table)|raw }} - - {% if result_count > 0 %} - {% set url_params = { - 'db': db, - 'table': table, - 'goto': 'db_sql.php', - 'pos': 0, - 'is_js_confirmed': 0 - } %} - - - {% trans 'Browse' %} - - - - - {% trans 'Delete' %} - - - {% else %} - - - {% endif %} - diff --git a/test/classes/Database/SearchTest.php b/test/classes/Database/SearchTest.php index da8a00b329..93dfd3c5a0 100644 --- a/test/classes/Database/SearchTest.php +++ b/test/classes/Database/SearchTest.php @@ -163,64 +163,12 @@ class SearchTest extends PmaTestCase */ public function testGetSearchResults() { - $this->assertEquals( - '
Search results ' - . 'for "" :
', + $this->assertContains( + 'Search results for "" :', $this->object->getSearchResults() ); } - /** - * Test for getResultsRow - * - * @param string $table Tables on which search is to be performed - * @param array $newSearchSqls Contains SQL queries - * @param string $expected Expected HTML output - * - * @return void - * - * @dataProvider providerForTestGetResultsRow - */ - public function testGetResultsRow($table, $newSearchSqls, $needle) - { - $haystack = $this->_callProtectedFunction( - 'getResultsRow', - array($table, $newSearchSqls, 2) - ); - $this->assertContains($needle, $haystack); - } - - /** - * Data provider for testGetResultRow - * - * @return array provider for testGetResultsRow - */ - public function providerForTestGetResultsRow() - { - return array( - array( - 'table1', - array('select_columns' => 'column1', 'delete' => 'column2'), - '2 matches in table1' - ), - array( - 'table1', - array('select_columns' => 'column1', 'delete' => 'column2'), - 'data-table-name="table1"' - ), - array( - 'table1', - array('select_columns' => 'column1', 'delete' => 'column2'), - 'data-browse-sql="column1"' - ), - array( - 'table1', - array('select_columns' => 'column1', 'delete' => 'column2'), - 'data-delete-sql="column2"' - ) - ); - } - /** * Test for getSelectionForm *