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 (@) { #{{{
32 my $page=$params{page};
33 my $template=$params{template};
35 # Add search box to page header.
36 if ($template->query(name => "searchform")) {
37 $template->param(searchform => qq{
38 <form method="get" action="$IkiWiki::config{cgiurl}" id="searchform">
40 <input type="text" name="phrase" value="" size="16" />
41 <input type="hidden" name="enc" value="UTF-8" />
42 <input type="hidden" name="do" value="hyperestraier" />
50 IkiWiki::debug("cleaning hyperestraier search index");
51 IkiWiki::estcmd("purge -cl");
56 IkiWiki::debug("updating hyperestraier search index");
57 IkiWiki::estcmd("gather -cm -bc -cl -sd",
59 Encode::encode_utf8($IkiWiki::config{destdir}."/".$IkiWiki::renderedfiles{IkiWiki::pagename($_)})
68 if (defined $cgi->param('phrase')) {
69 # only works for GET requests
70 chdir("$IkiWiki::config{wikistatedir}/hyperestraier") || IkiWiki::error("chdir: $!");
71 exec("./".IkiWiki::basename($IkiWiki::config{cgiurl})) || IkiWiki::error("estseek.cgi failed");
75 # Easier to keep these in the IkiWiki namespace.
80 return if $configured;
83 my $estdir="$config{wikistatedir}/hyperestraier";
84 my $cgi=basename($config{cgiurl});
86 open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
87 error("write $estdir/$cgi.tmpl: $!");
88 print TEMPLATE misctemplate("search",
89 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
91 open(TEMPLATE, ">$estdir/$cgi.conf") ||
92 error("write $estdir/$cgi.conf: $!");
93 my $template=template("estseek.conf");
94 eval q{use Cwd 'abs_path'};
97 tmplfile => "$estdir/$cgi.tmpl",
98 destdir => abs_path($config{destdir}),
101 print TEMPLATE $template->output;
103 $cgi="$estdir/".basename($config{cgiurl});
105 symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
106 error("symlink $cgi: $!");
109 sub estcmd ($;@) { #{{{
110 my @params=split(' ', shift);
111 push @params, "-cl", "$config{wikistatedir}/hyperestraier";
116 my $pid=open(CHILD, "|-");
122 close(CHILD) || error("estcmd @params exited nonzero: $?");
126 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
127 exec("estcmd", @params) || error("can't run estcmd");