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
70 $qpa = db_construct_qpa () ;
72 if (forge_get_config('use_fti')) {
73 if (count ($this->words)) {
74 $qpa = db_construct_qpa ($qpa,
75 'SELECT DISTINCT x.* FROM (SELECT artifact.artifact_id, artifact.group_artifact_id, artifact.summary, artifact.open_date, users.realname, artifact_group_list.name, (ts_rank(artifact_idx.vectors, q)+ts_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 ',
76 array ($this->getFormattedWords(),
78 $tsmatch = "(artifact_idx.vectors @@ q OR artifact_message_idx.vectors @@ q)";
79 $phraseOp = $this->getOperator();
81 $qpa = db_construct_qpa ($qpa,
82 '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 ',
83 array ($this->groupId)) ;
91 if (count($this->phrases)) {
92 $qpa = db_construct_qpa ($qpa,
94 $qpa = $this->addMatchCondition($qpa, 'artifact.details');
95 $qpa = db_construct_qpa ($qpa,
97 $qpa = $this->addMatchCondition($qpa, 'artifact.summary');
98 $qpa = db_construct_qpa ($qpa,
100 $qpa = $this->addMatchCondition($qpa, 'artifact_message.body');
101 $qpa = db_construct_qpa ($qpa,
104 if ($this->sections != SEARCH__ALL_SECTIONS) {
105 $qpa = db_construct_qpa ($qpa,
106 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
107 db_int_array_to_any_clause ($this->sections)) ;
109 if (!$this->showNonPublic) {
110 $qpa = db_construct_qpa ($qpa,
111 'AND artifact_group_list.is_public = 1 ') ;
113 $qpa = db_construct_qpa ($qpa,
115 if (count ($this->words)) {
116 $qpa = db_construct_qpa ($qpa,
117 'ORDER BY rank DESC') ;
120 $qpa = db_construct_qpa ($qpa,
121 '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 ',
122 array ($this->groupId)) ;
123 if ($this->sections != SEARCH__ALL_SECTIONS) {
124 $qpa = db_construct_qpa ($qpa,
125 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
126 db_int_array_to_any_clause ($this->sections)) ;
128 if (!$this->showNonPublic) {
129 $qpa = db_construct_qpa ($qpa,
130 'AND artifact_group_list.is_public = 1 ') ;
133 $qpa = db_construct_qpa ($qpa,
135 $qpa = $this->addIlikeCondition ($qpa, 'artifact.details') ;
136 $qpa = db_construct_qpa ($qpa,
138 $qpa = $this->addIlikeCondition ($qpa, 'artifact.summary') ;
139 $qpa = db_construct_qpa ($qpa,
141 $qpa = $this->addIlikeCondition ($qpa, 'artifact_message.body') ;
142 $qpa = db_construct_qpa ($qpa,
143 ')) ORDER BY artifact_group_list.name, artifact.artifact_id') ;
149 * getSections - returns the list of available trackers
151 * @param $groupId int group id
152 * @param $showNonPublic boolean if we should consider non public sections
154 static function getSections($groupId, $showNonPublic=false) {
155 $sql = 'SELECT group_artifact_id, name FROM artifact_group_list WHERE group_id = $1';
156 if (!$showNonPublic) {
157 $sql .= ' AND artifact_group_list.is_public = 1';
159 $sql .= ' ORDER BY name';
161 $res = db_query_params ($sql,
164 while($data = db_fetch_array($res)) {
165 $sections[$data['group_artifact_id']] = $data['name'];
170 function getSearchByIdQuery() {
171 $qpa = db_construct_qpa () ;
172 $qpa = db_construct_qpa ($qpa,
173 '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 ',
174 array ($this->groupId)) ;
175 if ($this->sections != SEARCH__ALL_SECTIONS) {
176 $qpa = db_construct_qpa ($qpa,
177 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
178 db_int_array_to_any_clause ($this->sections)) ;
180 if (!$this->showNonPublic) {
181 $qpa = db_construct_qpa ($qpa,
182 'AND artifact_group_list.is_public = 1 ') ;
184 $qpa = db_construct_qpa ($qpa,
185 'AND artifact.artifact_id=$1 ORDER BY artifact_group_list.name, artifact.artifact_id',
186 array ($this->searchId)) ;
194 // c-file-style: "bsd"