]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/search.pm
* Avoid syntax errors in templates used by the template plugin crashing
[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}."/".$_)
67                                 foreach @{$renderedfiles{pagename($_)}};
68                 } @_
69         );
70         estcfg();
71 } #}}}
72
73 sub cgi ($) { #{{{
74         my $cgi=shift;
75
76         if (defined $cgi->param('phrase')) {
77                 # only works for GET requests
78                 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
79                 exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
80         }
81 } #}}}
82
83 my $configured=0;
84 sub estcfg () { #{{{
85         return if $configured;
86         $configured=1;
87         
88         my $estdir="$config{wikistatedir}/hyperestraier";
89         my $cgi=IkiWiki::basename($config{cgiurl});
90         $cgi=~s/\..*$//;
91         open(TEMPLATE, ">:utf8", "$estdir/$cgi.tmpl") ||
92                 error("write $estdir/$cgi.tmpl: $!");
93         print TEMPLATE IkiWiki::misctemplate("search", 
94                 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
95                 baseurl => IkiWiki::dirname($config{cgiurl})."/");
96         close TEMPLATE;
97         open(TEMPLATE, ">$estdir/$cgi.conf") ||
98                 error("write $estdir/$cgi.conf: $!");
99         my $template=template("estseek.conf");
100         eval q{use Cwd 'abs_path'};
101         $template->param(
102                 index => $estdir,
103                 tmplfile => "$estdir/$cgi.tmpl",
104                 destdir => abs_path($config{destdir}),
105                 url => $config{url},
106         );
107         print TEMPLATE $template->output;
108         close TEMPLATE;
109         $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
110         unlink($cgi);
111         my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
112         symlink($estseek, $cgi) ||
113                 error("symlink $estseek $cgi: $!");
114 } # }}}
115
116 sub estcmd ($;@) { #{{{
117         my @params=split(' ', shift);
118         push @params, "-cl", "$config{wikistatedir}/hyperestraier";
119         if (@_) {
120                 push @params, "-";
121         }
122         
123         my $pid=open(CHILD, "|-");
124         if ($pid) {
125                 # parent
126                 foreach (@_) {
127                         print CHILD "$_\n";
128                 }
129                 close(CHILD) || error("estcmd @params exited nonzero: $?");
130         }
131         else {
132                 # child
133                 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
134                 exec("estcmd", @params) || error("can't run estcmd");
135         }
136 } #}}}
137
138 1