diff --git a/db_qbe.php b/db_qbe.php index f27bd1f1b8..7ed278204b 100644 --- a/db_qbe.php +++ b/db_qbe.php @@ -40,6 +40,15 @@ foreach (array_keys($_POST) as $post_key) { } } } +/** + * Initialize some more global variables + */ +$GLOBALS['curField'] = array(); +$GLOBALS['curSort'] = array(); +$GLOBALS['curShow'] = array(); +$GLOBALS['curCriteria'] = array(); +$GLOBALS['curAndOrRow'] = array(); +$GLOBALS['curAndOrCol'] = array(); /** * Gets the relation settings @@ -170,32 +179,393 @@ $realwidth = $form_column_width . 'ex'; */ /** - * Enter description here... + * Provides select options list containing column names * - * @param array $columns - * @param integer $column_number - * @param string $selected + * @param array $columns All Column Names + * @param integer $column_number Column Number (0,1,2) or more + * @param string $selected Selected criteria column name + * + * @return HTML for select options */ function showColumnSelectCell($columns, $column_number, $selected = '') { - ?> - - '; + $html_output .= ''; foreach ($columns as $column) { - if ($column === $selected) { - $sel = ' selected="selected"'; - } else { - $sel = ''; - } - echo '' . "\n"; + $html_output .= ''; } - ?> - - - '; + $html_output .= ''; + return $html_output; +} + +/** + * Provides select options list containing sort options (ASC/DESC) + * + * @param integer $column_number Column Number (0,1,2) or more + * @param string $realwidth Largest column width found + * @param string $asc_selected Selected criteria 'Ascending' + * @param string $desc_selected Selected criteria 'Descending' + * + * @return HTML for select options + */ +function getSortSelectCell($column_number, $realwidth, $asc_selected = '', + $desc_selected = '') +{ + $html_output = ''; + $html_output .= ''; + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing column select options + * + * @param array $criteria_column_count Number of criteria columns + * @param integer $columns All column names + * @param string $ins_col If a new criteria column is needed + * @param string $del_col If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbegetColumnNamesRow( + $criteria_column_count, $columns, $ins_col = null, $del_col = null +) { + $html_output = ''; + $html_output .= '' . __('Column') . ':'; + $z = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + if (isset($ins_col[$column_index]) && $ins_col[$column_index] == 'on') { + $html_output .= showColumnSelectCell($columns, $z); + $z++; + } + if (! empty($del_col) && isset($del_col[$column_index]) && $del_col[$column_index] == 'on') { + continue; + } + $selected = ''; + if (isset($_REQUEST['Field'][$column_index])) { + $selected = $_REQUEST['Field'][$column_index]; + $GLOBALS['curField'][$z] = $_REQUEST['Field'][$column_index]; + } + $html_output .= showColumnSelectCell($columns, $z, $selected); + $z++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing sort(ASC/DESC) select options + * + * @param array $criteria_column_count Number of criteria columns + * @param string $realwidth Largest column width found + * @param string $ins_col If a new criteria column is needed + * @param string $del_col If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbegetSortRow( + $criteria_column_count, $realwidth, $ins_col = null, $del_col = null +) { + $html_output = ''; + $html_output .= '' . __('Sort') . ':'; + $z = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + if (! empty($ins_col) && isset($ins_col[$column_index]) && $ins_col[$column_index] == 'on') { + $html_output .= getSortSelectCell($z, $realwidth); + $z++; + } // end if + + if (! empty($del_col) && isset($del_col[$column_index]) && $del_col[$column_index] == 'on') { + continue; + } + // If they have chosen all fields using the * selector, + // then sorting is not available, Fix for Bug #570698 + if (isset($_REQUEST['Sort'][$column_index]) && isset($_REQUEST['Field'][$column_index]) + && substr($_REQUEST['Field'][$column_index], -2) == '.*' + ) { + $_REQUEST['Sort'][$column_index] = ''; + } //end if + // Set asc_selected + if (isset($_REQUEST['Sort'][$column_index]) && $_REQUEST['Sort'][$column_index] == 'ASC') { + $GLOBALS['curSort'][$z] = $_REQUEST['Sort'][$column_index]; + $asc_selected = ' selected="selected"'; + } else { + $asc_selected = ''; + } // end if + // Set desc selected + if (isset($_REQUEST['Sort'][$column_index]) && $_REQUEST['Sort'][$column_index] == 'DESC') { + $GLOBALS['curSort'][$z] = $_REQUEST['Sort'][$column_index]; + $desc_selected = ' selected="selected"'; + } else { + $desc_selected = ''; + } // end if + $html_output .= getSortSelectCell( + $z, $realwidth, $asc_selected, $desc_selected + ); + $z++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing SHOW checkboxes + * + * @param array $criteria_column_count Number of criteria columns + * @param string $ins_col If a new criteria column is needed + * @param string $del_col If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbegetShowRow( + $criteria_column_count, $ins_col = null, $del_col = null +) { + $html_output = ''; + $html_output .= '' . __('Show') . ':'; + $z = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + if (! empty($ins_col) && isset($ins_col[$column_index]) && $ins_col[$column_index] == 'on') { + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $z++; + } // end if + if (! empty($del_col) && isset($del_col[$column_index]) && $del_col[$column_index] == 'on') { + continue; + } + if (isset($_REQUEST['Show'][$column_index])) { + $checked = ' checked="checked"'; + $GLOBALS['curShow'][$z] = $_REQUEST['Show'][$column_index]; + } else { + $checked = ''; + } + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $z++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing criteria Inputboxes + * + * @param array $criteria_column_count Number of criteria columns + * @param string $realwidth Largest column width found + * @param string $criteria Already Filled criteria + * @param string $prev_criteria Previously filled criteria(hidden form field) + * @param string $ins_col If a new criteria column is needed + * @param string $del_col If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbegetCriteriaInputboxRow( + $criteria_column_count, $realwidth, $criteria, $prev_criteria, + $ins_col = null, $del_col = null +) { + $html_output = ''; + $html_output .= '' . __('Criteria') . ':'; + $z = 0; + for ($column_index = 0; $column_index < $criteria_column_count; $column_index++) + { + if (! empty($ins_col) && isset($ins_col[$column_index]) && $ins_col[$column_index] == 'on') { + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $z++; + } // end if + if (! empty($del_col) && isset($del_col[$column_index]) && $del_col[$column_index] == 'on') { + continue; + } + if (isset($criteria[$column_index])) { + $tmp_criteria = $criteria[$column_index]; + } + if ((empty($prev_criteria) || ! isset($prev_criteria[$column_index])) + || $prev_criteria[$column_index] != htmlspecialchars($tmp_criteria) + ) { + $GLOBALS['curCriteria'][$z] = $tmp_criteria; + } else { + $GLOBALS['curCriteria'][$z] = $prev_criteria[$column_index]; + } + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $html_output .= ''; + $z++; + } // end for + $html_output .= ''; + return $html_output; +} + +/** + * Provides footer options for adding/deleting row/columns + * + * @param string $type Whether row or column + * + * @return HTML for footer options + */ +function PMA_dbQbeGetFootersOptions($type) +{ + $html_output = '
'; + $html_output .= (($type == 'row') + ? __('Add/Delete criteria rows') : __('Add/Delete columns')); + $html_output .= ':'; + $html_output .= '
'; + return $html_output; +} + +/** + * Provides search form table's footer options + * + * @return HTML for table footer + */ +function PMA_dbQbeGetTableFooters() +{ + $html_output = '
'; + $html_output .= PMA_dbQbeGetFootersOptions("row"); + $html_output .= PMA_dbQbeGetFootersOptions("column"); + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + return $html_output; +} + +/** + * Provides a select list of database tables + * + * @param array $table_names Names of all the tables + * + * @return HTML for table select list + */ +function PMA_dbQbeGetTablesList($table_names) +{ + $html_output = '
'; + $html_output .= '
'; + $html_output .= '' . __('Use Tables') . ''; + // Build the options list for each table name + $options = ''; + $numTableListOptions = 0; + foreach ($table_names as $key => $val) { + $options .= ''; + $numTableListOptions++; + } + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + $html_output .= ''; + $html_output .= '
'; + $html_output .= '
'; + return $html_output; +} + +/** + * Provides And/Or modification cell along with Insert/Delete options + * (For modifying search form's table columns) + * + * @param integer $column_number Column Number (0,1,2) or more + * @param array $selected Selected criteria column name + * + * @return HTML for modification cell + */ +function PMA_dbQbeGetAndOrColCell($column_number, $selected = null) +{ + $html_output = ''; + $html_output .= '' . __('Or') . ':'; + $html_output .= ''; + $html_output .= '  ' . __('And') . ':'; + $html_output .= ''; + $html_output .= '
' . __('Ins'); + $html_output .= ''; + $html_output .= '  ' . __('Del'); + $html_output .= ''; + $html_output .= ''; + return $html_output; +} + +/** + * Provides search form's row containing column modifications options + * (For modifying search form's table columns) + * + * @param array $criteria_column_count Number of criteria columns + * @param string $realwidth Largest column width found + * @param string $criteria Already Filled criteria + * @param string $prev_criteria Previously filled criteria(hidden form field) + * @param string $ins_col If a new criteria column is needed + * @param string $del_col If a criteria column is to be deleted + * + * @return HTML for search table's row + */ +function PMA_dbQbeGetModifyColumnsRow($criteria_column_count, $and_or_col, + $ins_col = null, $del_col = null +) { + $html_output = ''; + $html_output .= '' . __('Modify') . ':'; + $z = 0; + for ($x = 0; $x < $criteria_column_count; $x++) { + if (! empty($ins_col) && isset($ins_col[$x]) && $ins_col[$x] == 'on') { + $html_output .= PMA_dbQbeGetAndOrColCell($z); + $z++; + } // end if + + if (! empty($del_col) && isset($del_col[$x]) && $del_col[$x] == 'on') { + continue; + } + + if (isset($and_or_col[$x])) { + $GLOBALS['curAndOrCol'][$z] = $and_or_col[$x]; + } + if (isset($and_or_col[$x]) && $and_or_col[$x] == 'or') { + $chk['or'] = ' checked="checked"'; + $chk['and'] = ''; + } else { + $chk['and'] = ' checked="checked"'; + $chk['or'] = ''; + } + $html_output .= PMA_dbQbeGetAndOrColCell($z, $chk); + $z++; + } // end for + $html_output .= ''; + return $html_output; } if ($cfgRelation['designerwork']) { @@ -217,172 +587,20 @@ if ($cfgRelation['designerwork']) {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - -
:
: - - - -
: - - - /> -
: - - - - -
: - : - /> -   : - /> -
- - -    - -
- : - /> -   : - /> -
- - -    - -
- -
-
-
- : - -
-
- : - -
- -
- -
-
-
-
- $val) { - $options .= ' '; - $options .= '' . "\n"; - $numTableListOptions++; -} +echo PMA_dbQbeGetTableFooters(); +echo PMA_dbQbeGetTablesList($tbl_names); ?> - -
-
- -
-
@@ -694,11 +804,14 @@ if (! isset($qry_select)) { $qry_select = ''; } for ($x = 0; $x < $col; $x++) { - if (! empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') { + if (! empty($GLOBALS['curField'][$x]) + && isset($GLOBALS['curShow'][$x]) + && $GLOBALS['curShow'][$x] == 'on') + { if ($last_select) { $qry_select .= ', '; } - $qry_select .= $curField[$x]; + $qry_select .= $GLOBALS['curField'][$x]; $last_select = 1; } } // end for @@ -901,11 +1014,16 @@ if (! empty($qry_from)) { $qry_where = ''; $criteria_cnt = 0; for ($x = 0; $x < $col; $x++) { - if (! empty($curField[$x]) && ! empty($curCriteria[$x]) && $x && isset($last_where) && isset($curAndOrCol)) { - $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' '; + if (! empty($GLOBALS['curField'][$x]) + && ! empty($GLOBALS['curCriteria'][$x]) + && $x + && isset($last_where) + && isset($GLOBALS['curAndOrCol'])) { + $qry_where .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_where]) . ' '; } - if (! empty($curField[$x]) && ! empty($curCriteria[$x])) { - $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')'; + if (! empty($GLOBALS['curField'][$x]) && ! empty($GLOBALS['curCriteria'][$x])) { + $qry_where .= '(' . $GLOBALS['curField'][$x] . ' ' + . $GLOBALS['curCriteria'][$x] . ')'; $last_where = $x; $criteria_cnt++; } @@ -914,19 +1032,19 @@ if ($criteria_cnt > 1) { $qry_where = '(' . $qry_where . ')'; } // OR rows ${'cur' . $or}[$x] -if (! isset($curAndOrRow)) { - $curAndOrRow = array(); +if (! isset($GLOBALS['curAndOrRow'])) { + $GLOBALS['curAndOrRow'] = array(); } for ($y = 0; $y <= $row; $y++) { $criteria_cnt = 0; $qry_orwhere = ''; $last_orwhere = ''; for ($x = 0; $x < $col; $x++) { - if (! empty($curField[$x]) && ! empty(${'curOr' . $y}[$x]) && $x) { - $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' '; + if (! empty($GLOBALS['curField'][$x]) && ! empty(${'curOr' . $y}[$x]) && $x) { + $qry_orwhere .= ' ' . strtoupper($GLOBALS['curAndOrCol'][$last_orwhere]) . ' '; } - if (! empty($curField[$x]) && ! empty(${'curOr' . $y}[$x])) { - $qry_orwhere .= '(' . $curField[$x] + if (! empty($GLOBALS['curField'][$x]) && ! empty(${'curOr' . $y}[$x])) { + $qry_orwhere .= '(' . $GLOBALS['curField'][$x] . ' ' . ${'curOr' . $y}[$x] . ')'; @@ -939,7 +1057,7 @@ for ($y = 0; $y <= $row; $y++) { } if (! empty($qry_orwhere)) { $qry_where .= "\n" - . strtoupper(isset($curAndOrRow[$y]) ? $curAndOrRow[$y] . ' ' : '') + . strtoupper(isset($GLOBALS['curAndOrRow'][$y]) ? $GLOBALS['curAndOrRow'][$y] . ' ' : '') . $qry_orwhere; } // end if } // end for @@ -955,15 +1073,15 @@ if (! isset($qry_orderby)) { $qry_orderby = ''; } for ($x = 0; $x < $col; $x++) { - if ($last_orderby && $x && ! empty($curField[$x]) && ! empty($curSort[$x])) { + if ($last_orderby && $x && ! empty($GLOBALS['curField'][$x]) && ! empty($GLOBALS['curSort'][$x])) { $qry_orderby .= ', '; } - if (! empty($curField[$x]) && ! empty($curSort[$x])) { + if (! empty($GLOBALS['curField'][$x]) && ! empty($GLOBALS['curSort'][$x])) { // if they have chosen all fields using the * selector, // then sorting is not available // Fix for Bug #570698 - if (substr($curField[$x], -2) != '.*') { - $qry_orderby .= $curField[$x] . ' ' . $curSort[$x]; + if (substr($GLOBALS['curField'][$x], -2) != '.*') { + $qry_orderby .= $GLOBALS['curField'][$x] . ' ' . $GLOBALS['curSort'][$x]; $last_orderby = 1; } } diff --git a/db_search.php b/db_search.php index 07de04675d..dcb18eb701 100644 --- a/db_search.php +++ b/db_search.php @@ -9,10 +9,10 @@ */ /** - * + * Gets some core libraries */ require_once 'libraries/common.inc.php'; -require_once 'libraries/db_search.lib.php'; +require_once 'libraries/DbSearch.class.php'; $response = PMA_Response::getInstance(); $header = $response->getHeader(); @@ -21,119 +21,39 @@ $scripts->addFile('db_search.js'); $scripts->addFile('sql.js'); $scripts->addFile('makegrid.js'); $scripts->addFile('jquery/timepicker.js'); -$common_functions = PMA_CommonFunctions::getInstance(); -/** - * Gets some core libraries and send headers - */ require 'libraries/db_common.inc.php'; -/** - * init - */ // If config variable $GLOBALS['cfg']['Usedbsearch'] is on false : exit. if (! $GLOBALS['cfg']['UseDbSearch']) { - $common_functions->mysqlDie(__('Access denied'), '', false, $err_url); + PMA_CommonFunctions::getInstance()->mysqlDie( + __('Access denied'), '', false, $err_url + ); } // end if $url_query .= '&goto=db_search.php'; $url_params['goto'] = 'db_search.php'; -/** - * @global array list of tables from the current database - * but do not clash with $tables coming from db_info.inc.php - */ -$tables_names_only = PMA_DBI_get_tables($GLOBALS['db']); - -$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'], $searchTypes) -) { - $criteriaSearchType = 1; - unset($_REQUEST['submit_search']); -} else { - $criteriaSearchType = (int) $_REQUEST['criteriaSearchType']; - $searchTypeDescription = $searchTypes[$_REQUEST['criteriaSearchType']]; -} - -if (empty($_REQUEST['criteriaSearchString']) - || ! is_string($_REQUEST['criteriaSearchString']) -) { - $criteriaSearchString = ''; - unset($_REQUEST['submit_search']); -} else { - $criteriaSearchString = $_REQUEST['criteriaSearchString']; -} - -$criteriaTables = array(); -if (empty($_REQUEST['criteriaTables']) || ! is_array($_REQUEST['criteriaTables'])) { - unset($_REQUEST['submit_search']); -} elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) { - $criteriaTables = array_intersect( - $_REQUEST['criteriaTables'], $tables_names_only - ); -} - -if (isset($_REQUEST['selectall'])) { - $criteriaTables = $tables_names_only; -} elseif (isset($_REQUEST['unselectall'])) { - $criteriaTables = array(); -} - -if (empty($_REQUEST['criteriaColumnName']) - || ! is_string($_REQUEST['criteriaColumnName']) -) { - unset($criteriaColumnName); -} else { - $criteriaColumnName = $common_functions->sqlAddSlashes( - $_REQUEST['criteriaColumnName'], true - ); -} - -/** - * Displays top links if we are not in an Ajax request - */ -$sub_part = ''; +// Create a database search instance +$db_search = new PMA_DbSearch($GLOBALS['db']); +// Display top links if we are not in an Ajax request if ( $GLOBALS['is_ajax_request'] != true) { include 'libraries/db_info.inc.php'; $response->addHTML('
'); } -/** - * Main search form has been submitted - */ +// Main search form has been submitted, get results if (isset($_REQUEST['submit_search'])) { - $response->addHTML( - PMA_dbSearchGetSearchResults( - $criteriaTables, $searchTypeDescription, - $criteriaSearchString, $criteriaSearchType, - (! empty($criteriaColumnName) ? $criteriaColumnName : '') - ) - ); + $response->addHTML($db_search->getSearchResults()); } -/** - * If we are in an Ajax request, we need to exit after displaying all the HTML - */ +// 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 } -// Add search form -$response->addHTML( - PMA_dbSearchGetSelectionForm( - $criteriaSearchString, $criteriaSearchType, $tables_names_only, - $criteriaTables, $url_params, - (! empty($criteriaColumnName) ? $criteriaColumnName : '') - ) -); +// Display the search form +$response->addHTML($db_search->getSelectionForm($url_params)); ?> diff --git a/libraries/DbSearch.class.php b/libraries/DbSearch.class.php new file mode 100644 index 0000000000..8e7f14cf0b --- /dev/null +++ b/libraries/DbSearch.class.php @@ -0,0 +1,526 @@ +_db = $db; + // Sets criteria parameters + $this->_setSearchParams(); + } + + /** + * Set CommmonFunctions + * + * @param PMA_CommonFunctions $commonFunctions + * + * @return void + */ + public function setCommonFunctions(PMA_CommonFunctions $commonFunctions) + { + $this->_common_functions = $commonFunctions; + } + + + /** + * Get CommmonFunctions + * + * @return CommonFunctions object + */ + public function getCommonFunctions() + { + if (is_null($this->_common_functions)) { + $this->_common_functions = PMA_CommonFunctions::getInstance(); + } + return $this->_common_functions; + } + + /** + * Sets search parameters + * + */ + 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']); + } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) { + $this->_criteriaTables = array_intersect( + $_REQUEST['criteriaTables'], $this->_tables_names_only + ); + } + + if (isset($_REQUEST['selectall'])) { + $this->_criteriaTables = $this->_tables_names_only; + } elseif (isset($_REQUEST['unselectall'])) { + $this->_criteriaTables = array(); + } + + if (empty($_REQUEST['criteriaColumnName']) + || ! is_string($_REQUEST['criteriaColumnName']) + ) { + unset($this->_criteriaColumnName); + } else { + $this->_criteriaColumnName = $this->getCommonFunctions()->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 ' + . $this->getCommonFunctions()->backquote($GLOBALS['db']) . '.' + . $this->getCommonFunctions()->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 = $this->getCommonFunctions()->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) + ? $this->getCommonFunctions()->backquote($column['Field']) + : 'CONVERT(' . $this->getCommonFunctions()->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 .= '
' + . '' + . ''; + + $num_search_result_total = 0; + $odd_row = true; + // For each table selected as search criteria + foreach ($this->_criteriaTables as $each_table) { + // Gets the SQL statements + $newsearchsqls = $this->_getSearchSqls($each_table); + // Executes the "COUNT" statement + $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']); + $num_search_result_total += $res_cnt; + // Gets the result row's HTML for a table + $html_output .= $this->_getResultsRow( + $each_table, $newsearchsqls, $odd_row + ); + $odd_row = ! $odd_row; + } // 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 .= '

'; + } + 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 + */ + private function _getResultsRow($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 = ''; + // 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['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 array $url_params URL parameters + * + * @return string HTML for selection form + */ + public function getSelectionForm($url_params) + { + $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 .= ''; + // 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: "%"):') + . '
' . __('Find:') . ''; + $choices = array( + '1' => __('at least one of the words') + . $this->getCommonFunctions()->showHint( + __('Words are separated by a space character (" ").') + ), + '2' => __('all words') + . $this->getCommonFunctions()->showHint( + __('Words are separated by a space character (" ").') + ), + '3' => __('the exact phrase'), + '4' => __('as regular expression') + . ' ' . $this->getCommonFunctions()->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 .= $this->getCommonFunctions()->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 .= $this->_getResultDivs(); + + return $html_output; + } + + /** + * Provides div tags for browsing search results and sql query form. + * + * @return string div tags + */ + private 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/TableSearch.class.php b/libraries/TableSearch.class.php index df4cc2c69f..5c96dd6c0f 100644 --- a/libraries/TableSearch.class.php +++ b/libraries/TableSearch.class.php @@ -118,7 +118,6 @@ class PMA_TableSearch * @param string $table Table name * @param string $searchType Whether normal or zoom search * - * @return New PMA_TableSearch */ public function __construct($db, $table, $searchType) { @@ -132,7 +131,7 @@ class PMA_TableSearch $this->_geomColumnFlag = false; $this->_foreigners = array(); // Loads table's information - $this->_loadTableInfo($this->_db, $this->_table); + $this->_loadTableInfo(); } /** @@ -149,8 +148,6 @@ class PMA_TableSearch * Gets all the columns of a table along with their types, collations * and whether null or not. * - * @return array Array containing the column list, column types, collations - * and null constraint */ private function _loadTableInfo() { @@ -396,7 +393,7 @@ EOT; * @param bool $in_fbs Whether we are in 'function based search' * @param bool $in_zoom_search_edit Whether we are in zoom search edit * - * @return string HTML content for viewing foreing data and elements + * @return string HTML content for viewing foreign data and elements * for search criteria input. */ private function _getInputbox($foreignData, $column_name, $column_type, @@ -456,7 +453,7 @@ EOT; * Return the where clause in case column's type is ENUM. * * @param mixed $criteriaValues Search criteria input - * @param string $func_type Search fucntion/operator + * @param string $func_type Search function/operator * * @return string part of where clause. */ @@ -501,7 +498,7 @@ EOT; * * @param mixed $criteriaValues Search criteria input * @param string $names Name of the column on which search is submitted - * @param string $func_type Search fucntion/operator + * @param string $func_type Search function/operator * @param bool $geom_func Whether geometry functions should be applied * * @return string part of where clause. @@ -556,7 +553,7 @@ EOT; * @param string $names Name of the column on which search is submitted * @param string $types Type of the field * @param string $collations Field collation - * @param string $func_type Search fucntion/operator + * @param string $func_type Search function/operator * @param bool $unaryFlag Whether operator unary or not * @param bool $geom_func Whether geometry functions should be applied * diff --git a/libraries/db_search.lib.php b/libraries/db_search.lib.php deleted file mode 100644 index 5720246dc1..0000000000 --- a/libraries/db_search.lib.php +++ /dev/null @@ -1,389 +0,0 @@ - 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 -) { - $common_functions = PMA_CommonFunctions::getInstance(); - // Statement types - $sqlstr_select = 'SELECT'; - $sqlstr_delete = 'DELETE'; - // Table to use - $sqlstr_from = ' FROM ' - . $common_functions->backquote($GLOBALS['db']) . '.' - . $common_functions->backquote($table); - // Gets where clause for the query - $where_clause = PMA_dbSearchGetWhereClause( - $table, $criteriaSearchString, $criteriaSearchType, $criteriaColumnName - ); - // 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 $criteriaSearchString The search word/phrase/regexp to be searched - * @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 The generated where clause - */ -function PMA_dbSearchGetWhereClause($table, $criteriaSearchString, - $criteriaSearchType, $criteriaColumnName -) { - $common_functions = PMA_CommonFunctions::getInstance(); - $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 = (($criteriaSearchType == 4) ? 'REGEXP' : 'LIKE'); - $automatic_wildcard = (($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. - $criteriaSearchString = $common_functions->sqlAddSlashes( - $criteriaSearchString, ($criteriaSearchType == 4 ? false : true) - ); - // Extract search words or pattern - $search_words = (($criteriaSearchType > 2) - ? array($criteriaSearchString) : explode(' ', $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($criteriaColumnName) - || strlen($criteriaColumnName) == 0 - || $column['Field'] == $criteriaColumnName - ) { - // Drizzle has no CONVERT and all text columns are UTF-8 - $column = ((PMA_DRIZZLE) - ? $common_functions->backquote($column['Field']) - : 'CONVERT(' . $common_functions->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 = ($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 $searchTypeDescription Description for search type - * @param string $criteriaSearchString The search word/phrase/regexp to be searched - * @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, $searchTypeDescription, - $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; - // Gets the result row's HTML for a table - $html_output .= PMA_dbSearchGetResultsRow( - $each_table, $newsearchsqls, $odd_row - ); - $odd_row = ! $odd_row; - } // end for - $html_output .= '
' - . sprintf( - __('Search results for "%s" %s:'), - htmlspecialchars($criteriaSearchString), $searchTypeDescription - ) - . '
'; - // Displays total number of matches - 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 = ''; - // 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['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 $criteriaSearchString Keyword/Regular expression earlier entered - * @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($criteriaSearchString, $criteriaSearchType, - $tables_names_only, $criteriaTables, $url_params, $criteriaColumnName = null -) { - $common_functions = PMA_CommonFunctions::getInstance(); - $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 .= ''; - // 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: "%"):') - . '
' . __('Find:') . ''; - $choices = array( - '1' => __('at least one of the words') - . $common_functions->showHint( - __('Words are separated by a space character (" ").') - ), - '2' => __('all words') - . $common_functions->showHint( - __('Words are separated by a space character (" ").') - ), - '3' => __('the exact phrase'), - '4' => __('as regular expression') - . ' ' . $common_functions->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 .= $common_functions->getRadioFields( - 'criteriaSearchType', $choices, $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 .= 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; -} -?>