2006-05-03 23:50:39 +02:00
|
|
|
#!/usr/bin/perl
|
2008-06-03 21:29:54 +02:00
|
|
|
# xapian-omega search engine plugin
|
2006-05-03 23:50:39 +02:00
|
|
|
package IkiWiki::Plugin::search;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2007-04-27 04:55:52 +02:00
|
|
|
use IkiWiki 2.00;
|
2006-05-03 23:50:39 +02:00
|
|
|
|
|
|
|
sub import { #{{{
|
2008-06-03 21:29:54 +02:00
|
|
|
hook(type => "checkconfig", id => "search", call => \&checkconfig);
|
|
|
|
hook(type => "pagetemplate", id => "search", call => \&pagetemplate);
|
2008-06-04 06:58:46 +02:00
|
|
|
hook(type => "sanitize", id => "search", call => \&index);
|
2008-06-03 21:29:54 +02:00
|
|
|
hook(type => "delete", id => "search", call => \&delete);
|
|
|
|
hook(type => "cgi", id => "search", call => \&cgi);
|
2006-05-03 23:50:39 +02:00
|
|
|
} # }}}
|
|
|
|
|
|
|
|
sub checkconfig () { #{{{
|
|
|
|
foreach my $required (qw(url cgiurl)) {
|
2006-09-10 00:50:27 +02:00
|
|
|
if (! length $config{$required}) {
|
2006-12-29 05:38:40 +01:00
|
|
|
error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
|
2006-05-03 23:50:39 +02:00
|
|
|
}
|
|
|
|
}
|
2008-06-03 21:29:54 +02:00
|
|
|
|
|
|
|
if (! exists $config{omega_cgi}) {
|
|
|
|
$config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
|
|
|
|
writefile("omega.conf", $config{wikistatedir}."/xapian",
|
|
|
|
"database_dir .\n".
|
|
|
|
"template_dir ./templates\n");
|
|
|
|
writefile("query", $config{wikistatedir}."/xapian/templates",
|
|
|
|
IkiWiki::misctemplate(gettext("search"),
|
|
|
|
readfile(IkiWiki::template_file("searchquery.tmpl"))));
|
|
|
|
}
|
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
|
|
|
} #}}}
|
|
|
|
|
2008-06-04 05:52:56 +02:00
|
|
|
my $scrubber;
|
2008-06-04 06:58:46 +02:00
|
|
|
sub index (@) { #{{{
|
2008-06-04 03:14:56 +02:00
|
|
|
my %params=@_;
|
|
|
|
|
2008-06-04 06:58:46 +02:00
|
|
|
return $params{content} if %IkiWiki::preprocessing;
|
|
|
|
|
|
|
|
my $db=xapiandb();
|
|
|
|
my $doc=Search::Xapian::Document->new();
|
|
|
|
my $title;
|
|
|
|
if (exists $pagestate{$params{page}}{meta} &&
|
|
|
|
exists $pagestate{$params{page}}{meta}{title}) {
|
|
|
|
$title=$pagestate{$params{page}}{meta}{title};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$title=IkiWiki::pagetitle($params{page});
|
|
|
|
}
|
2008-06-04 03:14:56 +02:00
|
|
|
|
2008-06-04 06:58:46 +02:00
|
|
|
# Remove any html from text to be indexed.
|
|
|
|
if (! defined $scrubber) {
|
|
|
|
eval q{use HTML::Scrubber};
|
|
|
|
if (! $@) {
|
|
|
|
$scrubber=HTML::Scrubber->new(allow => []);
|
2008-06-04 05:52:56 +02:00
|
|
|
}
|
2008-06-04 06:58:46 +02:00
|
|
|
}
|
|
|
|
my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
|
|
|
|
|
|
|
|
# Take 512 characters for a sample, then extend it out
|
|
|
|
# if it stopped in the middle of a word.
|
|
|
|
my $size=512;
|
|
|
|
my ($sample)=substr($toindex, 0, $size);
|
|
|
|
if (length($sample) == $size) {
|
|
|
|
my $max=length($toindex);
|
|
|
|
my $next;
|
|
|
|
while ($size < $max &&
|
|
|
|
($next=substr($toindex, $size++, 1)) !~ /\s/) {
|
|
|
|
$sample.=$next;
|
2008-06-04 05:52:56 +02:00
|
|
|
}
|
2008-06-04 03:14:56 +02:00
|
|
|
}
|
2008-06-04 06:58:46 +02:00
|
|
|
$sample=~s/\n/ /g;
|
|
|
|
|
|
|
|
# data used by omega
|
|
|
|
$doc->set_data(
|
|
|
|
"url=".urlto($params{page}, "")."\n".
|
|
|
|
"sample=$sample\n".
|
|
|
|
"caption=$title\n".
|
|
|
|
"modtime=$IkiWiki::pagemtime{$params{page}}\n".
|
|
|
|
"size=".length($params{content})."\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
my $tg = Search::Xapian::TermGenerator->new();
|
|
|
|
$tg->set_stemmer(new Search::Xapian::Stem("english"));
|
|
|
|
$tg->set_document($doc);
|
|
|
|
$tg->index_text($params{page}, 2);
|
|
|
|
$tg->index_text($title, 2);
|
|
|
|
$tg->index_text($toindex);
|
|
|
|
|
|
|
|
my $pageterm=pageterm($params{page});
|
|
|
|
$doc->add_term($pageterm);
|
|
|
|
$db->replace_document_by_term($pageterm, $doc);
|
2008-06-04 03:14:56 +02:00
|
|
|
|
|
|
|
return $params{content};
|
|
|
|
} #}}}
|
|
|
|
|
|
|
|
sub delete (@) { #{{{
|
|
|
|
my $db=xapiandb();
|
|
|
|
foreach my $page (@_) {
|
|
|
|
$db->delete_document_by_term(pageterm($page));
|
|
|
|
}
|
2006-05-03 23:50:39 +02:00
|
|
|
} #}}}
|
|
|
|
|
|
|
|
sub cgi ($) { #{{{
|
|
|
|
my $cgi=shift;
|
|
|
|
|
2008-06-03 21:29:54 +02:00
|
|
|
if (defined $cgi->param('P')) {
|
2006-05-03 23:50:39 +02:00
|
|
|
# only works for GET requests
|
2008-06-03 21:29:54 +02:00
|
|
|
chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
|
|
|
|
$ENV{OMEGA_CONFIG_FILE}="./omega.conf";
|
|
|
|
$ENV{CGIURL}=$config{cgiurl},
|
|
|
|
exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
|
2006-05-03 23:50:39 +02:00
|
|
|
}
|
|
|
|
} #}}}
|
|
|
|
|
2008-06-04 03:14:56 +02:00
|
|
|
sub pageterm ($) { #{{{
|
|
|
|
my $page=shift;
|
|
|
|
|
|
|
|
# TODO: check if > 255 char page names overflow term
|
|
|
|
# length; use sha1 if so?
|
2008-06-04 05:59:25 +02:00
|
|
|
return "U:".$page;
|
2008-06-04 03:14:56 +02:00
|
|
|
} #}}}
|
|
|
|
|
|
|
|
my $db;
|
|
|
|
sub xapiandb () { #{{{
|
|
|
|
if (! defined $db) {
|
|
|
|
eval q{
|
|
|
|
use Search::Xapian;
|
|
|
|
use Search::Xapian::WritableDatabase;
|
|
|
|
};
|
|
|
|
error($@) if $@;
|
|
|
|
$db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
|
|
|
|
Search::Xapian::DB_CREATE_OR_OPEN());
|
|
|
|
}
|
|
|
|
return $db;
|
|
|
|
} #}}}
|
|
|
|
|
2006-05-03 23:50:39 +02:00
|
|
|
1
|