3 * FusionForge search engine
5 * Copyright 2004, Dominik Haas
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 boolean $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 array $sections sections to search in
52 * @param boolean $showNonPublic flag if private sections are searched too
54 function TrackersSearchQuery($words, $offset, $isExact, $groupId, $sections=SEARCH__ALL_SECTIONS, $showNonPublic=false) {
55 $this->groupId = $groupId;
56 $this->showNonPublic = $showNonPublic;
58 $this->SearchQuery($words, $offset, $isExact);
60 $this->setSections($sections);
64 * getQuery - get the query built to get the search results
66 * @return array query+params array
71 $qpa = db_construct_qpa () ;
74 if (count ($this->words)) {
75 $qpa = db_construct_qpa ($qpa,
76 'SELECT DISTINCT x.* FROM (SELECT artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact_group_list.name, (rank(artifact_idx.vectors, q)+rank(artifact_message_idx.vectors, q)) AS rank FROM artifact LEFT OUTER JOIN artifact_message USING (artifact_id), users, artifact_group_list, to_tsquery($1) q, artifact_idx, artifact_message_idx WHERE users.user_id = artifact.submitted_by AND artifact_idx.artifact_id = artifact.artifact_id AND artifact_message_idx.id = artifact_message.id AND artifact_message_idx.artifact_id = artifact_message_idx.artifact_id AND artifact_group_list.group_artifact_id = artifact.group_artifact_id AND artifact_group_list.group_id = $2 ',
78 array ($this->getFormattedWords(),
80 $tsmatch = "(artifact_idx.vectors @@ q OR artifact_message_idx.vectors @@ q)";
81 $phraseOp = $this->getOperator();
83 $qpa = db_construct_qpa ($qpa,
84 'SELECT DISTINCT x.* FROM (SELECT 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 ',
85 array ($this->groupId)) ;
93 if (count($this->phrases)) {
94 $qpa = db_construct_qpa ($qpa,
96 $qpa = $this->addMatchCondition($qpa, 'artifact.details');
97 $qpa = db_construct_qpa ($qpa,
99 $qpa = $this->addMatchCondition($qpa, 'artifact.summary');
100 $qpa = db_construct_qpa ($qpa,
102 $qpa = $this->addMatchCondition($qpa, 'artifact_message.body');
103 $qpa = db_construct_qpa ($qpa,
106 if ($this->sections != SEARCH__ALL_SECTIONS) {
107 $qpa = db_construct_qpa ($qpa,
108 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
109 db_int_array_to_any_clause ($this->sections)) ;
111 if (!$this->showNonPublic) {
112 $qpa = db_construct_qpa ($qpa,
113 'AND artifact_group_list.is_public = 1 ') ;
115 $qpa = db_construct_qpa ($qpa,
117 if (count ($this->words)) {
118 $qpa = db_construct_qpa ($qpa,
119 'ORDER BY rank DESC') ;
122 $qpa = db_construct_qpa ($qpa,
123 '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 ',
124 array ($this->groupId)) ;
125 if ($this->sections != SEARCH__ALL_SECTIONS) {
126 $qpa = db_construct_qpa ($qpa,
127 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
128 db_int_array_to_any_clause ($this->sections)) ;
130 if (!$this->showNonPublic) {
131 $qpa = db_construct_qpa ($qpa,
132 'AND artifact_group_list.is_public = 1 ') ;
135 $qpa = db_construct_qpa ($qpa,
137 $qpa = $this->addIlikeCondition ($qpa, 'artifact.details') ;
138 $qpa = db_construct_qpa ($qpa,
140 $qpa = $this->addIlikeCondition ($qpa, 'artifact.summary') ;
141 $qpa = db_construct_qpa ($qpa,
143 $qpa = $this->addIlikeCondition ($qpa, 'artifact_message.body') ;
144 $qpa = db_construct_qpa ($qpa,
145 ')) ORDER BY artifact_group_list.name, artifact.artifact_id') ;
151 * getSections - returns the list of available trackers
153 * @param $groupId int group id
154 * @param $showNonPublic boolean if we should consider non public sections
156 function getSections($groupId, $showNonPublic=false) {
157 $sql = 'SELECT group_artifact_id, name FROM artifact_group_list WHERE group_id = $1';
158 if (!$showNonPublic) {
159 $sql .= ' AND artifact_group_list.is_public = 1';
161 $sql .= ' ORDER BY name';
163 $res = db_query_params ($sql,
166 while($data = db_fetch_array($res)) {
167 $sections[$data['group_artifact_id']] = $data['name'];
172 function getSearchByIdQuery() {
173 $qpa = db_construct_qpa () ;
174 $qpa = db_construct_qpa ($qpa,
175 '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 ',
176 array ($this->groupId)) ;
177 if ($this->sections != SEARCH__ALL_SECTIONS) {
178 $qpa = db_construct_qpa ($qpa,
179 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
180 db_int_array_to_any_clause ($this->sections)) ;
182 if (!$this->showNonPublic) {
183 $qpa = db_construct_qpa ($qpa,
184 'AND artifact_group_list.is_public = 1 ') ;
186 $qpa = db_construct_qpa ($qpa,
187 'AND artifact.artifact_id=$1 ORDER BY artifact_group_list.name, artifact.artifact_id',
188 array ($this->searchId)) ;
196 // c-file-style: "bsd"