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 DocsSearchQuery 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 DocsSearchQuery($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
68 if (forge_get_config('use_fti')) {
69 return $this->getFTIQuery();
71 $qpa = db_construct_qpa();
72 $qpa = db_construct_qpa($qpa,
73 'SELECT doc_data.docid, doc_data.title, doc_data.description, doc_data.filename, doc_groups.groupname FROM doc_data, doc_groups WHERE doc_data.doc_group = doc_groups.doc_group AND doc_data.group_id = $1',
74 array ($this->groupId));
75 if ($this->sections != SEARCH__ALL_SECTIONS) {
76 $qpa = db_construct_qpa($qpa,
77 'AND doc_groups.doc_group = ANY ($1) ',
78 db_int_array_to_any_clause($this->sections));
80 if ($this->showNonPublic) {
81 $qpa = db_construct_qpa($qpa,
82 ' AND doc_data.stateid IN (1, 4, 5)');
84 $qpa = db_construct_qpa($qpa,
85 ' AND doc_data.stateid = 1');
87 $qpa = db_construct_qpa($qpa,
89 $qpa = $this->addIlikeCondition($qpa, 'title');
90 $qpa = db_construct_qpa($qpa,
92 $qpa = $this->addIlikeCondition($qpa, 'description');
93 $qpa = db_construct_qpa($qpa,
94 ')) ORDER BY doc_groups.groupname, doc_data.docid');
99 function getFTIQuery() {
100 $words = $this->getFormattedWords();
101 $group_id=$this->groupId;
103 $qpa = db_construct_qpa () ;
104 if(count($this->words)) {
105 $qpa = db_construct_qpa($qpa,
106 'SELECT doc_data.docid, doc_data.filename, 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',
107 array (implode (' ', $words)));
108 $qpa = db_construct_qpa($qpa,
109 ' WHERE doc_data.doc_group = doc_groups.doc_group AND doc_data.docid = doc_data_idx.docid AND (vectors @@ q') ;
110 if (count($this->phrases)) {
111 $qpa = db_construct_qpa($qpa,
112 $this->getOperator());
113 $qpa = db_construct_qpa($qpa,
115 $qpa = $this->addMatchCondition($qpa, 'title');
116 $qpa = db_construct_qpa($qpa,
118 $qpa = $this->addMatchCondition($qpa, 'description');
119 $qpa = db_construct_qpa($qpa,
122 $qpa = db_construct_qpa($qpa,
123 ') AND doc_data.group_id = $1',
125 if ($this->sections != SEARCH__ALL_SECTIONS) {
126 $qpa = db_construct_qpa($qpa,
127 ' AND doc_groups.doc_group = ANY ($1)',
128 db_int_array_to_any_clause ($this->sections));
130 if ($this->showNonPublic) {
131 $qpa = db_construct_qpa($qpa,
132 ' AND doc_data.stateid IN (1, 4, 5)');
134 $qpa = db_construct_qpa($qpa,
135 ' AND doc_data.stateid = 1');
137 $qpa = db_construct_qpa($qpa,
138 ' ORDER BY rank(vectors, q) DESC, groupname ASC');
140 $qpa = db_construct_qpa($qpa,
141 'SELECT doc_data.docid, filename, title, description doc_groups.groupname FROM doc_data, doc_groups');
142 $qpa = db_construct_qpa($qpa,
143 'WHERE doc_data.doc_group = doc_groups.doc_group');
144 if (count($this->phrases)) {
145 $qpa = db_construct_qpa($qpa,
146 $this->getOperator()) ;
147 $qpa = db_construct_qpa($qpa,
149 $qpa = $this->addMatchCondition($qpa, 'title');
150 $qpa = db_construct_qpa($qpa,
152 $qpa = $this->addMatchCondition($qpa, 'description');
153 $qpa = db_construct_qpa($qpa,
156 $qpa = db_construct_qpa($qpa,
157 ') AND doc_data.group_id = $1',
159 if ($this->sections != SEARCH__ALL_SECTIONS) {
160 $qpa = db_construct_qpa($qpa,
161 'AND doc_groups.doc_group = ANY ($1) ',
162 db_int_array_to_any_clause($this->sections));
164 if ($this->showNonPublic) {
165 $qpa = db_construct_qpa($qpa,
166 ' AND doc_data.stateid IN (1, 4, 5)');
168 $qpa = db_construct_qpa($qpa,
169 ' AND doc_data.stateid = 1');
171 $qpa = db_construct_qpa($qpa,
172 ' ORDER BY groupname');
178 * getSections - returns the list of available doc groups
180 * @param $groupId int group id
181 * @param $showNonPublic boolean if we should consider non public sections
183 static function getSections($groupId, $showNonPublic = false) {
184 $sql = 'SELECT doc_groups.doc_group, doc_groups.groupname FROM doc_groups, doc_data'
185 .' WHERE doc_groups.doc_group = doc_data.doc_group AND doc_groups.group_id = $1';
186 if ($showNonPublic) {
187 $sql .= ' AND doc_data.stateid IN (1, 4, 5) AND doc_groups.stateid = 1';
189 $sql .= ' AND doc_data.stateid = 1 AND doc_groups.stateid = 1';
191 $sql .= ' ORDER BY groupname';
194 $res = db_query_params($sql,
196 while($data = db_fetch_array($res)) {
197 $sections[$data['doc_group']] = $data['groupname'];
205 // c-file-style: "bsd"