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};
27 Getopt::Long::Configure('pass_through');
28 GetOptions("estseek=s" => \$config{estseek});
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");
40 sub pagetemplate (@) { #{{{
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;
53 $template->param(searchform => $form);
58 debug("cleaning hyperestraier search index");
64 debug("updating hyperestraier search index");
65 estcmd("gather -cm -bc -cl -sd",
67 Encode::encode_utf8($config{destdir}."/".$_)
68 foreach @{$renderedfiles{pagename($_)}};
77 if (defined $cgi->param('phrase')) {
78 # only works for GET requests
79 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
80 exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
86 return if $configured;
89 my $estdir="$config{wikistatedir}/hyperestraier";
90 my $cgi=IkiWiki::basename($config{cgiurl});
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})."/");
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'};
104 tmplfile => "$estdir/$cgi.tmpl",
105 destdir => abs_path($config{destdir}),
108 print TEMPLATE $template->output;
110 $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
112 my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
113 symlink($estseek, $cgi) ||
114 error("symlink $estseek $cgi: $!");
117 sub estcmd ($;@) { #{{{
118 my @params=split(' ', shift);
119 push @params, "-cl", "$config{wikistatedir}/hyperestraier";
124 my $pid=open(CHILD, "|-");
130 close(CHILD) || error("estcmd @params exited nonzero: $?");
134 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
135 exec("estcmd", @params) || error("can't run estcmd");