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