]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/search.pm
let's actually work, shall we?
[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         IkiWiki::hook(type => "checkconfig", id => "hyperestraier",
11                 call => \&checkconfig);
12         IkiWiki::hook(type => "delete", id => "hyperestraier",
13                 call => \&delete);
14         IkiWiki::hook(type => "render", id => "hyperestraier",
15                 call => \&render);
16         IkiWiki::hook(type => "cgi", id => "hyperestraier",
17                 call => \&cgi);
18 } # }}}
19
20 sub checkconfig () { #{{{
21         foreach my $required (qw(url cgiurl)) {
22                 if (! length $IkiWiki::config{$required}) {
23                         IkiWiki::error("Must specify $required when using the search plugin\n");
24                 }
25         }
26
27         $IkiWiki::config{headercontent}.=qq{
28 <form method="get" action="$IkiWiki::config{cgiurl}" id="searchform">
29 <div>
30 <input type="text" name="phrase" value="" size="16" />
31 <input type="hidden" name="enc" value="UTF-8" />
32 <input type="hidden" name="do" value="hyperestraier" />
33 </div>
34 </form>
35 };
36 } #}}}
37
38 sub delete (@) { #{{{
39         IkiWiki::debug("cleaning hyperestraier search index");
40         IkiWiki::estcmd("purge -cl");
41         IkiWiki::estcfg();
42 } #}}}
43
44 sub render (@) { #{{{
45         IkiWiki::debug("updating hyperestraier search index");
46         IkiWiki::estcmd("gather -cm -bc -cl -sd",
47                 map {
48                         $IkiWiki::config{destdir}."/".$IkiWiki::renderedfiles{IkiWiki::pagename($_)}
49                 } @_
50         );
51         IkiWiki::estcfg();
52 } #}}}
53
54 sub cgi ($) { #{{{
55         my $cgi=shift;
56
57         if (defined $cgi->param('phrase')) {
58                 # only works for GET requests
59                 chdir("$IkiWiki::config{wikistatedir}/hyperestraier") || IkiWiki::error("chdir: $!");
60                 exec("./".IkiWiki::basename($IkiWiki::config{cgiurl})) || IkiWiki::error("estseek.cgi failed");
61         }
62 } #}}}
63
64 # Easier to keep these in the IkiWiki namespace.
65 package IkiWiki;
66
67 my $configured=0;
68 sub estcfg () { #{{{
69         return if $configured;
70         $configured=1;
71         
72         my $estdir="$config{wikistatedir}/hyperestraier";
73         my $cgi=basename($config{cgiurl});
74         $cgi=~s/\..*$//;
75         open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
76                 error("write $estdir/$cgi.tmpl: $!");
77         print TEMPLATE misctemplate("search", 
78                 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
79         close TEMPLATE;
80         open(TEMPLATE, ">$estdir/$cgi.conf") ||
81                 error("write $estdir/$cgi.conf: $!");
82         my $template=HTML::Template->new(
83                 filename => "$config{templatedir}/estseek.conf"
84         );
85         eval q{use Cwd 'abs_path'};
86         $template->param(
87                 index => $estdir,
88                 tmplfile => "$estdir/$cgi.tmpl",
89                 destdir => abs_path($config{destdir}),
90                 url => $config{url},
91         );
92         print TEMPLATE $template->output;
93         close TEMPLATE;
94         $cgi="$estdir/".basename($config{cgiurl});
95         unlink($cgi);
96         symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
97                 error("symlink $cgi: $!");
98 } # }}}
99
100 sub estcmd ($;@) { #{{{
101         my @params=split(' ', shift);
102         push @params, "-cl", "$config{wikistatedir}/hyperestraier";
103         if (@_) {
104                 push @params, "-";
105         }
106         
107         my $pid=open(CHILD, "|-");
108         if ($pid) {
109                 # parent
110                 foreach (@_) {
111                         print CHILD "$_\n";
112                 }
113                 close(CHILD) || error("estcmd @params exited nonzero: $?");
114         }
115         else {
116                 # child
117                 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
118                 exec("estcmd", @params) || error("can't run estcmd");
119         }
120 } #}}}
121
122 1