3 * FusionForge search engine
5 * Copyright 1999-2001, VA Linux Systems, Inc
6 * Copyright 2004, Guillaume Smet/Open Wide
7 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
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 ArtifactSearchQuery extends SearchQuery {
39 * @var int $artifactId
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 int $artifactId artifact id
52 function __construct($words, $offset, $isExact, $groupId, $artifactId) {
53 //TODO: Why is groupId an arg and var since it isn't used anywhere?
54 $this->groupId = $groupId;
55 $this->artifactId = $artifactId;
57 parent::__construct($words, $offset, $isExact);
61 * getQuery - get the query built to get the search results
63 * @return array query+params array
66 $qpa = db_construct_qpa () ;
68 $words = $this->getFTIwords();
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.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 $qpa = db_construct_qpa ($qpa,
75 ', (artifact_idx.vectors || coalesce(ff_tsvector_agg(artifact_message_idx.vectors), to_tsvector($1))) AS full_vector_agg',
78 $qpa = db_construct_qpa ($qpa,
79 ' FROM artifact LEFT OUTER JOIN artifact_message USING (artifact_id) ',
81 if (forge_get_config('use_fti')) {
82 $qpa = db_construct_qpa ($qpa, ' LEFT JOIN artifact_message_idx USING (artifact_id) ', array()) ;
84 $qpa = db_construct_qpa ($qpa, ' , users ', array()) ;
85 if (forge_get_config('use_fti')) {
86 $qpa = db_construct_qpa ($qpa,
90 $qpa = db_construct_qpa ($qpa,
91 ' WHERE users.user_id = artifact.submitted_by AND artifact.group_artifact_id = $1 ',
92 array ($this->artifactId)) ;
93 if (forge_get_config('use_fti')) {
94 $qpa = db_construct_qpa ($qpa,
95 'AND artifact.artifact_id = artifact_idx.artifact_id ',
98 $qpa = db_construct_qpa ($qpa,
99 'GROUP BY artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact.details') ;
101 if (forge_get_config('use_fti')) {
102 $qpa = db_construct_qpa ($qpa,
103 ', artifact_idx.vectors',
106 $qpa = db_construct_qpa ($qpa,
109 if (forge_get_config('use_fti')) {
110 $qpa = db_construct_qpa ($qpa,
111 'full_vector_agg @@ to_tsquery($1) ',
113 if (count($this->phrases)) {
114 $qpa = db_construct_qpa ($qpa,
116 $qpa = $this->addMatchCondition ($qpa, 'x.full_string_agg') ;
117 $qpa = db_construct_qpa ($qpa,
120 $qpa = db_construct_qpa ($qpa,
121 'ORDER BY ts_rank(full_vector_agg, to_tsquery($1)) DESC',
125 $qpa = $this->addIlikeCondition ($qpa, 'x.full_string_agg') ;
126 $qpa = db_construct_qpa ($qpa,
127 'ORDER BY x.artifact_id') ;
136 $qpa = db_construct_qpa () ;
138 if (forge_get_config('use_fti')) {
139 $words=$this->getFTIwords();
140 $artifactId = $this->artifactId;
142 $qpa = db_construct_qpa ($qpa,
143 'SELECT a.group_artifact_id, a.artifact_id, ts_headline(summary, to_tsquery($1)) AS summary, ',
145 $qpa = db_construct_qpa ($qpa,
146 'a.open_date, users.realname, rank FROM (SELECT a.artifact_id, SUM (ts_rank(ai.vectors, q) + ts_rank(ami.vectors, q)) AS rank, artifact.summary||$1||artifact.details||$1||coalesce(ff_string_agg(artifact_message.body), $1) as full_string_agg FROM artifact a LEFT OUTER JOIN artifact_message am USING (artifact_id)',
147 array($this->field_separator)) ;
149 $qpa = db_construct_qpa ($qpa,
150 ', artifact_idx ai, artifact_message_idx ami, to_tsquery($1) q',
152 $qpa = db_construct_qpa ($qpa,
153 'WHERE a.group_artifact_id=$1',
154 array ($artifactId)) ;
155 $qpa = db_construct_qpa ($qpa,
156 ' AND ai.artifact_id = a.artifact_id AND ami.id = am.id AND ((ai.vectors @@ q OR ami.vectors @@ q) ') ;
158 if (count($this->phrases)) {
159 $qpa = db_construct_qpa ($qpa,
160 $this->getOperator()) ;
161 $qpa = db_construct_qpa ($qpa,
163 $qpa = $this->addMatchCondition($qpa, 'a.details');
164 $qpa = db_construct_qpa ($qpa,
166 $qpa = $this->addMatchCondition($qpa, 'a.summary');
167 $qpa = db_construct_qpa ($qpa,
169 $qpa = $this->addMatchCondition($qpa, 'am.body');
170 $qpa = db_construct_qpa ($qpa,
173 $qpa = db_construct_qpa ($qpa,
174 ') GROUP BY a.artifact_id) x, artifact a, users WHERE a.artifact_id=x.artifact_id AND users.user_id=a.submitted_by ORDER BY group_artifact_id ASC, rank DESC, a.artifact_id ASC') ;
176 $qpa = db_construct_qpa ($qpa,
177 'SELECT DISTINCT ON (a.group_artifact_id,a.artifact_id) a.group_artifact_id,a.artifact_id,a.summary,a.open_date,users.realname,a.status_id ') ;
178 $qpa = db_construct_qpa ($qpa,
179 'FROM artifact a LEFT OUTER JOIN artifact_message am USING (artifact_id), users WHERE a.group_artifact_id=$1 AND users.user_id=a.submitted_by AND ((',
180 array ($this->artifactId)) ;
182 $qpa = $this->addIlikeCondition ($qpa, 'cast(a.artifact_id as text)') ;
183 $qpa = db_construct_qpa ($qpa,
185 $qpa = $this->addIlikeCondition ($qpa, 'a.details') ;
186 $qpa = db_construct_qpa ($qpa,
188 $qpa = $this->addIlikeCondition ($qpa, 'a.summary') ;
189 $qpa = db_construct_qpa ($qpa,
191 $qpa = $this->addIlikeCondition ($qpa, 'am.body') ;
192 $qpa = db_construct_qpa ($qpa,
193 ')) ORDER BY group_artifact_id ASC, a.artifact_id ASC') ;
199 * getSearchByIdQuery - get the query built to get the search results when we are looking for an int
201 * @return array query+params array
203 function getSearchByIdQuery() {
204 $qpa = db_construct_qpa () ;
206 $qpa = db_construct_qpa ($qpa,
207 'SELECT DISTINCT ON (a.group_artifact_id,a.artifact_id) a.group_artifact_id, a.artifact_id') ;
208 $qpa = db_construct_qpa ($qpa,
209 ' FROM artifact a WHERE a.group_artifact_id=$1 AND a.artifact_id=$2',
210 array ($this->artifactId,
219 // c-file-style: "bsd"