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(
31 define('SVN', '$Id$');
33 $focus['search'] = 'focus';
35 require_once('./global.php');
36 require_once('./includes/functions_product.php');
37 require_once('./includes/class_sort.php');
39 if (!can_perform('cansearch'))
41 $message->error_permission();
44 define('MODE_ANY', ($bugsys->in
['mode'] == 1));
45 define('MODE_ALL', ($bugsys->in
['mode'] == 2));
46 define('MODE_RAW', ($bugsys->in
['mode'] == 3));
48 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
49 define('SEARCH_WORD_MIN', $var['Value']);
51 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = 0 AND dateline < " . (TIMENOW
- 3600));
53 $show['search'] = true
;
55 // ###################################################################
57 if (empty($_REQUEST['do']))
59 $_REQUEST['do'] = 'search';
62 // ###################################################################
64 if ($_REQUEST['do'] == 'process')
66 // -------------------------------------------------------------------
68 if ($bugsys->in
['summary'])
70 $keywords = preg_split('#\s+#', $bugsys->in
['summary']);
72 // TODO - need to have some str to bool conversions
74 foreach ($keywords AS $word)
76 if (strlen($word) < SEARCH_WORD_MIN
)
83 $querybuild['text'] .= " +$word";
87 $querybuild['text'] .= " $word";
90 if (!preg_match('#-(.+?)#', trim($word)))
96 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
97 $hilight = trim($hilight);
98 $hilight = preg_replace('#\s#', '+', $hilight);
100 $temp = trim($querybuild['text']);
102 if (MODE_ALL
OR MODE_RAW
)
104 $bool_flag = ' IN BOOLEAN MODE';
107 $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)";
110 // -------------------------------------------------------------------
112 if ($bugsys->in
['reporter'])
114 // force email or name?? make a distinction?
115 // more elegant way to do this? probably
116 $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')) . "%'");
119 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
123 // -------------------------------------------------------------------
124 // product/component/version stuff
125 if (is_array($bugsys->in
['product']))
127 foreach ($bugsys->in
['product'] AS $prod)
129 $product = explode(',', $prod);
130 $product = $bugsys->clean($product, TYPE_UINT
);
131 $products[] = $product[0];
132 $components[] = $product[1];
133 $versions[] = $product[2];
135 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
138 // -------------------------------------------------------------------
139 // severity, priority, status, resolution, assignedto
142 if ($bugsys->in
['severity'])
144 $bugsys->input_clean('severity', TYPE_UINT
);
145 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in
['severity']) . ")";
149 if ($bugsys->in
['priority'])
151 $bugsys->input_clean('priority', TYPE_UINT
);
152 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in
['priority']) . ")";
156 if ($bugsys->in
['status'])
158 $bugsys->input_clean('status', TYPE_UINT
);
159 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in
['status']) . ")";
163 if ($bugsys->in
['resolution'])
165 $bugsys->input_clean('resolution', TYPE_UINT
);
166 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in
['resolution']) . ")";
170 if ($bugsys->in
['assignedto'])
172 $bugsys->input_clean('assignedto', TYPE_UINT
);
173 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in
['assignedto']) . ")";
176 // -------------------------------------------------------------------
178 if ($bugsys->in
['date'])
180 // now - (seconds/day * number of days)
181 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT
) * 3600);
182 $querybuild['date'] = "AND bug.dateline >= $dateline";
185 // -------------------------------------------------------------------
187 $bugsys->input_clean('favorite', TYPE_INT
);
188 if ($bugsys->in
['favorite'] != 0 AND $bugsys->userinfo
['userid'])
190 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . $bugsys->userinfo
['userid']);
191 while ($favorite = $db->fetch_array($favorites))
193 $ids[] = $favorite['bugid'];
195 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in
['favorite'] > 0 ?
"IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
198 // -------------------------------------------------------------------
200 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
201 $orderby = array('ASC', 'DESC');
202 $bugsys->in
['orderby'] = strtoupper($bugsys->in
['orderby']);
203 if (in_array($bugsys->in
['sortby'], $sortby) AND in_array($bugsys->in
['orderby'], $orderby))
205 $sortclause = "ORDER BY " . $bugsys->in
['sortby'] . ' ' . $bugsys->in
['orderby'];
207 else if ($bugsys->in
['sortby'] == 'relevance')
216 // -------------------------------------------------------------------
218 $fields_fetch = $bugsys->db
->query("
220 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
221 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
222 ON (bugfield.fieldid = permission.fieldid)
223 WHERE permission.mask <> 0
224 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
225 AND bugfield.cansearch = 1"
227 while ($field = $bugsys->db
->fetch_array($fields_fetch))
229 if (!empty($bugsys->in
["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in
["field$field[fieldid]"])))
231 if ($field['type'] == 'input_checkbox' AND $bugsys->input_clean("field$field[fieldid]", TYPE_INT
) != 0)
233 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in
["field$field[fieldid]"] > 0) ?
1 : 0);
235 else if ($field['type'] == 'input_text')
237 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in
["field$field[fieldid]"] . "%'";
239 else if ($field['type'] == 'select_single' AND $bugsys->in
["field$field[fieldid]"] != -1)
241 $temp = unserialize($field['selects']);
242 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in
["field$field[fieldid]"]) ]) . "'";
247 // -------------------------------------------------------------------
248 // have to search something
249 if (sizeof($querybuild) < 1)
251 $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
));
254 // -------------------------------------------------------------------
257 SELECT bug.*, comment.commentid
258 FROM " . TABLE_PREFIX
. "bug AS bug
259 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
260 ON (bug.bugid = comment.bugid)
261 LEFT JOIN " . TABLE_PREFIX
. "bugvaluefill AS bugfieldvalue
262 ON (bug.bugid = bugfieldvalue.bugid)
264 AND bug.product IN (#<'ONBITS:VIEW'>#)
265 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'>#))" : "") . ")
266 " . implode("\n\t\t", $querybuild) . "
270 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query);
272 $search = $db->query($runquery);
274 $numrows = $db->num_rows($search);
278 $message->error(_('No search results were returned that matched your criteria.'));
281 while ($result = $db->fetch_array($search))
283 $ids[] = $result['bugid'];
284 $results[] = $result;
287 if ($bugsys->userinfo
['userid'])
289 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = " . $bugsys->userinfo
['userid'] . " AND name IS NULL");
293 INSERT INTO " . TABLE_PREFIX
. "search
294 (userid, dateline, query, ids, orderby, hilight, resultcount)
296 (" . $bugsys->userinfo
['userid'] . ",
297 " . TIMENOW
. ", '" . $bugsys->escape($query) . "',
298 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
299 '" . $bugsys->escape($hilight) . "',
300 " . sizeof($results) . "
304 $searchid = $db->insert_id();
307 $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
309 $_POST['do'] = 'results';
312 // ###################################################################
314 if ($_REQUEST['do'] == 'search')
316 if ($bugsys->in
['searchid'] AND !$bugsys->in
['new'])
318 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']))
320 $_POST['do'] = 'results';
321 $searchid = $cachedsearch['searchid'];
328 else if ($bugsys->userinfo
['userid'] AND !$bugsys->in
['new'])
330 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . $bugsys->userinfo
['userid']))
332 $_POST['do'] = 'results';
333 $searchid = $cachedsearch['searchid'];
347 if (!is_array($bugsys->datastore
['product']))
349 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
352 if (!is_array($bugsys->datastore
['version']))
354 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
357 $productSelect = ConstructProductSelect();
359 // -------------------------------------------------------------------
361 $fields = construct_custom_fields(null
, true
, false
, true
);
363 foreach ($fields AS $field)
367 $customfields['left'] .= $field;
371 $customfields['right'] .= $field;
376 // -------------------------------------------------------------------
378 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
379 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
380 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
381 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
384 if ($bugsys->userinfo
['userid'])
386 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NOT NULL AND userid = " . $bugsys->userinfo
['userid']);
387 while ($search = $db->fetch_array($searchesFetch))
389 $value = $search['searchid'];
390 $label = $search['name'];
391 eval('$searches .= "' . $template->fetch('selectoption') . '";');
396 foreach ($bugsys->datastore
['assignto'] AS $dev)
398 $value = $dev['userid'];
399 $label = construct_user_display($dev, false
);
400 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
403 eval('$template->flush("' . $template->fetch('search') . '");');
407 // ###################################################################
409 if ($_REQUEST['do'] == 'export')
411 if (!$bugsys->in
['searchid'] AND $bugsys->userinfo
['userid'])
413 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . $bugsys->userinfo
['userid']);
415 else if ($bugsys->in
['searchid'])
417 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
421 $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.'));
426 $message->error(_('Your search has expired because it is older than one hour. Please start over <a href="search.php?new=1">here</a>.'));
429 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby]");
431 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
433 <bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo
, false
)) . '" date="' . $datef->format('r', TIMENOW
) . '" searchid="' . $search['searchid'] . '">';
435 while ($bug = $db->fetch_array($bugs))
439 ProcessBugDataForDisplay(&$bug);
441 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
442 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
445 $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE userid = $bug[userid]"), false
) . "</reporter>";
447 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
448 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
449 if ($bug['component'])
451 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
453 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
454 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
455 if ($bug['assignedto'])
457 $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore
['assignto']["$bug[assignedto]"], false
) . "</assignedTo>";
459 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
460 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
461 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
463 $xml .= "\n\t</bug>";
466 $xml .= "\n</bugdarExport>";
468 $funct->download_file($xml, _('bugdar-search-' . $search['searchid'] . '.xml'), true
);
471 // ###################################################################
473 if ($_POST['do'] == 'dosave')
475 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
478 $message->add_error(_('The search does not exist.'));
482 $message->add_error(sprintf(_('This search has already been named "%1$s".'), $search['name']));
484 if (empty($bugsys->in
['name']))
486 $message->add_error(_('The name cannot be empty.'));
489 if (!$message->items
)
491 $db->query("UPDATE " . TABLE_PREFIX
. "search SET name = '" . $bugsys->input_escape('name') . "' WHERE searchid = " . $bugsys->in
['searchid']);
492 $message->redirect(_('Your search has been saved.'), 'search.php?searchid=' . $search['searchid']);
496 $message->error_list_process();
497 $_REQUEST['do'] = 'save';
498 $show['errors'] = true
;
502 // ###################################################################
504 if ($_REQUEST['do'] == 'save')
506 if (!$bugsys->userinfo
['userid'])
508 $message->error_permission();
513 eval('$template->flush("' . $template->fetch('search_save') . '");');
516 // ###################################################################
518 if ($_POST['do'] == 'results')
520 $show['cached'] = false
;
521 if ($searchid AND !$justprocess)
523 $search = $cachedsearch;
524 if ($search['dateline'] < TIMENOW
- 900 OR $bugsys->in
['rerun'])
526 $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']));
527 while ($bug = $db->fetch_array($research))
529 $ids[] = $bug['bugid'];
532 $search['ids'] = implode(',', $ids);
533 $db->query("UPDATE " . TABLE_PREFIX
. "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW
. ", resultcount = " . sizeof($results) . " WHERE searchid = " . $search['searchid']);
535 $show['cached'] = true
;
536 $hilight = $search['hilight'];
541 $message->error(_('No bugs matched your search criteria. Please <a href="search.php?new=1">try again</a> with different search requirements.'));
544 LoadPaginationFramework();
545 $pagination->setTotal($search['resultcount']);
546 $pagination->splitPages();
548 $sort = new ListSorter('search');
550 $show['save'] = ($bugsys->userinfo
['userid'] AND !$search['name']);
551 $show['update'] = can_perform('caneditother');
554 $search = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
555 while ($bug = $db->fetch_array($search))
557 $funct->exec_swap_bg($stylevar['alt_color'], '');
558 ProcessBugDataForDisplay(&$bug, $funct->bgcolour
);
559 $bugs .= $sort->constructRow($bug, "&hilight=$hilight");
562 $columnHeads = $sort->constructColumnHeaders(false
);
563 $show['pagenav'] = ($pagination->getPageCount() > 1);
564 $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
566 eval('$template->flush("' . $template->fetch('search_results') . '");');
569 /*=====================================================================*\
570 || ###################################################################
573 || ###################################################################
574 \*=====================================================================*/