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 ForumsSearchQuery 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 ForumsSearchQuery($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
70 $qpa = db_construct_qpa () ;
72 if (forge_get_config('use_fti')) {
75 if ($this->showNonPublic) {
78 if ($this->sections != SEARCH__ALL_SECTIONS) {
79 $sections = $this->sections;
82 $qpa = db_construct_qpa ($qpa,
83 'SELECT forum.group_forum_id, forum.msg_id, ts_headline(forum.subject, q) AS subject, forum.post_date, users.realname, forum_group_list.forum_name, forum.subject||$2||forum.body as full_string_agg FROM forum, users, forum_group_list, forum_idx, to_tsquery($1) as q ',
84 array ($this->getFTIwords(),
85 $this->field_separator)) ;
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 ') ;
99 if(count($this->phrases)) {
100 $qpa = db_construct_qpa ($qpa,
102 $qpa = $this->addMatchCondition($qpa, 'full_string_agg');
103 $qpa = db_construct_qpa ($qpa,
107 $qpa = db_construct_qpa ($qpa,
108 'ORDER BY forum_group_list.forum_name ASC, forum.msg_id ASC, ts_rank(vectors, q) DESC') ;
110 $qpa = db_construct_qpa ($qpa,
111 'SELECT x.* FROM (SELECT forum.group_forum_id, forum.msg_id, forum.subject, forum.post_date, users.realname, forum_group_list.forum_name, forum.subject||$1||forum.body as full_string_agg 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 = $2) ',
112 array ($this->field_separator,
114 if ($this->sections != SEARCH__ALL_SECTIONS) {
115 $qpa = db_construct_qpa ($qpa,
116 'AND forum_group_list.group_forum_id = ANY ($1) ',
117 array (db_int_array_to_any_clause ($this->sections))) ;
119 if (!$this->showNonPublic) {
120 $qpa = db_construct_qpa ($qpa,
121 'AND forum_group_list.is_public = 1 ') ;
123 $qpa = db_construct_qpa ($qpa,
125 $qpa = $this->addIlikeCondition ($qpa, 'full_string_agg') ;
126 $qpa = db_construct_qpa ($qpa,
127 ' ORDER BY x.forum_name, x.msg_id') ;
133 * getSearchByIdQuery - get the sql query built to get the search results when we are looking for an int
135 * @return array query+params array
137 function getSearchByIdQuery() {
138 $qpa = db_construct_qpa () ;
139 $qpa = db_construct_qpa ($qpa,
140 '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',
141 array ($this->searchId,
143 if (!$this->showNonPublic) {
144 $qpa = db_construct_qpa ($qpa,
145 ' AND forum_group_list.is_public=1') ;
152 * getSections - returns the list of available forums
154 * @param $groupId int group id
155 * @param $showNonPublic boolean if we should consider non public sections
157 static function getSections($groupId, $showNonPublic=false) {
158 $sql = 'SELECT group_forum_id, forum_name FROM forum_group_list WHERE group_id = $1 AND is_public <> 9';
159 $sql .= ' ORDER BY forum_name';
162 $res = db_query_params ($sql,
164 while($data = db_fetch_array($res)) {
165 if (forge_check_perm('forum',$data['group_forum_id'],'read')) {
166 $sections[$data['group_forum_id']] = $data['forum_name'];
175 // c-file-style: "bsd"