_db = $db;
// Sets criteria parameters
$this->_setSearchParams();
}
/**
* Sets search parameters
*
* @return void
*/
private function _setSearchParams()
{
$this->_tables_names_only = PMA_DBI_get_tables($this->_db);
$this->_searchTypes = array(
'1' => __('at least one of the words'),
'2' => __('all words'),
'3' => __('the exact phrase'),
'4' => __('as regular expression'),
);
if (empty($_REQUEST['criteriaSearchType'])
|| ! is_string($_REQUEST['criteriaSearchType'])
|| ! array_key_exists($_REQUEST['criteriaSearchType'], $this->_searchTypes)
) {
$this->_criteriaSearchType = 1;
unset($_REQUEST['submit_search']);
} else {
$this->_criteriaSearchType = (int) $_REQUEST['criteriaSearchType'];
$this->_searchTypeDescription
= $this->_searchTypes[$_REQUEST['criteriaSearchType']];
}
if (empty($_REQUEST['criteriaSearchString'])
|| ! is_string($_REQUEST['criteriaSearchString'])
) {
$this->_criteriaSearchString = '';
unset($_REQUEST['submit_search']);
} else {
$this->_criteriaSearchString = $_REQUEST['criteriaSearchString'];
}
$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
);
}
if (empty($_REQUEST['criteriaColumnName'])
|| ! is_string($_REQUEST['criteriaColumnName'])
) {
unset($this->_criteriaColumnName);
} else {
$this->_criteriaColumnName = PMA_Util::sqlAddSlashes(
$_REQUEST['criteriaColumnName'], true
);
}
}
/**
* Builds the SQL search query
*
* @param string $table The table name
*
* @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
*/
private function _getSearchSqls($table)
{
// Statement types
$sqlstr_select = 'SELECT';
$sqlstr_delete = 'DELETE';
// Table to use
$sqlstr_from = ' FROM '
. PMA_Util::backquote($GLOBALS['db']) . '.'
. PMA_Util::backquote($table);
// Gets where clause for the query
$where_clause = $this->_getWhereClause($table);
// 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
*
* @return string The generated where clause
*/
private function _getWhereClause($table)
{
$where_clause = '';
// Columns to select
$allColumns = PMA_DBI_get_columns($GLOBALS['db'], $table);
$likeClauses = array();
// Based on search type, decide like/regex & '%'/''
$like_or_regex = (($this->_criteriaSearchType == 4) ? 'REGEXP' : 'LIKE');
$automatic_wildcard = (($this->_criteriaSearchType < 3) ? '%' : '');
// For "as regular expression" (search option 4), LIKE won't be used
// Usage example: If user is seaching for a literal $ in a regexp search,
// he should enter \$ as the value.
$this->_criteriaSearchString = PMA_Util::sqlAddSlashes(
$this->_criteriaSearchString,
($this->_criteriaSearchType == 4 ? false : true)
);
// Extract search words or pattern
$search_words = (($this->_criteriaSearchType > 2)
? array($this->_criteriaSearchString)
: explode(' ', $this->_criteriaSearchString));
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($this->_criteriaColumnName)
|| strlen($this->_criteriaColumnName) == 0
|| $column['Field'] == $this->_criteriaColumnName
) {
// Drizzle has no CONVERT and all text columns are UTF-8
$column = ((PMA_DRIZZLE)
? PMA_Util::backquote($column['Field'])
: 'CONVERT(' . PMA_Util::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
// Use 'OR' if 'at least one word' is to be searched, else use '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
$where_clause = ' WHERE FALSE';
} else {
$where_clause = ' WHERE ('
. implode(') ' . $implode_str . ' (', $likeClauses)
. ')';
}
return $where_clause;
}
/**
* Displays database search results
*
* @return string HTML for search results
*/
public function getSearchResults()
{
$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 One of the tables on which search was performed * @param array $newsearchsqls Contains SQL queries * @param bool $odd_row For displaying contrasting table rows * @param integer $res_cnt Number of results found * * @return string HTML row */ private function _getResultsRow($each_table, $newsearchsqls, $odd_row, $res_cnt) { $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 = '