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 x.* FROM (SELECT doc_data.docid, doc_data.title, doc_data.filename, doc_data.description, doc_groups.groupname, title||$1||description AS full_string_agg FROM doc_data, doc_groups WHERE doc_data.doc_group = doc_groups.doc_group AND doc_data.group_id = $2',
74 array ($this->field_separator,
76 if ($this->sections != SEARCH__ALL_SECTIONS) {
77 $qpa = db_construct_qpa ($qpa,
78 'AND doc_groups.doc_group = ANY ($1) ',
79 array( db_int_array_to_any_clause ($this->sections))) ;
81 if ($this->showNonPublic) {
82 $qpa = db_construct_qpa($qpa, ' AND doc_data.stateid IN (1, 4, 5)');
84 $qpa = db_construct_qpa($qpa, ' AND doc_data.stateid = 1');
86 $qpa = db_construct_qpa ($qpa,
88 $qpa = $this->addIlikeCondition ($qpa, 'full_string_agg') ;
89 $qpa = db_construct_qpa ($qpa,
90 ' ORDER BY x.groupname, x.docid') ;
95 function getFTIQuery() {
96 $words = $this->getFTIwords();
97 $group_id=$this->groupId;
99 $qpa = db_construct_qpa () ;
101 $qpa = db_construct_qpa ($qpa,
102 'SELECT x.* FROM (SELECT doc_data.docid, doc_data.filename, ts_headline(doc_data.title, q) AS title, ts_headline(doc_data.description, q) AS description, doc_groups.groupname, title||$1||description AS full_string_agg, doc_data_idx.vectors FROM doc_data, doc_groups, doc_data_idx, to_tsquery($2) AS q',
103 array ($this->field_separator,
105 $qpa = db_construct_qpa ($qpa,
106 ' WHERE doc_data.doc_group = doc_groups.doc_group AND doc_data.docid = doc_data_idx.docid AND (vectors @@ to_tsquery($1)',
108 $qpa = db_construct_qpa ($qpa,
109 ') AND doc_data.group_id = $1',
111 if ($this->sections != SEARCH__ALL_SECTIONS) {
112 $qpa = db_construct_qpa ($qpa,
113 ' AND doc_groups.doc_group = ANY ($1)',
114 array( db_int_array_to_any_clause ($this->sections))) ;
116 if ($this->showNonPublic) {
117 $qpa = db_construct_qpa ($qpa,
118 ' AND doc_data.stateid IN (1, 4, 5)') ;
120 $qpa = db_construct_qpa ($qpa,
121 ' AND doc_data.stateid = 1') ;
123 $qpa = db_construct_qpa ($qpa,
125 if (count($this->phrases)) {
126 $qpa = db_construct_qpa ('WHERE ') ;
127 $qpa = $this->addMatchCondition($qpa, 'full_string_agg');
129 $qpa = db_construct_qpa ($qpa,
130 ' ORDER BY ts_rank(vectors, to_tsquery($1)) DESC, groupname ASC',
136 * getSections - returns the list of available doc groups
138 * @param $groupId int group id
139 * @param $showNonPublic boolean if we should consider non public sections
141 static function getSections($groupId, $showNonPublic = false) {
142 if (!forge_check_perm('docman',$groupId,'read')) {
146 $sql = 'SELECT doc_groups.doc_group, doc_groups.groupname FROM doc_groups, doc_data'
147 .' WHERE doc_groups.doc_group = doc_data.doc_group AND doc_groups.group_id = $1';
148 if ($showNonPublic) {
149 $sql .= ' AND doc_data.stateid IN (1, 4, 5) AND doc_groups.stateid = 1';
151 $sql .= ' AND doc_data.stateid = 1 AND doc_groups.stateid = 1';
153 $sql .= ' ORDER BY groupname';
156 $res = db_query_params($sql,
158 while($data = db_fetch_array($res)) {
159 $sections[$data['doc_group']] = $data['groupname'];
167 // c-file-style: "bsd"