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 APIError(array($message, 'error'));
46 if (!can_perform('cansearch'))
48 $message->errorPermission();
51 define('MODE_ANY', 1);
52 define('MODE_ALL', 2);
53 define('MODE_RAW', 3);
55 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
56 define('SEARCH_WORD_MIN', $var['Value']);
58 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = 0 AND dateline < " . (TIMENOW
- 3600));
60 $show['search'] = true
;
62 // ###################################################################
64 if (empty($_REQUEST['do']))
66 $_REQUEST['do'] = 'search';
69 // ###################################################################
71 if ($_REQUEST['do'] == 'search')
73 if ($bugsys->in
['new'])
77 else if ($bugsys->in
['searchid'])
79 $cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
81 else if ($bugsys->userinfo
['userid'])
83 $cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . $bugsys->userinfo
['userid']);
92 $show['cached'] = true
;
93 if ($cachedsearch['dateline'] < TIMENOW
- 900 OR $bugsys->in
['rerun'])
95 $_REQUEST['do'] = 'process';
96 $bugsys->in
= array_merge(unserialize($cachedsearch['query']), $bugsys->in
);
97 $bugsys->debug('rerunning the search');
101 $search = $cachedsearch;
102 $_POST['do'] = 'results';
112 if (!is_array($bugsys->datastore
['product']))
114 $message->error(T('No products are setup, therefore there can be no bugs and thus search cannot function.'));
117 if (!is_array($bugsys->datastore
['version']))
119 $message->error(T('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
122 $productSelect = ConstructProductSelect();
124 // -------------------------------------------------------------------
126 $fields = construct_custom_fields(null
, true
, false
, true
);
128 foreach ($fields AS $field)
132 $customfields['left'] .= $field;
136 $customfields['right'] .= $field;
141 // -------------------------------------------------------------------
143 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
144 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
145 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
146 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
149 if ($bugsys->userinfo
['userid'])
151 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NOT NULL AND userid = " . $bugsys->userinfo
['userid']);
152 while ($search = $db->fetch_array($searchesFetch))
154 $value = $search['searchid'];
155 $label = $search['name'];
156 eval('$searches .= "' . $template->fetch('selectoption') . '";');
161 foreach ($bugsys->datastore
['assignto'] AS $dev)
163 $value = $dev['userid'];
164 $label = construct_user_display($dev, false
);
165 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
168 eval('$template->flush("' . $template->fetch('search') . '");');
172 // ###################################################################
174 if ($_REQUEST['do'] == 'process')
176 // -------------------------------------------------------------------
178 if ($bugsys->in
['summary'])
180 $keywords = preg_split('#\s+#', $bugsys->in
['summary']);
182 // TODO - need to have some str to bool conversions
184 foreach ($keywords AS $word)
186 if (strlen($word) < SEARCH_WORD_MIN
)
191 $word = str_replace("'", "\'", $word);
192 if ($bugsys->in
['mode'] == MODE_ALL
)
194 $querybuild['text'] .= " +$word";
198 $querybuild['text'] .= " $word";
201 if (!preg_match('#-(.+?)#', trim($word)))
203 $hilight .= " $word";
207 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
208 $hilight = trim($hilight);
209 $hilight = preg_replace('#\s#', '+', $hilight);
211 $temp = trim($querybuild['text']);
213 if ($bugsys->in
['mode'] == MODE_ALL
OR $bugsys->in
['mode'] == MODE_RAW
)
215 $bool_flag = ' IN BOOLEAN MODE';
218 $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)";
221 // -------------------------------------------------------------------
223 if ($bugsys->in
['reporter'])
225 // force email or name?? make a distinction?
226 // more elegant way to do this? probably
227 $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')) . "%'");
230 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
234 // -------------------------------------------------------------------
235 // product/component/version stuff
236 if (is_array($bugsys->in
['product']))
238 foreach ($bugsys->in
['product'] AS $prod)
240 $product = explode(',', $prod);
241 $product = $bugsys->clean($product, TYPE_UINT
);
242 $products[] = $product[0];
243 $components[] = $product[1];
244 $versions[] = $product[2];
246 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
249 // -------------------------------------------------------------------
250 // severity, priority, status, resolution, assignedto
253 if ($bugsys->in
['severity'])
255 $bugsys->input_clean('severity', TYPE_UINT
);
256 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in
['severity']) . ")";
260 if ($bugsys->in
['priority'])
262 $bugsys->input_clean('priority', TYPE_UINT
);
263 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in
['priority']) . ")";
267 if ($bugsys->in
['status'])
269 $bugsys->input_clean('status', TYPE_UINT
);
270 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in
['status']) . ")";
274 if ($bugsys->in
['resolution'])
276 $bugsys->input_clean('resolution', TYPE_UINT
);
277 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in
['resolution']) . ")";
281 if ($bugsys->in
['assignedto'])
283 $bugsys->input_clean('assignedto', TYPE_UINT
);
284 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in
['assignedto']) . ")";
287 // -------------------------------------------------------------------
289 if ($bugsys->in
['date'])
291 // now - (seconds/day * number of days)
292 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT
) * 3600);
293 $querybuild['date'] = "AND bug.dateline >= $dateline";
296 // -------------------------------------------------------------------
298 $bugsys->input_clean('favorite', TYPE_INT
);
299 if ($bugsys->in
['favorite'] != 0 AND $bugsys->userinfo
['userid'])
301 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . $bugsys->userinfo
['userid']);
302 while ($favorite = $db->fetch_array($favorites))
304 $ids[] = $favorite['bugid'];
306 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in
['favorite'] > 0 ?
"IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
309 // -------------------------------------------------------------------
311 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
312 $orderby = array('ASC', 'DESC');
313 $bugsys->in
['orderby'] = strtoupper($bugsys->in
['orderby']);
314 if (in_array($bugsys->in
['sortby'], $sortby) AND in_array($bugsys->in
['orderby'], $orderby))
316 $sortclause = "ORDER BY " . $bugsys->in
['sortby'] . ' ' . $bugsys->in
['orderby'];
318 else if ($bugsys->in
['sortby'] == 'relevance')
327 // -------------------------------------------------------------------
329 $fields_fetch = $bugsys->db
->query("
330 SELECT bugfield.*, MAX(permission.mask) AS mask
331 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
332 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
333 ON (bugfield.fieldid = permission.fieldid)
335 AND permission.usergroupid IN ({$bugsys->userinfo['usergroupid']}" . (sizeof($bugsys->userinfo
['groupids']) != 0 ?
',' . implode(',', $bugsys->userinfo
['groupids']) : '') . ")
336 AND bugfield.cansearch = 1
337 GROUP BY (bugfield.fieldid)"
339 while ($field = $bugsys->db
->fetch_array($fields_fetch))
341 if (!empty($bugsys->in
["custom$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in
["custom$field[fieldid]"])))
343 if ($field['type'] == 'input_checkbox' AND $bugsys->input_clean("custom$field[fieldid]", TYPE_INT
) != 0)
345 $querybuild[] = "AND bug.custom$field[fieldid] = " . ($bugsys->in
["custom$field[fieldid]"] > 0 ?
1 : 0);
347 else if ($field['type'] == 'input_text')
349 $like = str_replace("'", "\'", $bugsys->in
["custom$field[fieldid]"]);
350 $querybuild[] = "AND bug.custom$field[fieldid] LIKE '%$like%'";
352 else if ($field['type'] == 'select_single' AND $bugsys->in
["custom$field[fieldid]"] != -1)
354 $temp = unserialize($field['selects']);
355 $querybuild[] = "AND bug.custom$field[fieldid] = '" . trim($temp[ intval($bugsys->in
["custom$field[fieldid]"]) ]) . "'";
360 // -------------------------------------------------------------------
361 // have to search something
362 if (sizeof($querybuild) < 1)
364 $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
));
367 // -------------------------------------------------------------------
370 $search = $db->query("
371 SELECT bug.*, comment.commentid
372 FROM " . TABLE_PREFIX
. "bug AS bug
373 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
374 ON (bug.bugid = comment.bugid)
376 AND bug.product IN (" . fetch_on_bits('canviewbugs') . ")
381 (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ?
"
383 (bug.hidden AND bug.userid = " . $bugsys->userinfo
['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewonhidden') . "))" : "") . "
385 " . implode("\n\t\t", $querybuild) . "
390 $numrows = $db->num_rows($search);
394 $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.'));
397 while ($result = $db->fetch_array($search))
399 $ids[] = $result['bugid'];
400 $results[] = $result;
403 if ($bugsys->userinfo
['userid'] AND !$cachedsearch AND !$bugsys->in
['rerun'])
405 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = " . $bugsys->userinfo
['userid'] . " AND name IS NULL");
408 // store the search params
409 $params = $bugsys->in
;
410 foreach ($_COOKIE AS $key => $value)
412 unset($params["$key"]);
417 $search = $cachedsearch;
418 $search['ids'] = implode(',', $ids);
419 $search['resultcount'] = sizeof($results);
420 $db->query("UPDATE " . TABLE_PREFIX
. "search SET ids = '$search[ids]', resultcount = $search[resultcount], dateline = " . TIMENOW
. " WHERE searchid = " . $cachedsearch['searchid']);
425 INSERT INTO " . TABLE_PREFIX
. "search
426 (userid, dateline, query, ids, orderby, hilight, resultcount)
428 (" . $bugsys->userinfo
['userid'] . ",
429 " . TIMENOW
. ", '" . $bugsys->escape(serialize($params)) . "',
430 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
431 '" . $bugsys->escape($hilight) . "',
432 " . sizeof($results) . "
435 $search = array('searchid' => $db->insert_id(), 'ids' => implode(',', $ids), 'orderby' => $sortclause, 'hilight' => $hilight, 'resultcount' => sizeof($results));
438 $_POST['do'] = 'results';
441 // ###################################################################
443 if ($_REQUEST['do'] == 'update')
445 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
446 if (!$search OR !can_perform('caneditother'))
448 $message->errorPermission();
451 $productSelect = ConstructProductSelect();
453 $show['update'] = true
;
455 // -------------------------------------------------------------------
457 $fields = construct_custom_fields(null
, true
, false
, true
);
459 foreach ($fields AS $field)
463 $customfields['left'] .= $field;
467 $customfields['right'] .= $field;
472 // -------------------------------------------------------------------
474 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, 0);
475 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, 0);
476 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, 0);
477 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, 0);
483 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
485 foreach ($bugsys->datastore
['assignto'] AS $dev)
487 $value = $dev['userid'];
488 $label = construct_user_display($dev, false
);
489 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
492 eval('$template->flush("' . $template->fetch('search_update') . '");');
495 // ###################################################################
497 if ($_POST['do'] == 'doupdate')
499 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
500 if (!$search OR !can_perform('caneditother'))
502 $message->errorPermission();
505 // find all the bugs that we can edit
507 SELECT * FROM " . TABLE_PREFIX
. "bug
508 WHERE bugid IN ($search[ids])
509 AND product IN (" . fetch_on_bits('canviewbugs') . ")
512 product IN (" . fetch_on_bits('caneditother') . ")
514 (userid = " . $bugsys->userinfo
['userid'] . " AND product IN (" . fetch_on_bits('caneditown') . "))
517 while ($bug = $db->fetch_array($bugs))
519 if (!((can_perform('caneditown', $bug['product']) AND $bugsys->userinfo
['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['product']) AND $bugsys->userinfo
['userid'] != $bug['userid'])) AND !can_perform('canpostcomments', $bug['product']))
524 $api = new BugApi($bugsys);
525 $api->set('bugid', $bug['bugid']);
526 $api->set_condition();
529 $log = new Logging();
530 $log->set_bugid($bug['bugid']);
531 $log->add_data(true
, $bug, $log->getCommonFields(), true
);
533 if ($bugsys->in
['status'] AND can_perform('canchangestatus', $bug['product']))
535 $api->set('status', $bugsys->in
['status']);
537 if ($bugsys->in
['priority'] AND can_perform('canchangestatus', $bug['product']))
539 $api->set('priority', $bugsys->in
['priority']);
541 if ($bugsys->in
['severity'])
543 $api->set('severity', $bugsys->in
['severity']);
545 if ($bugsys->in
['resolution'] AND can_perform('canchangestatus', $bug['product']))
547 $api->set('resolution', $bugsys->in
['resolution']);
549 if ($bugsys->in
['assignedto'] AND can_perform('canassign', $bug['product']))
551 $api->set('assignedto', $bugsys->in
['assignedto']);
553 if ($bugsys->in
['product'])
555 $product = explode(',', $bugsys->in
['product']);
556 $api->set('product', $product[0]);
557 $api->set('component', $product[1]);
558 $api->set('version', $product[2]);
561 process_custom_fields($api, $message, false
, true
);
563 $log->add_data(false
, $api->values
, $log->getCommonFields(), true
);
566 $log->update_history();
569 $message->redirect(T('The specified bugs have been updated and you will now return to your search results.'), 'search.php?searchid=' . $bugsys->in
['searchid']);
572 // ###################################################################
574 if ($_REQUEST['do'] == 'export')
576 if (!$bugsys->in
['searchid'] AND $bugsys->userinfo
['userid'])
578 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . $bugsys->userinfo
['userid']);
580 else if ($bugsys->in
['searchid'])
582 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
586 $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.'));
591 $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>.'));
594 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby]");
596 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
598 <bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo
, false
)) . '" date="' . $datef->format('r', TIMENOW
) . '" searchid="' . $search['searchid'] . '">';
600 while ($bug = $db->fetch_array($bugs))
604 $bug = ProcessBugDataForDisplay($bug);
606 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
607 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
610 $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX
. "user WHERE userid = $bug[userid]"), false
) . "</reporter>";
612 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
613 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
614 if ($bug['component'])
616 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
618 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
619 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
620 if ($bug['assignedto'])
622 $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore
['assignto']["$bug[assignedto]"], false
) . "</assignedTo>";
624 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
625 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
626 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
628 $xml .= "\n\t</bug>";
631 $xml .= "\n</bugdarExport>";
633 $funct->download_file($xml, T('bugdar-search-' . $search['searchid'] . '.xml'), true
);
636 // ###################################################################
638 if ($_POST['do'] == 'dosave')
640 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT
) . " AND userid = " . $bugsys->userinfo
['userid']);
643 $message->addError(T('The search does not exist.'));
647 $message->addError(sprintf(T('This search has already been named "%1$s".'), $search['name']));
649 if (empty($bugsys->in
['name']))
651 $message->addError(T('The name cannot be empty.'));
654 if (!$message->hasErrors())
656 $db->query("UPDATE " . TABLE_PREFIX
. "search SET name = '" . $bugsys->input_escape('name') . "' WHERE searchid = " . $bugsys->in
['searchid']);
657 $message->redirect(T('Your search has been saved.'), 'search.php?searchid=' . $search['searchid']);
661 $_REQUEST['do'] = 'save';
662 $show['errors'] = true
;
666 // ###################################################################
668 if ($_REQUEST['do'] == 'save')
670 if (!$bugsys->userinfo
['userid'])
672 $message->errorPermission();
675 eval('$template->flush("' . $template->fetch('search_save') . '");');
678 // ###################################################################
680 if ($_POST['do'] == 'results')
684 $message->error(T('No bugs matched your search criteria. Please <a href="search.php?new=1">try again</a> with different search requirements.'));
687 $searchid = $search['searchid'];
688 $hilight = $search['hilight'];
690 LoadPaginationFramework();
691 $pagination->setTotal($search['resultcount']);
692 $pagination->splitPages();
694 $sort = new ListSorter('search');
696 $show['save'] = ($bugsys->userinfo
['userid'] AND !$search['name']);
697 $show['update'] = can_perform('caneditother');
700 $search = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
701 while ($bug = $db->fetch_array($search))
703 $funct->exec_swap_bg('altcolor', '');
704 $bug = ProcessBugDataForDisplay($bug, $funct->bgcolour
);
705 $bugs .= $sort->constructRow($bug, "&hilight=$hilight");
708 $columnHeads = $sort->constructColumnHeaders(false
);
709 $show['pagenav'] = ($pagination->getPageCount() > 1);
710 $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
712 eval('$template->flush("' . $template->fetch('search_results') . '");');
715 /*=====================================================================*\
716 || ###################################################################
719 || ###################################################################
720 \*=====================================================================*/