2 # hyperestraier search engine plugin
3 package IkiWiki::Plugin::search;
10 hook(type => "checkconfig", id => "hyperestraier",
11 call => \&checkconfig);
12 hook(type => "pagetemplate", id => "hyperestraier",
13 call => \&pagetemplate);
14 hook(type => "delete", id => "hyperestraier",
16 hook(type => "change", id => "hyperestraier",
18 hook(type => "cgi", id => "hyperestraier",
22 sub checkconfig () { #{{{
23 foreach my $required (qw(url cgiurl)) {
24 if (! length $config{$required}) {
25 error("Must specify $required when using the search plugin\n");
31 sub pagetemplate (@) { #{{{
33 my $page=$params{page};
34 my $template=$params{template};
36 # Add search box to page header.
37 if ($template->query(name => "searchform")) {
38 if (! defined $form) {
39 my $searchform = template("searchform.tmpl", blind_cache => 1);
40 $searchform->param(searchaction => $config{cgiurl});
41 $form=$searchform->output;
44 $template->param(searchform => $form);
49 debug("cleaning hyperestraier search index");
55 debug("updating hyperestraier search index");
56 estcmd("gather -cm -bc -cl -sd",
58 Encode::encode_utf8($config{destdir}."/".$renderedfiles{pagename($_)})
67 if (defined $cgi->param('phrase')) {
68 # only works for GET requests
69 chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
70 exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
76 return if $configured;
79 my $estdir="$config{wikistatedir}/hyperestraier";
80 my $cgi=IkiWiki::basename($config{cgiurl});
82 open(TEMPLATE, ">:utf8", "$estdir/$cgi.tmpl") ||
83 error("write $estdir/$cgi.tmpl: $!");
84 print TEMPLATE IkiWiki::misctemplate("search",
85 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
86 baseurl => IkiWiki::dirname($config{cgiurl})."/");
88 open(TEMPLATE, ">$estdir/$cgi.conf") ||
89 error("write $estdir/$cgi.conf: $!");
90 my $template=template("estseek.conf");
91 eval q{use Cwd 'abs_path'};
94 tmplfile => "$estdir/$cgi.tmpl",
95 destdir => abs_path($config{destdir}),
98 print TEMPLATE $template->output;
100 $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
102 symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
103 error("symlink $cgi: $!");
106 sub estcmd ($;@) { #{{{
107 my @params=split(' ', shift);
108 push @params, "-cl", "$config{wikistatedir}/hyperestraier";
113 my $pid=open(CHILD, "|-");
119 close(CHILD) || error("estcmd @params exited nonzero: $?");
123 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
124 exec("estcmd", @params) || error("can't run estcmd");