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(
34 $focus['search'] = 'focus';
36 require_once('./global.php');
37 require_once('./includes/functions_product.php');
38 require_once('./includes/class_sort.php');
39 require_once('./includes/class_logging.php');
40 require_once('./includes/api_bug.php');
42 if (!can_perform('cansearch'))
44 $message->errorPermission();
47 define('MODE_ANY', 1);
48 define('MODE_ALL', 2);
49 define('MODE_RAW', 3);
51 $var = $db->queryFirst("SHOW VARIABLES LIKE 'ft_min_word_len'");
52 define('SEARCH_WORD_MIN', $var['Value']);
54 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = 0 AND dateline < " . (TIMENOW
- 3600));
56 $show['search'] = true
;
58 // ###################################################################
60 if (empty($_REQUEST['do']))
62 $_REQUEST['do'] = 'search';
65 // ###################################################################
67 if ($_REQUEST['do'] == 'search')
69 if ($input->in
['new'])
73 else if ($input->in
['searchid'])
75 $cachedsearch = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT
) . " AND userid = " . bugdar
::$userinfo['userid']);
77 else if (bugdar
::$userinfo['userid'])
79 $cachedsearch = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . bugdar
::$userinfo['userid']);
88 $show['cached'] = true
;
89 if ($cachedsearch['dateline'] < TIMENOW
- 900 OR $input->in
['rerun'])
91 $_REQUEST['do'] = 'process';
92 $input->in
= array_merge(unserialize($cachedsearch['query']), $input->in
);
93 BSApp
::debug('rerunning the search');
97 $search = $cachedsearch;
98 $_POST['do'] = 'results';
108 if (!is_array(bugdar
::$datastore['product']))
110 $message->error(T('No products are setup, therefore there can be no bugs and thus search cannot function.'));
113 if (!is_array(bugdar
::$datastore['version']))
115 $message->error(T('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
118 // -------------------------------------------------------------------
120 $fields = construct_custom_fields(null
, true
, false
, true
);
122 foreach ($fields AS $field)
126 $customfields['left'] .= $field;
130 $customfields['right'] .= $field;
135 // -------------------------------------------------------------------
137 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
138 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
139 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
140 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
143 if (bugdar
::$userinfo['userid'])
145 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NOT NULL AND userid = " . bugdar
::$userinfo['userid']);
146 foreach ($searchesFetch as $search)
148 $tpl = new BSTemplate('selectoption');
150 'value' => $search['searchid'],
151 'label' => $search['name']
153 $searches .= $tpl->evaluate()->getTemplate();
158 foreach (bugdar
::$datastore['assignto'] AS $dev)
160 $tpl = new BSTemplate('selectoption');
162 'value' => $dev['userid'],
163 'label' => construct_user_display($dev, false
)
165 $select['dev'] .= $tpl->evaluate()->getTemplate();
168 $tpl = new BSTemplate('search');
171 'productSelect' => construct_product_select(),
172 'customfields' => $customfields,
173 'searches' => $searches
175 $tpl->evaluate()->flush();
179 // ###################################################################
181 if ($_REQUEST['do'] == 'process')
183 // -------------------------------------------------------------------
185 if ($input->in
['summary'])
187 $keywords = preg_split('#\s+#', $input->in
['summary']);
189 // TODO - need to have some str to bool conversions
191 foreach ($keywords AS $word)
193 if (strlen($word) < SEARCH_WORD_MIN
)
198 if ($input->in
['mode'] == MODE_ALL
)
200 $querybuild['text'] .= " +$word";
204 $querybuild['text'] .= " $word";
207 if (!preg_match('#-(.+?)#', trim($word)))
209 $hilight .= " $word";
213 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
214 $hilight = trim($hilight);
215 $hilight = preg_replace('#\s#', '+', $hilight);
217 $temp = trim($querybuild['text']);
219 if ($input->in
['mode'] == MODE_ALL
OR $input->in
['mode'] == MODE_RAW
)
221 $bool_flag = ' IN BOOLEAN MODE';
224 $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)";
227 // -------------------------------------------------------------------
229 if ($input->in
['reporter'])
231 // force email or name?? make a distinction?
232 // more elegant way to do this? probably
233 $user = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "user WHERE email LIKE '%" . str_replace('%', '\%', $input->inputEscape('reporter')) . "' OR displayname LIKE '%" . str_replace('%', '\%', $input->inputEscape('reporter')) . "%'");
236 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
240 // -------------------------------------------------------------------
241 // product/component/version stuff
242 if (is_array($input->in
['product']))
244 foreach ($input->in
['product'] AS $prod)
246 $product = explode(',', $prod);
247 $product = $input->clean($product, TYPE_UINT
);
248 $products[$product[0]] = $product[0];
249 $components[$product[1]] = $product[1];
250 $versions[$product[2]] = $product[2];
252 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
255 // -------------------------------------------------------------------
256 // severity, priority, status, resolution, assignedto
259 if ($input->in
['severity'])
261 $input->inputClean('severity', TYPE_UINT
);
262 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $input->in
['severity']) . ")";
266 if ($input->in
['priority'])
268 $input->inputClean('priority', TYPE_UINT
);
269 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $input->in
['priority']) . ")";
273 if ($input->in
['status'])
275 $input->inputClean('status', TYPE_UINT
);
276 $querybuild['status'] = "AND bug.status IN (" . implode(',', $input->in
['status']) . ")";
280 if ($input->in
['resolution'])
282 $input->inputClean('resolution', TYPE_UINT
);
283 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $input->in
['resolution']) . ")";
287 if ($input->in
['assignedto'])
289 $input->inputClean('assignedto', TYPE_UINT
);
290 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $input->in
['assignedto']) . ")";
293 // -------------------------------------------------------------------
295 if ($input->in
['date'])
297 // now - (seconds/day * number of days)
298 $dateline = time() - ($input->inputClean('date', TYPE_INT
) * 3600);
299 $querybuild['date'] = "AND bug.dateline >= $dateline";
302 // -------------------------------------------------------------------
304 $input->inputClean('favorite', TYPE_INT
);
305 if ($input->in
['favorite'] != 0 AND bugdar
::$userinfo['userid'])
307 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX
. "favorite WHERE userid = " . bugdar
::$userinfo['userid']);
308 foreach ($favorites as $favorite)
310 $ids[] = $favorite['bugid'];
312 $querybuild['favorites'] = "AND bug.bugid " . ($input->in
['favorite'] > 0 ?
"IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
315 // -------------------------------------------------------------------
317 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
318 $orderby = array('ASC', 'DESC');
319 $input->in
['orderby'] = strtoupper($input->in
['orderby']);
320 if (in_array($input->in
['sortby'], $sortby) AND in_array($input->in
['orderby'], $orderby))
322 $sortclause = "ORDER BY " . $input->in
['sortby'] . ' ' . $input->in
['orderby'];
324 else if ($input->in
['sortby'] == 'relevance')
333 // -------------------------------------------------------------------
335 $fields_fetch = $db->query("
336 SELECT bugfield.*, MAX(permission.mask) AS mask
337 FROM " . TABLE_PREFIX
. "bugfield AS bugfield
338 LEFT JOIN " . TABLE_PREFIX
. "bugfieldpermission AS permission
339 ON (bugfield.fieldid = permission.fieldid)
341 AND permission.usergroupid IN (" . bugdar
::$userinfo['usergroupid'] . (sizeof(bugdar
::$userinfo['groupids']) != 0 ?
',' . implode(',', bugdar
::$userinfo['groupids']) : '') . ")
342 AND bugfield.cansearch = 1
343 GROUP BY (bugfield.fieldid)"
345 foreach ($fields_fetch as $field)
347 if (!empty($input->in
["custom$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($input->in
["custom$field[fieldid]"])))
349 if ($field['type'] == 'input_checkbox' AND $input->inputClean("custom$field[fieldid]", TYPE_INT
) != 0)
351 $querybuild[] = "AND bug.custom$field[fieldid] = " . ($input->in
["custom$field[fieldid]"] > 0 ?
1 : 0);
353 else if ($field['type'] == 'input_text')
355 $querybuild[] = "AND bug.custom$field[fieldid] LIKE '%" . $input->in
["custom$field[fieldid]"] . "%'";
357 else if ($field['type'] == 'select_single' AND $input->in
["custom$field[fieldid]"] != -1)
359 $temp = unserialize($field['selects']);
360 $querybuild[] = "AND bug.custom$field[fieldid] = '" . trim($temp[ intval($input->in
["custom$field[fieldid]"]) ]) . "'";
365 // -------------------------------------------------------------------
366 // have to search something
367 if (sizeof($querybuild) < 1)
369 $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
));
372 // -------------------------------------------------------------------
375 $search = $db->query("
376 SELECT bug.*, comment.commentid
377 FROM " . TABLE_PREFIX
. "bug AS bug
378 LEFT JOIN " . TABLE_PREFIX
. "comment AS comment
379 ON (bug.bugid = comment.bugid)
381 AND bug.product IN (" . fetch_on_bits('canviewbugs') . ")
386 (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ?
"
388 (bug.hidden AND bug.userid = " . bugdar
::$userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewonhidden') . "))" : "") . "
390 " . implode("\n\t\t", $querybuild) . "
395 $numrows = $search->size();
399 $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.'));
402 foreach ($search as $result)
404 $ids[] = $result['bugid'];
405 $results[] = $result;
408 if (bugdar
::$userinfo['userid'] AND !$cachedsearch AND !$input->in
['rerun'])
410 $db->query("DELETE FROM " . TABLE_PREFIX
. "search WHERE userid = " . bugdar
::$userinfo['userid'] . " AND name IS NULL");
413 // store the search params
414 $params = $input->in
;
415 foreach ($_COOKIE AS $key => $value)
417 unset($params["$key"]);
422 $search = $cachedsearch;
423 $search['ids'] = implode(',', $ids);
424 $search['resultcount'] = sizeof($results);
425 $db->query("UPDATE " . TABLE_PREFIX
. "search SET ids = '$search[ids]', resultcount = $search[resultcount], dateline = " . TIMENOW
. " WHERE searchid = " . $cachedsearch['searchid']);
430 INSERT INTO " . TABLE_PREFIX
. "search
431 (userid, dateline, query, ids, orderby, hilight, resultcount)
433 (" . bugdar
::$userinfo['userid'] . ",
434 " . TIMENOW
. ", '" . $input->escape(serialize($params)) . "',
435 '" . implode(',', $ids) . "', '" . $input->escape($sortclause) . "',
436 '" . $input->escape($hilight) . "',
437 " . sizeof($results) . "
440 $search = array('searchid' => $db->insertId(), 'ids' => implode(',', $ids), 'orderby' => $sortclause, 'hilight' => $hilight, 'resultcount' => sizeof($results));
443 $_POST['do'] = 'results';
446 // ###################################################################
448 if ($_REQUEST['do'] == 'update')
450 $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT
) . " AND userid = " . bugdar
::$userinfo['userid']);
451 if (!$search OR !can_perform('caneditother'))
453 $message->errorPermission();
456 $show['update'] = true
;
458 // -------------------------------------------------------------------
460 $fields = construct_custom_fields(null
, true
, false
, true
);
462 foreach ($fields AS $field)
466 $customfields['left'] .= $field;
470 $customfields['right'] .= $field;
475 // -------------------------------------------------------------------
477 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', 0, 0);
478 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', 0, 0);
479 $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, 0);
480 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, 0);
482 $tpl = new BSTemplate('selectoption');
488 $select['dev'] = $tpl->evaluate()->getTemplate();
490 foreach (bugdar
::$datastore['assignto'] as $dev)
492 $tpl = new BSTemplate('selectoption');
494 'value' => $dev['userid'],
495 'label' => construct_user_display($dev, false
)
497 $select['dev'] .= $tpl->evaluate()->getTemplate();
500 $tpl = new BSTemplate('search_update');
504 'customfields' => $customfields,
505 'productSelect' => construct_product_select()
507 $tpl->evaluate()->flush();
510 // ###################################################################
512 if ($_POST['do'] == 'doupdate')
514 $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT
) . " AND userid = " . bugdar
::$userinfo['userid']);
515 if (!$search OR !can_perform('caneditother'))
517 $message->errorPermission();
520 // find all the bugs that we can edit
522 SELECT * FROM " . TABLE_PREFIX
. "bug
523 WHERE bugid IN ($search[ids])
524 AND product IN (" . fetch_on_bits('canviewbugs') . ")
527 product IN (" . fetch_on_bits('caneditother') . ")
529 (userid = " . bugdar
::$userinfo['userid'] . " AND product IN (" . fetch_on_bits('caneditown') . "))
532 foreach ($bugs as $bug)
534 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->set('bugid', $bug['bugid']);
543 $log = new Logging();
544 $log->setBugId($bug['bugid']);
545 $log->addData(true
, $bug, $log->getCommonFields(), true
);
547 if ($input->in
['status'] AND can_perform('canchangestatus', $bug['product']))
549 $api->set('status', $input->in
['status']);
551 if ($input->in
['priority'] AND can_perform('canchangestatus', $bug['product']))
553 $api->set('priority', $input->in
['priority']);
555 if ($input->in
['severity'])
557 $api->set('severity', $input->in
['severity']);
559 if ($input->in
['resolution'] AND can_perform('canchangestatus', $bug['product']))
561 $api->set('resolution', $input->in
['resolution']);
563 if ($input->in
['assignedto'] AND can_perform('canassign', $bug['product']))
565 $api->set('assignedto', $input->in
['assignedto']);
567 if ($input->in
['product'])
569 $product = explode(',', $input->in
['product']);
570 $api->set('product', $product[0]);
571 $api->set('component', $product[1]);
572 $api->set('version', $product[2]);
575 process_custom_fields($api, $message, false
, true
);
577 $log->addData(false
, $api->values
, $log->getCommonFields(), true
);
580 $log->updateHistory();
583 $message->redirect(T('The specified bugs have been updated and you will now return to your search results.'), 'search.php?searchid=' . $input->in
['searchid']);
586 // ###################################################################
588 if ($_REQUEST['do'] == 'export')
590 if (!$input->in
['searchid'] AND bugdar
::$userinfo['userid'])
592 $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "search WHERE name IS NULL AND userid = " . bugdar
::$userinfo['userid']);
594 else if ($input->in
['searchid'])
596 $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT
) . " AND userid = " . bugdar
::$userinfo['userid']);
600 $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.'));
605 $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>.'));
608 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby]");
610 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
612 <bugdarExport user="' . $input->unsanitize(construct_user_display(bugdar
::$userinfo, false
)) . '" date="' . $datef->format('r', TIMENOW
) . '" searchid="' . $search['searchid'] . '">';
614 foreach ($bugs as $bug)
618 $bug = ProcessBugDataForDisplay($bug);
620 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
621 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
624 $xml .= "\n\t\t<reporter>" . construct_user_display($db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "user WHERE userid = $bug[userid]"), false
) . "</reporter>";
626 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
627 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
628 if ($bug['component'])
630 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
632 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
633 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
634 if ($bug['assignedto'])
636 $xml .= "\n\t\t<assignedTo>" . construct_user_display(bugdar
::$datastore['assignto']["$bug[assignedto]"], false
) . "</assignedTo>";
638 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
639 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
640 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
642 $xml .= "\n\t</bug>";
645 $xml .= "\n</bugdarExport>";
647 BSFunctions
::download_file($xml, T('bugdar-search-' . $search['searchid'] . '.xml'), true
);
650 // ###################################################################
652 if ($_POST['do'] == 'dosave')
654 $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX
. "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT
) . " AND userid = " . bugdar
::$userinfo['userid']);
657 $message->addError(T('The search does not exist.'));
661 $message->addError(sprintf(T('This search has already been named "%1$s".'), $search['name']));
663 if (empty($input->in
['name']))
665 $message->addError(T('The name cannot be empty.'));
668 if (!$message->hasErrors())
670 $db->query("UPDATE " . TABLE_PREFIX
. "search SET name = '" . $input->inputEscape('name') . "' WHERE searchid = " . $input->in
['searchid']);
671 $message->redirect(T('Your search has been saved.'), 'search.php?searchid=' . $search['searchid']);
675 $_REQUEST['do'] = 'save';
676 $show['errors'] = true
;
680 // ###################################################################
682 if ($_REQUEST['do'] == 'save')
684 if (!bugdar
::$userinfo['userid'])
686 $message->errorPermission();
689 $tpl = new BSTemplate('search_save');
691 'message' => $message,
692 'searchid' => $input->in
['searchid'],
693 'name' => $input->in
['name']
695 $tpl->evaluate()->flush();
698 // ###################################################################
700 if ($_POST['do'] == 'results')
704 $message->error(T('No bugs matched your search criteria. Please <a href="search.php?new=1">try again</a> with different search requirements.'));
707 $searchid = $search['searchid'];
708 $hilight = $search['hilight'];
710 require_once 'includes/pagination.php';
711 $pagination = new Pagination();
712 $pagination->processIncomingData();
713 $pagination->setTotal($search['resultcount']);
714 $pagination->splitPages();
716 $sort = new ListSorter('search');
718 $show['save'] = (bugdar
::$userinfo['userid'] AND !$search['name']);
719 $show['update'] = can_perform('caneditother');
722 $search = $db->query("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
723 foreach ($search as $bug)
725 BSFunctions
::swap_css_classes('altcolor', '');
726 $bug = ProcessBugDataForDisplay($bug, BSFunctions
::$cssClass);
727 $bugs .= $sort->constructRow($bug, "&hilight=$hilight");
730 $columnHeads = $sort->constructColumnHeaders(false
);
731 $show['pagenav'] = ($pagination->getPageCount() > 1);
732 $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
734 $tpl = new BSTemplate('search_results');
736 'columnHeads' => $columnHeads,
738 'searchid' => $searchid
740 $tpl->evaluate()->flush();