2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Handles Table search and Zoom search
8 if (! defined('PHPMYADMIN')) {
13 * Class to handle normal-search
14 * and zoom-search in a table
35 * Normal search or Zoom search
47 private $_columnNames;
54 private $_columnTypes;
56 * Collations of columns
61 private $_columnCollations;
63 * Null Flags of columns
68 private $_columnNullFlags;
70 * Whether a geometry column is present
75 private $_geomColumnFlag;
88 * @param string $db Database name
89 * @param string $table Table name
90 * @param string $searchType Whether normal or zoom search
92 public function __construct($db, $table, $searchType)
95 $this->_table
= $table;
96 $this->_searchType
= $searchType;
97 $this->_columnNames
= array();
98 $this->_columnNullFlags
= array();
99 $this->_columnTypes
= array();
100 $this->_columnCollations
= array();
101 $this->_geomColumnFlag
= false;
102 $this->_foreigners
= array();
103 // Loads table's information
104 $this->_loadTableInfo();
108 * Returns Column names array
110 * @return array column names
112 public function getColumnNames()
114 return $this->_columnNames
;
118 * Gets all the columns of a table along with their types, collations
119 * and whether null or not.
123 private function _loadTableInfo()
125 // Gets the list and number of columns
126 $columns = $GLOBALS['dbi']->getColumns(
127 $this->_db
, $this->_table
, null, true
129 // Get details about the geometry functions
130 $geom_types = PMA_Util
::getGISDatatypes();
132 foreach ($columns as $row) {
134 $this->_columnNames
[] = $row['Field'];
136 $type = $row['Type'];
137 // check whether table contains geometric columns
138 if (in_array($type, $geom_types)) {
139 $this->_geomColumnFlag
= true;
141 // reformat mysql query output
142 if (strncasecmp($type, 'set', 3) == 0
143 ||
strncasecmp($type, 'enum', 4) == 0
145 $type = str_replace(',', ', ', $type);
147 // strip the "BINARY" attribute, except if we find "BINARY(" because
148 // this would be a BINARY or VARBINARY column type
149 if (! preg_match('@BINARY[\(]@i', $type)) {
150 $type = preg_replace('@BINARY@i', '', $type);
152 $type = preg_replace('@ZEROFILL@i', '', $type);
153 $type = preg_replace('@UNSIGNED@i', '', $type);
154 $type = /*overload*/mb_strtolower($type);
159 $this->_columnTypes
[] = $type;
160 $this->_columnNullFlags
[] = $row['Null'];
161 $this->_columnCollations
[]
162 = ! empty($row['Collation']) && $row['Collation'] != 'NULL'
167 // Retrieve foreign keys
168 $this->_foreigners
= PMA_getForeigners($this->_db
, $this->_table
);
172 * Sets the table header for displaying a table in query-by-example format.
174 * @return string HTML content, the tags and content for table header
176 private function _getTableHeader()
178 // Display the Function column only if there is at least one geometry column
180 if ($this->_geomColumnFlag
) {
181 $func = '<th>' . __('Function') . '</th>';
185 <tr>' . $func . '<th>' . __('Column') . '</th>
186 <th>' . __('Type') . '</th>
187 <th>' . __('Collation') . '</th>
188 <th>' . __('Operator') . '</th>
189 <th>' . __('Value') . '</th>
195 * Returns an array with necessary configurations to create
196 * sub-tabs in the table_select page.
198 * @return array Array containing configuration (icon, text, link, id, args)
201 private function _getSubTabs()
204 $subtabs['search']['icon'] = 'b_search.png';
205 $subtabs['search']['text'] = __('Table search');
206 $subtabs['search']['link'] = 'tbl_select.php';
207 $subtabs['search']['id'] = 'tbl_search_id';
208 $subtabs['search']['args']['pos'] = 0;
210 $subtabs['zoom']['icon'] = 'b_props.png';
211 $subtabs['zoom']['link'] = 'tbl_zoom_select.php';
212 $subtabs['zoom']['text'] = __('Zoom search');
213 $subtabs['zoom']['id'] = 'zoom_search_id';
215 $subtabs['replace']['icon'] = 'b_find_replace.png';
216 $subtabs['replace']['link'] = 'tbl_find_replace.php';
217 $subtabs['replace']['text'] = __('Find and replace');
218 $subtabs['replace']['id'] = 'find_replace_id';
224 * Provides html elements for search criteria inputbox
225 * in case the column's type is geometrical
227 * @param int $column_index Column's index
228 * @param bool $in_fbs Whether we are in 'function based search'
230 * @return string HTML elements.
232 private function _getGeometricalInputBox($column_index, $in_fbs)
234 $html_output = '<input type="text" name="criteriaValues['
235 . $column_index . ']"'
236 . ' size="40" class="textfield" id="field_' . $column_index . '" />';
239 $edit_url = 'gis_data_editor.php' . PMA_URL_getCommon();
240 $edit_str = PMA_Util
::getIcon('b_edit.png', __('Edit/Insert'));
241 $html_output .= '<span class="open_search_gis_editor">';
242 $html_output .= PMA_Util
::linkOrButton(
243 $edit_url, $edit_str, array(), false, false, '_blank'
245 $html_output .= '</span>';
251 * Provides html elements for search criteria inputbox
252 * in case the column is a Foreign Key
254 * @param array $foreignData Foreign keys data
255 * @param string $column_name Column name
256 * @param int $column_index Column index
257 * @param array $titles Selected title
258 * @param int $foreignMaxLimit Max limit of displaying foreign elements
259 * @param array $criteriaValues Array of search criteria inputs
260 * @param string $column_id Column's inputbox's id
262 * @return string HTML elements.
264 private function _getForeignKeyInputBox($foreignData, $column_name,
265 $column_index, $titles, $foreignMaxLimit, $criteriaValues, $column_id
268 if (is_array($foreignData['disp_row'])) {
269 $html_output .= '<select name="criteriaValues[' . $column_index . ']"'
270 . ' id="' . $column_id . $column_index . '">';
271 $html_output .= PMA_foreignDropdown(
272 $foreignData['disp_row'], $foreignData['foreign_field'],
273 $foreignData['foreign_display'], '', $foreignMaxLimit
275 $html_output .= '</select>';
277 } elseif ($foreignData['foreign_link'] == true) {
278 $html_output .= '<input type="text" id="' . $column_id
279 . $column_index . '"'
280 . ' name="criteriaValues[' . $column_index . ']" id="field_'
281 . md5($column_name) . '[' . $column_index . ']" class="textfield"'
282 . (isset($criteriaValues[$column_index])
283 && is_string($criteriaValues[$column_index])
284 ?
(' value="' . $criteriaValues[$column_index] . '"')
288 $html_output .= '<a class="ajax browse_foreign" href="'
289 . 'browse_foreigners.php'
291 array('db' => $this->_db
, 'table' => $this->_table
)
293 . '&field=' . urlencode($column_name) . '&fieldkey='
294 . $column_index . '&fromsearch=1"';
295 $html_output .= '>' . str_replace("'", "\'", $titles['Browse']) . '</a>';
301 * Provides html elements for search criteria inputbox
302 * in case the column is of ENUM or SET type
304 * @param int $column_index Column index
305 * @param array $criteriaValues Array of search criteria inputs
306 * @param string $column_type Column type
307 * @param string $column_id Column's inputbox's id
308 * @param bool $in_zoom_search_edit Whether we are in zoom search edit
310 * @return string HTML elements.
312 private function _getEnumSetInputBox($column_index, $criteriaValues,
313 $column_type, $column_id, $in_zoom_search_edit = false
315 $column_type = htmlspecialchars($column_type);
319 str_replace("'", '', /*overload*/mb_substr($column_type, 5, -1))
321 $cnt_value = count($value);
324 * Enum in edit mode --> dropdown
325 * Enum in search mode --> multiselect
326 * Set in edit mode --> multiselect
327 * Set in search mode --> input (skipped here, so the 'else'
328 * section would handle it)
330 if ((strncasecmp($column_type, 'enum', 4) && ! $in_zoom_search_edit)
331 ||
(strncasecmp($column_type, 'set', 3) && $in_zoom_search_edit)
333 $html_output .= '<select name="criteriaValues[' . ($column_index)
334 . ']" id="' . $column_id . $column_index . '">';
336 $html_output .= '<select name="criteriaValues[' . $column_index . ']"'
337 . ' id="' . $column_id . $column_index . '" multiple="multiple"'
338 . ' size="' . min(3, $cnt_value) . '">';
342 for ($j = 0; $j < $cnt_value; $j++
) {
343 if (isset($criteriaValues[$column_index])
344 && is_array($criteriaValues[$column_index])
345 && in_array($value[$j], $criteriaValues[$column_index])
347 $html_output .= '<option value="' . $value[$j] . '" Selected>'
348 . $value[$j] . '</option>';
350 $html_output .= '<option value="' . $value[$j] . '">'
351 . $value[$j] . '</option>';
354 $html_output .= '</select>';
359 * Creates the HTML content for:
360 * 1) Browsing foreign data for a column.
361 * 2) Creating elements for search criteria input on columns.
363 * @param array $foreignData Foreign keys data
364 * @param string $column_name Column name
365 * @param string $column_type Column type
366 * @param int $column_index Column index
367 * @param array $titles Selected title
368 * @param int $foreignMaxLimit Max limit of displaying foreign elements
369 * @param array $criteriaValues Array of search criteria inputs
370 * @param bool $in_fbs Whether we are in 'function based search'
371 * @param bool $in_zoom_search_edit Whether we are in zoom search edit
373 * @return string HTML content for viewing foreign data and elements
374 * for search criteria input.
376 private function _getInputbox($foreignData, $column_name, $column_type,
377 $column_index, $titles, $foreignMaxLimit, $criteriaValues, $in_fbs = false,
378 $in_zoom_search_edit = false
381 $column_type = (string)$column_type;
382 $column_id = ($in_zoom_search_edit) ?
'edit_fieldID_' : 'fieldID_';
384 // Get inputbox based on different column types
385 // (Foreign key, geometrical, enum)
386 if ($this->_foreigners
387 && PMA_searchColumnInForeigners($this->_foreigners
, $column_name)
389 $str .= $this->_getForeignKeyInputBox(
390 $foreignData, $column_name, $column_index, $titles,
391 $foreignMaxLimit, $criteriaValues, $column_id
394 } elseif (in_array($column_type, PMA_Util
::getGISDatatypes())) {
395 $str .= $this->_getGeometricalInputBox($column_index, $in_fbs);
397 } elseif (strncasecmp($column_type, 'enum', 4) == 0
398 ||
(strncasecmp($column_type, 'set', 3) == 0 && $in_zoom_search_edit)
400 $str .= $this->_getEnumSetInputBox(
401 $column_index, $criteriaValues, $column_type, $column_id,
402 $in_zoom_search_edit = false
407 $the_class = 'textfield';
409 if ($column_type == 'date') {
410 $the_class .= ' datefield';
411 } elseif ($column_type == 'datetime'
412 ||
substr($column_type, 0, 9) == 'timestamp'
414 $the_class .= ' datetimefield';
415 } elseif (substr($column_type, 0, 3) == 'bit') {
416 $the_class .= ' bit';
419 $str .= '<input type="text" name="criteriaValues[' . $column_index . ']"'
420 . ' size="40" class="' . $the_class . '" id="'
421 . $column_id . $column_index . '"'
422 . (isset($criteriaValues[$column_index])
423 && is_string($criteriaValues[$column_index])
424 ?
(' value="' . $criteriaValues[$column_index] . '"')
432 * Return the where clause in case column's type is ENUM.
434 * @param mixed $criteriaValues Search criteria input
435 * @param string $func_type Search function/operator
437 * @return string part of where clause.
439 private function _getEnumWhereClause($criteriaValues, $func_type)
441 if (! is_array($criteriaValues)) {
442 $criteriaValues = explode(',', $criteriaValues);
444 $enum_selected_count = count($criteriaValues);
445 if ($func_type == '=' && $enum_selected_count > 1) {
450 } elseif ($func_type == '!=' && $enum_selected_count > 1) {
451 $func_type = 'NOT IN';
460 . PMA_Util
::sqlAddSlashes($criteriaValues[0]) . '\'';
461 for ($e = 1; $e < $enum_selected_count; $e++
) {
462 $enum_where .= ', \''
463 . PMA_Util
::sqlAddSlashes($criteriaValues[$e]) . '\'';
466 return ' ' . $func_type . ' ' . $parens_open
467 . $enum_where . $parens_close;
471 * Return the where clause for a geometrical column.
473 * @param mixed $criteriaValues Search criteria input
474 * @param string $names Name of the column on which search is submitted
475 * @param string $func_type Search function/operator
476 * @param string $types Type of the field
477 * @param bool $geom_func Whether geometry functions should be applied
479 * @return string part of where clause.
481 private function _getGeomWhereClause($criteriaValues, $names,
482 $func_type, $types, $geom_func = null
484 $geom_unary_functions = array(
492 // Get details about the geometry functions
493 $geom_funcs = PMA_Util
::getGISFunctions($types, true, false);
494 // New output type is the output type of the function being applied
495 $types = $geom_funcs[$geom_func]['type'];
497 // If the function takes a single parameter
498 if ($geom_funcs[$geom_func]['params'] == 1) {
499 $backquoted_name = $geom_func . '(' . PMA_Util
::backquote($names) . ')';
501 // If the function takes two parameters
502 // create gis data from the criteria input
503 $gis_data = PMA_Util
::createGISData($criteriaValues);
504 $where = $geom_func . '(' . PMA_Util
::backquote($names)
505 . ',' . $gis_data . ')';
509 // If the where clause is something like 'IsEmpty(`spatial_col_name`)'
510 if (isset($geom_unary_functions[$geom_func])
511 && trim($criteriaValues) == ''
513 $where = $backquoted_name;
515 } elseif (in_array($types, PMA_Util
::getGISDatatypes())
516 && ! empty($criteriaValues)
518 // create gis data from the criteria input
519 $gis_data = PMA_Util
::createGISData($criteriaValues);
520 $where = $backquoted_name . ' ' . $func_type . ' ' . $gis_data;
526 * Return the where clause for query generation based on the inputs provided.
528 * @param mixed $criteriaValues Search criteria input
529 * @param string $names Name of the column on which search is submitted
530 * @param string $types Type of the field
531 * @param string $func_type Search function/operator
532 * @param bool $unaryFlag Whether operator unary or not
533 * @param bool $geom_func Whether geometry functions should be applied
535 * @return string generated where clause.
537 private function _getWhereClause($criteriaValues, $names, $types,
538 $func_type, $unaryFlag, $geom_func = null
540 // If geometry function is set
541 if ($geom_func != null && trim($geom_func) != '') {
542 return $this->_getGeomWhereClause(
543 $criteriaValues, $names, $func_type, $types, $geom_func
547 $backquoted_name = PMA_Util
::backquote($names);
550 $where = $backquoted_name . ' ' . $func_type;
552 } elseif (strncasecmp($types, 'enum', 4) == 0 && ! empty($criteriaValues)) {
553 $where = $backquoted_name;
554 $where .= $this->_getEnumWhereClause($criteriaValues, $func_type);
556 } elseif ($criteriaValues != '') {
557 // For these types we quote the value. Even if it's another type
558 // (like INT), for a LIKE we always quote the value. MySQL converts
559 // strings to numbers and numbers to strings as necessary
560 // during the comparison
561 if (preg_match('@char|binary|blob|text|set|date|time|year@i', $types)
562 ||
/*overload*/mb_strpos(' ' . $func_type, 'LIKE')
570 if ($func_type == 'LIKE %...%') {
572 $criteriaValues = '%' . $criteriaValues . '%';
574 if ($func_type == 'REGEXP ^...$') {
575 $func_type = 'REGEXP';
576 $criteriaValues = '^' . $criteriaValues . '$';
579 if ('IN (...)' != $func_type
580 && 'NOT IN (...)' != $func_type
581 && 'BETWEEN' != $func_type
582 && 'NOT BETWEEN' != $func_type
584 if ($func_type == 'LIKE %...%' ||
$func_type == 'LIKE') {
585 $where = $backquoted_name . ' ' . $func_type . ' ' . $quot
586 . PMA_Util
::sqlAddSlashes($criteriaValues, true) . $quot;
588 $where = $backquoted_name . ' ' . $func_type . ' ' . $quot
589 . PMA_Util
::sqlAddSlashes($criteriaValues) . $quot;
593 $func_type = str_replace(' (...)', '', $func_type);
595 //Don't explode if this is already an array
596 //(Case for (NOT) IN/BETWEEN.)
597 if (is_array($criteriaValues)) {
598 $values = $criteriaValues;
600 $values = explode(',', $criteriaValues);
602 // quote values one by one
604 foreach ($values as $key => &$value) {
610 $value = $quot . PMA_Util
::sqlAddSlashes(trim($value))
614 if ('BETWEEN' == $func_type ||
'NOT BETWEEN' == $func_type) {
615 $where = $backquoted_name . ' ' . $func_type . ' '
616 . (isset($values[0]) ?
$values[0] : '')
617 . ' AND ' . (isset($values[1]) ?
$values[1] : '');
619 if (false !== $emptyKey) {
620 unset($values[$emptyKey]);
623 if (!empty($values)) {
624 $wheres[] = $backquoted_name . ' ' . $func_type
625 . ' (' . implode(',', $values) . ')';
627 if (false !== $emptyKey) {
628 $wheres[] = $backquoted_name . ' IS NULL';
630 $where = implode(' OR ', $wheres);
631 if (1 < count($wheres)) {
632 $where = '(' . $where . ')';
641 * Builds the sql search query from the post parameters
643 * @return string the generated SQL query
645 public function buildSqlQuery()
647 $sql_query = 'SELECT ';
649 // If only distinct values are needed
650 $is_distinct = (isset($_POST['distinct'])) ?
'true' : 'false';
651 if ($is_distinct == 'true') {
652 $sql_query .= 'DISTINCT ';
655 // if all column names were selected to display, we do a 'SELECT *'
656 // (more efficient and this helps prevent a problem in IE
657 // if one of the rows is edited and we come back to the Select results)
658 if (isset($_POST['zoom_submit']) ||
! empty($_POST['displayAllColumns'])) {
661 $sql_query .= implode(
663 PMA_Util
::backquote($_POST['columnsToDisplay'])
667 $sql_query .= ' FROM '
668 . PMA_Util
::backquote($_POST['table']);
669 $whereClause = $this->_generateWhereClause();
670 $sql_query .= $whereClause;
672 // if the search results are to be ordered
673 if (isset($_POST['orderByColumn']) && $_POST['orderByColumn'] != '--nil--') {
674 $sql_query .= ' ORDER BY '
675 . PMA_Util
::backquote($_POST['orderByColumn'])
676 . ' ' . $_POST['order'];
682 * Generates the where clause for the SQL search query to be executed
684 * @return string the generated where clause
686 private function _generateWhereClause()
688 if (isset($_POST['customWhereClause'])
689 && trim($_POST['customWhereClause']) != ''
691 return ' WHERE ' . $_POST['customWhereClause'];
694 // If there are no search criteria set or no unary criteria operators,
696 if (! isset($_POST['criteriaValues'])
697 && ! isset($_POST['criteriaColumnOperators'])
702 // else continue to form the where clause from column criteria values
703 $fullWhereClause = array();
704 reset($_POST['criteriaColumnOperators']);
705 while (list($column_index, $operator) = each(
706 $_POST['criteriaColumnOperators']
709 $unaryFlag = $GLOBALS['PMA_Types']->isUnaryOperator($operator);
710 $tmp_geom_func = isset($geom_func[$column_index])
711 ?
$geom_func[$column_index] : null;
713 $whereClause = $this->_getWhereClause(
714 $_POST['criteriaValues'][$column_index],
715 $_POST['criteriaColumnNames'][$column_index],
716 $_POST['criteriaColumnTypes'][$column_index],
723 $fullWhereClause[] = $whereClause;
727 if ($fullWhereClause) {
728 return ' WHERE ' . implode(' AND ', $fullWhereClause);
734 * Generates HTML for a geometrical function column to be displayed in table
735 * search selection form
737 * @param integer $column_index index of current column in $columnTypes array
739 * @return string the generated HTML
741 private function _getGeomFuncHtml($column_index)
744 // return if geometrical column is not present
745 if (! $this->_geomColumnFlag
) {
750 * Displays 'Function' column if it is present
752 $html_output .= '<td>';
753 $geom_types = PMA_Util
::getGISDatatypes();
754 // if a geometry column is present
755 if (in_array($this->_columnTypes
[$column_index], $geom_types)) {
756 $html_output .= '<select class="geom_func" name="geom_func['
757 . $column_index . ']">';
758 // get the relevant list of GIS functions
759 $funcs = PMA_Util
::getGISFunctions(
760 $this->_columnTypes
[$column_index], true, true
763 * For each function in the list of functions,
764 * add an option to select list
766 foreach ($funcs as $func_name => $func) {
767 $name = isset($func['display']) ?
$func['display'] : $func_name;
768 $html_output .= '<option value="' . htmlspecialchars($name) . '">'
769 . htmlspecialchars($name) . '</option>';
771 $html_output .= '</select>';
773 $html_output .= ' ';
775 $html_output .= '</td>';
780 * Generates formatted HTML for extra search options in table search form
782 * @return string the generated HTML
784 private function _getOptions()
787 $html_output .= PMA_Util
::getDivForSliderEffect(
788 'searchoptions', __('Options')
792 * Displays columns select list for selecting distinct columns in the search
794 $html_output .= '<fieldset id="fieldset_select_fields">'
795 . '<legend>' . __('Select columns (at least one):') . '</legend>'
796 . '<select name="columnsToDisplay[]"'
797 . ' size="' . min(count($this->_columnNames
), 10) . '"'
798 . ' multiple="multiple">';
799 // Displays the list of the fields
800 foreach ($this->_columnNames
as $each_field) {
802 . '<option value="' . htmlspecialchars($each_field) . '"'
803 . ' selected="selected">' . htmlspecialchars($each_field)
804 . '</option>' . "\n";
806 $html_output .= '</select>'
807 . '<input type="checkbox" name="distinct" value="DISTINCT"'
808 . ' id="oDistinct" />'
809 . '<label for="oDistinct">DISTINCT</label></fieldset>';
812 * Displays input box for custom 'Where' clause to be used in the search
814 $html_output .= '<fieldset id="fieldset_search_conditions">'
815 . '<legend>' . '<em>' . __('Or') . '</em> '
816 . __('Add search conditions (body of the "where" clause):')
818 $html_output .= PMA_Util
::showMySQLDocu('Functions');
819 $html_output .= '<input type="text" name="customWhereClause"'
820 . ' class="textfield" size="64" />';
821 $html_output .= '</fieldset>';
824 * Displays option of changing default number of rows displayed per page
826 $html_output .= '<fieldset id="fieldset_limit_rows">'
827 . '<legend>' . __('Number of rows per page') . '</legend>'
828 . '<input type="number" name="session_max_rows" required="required" '
830 . 'value="' . $GLOBALS['cfg']['MaxRows'] . '" class="textfield" />'
834 * Displays option for ordering search results
835 * by a column value (Asc or Desc)
837 $html_output .= '<fieldset id="fieldset_display_order">'
838 . '<legend>' . __('Display order:') . '</legend>'
839 . '<select name="orderByColumn"><option value="--nil--"></option>';
840 foreach ($this->_columnNames
as $each_field) {
842 . '<option value="' . htmlspecialchars($each_field) . '">'
843 . htmlspecialchars($each_field) . '</option>' . "\n";
845 $html_output .= '</select>';
847 'ASC' => __('Ascending'),
848 'DESC' => __('Descending')
850 $html_output .= PMA_Util
::getRadioFields(
851 'order', $choices, 'ASC', false, true, "formelement"
855 $html_output .= '</fieldset><br style="clear: both;"/></div>';
860 * Other search criteria like data label
861 * (for tbl_zoom_select.php)
863 * @param string|null $dataLabel Label for points in zoom plot
865 * @return string the generated html
867 private function _getOptionsZoom($dataLabel)
870 $html_output .= '<table class="data">';
871 //Select options for datalabel
872 $html_output .= '<tr>';
873 $html_output .= '<td><label for="dataLabel">'
874 . __("Use this column to label each point") . '</label></td>';
875 $html_output .= '<td><select name="dataLabel" id="dataLabel" >'
876 . '<option value = "">' . __('None') . '</option>';
877 for ($j = 0, $nb = count($this->_columnNames
); $j < $nb; $j++
) {
878 if (isset($dataLabel)
879 && $dataLabel == htmlspecialchars($this->_columnNames
[$j])
881 $html_output .= '<option value="'
882 . htmlspecialchars($this->_columnNames
[$j])
883 . '" selected="selected">'
884 . htmlspecialchars($this->_columnNames
[$j])
887 $html_output .= '<option value="'
888 . htmlspecialchars($this->_columnNames
[$j]) . '" >'
889 . htmlspecialchars($this->_columnNames
[$j]) . '</option>';
892 $html_output .= '</select></td>';
893 $html_output .= '</tr>';
894 //Inputbox for changing default maximum rows to plot
895 $html_output .= '<tr>';
896 $html_output .= '<td><label for="maxRowPlotLimit">'
897 . __("Maximum rows to plot") . '</label></td>';
898 $html_output .= '<td>';
899 $html_output .= '<input type="number" name="maxPlotLimit"'
900 . ' id="maxRowPlotLimit" required="required"'
901 . ' value="' . ((! empty($_POST['maxPlotLimit']))
902 ?
htmlspecialchars($_POST['maxPlotLimit'])
903 : $GLOBALS['cfg']['maxRowPlotLimit'])
905 $html_output .= '</td></tr>';
906 $html_output .= '</table>';
911 * Provides a column's type, collation, operators list, and criteria value
912 * to display in table search form
914 * @param integer $search_index Row number in table search form
915 * @param integer $column_index Column index in ColumnNames array
917 * @return array Array containing column's properties
919 public function getColumnProperties($search_index, $column_index)
921 $selected_operator = (isset($_POST['criteriaColumnOperators'])
922 ?
$_POST['criteriaColumnOperators'][$search_index] : '');
923 $entered_value = (isset($_POST['criteriaValues'])
924 ?
$_POST['criteriaValues'] : '');
926 'Browse' => PMA_Util
::getIcon(
927 'b_browse.png', __('Browse foreign values')
930 //Gets column's type and collation
931 $type = $this->_columnTypes
[$column_index];
932 $collation = $this->_columnCollations
[$column_index];
933 //Gets column's comparison operators depending on column type
934 $func = '<select name="criteriaColumnOperators['
935 . $search_index . ']" onchange="changeValueFieldType(this, '
936 . $search_index . ')">';
937 $func .= $GLOBALS['PMA_Types']->getTypeOperatorsHtml(
938 preg_replace('@\(.*@s', '', $this->_columnTypes
[$column_index]),
939 $this->_columnNullFlags
[$column_index], $selected_operator
941 $func .= '</select>';
942 //Gets link to browse foreign data(if any) and criteria inputbox
943 $foreignData = PMA_getForeignData(
944 $this->_foreigners
, $this->_columnNames
[$column_index], false, '', ''
946 $value = $this->_getInputbox(
947 $foreignData, $this->_columnNames
[$column_index], $type, $search_index,
948 $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'], $entered_value
952 'collation' => $collation,
959 * Provides the search form's table row in case of Normal Search
960 * (for tbl_select.php)
962 * @return string the generated table row
964 private function _getRowsNormal()
968 // for every column present in table
970 $column_index = 0, $nb = count($this->_columnNames
);
974 $html_output .= '<tr class="noclick '
975 . ($odd_row ?
'odd' : 'even')
977 $odd_row = !$odd_row;
978 //If 'Function' column is present
979 $html_output .= $this->_getGeomFuncHtml($column_index);
980 //Displays column's name, type, collation and value
981 $html_output .= '<th>'
982 . htmlspecialchars($this->_columnNames
[$column_index]) . '</th>';
983 $properties = $this->getColumnProperties($column_index, $column_index);
984 $html_output .= '<td>'
985 . htmlspecialchars($properties['type'])
987 $html_output .= '<td>' . $properties['collation'] . '</td>';
988 $html_output .= '<td>' . $properties['func'] . '</td>';
989 // here, the data-type attribute is needed for a date/time picker
990 $html_output .= '<td data-type="'
991 . htmlspecialchars($properties['type']) . '"'
992 . '>' . $properties['value'] . '</td>';
993 $html_output .= '</tr>';
994 //Displays hidden fields
995 $html_output .= '<tr><td>';
996 $html_output .= '<input type="hidden"'
997 . ' name="criteriaColumnNames[' . $column_index . ']"'
999 . htmlspecialchars($this->_columnNames
[$column_index])
1001 $html_output .= '<input type="hidden"'
1002 . ' name="criteriaColumnTypes[' . $column_index . ']"'
1004 . htmlspecialchars($this->_columnTypes
[$column_index]) . '" />';
1005 $html_output .= '<input type="hidden"'
1006 . ' name="criteriaColumnCollations[' . $column_index . ']"'
1007 . ' value="' . $this->_columnCollations
[$column_index] . '" />';
1008 $html_output .= '</td></tr>';
1011 return $html_output;
1015 * Provides the search form's table row in case of Zoom search
1016 * (for tbl_zoom_select.php)
1018 * @return string the generated table row
1020 private function _getRowsZoom()
1024 $type = $collation = $func = $value = array();
1026 * Get already set search criteria (if any)
1029 //Displays column rows for search criteria input
1030 for ($i = 0; $i < 4; $i++
) {
1031 //After X-Axis and Y-Axis column rows, display additional criteria
1034 $html_output .= '<tr><td>';
1035 $html_output .= __("Additional search criteria");
1036 $html_output .= '</td></tr>';
1038 $html_output .= '<tr class="noclick '
1039 . ($odd_row ?
'odd' : 'even')
1041 $odd_row = ! $odd_row;
1042 //Select options for column names
1043 $html_output .= '<th><select name="criteriaColumnNames[]" id="'
1044 . 'tableid_' . $i . '" >';
1045 $html_output .= '<option value="' . 'pma_null' . '">' . __('None')
1047 for ($j = 0, $nb = count($this->_columnNames
); $j < $nb; $j++
) {
1048 if (isset($_POST['criteriaColumnNames'][$i])
1049 && $_POST['criteriaColumnNames'][$i] == htmlspecialchars($this->_columnNames
[$j])
1051 $html_output .= '<option value="'
1052 . htmlspecialchars($this->_columnNames
[$j])
1053 . '" selected="selected">'
1054 . htmlspecialchars($this->_columnNames
[$j])
1057 $html_output .= '<option value="'
1058 . htmlspecialchars($this->_columnNames
[$j]) . '">'
1059 . htmlspecialchars($this->_columnNames
[$j]) . '</option>';
1062 $html_output .= '</select></th>';
1063 if (isset($_POST['criteriaColumnNames'])
1064 && $_POST['criteriaColumnNames'][$i] != 'pma_null'
1066 $key = array_search(
1067 $_POST['criteriaColumnNames'][$i],
1070 $properties = $this->getColumnProperties($i, $key);
1071 $type[$i] = $properties['type'];
1072 $collation[$i] = $properties['collation'];
1073 $func[$i] = $properties['func'];
1074 $value[$i] = $properties['value'];
1077 $html_output .= '<td>' . (isset($type[$i]) ?
$type[$i] : '') . '</td>';
1079 $html_output .= '<td>' . (isset($collation[$i]) ?
$collation[$i] : '')
1081 //Select options for column operators
1082 $html_output .= '<td>' . (isset($func[$i]) ?
$func[$i] : '') . '</td>';
1083 //Inputbox for search criteria value
1084 $html_output .= '<td>' . (isset($value[$i]) ?
$value[$i] : '') . '</td>';
1085 $html_output .= '</tr>';
1086 //Displays hidden fields
1087 $html_output .= '<tr><td>';
1089 .= '<input type="hidden" name="criteriaColumnTypes[' . $i . ']"'
1090 . ' id="types_' . $i . '" ';
1091 if (isset($_POST['criteriaColumnTypes'][$i])) {
1092 $html_output .= 'value="' . $_POST['criteriaColumnTypes'][$i] . '" ';
1094 $html_output .= '/>';
1095 $html_output .= '<input type="hidden" name="criteriaColumnCollations['
1096 . $i . ']" id="collations_' . $i . '" />';
1097 $html_output .= '</td></tr>';
1099 return $html_output;
1103 * Generates HTML for displaying fields table in search form
1105 * @return string the generated HTML
1107 private function _getFieldsTableHtml()
1110 $html_output .= '<table class="data"'
1111 . ($this->_searchType
== 'zoom' ?
' id="tableFieldsId"' : '') . '>';
1112 $html_output .= $this->_getTableHeader();
1113 $html_output .= '<tbody>';
1115 if ($this->_searchType
== 'zoom') {
1116 $html_output .= $this->_getRowsZoom();
1118 $html_output .= $this->_getRowsNormal();
1121 $html_output .= '</tbody></table>';
1122 return $html_output;
1126 * Provides the form tag for table search form
1127 * (normal search or zoom search)
1129 * @param string $goto Goto URL
1131 * @return string the HTML for form tag
1133 private function _getFormTag($goto)
1138 switch ($this->_searchType
) {
1140 $scriptName = 'tbl_select.php';
1141 $formId = 'tbl_search_form';
1144 $scriptName = 'tbl_zoom_select.php';
1145 $formId = 'zoom_search_form';
1148 $scriptName = 'tbl_find_replace.php';
1149 $formId = 'find_replace_form';
1153 $html_output .= '<form method="post" action="' . $scriptName . '" '
1154 . 'name="insertForm" id="' . $formId . '" '
1155 . 'class="ajax"' . '>';
1157 $html_output .= PMA_URL_getHiddenInputs($this->_db
, $this->_table
);
1158 $html_output .= '<input type="hidden" name="goto" value="' . $goto . '" />';
1159 $html_output .= '<input type="hidden" name="back" value="' . $scriptName
1162 return $html_output;
1166 * Returns the HTML for secondary levels tabs of the table search page
1168 * @return string HTML for secondary levels tabs
1170 public function getSecondaryTabs()
1172 $url_params = array();
1173 $url_params['db'] = $this->_db
;
1174 $url_params['table'] = $this->_table
;
1176 $html_output = '<ul id="topmenu2">';
1177 foreach ($this->_getSubTabs() as $tab) {
1178 $html_output .= PMA_Util
::getHtmlTab($tab, $url_params);
1180 $html_output .= '</ul>';
1181 $html_output .= '<div class="clearfloat"></div>';
1182 return $html_output;
1186 * Generates the table search form under table search tab
1188 * @param string $goto Goto URL
1189 * @param string|null $dataLabel Label for points in zoom plot
1191 * @return string the generated HTML for table search form
1193 public function getSelectionForm($goto, $dataLabel = null)
1195 $html_output = $this->_getFormTag($goto);
1197 if ($this->_searchType
== 'zoom') {
1198 $html_output .= '<fieldset id="fieldset_zoom_search">';
1199 $html_output .= '<fieldset id="inputSection">';
1200 $html_output .= '<legend>'
1202 'Do a "query by example" (wildcard: "%") for two'
1203 . ' different columns'
1206 $html_output .= $this->_getFieldsTableHtml();
1207 $html_output .= $this->_getOptionsZoom($dataLabel);
1208 $html_output .= '</fieldset>';
1209 $html_output .= '</fieldset>';
1210 } else if ($this->_searchType
== 'normal') {
1211 $html_output .= '<fieldset id="fieldset_table_search">';
1212 $html_output .= '<fieldset id="fieldset_table_qbe">';
1213 $html_output .= '<legend>'
1214 . __('Do a "query by example" (wildcard: "%")')
1216 $html_output .= $this->_getFieldsTableHtml();
1217 $html_output .= '<div id="gis_editor"></div>';
1218 $html_output .= '<div id="popup_background"></div>';
1219 $html_output .= '</fieldset>';
1220 $html_output .= $this->_getOptions();
1221 $html_output .= '</fieldset>';
1222 } else if ($this->_searchType
== 'replace') {
1223 $html_output .= '<fieldset id="fieldset_find_replace">';
1224 $html_output .= '<fieldset id="fieldset_find">';
1225 $html_output .= '<legend>' . __('Find and replace') . '</legend>';
1226 $html_output .= $this->_getSearchAndReplaceHTML();
1227 $html_output .= '</fieldset>';
1228 $html_output .= '</fieldset>';
1232 * Displays selection form's footer elements
1234 $html_output .= '<fieldset class="tblFooters">';
1235 $html_output .= '<input type="submit" name="'
1236 . ($this->_searchType
== 'zoom' ?
'zoom_submit' : 'submit')
1237 . ($this->_searchType
== 'zoom' ?
'" id="inputFormSubmitId"' : '" ')
1238 . 'value="' . __('Go') . '" />';
1239 $html_output .= '</fieldset></form>';
1240 $html_output .= '<div id="sqlqueryresultsouter"></div>';
1241 return $html_output;
1245 * Provides form for displaying point data and also the scatter plot
1246 * (for tbl_zoom_select.php)
1248 * @param string $goto Goto URL
1249 * @param array $data Array containing SQL query data
1251 * @return string form's html
1253 public function getZoomResultsForm($goto, $data)
1257 'Browse' => PMA_Util
::getIcon(
1259 __('Browse foreign values')
1262 $html_output .= '<form method="post" action="tbl_zoom_select.php"'
1263 . ' name="displayResultForm" id="zoom_display_form"'
1264 . ' class="ajax"' . '>';
1265 $html_output .= PMA_URL_getHiddenInputs($this->_db
, $this->_table
);
1266 $html_output .= '<input type="hidden" name="goto" value="' . $goto . '" />';
1268 .= '<input type="hidden" name="back" value="tbl_zoom_select.php" />';
1270 $html_output .= '<fieldset id="displaySection">';
1271 $html_output .= '<legend>' . __('Browse/Edit the points') . '</legend>';
1273 //JSON encode the data(query result)
1274 $html_output .= '<center>';
1275 if (isset($_POST['zoom_submit']) && ! empty($data)) {
1276 $html_output .= '<div id="resizer">';
1277 $html_output .= '<center><a href="#" onclick="displayHelp();">'
1278 . __('How to use') . '</a></center>';
1279 $html_output .= '<div id="querydata" style="display:none">'
1280 . json_encode($data) . '</div>';
1281 $html_output .= '<div id="querychart"></div>';
1282 $html_output .= '<button class="button-reset">'
1283 . __('Reset zoom') . '</button>';
1284 $html_output .= '</div>';
1286 $html_output .= '</center>';
1288 //Displays rows in point edit form
1289 $html_output .= '<div id="dataDisplay" style="display:none">';
1290 $html_output .= '<table><thead>';
1291 $html_output .= '<tr>';
1292 $html_output .= '<th>' . __('Column') . '</th>'
1293 . '<th>' . __('Null') . '</th>'
1294 . '<th>' . __('Value') . '</th>';
1295 $html_output .= '</tr>';
1296 $html_output .= '</thead>';
1298 $html_output .= '<tbody>';
1301 $column_index = 0, $nb = count($this->_columnNames
);
1302 $column_index < $nb;
1305 $fieldpopup = $this->_columnNames
[$column_index];
1306 $foreignData = PMA_getForeignData(
1314 .= '<tr class="noclick ' . ($odd_row ?
'odd' : 'even') . '">';
1315 $odd_row = ! $odd_row;
1316 //Display column Names
1318 .= '<th>' . htmlspecialchars($this->_columnNames
[$column_index])
1320 //Null checkbox if column can be null
1321 $html_output .= '<th>'
1322 . (($this->_columnNullFlags
[$column_index] == 'YES')
1323 ?
'<input type="checkbox" class="checkbox_null"'
1324 . ' name="criteriaColumnNullFlags[' . $column_index . ']"'
1325 . ' id="edit_fields_null_id_' . $column_index . '" />'
1327 $html_output .= '</th>';
1328 //Column's Input box
1329 $html_output .= '<th>';
1330 $html_output .= $this->_getInputbox(
1331 $foreignData, $fieldpopup, $this->_columnTypes
[$column_index],
1332 $column_index, $titles, $GLOBALS['cfg']['ForeignKeyMaxLimit'],
1335 $html_output .= '</th></tr>';
1337 $html_output .= '</tbody></table>';
1338 $html_output .= '</div>';
1339 $html_output .= '<input type="hidden" id="queryID" name="sql_query" />';
1340 $html_output .= '</form>';
1341 return $html_output;
1345 * Displays the 'Find and replace' form
1347 * @return string HTML for 'Find and replace' form
1349 function _getSearchAndReplaceHTML()
1351 $htmlOutput = __('Find:')
1352 . '<input type="text" value="" name="find" required />';
1353 $htmlOutput .= __('Replace with:')
1354 . '<input type="text" value="" name="replaceWith" required />';
1356 $htmlOutput .= __('Column:') . '<select name="columnIndex">';
1357 for ($i = 0, $nb = count($this->_columnNames
); $i < $nb; $i++
) {
1358 $type = preg_replace('@\(.*@s', '', $this->_columnTypes
[$i]);
1359 if ($GLOBALS['PMA_Types']->getTypeClass($type) == 'CHAR') {
1360 $column = $this->_columnNames
[$i];
1361 $htmlOutput .= '<option value="' . $i . '">'
1362 . htmlspecialchars($column) . '</option>';
1365 $htmlOutput .= '</select>';
1367 $htmlOutput .= '<br>'
1368 . PMA_Util
::getCheckbox(
1370 __('Use regular expression'),
1379 * Finds and returns Regex pattern and their replacements
1381 * @param int $columnIndex index of the column
1382 * @param string $find string to find in the column
1383 * @param string $replaceWith string to replace with
1384 * @param string $charSet character set of the connection
1386 * @return array Array containing original values, replaced values and count
1388 function _getRegexReplaceRows($columnIndex, $find, $replaceWith, $charSet)
1390 $column = $this->_columnNames
[$columnIndex];
1391 $sql_query = "SELECT "
1392 . PMA_Util
::backquote($column) . ","
1393 . " 1," // to add an extra column that will have replaced value
1395 . " FROM " . PMA_Util
::backquote($this->_db
)
1396 . "." . PMA_Util
::backquote($this->_table
)
1397 . " WHERE " . PMA_Util
::backquote($column)
1398 . " RLIKE '" . PMA_Util
::sqlAddSlashes($find) . "' COLLATE "
1399 . $charSet . "_bin"; // here we
1400 // change the collation of the 2nd operand to a case sensitive
1401 // binary collation to make sure that the comparison is case sensitive
1402 $sql_query .= " GROUP BY " . PMA_Util
::backquote($column)
1403 . " ORDER BY " . PMA_Util
::backquote($column) . " ASC";
1405 $result = $GLOBALS['dbi']->fetchResult($sql_query, 0);
1407 if (is_array($result)) {
1408 foreach ($result as $index=>$row) {
1409 $result[$index][1] = preg_replace(
1420 * Returns HTML for previewing strings found and their replacements
1422 * @param int $columnIndex index of the column
1423 * @param string $find string to find in the column
1424 * @param string $replaceWith string to replace with
1425 * @param boolean $useRegex to use Regex replace or not
1426 * @param string $charSet character set of the connection
1428 * @return string HTML for previewing strings found and their replacements
1430 function getReplacePreview($columnIndex, $find, $replaceWith, $useRegex,
1433 $column = $this->_columnNames
[$columnIndex];
1435 $result = $this->_getRegexReplaceRows(
1436 $columnIndex, $find, $replaceWith, $charSet
1439 $sql_query = "SELECT "
1440 . PMA_Util
::backquote($column) . ","
1442 . PMA_Util
::backquote($column) . ", '" . $find . "', '"
1446 . " FROM " . PMA_Util
::backquote($this->_db
)
1447 . "." . PMA_Util
::backquote($this->_table
)
1448 . " WHERE " . PMA_Util
::backquote($column)
1449 . " LIKE '%" . $find . "%' COLLATE " . $charSet . "_bin"; // here we
1450 // change the collation of the 2nd operand to a case sensitive
1451 // binary collation to make sure that the comparison
1452 // is case sensitive
1453 $sql_query .= " GROUP BY " . PMA_Util
::backquote($column)
1454 . " ORDER BY " . PMA_Util
::backquote($column) . " ASC";
1456 $result = $GLOBALS['dbi']->fetchResult($sql_query, 0);
1459 $htmlOutput = '<form method="post" action="tbl_find_replace.php"'
1460 . ' name="previewForm" id="previewForm" class="ajax">';
1461 $htmlOutput .= PMA_URL_getHiddenInputs($this->_db
, $this->_table
);
1462 $htmlOutput .= '<input type="hidden" name="replace" value="true" />';
1463 $htmlOutput .= '<input type="hidden" name="columnIndex" value="'
1464 . $columnIndex . '" />';
1465 $htmlOutput .= '<input type="hidden" name="findString"'
1466 . ' value="' . htmlspecialchars($find) . '" />';
1467 $htmlOutput .= '<input type="hidden" name="replaceWith"'
1468 . ' value="' . htmlspecialchars($replaceWith) . '" />';
1469 $htmlOutput .= '<input type="hidden" name="useRegex"'
1470 . ' value="' . $useRegex . '" />';
1472 $htmlOutput .= '<fieldset id="fieldset_find_replace_preview">';
1473 $htmlOutput .= '<legend>' . __('Find and replace - preview') . '</legend>';
1475 $htmlOutput .= '<table id="previewTable">'
1477 . '<th>' . __('Count') . '</th>'
1478 . '<th>' . __('Original string') . '</th>'
1479 . '<th>' . __('Replaced string') . '</th>'
1482 $htmlOutput .= '<tbody>';
1484 if (is_array($result)) {
1485 foreach ($result as $row) {
1487 $replaced = $row[1];
1490 $htmlOutput .= '<tr class="' . ($odd ?
'odd' : 'even') . '">';
1491 $htmlOutput .= '<td class="right">' . htmlspecialchars($count)
1493 $htmlOutput .= '<td>' . htmlspecialchars($val) . '</td>';
1494 $htmlOutput .= '<td>' . htmlspecialchars($replaced) . '</td>';
1495 $htmlOutput .= '</tr>';
1500 $htmlOutput .= '</tbody>';
1501 $htmlOutput .= '</table>';
1502 $htmlOutput .= '</fieldset>';
1504 $htmlOutput .= '<fieldset class="tblFooters">';
1505 $htmlOutput .= '<input type="submit" name="replace"'
1506 . ' value="' . __('Replace') . '" />';
1507 $htmlOutput .= '</fieldset>';
1509 $htmlOutput .= '</form>';
1514 * Replaces a given string in a column with a give replacement
1516 * @param int $columnIndex index of the column
1517 * @param string $find string to find in the column
1518 * @param string $replaceWith string to replace with
1519 * @param boolean $useRegex to use Regex replace or not
1520 * @param string $charSet character set of the connection
1524 function replace($columnIndex, $find, $replaceWith, $useRegex, $charSet)
1526 $column = $this->_columnNames
[$columnIndex];
1528 $toReplace = $this->_getRegexReplaceRows(
1529 $columnIndex, $find, $replaceWith, $charSet
1531 $sql_query = "UPDATE " . PMA_Util
::backquote($this->_db
)
1532 . "." . PMA_Util
::backquote($this->_table
)
1533 . " SET " . PMA_Util
::backquote($column) . " = CASE";
1534 if (is_array($toReplace)) {
1535 foreach ($toReplace as $row) {
1536 $sql_query .= "\n WHEN " . PMA_Util
::backquote($column)
1537 . " = '" . PMA_Util
::sqlAddSlashes($row[0])
1538 . "' THEN '" . PMA_Util
::sqlAddSlashes($row[1]) . "'";
1541 $sql_query .= " END"
1542 . " WHERE " . PMA_Util
::backquote($column)
1543 . " RLIKE '" . PMA_Util
::sqlAddSlashes($find) . "' COLLATE "
1544 . $charSet . "_bin"; // here we
1545 // change the collation of the 2nd operand to a case sensitive
1546 // binary collation to make sure that the comparison
1547 // is case sensitive
1549 $sql_query = "UPDATE " . PMA_Util
::backquote($this->_db
)
1550 . "." . PMA_Util
::backquote($this->_table
)
1551 . " SET " . PMA_Util
::backquote($column) . " ="
1553 . PMA_Util
::backquote($column) . ", '" . $find . "', '"
1556 . " WHERE " . PMA_Util
::backquote($column)
1557 . " LIKE '%" . $find . "%' COLLATE " . $charSet . "_bin"; // here we
1558 // change the collation of the 2nd operand to a case sensitive
1559 // binary collation to make sure that the comparison
1560 // is case sensitive
1562 $GLOBALS['dbi']->query(
1563 $sql_query, null, PMA_DatabaseInterface
::QUERY_STORE
1565 $GLOBALS['sql_query'] = $sql_query;
1569 * Finds minimum and maximum value of a given column.
1571 * @param string $column Column name
1575 public function getColumnMinMax($column)
1577 $sql_query = 'SELECT MIN(' . PMA_Util
::backquote($column) . ') AS `min`, '
1578 . 'MAX(' . PMA_Util
::backquote($column) . ') AS `max` '
1579 . 'FROM ' . PMA_Util
::backquote($this->_db
) . '.'
1580 . PMA_Util
::backquote($this->_table
);
1582 $result = $GLOBALS['dbi']->fetchSingleRow($sql_query);