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';
41 $GLOBALS['js_include'][] = 'sql.js';
42 $GLOBALS['js_include'][] = 'makegrid.js';
45 * Gets some core libraries and send headers
47 require './libraries/db_common.inc.php';
52 // If config variable $GLOBALS['cfg']['Usedbsearch'] is on false : exit.
53 if (! $GLOBALS['cfg']['UseDbSearch']) {
54 PMA_mysqlDie(__('Access denied'), '', false, $err_url);
56 $url_query .= '&goto=db_search.php';
57 $url_params['goto'] = 'db_search.php';
60 * @global array list of tables from the current database
61 * but do not clash with $tables coming from db_info.inc.php
63 $tables_names_only = PMA_DBI_get_tables($GLOBALS['db']);
65 $search_options = array(
66 '1' => __('at least one of the words'),
67 '2' => __('all words'),
68 '3' => __('the exact phrase'),
69 '4' => __('as regular expression'),
72 if (empty($_REQUEST['search_option']) ||
! is_string($_REQUEST['search_option'])
73 ||
! array_key_exists($_REQUEST['search_option'], $search_options)) {
75 unset($_REQUEST['submit_search']);
77 $search_option = (int) $_REQUEST['search_option'];
78 $option_str = $search_options[$_REQUEST['search_option']];
81 if (empty($_REQUEST['search_str']) ||
! is_string($_REQUEST['search_str'])) {
82 unset($_REQUEST['submit_search']);
85 $searched = htmlspecialchars($_REQUEST['search_str']);
86 // For "as regular expression" (search option 4), we should not treat
87 // this as an expression that contains a LIKE (second parameter of
88 // PMA_sqlAddslashes()).
90 // Usage example: If user is seaching for a literal $ in a regexp search,
91 // he should enter \$ as the value.
92 $search_str = PMA_sqlAddslashes($_REQUEST['search_str'], ($search_option == 4 ?
false : true));
95 $tables_selected = array();
96 if (empty($_REQUEST['table_select']) ||
! is_array($_REQUEST['table_select'])) {
97 unset($_REQUEST['submit_search']);
98 } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) {
99 $tables_selected = array_intersect($_REQUEST['table_select'], $tables_names_only);
102 if (isset($_REQUEST['selectall'])) {
103 $tables_selected = $tables_names_only;
104 } elseif (isset($_REQUEST['unselectall'])) {
105 $tables_selected = array();
108 if (empty($_REQUEST['field_str']) ||
! is_string($_REQUEST['field_str'])) {
111 $field_str = PMA_sqlAddslashes($_REQUEST['field_str'], true);
115 * Displays top links if we are not in an Ajax request
119 if( $GLOBALS['is_ajax_request'] != true) {
120 require './libraries/db_info.inc.php';
121 echo '<div id="searchresults">';
125 * 1. Main search form has been submitted
127 if (isset($_REQUEST['submit_search'])) {
130 * Builds the SQL search query
132 * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this?
133 * @uses PMA_DBI_query
135 * PMA_DBI_free_result
136 * PMA_DBI_fetch_assoc
141 * @param string the table name
142 * @param string restrict the search to this field
143 * @param string the string to search
144 * @param integer type of search (1 -> 1 word at least, 2 -> all words,
145 * 3 -> exact string, 4 -> regexp)
147 * @return array 3 SQL querys (for count, display and delete results)
149 * @global string the url to return to in case of errors
150 * @global string charset connection
152 function PMA_getSearchSqls($table, $field, $search_str, $search_option)
157 $sqlstr_select = 'SELECT';
158 $sqlstr_delete = 'DELETE';
161 $tblfields = PMA_DBI_get_columns($GLOBALS['db'], $table);
164 $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
166 $search_words = (($search_option > 2) ?
array($search_str) : explode(' ', $search_str));
167 $search_wds_cnt = count($search_words);
169 $like_or_regex = (($search_option == 4) ?
'REGEXP' : 'LIKE');
170 $automatic_wildcard = (($search_option < 3) ?
'%' : '');
172 $fieldslikevalues = array();
173 foreach ($search_words as $search_word) {
174 // Eliminates empty values
175 if (strlen($search_word) === 0) {
179 $thefieldlikevalue = array();
180 foreach ($tblfields as $tblfield) {
181 if (! isset($field) ||
strlen($field) == 0 ||
$tblfield['Field'] == $field) {
182 $thefieldlikevalue[] = 'CONVERT(' . PMA_backquote($tblfield['Field']) . ' USING utf8)'
183 . ' ' . $like_or_regex . ' '
184 . "'" . $automatic_wildcard
186 . $automatic_wildcard . "'";
190 if (count($thefieldlikevalue) > 0) {
191 $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
195 $implode_str = ($search_option == 1 ?
' OR ' : ' AND ');
196 if ( empty($fieldslikevalues)) {
197 // this could happen when the "inside field" does not exist
198 // in any selected tables
199 $sqlstr_where = ' WHERE FALSE';
201 $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
203 unset($fieldslikevalues);
205 // Builds complete queries
206 $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
207 // here, I think we need to still use the COUNT clause, even for
208 // VIEWs, anyway we have a WHERE clause that should limit results
209 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
210 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
213 } // end of the "PMA_getSearchSqls()" function
217 * Displays the results
219 $this_url_params = array(
220 'db' => $GLOBALS['db'],
221 'goto' => 'db_sql.php',
223 'is_js_confirmed' => 0,
226 // Displays search string
228 .'<table class="data">' . "\n"
229 .'<caption class="tblHeaders">' . "\n"
230 .sprintf(__('Search results for "<i>%s</i>" %s:'),
231 $searched, $option_str) . "\n"
232 .'</caption>' . "\n";
234 $num_search_result_total = 0;
237 foreach ($tables_selected as $each_table) {
238 // Gets the SQL statements
239 $newsearchsqls = PMA_getSearchSqls($each_table, (! empty($field_str) ?
$field_str : ''), $search_str, $search_option);
241 // Executes the "COUNT" statement
242 $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
243 $num_search_result_total +
= $res_cnt;
245 $sql_query .= $newsearchsqls['select_count'];
247 echo '<tr class="noclick ' . ($odd_row ?
'odd' : 'even') . '">'
248 .'<td>' . sprintf(_ngettext('%s match inside table <i>%s</i>', '%s matches inside table <i>%s</i>', $res_cnt), $res_cnt,
249 htmlspecialchars($each_table)) . "</td>\n";
252 $this_url_params['sql_query'] = $newsearchsqls['select_fields'];
253 $browse_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
255 <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
>
257 $this_url_params['sql_query'] = $newsearchsqls['delete'];
258 $delete_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
260 <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
>
263 echo '<td> </td>' . "\n"
264 .'<td> </td>' . "\n";
266 $odd_row = ! $odd_row;
270 echo '</table>' . "\n";
272 if (count($tables_selected) > 1) {
273 echo '<p>' . sprintf(_ngettext('<b>Total:</b> <i>%s</i> match', '<b>Total:</b> <i>%s</i> matches', $num_search_result_total),
274 $num_search_result_total) . '</p>' . "\n";
279 * If we are in an Ajax request, we need to exit after displaying all the HTML
281 if($GLOBALS['is_ajax_request'] == true) {
285 echo '</div>';//end searchresults div
289 * 2. Displays the main search form
292 <a name
="db_search"></a
>
293 <form id
="db_search_form"<?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax"' : ''); ?
> method
="post" action
="db_search.php" name
="db_search">
294 <?php
echo PMA_generate_common_hidden_inputs($GLOBALS['db']); ?
>
296 <legend
><?php
echo __('Search in database'); ?
></legend
>
298 <table
class="formlayout">
299 <tr
><td
><?php
echo __('Word(s) or value(s) to search for (wildcard: "%"):'); ?
></td
>
300 <td
><input type
="text" name
="search_str" size
="60"
301 value
="<?php echo $searched; ?>" /></td
>
303 <tr
><td align
="right" valign
="top">
304 <?php
echo __('Find:'); ?
></td
>
308 '1' => __('at least one of the words') . PMA_showHint(__('Words are separated by a space character (" ").')),
309 '2' => __('all words') . PMA_showHint(__('Words are separated by a space character (" ").')),
310 '3' => __('the exact phrase'),
311 '4' => __('as regular expression') . ' ' . PMA_showMySQLDocu('Regexp', 'Regexp')
313 // 4th parameter set to true to add line breaks
314 // 5th parameter set to false to avoid htmlspecialchars() escaping in the label
315 // since we have some HTML in some labels
316 PMA_display_html_radio('search_option', $choices, $search_option, true, false);
321 <tr
><td align
="right" valign
="top">
322 <?php
echo __('Inside table(s):'); ?
></td
>
325 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
326 foreach ($tables_names_only as $each_table) {
327 if (in_array($each_table, $tables_selected)) {
328 $is_selected = ' selected="selected"';
333 echo ' <option value="' . htmlspecialchars($each_table) . '"'
335 . str_replace(' ', ' ', htmlspecialchars($each_table)) . '</option>' . "\n";
338 echo ' </select>' . "\n";
340 '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('selectall' => 1))) . '#db_search"'
341 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', true); return false;">' . __('Select All') . '</a>'
343 . '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('unselectall' => 1))) . '#db_search"'
344 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', false); return false;">' . __('Unselect All') . '</a>';
348 <tr
><td align
="right" valign
="bottom">
349 <?php
echo $alter_select; ?
></td
>
351 <tr
><td align
="right">
352 <?php
echo __('Inside column:'); ?
></td
>
353 <td
><input type
="text" name
="field_str" size
="60"
354 value
="<?php echo ! empty($field_str) ? htmlspecialchars($field_str) : ''; ?>" /></td
>
358 <fieldset
class="tblFooters">
359 <input type
="submit" name
="submit_search" value
="<?php echo __('Go'); ?>"
364 <!-- These two table
-image
and table
-link elements display the table name in browse search results
-->
365 <div id
='table-info'>
366 <a
class="item" id
="table-link" ></a
>
368 <div id
="browse-results">
369 <!-- this browse
-results div is used to load the browse
and delete results in the db search
-->
371 <br
class="clearfloat" />
372 <div id
="sqlqueryform">
373 <!-- this sqlqueryform div is used to load the delete form in the db search
-->
375 <!-- toggle query box link
-->
376 <a id
="togglequerybox"></a
>
380 * Displays the footer
382 require './libraries/footer.inc.php';