2 # hyperestraier search engine plugin
3 package IkiWiki::Plugin::search;
10 IkiWiki::hook(type => "checkconfig", id => "hyperestraier",
11 call => \&checkconfig);
12 IkiWiki::hook(type => "pagetemplate", id => "hyperestraier",
13 call => \&pagetemplate);
14 IkiWiki::hook(type => "delete", id => "hyperestraier",
16 IkiWiki::hook(type => "change", id => "hyperestraier",
18 IkiWiki::hook(type => "cgi", id => "hyperestraier",
22 sub checkconfig () { #{{{
23 foreach my $required (qw(url cgiurl)) {
24 if (! length $IkiWiki::config{$required}) {
25 IkiWiki::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 = IkiWiki::template("searchform.tmpl", blind_cache => 1);
40 $searchform->param(searchaction => $IkiWiki::config{cgiurl});
41 $form=$searchform->output;
44 $template->param(searchform => $form);
49 IkiWiki::debug("cleaning hyperestraier search index");
50 IkiWiki::estcmd("purge -cl");
55 IkiWiki::debug("updating hyperestraier search index");
56 IkiWiki::estcmd("gather -cm -bc -cl -sd",
58 Encode::encode_utf8($IkiWiki::config{destdir}."/".$IkiWiki::renderedfiles{IkiWiki::pagename($_)})
67 if (defined $cgi->param('phrase')) {
68 # only works for GET requests
69 chdir("$IkiWiki::config{wikistatedir}/hyperestraier") || IkiWiki::error("chdir: $!");
70 exec("./".IkiWiki::basename($IkiWiki::config{cgiurl})) || IkiWiki::error("estseek.cgi failed");
74 # Easier to keep these in the IkiWiki namespace.
79 return if $configured;
82 my $estdir="$config{wikistatedir}/hyperestraier";
83 my $cgi=basename($config{cgiurl});
85 open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
86 error("write $estdir/$cgi.tmpl: $!");
87 print TEMPLATE misctemplate("search",
88 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
90 open(TEMPLATE, ">$estdir/$cgi.conf") ||
91 error("write $estdir/$cgi.conf: $!");
92 my $template=template("estseek.conf");
93 eval q{use Cwd 'abs_path'};
96 tmplfile => "$estdir/$cgi.tmpl",
97 destdir => abs_path($config{destdir}),
100 print TEMPLATE $template->output;
102 $cgi="$estdir/".basename($config{cgiurl});
104 symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
105 error("symlink $cgi: $!");
108 sub estcmd ($;@) { #{{{
109 my @params=split(' ', shift);
110 push @params, "-cl", "$config{wikistatedir}/hyperestraier";
115 my $pid=open(CHILD, "|-");
121 close(CHILD) || error("estcmd @params exited nonzero: $?");
125 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
126 exec("estcmd", @params) || error("can't run estcmd");