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(sprintf(gettext("Must specify %s when using the search plugin"), $required));
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(gettext("cleaning hyperestraier search index"));
64 debug(gettext("updating hyperestraier search index"));
65 estcmd("gather -cm -bc -cl -sd",
68 Encode::encode_utf8($config{destdir}."/".$_)
69 } @{$renderedfiles{pagename($_)}};
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");
87 return if $configured;
90 my $estdir="$config{wikistatedir}/hyperestraier";
91 my $cgi=IkiWiki::basename($config{cgiurl});
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);
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'};
111 tmplfile => "$estdir/$cgi.tmpl",
112 destdir => abs_path($config{destdir}),
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});
122 my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
123 symlink($estseek, $cgi) || error("symlink $estseek $cgi: $!");
126 sub estcmd ($;@) { #{{{
127 my @params=split(' ', shift);
128 push @params, "-cl", "$config{wikistatedir}/hyperestraier";
133 my $pid=open(CHILD, "|-");
139 close(CHILD) || print STDERR "estcmd @params exited nonzero: $?\n";
143 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
144 exec("estcmd", @params) || error("can't run estcmd");