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;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-05-03 23:50:39 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-07-26 06:38:13 +02:00
|
|
|
hook(type => "getsetup", id => "search", call => \&getsetup);
|
2008-06-03 21:29:54 +02:00
|
|
|
hook(type => "checkconfig", id => "search", call => \&checkconfig);
|
|
|
|
hook(type => "pagetemplate", id => "search", call => \&pagetemplate);
|
2010-05-07 05:20:48 +02:00
|
|
|
hook(type => "indexhtml", id => "search", call => \&indexhtml);
|
2008-06-03 21:29:54 +02:00
|
|
|
hook(type => "delete", id => "search", call => \&delete);
|
|
|
|
hook(type => "cgi", id => "search", call => \&cgi);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-05-03 23:50:39 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-07-26 06:38:13 +02:00
|
|
|
return
|
2008-08-03 23:20:21 +02:00
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1,
|
2010-02-12 10:22:15 +01:00
|
|
|
section => "web",
|
2008-08-03 23:20:21 +02:00
|
|
|
},
|
2008-07-26 06:38:13 +02:00
|
|
|
omega_cgi => {
|
|
|
|
type => "string",
|
2008-07-27 03:14:07 +02:00
|
|
|
example => "/usr/lib/cgi-bin/omega/omega",
|
2008-07-26 06:38:13 +02:00
|
|
|
description => "path to the omega cgi program",
|
|
|
|
safe => 0, # external program
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-26 06:38:13 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub checkconfig () {
|
2006-05-03 23:50:39 +02:00
|
|
|
foreach my $required (qw(url cgiurl)) {
|
2006-09-10 00:50:27 +02:00
|
|
|
if (! length $config{$required}) {
|
2009-01-02 15:00:34 +01:00
|
|
|
error(sprintf(gettext("Must specify %s when using the %s plugin"), $required, 'search'));
|
2006-05-03 23:50:39 +02:00
|
|
|
}
|
|
|
|
}
|
2008-06-04 21:24:05 +02:00
|
|
|
|
2008-07-27 03:14:07 +02:00
|
|
|
if (! defined $config{omega_cgi}) {
|
2008-06-03 21:29:54 +02:00
|
|
|
$config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
|
|
|
|
}
|
2010-04-25 02:22:20 +02:00
|
|
|
|
|
|
|
# This is a mass dependency, so if the search form template
|
|
|
|
# changes, every page is rebuilt.
|
2010-04-25 02:41:35 +02:00
|
|
|
add_depends("", "templates/searchform.tmpl");
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-05-26 03:10:58 +02:00
|
|
|
|
2006-08-26 23:57:59 +02:00
|
|
|
my $form;
|
2008-12-17 21:22:16 +01:00
|
|
|
sub pagetemplate (@) {
|
2006-07-28 01:41:58 +02:00
|
|
|
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) {
|
2010-04-25 02:22:20 +02:00
|
|
|
my $searchform = template("searchform.tmpl", blind_cache => 1);
|
2006-09-10 00:50:27 +02:00
|
|
|
$searchform->param(searchaction => $config{cgiurl});
|
2010-05-02 19:49:56 +02:00
|
|
|
$searchform->param(html5 => $config{html5});
|
2006-08-26 23:57:59 +02:00
|
|
|
$form=$searchform->output;
|
|
|
|
}
|
|
|
|
|
|
|
|
$template->param(searchform => $form);
|
2006-07-28 01:08:03 +02:00
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-05-03 23:50:39 +02:00
|
|
|
|
2008-06-04 05:52:56 +02:00
|
|
|
my $scrubber;
|
2008-06-04 08:41:02 +02:00
|
|
|
my $stemmer;
|
2010-05-07 05:20:48 +02:00
|
|
|
sub indexhtml (@) {
|
2008-06-04 03:14:56 +02:00
|
|
|
my %params=@_;
|
2008-06-04 21:24:05 +02:00
|
|
|
|
|
|
|
setupfiles();
|
|
|
|
|
|
|
|
# A unique pageterm is used to identify the document for a page.
|
|
|
|
my $pageterm=pageterm($params{page});
|
2010-04-20 06:07:41 +02:00
|
|
|
return unless defined $pageterm;
|
2008-06-04 06:58:46 +02:00
|
|
|
|
|
|
|
my $db=xapiandb();
|
|
|
|
my $doc=Search::Xapian::Document->new();
|
2008-09-27 20:14:36 +02:00
|
|
|
my $caption=pagetitle($params{page});
|
2008-06-04 20:13:21 +02:00
|
|
|
my $title;
|
|
|
|
if (exists $pagestate{$params{page}}{meta} &&
|
|
|
|
exists $pagestate{$params{page}}{meta}{title}) {
|
|
|
|
$title=$pagestate{$params{page}}{meta}{title};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$title=$caption;
|
|
|
|
}
|
2008-06-04 03:14:56 +02:00
|
|
|
|
2008-06-04 07:24:23 +02:00
|
|
|
# Remove html from text to be indexed.
|
2008-06-04 06:58:46 +02:00
|
|
|
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;
|
|
|
|
|
2010-05-07 05:28:40 +02:00
|
|
|
my $url=urlto($params{destpage}, "");
|
|
|
|
if (defined $pagestate{$params{page}}{meta}{permalink}) {
|
|
|
|
$url=$pagestate{$params{page}}{meta}{permalink}
|
|
|
|
}
|
|
|
|
|
2008-06-04 06:58:46 +02:00
|
|
|
# data used by omega
|
2008-06-04 07:50:51 +02:00
|
|
|
# Decode html entities in it, since omega re-encodes them.
|
|
|
|
eval q{use HTML::Entities};
|
2010-03-11 22:10:04 +01:00
|
|
|
error $@ if $@;
|
2008-06-04 06:58:46 +02:00
|
|
|
$doc->set_data(
|
2010-05-07 05:28:40 +02:00
|
|
|
"url=".$url."\n".
|
2008-06-04 07:50:51 +02:00
|
|
|
"sample=".decode_entities($sample)."\n".
|
2008-06-04 20:13:21 +02:00
|
|
|
"caption=".decode_entities($caption)."\n".
|
2008-06-04 06:58:46 +02:00
|
|
|
"modtime=$IkiWiki::pagemtime{$params{page}}\n".
|
|
|
|
"size=".length($params{content})."\n"
|
|
|
|
);
|
|
|
|
|
2008-06-04 20:13:21 +02:00
|
|
|
# Index document and add terms for other metadata.
|
2008-06-04 06:58:46 +02:00
|
|
|
my $tg = Search::Xapian::TermGenerator->new();
|
2008-06-04 08:41:02 +02:00
|
|
|
if (! $stemmer) {
|
|
|
|
my $langcode=$ENV{LANG} || "en";
|
|
|
|
$langcode=~s/_.*//;
|
2008-06-13 19:05:44 +02:00
|
|
|
|
|
|
|
# This whitelist is here to work around a xapian bug (#486138)
|
|
|
|
my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
|
|
|
|
|
|
|
|
if (grep { $_ eq $langcode } @whitelist) {
|
|
|
|
$stemmer=Search::Xapian::Stem->new($langcode);
|
|
|
|
}
|
|
|
|
else {
|
2008-06-04 08:41:02 +02:00
|
|
|
$stemmer=Search::Xapian::Stem->new("english");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$tg->set_stemmer($stemmer);
|
2008-06-04 06:58:46 +02:00
|
|
|
$tg->set_document($doc);
|
|
|
|
$tg->index_text($params{page}, 2);
|
2008-06-04 20:13:21 +02:00
|
|
|
$tg->index_text($caption, 2);
|
|
|
|
$tg->index_text($title, 2) if $title ne $caption;
|
2008-06-04 06:58:46 +02:00
|
|
|
$tg->index_text($toindex);
|
2008-07-17 12:37:08 +02:00
|
|
|
$tg->index_text(lc($title), 1, "S"); # for title:foo
|
2008-06-04 20:13:21 +02:00
|
|
|
foreach my $link (@{$links{$params{page}}}) {
|
2008-07-17 12:37:08 +02:00
|
|
|
$tg->index_text(lc($link), 1, "XLINK"); # for link:bar
|
2008-06-04 20:13:21 +02:00
|
|
|
}
|
2008-06-04 06:58:46 +02:00
|
|
|
|
|
|
|
$doc->add_term($pageterm);
|
|
|
|
$db->replace_document_by_term($pageterm, $doc);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-06-04 03:14:56 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub delete (@) {
|
2008-06-04 03:14:56 +02:00
|
|
|
my $db=xapiandb();
|
|
|
|
foreach my $page (@_) {
|
2008-06-04 21:24:05 +02:00
|
|
|
my $pageterm=pageterm(pagename($page));
|
|
|
|
$db->delete_document_by_term($pageterm) if defined $pageterm;
|
2008-06-04 03:14:56 +02:00
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-05-03 23:50:39 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub cgi ($) {
|
2006-05-03 23:50:39 +02:00
|
|
|
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},
|
2008-06-04 21:24:05 +02:00
|
|
|
IkiWiki::loadindex();
|
|
|
|
$ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
|
|
|
|
noimageinline => 1, linktext => "Help");
|
2008-06-03 21:29:54 +02:00
|
|
|
exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
|
2006-05-03 23:50:39 +02:00
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-05-03 23:50:39 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub pageterm ($) {
|
2008-06-04 03:14:56 +02:00
|
|
|
my $page=shift;
|
|
|
|
|
2008-06-04 21:24:05 +02:00
|
|
|
# 240 is the number used by omindex to decide when to hash an
|
|
|
|
# overlong term. This does not use a compatible hash method though.
|
|
|
|
if (length $page > 240) {
|
|
|
|
eval q{use Digest::SHA1};
|
|
|
|
if ($@) {
|
|
|
|
debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Note no colon, therefore it's guaranteed to not overlap
|
|
|
|
# with a page with the same name as the hash..
|
|
|
|
return "U".lc(Digest::SHA1::sha1_hex($page));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "U:".$page;
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-06-04 03:14:56 +02:00
|
|
|
|
|
|
|
my $db;
|
2008-12-17 21:22:16 +01:00
|
|
|
sub xapiandb () {
|
2008-06-04 03:14:56 +02:00
|
|
|
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;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-06-04 03:14:56 +02:00
|
|
|
|
2008-07-07 07:54:28 +02:00
|
|
|
{
|
|
|
|
my $setup=0;
|
2008-12-17 21:22:16 +01:00
|
|
|
sub setupfiles () {
|
2008-07-07 07:54:28 +02:00
|
|
|
if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
|
2008-06-04 21:24:05 +02:00
|
|
|
writefile("omega.conf", $config{wikistatedir}."/xapian",
|
|
|
|
"database_dir .\n".
|
|
|
|
"template_dir ./templates\n");
|
2010-03-11 22:10:04 +01:00
|
|
|
|
|
|
|
# Avoid omega interpreting anything in the misctemplate
|
|
|
|
# as an omegascript command.
|
2010-05-15 03:42:48 +02:00
|
|
|
my $misctemplate=IkiWiki::misctemplate(gettext("search"), "\0",
|
2010-05-15 03:45:22 +02:00
|
|
|
searchform => "", # avoid showing the small search form
|
|
|
|
);
|
2010-03-11 22:10:04 +01:00
|
|
|
eval q{use HTML::Entities};
|
|
|
|
error $@ if $@;
|
|
|
|
$misctemplate=encode_entities($misctemplate, '\$');
|
|
|
|
|
|
|
|
my $querytemplate=readfile(IkiWiki::template_file("searchquery.tmpl"));
|
|
|
|
$misctemplate=~s/\0/$querytemplate/;
|
|
|
|
|
2008-06-04 21:24:05 +02:00
|
|
|
writefile("query", $config{wikistatedir}."/xapian/templates",
|
2010-03-11 22:10:04 +01:00
|
|
|
$misctemplate);
|
2008-07-06 18:06:10 +02:00
|
|
|
$setup=1;
|
2008-06-04 21:24:05 +02:00
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-07 07:54:28 +02:00
|
|
|
}
|
2008-06-04 21:24:05 +02:00
|
|
|
|
2006-05-03 23:50:39 +02:00
|
|
|
1
|