r765: Say hello to the GPL
[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.productid IN (" . implode(',', $products) . ") AND bug.componentid IN (" . implode(',', $components) . ") AND bug.versionid IN (" . implode(',', $versions) . ")";
129 }
130
131 // -------------------------------------------------------------------
132 // severity, priority, status, resolution, assignedto
133
134 // severity
135 if ($bugsys->in['severity'])
136 {
137 $querybuild['severity'] = "AND bug.severity = " . intval($bugsys->in['severity']);
138 }
139
140 // priority
141 if ($bugsys->in['priority'])
142 {
143 $querybuild['priority'] = "AND bug.priority = " . intval($bugsys->in['priority']);
144 }
145
146 // status
147 if ($bugsys->in['status'])
148 {
149 $querybuild['status'] = "AND bug.status = " . intval($bugsys->in['status']);
150 }
151
152 // resolution
153 if ($bugsys->in['resolution'])
154 {
155 $querybuild['resolution'] = "AND bug.resolution = " . intval($bugsys->in['resolution']);
156 }
157
158 // assignment
159 if ($bugsys->in['assignedto'])
160 {
161 $querybuild['assignedto'] = "AND bug.assignedto = " . intval($bugsys->in['assignedto']);
162 }
163
164 // -------------------------------------------------------------------
165 // date
166 if ($bugsys->in['date'])
167 {
168 // now - (seconds/day * number of days)
169 $dateline = time() - (intval($bugsys->in['date']) * 3600);
170 $querybuild['date'] = "AND bug.dateline >= $dateline";
171 }
172
173 // -------------------------------------------------------------------
174 // favourites
175 if ($bugsys->in['favourite'] AND $bugsys->userinfo['userid'])
176 {
177 $favourites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE userid = " . $bugsys->userinfo['userid']);
178 while ($favourite = $db->fetch_array($favourites))
179 {
180 $ids[] = $favourite['bugid'];
181 }
182 $querybuild['favourites'] = "AND bug.bugid IN (" . implode(', ', $ids) . ")";
183 }
184
185 // -------------------------------------------------------------------
186 // sort by
187 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
188 $orderby = array('ASC', 'DESC');
189 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
190 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
191 {
192 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
193 }
194 else if ($bugsys->in['sortby'] == 'relevance')
195 {
196 $sortclause = '';
197 }
198 else
199 {
200 $sortclause = '';
201 }
202
203 // -------------------------------------------------------------------
204 // custom fields
205 $fields_fetch = $bugsys->db->query("
206 SELECT bugfield.*
207 FROM " . TABLE_PREFIX . "bugfield AS bugfield
208 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
209 ON (bugfield.fieldid = permission.fieldid)
210 WHERE permission.mask <> 0
211 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
212 AND bugfield.cansearch = 1"
213 );
214 while ($field = $bugsys->db->fetch_array($fields_fetch))
215 {
216 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
217 {
218 if ($field['type'] == 'input_checkbox')
219 {
220 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
221 }
222 else if ($field['type'] == 'input_text')
223 {
224 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
225 }
226 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
227 {
228 $temp = unserialize($field['selects']);
229 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
230 }
231 }
232 }
233
234 // -------------------------------------------------------------------
235 // have to search something
236 if (count($querybuild) < 1)
237 {
238 $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));
239 }
240
241 // -------------------------------------------------------------------
242 // do the search
243 $query = "
244 SELECT bug.*, comment.commentid,
245 user1.displayname AS firstreport,
246 user2.displayname AS lastpost
247 FROM " . TABLE_PREFIX . "bug AS bug
248 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
249 ON (bug.bugid = comment.bugid)
250 LEFT JOIN " . TABLE_PREFIX . "user AS user1
251 ON (bug.userid = user1.userid)
252 LEFT JOIN " . TABLE_PREFIX . "user AS user2
253 ON (bug.lastpostby = user2.userid)
254 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
255 ON (bug.bugid = bugfieldvalue.bugid)
256 WHERE bug.bugid <> 0
257 AND bug.productid IN (#<'ONBITS:VIEW'>#)
258 AND (!bug.hidden OR (bug.hidden AND bug.productid IN (#<'ONBITS:HIDDEN'>#)))
259 " . implode("\n\t\t", $querybuild) . "
260 GROUP BY bug.bugid
261 $sortclause";
262
263 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $query);
264
265 $search = $db->query($runquery);
266
267 $numrows = $db->num_rows($search);
268
269 if ($numrows < 1)
270 {
271 $message->error($lang->string('No search results were returned that matched your criteria.'));
272 }
273
274 while ($result = $db->fetch_array($search))
275 {
276 $ids[] = $result['bugid'];
277 $results[] = $result;
278 }
279
280 if ($bugsys->userinfo['userid'])
281 {
282 $db->query("
283 REPLACE INTO " . TABLE_PREFIX . "search
284 (userid, dateline, query, ids, orderby, hilight)
285 VALUES
286 (" . $bugsys->userinfo['userid'] . ",
287 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
288 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
289 '" . $bugsys->escape($hilight) . "'
290 )"
291 );
292 }
293
294 $justprocess = true;
295
296 $_REQUEST['do'] = 'results';
297 }
298
299 // ###################################################################
300
301 if ($_REQUEST['do'] == 'search')
302 {
303 if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
304 {
305 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid']))
306 {
307 $_REQUEST['do'] = 'results';
308 }
309 else
310 {
311 $newsearch = true;
312 }
313 }
314 else
315 {
316 $newsearch = true;
317 }
318
319 if ($newsearch)
320 {
321 $pcv_select = construct_pcv_select();
322
323 // -------------------------------------------------------------------
324 // custom fields
325 $fields = construct_custom_fields(null, true);
326 $i = 0;
327 foreach ($fields AS $field)
328 {
329 if ($i % 2 == 0)
330 {
331 $customfields['left'] .= $field;
332 }
333 else
334 {
335 $customfields['right'] .= $field;
336 }
337 $i++;
338 }
339
340 // -------------------------------------------------------------------
341 // built-in fields
342 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, true);
343 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, true);
344 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, true);
345 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, true);
346
347 $select['dev'] = '<option value="0" selected="selected"></option>';
348 foreach ($bugsys->datastore['assignto'] AS $dev)
349 {
350 $value = $dev['userid'];
351 $label = construct_user_display($dev, false);
352 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
353 }
354
355 eval('$template->flush("' . $template->fetch('search') . '");');
356 }
357 }
358
359 // ###################################################################
360
361 if ($_REQUEST['do'] == 'results')
362 {
363 $show['cached'] = false;
364 if ($bugsys->userinfo['userid'] AND !$justprocess)
365 {
366 $search = $cachedsearch;
367 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
368 {
369 $search = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden')), $search['query']));
370 while ($bug = $db->fetch_array($search))
371 {
372 $ids[] = $bug['bugid'];
373 $results[] = $bug;
374 }
375 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . " WHERE userid = " . $bugsys->userinfo['userid']);
376 }
377 else
378 {
379 $search = $db->query("
380 SELECT bug.*, user1.displayname AS firstreport,
381 user2.displayname AS lastpost
382 FROM " . TABLE_PREFIX . "bug AS bug
383 LEFT JOIN " . TABLE_PREFIX . "user AS user1
384 ON (bug.userid = user1.userid)
385 LEFT JOIN " . TABLE_PREFIX . "user AS user2
386 ON (bug.lastpostby = user2.userid)
387 WHERE bug.bugid IN ($search[ids])
388 $search[orderby]"
389 );
390 while ($bug = $db->fetch_array($search))
391 {
392 $results[] = $bug;
393 }
394 }
395 $show['cached'] = true;
396 $hilight = $search['hilight'];
397 }
398
399 foreach ($results AS $bug)
400 {
401 $funct->exec_swap_bg($stylevar['alt_colour'], '');
402 $bug['bgcolour'] = ($bugsys->userinfo['showcolours'] ? $bugsys->datastore['status']["$bug[status]"]['color'] : $funct->bgcolour);
403 $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
404 $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
405 $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
406 $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
407 $bug['priority'] = $bugsys->datastore['priority']["$bug[priority]"]['priority'];
408 $bug['severity'] = $bugsys->datastore['severity']["$bug[severity]"]['severity'];
409 $bug['lastposttime'] = (($bug['hiddendisplay']) ? $bug['hiddenlastposttime'] : $bug['lastposttime']);
410 $bug['lastpost'] = (($bug['hiddendisplay']) ? $bug['hiddenlastpost'] : $bug['lastpost']);
411 $bug['lastposttime'] = $datef->format($bugsys->options['dateformat'], $bug['lastposttime']);
412 $bug['urladd'] = "&amp;hilight=$hilight";
413 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
414 }
415
416 eval('$template->flush("' . $template->fetch('search_results') . '");');
417 }
418
419 /*=====================================================================*\
420 || ###################################################################
421 || # $HeadURL$
422 || # $Id$
423 || ###################################################################
424 \*=====================================================================*/
425 ?>