2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * searchs the entire database
6 * @todo make use of UNION when searching multiple tables
7 * @todo display executed query, optional?
14 require_once 'libraries/common.inc.php';
16 $GLOBALS['js_include'][] = 'db_search.js';
17 $GLOBALS['js_include'][] = 'sql.js';
18 $GLOBALS['js_include'][] = 'makegrid.js';
19 $GLOBALS['js_include'][] = 'jquery/timepicker.js';
22 * Gets some core libraries and send headers
24 require 'libraries/db_common.inc.php';
29 // If config variable $GLOBALS['cfg']['Usedbsearch'] is on false : exit.
30 if (! $GLOBALS['cfg']['UseDbSearch']) {
31 PMA_mysqlDie(__('Access denied'), '', false, $err_url);
33 $url_query .= '&goto=db_search.php';
34 $url_params['goto'] = 'db_search.php';
37 * @global array list of tables from the current database
38 * but do not clash with $tables coming from db_info.inc.php
40 $tables_names_only = PMA_DBI_get_tables($GLOBALS['db']);
42 $search_options = array(
43 '1' => __('at least one of the words'),
44 '2' => __('all words'),
45 '3' => __('the exact phrase'),
46 '4' => __('as regular expression'),
49 if (empty($_REQUEST['search_option'])
50 ||
! is_string($_REQUEST['search_option'])
51 ||
! array_key_exists($_REQUEST['search_option'], $search_options)
54 unset($_REQUEST['submit_search']);
56 $search_option = (int) $_REQUEST['search_option'];
57 $option_str = $search_options[$_REQUEST['search_option']];
60 if (empty($_REQUEST['search_str']) ||
! is_string($_REQUEST['search_str'])) {
61 unset($_REQUEST['submit_search']);
64 $searched = htmlspecialchars($_REQUEST['search_str']);
65 // For "as regular expression" (search option 4), we should not treat
66 // this as an expression that contains a LIKE (second parameter of
67 // PMA_sqlAddSlashes()).
69 // Usage example: If user is seaching for a literal $ in a regexp search,
70 // he should enter \$ as the value.
71 $search_str = PMA_sqlAddSlashes(
72 $_REQUEST['search_str'], ($search_option == 4 ?
false : true)
76 $tables_selected = array();
77 if (empty($_REQUEST['table_select']) ||
! is_array($_REQUEST['table_select'])) {
78 unset($_REQUEST['submit_search']);
79 } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) {
80 $tables_selected = array_intersect($_REQUEST['table_select'], $tables_names_only);
83 if (isset($_REQUEST['selectall'])) {
84 $tables_selected = $tables_names_only;
85 } elseif (isset($_REQUEST['unselectall'])) {
86 $tables_selected = array();
89 if (empty($_REQUEST['field_str']) ||
! is_string($_REQUEST['field_str'])) {
92 $field_str = PMA_sqlAddSlashes($_REQUEST['field_str'], true);
96 * Displays top links if we are not in an Ajax request
100 if ( $GLOBALS['is_ajax_request'] != true) {
101 include 'libraries/db_info.inc.php';
102 echo '<div id="searchresults">';
106 * 1. Main search form has been submitted
108 if (isset($_REQUEST['submit_search'])) {
111 * Builds the SQL search query
113 * @param string $table the table name
114 * @param string $field restrict the search to this field
115 * @param string $search_str the string to search
116 * @param integer $search_option type of search
117 * (1 -> 1 word at least, 2 -> all words,
118 * 3 -> exact string, 4 -> regexp)
120 * @return array 3 SQL querys (for count, display and delete results)
122 * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this?
124 * PMA_DBI_free_result
125 * PMA_DBI_fetch_assoc
131 function PMA_getSearchSqls($table, $field, $search_str, $search_option)
134 $sqlstr_select = 'SELECT';
135 $sqlstr_delete = 'DELETE';
138 $tblfields = PMA_DBI_get_columns($GLOBALS['db'], $table);
141 $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
143 $search_words = (($search_option > 2) ?
array($search_str) : explode(' ', $search_str));
145 $like_or_regex = (($search_option == 4) ?
'REGEXP' : 'LIKE');
146 $automatic_wildcard = (($search_option < 3) ?
'%' : '');
148 $fieldslikevalues = array();
149 foreach ($search_words as $search_word) {
150 // Eliminates empty values
151 if (strlen($search_word) === 0) {
155 $thefieldlikevalue = array();
156 foreach ($tblfields as $tblfield) {
157 if (! isset($field) ||
strlen($field) == 0 ||
$tblfield['Field'] == $field) {
158 // Drizzle has no CONVERT and all text columns are UTF-8
160 $thefieldlikevalue[] = PMA_backquote($tblfield['Field'])
161 . ' ' . $like_or_regex . ' '
162 . "'" . $automatic_wildcard
164 . $automatic_wildcard . "'";
166 $thefieldlikevalue[] = 'CONVERT(' . PMA_backquote($tblfield['Field']) . ' USING utf8)'
167 . ' ' . $like_or_regex . ' '
168 . "'" . $automatic_wildcard
170 . $automatic_wildcard . "'";
175 if (count($thefieldlikevalue) > 0) {
176 $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
180 $implode_str = ($search_option == 1 ?
' OR ' : ' AND ');
181 if ( empty($fieldslikevalues)) {
182 // this could happen when the "inside field" does not exist
183 // in any selected tables
184 $sqlstr_where = ' WHERE FALSE';
186 $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
188 unset($fieldslikevalues);
190 // Builds complete queries
191 $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
192 // here, I think we need to still use the COUNT clause, even for
193 // VIEWs, anyway we have a WHERE clause that should limit results
194 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
195 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
198 } // end of the "PMA_getSearchSqls()" function
202 * Displays the results
204 $this_url_params = array(
205 'db' => $GLOBALS['db'],
206 'goto' => 'db_sql.php',
208 'is_js_confirmed' => 0,
211 // Displays search string
213 . '<table class="data">' . "\n"
214 . '<caption class="tblHeaders">' . "\n"
216 __('Search results for "<i>%s</i>" %s:'),
217 $searched, $option_str
219 . '</caption>' . "\n";
221 $num_search_result_total = 0;
224 foreach ($tables_selected as $each_table) {
225 // Gets the SQL statements
226 $newsearchsqls = PMA_getSearchSqls(
227 $each_table, (! empty($field_str) ?
$field_str : ''),
228 $search_str, $search_option
231 // Executes the "COUNT" statement
232 $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
233 $num_search_result_total +
= $res_cnt;
235 $sql_query .= $newsearchsqls['select_count'];
237 echo '<tr class="noclick ' . ($odd_row ?
'odd' : 'even') . '">'
239 _ngettext('%1$s match inside table <i>%2$s</i>', '%1$s matches inside table <i>%2$s</i>', $res_cnt),
240 $res_cnt, htmlspecialchars($each_table)
244 $this_url_params['sql_query'] = $newsearchsqls['select_fields'];
245 $browse_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
247 <td
> <a name
="browse_search" href
="<?php echo $browse_result_path; ?>" onclick
="loadResult('<?php echo $browse_result_path ?> ',' <?php echo $each_table?> ' , '<?php echo PMA_generate_common_url($GLOBALS['db'], $each_table)?>','<?php echo ($GLOBALS['cfg']['AjaxEnable']); ?>');return false;" ><?php
echo __('Browse') ?
></a
> </td
>
249 $this_url_params['sql_query'] = $newsearchsqls['delete'];
250 $delete_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
252 <td
> <a name
="delete_search" href
="<?php echo $delete_result_path; ?>" onclick
="deleteResult('<?php echo $delete_result_path ?>' , ' <?php printf(__('Delete the matches for the %s table?'), htmlspecialchars($each_table)); ?>','<?php echo ($GLOBALS['cfg']['AjaxEnable']); ?>');return false;" ><?php
echo __('Delete') ?
></a
> </td
>
255 echo '<td> </td>' . "\n"
256 .'<td> </td>' . "\n";
258 $odd_row = ! $odd_row;
262 echo '</table>' . "\n";
264 if (count($tables_selected) > 1) {
265 echo '<p>' . sprintf(
266 _ngettext('<b>Total:</b> <i>%s</i> match', '<b>Total:</b> <i>%s</i> matches', $num_search_result_total),
267 $num_search_result_total
273 * If we are in an Ajax request, we need to exit after displaying all the HTML
275 if ($GLOBALS['is_ajax_request'] == true) {
278 echo '</div>';//end searchresults div
282 * 2. Displays the main search form
285 <a id
="db_search"></a
>
286 <form id
="db_search_form"<?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax"' : ''); ?
> method
="post" action
="db_search.php" name
="db_search">
287 <?php
echo PMA_generate_common_hidden_inputs($GLOBALS['db']); ?
>
289 <legend
><?php
echo __('Search in database'); ?
></legend
>
291 <table
class="formlayout">
292 <tr
><td
><?php
echo __('Words or values to search for (wildcard: "%"):'); ?
></td
>
293 <td
><input type
="text" name
="search_str" size
="60"
294 value
="<?php echo $searched; ?>" /></td
>
296 <tr
><td
class="right vtop">
297 <?php
echo __('Find:'); ?
></td
>
301 '1' => __('at least one of the words') . PMA_showHint(__('Words are separated by a space character (" ").')),
302 '2' => __('all words') . PMA_showHint(__('Words are separated by a space character (" ").')),
303 '3' => __('the exact phrase'),
304 '4' => __('as regular expression') . ' ' . PMA_showMySQLDocu('Regexp', 'Regexp')
306 // 4th parameter set to true to add line breaks
307 // 5th parameter set to false to avoid htmlspecialchars() escaping in the label
308 // since we have some HTML in some labels
309 PMA_display_html_radio('search_option', $choices, $search_option, true, false);
314 <tr
><td
class="right vtop">
315 <?php
echo __('Inside tables:'); ?
></td
>
318 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
319 foreach ($tables_names_only as $each_table) {
320 if (in_array($each_table, $tables_selected)) {
321 $is_selected = ' selected="selected"';
326 echo ' <option value="' . htmlspecialchars($each_table) . '"'
328 . str_replace(' ', ' ', htmlspecialchars($each_table)) . '</option>' . "\n";
331 echo ' </select>' . "\n";
333 = '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('selectall' => 1))) . '#db_search"'
334 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', true); return false;">' . __('Select All') . '</a>'
336 . '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('unselectall' => 1))) . '#db_search"'
337 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', false); return false;">' . __('Unselect All') . '</a>';
341 <tr
><td
class="right vbottom">
342 <?php
echo $alter_select; ?
></td
>
344 <tr
><td
class="right">
345 <?php
echo __('Inside column:'); ?
></td
>
346 <td
><input type
="text" name
="field_str" size
="60"
347 value
="<?php echo ! empty($field_str) ? htmlspecialchars($field_str) : ''; ?>" /></td
>
351 <fieldset
class="tblFooters">
352 <input type
="submit" name
="submit_search" value
="<?php echo __('Go'); ?>"
357 <!-- These two table
-image
and table
-link elements display the table name in browse search results
-->
358 <div id
='table-info'>
359 <a
class="item" id
="table-link" ></a
>
361 <div id
="browse-results">
362 <!-- this browse
-results div is used to load the browse
and delete results in the db search
-->
364 <br
class="clearfloat" />
365 <div id
="sqlqueryform">
366 <!-- this sqlqueryform div is used to load the delete form in the db search
-->
368 <!-- toggle query box link
-->
369 <a id
="togglequerybox"></a
>
373 * Displays the footer
375 require 'libraries/footer.inc.php';