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");
30 sub pagetemplate ($$) { #{{{
34 # Add search box to page header.
35 $template->param(searchform => qq{
36 <form method="get" action="$IkiWiki::config{cgiurl}" id="searchform">
38 <input type="text" name="phrase" value="" size="16" />
39 <input type="hidden" name="enc" value="UTF-8" />
40 <input type="hidden" name="do" value="hyperestraier" />
47 IkiWiki::debug("cleaning hyperestraier search index");
48 IkiWiki::estcmd("purge -cl");
53 IkiWiki::debug("updating hyperestraier search index");
54 IkiWiki::estcmd("gather -cm -bc -cl -sd",
56 $IkiWiki::config{destdir}."/".$IkiWiki::renderedfiles{IkiWiki::pagename($_)}
65 if (defined $cgi->param('phrase')) {
66 # only works for GET requests
67 chdir("$IkiWiki::config{wikistatedir}/hyperestraier") || IkiWiki::error("chdir: $!");
68 exec("./".IkiWiki::basename($IkiWiki::config{cgiurl})) || IkiWiki::error("estseek.cgi failed");
72 # Easier to keep these in the IkiWiki namespace.
77 return if $configured;
80 my $estdir="$config{wikistatedir}/hyperestraier";
81 my $cgi=basename($config{cgiurl});
83 open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
84 error("write $estdir/$cgi.tmpl: $!");
85 print TEMPLATE misctemplate("search",
86 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
88 open(TEMPLATE, ">$estdir/$cgi.conf") ||
89 error("write $estdir/$cgi.conf: $!");
90 my $template=HTML::Template->new(
91 filename => "$config{templatedir}/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");