3 * FusionForge search engine
5 * Copyright 2004, Dominik Haas
6 * Copyright 2009, Roland Mas
7 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
8 * Copyright 2013, French Ministry of National Education
9 * http://fusionforge.org
11 * This file is part of FusionForge. FusionForge is free software;
12 * you can redistribute it and/or modify it under the terms of the
13 * GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the Licence, or (at your option)
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 require_once $gfcommon.'search/SearchQuery.class.php';
29 class ForumsSearchQuery extends SearchQuery {
39 * flag if non public items are returned
41 * @var boolean $showNonPublic
48 * @param string $words words we are searching for
49 * @param int $offset offset
50 * @param bool $isExact if we want to search for all the words or if only one matching the query is sufficient
51 * @param int $groupId group id
52 * @param string $sections sections to search in
53 * @param bool $showNonPublic flag if private sections are searched too
55 function __construct($words, $offset, $isExact, $groupId, $sections=SEARCH__ALL_SECTIONS, $showNonPublic=false) {
56 $this->groupId = $groupId;
57 $this->showNonPublic = $showNonPublic;
59 parent::__construct($words, $offset, $isExact);
61 $this->setSections($sections);
65 * getQuery - get the query built to get the search results
67 * @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.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 ',
85 array ($this->getFTIwords(),
86 $this->field_separator)) ;
87 $qpa = db_construct_qpa ($qpa,
88 '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_forum_id IN (SELECT group_forum_id FROM forum_group_list WHERE group_id = $1) ',
89 array ($this->groupId));
90 if ($this->sections != SEARCH__ALL_SECTIONS) {
91 $qpa = db_construct_qpa ($qpa,
92 'AND forum_group_list.group_forum_id = ANY ($1) ',
93 array (db_int_array_to_any_clause ($this->sections))) ;
96 if(count($this->phrases)) {
97 $qpa = db_construct_qpa ($qpa,
99 $qpa = $this->addMatchCondition($qpa, 'full_string_agg');
100 $qpa = db_construct_qpa ($qpa,
104 $qpa = db_construct_qpa ($qpa,
105 'ORDER BY forum_group_list.forum_name ASC, forum.msg_id ASC, ts_rank(vectors, q) DESC') ;
107 $qpa = db_construct_qpa ($qpa,
108 '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_forum_id IN (SELECT group_forum_id FROM forum_group_list WHERE group_id = $2) ',
109 array ($this->field_separator,
111 if ($this->sections != SEARCH__ALL_SECTIONS) {
112 $qpa = db_construct_qpa ($qpa,
113 'AND forum_group_list.group_forum_id = ANY ($1) ',
114 array (db_int_array_to_any_clause ($this->sections))) ;
116 $qpa = db_construct_qpa ($qpa,
118 $qpa = $this->addIlikeCondition ($qpa, 'full_string_agg') ;
119 $qpa = db_construct_qpa ($qpa,
120 ' ORDER BY x.forum_name, x.msg_id') ;
126 * getSections - returns the list of available forums
128 * @param $groupId int group id
129 * @param $showNonPublic boolean if we should consider non public sections
132 static function getSections($groupId, $showNonPublic=false) {
133 $sql = 'SELECT group_forum_id, forum_name FROM forum_group_list WHERE group_id = $1 AND ';
134 $sql .= 'group_forum_id NOT IN (SELECT forum_id FROM news_bytes) ORDER BY forum_name';
137 $res = db_query_params ($sql,
139 while($data = db_fetch_array($res)) {
140 if (forge_check_perm('forum',$data['group_forum_id'],'read')) {
141 $sections[$data['group_forum_id']] = $data['forum_name'];
150 // c-file-style: "bsd"