]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/search.pm
Fix ugly display when editing a page that has vanished.
[git.ikiwiki.info.git] / IkiWiki / Plugin / search.pm
1 #!/usr/bin/perl
2 # hyperestraier search engine plugin
3 package IkiWiki::Plugin::search;
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
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 } # }}}
24 sub getopt () { #{{{
25         eval q{use Getopt::Long};
26         error($@) if $@;
27         Getopt::Long::Configure('pass_through');
28         GetOptions("estseek=s" => \$config{estseek});
29 } #}}}
31 sub checkconfig () { #{{{
32         foreach my $required (qw(url cgiurl)) {
33                 if (! length $config{$required}) {
34                         error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
35                 }
36         }
37 } #}}}
39 my $form;
40 sub pagetemplate (@) { #{{{
41         my %params=@_;
42         my $page=$params{page};
43         my $template=$params{template};
45         # Add search box to page header.
46         if ($template->query(name => "searchform")) {
47                 if (! defined $form) {
48                         my $searchform = template("searchform.tmpl", blind_cache => 1);
49                         $searchform->param(searchaction => $config{cgiurl});
50                         $form=$searchform->output;
51                 }
53                 $template->param(searchform => $form);
54         }
55 } #}}}
57 sub delete (@) { #{{{
58         debug(gettext("cleaning hyperestraier search index"));
59         estcmd("purge -cl");
60         estcfg();
61 } #}}}
63 sub change (@) { #{{{
64         debug(gettext("updating hyperestraier search index"));
65         estcmd("gather -cm -bc -cl -sd",
66                 map {
67                         map {
68                                 Encode::encode_utf8($config{destdir}."/".$_)
69                         } @{$renderedfiles{pagename($_)}};
70                 } @_
71         );
72         estcfg();
73 } #}}}
75 sub cgi ($) { #{{{
76         my $cgi=shift;
78         if (defined $cgi->param('phrase') || defined $cgi->param("navi")) {
79                 # only works for GET requests
80                 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
81                 exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
82         }
83 } #}}}
85 my $configured=0;
86 sub estcfg () { #{{{
87         return if $configured;
88         $configured=1;
89         
90         my $estdir="$config{wikistatedir}/hyperestraier";
91         my $cgi=IkiWiki::basename($config{cgiurl});
92         $cgi=~s/\..*$//;
94         my $newfile="$estdir/$cgi.tmpl.new";
95         my $cleanup = sub { unlink($newfile) };
96         open(TEMPLATE, ">:utf8", $newfile) || error("open $newfile: $!", $cleanup);
97         print TEMPLATE IkiWiki::misctemplate("search", 
98                 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
99                 forcebaseurl => IkiWiki::dirname($config{cgiurl})."/") ||
100                         error("write $newfile: $!", $cleanup);
101         close TEMPLATE || error("save $newfile: $!", $cleanup);
102         rename($newfile, "$estdir/$cgi.tmpl") ||
103                 error("rename $newfile: $!", $cleanup);
104         
105         $newfile="$estdir/$cgi.conf";
106         open(TEMPLATE, ">$newfile") || error("open $newfile: $!", $cleanup);
107         my $template=template("estseek.conf");
108         eval q{use Cwd 'abs_path'};
109         $template->param(
110                 index => $estdir,
111                 tmplfile => "$estdir/$cgi.tmpl",
112                 destdir => abs_path($config{destdir}),
113                 url => $config{url},
114         );
115         print TEMPLATE $template->output || error("write $newfile: $!", $cleanup);
116         close TEMPLATE || error("save $newfile: $!", $cleanup);
117         rename($newfile, "$estdir/$cgi.conf") ||
118                 error("rename $newfile: $!", $cleanup);
120         $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
121         unlink($cgi);
122         my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
123         symlink($estseek, $cgi) || error("symlink $estseek $cgi: $!");
124 } # }}}
126 sub estcmd ($;@) { #{{{
127         my @params=split(' ', shift);
128         push @params, "-cl", "$config{wikistatedir}/hyperestraier";
129         if (@_) {
130                 push @params, "-";
131         }
132         
133         my $pid=open(CHILD, "|-");
134         if ($pid) {
135                 # parent
136                 foreach (@_) {
137                         print CHILD "$_\n";
138                 }
139                 close(CHILD) || print STDERR "estcmd @params exited nonzero: $?\n";
140         }
141         else {
142                 # child
143                 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
144                 exec("estcmd", @params) || error("can't run estcmd");
145         }
146 } #}}}