Translation update done using Pootle.
[phpmyadmin/madhuracj.git] / db_search.php
blobc0f2c082af16c7a928c9c6306941f6d0aa6ed446
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';
20 /**
21 * Gets some core libraries and send headers
23 require './libraries/db_common.inc.php';
25 /**
26 * init
28 // If config variable $GLOBALS['cfg']['Usedbsearch'] is on false : exit.
29 if (! $GLOBALS['cfg']['UseDbSearch']) {
30 PMA_mysqlDie(__('Access denied'), '', false, $err_url);
31 } // end if
32 $url_query .= '&amp;goto=db_search.php';
33 $url_params['goto'] = 'db_search.php';
35 /**
36 * @global array list of tables from the current database
37 * but do not clash with $tables coming from db_info.inc.php
39 $tables_names_only = PMA_DBI_get_tables($GLOBALS['db']);
41 $search_options = array(
42 '1' => __('at least one of the words'),
43 '2' => __('all words'),
44 '3' => __('the exact phrase'),
45 '4' => __('as regular expression'),
48 if (empty($_REQUEST['search_option']) || ! is_string($_REQUEST['search_option'])
49 || ! array_key_exists($_REQUEST['search_option'], $search_options)) {
50 $search_option = 1;
51 unset($_REQUEST['submit_search']);
52 } else {
53 $search_option = (int) $_REQUEST['search_option'];
54 $option_str = $search_options[$_REQUEST['search_option']];
57 if (empty($_REQUEST['search_str']) || ! is_string($_REQUEST['search_str'])) {
58 unset($_REQUEST['submit_search']);
59 $searched = '';
60 } else {
61 $searched = htmlspecialchars($_REQUEST['search_str']);
62 // For "as regular expression" (search option 4), we should not treat
63 // this as an expression that contains a LIKE (second parameter of
64 // PMA_sqlAddSlashes()).
66 // Usage example: If user is seaching for a literal $ in a regexp search,
67 // he should enter \$ as the value.
68 $search_str = PMA_sqlAddSlashes($_REQUEST['search_str'], ($search_option == 4 ? false : true));
71 $tables_selected = array();
72 if (empty($_REQUEST['table_select']) || ! is_array($_REQUEST['table_select'])) {
73 unset($_REQUEST['submit_search']);
74 } elseif (! isset($_REQUEST['selectall']) && ! isset($_REQUEST['unselectall'])) {
75 $tables_selected = array_intersect($_REQUEST['table_select'], $tables_names_only);
78 if (isset($_REQUEST['selectall'])) {
79 $tables_selected = $tables_names_only;
80 } elseif (isset($_REQUEST['unselectall'])) {
81 $tables_selected = array();
84 if (empty($_REQUEST['field_str']) || ! is_string($_REQUEST['field_str'])) {
85 unset($field_str);
86 } else {
87 $field_str = PMA_sqlAddSlashes($_REQUEST['field_str'], true);
90 /**
91 * Displays top links if we are not in an Ajax request
93 $sub_part = '';
95 if ( $GLOBALS['is_ajax_request'] != true) {
96 require './libraries/db_info.inc.php';
97 echo '<div id="searchresults">';
101 * 1. Main search form has been submitted
103 if (isset($_REQUEST['submit_search'])) {
106 * Builds the SQL search query
108 * @todo can we make use of fulltextsearch IN BOOLEAN MODE for this?
109 * PMA_backquote
110 * PMA_DBI_free_result
111 * PMA_DBI_fetch_assoc
112 * $GLOBALS['db']
113 * explode
114 * count
115 * strlen
116 * @param string the table name
117 * @param string restrict the search to this field
118 * @param string the string to search
119 * @param integer type of search (1 -> 1 word at least, 2 -> all words,
120 * 3 -> exact string, 4 -> regexp)
122 * @return array 3 SQL querys (for count, display and delete results)
124 function PMA_getSearchSqls($table, $field, $search_str, $search_option)
126 // Statement types
127 $sqlstr_select = 'SELECT';
128 $sqlstr_delete = 'DELETE';
130 // Fields to select
131 $tblfields = PMA_DBI_fetch_result('SHOW FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($GLOBALS['db']),
132 null, 'Field');
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) {
152 $thefieldlikevalue[] = 'CONVERT(' . PMA_backquote($tblfield) . ' USING utf8)'
153 . ' ' . $like_or_regex . ' '
154 . "'" . $automatic_wildcard
155 . $search_word
156 . $automatic_wildcard . "'";
158 } // end for
160 if (count($thefieldlikevalue) > 0) {
161 $fieldslikevalues[] = implode(' OR ', $thefieldlikevalue);
163 } // end for
165 $implode_str = ($search_option == 1 ? ' OR ' : ' AND ');
166 if ( empty($fieldslikevalues)) {
167 // this could happen when the "inside field" does not exist
168 // in any selected tables
169 $sqlstr_where = ' WHERE FALSE';
170 } else {
171 $sqlstr_where = ' WHERE (' . implode(') ' . $implode_str . ' (', $fieldslikevalues) . ')';
173 unset($fieldslikevalues);
175 // Builds complete queries
176 $sql['select_fields'] = $sqlstr_select . ' * ' . $sqlstr_from . $sqlstr_where;
177 // here, I think we need to still use the COUNT clause, even for
178 // VIEWs, anyway we have a WHERE clause that should limit results
179 $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS `count`' . $sqlstr_from . $sqlstr_where;
180 $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where;
182 return $sql;
183 } // end of the "PMA_getSearchSqls()" function
187 * Displays the results
189 $this_url_params = array(
190 'db' => $GLOBALS['db'],
191 'goto' => 'db_sql.php',
192 'pos' => 0,
193 'is_js_confirmed' => 0,
196 // Displays search string
197 echo '<br />' . "\n"
198 .'<table class="data">' . "\n"
199 .'<caption class="tblHeaders">' . "\n"
200 .sprintf(__('Search results for "<i>%s</i>" %s:'),
201 $searched, $option_str) . "\n"
202 .'</caption>' . "\n";
204 $num_search_result_total = 0;
205 $odd_row = true;
207 foreach ($tables_selected as $each_table) {
208 // Gets the SQL statements
209 $newsearchsqls = PMA_getSearchSqls($each_table, (! empty($field_str) ? $field_str : ''), $search_str, $search_option);
211 // Executes the "COUNT" statement
212 $res_cnt = PMA_DBI_fetch_value($newsearchsqls['select_count']);
213 $num_search_result_total += $res_cnt;
215 $sql_query .= $newsearchsqls['select_count'];
217 echo '<tr class="noclick ' . ($odd_row ? 'odd' : 'even') . '">'
218 .'<td>' . sprintf(_ngettext('%s match inside table <i>%s</i>', '%s matches inside table <i>%s</i>', $res_cnt), $res_cnt,
219 htmlspecialchars($each_table)) . "</td>\n";
221 if ($res_cnt > 0) {
222 $this_url_params['sql_query'] = $newsearchsqls['select_fields'];
223 $browse_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
225 <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>
226 <?php
227 $this_url_params['sql_query'] = $newsearchsqls['delete'];
228 $delete_result_path = 'sql.php' . PMA_generate_common_url($this_url_params);
230 <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>
231 <?php
232 } else {
233 echo '<td>&nbsp;</td>' . "\n"
234 .'<td>&nbsp;</td>' . "\n";
235 }// end if else
236 $odd_row = ! $odd_row;
237 echo '</tr>' . "\n";
238 } // end for
240 echo '</table>' . "\n";
242 if (count($tables_selected) > 1) {
243 echo '<p>' . sprintf(_ngettext('<b>Total:</b> <i>%s</i> match', '<b>Total:</b> <i>%s</i> matches', $num_search_result_total),
244 $num_search_result_total) . '</p>' . "\n";
246 } // end 1.
249 * If we are in an Ajax request, we need to exit after displaying all the HTML
251 if ($GLOBALS['is_ajax_request'] == true) {
252 exit;
254 else {
255 echo '</div>';//end searchresults div
259 * 2. Displays the main search form
262 <a name="db_search"></a>
263 <form id="db_search_form"<?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax"' : ''); ?> method="post" action="db_search.php" name="db_search">
264 <?php echo PMA_generate_common_hidden_inputs($GLOBALS['db']); ?>
265 <fieldset>
266 <legend><?php echo __('Search in database'); ?></legend>
268 <table class="formlayout">
269 <tr><td><?php echo __('Words or values to search for (wildcard: "%"):'); ?></td>
270 <td><input type="text" name="search_str" size="60"
271 value="<?php echo $searched; ?>" /></td>
272 </tr>
273 <tr><td align="right" valign="top">
274 <?php echo __('Find:'); ?></td>
275 <td><?php
277 $choices = array(
278 '1' => __('at least one of the words') . PMA_showHint(__('Words are separated by a space character (" ").')),
279 '2' => __('all words') . PMA_showHint(__('Words are separated by a space character (" ").')),
280 '3' => __('the exact phrase'),
281 '4' => __('as regular expression') . ' ' . PMA_showMySQLDocu('Regexp', 'Regexp')
283 // 4th parameter set to true to add line breaks
284 // 5th parameter set to false to avoid htmlspecialchars() escaping in the label
285 // since we have some HTML in some labels
286 PMA_display_html_radio('search_option', $choices, $search_option, true, false);
287 unset($choices);
289 </td>
290 </tr>
291 <tr><td align="right" valign="top">
292 <?php echo __('Inside tables:'); ?></td>
293 <td rowspan="2">
294 <?php
295 echo ' <select name="table_select[]" size="6" multiple="multiple">' . "\n";
296 foreach ($tables_names_only as $each_table) {
297 if (in_array($each_table, $tables_selected)) {
298 $is_selected = ' selected="selected"';
299 } else {
300 $is_selected = '';
303 echo ' <option value="' . htmlspecialchars($each_table) . '"'
304 . $is_selected . '>'
305 . str_replace(' ', '&nbsp;', htmlspecialchars($each_table)) . '</option>' . "\n";
306 } // end while
308 echo ' </select>' . "\n";
309 $alter_select =
310 '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('selectall' => 1))) . '#db_search"'
311 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', true); return false;">' . __('Select All') . '</a>'
312 . '&nbsp;/&nbsp;'
313 . '<a href="db_search.php' . PMA_generate_common_url(array_merge($url_params, array('unselectall' => 1))) . '#db_search"'
314 . ' onclick="setSelectOptions(\'db_search\', \'table_select[]\', false); return false;">' . __('Unselect All') . '</a>';
316 </td>
317 </tr>
318 <tr><td align="right" valign="bottom">
319 <?php echo $alter_select; ?></td>
320 </tr>
321 <tr><td align="right">
322 <?php echo __('Inside column:'); ?></td>
323 <td><input type="text" name="field_str" size="60"
324 value="<?php echo ! empty($field_str) ? htmlspecialchars($field_str) : ''; ?>" /></td>
325 </tr>
326 </table>
327 </fieldset>
328 <fieldset class="tblFooters">
329 <input type="submit" name="submit_search" value="<?php echo __('Go'); ?>"
330 id="buttonGo" />
331 </fieldset>
332 </form>
334 <!-- These two table-image and table-link elements display the table name in browse search results -->
335 <div id='table-info'>
336 <a class="item" id="table-link" ></a>
337 </div>
338 <div id="browse-results">
339 <!-- this browse-results div is used to load the browse and delete results in the db search -->
340 </div>
341 <br class="clearfloat" />
342 <div id="sqlqueryform">
343 <!-- this sqlqueryform div is used to load the delete form in the db search -->
344 </div>
345 <!-- toggle query box link-->
346 <a id="togglequerybox"></a>
348 <?php
350 * Displays the footer
352 require './libraries/footer.inc.php';