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, $criteriaSearchString, $criteriaSearchType ) { // 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 = (($criteriaSearchType > 2) ? array($criteriaSearchString) : explode(' ', $criteriaSearchString)); $like_or_regex = (($criteriaSearchType == 4) ? 'REGEXP' : 'LIKE'); $automatic_wildcard = (($criteriaSearchType < 3) ? '%' : ''); $where_clause = PMA_dbSearchGetWhereClause( $table, $search_words, $criteriaSearchType, $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 $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 * @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, $criteriaSearchType, $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 = ($criteriaSearchType == 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 $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($criteriaTables, $searched, $option_str, $criteriaSearchString, $criteriaSearchType, $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 ($criteriaTables as $each_table) { // Gets the SQL statements $newsearchsqls = PMA_getSearchSqls( $each_table, (! empty($criteriaColumnName) ? $criteriaColumnName : ''), $criteriaSearchString, $criteriaSearchType ); // 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($criteriaTables) > 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 $criteriaSearchType Type of search (one word, phrase etc.) * @param array $tables_names_only Names of all tables * @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, $criteriaSearchType, $tables_names_only, $criteriaTables, $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( 'criteriaSearchType', $choices, $criteriaSearchType, 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; } ?>