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
');
42 require_once('./includes
/class_api_error
.php
');
44 if (!can_perform('cansearch
'))
46 $message->errorPermission();
49 define('MODE_ANY
', 1);
50 define('MODE_ALL
', 2);
51 define('MODE_RAW
', 3);
53 $var = $db->queryFirst("SHOW VARIABLES LIKE 'ft_min_word_len
'");
54 define('SEARCH_WORD_MIN
', $var['Value
']);
56 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = 0 AND dateline < " . (TIMENOW - 3600));
58 $show['search
'] = true;
60 // ###################################################################
62 if (empty($_REQUEST['do']))
64 $_REQUEST['do'] = 'search
';
67 // ###################################################################
69 if ($_REQUEST['do'] == 'search
')
71 if ($input->in['new'])
75 else if ($input->in['searchid
'])
77 $cachedsearch = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid
', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid
']);
79 else if (bugdar::$userinfo['userid
'])
81 $cachedsearch = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . bugdar::$userinfo['userid
']);
90 $show['cached
'] = true;
91 if ($cachedsearch['dateline
'] < TIMENOW - 900 OR $input->in['rerun
'])
93 $_REQUEST['do'] = 'process
';
94 $input->in = array_merge(unserialize($cachedsearch['query
']), $input->in);
95 BSApp::debug('rerunning the search
');
99 $search = $cachedsearch;
100 $_POST['do'] = 'results
';
110 if (!is_array(bugdar::$datastore['product
']))
112 $message->error(T('No products are setup
, therefore there can be no bugs
and thus search cannot
function.'));
115 if (!is_array(bugdar::$datastore['version
']))
117 $message->error(T('No versions have been added underneath your
product(s
), there can be no bugs
and thus search cannot
function.'));
120 // -------------------------------------------------------------------
122 $fields = construct_custom_fields(null, true, false, true);
124 foreach ($fields AS $field)
128 $customfields['left
'] .= $field;
132 $customfields['right
'] .= $field;
137 // -------------------------------------------------------------------
139 $select['severity
'] = construct_datastore_select('severity
', 'severity
', 'severityid
');
140 $select['priority
'] = construct_datastore_select('priority
', 'priority
', 'priorityid
');
141 $select['status
'] = construct_datastore_select('status
', 'status
', 'statusid
');
142 $select['resolution
'] = construct_datastore_select('resolution
', 'resolution
', 'resolutionid
');
145 if (bugdar::$userinfo['userid
'])
147 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . bugdar::$userinfo['userid
']);
148 foreach ($searchesFetch as $search)
150 $tpl = new BSTemplate('selectoption
');
152 'value
' => $search['searchid
'],
153 'label
' => $search['name
']
155 $searches .= $tpl->evaluate()->getTemplate();
160 foreach (bugdar::$datastore['assignto
'] AS $dev)
162 $tpl = new BSTemplate('selectoption
');
164 'value
' => $dev['userid
'],
165 'label
' => construct_user_display($dev, false)
167 $select['dev
'] .= $tpl->evaluate()->getTemplate();
170 $tpl = new BSTemplate('search
');
173 'productSelect
' => ConstructProductSelect(),
174 'customfields
' => $customfields,
175 'searches
' => $searches
177 $tpl->evaluate()->flush();
181 // ###################################################################
183 if ($_REQUEST['do'] == 'process
')
185 // -------------------------------------------------------------------
187 if ($input->in['summary
'])
189 $keywords = preg_split('#\s+#', $input->in['summary']);
191 // TODO - need to have some str to bool conversions
193 foreach ($keywords AS $word)
195 if (strlen($word) < SEARCH_WORD_MIN
)
200 if ($input->in
['mode'] == MODE_ALL
)
202 $querybuild['text'] .= " +$word";
206 $querybuild['text'] .= " $word";
209 if (!preg_match('#-(.+?)#', trim($word)))
211 $hilight .= " $word";
215 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
216 $hilight = trim($hilight);
217 $hilight = preg_replace('#\s#', '+', $hilight);
219 $temp = trim($querybuild['text']);
221 if ($input->in['mode'] == MODE_ALL OR $input->in['mode'] == MODE_RAW)
223 $bool_flag = ' IN BOOLEAN MODE';
226 $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)";
229 // -------------------------------------------------------------------
231 if ($input->in['reporter'])
233 // force email or name?? make a distinction?
234 // more elegant way to do this? probably
235 $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
')) . "%'");
238 $querybuild['reporter'] = "AND bug
.userid
= $user[userid
] OR comment
.userid
= $user[userid
]";
242 // -------------------------------------------------------------------
243 // product/component/version stuff
244 if (is_array($input->in['product']))
246 foreach ($input->in['product'] AS $prod)
248 $product = explode(',', $prod);
249 $product = $input->clean($product, TYPE_UINT);
250 $products[] = $product[0];
251 $components[] = $product[1];
252 $versions[] = $product[2];
254 $querybuild['pcv'] = "AND bug
.product
IN (" . implode(',', $products) . ") AND bug
.component
IN (" . implode(',', $components) . ") AND bug
.version
IN (" . implode(',', $versions) . ")";
257 // -------------------------------------------------------------------
258 // severity, priority, status, resolution, assignedto
261 if ($input->in['severity'])
263 $input->inputClean('severity', TYPE_UINT);
264 $querybuild['severity'] = "AND bug
.severity
IN (" . implode(',', $input->in['severity']) . ")";
268 if ($input->in['priority'])
270 $input->inputClean('priority', TYPE_UINT);
271 $querybuild['priority'] = "AND bug
.priority
IN (" . implode(',', $input->in['priority']) . ")";
275 if ($input->in['status'])
277 $input->inputClean('status', TYPE_UINT);
278 $querybuild['status'] = "AND bug
.status
IN (" . implode(',', $input->in['status']) . ")";
282 if ($input->in['resolution'])
284 $input->inputClean('resolution', TYPE_UINT);
285 $querybuild['resolution'] = "AND bug
.resolution
IN (" . implode(',', $input->in['resolution']) . ")";
289 if ($input->in['assignedto'])
291 $input->inputClean('assignedto', TYPE_UINT);
292 $querybuild['assignedto'] = "AND bug
.assignedto
IN (" . implode(',', $input->in['assignedto']) . ")";
295 // -------------------------------------------------------------------
297 if ($input->in['date'])
299 // now - (seconds/day * number of days)
300 $dateline = time() - ($input->inputClean('date', TYPE_INT) * 3600);
301 $querybuild['date'] = "AND bug
.dateline
>= $dateline";
304 // -------------------------------------------------------------------
306 $input->inputClean('favorite', TYPE_INT
);
307 if ($input->in
['favorite'] != 0 AND bugdar
::$userinfo['userid'])
309 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . bugdar
::$userinfo['userid']);
310 foreach ($favorites as $favorite)
312 $ids[] = $favorite['bugid'];
314 $querybuild['favorites'] = "AND bug.bugid " . ($input->in
['favorite'] > 0 ? "IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
317 // -------------------------------------------------------------------
319 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
320 $orderby = array('ASC', 'DESC');
321 $input->in
['orderby'] = strtoupper($input->in
['orderby']);
322 if (in_array($input->in
['sortby'], $sortby) AND in_array($input->in
['orderby'], $orderby))
324 $sortclause = "ORDER BY " . $input->in
['sortby'] . ' ' . $input->in
['orderby'];
326 else if ($input->in
['sortby'] == 'relevance')
335 // -------------------------------------------------------------------
337 $fields_fetch = $db->query("
338 SELECT bugfield.*, MAX(permission.mask) AS mask
339 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
340 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
341 ON (bugfield.fieldid = permission.fieldid)
343 AND permission.usergroupid IN (" . bugdar
::$userinfo['usergroupid'] . (sizeof(bugdar
::$userinfo['groupids']) != 0 ? ',' . implode(',', bugdar
::$userinfo['groupids']) : '') . ")
344 AND bugfield.cansearch = 1
345 GROUP BY (bugfield.fieldid)"
347 foreach ($fields_fetch as $field)
349 if (!empty($input->in
["custom$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($input->in
["custom$field[fieldid]"])))
351 if ($field['type'] == 'input_checkbox' AND $input->inputClean("custom$field[fieldid]", TYPE_INT
) != 0)
353 $querybuild[] = "AND bug.custom$field[fieldid] = " . ($input->in
["custom$field[fieldid]"] > 0 ? 1 : 0);
355 else if ($field['type'] == 'input_text')
357 $querybuild[] = "AND bug.custom$field[fieldid] LIKE '%" . $input->in
["custom$field[fieldid]"] . "%'";
359 else if ($field['type'] == 'select_single' AND $input->in
["custom$field[fieldid]"] != -1)
361 $temp = unserialize($field['selects']);
362 $querybuild[] = "AND bug.custom$field[fieldid] = '" . trim($temp[ intval($input->in
["custom$field[fieldid]"]) ]) . "'";
367 // -------------------------------------------------------------------
368 // have to search something
369 if (sizeof($querybuild) < 1)
371 $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
));
374 // -------------------------------------------------------------------
377 $search = $db->query("
378 SELECT bug.*, comment.commentid
379 FROM " . TABLE_PREFIX
. "bug AS bug
380 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
381 ON (bug.bugid = comment.bugid)
383 AND bug.product IN (" . fetch_on_bits('canviewbugs') . ")
388 (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ? "
390 (bug.hidden AND bug.userid = " . bugdar
::$userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewonhidden') . "))" : "") . "
392 " . implode("\n\t\t", $querybuild) . "
397 $numrows = $search->size();
401 $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.'));
404 foreach ($search as $result)
406 $ids[] = $result['bugid'];
407 $results[] = $result;
410 if (bugdar
::$userinfo['userid'] AND !$cachedsearch AND !$input->in
['rerun'])
412 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = " . bugdar
::$userinfo['userid'] . " AND name IS NULL");
415 // store the search params
416 $params = $input->in
;
417 foreach ($_COOKIE AS $key => $value)
419 unset($params["$key"]);
424 $search = $cachedsearch;
425 $search['ids'] = implode(',', $ids);
426 $search['resultcount'] = sizeof($results);
427 $db->query("UPDATE
" . TABLE_PREFIX . "search SET ids
= '$search[ids]', resultcount
= $search[resultcount
], dateline
= " . TIMENOW . " WHERE searchid
= " . $cachedsearch['searchid']);
432 INSERT INTO
" . TABLE_PREFIX . "search
433 (userid
, dateline
, query
, ids
, orderby
, hilight
, resultcount
)
435 (" . bugdar::$userinfo['userid'] . ",
436 " . TIMENOW . ", '" . $input->escape(serialize($params)) . "',
437 '" . implode(',', $ids) . "', '" . $input->escape($sortclause) . "',
438 '" . $input->escape($hilight) . "',
439 " . sizeof($results) . "
442 $search = array('searchid' => $db->insertId(), 'ids' => implode(',', $ids), 'orderby' => $sortclause, 'hilight' => $hilight, 'resultcount' => sizeof($results));
445 $_POST['do'] = 'results';
448 // ###################################################################
450 if ($_REQUEST['do'] == 'update')
452 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE searchid
= " . $input->inputClean('searchid', TYPE_UINT) . " AND userid
= " . bugdar::$userinfo['userid']);
453 if (!$search OR !can_perform('caneditother'))
455 $message->errorPermission();
458 $show['update'] = true;
460 // -------------------------------------------------------------------
462 $fields = construct_custom_fields(null, true, false, true);
464 foreach ($fields AS $field)
468 $customfields['left'] .= $field;
472 $customfields['right'] .= $field;
477 // -------------------------------------------------------------------
479 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, 0);
480 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, 0);
481 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, 0);
482 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, 0);
484 $tpl = new BSTemplate('selectoption');
490 $select['dev'] = $tpl->evaluate()->getTemplate();
492 foreach (bugdar::$datastore['assignto'] as $dev)
494 $tpl = new BSTemplate('selectoption');
496 'value' => $dev['userid'],
497 'label' => construct_user_display($dev, false)
499 $select['dev'] .= $tpl->evaluate()->getTemplate();
502 $tpl = new BSTemplate('search_update');
506 'customfields' => $customfields,
507 'productSelect' => ConstructProductSelect()
509 $tpl->evaluate()->flush();
512 // ###################################################################
514 if ($_POST['do'] == 'doupdate')
516 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE searchid
= " . $input->inputClean('searchid', TYPE_UINT) . " AND userid
= " . bugdar::$userinfo['userid']);
517 if (!$search OR !can_perform('caneditother'))
519 $message->errorPermission();
522 // find all the bugs that we can edit
524 SELECT
* FROM
" . TABLE_PREFIX . "bug
525 WHERE bugid
IN ($search[ids
])
526 AND product
IN (" . fetch_on_bits('canviewbugs') . ")
529 product
IN (" . fetch_on_bits('caneditother') . ")
531 (userid
= " . bugdar::$userinfo['userid'] . " AND product
IN (" . fetch_on_bits('caneditown') . "))
534 foreach ($bugs as $bug)
536 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']))
541 $api = new BugApi($bugsys);
542 $api->set('bugid', $bug['bugid']);
545 $log = new Logging();
546 $log->set_bugid($bug['bugid']);
547 $log->add_data(true, $bug, $log->getCommonFields(), true);
549 if ($input->in['status'] AND can_perform('canchangestatus', $bug['product']))
551 $api->set('status', $input->in['status']);
553 if ($input->in['priority'] AND can_perform('canchangestatus', $bug['product']))
555 $api->set('priority', $input->in['priority']);
557 if ($input->in['severity'])
559 $api->set('severity', $input->in['severity']);
561 if ($input->in['resolution'] AND can_perform('canchangestatus', $bug['product']))
563 $api->set('resolution', $input->in['resolution']);
565 if ($input->in['assignedto'] AND can_perform('canassign', $bug['product']))
567 $api->set('assignedto', $input->in['assignedto']);
569 if ($input->in['product'])
571 $product = explode(',', $input->in['product']);
572 $api->set('product', $product[0]);
573 $api->set('component', $product[1]);
574 $api->set('version', $product[2]);
577 process_custom_fields($api, $message, false, true);
579 $log->add_data(false, $api->values, $log->getCommonFields(), true);
582 $log->update_history();
585 $message->redirect(T('The specified bugs have been updated and you will now return to your search results.'), 'search.php?searchid=' . $input->in['searchid']);
588 // ###################################################################
590 if ($_REQUEST['do'] == 'export')
592 if (!$input->in['searchid'] AND bugdar::$userinfo['userid'])
594 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE name IS
NULL AND userid
= " . bugdar::$userinfo['userid']);
596 else if ($input->in['searchid'])
598 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE searchid
= " . $input->inputClean('searchid', TYPE_UINT) . " AND userid
= " . bugdar::$userinfo['userid']);
602 $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.'));
607 $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>.'));
610 $bugs = $db->query("SELECT
* FROM
" . TABLE_PREFIX . "bug WHERE bugid
IN ($search[ids
]) $search[orderby
]");
612 $xml = '<?xml version="1.0" encoding="' . $language['charset
'] . '"?>
614 <bugdarExport user="' . $input->unsanitize(construct_user_display(bugdar::$userinfo, false)) . '" date="' . $datef->format('r
', TIMENOW) . '" searchid="' . $search['searchid
'] . '">';
616 foreach ($bugs as $bug)
620 $bug = ProcessBugDataForDisplay($bug);
622 $xml .= "\n\t\t
<id
>" . $bug['bugid'] . "</id
>";
623 $xml .= "\n\t\t
<dateReported
>" . $datef->format('r', $bug['dateline']) . "</dateReported
>";
626 $xml .= "\n\t\t
<reporter
>" . construct_user_display($db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "user WHERE userid
= $bug[userid
]"), false) . "</reporter
>";
628 $xml .= "\n\t\t
<summary
>" . $bug['summary'] . "</summary
>";
629 $xml .= "\n\t\t
<product
>" . $bug['product'] . "</product
>";
630 if ($bug['component'])
632 $xml .= "\n\t\t
<component
>" . $bug['component'] . "</component
>";
634 $xml .= "\n\t\t
<version
>" . $bug['version'] . "</version
>";
635 $xml .= "\n\t\t
<status
>" . $bug['status'] . "</status
>";
636 if ($bug['assignedto'])
638 $xml .= "\n\t\t
<assignedTo
>" . construct_user_display(bugdar::$datastore['assignto']["$bug[assignedto
]"], false) . "</assignedTo
>";
640 $xml .= "\n\t\t
<resolution
>" . $bug['resolution'] . "</resolution
>";
641 $xml .= "\n\t\t
<severity
>" . $bug['severity'] . "</severity
>";
642 $xml .= "\n\t\t
<priority
>" . $bug['priority'] . "</priority
>";
644 $xml .= "\n\t
</bug
>";
647 $xml .= "\n
</bugdarExport
>";
649 BSFunctions::download_file($xml, T('bugdar-search-' . $search['searchid'] . '.xml'), true);
652 // ###################################################################
654 if ($_POST['do'] == 'dosave')
656 $search = $db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "search WHERE searchid
= " . $input->inputClean('searchid', TYPE_UINT) . " AND userid
= " . bugdar::$userinfo['userid']);
659 $message->addError(T('The search does not exist.'));
663 $message->addError(sprintf(T('This search has already been named "%
1$s".'), $search['name
']));
665 if (empty($input->in['name
']))
667 $message->addError(T('The name cannot be
empty.'));
670 if (!$message->hasErrors())
672 $db->query("UPDATE " . TABLE_PREFIX . "search SET name = '" . $input->inputEscape('name') . "' WHERE searchid = " . $input->in['searchid
']);
673 $message->redirect(T('Your search has been saved
.'), 'search
.php
?searchid
=' . $search['searchid
']);
677 $_REQUEST['do'] = 'save
';
678 $show['errors
'] = true;
682 // ###################################################################
684 if ($_REQUEST['do'] == 'save
')
686 if (!bugdar::$userinfo['userid
'])
688 $message->errorPermission();
691 $tpl = new BSTemplate('search_save
');
693 'message
' => $message,
694 'searchid
' => $input->in['searchid
'],
695 'name
' => $input->in['name
']
697 $tpl->evaluate()->flush();
700 // ###################################################################
702 if ($_POST['do'] == 'results
')
706 $message->error(T('No bugs matched your search criteria
. Please
<a href
="search.php?new=1">try again
</a
> with different search requirements
.'));
709 $searchid = $search['searchid
'];
710 $hilight = $search['hilight
'];
712 require_once 'includes
/pagination
.php
';
713 $pagination = new Pagination();
714 $pagination->processIncomingData();
715 $pagination->setTotal($search['resultcount
']);
716 $pagination->splitPages();
718 $sort = new ListSorter('search
');
720 $show['save
'] = (bugdar::$userinfo['userid
'] AND !$search['name
']);
721 $show['update
'] = can_perform('caneditother
');
724 $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
725 foreach ($search as $bug)
727 BSFunctions::swap_css_classes('altcolor
', '');
728 $bug = ProcessBugDataForDisplay($bug, BSFunctions::$cssClass);
729 $bugs .= $sort->constructRow($bug, "&hilight=$hilight");
732 $columnHeads = $sort->constructColumnHeaders(false);
733 $show['pagenav
'] = ($pagination->getPageCount() > 1);
734 $pagenav = $pagination->constructPageNav('search
.php
?searchid
=' . $searchid);
736 $tpl = new BSTemplate('search_results
');
738 'columnHeads
' => $columnHeads,
740 'searchid
' => $searchid
742 $tpl->evaluate()->flush();
745 /*=====================================================================*\
746 || ###################################################################
749 || ###################################################################
750 \*=====================================================================*/