]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/search.pm
* Updated ikiwiki.svgz from Recai, includes an icon and is used to generate
[ikiwiki.git] / IkiWiki / Plugin / search.pm
1 #!/usr/bin/perl
2 # hyperestraier search engine plugin
3 package IkiWiki::Plugin::search;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8
9 sub import { #{{{
10         hook(type => "checkconfig", id => "hyperestraier",
11                 call => \&checkconfig);
12         hook(type => "pagetemplate", id => "hyperestraier",
13                 call => \&pagetemplate);
14         hook(type => "delete", id => "hyperestraier",
15                 call => \&delete);
16         hook(type => "change", id => "hyperestraier",
17                 call => \&change);
18         hook(type => "cgi", id => "hyperestraier",
19                 call => \&cgi);
20 } # }}}
21
22 sub checkconfig () { #{{{
23         foreach my $required (qw(url cgiurl)) {
24                 if (! length $config{$required}) {
25                         error("Must specify $required when using the search plugin\n");
26                 }
27         }
28 } #}}}
29
30 my $form;
31 sub pagetemplate (@) { #{{{
32         my %params=@_;
33         my $page=$params{page};
34         my $template=$params{template};
35
36         # Add search box to page header.
37         if ($template->query(name => "searchform")) {
38                 if (! defined $form) {
39                         my $searchform = template("searchform.tmpl", blind_cache => 1);
40                         $searchform->param(searchaction => $config{cgiurl});
41                         $form=$searchform->output;
42                 }
43
44                 $template->param(searchform => $form);
45         }
46 } #}}}
47
48 sub delete (@) { #{{{
49         debug("cleaning hyperestraier search index");
50         estcmd("purge -cl");
51         estcfg();
52 } #}}}
53
54 sub change (@) { #{{{
55         debug("updating hyperestraier search index");
56         estcmd("gather -cm -bc -cl -sd",
57                 map {
58                         Encode::encode_utf8($config{destdir}."/".$renderedfiles{pagename($_)})
59                 } @_
60         );
61         estcfg();
62 } #}}}
63
64 sub cgi ($) { #{{{
65         my $cgi=shift;
66
67         if (defined $cgi->param('phrase')) {
68                 # only works for GET requests
69                 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
70                 exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
71         }
72 } #}}}
73
74 my $configured=0;
75 sub estcfg () { #{{{
76         return if $configured;
77         $configured=1;
78         
79         my $estdir="$config{wikistatedir}/hyperestraier";
80         my $cgi=IkiWiki::basename($config{cgiurl});
81         $cgi=~s/\..*$//;
82         open(TEMPLATE, ">:utf8", "$estdir/$cgi.tmpl") ||
83                 error("write $estdir/$cgi.tmpl: $!");
84         print TEMPLATE IkiWiki::misctemplate("search", 
85                 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
86                 baseurl => IkiWiki::dirname($config{cgiurl})."/");
87         close TEMPLATE;
88         open(TEMPLATE, ">$estdir/$cgi.conf") ||
89                 error("write $estdir/$cgi.conf: $!");
90         my $template=template("estseek.conf");
91         eval q{use Cwd 'abs_path'};
92         $template->param(
93                 index => $estdir,
94                 tmplfile => "$estdir/$cgi.tmpl",
95                 destdir => abs_path($config{destdir}),
96                 url => $config{url},
97         );
98         print TEMPLATE $template->output;
99         close TEMPLATE;
100         $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
101         unlink($cgi);
102         symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
103                 error("symlink $cgi: $!");
104 } # }}}
105
106 sub estcmd ($;@) { #{{{
107         my @params=split(' ', shift);
108         push @params, "-cl", "$config{wikistatedir}/hyperestraier";
109         if (@_) {
110                 push @params, "-";
111         }
112         
113         my $pid=open(CHILD, "|-");
114         if ($pid) {
115                 # parent
116                 foreach (@_) {
117                         print CHILD "$_\n";
118                 }
119                 close(CHILD) || error("estcmd @params exited nonzero: $?");
120         }
121         else {
122                 # child
123                 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
124                 exec("estcmd", @params) || error("can't run estcmd");
125         }
126 } #}}}
127
128 1