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 boolean $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 array $sections sections to search in
53 * @param boolean $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
72 $qpa = db_construct_qpa () ;
74 if (forge_get_config('use_fti')) {
77 if ($this->showNonPublic) {
80 if ($this->sections != SEARCH__ALL_SECTIONS) {
81 $sections = $this->sections;
84 $qpa = db_construct_qpa ($qpa,
85 '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 ',
86 array ($this->getFTIwords(),
87 $this->field_separator)) ;
88 $qpa = db_construct_qpa ($qpa,
89 '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) ',
90 array ($this->groupId));
91 if ($this->sections != SEARCH__ALL_SECTIONS) {
92 $qpa = db_construct_qpa ($qpa,
93 'AND forum_group_list.group_forum_id = ANY ($1) ',
94 array (db_int_array_to_any_clause ($this->sections))) ;
97 if(count($this->phrases)) {
98 $qpa = db_construct_qpa ($qpa,
100 $qpa = $this->addMatchCondition($qpa, 'full_string_agg');
101 $qpa = db_construct_qpa ($qpa,
105 $qpa = db_construct_qpa ($qpa,
106 'ORDER BY forum_group_list.forum_name ASC, forum.msg_id ASC, ts_rank(vectors, q) DESC') ;
108 $qpa = db_construct_qpa ($qpa,
109 '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) ',
110 array ($this->field_separator,
112 if ($this->sections != SEARCH__ALL_SECTIONS) {
113 $qpa = db_construct_qpa ($qpa,
114 'AND forum_group_list.group_forum_id = ANY ($1) ',
115 array (db_int_array_to_any_clause ($this->sections))) ;
117 $qpa = db_construct_qpa ($qpa,
119 $qpa = $this->addIlikeCondition ($qpa, 'full_string_agg') ;
120 $qpa = db_construct_qpa ($qpa,
121 ' ORDER BY x.forum_name, x.msg_id') ;
127 * getSections - returns the list of available forums
129 * @param $groupId int group id
130 * @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"