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?
8 * @uses $cfg['UseDbSearch']
10 * @uses PMA_DBI_get_tables()
11 * @uses PMA_sqlAddslashes()
12 * @uses PMA_getSearchSqls()
13 * @uses PMA_DBI_fetch_value()
14 * @uses PMA_linkOrButton()
15 * @uses PMA_generate_common_url()
16 * @uses PMA_generate_common_hidden_inputs()
17 * @uses PMA_showMySQLDocu()
18 * @uses $_REQUEST['search_str']
19 * @uses $_REQUEST['submit_search']
20 * @uses $_REQUEST['search_option']
21 * @uses $_REQUEST['table_select']
22 * @uses $_REQUEST['unselectall']
23 * @uses $_REQUEST['selectall']
24 * @uses $_REQUEST['field_str']
26 * @uses htmlspecialchars()
27 * @uses array_key_exists()
29 * @uses array_intersect()
38 require_once './libraries/common.inc.php';
40 $GLOBALS['js_include'][] = 'db_search.js';
43 * Gets some core libraries and send headers
45 require './libraries/db_common.inc.php';
50 // If config variable $GLOBALS['cfg']['Usedbsearch'] is on false : exit.
51 if (! $GLOBALS['cfg']['UseDbSearch']) {
52 PMA_mysqlDie(__('Access denied'), '', false, $err_url);
54 $url_query .= '&goto=db_search.php';
55 $url_params['goto'] = 'db_search.php';
58 * @global array list of tables from the current database
59 * but do not clash with $tables coming from db_info.inc.php
61 $tables_names_only = PMA_DBI_get_tables($GLOBALS['db']);
63 $search_options = array(
64 '1' => __('at least one of the words'),
65 '2' => __('all words'),
66 '3' => __('the exact phrase'),
67 '4' => __('as regular expression'),
70 if (empty($_REQUEST['search_option']) ||
! is_string($_REQUEST['search_option'])
71 ||
! array_key_exists($_REQUEST['search_option'], $search_options)) {
73 unset($_REQUEST['submit_search']);
75 $search_option = (int) $_REQUEST['search_option'];
76 $option_str = $search_options[$_REQUEST['search_option']];
79 if (empty($_REQUEST['search_str']) ||
! is_string($_REQUEST['search_str'])) {
80 unset($_REQUEST['submit_search']);
83 $searched = htmlspecialchars($_REQUEST['search_str']);
84 // For "as regular expression" (search option 4), we should not treat
85 // this as an expression that contains a LIKE (second parameter of
86 // PMA_sqlAddslashes()).
88 // Usage example: If user is seaching for a literal $ in a regexp search,
89 // he should enter \$ as the value.
90 $search_str = PMA_sqlAddslashes($_REQUEST['search_str'], ($search_option == 4 ?
false : true));
93 $tables_selected = array();
94 if (empty($_REQUEST['table_select']) ||
! is_array($_REQUEST['table_select'])) {
95 unset($_REQUEST['submit_search']);
96 } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) {
97 $tables_selected = array_intersect($_REQUEST['table_select'], $tables_names_only);
100 if (isset($_REQUEST['selectall'])) {
101 $tables_selected = $tables_names_only;
102 } elseif (isset($_REQUEST['unselectall'])) {
103 $tables_selected = array();
106 if (empty($_REQUEST['field_str']) ||
! is_string($_REQUEST['field_str'])) {
109 $field_str = PMA_sqlAddslashes($_REQUEST['field_str'], true);
113 * Displays top links if we are not in an Ajax request
117 if( $GLOBALS['is_ajax_request'] != true) {
118 require './libraries/db_info.inc.php';
119 echo '<div id="searchresults">';
123 * 1. Main search form has been submitted
125 if (isset($_REQUEST['submit_search'])) {
128 * Builds the SQL search query
130 * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this?
131 * @uses PMA_DBI_query
133 * PMA_DBI_free_result
134 * PMA_DBI_fetch_assoc
139 * @param string the table name
140 * @param string restrict the search to this field
141 * @param string the string to search
142 * @param integer type of search (1 -> 1 word at least, 2 -> all words,
143 * 3 -> exact string, 4 -> regexp)
145 * @return array 3 SQL querys (for count, display and delete results)
147 * @global string the url to return to in case of errors
148 * @global string charset connection
150 function PMA_getSearchSqls($table, $field, $search_str, $search_option)
155 $sqlstr_select = 'SELECT';
156 $sqlstr_delete = 'DELETE';
159 $tblfields = PMA_DBI_fetch_result('SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']),
163 $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
165 $search_words = (($search_option > 2) ?
array($search_str) : explode(' ', $search_str));
166 $search_wds_cnt = count($search_words);
168 $like_or_regex = (($search_option == 4) ?
'REGEXP' : 'LIKE');
169 $automatic_wildcard = (($search_option < 3) ?
'%' : '');
171 $fieldslikevalues = array();
172 foreach ($search_words as $search_word) {
173 // Eliminates empty values
174 if (strlen($search_word) === 0) {
178 $thefieldlikevalue = array();
179 foreach ($tblfields as $tblfield) {
180 if (! isset($field) ||
strlen($field) == 0 ||
$tblfield == $field) {
181 $thefieldlikevalue[] = 'CONVERT(' . PMA_backquote($tblfield) . ' USING utf8)'
182 . ' ' . $like_or_regex . ' '
183 . "'" . $automatic_wildcard
185 . $automatic_wildcard . "'";
189 if (count($thefieldlikevalue) > 0) {
190 $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
194 $implode_str = ($search_option == 1 ?
' OR ' : ' AND ');
195 if ( empty($fieldslikevalues)) {
196 // this could happen when the "inside field" does not exist
197 // in any selected tables
198 $sqlstr_where = ' WHERE FALSE';
200 $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
202 unset($fieldslikevalues);
204 // Builds complete queries
205 $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
206 // here, I think we need to still use the COUNT clause, even for
207 // VIEWs, anyway we have a WHERE clause that should limit results
208 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
209 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
212 } // end of the "PMA_getSearchSqls()" function
216 * Displays the results
218 $this_url_params = array(
219 'db' => $GLOBALS['db'],
220 'goto' => 'db_sql.php',
222 'is_js_confirmed' => 0,
225 // Displays search string
227 .'<table class="data">' . "\n"
228 .'<caption class="tblHeaders">' . "\n"
229 .sprintf(__('Search results for "<i>%s</i>" %s:'),
230 $searched, $option_str) . "\n"
231 .'</caption>' . "\n";
233 $num_search_result_total = 0;
236 foreach ($tables_selected as $each_table) {
237 // Gets the SQL statements
238 $newsearchsqls = PMA_getSearchSqls($each_table, (! empty($field_str) ?
$field_str : ''), $search_str, $search_option);
240 // Executes the "COUNT" statement
241 $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
242 $num_search_result_total +
= $res_cnt;
244 $sql_query .= $newsearchsqls['select_count'];
246 echo '<tr class="noclick ' . ($odd_row ?
'odd' : 'even') . '">'
247 .'<td>' . sprintf(_ngettext('%s match inside table <i>%s</i>', '%s matches inside table <i>%s</i>', $res_cnt), $res_cnt,
248 htmlspecialchars($each_table)) . "</td>\n";
251 $this_url_params['sql_query'] = $newsearchsqls['select_fields'];
252 $browse_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
254 <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
>
256 $this_url_params['sql_query'] = $newsearchsqls['delete'];
257 $delete_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
259 <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
>
262 echo '<td> </td>' . "\n"
263 .'<td> </td>' . "\n";
265 $odd_row = ! $odd_row;
269 echo '</table>' . "\n";
271 if (count($tables_selected) > 1) {
272 echo '<p>' . sprintf(_ngettext('<b>Total:</b> <i>%s</i> match', '<b>Total:</b> <i>%s</i> matches', $num_search_result_total),
273 $num_search_result_total) . '</p>' . "\n";
278 * If we are in an Ajax request, we need to exit after displaying all the HTML
280 if($GLOBALS['is_ajax_request'] == true) {
284 echo '</div>';//end searchresults div
288 * 2. Displays the main search form
291 <a name
="db_search"></a
>
292 <form id
="db_search_form"<?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax"' : ''); ?
> method
="post" action
="db_search.php" name
="db_search">
293 <?php
echo PMA_generate_common_hidden_inputs($GLOBALS['db']); ?
>
295 <legend
><?php
echo __('Search in database'); ?
></legend
>
297 <table
class="formlayout">
298 <tr
><td
><?php
echo __('Word(s) or value(s) to search for (wildcard: "%"):'); ?
></td
>
299 <td
><input type
="text" name
="search_str" size
="60"
300 value
="<?php echo $searched; ?>" /></td
>
302 <tr
><td align
="right" valign
="top">
303 <?php
echo __('Find:'); ?
></td
>
307 '1' => __('at least one of the words') . PMA_showHint(__('Words are separated by a space character (" ").')),
308 '2' => __('all words') . PMA_showHint(__('Words are separated by a space character (" ").')),
309 '3' => __('the exact phrase'),
310 '4' => __('as regular expression') . ' ' . PMA_showMySQLDocu('Regexp', 'Regexp')
312 // 4th parameter set to true to add line breaks
313 // 5th parameter set to false to avoid htmlspecialchars() escaping in the label
314 // since we have some HTML in some labels
315 PMA_display_html_radio('search_option', $choices, $search_option, true, false);
320 <tr
><td align
="right" valign
="top">
321 <?php
echo __('Inside table(s):'); ?
></td
>
324 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
325 foreach ($tables_names_only as $each_table) {
326 if (in_array($each_table, $tables_selected)) {
327 $is_selected = ' selected="selected"';
332 echo ' <option value="' . htmlspecialchars($each_table) . '"'
334 . str_replace(' ', ' ', htmlspecialchars($each_table)) . '</option>' . "\n";
337 echo ' </select>' . "\n";
339 '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('selectall' => 1))) . '#db_search"'
340 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', true); return false;">' . __('Select All') . '</a>'
342 . '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('unselectall' => 1))) . '#db_search"'
343 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', false); return false;">' . __('Unselect All') . '</a>';
347 <tr
><td align
="right" valign
="bottom">
348 <?php
echo $alter_select; ?
></td
>
350 <tr
><td align
="right">
351 <?php
echo __('Inside column:'); ?
></td
>
352 <td
><input type
="text" name
="field_str" size
="60"
353 value
="<?php echo ! empty($field_str) ? htmlspecialchars($field_str) : ''; ?>" /></td
>
357 <fieldset
class="tblFooters">
358 <input type
="submit" name
="submit_search" value
="<?php echo __('Go'); ?>"
363 <!-- These two table
-image
and table
-link elements display the table name in browse search results
-->
364 <div id
='table-info'>
365 <a
class="item" id
="table-link" ></a
>
367 <div id
="browse-results">
368 <!-- this browse
-results div is used to load the browse
and delete results in the db search
-->
370 <br
class="clearfloat" />
371 <div id
="sqlqueryform">
372 <!-- this sqlqueryform div is used to load the delete form in the db search
-->
374 <!-- toggle query box link
-->
375 <a id
="togglequerybox"></a
>
379 * Displays the footer
381 require './libraries/footer.inc.php';