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 DocsSearchQuery 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 DocsSearchQuery($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
70 if (forge_get_config('use_fti')) {
71 return $this->getFTIQuery();
73 $qpa = db_construct_qpa () ;
74 $qpa = db_construct_qpa ($qpa,
75 'SELECT doc_data.docid, doc_data.title, doc_data.description, doc_groups.groupname FROM doc_data, doc_groups WHERE doc_data.doc_group = doc_groups.doc_group AND doc_data.group_id = $1',
76 array ($this->groupId)) ;
77 if ($this->sections != SEARCH__ALL_SECTIONS) {
78 $qpa = db_construct_qpa ($qpa,
79 'AND doc_groups.doc_group = ANY ($1) ',
80 db_int_array_to_any_clause ($this->sections)) ;
82 if ($this->showNonPublic) {
83 $qpa = db_construct_qpa ($qpa,
84 ' AND doc_data.stateid IN (1, 4, 5)') ;
86 $qpa = db_construct_qpa ($qpa,
87 ' AND doc_data.stateid = 1') ;
89 $qpa = db_construct_qpa ($qpa,
91 $qpa = $this->addIlikeCondition ($qpa, 'title') ;
92 $qpa = db_construct_qpa ($qpa,
94 $qpa = $this->addIlikeCondition ($qpa, 'description') ;
95 $qpa = db_construct_qpa ($qpa,
96 ')) ORDER BY doc_groups.groupname, doc_data.docid') ;
101 function getFTIQuery() {
102 $words = $this->getFormattedWords();
103 $group_id=$this->groupId;
105 $qpa = db_construct_qpa () ;
106 if(count($this->words)) {
107 $qpa = db_construct_qpa ($qpa,
108 'SELECT doc_data.docid, headline(doc_data.title, q) AS title, headline(doc_data.description, q) AS description doc_groups.groupname FROM doc_data, doc_groups, doc_data_idx, to_tsquery($1) q',
109 array (implode (' ', $words))) ;
110 $qpa = db_construct_qpa ($qpa,
111 ' WHERE doc_data.doc_group = doc_groups.doc_group AND doc_data.docid = doc_data_idx.docid AND (vectors @@ q') ;
112 if (count($this->phrases)) {
113 $qpa = db_construct_qpa ($qpa,
114 $this->getOperator()) ;
115 $qpa = db_construct_qpa ($qpa,
117 $qpa = $this->addMatchCondition($qpa, 'title');
118 $qpa = db_construct_qpa ($qpa,
120 $qpa = $this->addMatchCondition($qpa, 'description');
121 $qpa = db_construct_qpa ($qpa,
124 $qpa = db_construct_qpa ($qpa,
125 ') AND doc_data.group_id = $1',
127 if ($this->sections != SEARCH__ALL_SECTIONS) {
128 $qpa = db_construct_qpa ($qpa,
129 ' AND doc_groups.doc_group = ANY ($1)',
130 db_int_array_to_any_clause ($this->sections)) ;
132 if ($this->showNonPublic) {
133 $qpa = db_construct_qpa ($qpa,
134 ' AND doc_data.stateid IN (1, 4, 5)') ;
136 $qpa = db_construct_qpa ($qpa,
137 ' AND doc_data.stateid = 1') ;
139 $qpa = db_construct_qpa ($qpa,
140 ' ORDER BY rank(vectors, q) DESC, groupname ASC') ;
142 $qpa = db_construct_qpa ($qpa,
143 'SELECT doc_data.docid, title, description doc_groups.groupname FROM doc_data, doc_groups') ;
144 $qpa = db_construct_qpa ($qpa,
145 'WHERE doc_data.doc_group = doc_groups.doc_group') ;
146 if (count($this->phrases)) {
147 $qpa = db_construct_qpa ($qpa,
148 $this->getOperator()) ;
149 $qpa = db_construct_qpa ($qpa,
151 $qpa = $this->addMatchCondition($qpa, 'title');
152 $qpa = db_construct_qpa ($qpa,
154 $qpa = $this->addMatchCondition($qpa, 'description');
155 $qpa = db_construct_qpa ($qpa,
158 $qpa = db_construct_qpa ($qpa,
159 ') AND doc_data.group_id = $1',
161 if ($this->sections != SEARCH__ALL_SECTIONS) {
162 $qpa = db_construct_qpa ($qpa,
163 'AND doc_groups.doc_group = ANY ($1) ',
164 db_int_array_to_any_clause ($this->sections)) ;
166 if ($this->showNonPublic) {
167 $qpa = db_construct_qpa ($qpa,
168 ' AND doc_data.stateid IN (1, 4, 5)') ;
170 $qpa = db_construct_qpa ($qpa,
171 ' AND doc_data.stateid = 1') ;
173 $qpa = db_construct_qpa ($qpa,
174 ' ORDER BY groupname') ;
180 * getSections - returns the list of available doc groups
182 * @param $groupId int group id
183 * @param $showNonPublic boolean if we should consider non public sections
185 function getSections($groupId, $showNonPublic=false) {
186 $sql = 'SELECT doc_groups.doc_group, doc_groups.groupname FROM doc_groups, doc_data'
187 .' WHERE doc_groups.doc_group = doc_data.doc_group AND doc_groups.group_id=$1';
188 if ($showNonPublic) {
189 $sql .= ' AND doc_data.stateid IN (1, 4, 5)';
191 $sql .= ' AND doc_data.stateid = 1';
193 $sql .= ' ORDER BY groupname';
196 $res = db_query_params ($sql,
198 while($data = db_fetch_array($res)) {
199 $sections[$data['doc_group']] = $data['groupname'];
207 // c-file-style: "bsd"