2 # hyperestraier search engine plugin
3 package IkiWiki::Plugin::search;
10 hook(type => "getopt", id => "hyperestraier",
12 hook(type => "checkconfig", id => "hyperestraier",
13 call => \&checkconfig);
14 hook(type => "pagetemplate", id => "hyperestraier",
15 call => \&pagetemplate);
16 hook(type => "delete", id => "hyperestraier",
18 hook(type => "change", id => "hyperestraier",
20 hook(type => "cgi", id => "hyperestraier",
25 eval q{use Getopt::Long};
26 Getopt::Long::Configure('pass_through');
27 GetOptions("estseek=s" => \$config{estseek});
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");
39 sub pagetemplate (@) { #{{{
41 my $page=$params{page};
42 my $template=$params{template};
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;
52 $template->param(searchform => $form);
57 debug("cleaning hyperestraier search index");
63 debug("updating hyperestraier search index");
64 estcmd("gather -cm -bc -cl -sd",
66 Encode::encode_utf8($config{destdir}."/".$_)
67 foreach @{$renderedfiles{pagename($_)}};
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");
85 return if $configured;
88 my $estdir="$config{wikistatedir}/hyperestraier";
89 my $cgi=IkiWiki::basename($config{cgiurl});
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})."/");
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'};
103 tmplfile => "$estdir/$cgi.tmpl",
104 destdir => abs_path($config{destdir}),
107 print TEMPLATE $template->output;
109 $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
111 my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
112 symlink($estseek, $cgi) ||
113 error("symlink $estseek $cgi: $!");
116 sub estcmd ($;@) { #{{{
117 my @params=split(' ', shift);
118 push @params, "-cl", "$config{wikistatedir}/hyperestraier";
123 my $pid=open(CHILD, "|-");
129 close(CHILD) || error("estcmd @params exited nonzero: $?");
133 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
134 exec("estcmd", @params) || error("can't run estcmd");