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');
36 require_once('./includes/class_sort.php');
38 if (!can_perform('cansearch'))
40 $message->error_permission();
43 define('MODE_ANY', ($bugsys->in
['mode'] == 1));
44 define('MODE_ALL', ($bugsys->in
['mode'] == 2));
45 define('MODE_RAW', ($bugsys->in
['mode'] == 3));
47 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
48 define('SEARCH_WORD_MIN', $var['Value']);
50 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = 0 AND dateline < " . (TIMENOW
- 3600));
52 $show['search'] = true
;
54 // ###################################################################
56 if (empty($_REQUEST['do']))
58 $_REQUEST['do'] = 'search';
61 // ###################################################################
63 if ($_REQUEST['do'] == 'process')
65 // -------------------------------------------------------------------
67 if ($bugsys->in
['summary'])
69 $keywords = preg_split('#\s+#', $bugsys->in
['summary']);
71 // TODO - need to have some str to bool conversions
73 foreach ($keywords AS $word)
75 if (strlen($word) < SEARCH_WORD_MIN
)
82 $querybuild['text'] .= " +$word";
86 $querybuild['text'] .= " $word";
89 if (!preg_match('#-(.+?)#', trim($word)))
95 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
96 $hilight = trim($hilight);
97 $hilight = preg_replace('#\s#', '+', $hilight);
99 $temp = trim($querybuild['text']);
101 if (MODE_ALL
OR MODE_RAW
)
103 $bool_flag = ' IN BOOLEAN MODE';
106 $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)";
109 // -------------------------------------------------------------------
111 if ($bugsys->in
['reporter'])
113 // force email or name?? make a distinction?
114 // more elegant way to do this? probably
115 $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')) . "%'");
118 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
122 // -------------------------------------------------------------------
123 // product/component/version stuff
124 if (is_array($bugsys->in
['product']))
126 foreach ($bugsys->in
['product'] AS $prod)
128 $product = explode(',', $prod);
129 $product = $bugsys->clean($product, TYPE_UINT
);
130 $products[] = $product[0];
131 $components[] = $product[1];
132 $versions[] = $product[2];
134 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
137 // -------------------------------------------------------------------
138 // severity, priority, status, resolution, assignedto
141 if ($bugsys->in
['severity'])
143 $bugsys->input_clean('severity', TYPE_UINT
);
144 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in
['severity']) . ")";
148 if ($bugsys->in
['priority'])
150 $bugsys->input_clean('priority', TYPE_UINT
);
151 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in
['priority']) . ")";
155 if ($bugsys->in
['status'])
157 $bugsys->input_clean('status', TYPE_UINT
);
158 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in
['status']) . ")";
162 if ($bugsys->in
['resolution'])
164 $bugsys->input_clean('resolution', TYPE_UINT
);
165 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in
['resolution']) . ")";
169 if ($bugsys->in
['assignedto'])
171 $bugsys->input_clean('assignedto', TYPE_UINT
);
172 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in
['assignedto']) . ")";
175 // -------------------------------------------------------------------
177 if ($bugsys->in
['date'])
179 // now - (seconds/day * number of days)
180 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT
) * 3600);
181 $querybuild['date'] = "AND bug.dateline >= $dateline";
184 // -------------------------------------------------------------------
186 if (($bugsys->in
['favorite'] == 1 OR $bugsys->in
['favorite'] == 0) AND $bugsys->userinfo
['userid'])
188 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . $bugsys->userinfo
['userid']);
189 while ($favorite = $db->fetch_array($favorites))
191 $ids[] = $favorite['bugid'];
193 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in
['favorite'] == 0 ?
"NOT IN" : "IN") . " (" . implode(', ', $ids) . ")";
196 // -------------------------------------------------------------------
198 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
199 $orderby = array('ASC', 'DESC');
200 $bugsys->in
['orderby'] = strtoupper($bugsys->in
['orderby']);
201 if (in_array($bugsys->in
['sortby'], $sortby) AND in_array($bugsys->in
['orderby'], $orderby))
203 $sortclause = "ORDER BY " . $bugsys->in
['sortby'] . ' ' . $bugsys->in
['orderby'];
205 else if ($bugsys->in
['sortby'] == 'relevance')
214 // -------------------------------------------------------------------
216 $fields_fetch = $bugsys->db
->query("
218 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
219 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
220 ON (bugfield.fieldid = permission.fieldid)
221 WHERE permission.mask <> 0
222 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
223 AND bugfield.cansearch = 1"
225 while ($field = $bugsys->db
->fetch_array($fields_fetch))
227 if (!empty($bugsys->in
["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in
["field$field[fieldid]"])))
229 if ($field['type'] == 'input_checkbox')
231 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in
["field$field[fieldid]"] == 1) ?
0 : 1);
233 else if ($field['type'] == 'input_text')
235 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in
["field$field[fieldid]"] . "%'";
237 else if ($field['type'] == 'select_single' AND $bugsys->in
["field$field[fieldid]"] != -1)
239 $temp = unserialize($field['selects']);
240 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in
["field$field[fieldid]"]) ]) . "'";
245 // -------------------------------------------------------------------
246 // have to search something
247 if (sizeof($querybuild) < 1)
249 $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
));
252 // -------------------------------------------------------------------
255 SELECT bug.*, comment.commentid
256 FROM " . TABLE_PREFIX
. "bug AS bug
257 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
258 ON (bug.bugid = comment.bugid)
259 LEFT JOIN " . TABLE_PREFIX
. "bugvaluefill AS bugfieldvalue
260 ON (bug.bugid = bugfieldvalue.bugid)
262 AND bug.product IN (#<'ONBITS:VIEW'>#)
263 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'>#))" : "") . ")
264 " . implode("\n\t\t", $querybuild) . "
268 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query);
270 $search = $db->query($runquery);
272 $numrows = $db->num_rows($search);
276 $message->error(_('No search results were returned that matched your criteria.'));
279 while ($result = $db->fetch_array($search))
281 $ids[] = $result['bugid'];
282 $results[] = $result;
285 if ($bugsys->userinfo
['userid'])
287 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = " . $bugsys->userinfo
['userid'] . " AND name IS NULL");
291 INSERT INTO " . TABLE_PREFIX
. "search
292 (userid, dateline, query, ids, orderby, hilight, resultcount)
294 (" . $bugsys->userinfo
['userid'] . ",
295 " . TIMENOW
. ", '" . $bugsys->escape($query) . "',
296 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
297 '" . $bugsys->escape($hilight) . "',
298 " . sizeof($results) . "
302 $searchid = $db->insert_id();
305 $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
307 $_POST['do'] = 'results';
310 // ###################################################################
312 if ($_REQUEST['do'] == 'search')
314 if ($bugsys->userinfo
['userid'] AND !$bugsys->in
['new'])
316 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . $bugsys->userinfo
['userid']))
318 $_POST['do'] = 'results';
319 $searchid = $cachedsearch['searchid'];
326 else if ($bugsys->in
['searchid'] AND !$bugsys->in
['new'])
328 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']))
330 $_POST['do'] = 'results';
331 $searchid = $cachedsearch['searchid'];
345 if (!is_array($bugsys->datastore
['product']))
347 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
350 if (!is_array($bugsys->datastore
['version']))
352 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
355 $productSelect = ConstructProductSelect();
357 // -------------------------------------------------------------------
359 $fields = construct_custom_fields(null
, true
);
361 foreach ($fields AS $field)
365 $customfields['left'] .= $field;
369 $customfields['right'] .= $field;
374 // -------------------------------------------------------------------
376 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
377 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
378 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
379 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
382 foreach ($bugsys->datastore
['assignto'] AS $dev)
384 $value = $dev['userid'];
385 $label = construct_user_display($dev, false
);
386 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
389 eval('$template->flush("' . $template->fetch('search') . '");');
393 // ###################################################################
395 if ($_REQUEST['do'] == 'export')
397 if (!$bugsys->in
['searchid'] AND $bugsys->userinfo
['userid'])
399 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . $bugsys->userinfo
['userid']);
401 else if ($bugsys->in
['searchid'])
403 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
407 $message->error(_('The search results are trying to export are invalid. Please start over <a href="search.php?new=1">here</a> and try again.'));
412 $message->error(_('Your search has expired because it is older than one hour. Please start over <a href="search.php?new=1">here</a>.'));
415 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby]");
417 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
419 <bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo
, false
)) . '" date="' . $datef->format('r', TIMENOW
) . '">';
421 while ($bug = $db->fetch_array($bugs))
425 ProcessBugDataForDisplay(&$bug);
427 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
428 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
431 $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE userid = $bug[userid]"), false
) . "</reporter>";
433 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
434 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
435 if ($bug['component'])
437 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
439 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
440 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
441 if ($bug['assignedto'])
443 $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore
['assignto']["$bug[assignedto]"], false
) . "</assignedTo>";
445 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
446 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
447 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
449 $xml .= "\n\t</bug>";
452 $xml .= "\n</bugdarExport>";
454 $funct->download_file($xml, _('bugdar-search-results.xml'), true
);
457 // ###################################################################
459 if ($_POST['do'] == 'results')
461 $show['cached'] = false
;
462 if ($searchid AND !$justprocess)
464 $search = $cachedsearch;
465 if ($search['dateline'] < TIMENOW
- 900 OR $bugsys->in
['rerun'])
467 $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']));
468 while ($bug = $db->fetch_array($research))
470 $ids[] = $bug['bugid'];
473 $search['ids'] = implode(',', $ids);
474 $db->query("UPDATE " . TABLE_PREFIX
. "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW
. ", resultcount = " . sizeof($results) . " WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
));
476 $show['cached'] = true
;
477 $hilight = $search['hilight'];
480 LoadPaginationFramework();
481 $pagination->setTotal($search['resultcount']);
482 $pagination->splitPages();
484 $sort = new ListSorter('search');
487 $search = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
488 while ($bug = $db->fetch_array($search))
490 $funct->exec_swap_bg($stylevar['alt_color'], '');
491 ProcessBugDataForDisplay(&$bug, $funct->bgcolour
);
492 $bugs .= $sort->constructRow($bug, "&hilight=$hilight");
495 $columnHeads = $sort->constructColumnHeaders(false
);
496 $show['pagenav'] = ($pagination->getPageCount() > 1);
497 $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
499 eval('$template->flush("' . $template->fetch('search_results') . '");');
502 /*=====================================================================*\
503 || ###################################################################
506 || ###################################################################
507 \*=====================================================================*/