r956: Cleaning up the search queries because there's no need for the mondo joins
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 $fetchtemplates = array(
23 'search',
24 'search_results',
25 'trackerhome_bits'
26 );
27
28 define('SVN', '$Id$');
29
30 $focus['search'] = 'focus';
31
32 require_once('./global.php');
33 require_once('./includes/functions_product.php');
34
35 if (!can_perform('cansearch'))
36 {
37 $message->error_permission();
38 }
39
40 define('MODE_ANY', ($bugsys->in['mode'] == 1));
41 define('MODE_ALL', ($bugsys->in['mode'] == 2));
42 define('MODE_RAW', ($bugsys->in['mode'] == 3));
43
44 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
45 define('SEARCH_WORD_MIN', $var['Value']);
46
47 $show['search'] = true;
48
49 // ###################################################################
50
51 if (empty($_REQUEST['do']))
52 {
53 $_REQUEST['do'] = 'search';
54 }
55
56 // ###################################################################
57
58 if ($_REQUEST['do'] == 'process')
59 {
60 // -------------------------------------------------------------------
61 // handle keywords
62 if ($bugsys->in['summary'])
63 {
64 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
65
66 // #*# need to have some str to bool conversions
67
68 foreach ($keywords AS $word)
69 {
70 if (strlen($word) < SEARCH_WORD_MIN)
71 {
72 continue;
73 }
74
75 if (MODE_ALL)
76 {
77 $querybuild['text'] .= " +$word";
78 }
79 else
80 {
81 $querybuild['text'] .= " $word";
82 }
83
84 if (!preg_match('#-(.+?)#', trim($word)))
85 {
86 $hilight .= " $word";
87 }
88 }
89
90 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
91 $hilight = trim($hilight);
92 $hilight = preg_replace('#\s#', '+', $hilight);
93
94 $temp = trim($querybuild['text']);
95
96 if (MODE_ALL OR MODE_RAW)
97 {
98 $bool_flag = ' IN BOOLEAN MODE';
99 }
100
101 $querybuild['text'] = "AND\n\t\t\t(\n\t\t\t\tMATCH (bug.summary) AGAINST ('$temp'$bool_flag)\n\t\t\t\tOR MATCH (comment.comment) AGAINST ('$temp'$bool_flag)\n\t\t\t)";
102 }
103
104 // -------------------------------------------------------------------
105 // reporter
106 if ($bugsys->in['reporter'])
107 {
108 // force email or name?? make a distinction?
109 // more elegant way to do this? probably
110 $user = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->in['reporter']) . "%'");
111 if ($user['userid'])
112 {
113 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
114 }
115 }
116
117 // -------------------------------------------------------------------
118 // product/component/version stuff
119 if (is_array($bugsys->in['pcv_select']))
120 {
121 foreach ($bugsys->in['pcv_select'] AS $pcv)
122 {
123 $pcv = parse_pcv_select($pcv, true);
124 $products[] = $pcv['product'];
125 $components[] = $pcv['component'];
126 $versions[] = $pcv['version'];
127 }
128 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
129 }
130
131 // -------------------------------------------------------------------
132 // severity, priority, status, resolution, assignedto
133
134 // severity
135 if ($bugsys->in['severity'])
136 {
137 $bugsys->input_clean('severity', TYPE_UINT);
138 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")";
139 }
140
141 // priority
142 if ($bugsys->in['priority'])
143 {
144 $bugsys->input_clean('priority', TYPE_UINT);
145 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")";
146 }
147
148 // status
149 if ($bugsys->in['status'])
150 {
151 $bugsys->input_clean('status', TYPE_UINT);
152 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")";
153 }
154
155 // resolution
156 if ($bugsys->in['resolution'])
157 {
158 $bugsys->input_clean('resolution', TYPE_UINT);
159 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")";
160 }
161
162 // assignment
163 if ($bugsys->in['assignedto'])
164 {
165 $bugsys->input_clean('assignedto', TYPE_UINT);
166 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")";
167 }
168
169 // -------------------------------------------------------------------
170 // date
171 if ($bugsys->in['date'])
172 {
173 // now - (seconds/day * number of days)
174 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT) * 3600);
175 $querybuild['date'] = "AND bug.dateline >= $dateline";
176 }
177
178 // -------------------------------------------------------------------
179 // favourites
180 if (($bugsys->in['favourite'] == 1 OR $bugsys->in['favourite'] == 0) AND $bugsys->userinfo['userid'])
181 {
182 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
183 while ($favourite = $db->fetch_array($favourites))
184 {
185 $ids[] = $favourite['bugid'];
186 }
187 $querybuild['favourites'] = "AND bug.bugid " . ($bugsys->in['favourite'] == 0 ? "NOT IN" : "IN") . " (" . implode(', ', $ids) . ")";
188 }
189
190 // -------------------------------------------------------------------
191 // sort by
192 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
193 $orderby = array('ASC', 'DESC');
194 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
195 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
196 {
197 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
198 }
199 else if ($bugsys->in['sortby'] == 'relevance')
200 {
201 $sortclause = '';
202 }
203 else
204 {
205 $sortclause = '';
206 }
207
208 // -------------------------------------------------------------------
209 // custom fields
210 $fields_fetch = $bugsys->db->query("
211 SELECT bugfield.*
212 FROM " . TABLE_PREFIX . "bugfield AS bugfield
213 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
214 ON (bugfield.fieldid = permission.fieldid)
215 WHERE permission.mask <> 0
216 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
217 AND bugfield.cansearch = 1"
218 );
219 while ($field = $bugsys->db->fetch_array($fields_fetch))
220 {
221 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
222 {
223 if ($field['type'] == 'input_checkbox')
224 {
225 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
226 }
227 else if ($field['type'] == 'input_text')
228 {
229 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
230 }
231 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
232 {
233 $temp = unserialize($field['selects']);
234 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
235 }
236 }
237 }
238
239 // -------------------------------------------------------------------
240 // have to search something
241 if (sizeof($querybuild) < 1)
242 {
243 $message->error(sprintf($lang->string('You have to enter some criteria to search for. Node that words less than %1$d characters are ignored by the search engine (and some other very common words, too).'), SEARCH_WORD_MIN));
244 }
245
246 // -------------------------------------------------------------------
247 // do the search
248 $query = "
249 SELECT bug.*, comment.commentid
250 FROM " . TABLE_PREFIX . "bug AS bug
251 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
252 ON (bug.bugid = comment.bugid)
253 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
254 ON (bug.bugid = bugfieldvalue.bugid)
255 WHERE bug.bugid <> 0
256 AND bug.product IN (#<'ONBITS:VIEW'>#)
257 AND (!bug.hidden OR (bug.hidden AND bug.product IN (#<'ONBITS:HIDDEN'>#)))
258 " . implode("\n\t\t", $querybuild) . "
259 GROUP BY bug.bugid
260 $sortclause";
261
262 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $query);
263
264 $search = $db->query($runquery);
265
266 $numrows = $db->num_rows($search);
267
268 if ($numrows < 1)
269 {
270 $message->error($lang->string('No search results were returned that matched your criteria.'));
271 }
272
273 while ($result = $db->fetch_array($search))
274 {
275 $ids[] = $result['bugid'];
276 $results[] = $result;
277 }
278
279 if ($bugsys->userinfo['userid'])
280 {
281 $db->query("
282 REPLACE INTO " . TABLE_PREFIX . "search
283 (userid, dateline, query, ids, orderby, hilight)
284 VALUES
285 (" . $bugsys->userinfo['userid'] . ",
286 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
287 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
288 '" . $bugsys->escape($hilight) . "'
289 )"
290 );
291 }
292
293 $justprocess = true;
294
295 $_REQUEST['do'] = 'results';
296 }
297
298 // ###################################################################
299
300 if ($_REQUEST['do'] == 'search')
301 {
302 if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
303 {
304 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid']))
305 {
306 $_REQUEST['do'] = 'results';
307 }
308 else
309 {
310 $newsearch = true;
311 }
312 }
313 else
314 {
315 $newsearch = true;
316 }
317
318 if ($newsearch)
319 {
320 $pcv_select = construct_pcv_select();
321
322 // -------------------------------------------------------------------
323 // custom fields
324 $fields = construct_custom_fields(null, true);
325 $i = 0;
326 foreach ($fields AS $field)
327 {
328 if ($i % 2 == 0)
329 {
330 $customfields['left'] .= $field;
331 }
332 else
333 {
334 $customfields['right'] .= $field;
335 }
336 $i++;
337 }
338
339 // -------------------------------------------------------------------
340 // built-in fields
341 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
342 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
343 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
344 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
345
346 $select['dev'] = '';
347 foreach ($bugsys->datastore['assignto'] AS $dev)
348 {
349 $value = $dev['userid'];
350 $label = construct_user_display($dev, false);
351 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
352 }
353
354 eval('$template->flush("' . $template->fetch('search') . '");');
355 }
356 }
357
358 // ###################################################################
359
360 if ($_REQUEST['do'] == 'results')
361 {
362 $show['cached'] = false;
363 if ($bugsys->userinfo['userid'] AND !$justprocess)
364 {
365 $search = $cachedsearch;
366 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
367 {
368 $search = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $search['query']));
369 while ($bug = $db->fetch_array($search))
370 {
371 $ids[] = $bug['bugid'];
372 $results[] = $bug;
373 }
374 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . " WHERE userid = " . $bugsys->userinfo['userid']);
375 }
376 else
377 {
378 $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby]");
379 while ($bug = $db->fetch_array($search))
380 {
381 $results[] = $bug;
382 }
383 }
384 $show['cached'] = true;
385 $hilight = $search['hilight'];
386 }
387
388 foreach ($results AS $bug)
389 {
390 $funct->exec_swap_bg($stylevar['alt_colour'], '');
391 $bug['bgcolour'] = ($bugsys->userinfo['showcolours'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour);
392 $bug['product'] = $bugsys->datastore['product']["$bug[product]"]['title'];
393 $bug['version'] = $bugsys->datastore['version']["$bug[version]"]['version'];
394 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
395 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
396 $bug['priority'] = $bugsys->datastore['priority']["$bug[priority]"]['priority'];
397 $bug['severity'] = $bugsys->datastore['severity']["$bug[severity]"]['severity'];
398 $bug['lastposttime'] = ($bug['hiddendisplay'] ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
399 $bug['lastpost'] = ($bug['hiddendisplay'] ? $bug['hiddenlastpostbyname'] : $bug['lastpostbyname']);
400 $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
401 $bug['urladd'] = "&amp;hilight=$hilight";
402 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
403 }
404
405 eval('$template->flush("' . $template->fetch('search_results') . '");');
406 }
407
408 /*=====================================================================*\
409 || ###################################################################
410 || # $HeadURL$
411 || # $Id$
412 || ###################################################################
413 \*=====================================================================*/
414 ?>