2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright 2002-2007 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 2 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(
33 define('SVN', '$Id$');
35 $focus['search
'] = 'focus
';
37 require_once('./global.php
');
38 require_once('./includes
/functions_product
.php
');
39 require_once('./includes
/class_sort
.php
');
40 require_once('./includes
/class_logging
.php
');
41 require_once('./includes
/api_bug
.php
');
43 if (!can_perform('cansearch
'))
45 $message->errorPermission();
48 define('MODE_ANY
', 1);
49 define('MODE_ALL
', 2);
50 define('MODE_RAW
', 3);
52 $var = $db->queryFirst("SHOW VARIABLES LIKE 'ft_min_word_len
'");
53 define('SEARCH_WORD_MIN
', $var['Value
']);
55 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = 0 AND dateline < " . (TIMENOW - 3600));
57 $show['search
'] = true;
59 // ###################################################################
61 if (empty($_REQUEST['do']))
63 $_REQUEST['do'] = 'search
';
66 // ###################################################################
68 if ($_REQUEST['do'] == 'search
')
70 if ($input->in['new'])
74 else if ($input->in['searchid
'])
76 $cachedsearch = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid
', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid
']);
78 else if (bugdar::$userinfo['userid
'])
80 $cachedsearch = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . bugdar::$userinfo['userid
']);
89 $show['cached
'] = true;
90 if ($cachedsearch['dateline
'] < TIMENOW - 900 OR $input->in['rerun
'])
92 $_REQUEST['do'] = 'process
';
93 $input->in = array_merge(unserialize($cachedsearch['query
']), $input->in);
94 BSApp::debug('rerunning the search
');
98 $search = $cachedsearch;
99 $_POST['do'] = 'results
';
109 if (!is_array(bugdar::$datastore['product
']))
111 $message->error(T('No products are setup
, therefore there can be no bugs
and thus search cannot
function.'));
114 if (!is_array(bugdar::$datastore['version
']))
116 $message->error(T('No versions have been added underneath your
product(s
), there can be no bugs
and thus search cannot
function.'));
119 // -------------------------------------------------------------------
121 $fields = construct_custom_fields(null, true, false, true);
123 foreach ($fields AS $field)
127 $customfields['left
'] .= $field;
131 $customfields['right
'] .= $field;
136 // -------------------------------------------------------------------
138 $select['severity
'] = construct_datastore_select('severity
', 'severity
', 'severityid
');
139 $select['priority
'] = construct_datastore_select('priority
', 'priority
', 'priorityid
');
140 $select['status
'] = construct_datastore_select('status
', 'status
', 'statusid
');
141 $select['resolution
'] = construct_datastore_select('resolution
', 'resolution
', 'resolutionid
');
144 if (bugdar::$userinfo['userid
'])
146 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . bugdar::$userinfo['userid
']);
147 foreach ($searchesFetch as $search)
149 $tpl = new BSTemplate('selectoption
');
151 'value
' => $search['searchid
'],
152 'label
' => $search['name
']
154 $searches .= $tpl->evaluate()->getTemplate();
159 foreach (bugdar::$datastore['assignto
'] AS $dev)
161 $tpl = new BSTemplate('selectoption
');
163 'value
' => $dev['userid
'],
164 'label
' => construct_user_display($dev, false)
166 $select['dev
'] .= $tpl->evaluate()->getTemplate();
169 $tpl = new BSTemplate('search
');
172 'productSelect
' => ConstructProductSelect(),
173 'customfields
' => $customfields,
174 'searches
' => $searches
176 $tpl->evaluate()->flush();
180 // ###################################################################
182 if ($_REQUEST['do'] == 'process
')
184 // -------------------------------------------------------------------
186 if ($input->in['summary
'])
188 $keywords = preg_split('#\s+#', $input->in['summary']);
190 // TODO - need to have some str to bool conversions
192 foreach ($keywords AS $word)
194 if (strlen($word) < SEARCH_WORD_MIN
)
199 if ($input->in
['mode'] == MODE_ALL
)
201 $querybuild['text'] .= " +$word";
205 $querybuild['text'] .= " $word";
208 if (!preg_match('#-(.+?)#', trim($word)))
210 $hilight .= " $word";
214 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
215 $hilight = trim($hilight);
216 $hilight = preg_replace('#\s#', '+', $hilight);
218 $temp = trim($querybuild['text']);
220 if ($input->in['mode'] == MODE_ALL OR $input->in['mode'] == MODE_RAW)
222 $bool_flag = ' IN BOOLEAN MODE';
225 $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)";
228 // -------------------------------------------------------------------
230 if ($input->in['reporter'])
232 // force email or name?? make a distinction?
233 // more elegant way to do this? probably
234 $user = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "user WHERE email LIKE
'%" . str_replace('%
', '\%
', $bugsys->input_escape('reporter
')) . "' OR displayname LIKE
'%" . str_replace('%
', '\%
', $bugsys->input_escape('reporter
')) . "%'");
237 $querybuild['reporter'] = "AND bug
.userid
= $user[userid
] OR comment
.userid
= $user[userid
]";
241 // -------------------------------------------------------------------
242 // product/component/version stuff
243 if (is_array($input->in['product']))
245 foreach ($input->in['product'] AS $prod)
247 $product = explode(',', $prod);
248 $product = $input->clean($product, TYPE_UINT);
249 $products[] = $product[0];
250 $components[] = $product[1];
251 $versions[] = $product[2];
253 $querybuild['pcv'] = "AND bug
.product
IN (" . implode(',', $products) . ") AND bug
.component
IN (" . implode(',', $components) . ") AND bug
.version
IN (" . implode(',', $versions) . ")";
256 // -------------------------------------------------------------------
257 // severity, priority, status, resolution, assignedto
260 if ($input->in['severity'])
262 $input->inputClean('severity', TYPE_UINT);
263 $querybuild['severity'] = "AND bug
.severity
IN (" . implode(',', $input->in['severity']) . ")";
267 if ($input->in['priority'])
269 $input->inputClean('priority', TYPE_UINT);
270 $querybuild['priority'] = "AND bug
.priority
IN (" . implode(',', $input->in['priority']) . ")";
274 if ($input->in['status'])
276 $input->inputClean('status', TYPE_UINT);
277 $querybuild['status'] = "AND bug
.status
IN (" . implode(',', $input->in['status']) . ")";
281 if ($input->in['resolution'])
283 $input->inputClean('resolution', TYPE_UINT);
284 $querybuild['resolution'] = "AND bug
.resolution
IN (" . implode(',', $input->in['resolution']) . ")";
288 if ($input->in['assignedto'])
290 $input->inputClean('assignedto', TYPE_UINT);
291 $querybuild['assignedto'] = "AND bug
.assignedto
IN (" . implode(',', $input->in['assignedto']) . ")";
294 // -------------------------------------------------------------------
296 if ($input->in['date'])
298 // now - (seconds/day * number of days)
299 $dateline = time() - ($input->inputClean('date', TYPE_INT) * 3600);
300 $querybuild['date'] = "AND bug
.dateline
>= $dateline";
303 // -------------------------------------------------------------------
305 $input->inputClean('favorite', TYPE_INT
);
306 if ($input->in
['favorite'] != 0 AND bugdar
::$userinfo['userid'])
308 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . bugdar
::$userinfo['userid']);
309 foreach ($favorites as $favorite)
311 $ids[] = $favorite['bugid'];
313 $querybuild['favorites'] = "AND bug.bugid " . ($input->in
['favorite'] > 0 ? "IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
316 // -------------------------------------------------------------------
318 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
319 $orderby = array('ASC', 'DESC');
320 $input->in
['orderby'] = strtoupper($input->in
['orderby']);
321 if (in_array($input->in
['sortby'], $sortby) AND in_array($input->in
['orderby'], $orderby))
323 $sortclause = "ORDER BY " . $input->in
['sortby'] . ' ' . $input->in
['orderby'];
325 else if ($input->in
['sortby'] == 'relevance')
334 // -------------------------------------------------------------------
336 $fields_fetch = $db->query("
337 SELECT bugfield.*, MAX(permission.mask) AS mask
338 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
339 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
340 ON (bugfield.fieldid = permission.fieldid)
342 AND permission.usergroupid IN (" . bugdar
::$userinfo['usergroupid'] . (sizeof(bugdar
::$userinfo['groupids']) != 0 ? ',' . implode(',', bugdar
::$userinfo['groupids']) : '') . ")
343 AND bugfield.cansearch = 1
344 GROUP BY (bugfield.fieldid)"
346 foreach ($fields_fetch as $field)
348 if (!empty($input->in
["custom$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($input->in
["custom$field[fieldid]"])))
350 if ($field['type'] == 'input_checkbox' AND $input->inputClean("custom$field[fieldid]", TYPE_INT
) != 0)
352 $querybuild[] = "AND bug.custom$field[fieldid] = " . ($input->in
["custom$field[fieldid]"] > 0 ? 1 : 0);
354 else if ($field['type'] == 'input_text')
356 $querybuild[] = "AND bug.custom$field[fieldid] LIKE '%" . $input->in
["custom$field[fieldid]"] . "%'";
358 else if ($field['type'] == 'select_single' AND $input->in
["custom$field[fieldid]"] != -1)
360 $temp = unserialize($field['selects']);
361 $querybuild[] = "AND bug.custom$field[fieldid] = '" . trim($temp[ intval($input->in
["custom$field[fieldid]"]) ]) . "'";
366 // -------------------------------------------------------------------
367 // have to search something
368 if (sizeof($querybuild) < 1)
370 $message->error(sprintf(T('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
));
373 // -------------------------------------------------------------------
376 $search = $db->query("
377 SELECT bug.*, comment.commentid
378 FROM " . TABLE_PREFIX
. "bug AS bug
379 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
380 ON (bug.bugid = comment.bugid)
382 AND bug.product IN (" . fetch_on_bits('canviewbugs') . ")
387 (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ? "
389 (bug.hidden AND bug.userid = " . bugdar
::$userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewonhidden') . "))" : "") . "
391 " . implode("\n\t\t", $querybuild) . "
396 $numrows = $search->size();
400 $message->error(T('No search results were returned that matched your criteria. Please <a href="search.php?new=1">try again</a> with different search requirements.'));
403 foreach ($search as $result)
405 $ids[] = $result['bugid'];
406 $results[] = $result;
409 if (bugdar
::$userinfo['userid'] AND !$cachedsearch AND !$input->in
['rerun'])
411 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = " . bugdar
::$userinfo['userid'] . " AND name IS NULL");
414 // store the search params
415 $params = $input->in
;
416 foreach ($_COOKIE AS $key => $value)
418 unset($params["$key"]);
423 $search = $cachedsearch;
424 $search['ids'] = implode(',', $ids);
425 $search['resultcount'] = sizeof($results);
426 $db->query("UPDATE
" . TABLE_PREFIX . "search SET ids
= '$search[ids]', resultcount
= $search[resultcount
], dateline
= " . TIMENOW . " WHERE searchid
= " . $cachedsearch['searchid']);
431 INSERT INTO
" . TABLE_PREFIX . "search
432 (userid
, dateline
, query
, ids
, orderby
, hilight
, resultcount
)
434 (" . bugdar::$userinfo['userid'] . ",
435 " . TIMENOW . ", '" . $input->escape(serialize($params)) . "',
436 '" . implode(',', $ids) . "', '" . $input->escape($sortclause) . "',
437 '" . $input->escape($hilight) . "',
438 " . sizeof($results) . "
441 $search = array('searchid' => $db->insertId(), 'ids' => implode(',', $ids), 'orderby' => $sortclause, 'hilight' => $hilight, 'resultcount' => sizeof($results));
444 $_POST['do'] = 'results';
447 // ###################################################################
449 if ($_REQUEST['do'] == 'update')
451 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE searchid
= " . $input->inputClean('searchid', TYPE_UINT) . " AND userid
= " . bugdar::$userinfo['userid']);
452 if (!$search OR !can_perform('caneditother'))
454 $message->errorPermission();
457 $show['update'] = true;
459 // -------------------------------------------------------------------
461 $fields = construct_custom_fields(null, true, false, true);
463 foreach ($fields AS $field)
467 $customfields['left'] .= $field;
471 $customfields['right'] .= $field;
476 // -------------------------------------------------------------------
478 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, 0);
479 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, 0);
480 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, 0);
481 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, 0);
483 $tpl = new BSTemplate('selectoption');
489 $select['dev'] = $tpl->evaluate()->getTemplate();
491 foreach (bugdar::$datastore['assignto'] as $dev)
493 $tpl = new BSTemplate('selectoption');
495 'value' => $dev['userid'],
496 'label' => construct_user_display($dev, false)
498 $select['dev'] .= $tpl->evaluate()->getTemplate();
501 $tpl = new BSTemplate('search_update');
505 'customfields' => $customfields,
506 'productSelect' => ConstructProductSelect()
508 $tpl->evaluate()->flush();
511 // ###################################################################
513 if ($_POST['do'] == 'doupdate')
515 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE searchid
= " . $input->inputClean('searchid', TYPE_UINT) . " AND userid
= " . bugdar::$userinfo['userid']);
516 if (!$search OR !can_perform('caneditother'))
518 $message->errorPermission();
521 // find all the bugs that we can edit
523 SELECT
* FROM
" . TABLE_PREFIX . "bug
524 WHERE bugid
IN ($search[ids
])
525 AND product
IN (" . fetch_on_bits('canviewbugs') . ")
528 product
IN (" . fetch_on_bits('caneditother') . ")
530 (userid
= " . bugdar::$userinfo['userid'] . " AND product
IN (" . fetch_on_bits('caneditown') . "))
533 foreach ($bugs as $bug)
535 if (!((can_perform('caneditown', $bug['product']) AND bugdar::$userinfo['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['product']) AND bugdar::$userinfo['userid'] != $bug['userid'])) AND !can_perform('canpostcomments', $bug['product']))
540 $api = new BugApi($bugsys);
541 $api->set('bugid', $bug['bugid']);
544 $log = new Logging();
545 $log->set_bugid($bug['bugid']);
546 $log->add_data(true, $bug, $log->getCommonFields(), true);
548 if ($input->in['status'] AND can_perform('canchangestatus', $bug['product']))
550 $api->set('status', $input->in['status']);
552 if ($input->in['priority'] AND can_perform('canchangestatus', $bug['product']))
554 $api->set('priority', $input->in['priority']);
556 if ($input->in['severity'])
558 $api->set('severity', $input->in['severity']);
560 if ($input->in['resolution'] AND can_perform('canchangestatus', $bug['product']))
562 $api->set('resolution', $input->in['resolution']);
564 if ($input->in['assignedto'] AND can_perform('canassign', $bug['product']))
566 $api->set('assignedto', $input->in['assignedto']);
568 if ($input->in['product'])
570 $product = explode(',', $input->in['product']);
571 $api->set('product', $product[0]);
572 $api->set('component', $product[1]);
573 $api->set('version', $product[2]);
576 process_custom_fields($api, $message, false, true);
578 $log->add_data(false, $api->values, $log->getCommonFields(), true);
581 $log->update_history();
584 $message->redirect(T('The specified bugs have been updated and you will now return to your search results.'), 'search.php?searchid=' . $input->in['searchid']);
587 // ###################################################################
589 if ($_REQUEST['do'] == 'export')
591 if (!$input->in['searchid'] AND bugdar::$userinfo['userid'])
593 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE name IS
NULL AND userid
= " . bugdar::$userinfo['userid']);
595 else if ($input->in['searchid'])
597 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE searchid
= " . $input->inputClean('searchid', TYPE_UINT) . " AND userid
= " . bugdar::$userinfo['userid']);
601 $message->error(T('The search results are trying to export are invalid. Please start over <a href="search
.php
?new=1">here</a> and try again.'));
606 $message->error(T('Your search has expired because it is older than one hour. Please start over <a href="search
.php
?new=1">here</a>.'));
609 $bugs = $db->query("SELECT
* FROM
" . TABLE_PREFIX . "bug WHERE bugid
IN ($search[ids
]) $search[orderby
]");
611 $xml = '<?xml version="1.0" encoding="' . $language['charset
'] . '"?>
613 <bugdarExport user="' . $input->unsanitize(construct_user_display(bugdar::$userinfo, false)) . '" date="' . $datef->format('r
', TIMENOW) . '" searchid="' . $search['searchid
'] . '">';
615 foreach ($bugs as $bug)
619 $bug = ProcessBugDataForDisplay($bug);
621 $xml .= "\n\t\t
<id
>" . $bug['bugid'] . "</id
>";
622 $xml .= "\n\t\t
<dateReported
>" . $datef->format('r', $bug['dateline']) . "</dateReported
>";
625 $xml .= "\n\t\t
<reporter
>" . construct_user_display($db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "user WHERE userid
= $bug[userid
]"), false) . "</reporter
>";
627 $xml .= "\n\t\t
<summary
>" . $bug['summary'] . "</summary
>";
628 $xml .= "\n\t\t
<product
>" . $bug['product'] . "</product
>";
629 if ($bug['component'])
631 $xml .= "\n\t\t
<component
>" . $bug['component'] . "</component
>";
633 $xml .= "\n\t\t
<version
>" . $bug['version'] . "</version
>";
634 $xml .= "\n\t\t
<status
>" . $bug['status'] . "</status
>";
635 if ($bug['assignedto'])
637 $xml .= "\n\t\t
<assignedTo
>" . construct_user_display(bugdar::$datastore['assignto']["$bug[assignedto
]"], false) . "</assignedTo
>";
639 $xml .= "\n\t\t
<resolution
>" . $bug['resolution'] . "</resolution
>";
640 $xml .= "\n\t\t
<severity
>" . $bug['severity'] . "</severity
>";
641 $xml .= "\n\t\t
<priority
>" . $bug['priority'] . "</priority
>";
643 $xml .= "\n\t
</bug
>";
646 $xml .= "\n
</bugdarExport
>";
648 BSFunctions::download_file($xml, T('bugdar-search-' . $search['searchid'] . '.xml'), true);
651 // ###################################################################
653 if ($_POST['do'] == 'dosave')
655 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE searchid
= " . $input->inputClean('searchid', TYPE_UINT) . " AND userid
= " . bugdar::$userinfo['userid']);
658 $message->addError(T('The search does not exist.'));
662 $message->addError(sprintf(T('This search has already been named "%
1$s".'), $search['name
']));
664 if (empty($input->in['name
']))
666 $message->addError(T('The name cannot be
empty.'));
669 if (!$message->hasErrors())
671 $db->query("UPDATE " . TABLE_PREFIX . "search SET name = '" . $input->inputEscape('name') . "' WHERE searchid = " . $input->in['searchid
']);
672 $message->redirect(T('Your search has been saved
.'), 'search
.php
?searchid
=' . $search['searchid
']);
676 $_REQUEST['do'] = 'save
';
677 $show['errors
'] = true;
681 // ###################################################################
683 if ($_REQUEST['do'] == 'save
')
685 if (!bugdar::$userinfo['userid
'])
687 $message->errorPermission();
690 $tpl = new BSTemplate('search_save
');
692 'message
' => $message,
693 'searchid
' => $input->in['searchid
'],
694 'name
' => $input->in['name
']
696 $tpl->evaluate()->flush();
699 // ###################################################################
701 if ($_POST['do'] == 'results
')
705 $message->error(T('No bugs matched your search criteria
. Please
<a href
="search.php?new=1">try again
</a
> with different search requirements
.'));
708 $searchid = $search['searchid
'];
709 $hilight = $search['hilight
'];
711 require_once 'includes
/pagination
.php
';
712 $pagination = new Pagination();
713 $pagination->processIncomingData();
714 $pagination->setTotal($search['resultcount
']);
715 $pagination->splitPages();
717 $sort = new ListSorter('search
');
719 $show['save
'] = (bugdar::$userinfo['userid
'] AND !$search['name
']);
720 $show['update
'] = can_perform('caneditother
');
723 $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
724 foreach ($search as $bug)
726 BSFunctions::swap_css_classes('altcolor
', '');
727 $bug = ProcessBugDataForDisplay($bug, BSFunctions::$cssClass);
728 $bugs .= $sort->constructRow($bug, "&hilight=$hilight");
731 $columnHeads = $sort->constructColumnHeaders(false);
732 $show['pagenav
'] = ($pagination->getPageCount() > 1);
733 $pagenav = $pagination->constructPageNav('search
.php
?searchid
=' . $searchid);
735 $tpl = new BSTemplate('search_results
');
737 'columnHeads
' => $columnHeads,
739 'searchid
' => $searchid
741 $tpl->evaluate()->flush();
744 /*=====================================================================*\
745 || ###################################################################
748 || ###################################################################
749 \*=====================================================================*/