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 $qpa = db_construct_qpa ($qpa,
71 'SELECT x.* FROM (SELECT artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact_group_list.name, artifact.summary||$1||artifact.details||$1||coalesce(ff_string_agg(artifact_message.body), $1) as full_string_agg',
72 array ($this->field_separator));
73 if (forge_get_config('use_fti')) {
74 $words = $this->getFTIwords();
75 $qpa = db_construct_qpa ($qpa,
76 ', (artifact_idx.vectors || coalesce(ff_tsvector_agg(artifact_message_idx.vectors), to_tsvector($1))) AS full_vector_agg',
79 $qpa = db_construct_qpa ($qpa,
80 ' FROM artifact LEFT OUTER JOIN artifact_message USING (artifact_id) ',
82 if (forge_get_config('use_fti')) {
83 $qpa = db_construct_qpa ($qpa, ' LEFT JOIN artifact_message_idx USING (artifact_id) ', array()) ;
85 $qpa = db_construct_qpa ($qpa, ' , users, artifact_group_list ', array()) ;
86 if (forge_get_config('use_fti')) {
87 $qpa = db_construct_qpa ($qpa,
91 $qpa = db_construct_qpa ($qpa,
92 ' 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 ',
93 array ($this->groupId)) ;
94 if ($this->sections != SEARCH__ALL_SECTIONS) {
95 $qpa = db_construct_qpa ($qpa,
96 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
97 array (db_int_array_to_any_clause ($this->sections))) ;
99 if (!$this->showNonPublic) {
100 $qpa = db_construct_qpa ($qpa,
101 'AND artifact_group_list.is_public = 1 ') ;
103 if (forge_get_config('use_fti')) {
104 $qpa = db_construct_qpa ($qpa,
105 'AND artifact.artifact_id = artifact_idx.artifact_id ',
108 $qpa = db_construct_qpa ($qpa,
109 'GROUP BY artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact_group_list.name, artifact.details') ;
111 if (forge_get_config('use_fti')) {
112 $qpa = db_construct_qpa ($qpa,
113 ', artifact_idx.vectors',
116 $qpa = db_construct_qpa ($qpa,
119 if (forge_get_config('use_fti')) {
120 $qpa = db_construct_qpa ($qpa,
121 'full_vector_agg @@ to_tsquery($1) ',
123 if (count($this->phrases)) {
124 $qpa = db_construct_qpa ($qpa,
126 $qpa = $this->addMatchCondition ($qpa, 'x.full_string_agg') ;
127 $qpa = db_construct_qpa ($qpa,
130 $qpa = db_construct_qpa ($qpa,
131 'ORDER BY ts_rank(full_vector_agg, to_tsquery($1)) DESC',
135 $qpa = $this->addIlikeCondition ($qpa, 'x.full_string_agg') ;
136 $qpa = db_construct_qpa ($qpa,
137 ' ORDER BY x.name, x.artifact_id') ;
143 * getSections - returns the list of available trackers
145 * @param $groupId int group id
146 * @param $showNonPublic boolean if we should consider non public sections
148 static function getSections($groupId, $showNonPublic=false) {
149 $sql = 'SELECT group_artifact_id, name FROM artifact_group_list WHERE group_id = $1';
150 $sql .= ' ORDER BY name';
152 $res = db_query_params ($sql,
155 while($data = db_fetch_array($res)) {
156 if (forge_check_perm('tracker',$data['group_artifact_id'],'read')) {
157 $sections[$data['group_artifact_id']] = $data['name'];
163 function getSearchByIdQuery() {
164 $qpa = db_construct_qpa () ;
165 $qpa = db_construct_qpa ($qpa,
166 '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 ',
167 array ($this->groupId)) ;
168 if ($this->sections != SEARCH__ALL_SECTIONS) {
169 $qpa = db_construct_qpa ($qpa,
170 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
171 array( db_int_array_to_any_clause ($this->sections))) ;
173 if (!$this->showNonPublic) {
174 $qpa = db_construct_qpa ($qpa,
175 'AND artifact_group_list.is_public = 1 ') ;
177 $qpa = db_construct_qpa ($qpa,
178 'AND artifact.artifact_id=$1 ORDER BY artifact_group_list.name, artifact.artifact_id',
179 array ($this->searchId)) ;
187 // c-file-style: "bsd"