2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright 2002-[#]year[#] Blue Static
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.
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
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 \*=====================================================================*/
22 $fetchtemplates = array(
30 define('SVN', '$Id$');
32 $focus['search'] = 'focus';
34 require_once('./global.php');
35 require_once('./includes/functions_product.php');
37 if (!can_perform('cansearch'))
39 $message->error_permission();
42 define('MODE_ANY', ($bugsys->in
['mode'] == 1));
43 define('MODE_ALL', ($bugsys->in
['mode'] == 2));
44 define('MODE_RAW', ($bugsys->in
['mode'] == 3));
46 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
47 define('SEARCH_WORD_MIN', $var['Value']);
49 $show['search'] = true
;
51 // ###################################################################
53 if (empty($_REQUEST['do']))
55 $_REQUEST['do'] = 'search';
58 // ###################################################################
60 if ($_REQUEST['do'] == 'process')
62 // -------------------------------------------------------------------
64 if ($bugsys->in
['summary'])
66 $keywords = preg_split('#\s+#', $bugsys->in
['summary']);
68 // TODO - need to have some str to bool conversions
70 foreach ($keywords AS $word)
72 if (strlen($word) < SEARCH_WORD_MIN
)
79 $querybuild['text'] .= " +$word";
83 $querybuild['text'] .= " $word";
86 if (!preg_match('#-(.+?)#', trim($word)))
92 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
93 $hilight = trim($hilight);
94 $hilight = preg_replace('#\s#', '+', $hilight);
96 $temp = trim($querybuild['text']);
98 if (MODE_ALL
OR MODE_RAW
)
100 $bool_flag = ' IN BOOLEAN MODE';
103 $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)";
106 // -------------------------------------------------------------------
108 if ($bugsys->in
['reporter'])
110 // force email or name?? make a distinction?
111 // more elegant way to do this? probably
112 $user = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "%'");
115 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
119 // -------------------------------------------------------------------
120 // product/component/version stuff
121 if (is_array($bugsys->in
['product']))
123 foreach ($bugsys->in
['product'] AS $prod)
125 $product = explode(',', $prod);
126 $product = $bugsys->clean($product, TYPE_UINT
);
127 $products[] = $product[0];
128 $components[] = $product[1];
129 $versions[] = $product[2];
131 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
134 // -------------------------------------------------------------------
135 // severity, priority, status, resolution, assignedto
138 if ($bugsys->in
['severity'])
140 $bugsys->input_clean('severity', TYPE_UINT
);
141 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in
['severity']) . ")";
145 if ($bugsys->in
['priority'])
147 $bugsys->input_clean('priority', TYPE_UINT
);
148 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in
['priority']) . ")";
152 if ($bugsys->in
['status'])
154 $bugsys->input_clean('status', TYPE_UINT
);
155 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in
['status']) . ")";
159 if ($bugsys->in
['resolution'])
161 $bugsys->input_clean('resolution', TYPE_UINT
);
162 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in
['resolution']) . ")";
166 if ($bugsys->in
['assignedto'])
168 $bugsys->input_clean('assignedto', TYPE_UINT
);
169 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in
['assignedto']) . ")";
172 // -------------------------------------------------------------------
174 if ($bugsys->in
['date'])
176 // now - (seconds/day * number of days)
177 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT
) * 3600);
178 $querybuild['date'] = "AND bug.dateline >= $dateline";
181 // -------------------------------------------------------------------
183 if (($bugsys->in
['favorite'] == 1 OR $bugsys->in
['favorite'] == 0) AND $bugsys->userinfo
['userid'])
185 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . $bugsys->userinfo
['userid']);
186 while ($favorite = $db->fetch_array($favorites))
188 $ids[] = $favorite['bugid'];
190 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in
['favorite'] == 0 ?
"NOT IN" : "IN") . " (" . implode(', ', $ids) . ")";
193 // -------------------------------------------------------------------
195 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
196 $orderby = array('ASC', 'DESC');
197 $bugsys->in
['orderby'] = strtoupper($bugsys->in
['orderby']);
198 if (in_array($bugsys->in
['sortby'], $sortby) AND in_array($bugsys->in
['orderby'], $orderby))
200 $sortclause = "ORDER BY " . $bugsys->in
['sortby'] . ' ' . $bugsys->in
['orderby'];
202 else if ($bugsys->in
['sortby'] == 'relevance')
211 // -------------------------------------------------------------------
213 $fields_fetch = $bugsys->db
->query("
215 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
216 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
217 ON (bugfield.fieldid = permission.fieldid)
218 WHERE permission.mask <> 0
219 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
220 AND bugfield.cansearch = 1"
222 while ($field = $bugsys->db
->fetch_array($fields_fetch))
224 if (!empty($bugsys->in
["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in
["field$field[fieldid]"])))
226 if ($field['type'] == 'input_checkbox')
228 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in
["field$field[fieldid]"] == 1) ?
0 : 1);
230 else if ($field['type'] == 'input_text')
232 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in
["field$field[fieldid]"] . "%'";
234 else if ($field['type'] == 'select_single' AND $bugsys->in
["field$field[fieldid]"] != -1)
236 $temp = unserialize($field['selects']);
237 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in
["field$field[fieldid]"]) ]) . "'";
242 // -------------------------------------------------------------------
243 // have to search something
244 if (sizeof($querybuild) < 1)
246 $message->error(sprintf(_('You have to enter some criteria to search for. Note that words less than %1$d characters are ignored by the search engine (and some other very common words, too).'), SEARCH_WORD_MIN
));
249 // -------------------------------------------------------------------
252 SELECT bug.*, comment.commentid
253 FROM " . TABLE_PREFIX
. "bug AS bug
254 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
255 ON (bug.bugid = comment.bugid)
256 LEFT JOIN " . TABLE_PREFIX
. "bugvaluefill AS bugfieldvalue
257 ON (bug.bugid = bugfieldvalue.bugid)
259 AND bug.product IN (#<'ONBITS:VIEW'>#)
260 AND (!bug.hidden OR (bug.hidden AND bug.product IN (#<'ONBITS:HIDDEN'>#))" . (can_perform('canviewownhidden') ?
" OR (bug.hidden AND bug.userid = " . $bugsys->userinfo
['userid'] . " AND bug.product IN (#<'ONBITS:OWNHIDDEN'>#))" : "") . ")
261 " . implode("\n\t\t", $querybuild) . "
265 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query);
267 $search = $db->query($runquery);
269 $numrows = $db->num_rows($search);
273 $message->error(_('No search results were returned that matched your criteria.'));
276 while ($result = $db->fetch_array($search))
278 $ids[] = $result['bugid'];
279 $results[] = $result;
282 if ($bugsys->userinfo
['userid'])
285 REPLACE INTO " . TABLE_PREFIX
. "search
286 (userid, dateline, query, ids, orderby, hilight, resultcount)
288 (" . $bugsys->userinfo
['userid'] . ",
289 " . TIMENOW
. ", '" . $bugsys->escape($query) . "',
290 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
291 '" . $bugsys->escape($hilight) . "',
292 " . sizeof($results) . "
298 $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
300 $_REQUEST['do'] = 'results';
303 // ###################################################################
305 if ($_REQUEST['do'] == 'search')
307 if ($bugsys->userinfo
['userid'] AND !$bugsys->in
['new'])
309 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE userid = " . $bugsys->userinfo
['userid']))
311 $_REQUEST['do'] = 'results';
325 if (!is_array($bugsys->datastore
['product']))
327 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
330 if (!is_array($bugsys->datastore
['version']))
332 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
335 $productSelect = ConstructProductSelect();
337 // -------------------------------------------------------------------
339 $fields = construct_custom_fields(null
, true
);
341 foreach ($fields AS $field)
345 $customfields['left'] .= $field;
349 $customfields['right'] .= $field;
354 // -------------------------------------------------------------------
356 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
357 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
358 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
359 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
362 foreach ($bugsys->datastore
['assignto'] AS $dev)
364 $value = $dev['userid'];
365 $label = construct_user_display($dev, false
);
366 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
369 eval('$template->flush("' . $template->fetch('search') . '");');
373 // ###################################################################
375 if ($_REQUEST['do'] == 'export')
377 // TODO - allow guests to use the export function too
378 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE userid = " . $bugsys->userinfo
['userid']);
379 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby]");
381 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
383 <bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo
, false
)) . '" date="' . $datef->format('r', TIMENOW
) . '">';
385 while ($bug = $db->fetch_array($bugs))
389 ProcessBugDataForDisplay(&$bug);
391 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
392 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
395 $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE userid = $bug[userid]"), false
) . "</reporter>";
397 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
398 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
399 if ($bug['component'])
401 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
403 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
404 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
405 if ($bug['assignedto'])
407 $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore
['assignto']["$bug[assignedto]"], false
) . "</assignedTo>";
409 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
410 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
411 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
413 $xml .= "\n\t</bug>";
416 $xml .= "\n</bugdarExport>";
418 $funct->download_file($xml, _('bugdar-search-results.xml'), true
);
421 // ###################################################################
423 if ($_REQUEST['do'] == 'results')
425 $show['cached'] = false
;
426 if ($bugsys->userinfo
['userid'] AND !$justprocess)
428 $search = $cachedsearch;
429 if ($search['dateline'] < TIMENOW
- 900 OR $bugsys->in
['rerun'])
431 $research = $db->query(str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewownhidden')), $search['query']));
432 while ($bug = $db->fetch_array($research))
434 $ids[] = $bug['bugid'];
437 $search['ids'] = implode(',', $ids);
438 $db->query("UPDATE " . TABLE_PREFIX
. "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW
. ", resultcount = " . sizeof($results) . " WHERE userid = " . $bugsys->userinfo
['userid']);
440 $show['cached'] = true
;
441 $hilight = $search['hilight'];
444 LoadPaginationFramework();
445 $pagination->setTotal($search['resultcount']);
446 $pagination->splitPages();
448 $search = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
449 while ($bug = $db->fetch_array($search))
451 $funct->exec_swap_bg($stylevar['alt_color'], '');
452 ProcessBugDataForDisplay(&$bug, $funct->bgcolour
);
453 $bug['urladd'] = "&hilight=$hilight";
454 eval('$bugs .= "' . $template->fetch('trackerhome_bits') . '";');
457 $show['pagenav'] = ($pagination->getPageCount() > 1);
458 $pagenav = $pagination->constructPageNav('search.php');
460 eval('$template->flush("' . $template->fetch('search_results') . '");');
463 /*=====================================================================*\
464 || ###################################################################
467 || ###################################################################
468 \*=====================================================================*/