1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
4 # This program is free software; you can redistribute it and/or modify *
5 # it under the terms of the GNU General Public License as published by *
6 # the Free Software Foundation; either version 2 of the License, or *
7 # (at your option) any later version. *
9 # This program is distributed in the hope that it will be useful, *
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 # GNU General Public License for more details. *
14 # You should have received a copy of the GNU General Public License *
15 # along with this program; if not, write to the *
16 # Free Software Foundation, Inc., *
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 #**************************************************************************/
20 package NDWeb::Pages::Forum::Search;
24 use CGI qw/:standard/;
28 use base qw/NDWeb::Pages::Forum/;
33 my $DBH = $self->{DBH};
34 $self->{TITLE} = 'Forum';
37 push @queries,'('.param('search').')';
39 my %cat = (body => 'D', topic => 'A', author => 'B');
40 for ('body','topic','author'){
42 my @words = split /\W+/,param($_);
43 my $op = param('all'.$_) ? '&' : '|';
45 my $query = join " $op ", map {"$_:$cat"} @words;
46 push @queries,"($query)";
49 my $search = join ' & ', @queries;
52 my $posts = $DBH->prepare(q{SELECT fp.ftid,u.username,ft.subject
53 ,ts_headline(fp.message,to_tsquery($2)) AS headline
54 ,ts_rank_cd(fp.textsearch, to_tsquery($2),32) AS rank
56 JOIN forum_threads ft USING (fbid)
57 JOIN forum_posts fp USING (ftid)
58 JOIN users u ON fp.uid = u.uid
59 WHERE fb.fbid IN (SELECT fbid FROM forum_access
60 WHERE gid IN (SELECT groups($1)))
61 AND fp.textsearch @@@ to_tsquery($2)
63 }) or warn $DBH->errstr;
64 $posts->execute($ND::UID,$search) or warn escapeHTML $DBH->errstr;
66 while (my $post = $posts->fetchrow_hashref){
69 $BODY->param(SearchResult => \@posts);