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 ForumsSearchQuery 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 ForumsSearchQuery($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
71 $qpa = db_construct_qpa () ;
73 if (forge_get_config('use_fti')) {
76 if ($this->showNonPublic) {
79 if ($this->sections != SEARCH__ALL_SECTIONS) {
80 $sections = $this->sections;
83 $qpa = db_construct_qpa ($qpa,
84 'SELECT forum.msg_id, headline(forum.subject, q) AS subject, forum.post_date, users.realname, forum_group_list.forum_name FROM forum, users, forum_group_list, forum_idx, to_tsquery($1) as q ',
85 array ($this->getFormattedWords())) ;
86 $qpa = db_construct_qpa ($qpa,
87 'WHERE users.user_id = forum.posted_by AND vectors @@ q AND forum.msg_id = forum_idx.msg_id AND forum_group_list.group_forum_id = forum.group_forum_id AND forum_group_list.is_public <> 9 AND forum.group_forum_id IN (SELECT group_forum_id FROM forum_group_list WHERE group_id = $1) ',
88 array ($this->groupId));
89 if ($this->sections != SEARCH__ALL_SECTIONS) {
90 $qpa = db_construct_qpa ($qpa,
91 'AND forum_group_list.group_forum_id = ANY ($1) ',
92 array (db_int_array_to_any_clause ($this->sections))) ;
94 if (!$this->showNonPublic) {
95 $qpa = db_construct_qpa ($qpa,
96 'AND forum_group_list.is_public = 1 ') ;
98 $qpa = db_construct_qpa ($qpa,
99 'ORDER BY forum_group_list.forum_name ASC, forum.msg_id ASC, rank(vectors, q) DESC') ;
101 $qpa = db_construct_qpa ($qpa,
102 'SELECT forum.msg_id, forum.subject, forum.post_date, users.realname, forum_group_list.forum_name FROM forum, users, forum_group_list WHERE users.user_id = forum.posted_by AND forum_group_list.group_forum_id = forum.group_forum_id AND forum_group_list.is_public <> 9 AND forum.group_forum_id IN (SELECT group_forum_id FROM forum_group_list WHERE group_id = $1) ',
103 array ($this->groupId)) ;
104 if ($this->sections != SEARCH__ALL_SECTIONS) {
105 $qpa = db_construct_qpa ($qpa,
106 'AND forum_group_list.group_forum_id = ANY ($1) ',
107 array (db_int_array_to_any_clause ($this->sections))) ;
109 if (!$this->showNonPublic) {
110 $qpa = db_construct_qpa ($qpa,
111 'AND forum_group_list.is_public = 1 ') ;
113 $qpa = db_construct_qpa ($qpa,
115 $qpa = $this->addIlikeCondition ($qpa, 'forum.body') ;
116 $qpa = db_construct_qpa ($qpa,
118 $qpa = $this->addIlikeCondition ($qpa,'forum.subject') ;
119 $qpa = db_construct_qpa ($qpa,
120 ')) ORDER BY forum_group_list.forum_name, forum.msg_id') ;
126 * getSearchByIdQuery - get the sql query built to get the search results when we are looking for an int
128 * @return array query+params array
130 function getSearchByIdQuery() {
131 $qpa = db_construct_qpa () ;
132 $qpa = db_construct_qpa ($qpa,
133 'SELECT msg_id FROM forum, forum_group_list WHERE msg_id=$1 AND forum_group_list.group_forum_id=forum.group_forum_id AND group_forum_id=$2',
134 array ($this->searchId,
136 if (!$this->showNonPublic) {
137 $qpa = db_construct_qpa ($qpa,
138 ' AND forum_group_list.is_public=1') ;
145 * getSections - returns the list of available forums
147 * @param $groupId int group id
148 * @param $showNonPublic boolean if we should consider non public sections
150 function getSections($groupId, $showNonPublic=false) {
151 $sql = 'SELECT group_forum_id, forum_name FROM forum_group_list WHERE group_id = $1 AND is_public <> 9';
152 if (!$showNonPublic) {
153 $sql .= ' AND is_public = 1';
155 $sql .= ' ORDER BY forum_name';
158 $res = db_query_params ($sql,
160 while($data = db_fetch_array($res)) {
161 $sections[$data['group_forum_id']] = $data['forum_name'];
169 // c-file-style: "bsd"