diff --git a/db_search.php b/db_search.php
index 2f4e08a79b..e96823447a 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();
@@ -42,57 +43,63 @@ $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['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);
}
/**
@@ -106,226 +113,16 @@ if ( $GLOBALS['is_ajax_request'] != true) {
}
/**
- * 1. Main search form has been submitted
+ * 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';
-
- // 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 = (($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);
-
- // Builds complete queries
- $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
- // 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;
-
- return $sql;
- } // end of the "PMA_getSearchSqls()" function
$response->addHTML(
PMA_dbSearchGetSearchResults(
- $tables_selected, $searched, $option_str,
- $search_str, $search_option, (! empty($field_str) ? $field_str : '')
+ $criteriaTables, $searched, $option_str,
+ $criteriaSearchString, $criteriaSearchType,
+ (! empty($criteriaColumnName) ? $criteriaColumnName : '')
)
);
-} // end 1.
-
-/**
- * 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
- *
- * @return string HTML for search results
- */
-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 .= '
'
- . '
'; - $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) -{ - $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']); - // Start forming search results row - $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 = '