3 * FusionForge search engine
5 * Copyright 2004, Dominik Haas
6 * Copyright 2009, Roland Mas
7 * http://fusionforge.org
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once $gfcommon.'search/SearchQuery.class.php';
27 class TrackersSearchQuery extends SearchQuery {
37 * flag if non public items are returned
39 * @var boolean $showNonPublic
46 * @param string $words words we are searching for
47 * @param int $offset offset
48 * @param boolean $isExact if we want to search for all the words or if only one matching the query is sufficient
49 * @param int $groupId group id
50 * @param array $sections sections to search in
51 * @param boolean $showNonPublic flag if private sections are searched too
53 function TrackersSearchQuery($words, $offset, $isExact, $groupId, $sections=SEARCH__ALL_SECTIONS, $showNonPublic=false) {
54 $this->groupId = $groupId;
55 $this->showNonPublic = $showNonPublic;
57 $this->SearchQuery($words, $offset, $isExact);
59 $this->setSections($sections);
63 * getQuery - get the query built to get the search results
65 * @return array query+params array
68 $qpa = db_construct_qpa () ;
70 if (forge_get_config('use_fti')) {
71 $words = $this->getFTIwords();
73 if (count($this->phrases)) {
74 $qpa = db_construct_qpa ($qpa,
75 '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 ',
76 array ($this->field_separator, $this->groupId)) ;
78 if ($this->sections != SEARCH__ALL_SECTIONS) {
79 $qpa = db_construct_qpa ($qpa,
80 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
81 array (db_int_array_to_any_clause ($this->sections))) ;
84 $qpa = db_construct_qpa ($qpa,
85 ' 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 ',
87 $qpa = $this->addMatchCondition ($qpa, 'full_string_agg') ;
88 $qpa = db_construct_qpa ($qpa,
89 ' ORDER BY ts_rank(vectors, to_tsquery($1)) DESC',
92 $qpa = db_construct_qpa ($qpa,
93 '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 ',
94 array ($this->groupId)) ;
96 if ($this->sections != SEARCH__ALL_SECTIONS) {
97 $qpa = db_construct_qpa ($qpa,
98 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
99 array (db_int_array_to_any_clause ($this->sections))) ;
102 $qpa = db_construct_qpa ($qpa,
103 'AND artifact.artifact_id = artifact_idx.artifact_id AND vectors @@ to_tsquery($1) ORDER BY ts_rank(vectors, to_tsquery($1)) DESC',
108 $qpa = db_construct_qpa ($qpa,
109 '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 ',
110 array ($this->field_separator, $this->groupId)) ;
112 if ($this->sections != SEARCH__ALL_SECTIONS) {
113 $qpa = db_construct_qpa ($qpa,
114 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
115 array (db_int_array_to_any_clause ($this->sections))) ;
118 $qpa = db_construct_qpa ($qpa,
119 'GROUP BY artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact.details) AS x WHERE ',
121 $qpa = $this->addIlikeCondition ($qpa, 'full_string_agg') ;
122 $qpa = db_construct_qpa ($qpa,
123 ' ORDER BY artifact_id') ;
130 * getSections - returns the list of available trackers
132 * @param $groupId int group id
133 * @param $showNonPublic boolean if we should consider non public sections
135 static function getSections($groupId, $showNonPublic=false) {
136 $sql = 'SELECT group_artifact_id, name FROM artifact_group_list WHERE group_id = $1';
137 $sql .= ' ORDER BY name';
139 $res = db_query_params ($sql,
142 while($data = db_fetch_array($res)) {
143 if (forge_check_perm('tracker',$data['group_artifact_id'],'read')) {
144 $sections[$data['group_artifact_id']] = $data['name'];
150 function getSearchByIdQuery() {
151 $qpa = db_construct_qpa () ;
152 $qpa = db_construct_qpa ($qpa,
153 'SELECT DISTINCT artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact_group_list.name FROM artifact LEFT OUTER JOIN artifact_message USING (artifact_id), users, artifact_group_list WHERE users.user_id = artifact.submitted_by AND artifact_group_list.group_artifact_id = artifact.group_artifact_id AND artifact_group_list.group_id = $1 ',
154 array ($this->groupId)) ;
155 if ($this->sections != SEARCH__ALL_SECTIONS) {
156 $qpa = db_construct_qpa ($qpa,
157 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
158 array( db_int_array_to_any_clause ($this->sections))) ;
160 if (!$this->showNonPublic) {
161 $qpa = db_construct_qpa ($qpa,
162 'AND artifact_group_list.is_public = 1 ') ;
164 $qpa = db_construct_qpa ($qpa,
165 'AND artifact.artifact_id=$1 ORDER BY artifact_group_list.name, artifact.artifact_id',
166 array ($this->searchId)) ;
174 // c-file-style: "bsd"