r1361: Include the searchid in the XML export
[bugdar.git] / search.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright 2002-[#]year[#] Blue Static
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 $fetchtemplates = array(
23 'search',
24 'search_results',
25 'trackerhome_bits',
26 'list_head',
27 'pagenav_bit',
28 'pagenav'
29 );
30
31 define('SVN', '$Id$');
32
33 $focus['search'] = 'focus';
34
35 require_once('./global.php');
36 require_once('./includes/functions_product.php');
37 require_once('./includes/class_sort.php');
38
39 if (!can_perform('cansearch'))
40 {
41 $message->error_permission();
42 }
43
44 define('MODE_ANY', ($bugsys->in['mode'] == 1));
45 define('MODE_ALL', ($bugsys->in['mode'] == 2));
46 define('MODE_RAW', ($bugsys->in['mode'] == 3));
47
48 $var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
49 define('SEARCH_WORD_MIN', $var['Value']);
50
51 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = 0 AND dateline < " . (TIMENOW - 3600));
52
53 $show['search'] = true;
54
55 // ###################################################################
56
57 if (empty($_REQUEST['do']))
58 {
59 $_REQUEST['do'] = 'search';
60 }
61
62 // ###################################################################
63
64 if ($_REQUEST['do'] == 'process')
65 {
66 // -------------------------------------------------------------------
67 // handle keywords
68 if ($bugsys->in['summary'])
69 {
70 $keywords = preg_split('#\s+#', $bugsys->in['summary']);
71
72 // TODO - need to have some str to bool conversions
73
74 foreach ($keywords AS $word)
75 {
76 if (strlen($word) < SEARCH_WORD_MIN)
77 {
78 continue;
79 }
80
81 if (MODE_ALL)
82 {
83 $querybuild['text'] .= " +$word";
84 }
85 else
86 {
87 $querybuild['text'] .= " $word";
88 }
89
90 if (!preg_match('#-(.+?)#', trim($word)))
91 {
92 $hilight .= " $word";
93 }
94 }
95
96 $hilight = preg_replace('#[^0-9a-zA-Z_ ]#', '', $hilight);
97 $hilight = trim($hilight);
98 $hilight = preg_replace('#\s#', '+', $hilight);
99
100 $temp = trim($querybuild['text']);
101
102 if (MODE_ALL OR MODE_RAW)
103 {
104 $bool_flag = ' IN BOOLEAN MODE';
105 }
106
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)";
108 }
109
110 // -------------------------------------------------------------------
111 // reporter
112 if ($bugsys->in['reporter'])
113 {
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')) . "%'");
117 if ($user['userid'])
118 {
119 $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
120 }
121 }
122
123 // -------------------------------------------------------------------
124 // product/component/version stuff
125 if (is_array($bugsys->in['product']))
126 {
127 foreach ($bugsys->in['product'] AS $prod)
128 {
129 $product = explode(',', $prod);
130 $product = $bugsys->clean($product, TYPE_UINT);
131 $products[] = $product[0];
132 $components[] = $product[1];
133 $versions[] = $product[2];
134 }
135 $querybuild['pcv'] = "AND bug.product IN (" . implode(',', $products) . ") AND bug.component IN (" . implode(',', $components) . ") AND bug.version IN (" . implode(',', $versions) . ")";
136 }
137
138 // -------------------------------------------------------------------
139 // severity, priority, status, resolution, assignedto
140
141 // severity
142 if ($bugsys->in['severity'])
143 {
144 $bugsys->input_clean('severity', TYPE_UINT);
145 $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")";
146 }
147
148 // priority
149 if ($bugsys->in['priority'])
150 {
151 $bugsys->input_clean('priority', TYPE_UINT);
152 $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")";
153 }
154
155 // status
156 if ($bugsys->in['status'])
157 {
158 $bugsys->input_clean('status', TYPE_UINT);
159 $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")";
160 }
161
162 // resolution
163 if ($bugsys->in['resolution'])
164 {
165 $bugsys->input_clean('resolution', TYPE_UINT);
166 $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")";
167 }
168
169 // assignment
170 if ($bugsys->in['assignedto'])
171 {
172 $bugsys->input_clean('assignedto', TYPE_UINT);
173 $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")";
174 }
175
176 // -------------------------------------------------------------------
177 // date
178 if ($bugsys->in['date'])
179 {
180 // now - (seconds/day * number of days)
181 $dateline = time() - ($bugsys->input_clean('date', TYPE_INT) * 3600);
182 $querybuild['date'] = "AND bug.dateline >= $dateline";
183 }
184
185 // -------------------------------------------------------------------
186 // favoritess
187 if (($bugsys->in['favorite'] == 1 OR $bugsys->in['favorite'] == 0) AND $bugsys->userinfo['userid'])
188 {
189 $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE userid = " . $bugsys->userinfo['userid']);
190 while ($favorite = $db->fetch_array($favorites))
191 {
192 $ids[] = $favorite['bugid'];
193 }
194 $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in['favorite'] == 0 ? "NOT IN" : "IN") . " (" . implode(', ', $ids) . ")";
195 }
196
197 // -------------------------------------------------------------------
198 // sort by
199 $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
200 $orderby = array('ASC', 'DESC');
201 $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
202 if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
203 {
204 $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
205 }
206 else if ($bugsys->in['sortby'] == 'relevance')
207 {
208 $sortclause = '';
209 }
210 else
211 {
212 $sortclause = '';
213 }
214
215 // -------------------------------------------------------------------
216 // custom fields
217 $fields_fetch = $bugsys->db->query("
218 SELECT bugfield.*
219 FROM " . TABLE_PREFIX . "bugfield AS bugfield
220 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
221 ON (bugfield.fieldid = permission.fieldid)
222 WHERE permission.mask <> 0
223 AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}
224 AND bugfield.cansearch = 1"
225 );
226 while ($field = $bugsys->db->fetch_array($fields_fetch))
227 {
228 if (!empty($bugsys->in["field$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["field$field[fieldid]"])))
229 {
230 if ($field['type'] == 'input_checkbox')
231 {
232 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = " . (($bugsys->in["field$field[fieldid]"] == 1) ? 0 : 1);
233 }
234 else if ($field['type'] == 'input_text')
235 {
236 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] LIKE '%" . $bugsys->in["field$field[fieldid]"] . "%'";
237 }
238 else if ($field['type'] == 'select_single' AND $bugsys->in["field$field[fieldid]"] != -1)
239 {
240 $temp = unserialize($field['selects']);
241 $querybuild[] = "AND bugfieldvalue.field$field[fieldid] = '" . trim($temp[ intval($bugsys->in["field$field[fieldid]"]) ]) . "'";
242 }
243 }
244 }
245
246 // -------------------------------------------------------------------
247 // have to search something
248 if (sizeof($querybuild) < 1)
249 {
250 $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));
251 }
252
253 // -------------------------------------------------------------------
254 // do the search
255 $query = "
256 SELECT bug.*, comment.commentid
257 FROM " . TABLE_PREFIX . "bug AS bug
258 LEFT JOIN " . TABLE_PREFIX . "comment AS comment
259 ON (bug.bugid = comment.bugid)
260 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugfieldvalue
261 ON (bug.bugid = bugfieldvalue.bugid)
262 WHERE bug.bugid <> 0
263 AND bug.product IN (#<'ONBITS:VIEW'>#)
264 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'>#))" : "") . ")
265 " . implode("\n\t\t", $querybuild) . "
266 GROUP BY bug.bugid
267 $sortclause";
268
269 $runquery = str_replace(array("#<'ONBITS:VIEW'>#", "#<'ONBITS:HIDDEN'>#", "#<'ONBITS:OWNHIDDEN'>#"), array(fetch_on_bits('canviewbugs'), fetch_on_bits('canviewhidden'), fetch_on_bits('canviewonhidden')), $query);
270
271 $search = $db->query($runquery);
272
273 $numrows = $db->num_rows($search);
274
275 if ($numrows < 1)
276 {
277 $message->error(_('No search results were returned that matched your criteria.'));
278 }
279
280 while ($result = $db->fetch_array($search))
281 {
282 $ids[] = $result['bugid'];
283 $results[] = $result;
284 }
285
286 if ($bugsys->userinfo['userid'])
287 {
288 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid'] . " AND name IS NULL");
289 }
290
291 $db->query("
292 INSERT INTO " . TABLE_PREFIX . "search
293 (userid, dateline, query, ids, orderby, hilight, resultcount)
294 VALUES
295 (" . $bugsys->userinfo['userid'] . ",
296 " . TIMENOW . ", '" . $bugsys->escape($query) . "',
297 '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
298 '" . $bugsys->escape($hilight) . "',
299 " . sizeof($results) . "
300 )"
301 );
302
303 $searchid = $db->insert_id();
304
305 $justprocess = true;
306 $search = array('ids' => implode(',', $ids), 'orderby' => $sortclause);
307
308 $_POST['do'] = 'results';
309 }
310
311 // ###################################################################
312
313 if ($_REQUEST['do'] == 'search')
314 {
315 if ($bugsys->in['searchid'] AND !$bugsys->in['new'])
316 {
317 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']))
318 {
319 $_POST['do'] = 'results';
320 $searchid = $cachedsearch['searchid'];
321 }
322 else
323 {
324 $newsearch = true;
325 }
326 }
327 else if ($bugsys->userinfo['userid'] AND !$bugsys->in['new'])
328 {
329 if ($cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']))
330 {
331 $_POST['do'] = 'results';
332 $searchid = $cachedsearch['searchid'];
333 }
334 else
335 {
336 $newsearch = true;
337 }
338 }
339 else
340 {
341 $newsearch = true;
342 }
343
344 if ($newsearch)
345 {
346 if (!is_array($bugsys->datastore['product']))
347 {
348 $message->error(_('No products are setup, therefore there can be no bugs and thus search cannot function.'));
349 }
350
351 if (!is_array($bugsys->datastore['version']))
352 {
353 $message->error(_('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
354 }
355
356 $productSelect = ConstructProductSelect();
357
358 // -------------------------------------------------------------------
359 // custom fields
360 $fields = construct_custom_fields(null, true);
361 $i = 0;
362 foreach ($fields AS $field)
363 {
364 if ($i % 2 == 0)
365 {
366 $customfields['left'] .= $field;
367 }
368 else
369 {
370 $customfields['right'] .= $field;
371 }
372 $i++;
373 }
374
375 // -------------------------------------------------------------------
376 // built-in fields
377 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid');
378 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid');
379 $select['status'] = construct_datastore_select('status', 'status', 'statusid');
380 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
381
382 $searches = '';
383 if ($bugsys->userinfo['userid'])
384 {
385 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . $bugsys->userinfo['userid']);
386 while ($search = $db->fetch_array($searchesFetch))
387 {
388 $value = $search['searchid'];
389 $label = $search['name'];
390 eval('$searches .= "' . $template->fetch('selectoption') . '";');
391 }
392 }
393
394 $select['dev'] = '';
395 foreach ($bugsys->datastore['assignto'] AS $dev)
396 {
397 $value = $dev['userid'];
398 $label = construct_user_display($dev, false);
399 eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
400 }
401
402 eval('$template->flush("' . $template->fetch('search') . '");');
403 }
404 }
405
406 // ###################################################################
407
408 if ($_REQUEST['do'] == 'export')
409 {
410 if (!$bugsys->in['searchid'] AND $bugsys->userinfo['userid'])
411 {
412 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']);
413 }
414 else if ($bugsys->in['searchid'])
415 {
416 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
417 }
418 else
419 {
420 $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.'));
421 }
422
423 if (!$search)
424 {
425 $message->error(_('Your search has expired because it is older than one hour. Please start over <a href="search.php?new=1">here</a>.'));
426 }
427
428 $bugs = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby]");
429
430 $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
431
432 <bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo, false)) . '" date="' . $datef->format('r', TIMENOW) . '" searchid="' . $search['searchid'] . '">';
433
434 while ($bug = $db->fetch_array($bugs))
435 {
436 $xml .= "\n\t<bug>";
437
438 ProcessBugDataForDisplay(&$bug);
439
440 $xml .= "\n\t\t<id>" . $bug['bugid'] . "</id>";
441 $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
442 if ($bug['userid'])
443 {
444 $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $bug[userid]"), false) . "</reporter>";
445 }
446 $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
447 $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
448 if ($bug['component'])
449 {
450 $xml .= "\n\t\t<component>" . $bug['component'] . "</component>";
451 }
452 $xml .= "\n\t\t<version>" . $bug['version'] . "</version>";
453 $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
454 if ($bug['assignedto'])
455 {
456 $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore['assignto']["$bug[assignedto]"], false) . "</assignedTo>";
457 }
458 $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
459 $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
460 $xml .= "\n\t\t<priority>" . $bug['priority'] . "</priority>";
461
462 $xml .= "\n\t</bug>";
463 }
464
465 $xml .= "\n</bugdarExport>";
466
467 $funct->download_file($xml, _('bugdar-search-' . $search['searchid'] . '.xml'), true);
468 }
469
470 // ###################################################################
471
472 if ($_POST['do'] == 'dosave')
473 {
474 $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
475 if (!$search)
476 {
477 $message->add_error(_('The search does not exist.'));
478 }
479 if ($search['name'])
480 {
481 $message->add_error(sprintf(_('This search has already been named "%1$s".'), $search['name']));
482 }
483 if (empty($bugsys->in['name']))
484 {
485 $message->add_error(_('The name cannot be empty.'));
486 }
487
488 if (!$message->items)
489 {
490 $db->query("UPDATE " . TABLE_PREFIX . "search SET name = '" . $bugsys->input_escape('name') . "' WHERE searchid = " . $bugsys->in['searchid']);
491 $message->redirect(_('Your search has been saved.'), 'search.php?searchid=' . $search['searchid']);
492 }
493 else
494 {
495 $message->error_list_process();
496 $_REQUEST['do'] = 'save';
497 $show['errors'] = true;
498 }
499 }
500
501 // ###################################################################
502
503 if ($_REQUEST['do'] == 'save')
504 {
505 if (!$bugsys->userinfo['userid'])
506 {
507 $message->error_permission();
508 }
509
510 $message-
511
512 eval('$template->flush("' . $template->fetch('search_save') . '");');
513 }
514
515 // ###################################################################
516
517 if ($_POST['do'] == 'results')
518 {
519 $show['cached'] = false;
520 if ($searchid AND !$justprocess)
521 {
522 $search = $cachedsearch;
523 if ($search['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
524 {
525 $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']));
526 while ($bug = $db->fetch_array($research))
527 {
528 $ids[] = $bug['bugid'];
529 $results[] = $bug;
530 }
531 $search['ids'] = implode(',', $ids);
532 $db->query("UPDATE " . TABLE_PREFIX . "search SET ids = '" . implode(',', $ids) . "', dateline = " . TIMENOW . ", resultcount = " . sizeof($results) . " WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT));
533 }
534 $show['cached'] = true;
535 $hilight = $search['hilight'];
536 }
537
538 LoadPaginationFramework();
539 $pagination->setTotal($search['resultcount']);
540 $pagination->splitPages();
541
542 $sort = new ListSorter('search');
543
544 $show['save'] = ($bugsys->userinfo['userid'] AND !$search['name']);
545
546 $bugs = '';
547 $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
548 while ($bug = $db->fetch_array($search))
549 {
550 $funct->exec_swap_bg($stylevar['alt_color'], '');
551 ProcessBugDataForDisplay(&$bug, $funct->bgcolour);
552 $bugs .= $sort->constructRow($bug, "&amp;hilight=$hilight");
553 }
554
555 $columnHeads = $sort->constructColumnHeaders(false);
556 $show['pagenav'] = ($pagination->getPageCount() > 1);
557 $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
558
559 eval('$template->flush("' . $template->fetch('search_results') . '");');
560 }
561
562 /*=====================================================================*\
563 || ###################################################################
564 || # $HeadURL$
565 || # $Id$
566 || ###################################################################
567 \*=====================================================================*/
568 ?>