]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/search.pm
* Add support for links of the form [[../foo]], this links to the page
[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("Must specify $required when using the search plugin\n");
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("cleaning hyperestraier search index");
59         estcmd("purge -cl");
60         estcfg();
61 } #}}}
62
63 sub change (@) { #{{{
64         debug("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         open(TEMPLATE, ">:utf8", "$estdir/$cgi.tmpl") ||
93                 error("write $estdir/$cgi.tmpl: $!");
94         print TEMPLATE IkiWiki::misctemplate("search", 
95                 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
96                 baseurl => IkiWiki::dirname($config{cgiurl})."/");
97         close TEMPLATE;
98         open(TEMPLATE, ">$estdir/$cgi.conf") ||
99                 error("write $estdir/$cgi.conf: $!");
100         my $template=template("estseek.conf");
101         eval q{use Cwd 'abs_path'};
102         $template->param(
103                 index => $estdir,
104                 tmplfile => "$estdir/$cgi.tmpl",
105                 destdir => abs_path($config{destdir}),
106                 url => $config{url},
107         );
108         print TEMPLATE $template->output;
109         close TEMPLATE;
110         $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
111         unlink($cgi);
112         my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
113         symlink($estseek, $cgi) ||
114                 error("symlink $estseek $cgi: $!");
115 } # }}}
116
117 sub estcmd ($;@) { #{{{
118         my @params=split(' ', shift);
119         push @params, "-cl", "$config{wikistatedir}/hyperestraier";
120         if (@_) {
121                 push @params, "-";
122         }
123         
124         my $pid=open(CHILD, "|-");
125         if ($pid) {
126                 # parent
127                 foreach (@_) {
128                         print CHILD "$_\n";
129                 }
130                 close(CHILD) || error("estcmd @params exited nonzero: $?");
131         }
132         else {
133                 # child
134                 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
135                 exec("estcmd", @params) || error("can't run estcmd");
136         }
137 } #}}}
138
139 1