2006-05-03 23:50:39 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# hyperestraier search engine plugin
|
|
|
|
package IkiWiki::Plugin::search;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use IkiWiki;
|
|
|
|
|
|
|
|
sub import { #{{{
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "checkconfig", id => "hyperestraier",
|
2006-05-03 23:50:39 +02:00
|
|
|
call => \&checkconfig);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "pagetemplate", id => "hyperestraier",
|
2006-05-26 03:10:58 +02:00
|
|
|
call => \&pagetemplate);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "delete", id => "hyperestraier",
|
2006-05-03 23:50:39 +02:00
|
|
|
call => \&delete);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "change", id => "hyperestraier",
|
2006-05-05 07:10:00 +02:00
|
|
|
call => \&change);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "cgi", id => "hyperestraier",
|
2006-05-03 23:50:39 +02:00
|
|
|
call => \&cgi);
|
|
|
|
} # }}}
|
|
|
|
|
|
|
|
sub checkconfig () { #{{{
|
|
|
|
foreach my $required (qw(url cgiurl)) {
|
2006-09-10 00:50:27 +02:00
|
|
|
if (! length $config{$required}) {
|
|
|
|
error("Must specify $required when using the search plugin\n");
|
2006-05-03 23:50:39 +02:00
|
|
|
}
|
|
|
|
}
|
2006-05-26 03:10:58 +02:00
|
|
|
} #}}}
|
|
|
|
|
2006-08-26 23:57:59 +02:00
|
|
|
my $form;
|
2006-07-28 01:41:58 +02:00
|
|
|
sub pagetemplate (@) { #{{{
|
|
|
|
my %params=@_;
|
|
|
|
my $page=$params{page};
|
|
|
|
my $template=$params{template};
|
2006-05-03 23:50:39 +02:00
|
|
|
|
2006-05-26 03:10:58 +02:00
|
|
|
# Add search box to page header.
|
2006-07-28 01:08:03 +02:00
|
|
|
if ($template->query(name => "searchform")) {
|
2006-08-26 23:57:59 +02:00
|
|
|
if (! defined $form) {
|
2006-09-10 00:50:27 +02:00
|
|
|
my $searchform = template("searchform.tmpl", blind_cache => 1);
|
|
|
|
$searchform->param(searchaction => $config{cgiurl});
|
2006-08-26 23:57:59 +02:00
|
|
|
$form=$searchform->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
$template->param(searchform => $form);
|
2006-07-28 01:08:03 +02:00
|
|
|
}
|
2006-05-03 23:50:39 +02:00
|
|
|
} #}}}
|
|
|
|
|
|
|
|
sub delete (@) { #{{{
|
2006-09-10 00:50:27 +02:00
|
|
|
debug("cleaning hyperestraier search index");
|
|
|
|
estcmd("purge -cl");
|
|
|
|
estcfg();
|
2006-05-03 23:50:39 +02:00
|
|
|
} #}}}
|
|
|
|
|
2006-05-05 07:10:00 +02:00
|
|
|
sub change (@) { #{{{
|
2006-09-10 00:50:27 +02:00
|
|
|
debug("updating hyperestraier search index");
|
|
|
|
estcmd("gather -cm -bc -cl -sd",
|
2006-05-03 23:50:39 +02:00
|
|
|
map {
|
2006-09-10 00:50:27 +02:00
|
|
|
Encode::encode_utf8($config{destdir}."/".$renderedfiles{pagename($_)})
|
2006-05-03 23:50:39 +02:00
|
|
|
} @_
|
|
|
|
);
|
2006-09-10 00:50:27 +02:00
|
|
|
estcfg();
|
2006-05-03 23:50:39 +02:00
|
|
|
} #}}}
|
|
|
|
|
|
|
|
sub cgi ($) { #{{{
|
|
|
|
my $cgi=shift;
|
|
|
|
|
|
|
|
if (defined $cgi->param('phrase')) {
|
|
|
|
# only works for GET requests
|
2006-09-10 00:50:27 +02:00
|
|
|
chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
|
|
|
|
exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
|
2006-05-03 23:50:39 +02:00
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
|
|
|
my $configured=0;
|
|
|
|
sub estcfg () { #{{{
|
|
|
|
return if $configured;
|
|
|
|
$configured=1;
|
|
|
|
|
|
|
|
my $estdir="$config{wikistatedir}/hyperestraier";
|
2006-09-10 00:50:27 +02:00
|
|
|
my $cgi=IkiWiki::basename($config{cgiurl});
|
2006-05-03 23:50:39 +02:00
|
|
|
$cgi=~s/\..*$//;
|
|
|
|
open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
|
|
|
|
error("write $estdir/$cgi.tmpl: $!");
|
2006-09-10 00:50:27 +02:00
|
|
|
print TEMPLATE IkiWiki::misctemplate("search",
|
2006-05-03 23:50:39 +02:00
|
|
|
"<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
|
|
|
|
close TEMPLATE;
|
|
|
|
open(TEMPLATE, ">$estdir/$cgi.conf") ||
|
|
|
|
error("write $estdir/$cgi.conf: $!");
|
2006-07-02 21:06:08 +02:00
|
|
|
my $template=template("estseek.conf");
|
2006-05-03 23:50:39 +02:00
|
|
|
eval q{use Cwd 'abs_path'};
|
|
|
|
$template->param(
|
|
|
|
index => $estdir,
|
|
|
|
tmplfile => "$estdir/$cgi.tmpl",
|
2006-09-10 00:50:27 +02:00
|
|
|
destdir => IkiWiki::abs_path($config{destdir}),
|
2006-05-03 23:50:39 +02:00
|
|
|
url => $config{url},
|
|
|
|
);
|
|
|
|
print TEMPLATE $template->output;
|
|
|
|
close TEMPLATE;
|
2006-09-10 00:50:27 +02:00
|
|
|
$cgi="$estdir/".IkiWiki::basename($config{cgiurl});
|
2006-05-03 23:50:39 +02:00
|
|
|
unlink($cgi);
|
|
|
|
symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
|
|
|
|
error("symlink $cgi: $!");
|
|
|
|
} # }}}
|
|
|
|
|
|
|
|
sub estcmd ($;@) { #{{{
|
|
|
|
my @params=split(' ', shift);
|
|
|
|
push @params, "-cl", "$config{wikistatedir}/hyperestraier";
|
|
|
|
if (@_) {
|
|
|
|
push @params, "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $pid=open(CHILD, "|-");
|
|
|
|
if ($pid) {
|
|
|
|
# parent
|
|
|
|
foreach (@_) {
|
|
|
|
print CHILD "$_\n";
|
|
|
|
}
|
|
|
|
close(CHILD) || error("estcmd @params exited nonzero: $?");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# child
|
|
|
|
open(STDOUT, "/dev/null"); # shut it up (closing won't work)
|
|
|
|
exec("estcmd", @params) || error("can't run estcmd");
|
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
|
|
|
1
|