3 * FusionForge search engine
5 * Copyright 2004, Dominik Haas
6 * Copyright 2009, Roland Mas
7 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
8 * http://fusionforge.org
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 __construct($words, $offset, $isExact, $groupId, $sections = SEARCH__ALL_SECTIONS, $showNonPublic = false) {
55 $this->groupId = $groupId;
56 $this->showNonPublic = $showNonPublic;
58 parent::__construct($words, $offset, $isExact);
60 $this->setSections($sections);
64 * getQuery - get the query built to get the search results
66 * @return array query+params array
69 if (forge_get_config('use_fti')) {
70 return $this->getFTIQuery();
72 $qpa = db_construct_qpa () ;
73 $qpa = db_construct_qpa ($qpa,
74 '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',
75 array ($this->field_separator,
77 if ($this->sections != SEARCH__ALL_SECTIONS) {
78 $qpa = db_construct_qpa ($qpa,
79 'AND doc_groups.doc_group = ANY ($1) ',
80 array( db_int_array_to_any_clause ($this->sections))) ;
82 if ($this->showNonPublic) {
83 $qpa = db_construct_qpa($qpa, ' AND doc_data.stateid IN (1, 4, 5)');
85 $qpa = db_construct_qpa($qpa, ' AND doc_data.stateid = 1');
87 $qpa = db_construct_qpa ($qpa,
89 $qpa = $this->addIlikeCondition ($qpa, 'full_string_agg') ;
90 $qpa = db_construct_qpa ($qpa,
91 ' ORDER BY x.groupname, x.docid') ;
96 function getFTIQuery() {
97 $words = $this->getFTIwords();
98 $group_id=$this->groupId;
100 $qpa = db_construct_qpa () ;
102 $qpa = db_construct_qpa ($qpa,
103 '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',
104 array ($this->field_separator,
106 $qpa = db_construct_qpa ($qpa,
107 ' WHERE doc_data.doc_group = doc_groups.doc_group AND doc_data.docid = doc_data_idx.docid AND (vectors @@ to_tsquery($1)',
109 $qpa = db_construct_qpa ($qpa,
110 ') AND doc_data.group_id = $1',
112 if ($this->sections != SEARCH__ALL_SECTIONS) {
113 $qpa = db_construct_qpa ($qpa,
114 ' AND doc_groups.doc_group = ANY ($1)',
115 array( db_int_array_to_any_clause ($this->sections))) ;
117 if ($this->showNonPublic) {
118 $qpa = db_construct_qpa ($qpa,
119 ' AND doc_data.stateid IN (1, 4, 5)') ;
121 $qpa = db_construct_qpa ($qpa,
122 ' AND doc_data.stateid = 1') ;
124 $qpa = db_construct_qpa ($qpa,
126 if (count($this->phrases)) {
127 $qpa = db_construct_qpa ('WHERE ') ;
128 $qpa = $this->addMatchCondition($qpa, 'full_string_agg');
130 $qpa = db_construct_qpa ($qpa,
131 ' ORDER BY ts_rank(vectors, to_tsquery($1)) DESC, groupname ASC',
137 * getSections - returns the list of available doc groups
139 * @param $groupId int group id
140 * @param $showNonPublic boolean if we should consider non public sections
142 static function getSections($groupId, $showNonPublic = false) {
143 if (!forge_check_perm('docman',$groupId,'read')) {
147 $sql = 'SELECT doc_groups.doc_group, doc_groups.groupname FROM doc_groups, doc_data'
148 .' WHERE doc_groups.doc_group = doc_data.doc_group AND doc_groups.group_id = $1';
149 if ($showNonPublic) {
150 $sql .= ' AND doc_data.stateid IN (1, 4, 5) AND doc_groups.stateid = 1';
152 $sql .= ' AND doc_data.stateid = 1 AND doc_groups.stateid = 1';
154 $sql .= ' ORDER BY groupname';
157 $res = db_query_params($sql,
159 while($data = db_fetch_array($res)) {
160 $sections[$data['doc_group']] = $data['groupname'];
168 // c-file-style: "bsd"