Translation update done using Pootle.
[phpmyadmin.git] / db_search.php
blob041242f22299d1da2996042a46f2bfd0a1190238
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * searchs the entire database
6 * @todo make use of UNION when searching multiple tables
7 * @todo display executed query, optional?
8 * @package PhpMyAdmin
9 */
11 /**
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';
21 /**
22 * Gets some core libraries and send headers
24 require './libraries/db_common.inc.php';
26 /**
27 * init
29 // If config variable $GLOBALS['cfg']['Usedbsearch'] is on false : exit.
30 if (! $GLOBALS['cfg']['UseDbSearch']) {
31 PMA_mysqlDie(__('Access denied'), '', false, $err_url);
32 } // end if
33 $url_query .= '&amp;goto=db_search.php';
34 $url_params['goto'] = 'db_search.php';
36 /**
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']) || ! is_string($_REQUEST['search_option'])
50 || ! array_key_exists($_REQUEST['search_option'], $search_options)) {
51 $search_option = 1;
52 unset($_REQUEST['submit_search']);
53 } else {
54 $search_option = (int) $_REQUEST['search_option'];
55 $option_str = $search_options[$_REQUEST['search_option']];
58 if (empty($_REQUEST['search_str']) || ! is_string($_REQUEST['search_str'])) {
59 unset($_REQUEST['submit_search']);
60 $searched = '';
61 } else {
62 $searched = htmlspecialchars($_REQUEST['search_str']);
63 // For "as regular expression" (search option 4), we should not treat
64 // this as an expression that contains a LIKE (second parameter of
65 // PMA_sqlAddSlashes()).
67 // Usage example: If user is seaching for a literal $ in a regexp search,
68 // he should enter \$ as the value.
69 $search_str = PMA_sqlAddSlashes($_REQUEST['search_str'], ($search_option == 4 ? false : true));
72 $tables_selected = array();
73 if (empty($_REQUEST['table_select']) || ! is_array($_REQUEST['table_select'])) {
74 unset($_REQUEST['submit_search']);
75 } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) {
76 $tables_selected = array_intersect($_REQUEST['table_select'], $tables_names_only);
79 if (isset($_REQUEST['selectall'])) {
80 $tables_selected = $tables_names_only;
81 } elseif (isset($_REQUEST['unselectall'])) {
82 $tables_selected = array();
85 if (empty($_REQUEST['field_str']) || ! is_string($_REQUEST['field_str'])) {
86 unset($field_str);
87 } else {
88 $field_str = PMA_sqlAddSlashes($_REQUEST['field_str'], true);
91 /**
92 * Displays top links if we are not in an Ajax request
94 $sub_part = '';
96 if ( $GLOBALS['is_ajax_request'] != true) {
97 include './libraries/db_info.inc.php';
98 echo '<div id="searchresults">';
102 * 1. Main search form has been submitted
104 if (isset($_REQUEST['submit_search'])) {
107 * Builds the SQL search query
109 * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this?
110 * PMA_backquote
111 * PMA_DBI_free_result
112 * PMA_DBI_fetch_assoc
113 * $GLOBALS['db']
114 * explode
115 * count
116 * strlen
117 * @param string the table name
118 * @param string restrict the search to this field
119 * @param string the string to search
120 * @param integer type of search (1 -> 1 word at least, 2 -> all words,
121 * 3 -> exact string, 4 -> regexp)
123 * @return array 3 SQL querys (for count, display and delete results)
125 function PMA_getSearchSqls($table, $field, $search_str, $search_option)
127 // Statement types
128 $sqlstr_select = 'SELECT';
129 $sqlstr_delete = 'DELETE';
131 // Fields to select
132 $tblfields = PMA_DBI_get_columns($GLOBALS['db'], $table);
134 // Table to use
135 $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table);
137 $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str));
139 $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE');
140 $automatic_wildcard = (($search_option < 3) ? '%' : '');
142 $fieldslikevalues = array();
143 foreach ($search_words as $search_word) {
144 // Eliminates empty values
145 if (strlen($search_word) === 0) {
146 continue;
149 $thefieldlikevalue = array();
150 foreach ($tblfields as $tblfield) {
151 if (! isset($field) || strlen($field) == 0 || $tblfield['Field'] == $field) {
152 // Drizzle has no CONVERT and all text columns are UTF-8
153 if (PMA_DRIZZLE) {
154 $thefieldlikevalue[] = PMA_backquote($tblfield['Field'])
155 . ' ' . $like_or_regex . ' '
156 . "'" . $automatic_wildcard
157 . $search_word
158 . $automatic_wildcard . "'";
159 } else {
160 $thefieldlikevalue[] = 'CONVERT(' . PMA_backquote($tblfield['Field']) . ' USING utf8)'
161 . ' ' . $like_or_regex . ' '
162 . "'" . $automatic_wildcard
163 . $search_word
164 . $automatic_wildcard . "'";
167 } // end for
169 if (count($thefieldlikevalue) > 0) {
170 $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
172 } // end for
174 $implode_str = ($search_option == 1 ? ' OR ' : ' AND ');
175 if ( empty($fieldslikevalues)) {
176 // this could happen when the "inside field" does not exist
177 // in any selected tables
178 $sqlstr_where = ' WHERE FALSE';
179 } else {
180 $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
182 unset($fieldslikevalues);
184 // Builds complete queries
185 $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
186 // here, I think we need to still use the COUNT clause, even for
187 // VIEWs, anyway we have a WHERE clause that should limit results
188 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
189 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
191 return $sql;
192 } // end of the "PMA_getSearchSqls()" function
196 * Displays the results
198 $this_url_params = array(
199 'db' => $GLOBALS['db'],
200 'goto' => 'db_sql.php',
201 'pos' => 0,
202 'is_js_confirmed' => 0,
205 // Displays search string
206 echo '<br />' . "\n"
207 .'<table class="data">' . "\n"
208 .'<caption class="tblHeaders">' . "\n"
209 .sprintf(__('Search results for "<i>%s</i>" %s:'),
210 $searched, $option_str) . "\n"
211 .'</caption>' . "\n";
213 $num_search_result_total = 0;
214 $odd_row = true;
216 foreach ($tables_selected as $each_table) {
217 // Gets the SQL statements
218 $newsearchsqls = PMA_getSearchSqls($each_table, (! empty($field_str) ? $field_str : ''), $search_str, $search_option);
220 // Executes the "COUNT" statement
221 $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
222 $num_search_result_total += $res_cnt;
224 $sql_query .= $newsearchsqls['select_count'];
226 echo '<tr class="noclick ' . ($odd_row ? 'odd' : 'even') . '">'
227 .'<td>' . sprintf(_ngettext('%s match inside table <i>%s</i>', '%s matches inside table <i>%s</i>', $res_cnt), $res_cnt,
228 htmlspecialchars($each_table)) . "</td>\n";
230 if ($res_cnt > 0) {
231 $this_url_params['sql_query'] = $newsearchsqls['select_fields'];
232 $browse_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
234 <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>
235 <?php
236 $this_url_params['sql_query'] = $newsearchsqls['delete'];
237 $delete_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
239 <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>
240 <?php
241 } else {
242 echo '<td>&nbsp;</td>' . "\n"
243 .'<td>&nbsp;</td>' . "\n";
244 }// end if else
245 $odd_row = ! $odd_row;
246 echo '</tr>' . "\n";
247 } // end for
249 echo '</table>' . "\n";
251 if (count($tables_selected) > 1) {
252 echo '<p>' . sprintf(_ngettext('<b>Total:</b> <i>%s</i> match', '<b>Total:</b> <i>%s</i> matches', $num_search_result_total),
253 $num_search_result_total) . '</p>' . "\n";
255 } // end 1.
258 * If we are in an Ajax request, we need to exit after displaying all the HTML
260 if ($GLOBALS['is_ajax_request'] == true) {
261 exit;
262 } else {
263 echo '</div>';//end searchresults div
267 * 2. Displays the main search form
270 <a name="db_search"></a>
271 <form id="db_search_form"<?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax"' : ''); ?> method="post" action="db_search.php" name="db_search">
272 <?php echo PMA_generate_common_hidden_inputs($GLOBALS['db']); ?>
273 <fieldset>
274 <legend><?php echo __('Search in database'); ?></legend>
276 <table class="formlayout">
277 <tr><td><?php echo __('Words or values to search for (wildcard: "%"):'); ?></td>
278 <td><input type="text" name="search_str" size="60"
279 value="<?php echo $searched; ?>" /></td>
280 </tr>
281 <tr><td align="right" valign="top">
282 <?php echo __('Find:'); ?></td>
283 <td><?php
285 $choices = array(
286 '1' => __('at least one of the words') . PMA_showHint(__('Words are separated by a space character (" ").')),
287 '2' => __('all words') . PMA_showHint(__('Words are separated by a space character (" ").')),
288 '3' => __('the exact phrase'),
289 '4' => __('as regular expression') . ' ' . PMA_showMySQLDocu('Regexp', 'Regexp')
291 // 4th parameter set to true to add line breaks
292 // 5th parameter set to false to avoid htmlspecialchars() escaping in the label
293 // since we have some HTML in some labels
294 PMA_display_html_radio('search_option', $choices, $search_option, true, false);
295 unset($choices);
297 </td>
298 </tr>
299 <tr><td align="right" valign="top">
300 <?php echo __('Inside tables:'); ?></td>
301 <td rowspan="2">
302 <?php
303 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
304 foreach ($tables_names_only as $each_table) {
305 if (in_array($each_table, $tables_selected)) {
306 $is_selected = ' selected="selected"';
307 } else {
308 $is_selected = '';
311 echo ' <option value="' . htmlspecialchars($each_table) . '"'
312 . $is_selected . '>'
313 . str_replace(' ', '&nbsp;', htmlspecialchars($each_table)) . '</option>' . "\n";
314 } // end while
316 echo ' </select>' . "\n";
317 $alter_select =
318 '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('selectall' => 1))) . '#db_search"'
319 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', true); return false;">' . __('Select All') . '</a>'
320 . '&nbsp;/&nbsp;'
321 . '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('unselectall' => 1))) . '#db_search"'
322 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', false); return false;">' . __('Unselect All') . '</a>';
324 </td>
325 </tr>
326 <tr><td align="right" valign="bottom">
327 <?php echo $alter_select; ?></td>
328 </tr>
329 <tr><td align="right">
330 <?php echo __('Inside column:'); ?></td>
331 <td><input type="text" name="field_str" size="60"
332 value="<?php echo ! empty($field_str) ? htmlspecialchars($field_str) : ''; ?>" /></td>
333 </tr>
334 </table>
335 </fieldset>
336 <fieldset class="tblFooters">
337 <input type="submit" name="submit_search" value="<?php echo __('Go'); ?>"
338 id="buttonGo" />
339 </fieldset>
340 </form>
342 <!-- These two table-image and table-link elements display the table name in browse search results -->
343 <div id='table-info'>
344 <a class="item" id="table-link" ></a>
345 </div>
346 <div id="browse-results">
347 <!-- this browse-results div is used to load the browse and delete results in the db search -->
348 </div>
349 <br class="clearfloat" />
350 <div id="sqlqueryform">
351 <!-- this sqlqueryform div is used to load the delete form in the db search -->
352 </div>
353 <!-- toggle query box link-->
354 <a id="togglequerybox"></a>
356 <?php
358 * Displays the footer
360 require './libraries/footer.inc.php';