2 # xapian-omega search engine plugin
3 package IkiWiki::Plugin::search;
10 hook(type => "checkconfig", id => "search", call => \&checkconfig);
11 hook(type => "pagetemplate", id => "search", call => \&pagetemplate);
12 hook(type => "sanitize", id => "search", call => \&index);
13 hook(type => "delete", id => "search", call => \&delete);
14 hook(type => "cgi", id => "search", call => \&cgi);
17 sub checkconfig () { #{{{
18 foreach my $required (qw(url cgiurl)) {
19 if (! length $config{$required}) {
20 error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
24 if (! exists $config{omega_cgi}) {
25 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
28 if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
29 writefile("omega.conf", $config{wikistatedir}."/xapian",
31 "template_dir ./templates\n");
32 writefile("query", $config{wikistatedir}."/xapian/templates",
33 IkiWiki::misctemplate(gettext("search"),
34 readfile(IkiWiki::template_file("searchquery.tmpl"))));
39 sub pagetemplate (@) { #{{{
41 my $page=$params{page};
42 my $template=$params{template};
44 # Add search box to page header.
45 if ($template->query(name => "searchform")) {
46 if (! defined $form) {
47 my $searchform = template("searchform.tmpl", blind_cache => 1);
48 $searchform->param(searchaction => $config{cgiurl});
49 $form=$searchform->output;
52 $template->param(searchform => $form);
61 return $params{content} if $IkiWiki::preprocessing{$params{destpage}};
64 my $doc=Search::Xapian::Document->new();
65 my $title=IkiWiki::pagetitle($params{page});
67 # Remove html from text to be indexed.
68 if (! defined $scrubber) {
69 eval q{use HTML::Scrubber};
71 $scrubber=HTML::Scrubber->new(allow => []);
74 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
76 # Take 512 characters for a sample, then extend it out
77 # if it stopped in the middle of a word.
79 my ($sample)=substr($toindex, 0, $size);
80 if (length($sample) == $size) {
81 my $max=length($toindex);
83 while ($size < $max &&
84 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
91 # Decode html entities in it, since omega re-encodes them.
92 eval q{use HTML::Entities};
94 "url=".urlto($params{page}, "")."\n".
95 "sample=".decode_entities($sample)."\n".
96 "caption=".decode_entities($title)."\n".
97 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
98 "size=".length($params{content})."\n"
101 my $tg = Search::Xapian::TermGenerator->new();
103 my $langcode=$ENV{LANG} || "en";
105 eval { $stemmer=Search::Xapian::Stem->new($langcode) };
107 $stemmer=Search::Xapian::Stem->new("english");
110 $tg->set_stemmer($stemmer);
111 $tg->set_document($doc);
112 $tg->index_text($params{page}, 2);
113 $tg->index_text($title, 2);
114 $tg->index_text($toindex);
116 my $pageterm=pageterm($params{page});
117 $doc->add_term($pageterm);
118 $db->replace_document_by_term($pageterm, $doc);
120 return $params{content};
123 sub delete (@) { #{{{
125 foreach my $page (@_) {
126 $db->delete_document_by_term(pageterm(pagename($page)));
133 if (defined $cgi->param('P')) {
134 # only works for GET requests
135 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
136 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
137 $ENV{CGIURL}=$config{cgiurl},
138 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
142 sub pageterm ($) { #{{{
145 # TODO: check if > 255 char page names overflow term
146 # length; use sha1 if so?
151 sub xapiandb () { #{{{
155 use Search::Xapian::WritableDatabase;
158 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
159 Search::Xapian::DB_CREATE_OR_OPEN());