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, (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 ',
77 array ($this->getFormattedWords(),
79 $tsmatch = "(artifact_idx.vectors @@ q OR artifact_message_idx.vectors @@ q)";
80 $phraseOp = $this->getOperator();
82 $qpa = db_construct_qpa ($qpa,
83 '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 ',
84 array ($this->groupId)) ;
92 if (count($this->phrases)) {
93 $qpa = db_construct_qpa ($qpa,
95 $qpa = $this->addMatchCondition($qpa, 'artifact.details');
96 $qpa = db_construct_qpa ($qpa,
98 $qpa = $this->addMatchCondition($qpa, 'artifact.summary');
99 $qpa = db_construct_qpa ($qpa,
101 $qpa = $this->addMatchCondition($qpa, 'artifact_message.body');
102 $qpa = db_construct_qpa ($qpa,
105 if ($this->sections != SEARCH__ALL_SECTIONS) {
106 $qpa = db_construct_qpa ($qpa,
107 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
108 db_int_array_to_any_clause ($this->sections)) ;
110 if (!$this->showNonPublic) {
111 $qpa = db_construct_qpa ($qpa,
112 'AND artifact_group_list.is_public = 1 ') ;
114 $qpa = db_construct_qpa ($qpa,
116 if (count ($this->words)) {
117 $qpa = db_construct_qpa ($qpa,
118 'ORDER BY rank DESC') ;
121 $qpa = db_construct_qpa ($qpa,
122 '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 ',
123 array ($this->groupId)) ;
124 if ($this->sections != SEARCH__ALL_SECTIONS) {
125 $qpa = db_construct_qpa ($qpa,
126 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
127 db_int_array_to_any_clause ($this->sections)) ;
129 if (!$this->showNonPublic) {
130 $qpa = db_construct_qpa ($qpa,
131 'AND artifact_group_list.is_public = 1 ') ;
134 $qpa = db_construct_qpa ($qpa,
136 $qpa = $this->addIlikeCondition ($qpa, 'artifact.details') ;
137 $qpa = db_construct_qpa ($qpa,
139 $qpa = $this->addIlikeCondition ($qpa, 'artifact.summary') ;
140 $qpa = db_construct_qpa ($qpa,
142 $qpa = $this->addIlikeCondition ($qpa, 'artifact_message.body') ;
143 $qpa = db_construct_qpa ($qpa,
144 ')) ORDER BY artifact_group_list.name, artifact.artifact_id') ;
150 * getSections - returns the list of available trackers
152 * @param $groupId int group id
153 * @param $showNonPublic boolean if we should consider non public sections
155 static function getSections($groupId, $showNonPublic=false) {
156 $sql = 'SELECT group_artifact_id, name FROM artifact_group_list WHERE group_id = $1';
157 if (!$showNonPublic) {
158 $sql .= ' AND artifact_group_list.is_public = 1';
160 $sql .= ' ORDER BY name';
162 $res = db_query_params ($sql,
165 while($data = db_fetch_array($res)) {
166 $sections[$data['group_artifact_id']] = $data['name'];
171 function getSearchByIdQuery() {
172 $qpa = db_construct_qpa () ;
173 $qpa = db_construct_qpa ($qpa,
174 '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 ',
175 array ($this->groupId)) ;
176 if ($this->sections != SEARCH__ALL_SECTIONS) {
177 $qpa = db_construct_qpa ($qpa,
178 'AND artifact_group_list.group_artifact_id = ANY ($1) ',
179 db_int_array_to_any_clause ($this->sections)) ;
181 if (!$this->showNonPublic) {
182 $qpa = db_construct_qpa ($qpa,
183 'AND artifact_group_list.is_public = 1 ') ;
185 $qpa = db_construct_qpa ($qpa,
186 'AND artifact.artifact_id=$1 ORDER BY artifact_group_list.name, artifact.artifact_id',
187 array ($this->searchId)) ;
195 // c-file-style: "bsd"