]> ruin.nu Git - ndwebbie.git/commitdiff
Added wiki search.
authorMichael Andreen <harv@ruin.nu>
Tue, 12 Aug 2008 15:36:43 +0000 (17:36 +0200)
committerMichael Andreen <harv@ruin.nu>
Tue, 12 Aug 2008 15:36:43 +0000 (17:36 +0200)
Both simple and more advanced search available.
Only searches pages the user has access to.

lib/NDWeb/Controller/Wiki.pm
root/lib/site/leftbar.tt2
root/src/wiki/search.tt2 [new file with mode: 0644]

index d15c609672670e43b3f9dbd477a0383f23b21229..1792fa1df22325adf5117211c9ef1da0644c1ef9 100644 (file)
@@ -152,6 +152,40 @@ sub postedit : Local {
        $c->stash(template => 'wiki/edit.tt2');
 }
 
+sub search : Local {
+       my ( $self, $c ) = @_;
+       my $dbh = $c->model;
+
+       if ($c->req->param('search')){
+               $c->stash(search => $c->req->param('search'));
+               my $queryfunc = 'plainto_tsquery';
+               $queryfunc = 'to_tsquery' if $c->req->param('advsearch');
+               my $posts = $dbh->prepare(q{SELECT wp.wpid,namespace,name
+                       ,(CASE WHEN namespace <> '' THEN namespace || ':' ELSE '' END) || name AS fullname
+                       ,ts_headline(wpr.text,}.$queryfunc.q{($2)) AS headline
+                       ,ts_rank_cd(textsearch, }.$queryfunc.q{($2),32) AS rank
+                       FROM wiki_pages wp
+                               JOIN wiki_page_revisions wpr USING (wprev)
+                       WHERE (namespace IN (SELECT namespace FROM wiki_namespace_access WHERE gid IN (SELECT groups($1)))
+                                       OR wp.wpid IN (SELECT wpid FROM wiki_page_access WHERE uid = $1))
+                               AND textsearch @@ }.$queryfunc.q{($2)
+                       ORDER BY rank DESC
+               });
+               eval {
+                       $posts->execute($c->stash->{UID},$c->req->param('search'));
+                       my @posts;
+                       while (my $post = $posts->fetchrow_hashref){
+                               push @posts,$post;
+                       }
+                       $c->stash(searchresults => \@posts);
+               };
+               if ($@){
+                       $c->stash( searcherror => $dbh->errstr);
+               }
+       }
+
+}
+
 sub findPage : Private {
        my ( $self, $c, $p ) = @_;
        my $dbh = $c->model;
index 8f42cfe2907aae8d7763d2b658a599c8473392c4..96d7cbc5c5e7785282546b73fb18fbc14dc39142 100644 (file)
@@ -2,6 +2,14 @@
        [% IF c.check_user_roles("member_menu") %]<li><a href="/members">Main page</a></li>[% END %]
 
        <li><a href="/wiki">Wiki</a></li>
+       <li>
+       <form action="/wiki/search" method="post">
+               <p>
+               <input type="text" name="search" value="">
+               <input type="submit" value="Wiki search">
+               </p>
+       </form>
+       </li>
        <li><a href="/forum">Forum</a></li>
        <li><a href="/forum/search">Forum search</a></li>
 [% IF user %]
diff --git a/root/src/wiki/search.tt2 b/root/src/wiki/search.tt2
new file mode 100644 (file)
index 0000000..20bc49a
--- /dev/null
@@ -0,0 +1,37 @@
+[% META title = 'Wiki search' %]
+
+<form action="[% c.uri_for('search') %]" method="post">
+
+<fieldset class="forum-post"> <legend>Wiki search</legend>
+       <p><input style="width:98%" type="text" name="search" value="[% search %]"></p>
+
+       <p>Use advanced search: <input type="checkbox" name="advsearch"></p>
+
+       Advanced search has the following syntax:
+       Use | (OR) or &amp; (AND) to separate words. Word:A searches for Word in
+       topic 'Two words' to search for a longer string. Word:D limits the search to just the message body.
+</fieldset>
+<p><input type="submit" name="cmd" value="Search"></p>
+</form>
+
+[% IF searcherror %]
+<p> Could not search, because of: </p>
+<p> [% searcherror | html %] </p>
+[% END %]
+
+[% IF searchresults %]
+<table>
+<tr>
+       <th>Page</th>
+       <th>Message</th>
+       <th>Rank</th>
+</tr>
+       [% FOR p IN searchresults %]
+       <tr align="left" class="[% IF loop.count % 2 == 0 %]even[% ELSE %]odd[% END %]">
+               <td><a href="[% c.uri_for('',p.fullname) %]">&nbsp;[% p.fullname %]&nbsp;</a></td>
+               <td align="center">[% p.headline %]</td>
+               <td>[% p.rank %]</td>
+       </tr>
+       [% END %]
+</table>
+[% END %]