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";
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 if (! defined $form) {
38 my $searchform = template("searchform.tmpl", blind_cache => 1);
39 $searchform->param(searchaction => $config{cgiurl});
40 $form=$searchform->output;
43 $template->param(searchform => $form);
52 return $params{content} if $IkiWiki::preprocessing{$params{destpage}};
56 # A unique pageterm is used to identify the document for a page.
57 my $pageterm=pageterm($params{page});
58 return $params{content} unless defined $pageterm;
61 my $doc=Search::Xapian::Document->new();
62 my $caption=IkiWiki::pagetitle($params{page});
64 if (exists $pagestate{$params{page}}{meta} &&
65 exists $pagestate{$params{page}}{meta}{title}) {
66 $title=$pagestate{$params{page}}{meta}{title};
72 # Remove html from text to be indexed.
73 if (! defined $scrubber) {
74 eval q{use HTML::Scrubber};
76 $scrubber=HTML::Scrubber->new(allow => []);
79 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
81 # Take 512 characters for a sample, then extend it out
82 # if it stopped in the middle of a word.
84 my ($sample)=substr($toindex, 0, $size);
85 if (length($sample) == $size) {
86 my $max=length($toindex);
88 while ($size < $max &&
89 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
96 # Decode html entities in it, since omega re-encodes them.
97 eval q{use HTML::Entities};
99 "url=".urlto($params{page}, "")."\n".
100 "sample=".decode_entities($sample)."\n".
101 "caption=".decode_entities($caption)."\n".
102 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
103 "size=".length($params{content})."\n"
106 # Index document and add terms for other metadata.
107 my $tg = Search::Xapian::TermGenerator->new();
109 my $langcode=$ENV{LANG} || "en";
112 # This whitelist is here to work around a xapian bug (#486138)
113 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
115 if (grep { $_ eq $langcode } @whitelist) {
116 $stemmer=Search::Xapian::Stem->new($langcode);
119 $stemmer=Search::Xapian::Stem->new("english");
122 $tg->set_stemmer($stemmer);
123 $tg->set_document($doc);
124 $tg->index_text($params{page}, 2);
125 $tg->index_text($caption, 2);
126 $tg->index_text($title, 2) if $title ne $caption;
127 $tg->index_text($toindex);
128 $tg->index_text(lc($title), 1, "ZS"); # for title:foo
129 foreach my $link (@{$links{$params{page}}}) {
130 $tg->index_text(lc($link), 1, "ZLINK"); # for link:bar
133 $doc->add_term($pageterm);
134 $db->replace_document_by_term($pageterm, $doc);
136 return $params{content};
139 sub delete (@) { #{{{
141 foreach my $page (@_) {
142 my $pageterm=pageterm(pagename($page));
143 $db->delete_document_by_term($pageterm) if defined $pageterm;
150 if (defined $cgi->param('P')) {
151 # only works for GET requests
152 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
153 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
154 $ENV{CGIURL}=$config{cgiurl},
155 IkiWiki::loadindex();
156 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
157 noimageinline => 1, linktext => "Help");
158 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
162 sub pageterm ($) { #{{{
165 # 240 is the number used by omindex to decide when to hash an
166 # overlong term. This does not use a compatible hash method though.
167 if (length $page > 240) {
168 eval q{use Digest::SHA1};
170 debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
174 # Note no colon, therefore it's guaranteed to not overlap
175 # with a page with the same name as the hash..
176 return "U".lc(Digest::SHA1::sha1_hex($page));
184 sub xapiandb () { #{{{
188 use Search::Xapian::WritableDatabase;
191 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
192 Search::Xapian::DB_CREATE_OR_OPEN());
199 sub setupfiles () { #{{{
200 if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
201 writefile("omega.conf", $config{wikistatedir}."/xapian",
203 "template_dir ./templates\n");
204 writefile("query", $config{wikistatedir}."/xapian/templates",
205 IkiWiki::misctemplate(gettext("search"),
206 readfile(IkiWiki::template_file("searchquery.tmpl"))));