3 * FusionForge search engine
5 * Copyright 2004, Dominik Haas
6 * Copyright 2009, Roland Mas
7 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
8 * http://fusionforge.org
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require_once $gfcommon.'search/SearchQuery.class.php';
28 class TrackersSearchQuery extends SearchQuery {
38 * flag if non public items are returned
40 * @var boolean $showNonPublic
47 * @param string $words words we are searching for
48 * @param int $offset offset
49 * @param bool $isExact if we want to search for all the words or if only one matching the query is sufficient
50 * @param int $groupId group id
51 * @param string $sections sections to search in
52 * @param bool $showNonPublic flag if private sections are searched too
54 function __construct($words, $offset, $isExact, $groupId, $sections=SEARCH__ALL_SECTIONS, $showNonPublic=false) {
55 $this->groupId = $groupId;
56 $this->showNonPublic = $showNonPublic;
58 parent::__construct($words, $offset, $isExact);
60 $this->setSections($sections);
64 * getQuery - get the query built to get the search results
66 * @return array query+params array
69 $qpa = db_construct_qpa () ;
71 if (forge_get_config('use_fti')) {
72 $words = $this->getFTIwords();
74 if (count($this->phrases)) {
75 $qpa = db_construct_qpa ($qpa,
76 'SELECT x.* FROM (SELECT artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact.summary||$1||artifact.details||$1||coalesce(ff_string_agg(artifact_message.body), $1) as full_string_agg, artifact_idx.vectors FROM artifact LEFT OUTER JOIN artifact_message USING (artifact_id), users, artifact_group_list, artifact_idx WHERE users.user_id = artifact.submitted_by AND artifact.group_artifact_id = artifact_group_list.group_artifact_id AND artifact_group_list.group_id = $2 ',
77 array ($this->field_separator, $this->groupId)) ;
79 if ($this->sections != SEARCH__ALL_SECTIONS) {
80 $qpa = db_construct_qpa ($qpa,
81 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
82 array (db_int_array_to_any_clause ($this->sections))) ;
85 $qpa = db_construct_qpa ($qpa,
86 ' AND artifact.artifact_id = artifact_idx.artifact_id AND vectors @@ to_tsquery($1) GROUP BY artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact.details, vectors) AS x WHERE ',
88 $qpa = $this->addMatchCondition ($qpa, 'full_string_agg') ;
89 $qpa = db_construct_qpa ($qpa,
90 ' ORDER BY ts_rank(vectors, to_tsquery($1)) DESC',
93 $qpa = db_construct_qpa ($qpa,
94 'SELECT artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact_idx.vectors FROM artifact, users, artifact_group_list, artifact_idx WHERE users.user_id = artifact.submitted_by AND artifact.group_artifact_id = artifact_group_list.group_artifact_id AND artifact_group_list.group_id = $1 ',
95 array ($this->groupId)) ;
97 if ($this->sections != SEARCH__ALL_SECTIONS) {
98 $qpa = db_construct_qpa ($qpa,
99 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
100 array (db_int_array_to_any_clause ($this->sections))) ;
103 $qpa = db_construct_qpa ($qpa,
104 'AND artifact.artifact_id = artifact_idx.artifact_id AND vectors @@ to_tsquery($1) ORDER BY ts_rank(vectors, to_tsquery($1)) DESC',
109 $qpa = db_construct_qpa ($qpa,
110 'SELECT x.* FROM (SELECT artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact.summary||$1||artifact.details||$1||coalesce(ff_string_agg(artifact_message.body), $1) as full_string_agg FROM artifact LEFT OUTER JOIN artifact_message USING (artifact_id), users, artifact_group_list WHERE users.user_id = artifact.submitted_by AND artifact.group_artifact_id = artifact_group_list.group_artifact_id AND artifact_group_list.group_id = $2 ',
111 array ($this->field_separator, $this->groupId)) ;
113 if ($this->sections != SEARCH__ALL_SECTIONS) {
114 $qpa = db_construct_qpa ($qpa,
115 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
116 array (db_int_array_to_any_clause ($this->sections))) ;
119 $qpa = db_construct_qpa ($qpa,
120 'GROUP BY artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact.details) AS x WHERE ',
122 $qpa = $this->addIlikeCondition ($qpa, 'full_string_agg') ;
123 $qpa = db_construct_qpa ($qpa,
124 ' ORDER BY artifact_id') ;
131 * getSections - returns the list of available trackers
133 * @param $groupId int group id
134 * @param $showNonPublic boolean if we should consider non public sections
137 static function getSections($groupId, $showNonPublic=false) {
138 $sql = 'SELECT group_artifact_id, name FROM artifact_group_list WHERE group_id = $1';
139 $sql .= ' ORDER BY name';
141 $res = db_query_params ($sql,
144 while($data = db_fetch_array($res)) {
145 if (forge_check_perm('tracker',$data['group_artifact_id'],'read')) {
146 $sections[$data['group_artifact_id']] = $data['name'];
155 // c-file-style: "bsd"