]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/search.pm
web commit by JoshTriplett: Add shortcut for wikitravel.
[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         error($@) if $@;
27         Getopt::Long::Configure('pass_through');
28         GetOptions("estseek=s" => \$config{estseek});
29 } #}}}
30
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 } #}}}
38
39 my $form;
40 sub pagetemplate (@) { #{{{
41         my %params=@_;
42         my $page=$params{page};
43         my $template=$params{template};
44
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                 }
52
53                 $template->param(searchform => $form);
54         }
55 } #}}}
56
57 sub delete (@) { #{{{
58         debug(gettext("cleaning hyperestraier search index"));
59         estcmd("purge -cl");
60         estcfg();
61 } #}}}
62
63 sub change (@) { #{{{
64         debug(gettext("updating hyperestraier search index"));
65         estcmd("gather -cm -bc -cl -sd",
66                 map {
67                         Encode::encode_utf8($config{destdir}."/".$_)
68                                 foreach @{$renderedfiles{pagename($_)}};
69                 } @_
70         );
71         estcfg();
72 } #}}}
73
74 sub cgi ($) { #{{{
75         my $cgi=shift;
76
77         if (defined $cgi->param('phrase') || defined $cgi->param("navi")) {
78                 # only works for GET requests
79                 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
80                 exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
81         }
82 } #}}}
83
84 my $configured=0;
85 sub estcfg () { #{{{
86         return if $configured;
87         $configured=1;
88         
89         my $estdir="$config{wikistatedir}/hyperestraier";
90         my $cgi=IkiWiki::basename($config{cgiurl});
91         $cgi=~s/\..*$//;
92
93         my $newfile="$estdir/$cgi.tmpl.new";
94         my $cleanup = sub { unlink($newfile) };
95         open(TEMPLATE, ">:utf8", $newfile) || error("open $newfile: $!", $cleanup);
96         print TEMPLATE IkiWiki::misctemplate("search", 
97                 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
98                 baseurl => IkiWiki::dirname($config{cgiurl})."/") ||
99                         error("write $newfile: $!", $cleanup);
100         close TEMPLATE || error("save $newfile: $!", $cleanup);
101         rename($newfile, "$estdir/$cgi.tmpl") ||
102                 error("rename $newfile: $!", $cleanup);
103         
104         $newfile="$estdir/$cgi.conf";
105         open(TEMPLATE, ">$newfile") || error("open $newfile: $!", $cleanup);
106         my $template=template("estseek.conf");
107         eval q{use Cwd 'abs_path'};
108         $template->param(
109                 index => $estdir,
110                 tmplfile => "$estdir/$cgi.tmpl",
111                 destdir => abs_path($config{destdir}),
112                 url => $config{url},
113         );
114         print TEMPLATE $template->output || error("write $newfile: $!", $cleanup);
115         close TEMPLATE || error("save $newfile: $!", $cleanup);
116         rename($newfile, "$estdir/$cgi.conf") ||
117                 error("rename $newfile: $!", $cleanup);
118
119         $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
120         unlink($cgi);
121         my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
122         symlink($estseek, $cgi) || error("symlink $estseek $cgi: $!");
123 } # }}}
124
125 sub estcmd ($;@) { #{{{
126         my @params=split(' ', shift);
127         push @params, "-cl", "$config{wikistatedir}/hyperestraier";
128         if (@_) {
129                 push @params, "-";
130         }
131         
132         my $pid=open(CHILD, "|-");
133         if ($pid) {
134                 # parent
135                 foreach (@_) {
136                         print CHILD "$_\n";
137                 }
138                 close(CHILD) || error("estcmd @params exited nonzero: $?");
139         }
140         else {
141                 # child
142                 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
143                 exec("estcmd", @params) || error("can't run estcmd");
144         }
145 } #}}}
146
147 1