From 23107c0521cf397edd559afd232ff3a59427111c Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Wed, 27 Jun 2012 07:58:23 +0530 Subject: [PATCH 1/7] Slice PMA_dbSearchGetWhereClause out of PMA_getSearchSqls --- db_search.php | 113 ++++++++++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 49 deletions(-) diff --git a/db_search.php b/db_search.php index 2f4e08a79b..1fc08c992b 100644 --- a/db_search.php +++ b/db_search.php @@ -136,69 +136,29 @@ if (isset($_REQUEST['submit_search'])) { // Statement types $sqlstr_select = 'SELECT'; $sqlstr_delete = 'DELETE'; - - // Fields to select - $tblfields = PMA_DBI_get_columns($GLOBALS['db'], $table); - // Table to use $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); - + // Search words or pattern $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str)); $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE'); $automatic_wildcard = (($search_option < 3) ? '%' : ''); - $fieldslikevalues = array(); - foreach ($search_words as $search_word) { - // Eliminates empty values - if (strlen($search_word) === 0) { - continue; - } - - $thefieldlikevalue = array(); - foreach ($tblfields as $tblfield) { - if (! isset($field) || strlen($field) == 0 || $tblfield['Field'] == $field) { - // Drizzle has no CONVERT and all text columns are UTF-8 - if (PMA_DRIZZLE) { - $thefieldlikevalue[] = PMA_backquote($tblfield['Field']) - . ' ' . $like_or_regex . ' ' - . "'" . $automatic_wildcard - . $search_word - . $automatic_wildcard . "'"; - } else { - $thefieldlikevalue[] = 'CONVERT(' . PMA_backquote($tblfield['Field']) . ' USING utf8)' - . ' ' . $like_or_regex . ' ' - . "'" . $automatic_wildcard - . $search_word - . $automatic_wildcard . "'"; - } - } - } // end for - - if (count($thefieldlikevalue) > 0) { - $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue); - } - } // end for - - $implode_str = ($search_option == 1 ? ' OR ' : ' AND '); - if ( empty($fieldslikevalues)) { - // this could happen when the "inside field" does not exist - // in any selected tables - $sqlstr_where = ' WHERE FALSE'; - } else { - $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')'; - } - unset($fieldslikevalues); + $where_clause = PMA_dbSearchGetWhereClause( + $table, $search_words, $search_option, $field, $like_or_regex, + $automatic_wildcard + ); // Builds complete queries - $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where; + $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $where_clause; // here, I think we need to still use the COUNT clause, even for // VIEWs, anyway we have a WHERE clause that should limit results - $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where; - $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where; + $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $where_clause; + $sql['delete'] = $sqlstr_delete . $sqlstr_from . $where_clause; return $sql; } // end of the "PMA_getSearchSqls()" function + $response->addHTML( PMA_dbSearchGetSearchResults( $tables_selected, $searched, $option_str, @@ -207,6 +167,61 @@ if (isset($_REQUEST['submit_search'])) { ); } // end 1. +/** + * Provides where clause for bulding SQL query + * + * @param string $table the table name + * @param integer $search_words Search words or pattern + * @param integer $search_option type of search + * (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) + * @param string $field Restrict the search to this field + * @param string $like_or_regex Whether to use 'LIKE' or 'REGEXP' + * @param string $automatic_wildcard Use automatic wildcard + * + * @return string The generated where clause + */ +function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, $field, + $like_or_regex, $automatic_wildcard +) { + $where_clause = ''; + // Fields to select + $tblfields = PMA_DBI_get_columns($GLOBALS['db'], $table); + $fieldslikevalues = array(); + + foreach ($search_words as $search_word) { + // Eliminates empty values + if (strlen($search_word) === 0) { + continue; + } + $thefieldlikevalue = array(); + // for each field in the table + foreach ($tblfields as $tblfield) { + if (! isset($field) || strlen($field) == 0 || $tblfield['Field'] == $field) { + // Drizzle has no CONVERT and all text columns are UTF-8 + $column = ((PMA_DRIZZLE) + ? PMA_backquote($tblfield['Field']) + : 'CONVERT(' . PMA_backquote($tblfield['Field']) . ' USING utf8)'); + $thefieldlikevalue[] = $column . ' ' . $like_or_regex . ' ' + . "'" . $automatic_wildcard . $search_word . $automatic_wildcard . "'"; + } + } // end for + if (count($thefieldlikevalue) > 0) { + $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue); + } + } // end for + + $implode_str = ($search_option == 1 ? ' OR ' : ' AND '); + if ( empty($fieldslikevalues)) { + // this could happen when the "inside field" does not exist + // in any selected tables + $where_clause = ' WHERE FALSE'; + } else { + $where_clause = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')'; + } + return $where_clause; +} + /** * Displays database search results * From e23ba53da008f6af60b5548925ce13ab04e449d5 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Wed, 27 Jun 2012 08:04:45 +0530 Subject: [PATCH 2/7] Rearrange content in db_search.php --- db_search.php | 131 +++++++++++++++++++++++++------------------------- 1 file changed, 65 insertions(+), 66 deletions(-) diff --git a/db_search.php b/db_search.php index 1fc08c992b..727bef47af 100644 --- a/db_search.php +++ b/db_search.php @@ -109,56 +109,6 @@ if ( $GLOBALS['is_ajax_request'] != true) { * 1. Main search form has been submitted */ if (isset($_REQUEST['submit_search'])) { - - /** - * Builds the SQL search query - * - * @param string $table the table name - * @param string $field restrict the search to this field - * @param string $search_str the string to search - * @param integer $search_option type of search - * (1 -> 1 word at least, 2 -> all words, - * 3 -> exact string, 4 -> regexp) - * - * @return array 3 SQL querys (for count, display and delete results) - * - * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this? - * PMA_backquote - * PMA_DBI_free_result - * PMA_DBI_fetch_assoc - * $GLOBALS['db'] - * explode - * count - * strlen - */ - function PMA_getSearchSqls($table, $field, $search_str, $search_option) - { - // Statement types - $sqlstr_select = 'SELECT'; - $sqlstr_delete = 'DELETE'; - // Table to use - $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); - // Search words or pattern - $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str)); - - $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE'); - $automatic_wildcard = (($search_option < 3) ? '%' : ''); - - $where_clause = PMA_dbSearchGetWhereClause( - $table, $search_words, $search_option, $field, $like_or_regex, - $automatic_wildcard - ); - - // Builds complete queries - $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $where_clause; - // here, I think we need to still use the COUNT clause, even for - // VIEWs, anyway we have a WHERE clause that should limit results - $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $where_clause; - $sql['delete'] = $sqlstr_delete . $sqlstr_from . $where_clause; - - return $sql; - } // end of the "PMA_getSearchSqls()" function - $response->addHTML( PMA_dbSearchGetSearchResults( $tables_selected, $searched, $option_str, @@ -167,6 +117,71 @@ if (isset($_REQUEST['submit_search'])) { ); } // end 1. +/** + * If we are in an Ajax request, we need to exit after displaying all the HTML + */ +if ($GLOBALS['is_ajax_request'] == true) { + exit; +} else { + $response->addHTML('');//end searchresults div +} + +$response->addHTML( + PMA_dbSearchGetSelectionForm( + $searched, $search_option, $tables_names_only, $tables_selected, $url_params, + (! empty($field_str) ? $field_str : '') + ) +); + +/** + * Builds the SQL search query + * + * @param string $table the table name + * @param string $field restrict the search to this field + * @param string $search_str the string to search + * @param integer $search_option type of search + * (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) + * + * @return array 3 SQL querys (for count, display and delete results) + * + * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this? + * PMA_backquote + * PMA_DBI_free_result + * PMA_DBI_fetch_assoc + * $GLOBALS['db'] + * explode + * count + * strlen + */ +function PMA_getSearchSqls($table, $field, $search_str, $search_option) +{ + // Statement types + $sqlstr_select = 'SELECT'; + $sqlstr_delete = 'DELETE'; + // Table to use + $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); + // Search words or pattern + $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str)); + + $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE'); + $automatic_wildcard = (($search_option < 3) ? '%' : ''); + + $where_clause = PMA_dbSearchGetWhereClause( + $table, $search_words, $search_option, $field, $like_or_regex, + $automatic_wildcard + ); + + // Builds complete queries + $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $where_clause; + // here, I think we need to still use the COUNT clause, even for + // VIEWs, anyway we have a WHERE clause that should limit results + $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $where_clause; + $sql['delete'] = $sqlstr_delete . $sqlstr_from . $where_clause; + + return $sql; +} + /** * Provides where clause for bulding SQL query * @@ -343,15 +358,6 @@ function PMA_dbSearchGetResultsRow($each_table, $newsearchsqls, $odd_row) return $html_output; } -/** - * If we are in an Ajax request, we need to exit after displaying all the HTML - */ -if ($GLOBALS['is_ajax_request'] == true) { - exit; -} else { - $response->addHTML('');//end searchresults div -} - /** * Provides the main search form's html * @@ -469,11 +475,4 @@ function getResultDivs() $html_output .= ''; return $html_output; } - -$response->addHTML( - PMA_dbSearchGetSelectionForm( - $searched, $search_option, $tables_names_only, $tables_selected, $url_params, - (! empty($field_str) ? $field_str : '') - ) -); ?> From 2c4a14a8f820353912591572b7f933f080c18d63 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Wed, 27 Jun 2012 08:22:41 +0530 Subject: [PATCH 3/7] Fix broken delete feature in database search --- db_search.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/db_search.php b/db_search.php index 727bef47af..0c91075e4e 100644 --- a/db_search.php +++ b/db_search.php @@ -254,12 +254,6 @@ function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, $fiel function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, $search_str, $search_option, $field_str = null ) { - $this_url_params = array( - 'db' => $GLOBALS['db'], - 'goto' => 'db_sql.php', - 'pos' => 0, - 'is_js_confirmed' => 0, - ); $html_output = ''; // Displays search string $html_output .= '
' @@ -317,6 +311,12 @@ function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, */ function PMA_dbSearchGetResultsRow($each_table, $newsearchsqls, $odd_row) { + $this_url_params = array( + 'db' => $GLOBALS['db'], + 'goto' => 'db_sql.php', + 'pos' => 0, + 'is_js_confirmed' => 0, + ); $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']); // Start forming search results row $html_output = ''; From b19d201c16af86c6e76e52ae7b6df61299f3a949 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Thu, 28 Jun 2012 07:17:40 +0530 Subject: [PATCH 4/7] Make variable names more readable in db_search.php --- db_search.php | 120 +++++++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 55 deletions(-) diff --git a/db_search.php b/db_search.php index 0c91075e4e..d6d1e08796 100644 --- a/db_search.php +++ b/db_search.php @@ -89,10 +89,12 @@ if (isset($_REQUEST['selectall'])) { $tables_selected = array(); } -if (empty($_REQUEST['field_str']) || ! is_string($_REQUEST['field_str'])) { - unset($field_str); +if (empty($_REQUEST['criteriaColumnName']) + || ! is_string($_REQUEST['criteriaColumnName']) +) { + unset($criteriaColumnName); } else { - $field_str = PMA_sqlAddSlashes($_REQUEST['field_str'], true); + $criteriaColumnName = PMA_sqlAddSlashes($_REQUEST['criteriaColumnName'], true); } /** @@ -112,7 +114,8 @@ if (isset($_REQUEST['submit_search'])) { $response->addHTML( PMA_dbSearchGetSearchResults( $tables_selected, $searched, $option_str, - $search_str, $search_option, (! empty($field_str) ? $field_str : '') + $search_str, $search_option, + (! empty($criteriaColumnName) ? $criteriaColumnName : '') ) ); } // end 1. @@ -129,19 +132,19 @@ if ($GLOBALS['is_ajax_request'] == true) { $response->addHTML( PMA_dbSearchGetSelectionForm( $searched, $search_option, $tables_names_only, $tables_selected, $url_params, - (! empty($field_str) ? $field_str : '') + (! empty($criteriaColumnName) ? $criteriaColumnName : '') ) ); /** * Builds the SQL search query * - * @param string $table the table name - * @param string $field restrict the search to this field - * @param string $search_str the string to search - * @param integer $search_option type of search - * (1 -> 1 word at least, 2 -> all words, - * 3 -> exact string, 4 -> regexp) + * @param string $table The table name + * @param string $criteriaColumnName Restrict the search to this column + * @param string $search_str the string to search + * @param integer $search_option type of search + * (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) * * @return array 3 SQL querys (for count, display and delete results) * @@ -154,7 +157,7 @@ $response->addHTML( * count * strlen */ -function PMA_getSearchSqls($table, $field, $search_str, $search_option) +function PMA_getSearchSqls($table, $criteriaColumnName, $search_str, $search_option) { // Statement types $sqlstr_select = 'SELECT'; @@ -162,21 +165,23 @@ function PMA_getSearchSqls($table, $field, $search_str, $search_option) // Table to use $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); // Search words or pattern - $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str)); + $search_words = (($search_option > 2) + ? array($search_str) : explode(' ', $search_str)); $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE'); $automatic_wildcard = (($search_option < 3) ? '%' : ''); $where_clause = PMA_dbSearchGetWhereClause( - $table, $search_words, $search_option, $field, $like_or_regex, + $table, $search_words, $search_option, $criteriaColumnName, $like_or_regex, $automatic_wildcard ); // Builds complete queries - $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $where_clause; + $sql['select_columns'] = $sqlstr_select . ' * ' . $sqlstr_from . $where_clause; // here, I think we need to still use the COUNT clause, even for // VIEWs, anyway we have a WHERE clause that should limit results - $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $where_clause; + $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' + . $sqlstr_from . $where_clause; $sql['delete'] = $sqlstr_delete . $sqlstr_from . $where_clause; return $sql; @@ -190,49 +195,54 @@ function PMA_getSearchSqls($table, $field, $search_str, $search_option) * @param integer $search_option type of search * (1 -> 1 word at least, 2 -> all words, * 3 -> exact string, 4 -> regexp) - * @param string $field Restrict the search to this field + * @param string $criteriaColumnName Restrict the search to this column * @param string $like_or_regex Whether to use 'LIKE' or 'REGEXP' * @param string $automatic_wildcard Use automatic wildcard * * @return string The generated where clause */ -function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, $field, - $like_or_regex, $automatic_wildcard +function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, + $criteriaColumnName, $like_or_regex, $automatic_wildcard ) { $where_clause = ''; - // Fields to select - $tblfields = PMA_DBI_get_columns($GLOBALS['db'], $table); - $fieldslikevalues = array(); + // Columns to select + $allColumns = PMA_DBI_get_columns($GLOBALS['db'], $table); + $likeClauses = array(); foreach ($search_words as $search_word) { // Eliminates empty values if (strlen($search_word) === 0) { continue; } - $thefieldlikevalue = array(); - // for each field in the table - foreach ($tblfields as $tblfield) { - if (! isset($field) || strlen($field) == 0 || $tblfield['Field'] == $field) { + $likeClausesPerColumn = array(); + // for each column in the table + foreach ($allColumns as $column) { + if (! isset($criteriaColumnName) + || strlen($criteriaColumnName) == 0 + || $column['Field'] == $criteriaColumnName + ) { // Drizzle has no CONVERT and all text columns are UTF-8 $column = ((PMA_DRIZZLE) - ? PMA_backquote($tblfield['Field']) - : 'CONVERT(' . PMA_backquote($tblfield['Field']) . ' USING utf8)'); - $thefieldlikevalue[] = $column . ' ' . $like_or_regex . ' ' - . "'" . $automatic_wildcard . $search_word . $automatic_wildcard . "'"; + ? PMA_backquote($column['Field']) + : 'CONVERT(' . PMA_backquote($column['Field']) . ' USING utf8)'); + $likeClausesPerColumn[] = $column . ' ' . $like_or_regex . ' ' + . "'" + . $automatic_wildcard . $search_word . $automatic_wildcard + . "'"; } } // end for - if (count($thefieldlikevalue) > 0) { - $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue); + if (count($likeClausesPerColumn) > 0) { + $likeClauses[] = implode(' OR ', $likeClausesPerColumn); } } // end for $implode_str = ($search_option == 1 ? ' OR ' : ' AND '); - if ( empty($fieldslikevalues)) { - // this could happen when the "inside field" does not exist + if ( empty($likeClauses)) { + // this could happen when the "inside column" does not exist // in any selected tables $where_clause = ' WHERE FALSE'; } else { - $where_clause = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')'; + $where_clause = ' WHERE (' . implode(') ' . $implode_str . ' (', $likeClauses) . ')'; } return $where_clause; } @@ -240,19 +250,19 @@ function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, $fiel /** * Displays database search results * - * @param array $tables_selected Tables on which search is to be performed - * @param string $searched The search word/phrase/regexp - * @param string $option_str Type of search - * @param string $search_str the string to search - * @param integer $search_option type of search - * (1 -> 1 word at least, 2 -> all words, - * 3 -> exact string, 4 -> regexp) - * @param string $field_str Restrict the search to this field + * @param array $tables_selected Tables on which search is to be performed + * @param string $searched The search word/phrase/regexp + * @param string $option_str Type of search + * @param string $search_str the string to search + * @param integer $search_option type of search + * (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) + * @param string $criteriaColumnName Restrict the search to this column * * @return string HTML for search results */ function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, - $search_str, $search_option, $field_str = null + $search_str, $search_option, $criteriaColumnName = null ) { $html_output = ''; // Displays search string @@ -271,7 +281,7 @@ function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, foreach ($tables_selected as $each_table) { // Gets the SQL statements $newsearchsqls = PMA_getSearchSqls( - $each_table, (! empty($field_str) ? $field_str : ''), + $each_table, (! empty($criteriaColumnName) ? $criteriaColumnName : ''), $search_str, $search_option ); // Executes the "COUNT" statement @@ -331,7 +341,7 @@ function PMA_dbSearchGetResultsRow($each_table, $newsearchsqls, $odd_row) $html_output .= ''; if ($res_cnt > 0) { - $this_url_params['sql_query'] = $newsearchsqls['select_fields']; + $this_url_params['sql_query'] = $newsearchsqls['select_columns']; $browse_result_path = 'sql.php' . PMA_generate_common_url($this_url_params); $html_output .= ''; $html_output .= '
'; $html_output .= ''; $html_output .= '' . __('Inside column:') . ''; - $html_output .= ''; $html_output .= ''; $html_output .= ''; From b10429b268dab578d9b1a829fe1347a3ee41408a Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Thu, 28 Jun 2012 07:25:09 +0530 Subject: [PATCH 5/7] Fix comment --- db_search.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/db_search.php b/db_search.php index d6d1e08796..867d4774cf 100644 --- a/db_search.php +++ b/db_search.php @@ -108,7 +108,7 @@ if ( $GLOBALS['is_ajax_request'] != true) { } /** - * 1. Main search form has been submitted + * Main search form has been submitted */ if (isset($_REQUEST['submit_search'])) { $response->addHTML( @@ -118,7 +118,7 @@ if (isset($_REQUEST['submit_search'])) { (! empty($criteriaColumnName) ? $criteriaColumnName : '') ) ); -} // end 1. +} /** * If we are in an Ajax request, we need to exit after displaying all the HTML @@ -128,7 +128,7 @@ if ($GLOBALS['is_ajax_request'] == true) { } else { $response->addHTML('');//end searchresults div } - +// Add search form $response->addHTML( PMA_dbSearchGetSelectionForm( $searched, $search_option, $tables_names_only, $tables_selected, $url_params, From f3d2f177000e8c3855798d14d442b68a1256e524 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Thu, 28 Jun 2012 07:44:41 +0530 Subject: [PATCH 6/7] Move functions to separate library file --- db_search.php | 351 +---------------------------------- libraries/db_search.lib.php | 361 ++++++++++++++++++++++++++++++++++++ 2 files changed, 362 insertions(+), 350 deletions(-) create mode 100644 libraries/db_search.lib.php diff --git a/db_search.php b/db_search.php index 867d4774cf..329f7772ab 100644 --- a/db_search.php +++ b/db_search.php @@ -12,6 +12,7 @@ * */ require_once 'libraries/common.inc.php'; +require_once 'libraries/db_search.lib.php'; $response = PMA_Response::getInstance(); $header = $response->getHeader(); @@ -135,354 +136,4 @@ $response->addHTML( (! empty($criteriaColumnName) ? $criteriaColumnName : '') ) ); - -/** - * Builds the SQL search query - * - * @param string $table The table name - * @param string $criteriaColumnName Restrict the search to this column - * @param string $search_str the string to search - * @param integer $search_option type of search - * (1 -> 1 word at least, 2 -> all words, - * 3 -> exact string, 4 -> regexp) - * - * @return array 3 SQL querys (for count, display and delete results) - * - * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this? - * PMA_backquote - * PMA_DBI_free_result - * PMA_DBI_fetch_assoc - * $GLOBALS['db'] - * explode - * count - * strlen - */ -function PMA_getSearchSqls($table, $criteriaColumnName, $search_str, $search_option) -{ - // Statement types - $sqlstr_select = 'SELECT'; - $sqlstr_delete = 'DELETE'; - // Table to use - $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); - // Search words or pattern - $search_words = (($search_option > 2) - ? array($search_str) : explode(' ', $search_str)); - - $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE'); - $automatic_wildcard = (($search_option < 3) ? '%' : ''); - - $where_clause = PMA_dbSearchGetWhereClause( - $table, $search_words, $search_option, $criteriaColumnName, $like_or_regex, - $automatic_wildcard - ); - - // Builds complete queries - $sql['select_columns'] = $sqlstr_select . ' * ' . $sqlstr_from . $where_clause; - // here, I think we need to still use the COUNT clause, even for - // VIEWs, anyway we have a WHERE clause that should limit results - $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' - . $sqlstr_from . $where_clause; - $sql['delete'] = $sqlstr_delete . $sqlstr_from . $where_clause; - - return $sql; -} - -/** - * Provides where clause for bulding SQL query - * - * @param string $table the table name - * @param integer $search_words Search words or pattern - * @param integer $search_option type of search - * (1 -> 1 word at least, 2 -> all words, - * 3 -> exact string, 4 -> regexp) - * @param string $criteriaColumnName Restrict the search to this column - * @param string $like_or_regex Whether to use 'LIKE' or 'REGEXP' - * @param string $automatic_wildcard Use automatic wildcard - * - * @return string The generated where clause - */ -function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, - $criteriaColumnName, $like_or_regex, $automatic_wildcard -) { - $where_clause = ''; - // Columns to select - $allColumns = PMA_DBI_get_columns($GLOBALS['db'], $table); - $likeClauses = array(); - - foreach ($search_words as $search_word) { - // Eliminates empty values - if (strlen($search_word) === 0) { - continue; - } - $likeClausesPerColumn = array(); - // for each column in the table - foreach ($allColumns as $column) { - if (! isset($criteriaColumnName) - || strlen($criteriaColumnName) == 0 - || $column['Field'] == $criteriaColumnName - ) { - // Drizzle has no CONVERT and all text columns are UTF-8 - $column = ((PMA_DRIZZLE) - ? PMA_backquote($column['Field']) - : 'CONVERT(' . PMA_backquote($column['Field']) . ' USING utf8)'); - $likeClausesPerColumn[] = $column . ' ' . $like_or_regex . ' ' - . "'" - . $automatic_wildcard . $search_word . $automatic_wildcard - . "'"; - } - } // end for - if (count($likeClausesPerColumn) > 0) { - $likeClauses[] = implode(' OR ', $likeClausesPerColumn); - } - } // end for - - $implode_str = ($search_option == 1 ? ' OR ' : ' AND '); - if ( empty($likeClauses)) { - // this could happen when the "inside column" does not exist - // in any selected tables - $where_clause = ' WHERE FALSE'; - } else { - $where_clause = ' WHERE (' . implode(') ' . $implode_str . ' (', $likeClauses) . ')'; - } - return $where_clause; -} - -/** - * Displays database search results - * - * @param array $tables_selected Tables on which search is to be performed - * @param string $searched The search word/phrase/regexp - * @param string $option_str Type of search - * @param string $search_str the string to search - * @param integer $search_option type of search - * (1 -> 1 word at least, 2 -> all words, - * 3 -> exact string, 4 -> regexp) - * @param string $criteriaColumnName Restrict the search to this column - * - * @return string HTML for search results - */ -function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, - $search_str, $search_option, $criteriaColumnName = null -) { - $html_output = ''; - // Displays search string - $html_output .= '
' - . '' - . ''; - - $num_search_result_total = 0; - $odd_row = true; - // For each table selected as search criteria - foreach ($tables_selected as $each_table) { - // Gets the SQL statements - $newsearchsqls = PMA_getSearchSqls( - $each_table, (! empty($criteriaColumnName) ? $criteriaColumnName : ''), - $search_str, $search_option - ); - // Executes the "COUNT" statement - $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']); - $num_search_result_total += $res_cnt; - $html_output .= PMA_dbSearchGetResultsRow( - $each_table, $newsearchsqls, $odd_row - ); - $odd_row = ! $odd_row; - } // end for - $html_output .= '
' - . sprintf( - __('Search results for "%s" %s:'), - $searched, $option_str - ) - . '
'; - - if (count($tables_selected) > 1) { - $html_output .= '

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

'; - } - return $html_output; -} - -/** - * Provides search results row with browse/delete links. - * (for a table) - * - * @param string $each_table Tables on which search is to be performed - * @param array $newsearchsqls Contains SQL queries - * @param bool $odd_row For displaying contrasting table rows - * - * @return string HTML row - */ -function PMA_dbSearchGetResultsRow($each_table, $newsearchsqls, $odd_row) -{ - $this_url_params = array( - 'db' => $GLOBALS['db'], - 'goto' => 'db_sql.php', - 'pos' => 0, - 'is_js_confirmed' => 0, - ); - $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']); - // Start forming search results row - $html_output = ''; - $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 .= ''; - - if ($res_cnt > 0) { - $this_url_params['sql_query'] = $newsearchsqls['select_columns']; - $browse_result_path = 'sql.php' . PMA_generate_common_url($this_url_params); - $html_output .= '' - . __('Browse') . ''; - $this_url_params['sql_query'] = $newsearchsqls['delete']; - $delete_result_path = 'sql.php' . PMA_generate_common_url($this_url_params); - $html_output .= '' - . __('Delete') . ''; - } else { - $html_output .= ' ' - .' '; - }// end if else - $html_output .= ''; - return $html_output; -} - -/** - * Provides the main search form's html - * - * @param string $searched Keyword/Regular expression to be searched - * @param integer $search_option Type of search (one word, phrase etc.) - * @param array $tables_names_only Names of all tables - * @param array $tables_selected Tables on which search is to be performed - * @param array $url_params URL parameters - * @param string $criteriaColumnName Restrict the search to this column - * - * @return string HTML for selection form - */ -function PMA_dbSearchGetSelectionForm($searched, $search_option, $tables_names_only, - $tables_selected, $url_params, $criteriaColumnName = null -) { - $html_output = ''; - $html_output .= ''; - $html_output .= PMA_generate_common_hidden_inputs($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 .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= ''; - $html_output .= '
' . __('Words or values to search for (wildcard: "%"):') - . '
' . __('Find:') . ''; - $choices = array( - '1' => __('at least one of the words') . PMA_showHint(__('Words are separated by a space character (" ").')), - '2' => __('all words') . PMA_showHint(__('Words are separated by a space character (" ").')), - '3' => __('the exact phrase'), - '4' => __('as regular expression') . ' ' . PMA_showMySQLDocu('Regexp', '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 .= PMA_getRadioFields( - 'search_option', $choices, $search_option, true, false - ); - $html_output .= '
' . __('Inside tables:') . ''; - $html_output .= ''; - $alter_select - = '' . __('Select All') . '' - . ' / ' - . '' . __('Unselect All') . ''; - $html_output .= '
' . $alter_select . '
' . __('Inside column:') . '
'; - $html_output .= '
'; - $html_output .= '
'; - $html_output .= ''; - $html_output .= '
'; - $html_output .= ''; - $html_output .= getResultDivs(); - - return $html_output; -} - -/** - * Provides div tags for browsing search results and sql query form. - * - * @return string div tags - */ -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; -} ?> diff --git a/libraries/db_search.lib.php b/libraries/db_search.lib.php new file mode 100644 index 0000000000..5a4266c5ef --- /dev/null +++ b/libraries/db_search.lib.php @@ -0,0 +1,361 @@ + 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) + * + * @return array 3 SQL querys (for count, display and delete results) + * + * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this? + * PMA_backquote + * PMA_DBI_free_result + * PMA_DBI_fetch_assoc + * $GLOBALS['db'] + * explode + * count + * strlen + */ +function PMA_getSearchSqls($table, $criteriaColumnName, $search_str, $search_option) +{ + // Statement types + $sqlstr_select = 'SELECT'; + $sqlstr_delete = 'DELETE'; + // Table to use + $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); + // Search words or pattern + $search_words = (($search_option > 2) + ? array($search_str) : explode(' ', $search_str)); + + $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE'); + $automatic_wildcard = (($search_option < 3) ? '%' : ''); + + $where_clause = PMA_dbSearchGetWhereClause( + $table, $search_words, $search_option, $criteriaColumnName, $like_or_regex, + $automatic_wildcard + ); + + // Builds complete queries + $sql['select_columns'] = $sqlstr_select . ' * ' . $sqlstr_from . $where_clause; + // here, I think we need to still use the COUNT clause, even for + // VIEWs, anyway we have a WHERE clause that should limit results + $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' + . $sqlstr_from . $where_clause; + $sql['delete'] = $sqlstr_delete . $sqlstr_from . $where_clause; + + return $sql; +} + +/** + * Provides where clause for bulding SQL query + * + * @param string $table the table name + * @param integer $search_words Search words or pattern + * @param integer $search_option type of search + * (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) + * @param string $criteriaColumnName Restrict the search to this column + * @param string $like_or_regex Whether to use 'LIKE' or 'REGEXP' + * @param string $automatic_wildcard Use automatic wildcard + * + * @return string The generated where clause + */ +function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, + $criteriaColumnName, $like_or_regex, $automatic_wildcard +) { + $where_clause = ''; + // Columns to select + $allColumns = PMA_DBI_get_columns($GLOBALS['db'], $table); + $likeClauses = array(); + + foreach ($search_words as $search_word) { + // Eliminates empty values + if (strlen($search_word) === 0) { + continue; + } + $likeClausesPerColumn = array(); + // for each column in the table + foreach ($allColumns as $column) { + if (! isset($criteriaColumnName) + || strlen($criteriaColumnName) == 0 + || $column['Field'] == $criteriaColumnName + ) { + // Drizzle has no CONVERT and all text columns are UTF-8 + $column = ((PMA_DRIZZLE) + ? PMA_backquote($column['Field']) + : 'CONVERT(' . PMA_backquote($column['Field']) . ' USING utf8)'); + $likeClausesPerColumn[] = $column . ' ' . $like_or_regex . ' ' + . "'" + . $automatic_wildcard . $search_word . $automatic_wildcard + . "'"; + } + } // end for + if (count($likeClausesPerColumn) > 0) { + $likeClauses[] = implode(' OR ', $likeClausesPerColumn); + } + } // end for + + $implode_str = ($search_option == 1 ? ' OR ' : ' AND '); + if ( empty($likeClauses)) { + // this could happen when the "inside column" does not exist + // in any selected tables + $where_clause = ' WHERE FALSE'; + } else { + $where_clause = ' WHERE (' . implode(') ' . $implode_str . ' (', $likeClauses) . ')'; + } + return $where_clause; +} + +/** + * Displays database search results + * + * @param array $tables_selected Tables on which search is to be performed + * @param string $searched The search word/phrase/regexp + * @param string $option_str Type of search + * @param string $search_str the string to search + * @param integer $search_option type of search + * (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) + * @param string $criteriaColumnName Restrict the search to this column + * + * @return string HTML for search results + */ +function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, + $search_str, $search_option, $criteriaColumnName = null +) { + $html_output = ''; + // Displays search string + $html_output .= '
' + . '' + . ''; + + $num_search_result_total = 0; + $odd_row = true; + // For each table selected as search criteria + foreach ($tables_selected as $each_table) { + // Gets the SQL statements + $newsearchsqls = PMA_getSearchSqls( + $each_table, (! empty($criteriaColumnName) ? $criteriaColumnName : ''), + $search_str, $search_option + ); + // Executes the "COUNT" statement + $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']); + $num_search_result_total += $res_cnt; + $html_output .= PMA_dbSearchGetResultsRow( + $each_table, $newsearchsqls, $odd_row + ); + $odd_row = ! $odd_row; + } // end for + $html_output .= '
' + . sprintf( + __('Search results for "%s" %s:'), + $searched, $option_str + ) + . '
'; + + if (count($tables_selected) > 1) { + $html_output .= '

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

'; + } + return $html_output; +} + +/** + * Provides search results row with browse/delete links. + * (for a table) + * + * @param string $each_table Tables on which search is to be performed + * @param array $newsearchsqls Contains SQL queries + * @param bool $odd_row For displaying contrasting table rows + * + * @return string HTML row + */ +function PMA_dbSearchGetResultsRow($each_table, $newsearchsqls, $odd_row) +{ + $this_url_params = array( + 'db' => $GLOBALS['db'], + 'goto' => 'db_sql.php', + 'pos' => 0, + 'is_js_confirmed' => 0, + ); + $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']); + // Start forming search results row + $html_output = ''; + $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 .= ''; + + if ($res_cnt > 0) { + $this_url_params['sql_query'] = $newsearchsqls['select_columns']; + $browse_result_path = 'sql.php' . PMA_generate_common_url($this_url_params); + $html_output .= '' + . __('Browse') . ''; + $this_url_params['sql_query'] = $newsearchsqls['delete']; + $delete_result_path = 'sql.php' . PMA_generate_common_url($this_url_params); + $html_output .= '' + . __('Delete') . ''; + } else { + $html_output .= ' ' + .' '; + }// end if else + $html_output .= ''; + return $html_output; +} + +/** + * Provides the main search form's html + * + * @param string $searched Keyword/Regular expression to be searched + * @param integer $search_option Type of search (one word, phrase etc.) + * @param array $tables_names_only Names of all tables + * @param array $tables_selected Tables on which search is to be performed + * @param array $url_params URL parameters + * @param string $criteriaColumnName Restrict the search to this column + * + * @return string HTML for selection form + */ +function PMA_dbSearchGetSelectionForm($searched, $search_option, $tables_names_only, + $tables_selected, $url_params, $criteriaColumnName = null +) { + $html_output = ''; + $html_output .= '
'; + $html_output .= PMA_generate_common_hidden_inputs($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 .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= '
' . __('Words or values to search for (wildcard: "%"):') + . '
' . __('Find:') . ''; + $choices = array( + '1' => __('at least one of the words') . PMA_showHint(__('Words are separated by a space character (" ").')), + '2' => __('all words') . PMA_showHint(__('Words are separated by a space character (" ").')), + '3' => __('the exact phrase'), + '4' => __('as regular expression') . ' ' . PMA_showMySQLDocu('Regexp', '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 .= PMA_getRadioFields( + 'search_option', $choices, $search_option, true, false + ); + $html_output .= '
' . __('Inside tables:') . ''; + $html_output .= ''; + $alter_select + = '' . __('Select All') . '' + . ' / ' + . '' . __('Unselect All') . ''; + $html_output .= '
' . $alter_select . '
' . __('Inside column:') . '
'; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= getResultDivs(); + + return $html_output; +} + +/** + * Provides div tags for browsing search results and sql query form. + * + * @return string div tags + */ +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; +} +?> From 417856ee9f7e1aa9472120f3b5a316ee3c8de1a4 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Fri, 29 Jun 2012 00:10:30 +0530 Subject: [PATCH 7/7] Variable name improvements --- db_search.php | 44 ++++++++++--------- libraries/db_search.lib.php | 86 +++++++++++++++++++------------------ 2 files changed, 68 insertions(+), 62 deletions(-) diff --git a/db_search.php b/db_search.php index 329f7772ab..e96823447a 100644 --- a/db_search.php +++ b/db_search.php @@ -43,51 +43,55 @@ $url_params['goto'] = 'db_search.php'; */ $tables_names_only = PMA_DBI_get_tables($GLOBALS['db']); -$search_options = array( +$searchTypes = array( '1' => __('at least one of the words'), '2' => __('all words'), '3' => __('the exact phrase'), '4' => __('as regular expression'), ); -if (empty($_REQUEST['search_option']) - || ! is_string($_REQUEST['search_option']) - || ! array_key_exists($_REQUEST['search_option'], $search_options) +if (empty($_REQUEST['criteriaSearchType']) + || ! is_string($_REQUEST['criteriaSearchType']) + || ! array_key_exists($_REQUEST['criteriaSearchType'], $searchTypes) ) { - $search_option = 1; + $criteriaSearchType = 1; unset($_REQUEST['submit_search']); } else { - $search_option = (int) $_REQUEST['search_option']; - $option_str = $search_options[$_REQUEST['search_option']]; + $criteriaSearchType = (int) $_REQUEST['criteriaSearchType']; + $option_str = $searchTypes[$_REQUEST['criteriaSearchType']]; } -if (empty($_REQUEST['search_str']) || ! is_string($_REQUEST['search_str'])) { +if (empty($_REQUEST['criteriaSearchString']) + || ! is_string($_REQUEST['criteriaSearchString']) +) { unset($_REQUEST['submit_search']); $searched = ''; } else { - $searched = htmlspecialchars($_REQUEST['search_str']); + $searched = htmlspecialchars($_REQUEST['criteriaSearchString']); // For "as regular expression" (search option 4), we should not treat // this as an expression that contains a LIKE (second parameter of // PMA_sqlAddSlashes()). // // Usage example: If user is seaching for a literal $ in a regexp search, // he should enter \$ as the value. - $search_str = PMA_sqlAddSlashes( - $_REQUEST['search_str'], ($search_option == 4 ? false : true) + $criteriaSearchString = PMA_sqlAddSlashes( + $_REQUEST['criteriaSearchString'], ($criteriaSearchType == 4 ? false : true) ); } -$tables_selected = array(); -if (empty($_REQUEST['table_select']) || ! is_array($_REQUEST['table_select'])) { +$criteriaTables = array(); +if (empty($_REQUEST['criteriaTables']) || ! is_array($_REQUEST['criteriaTables'])) { unset($_REQUEST['submit_search']); } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) { - $tables_selected = array_intersect($_REQUEST['table_select'], $tables_names_only); + $criteriaTables = array_intersect( + $_REQUEST['criteriaTables'], $tables_names_only + ); } if (isset($_REQUEST['selectall'])) { - $tables_selected = $tables_names_only; + $criteriaTables = $tables_names_only; } elseif (isset($_REQUEST['unselectall'])) { - $tables_selected = array(); + $criteriaTables = array(); } if (empty($_REQUEST['criteriaColumnName']) @@ -114,8 +118,8 @@ if ( $GLOBALS['is_ajax_request'] != true) { if (isset($_REQUEST['submit_search'])) { $response->addHTML( PMA_dbSearchGetSearchResults( - $tables_selected, $searched, $option_str, - $search_str, $search_option, + $criteriaTables, $searched, $option_str, + $criteriaSearchString, $criteriaSearchType, (! empty($criteriaColumnName) ? $criteriaColumnName : '') ) ); @@ -132,8 +136,8 @@ if ($GLOBALS['is_ajax_request'] == true) { // Add search form $response->addHTML( PMA_dbSearchGetSelectionForm( - $searched, $search_option, $tables_names_only, $tables_selected, $url_params, - (! empty($criteriaColumnName) ? $criteriaColumnName : '') + $searched, $criteriaSearchType, $tables_names_only, $criteriaTables, + $url_params, (! empty($criteriaColumnName) ? $criteriaColumnName : '') ) ); ?> diff --git a/libraries/db_search.lib.php b/libraries/db_search.lib.php index 5a4266c5ef..14032db9dd 100644 --- a/libraries/db_search.lib.php +++ b/libraries/db_search.lib.php @@ -12,14 +12,14 @@ if (! defined('PHPMYADMIN')) { /** * Builds the SQL search query * - * @param string $table The table name - * @param string $criteriaColumnName Restrict the search to this column - * @param string $search_str the string to search - * @param integer $search_option type of search - * (1 -> 1 word at least, 2 -> all words, - * 3 -> exact string, 4 -> regexp) + * @param string $table The table name + * @param string $criteriaColumnName Restrict the search to this column + * @param string $criteriaSearchString The string to search + * @param integer $criteriaSearchType Type of search + * (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) * - * @return array 3 SQL querys (for count, display and delete results) + * @return array 3 SQL querys (for count, display and delete results) * * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this? * PMA_backquote @@ -30,23 +30,25 @@ if (! defined('PHPMYADMIN')) { * count * strlen */ -function PMA_getSearchSqls($table, $criteriaColumnName, $search_str, $search_option) -{ +function PMA_getSearchSqls($table, $criteriaColumnName, $criteriaSearchString, + $criteriaSearchType +) { // Statement types $sqlstr_select = 'SELECT'; $sqlstr_delete = 'DELETE'; // Table to use - $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); + $sqlstr_from = ' FROM ' + . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); // Search words or pattern - $search_words = (($search_option > 2) - ? array($search_str) : explode(' ', $search_str)); + $search_words = (($criteriaSearchType > 2) + ? array($criteriaSearchString) : explode(' ', $criteriaSearchString)); - $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE'); - $automatic_wildcard = (($search_option < 3) ? '%' : ''); + $like_or_regex = (($criteriaSearchType == 4) ? 'REGEXP' : 'LIKE'); + $automatic_wildcard = (($criteriaSearchType < 3) ? '%' : ''); $where_clause = PMA_dbSearchGetWhereClause( - $table, $search_words, $search_option, $criteriaColumnName, $like_or_regex, - $automatic_wildcard + $table, $search_words, $criteriaSearchType, $criteriaColumnName, + $like_or_regex, $automatic_wildcard ); // Builds complete queries @@ -65,7 +67,7 @@ function PMA_getSearchSqls($table, $criteriaColumnName, $search_str, $search_opt * * @param string $table the table name * @param integer $search_words Search words or pattern - * @param integer $search_option type of search + * @param integer $criteriaSearchType Type of search * (1 -> 1 word at least, 2 -> all words, * 3 -> exact string, 4 -> regexp) * @param string $criteriaColumnName Restrict the search to this column @@ -74,7 +76,7 @@ function PMA_getSearchSqls($table, $criteriaColumnName, $search_str, $search_opt * * @return string The generated where clause */ -function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, +function PMA_dbSearchGetWhereClause($table, $search_words, $criteriaSearchType, $criteriaColumnName, $like_or_regex, $automatic_wildcard ) { $where_clause = ''; @@ -109,7 +111,7 @@ function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, } } // end for - $implode_str = ($search_option == 1 ? ' OR ' : ' AND '); + $implode_str = ($criteriaSearchType == 1 ? ' OR ' : ' AND '); if ( empty($likeClauses)) { // this could happen when the "inside column" does not exist // in any selected tables @@ -123,19 +125,19 @@ function PMA_dbSearchGetWhereClause($table, $search_words, $search_option, /** * Displays database search results * - * @param array $tables_selected Tables on which search is to be performed - * @param string $searched The search word/phrase/regexp - * @param string $option_str Type of search - * @param string $search_str the string to search - * @param integer $search_option type of search - * (1 -> 1 word at least, 2 -> all words, - * 3 -> exact string, 4 -> regexp) - * @param string $criteriaColumnName Restrict the search to this column + * @param array $criteriaTables Tables on which search is to be performed + * @param string $searched The search word/phrase/regexp + * @param string $option_str Type of search + * @param string $criteriaSearchString The string to search + * @param integer $criteriaSearchType Type of search + * (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) + * @param string $criteriaColumnName Restrict the search to this column * * @return string HTML for search results */ -function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, - $search_str, $search_option, $criteriaColumnName = null +function PMA_dbSearchGetSearchResults($criteriaTables, $searched, $option_str, + $criteriaSearchString, $criteriaSearchType, $criteriaColumnName = null ) { $html_output = ''; // Displays search string @@ -151,11 +153,11 @@ function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, $num_search_result_total = 0; $odd_row = true; // For each table selected as search criteria - foreach ($tables_selected as $each_table) { + foreach ($criteriaTables as $each_table) { // Gets the SQL statements $newsearchsqls = PMA_getSearchSqls( $each_table, (! empty($criteriaColumnName) ? $criteriaColumnName : ''), - $search_str, $search_option + $criteriaSearchString, $criteriaSearchType ); // Executes the "COUNT" statement $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']); @@ -167,7 +169,7 @@ function PMA_dbSearchGetSearchResults($tables_selected, $searched, $option_str, } // end for $html_output .= ''; - if (count($tables_selected) > 1) { + if (count($criteriaTables) > 1) { $html_output .= '

'; $html_output .= sprintf( _ngettext( @@ -245,16 +247,16 @@ function PMA_dbSearchGetResultsRow($each_table, $newsearchsqls, $odd_row) * Provides the main search form's html * * @param string $searched Keyword/Regular expression to be searched - * @param integer $search_option Type of search (one word, phrase etc.) + * @param integer $criteriaSearchType Type of search (one word, phrase etc.) * @param array $tables_names_only Names of all tables - * @param array $tables_selected Tables on which search is to be performed + * @param array $criteriaTables Tables on which search is to be performed * @param array $url_params URL parameters * @param string $criteriaColumnName Restrict the search to this column * * @return string HTML for selection form */ -function PMA_dbSearchGetSelectionForm($searched, $search_option, $tables_names_only, - $tables_selected, $url_params, $criteriaColumnName = null +function PMA_dbSearchGetSelectionForm($searched, $criteriaSearchType, + $tables_names_only, $criteriaTables, $url_params, $criteriaColumnName = null ) { $html_output = ''; $html_output .= '

'; - $html_output .= ''; $html_output .= ''; // choices for types of search @@ -286,16 +288,16 @@ function PMA_dbSearchGetSelectionForm($searched, $search_option, $tables_names_o // 5th parameter set to false to avoid htmlspecialchars() escaping in the label // since we have some HTML in some labels $html_output .= PMA_getRadioFields( - 'search_option', $choices, $search_option, true, false + 'criteriaSearchType', $choices, $criteriaSearchType, true, false ); $html_output .= ''; // displays table names as select options $html_output .= ''; $html_output .= '' . __('Inside tables:') . ''; $html_output .= ''; - $html_output .= ''; foreach ($tables_names_only as $each_table) { - if (in_array($each_table, $tables_selected)) { + if (in_array($each_table, $criteriaTables)) { $is_selected = ' selected="selected"'; } else { $is_selected = ''; @@ -308,10 +310,10 @@ function PMA_dbSearchGetSelectionForm($searched, $search_option, $tables_names_o $html_output .= ''; $alter_select = '' . __('Select All') . '' + . ' onclick="setSelectOptions(\'db_search\', \'criteriaTables[]\', true); return false;">' . __('Select All') . '' . ' / ' . '' . __('Unselect All') . ''; + . ' onclick="setSelectOptions(\'db_search\', \'criteriaTables[]\', false); return false;">' . __('Unselect All') . ''; $html_output .= ''; $html_output .= '' . $alter_select . ''; $html_output .= '';