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 => "postscan", 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);
54 # A unique pageterm is used to identify the document for a page.
55 my $pageterm=pageterm($params{page});
56 return $params{content} unless defined $pageterm;
59 my $doc=Search::Xapian::Document->new();
60 my $caption=IkiWiki::pagetitle($params{page});
62 if (exists $pagestate{$params{page}}{meta} &&
63 exists $pagestate{$params{page}}{meta}{title}) {
64 $title=$pagestate{$params{page}}{meta}{title};
70 # Remove html from text to be indexed.
71 if (! defined $scrubber) {
72 eval q{use HTML::Scrubber};
74 $scrubber=HTML::Scrubber->new(allow => []);
77 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
79 # Take 512 characters for a sample, then extend it out
80 # if it stopped in the middle of a word.
82 my ($sample)=substr($toindex, 0, $size);
83 if (length($sample) == $size) {
84 my $max=length($toindex);
86 while ($size < $max &&
87 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
94 # Decode html entities in it, since omega re-encodes them.
95 eval q{use HTML::Entities};
97 "url=".urlto($params{page}, "")."\n".
98 "sample=".decode_entities($sample)."\n".
99 "caption=".decode_entities($caption)."\n".
100 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
101 "size=".length($params{content})."\n"
104 # Index document and add terms for other metadata.
105 my $tg = Search::Xapian::TermGenerator->new();
107 my $langcode=$ENV{LANG} || "en";
110 # This whitelist is here to work around a xapian bug (#486138)
111 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
113 if (grep { $_ eq $langcode } @whitelist) {
114 $stemmer=Search::Xapian::Stem->new($langcode);
117 $stemmer=Search::Xapian::Stem->new("english");
120 $tg->set_stemmer($stemmer);
121 $tg->set_document($doc);
122 $tg->index_text($params{page}, 2);
123 $tg->index_text($caption, 2);
124 $tg->index_text($title, 2) if $title ne $caption;
125 $tg->index_text($toindex);
126 $tg->index_text(lc($title), 1, "S"); # for title:foo
127 foreach my $link (@{$links{$params{page}}}) {
128 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
131 $doc->add_term($pageterm);
132 $db->replace_document_by_term($pageterm, $doc);
135 sub delete (@) { #{{{
137 foreach my $page (@_) {
138 my $pageterm=pageterm(pagename($page));
139 $db->delete_document_by_term($pageterm) if defined $pageterm;
146 if (defined $cgi->param('P')) {
147 # only works for GET requests
148 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
149 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
150 $ENV{CGIURL}=$config{cgiurl},
151 IkiWiki::loadindex();
152 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
153 noimageinline => 1, linktext => "Help");
154 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
158 sub pageterm ($) { #{{{
161 # 240 is the number used by omindex to decide when to hash an
162 # overlong term. This does not use a compatible hash method though.
163 if (length $page > 240) {
164 eval q{use Digest::SHA1};
166 debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
170 # Note no colon, therefore it's guaranteed to not overlap
171 # with a page with the same name as the hash..
172 return "U".lc(Digest::SHA1::sha1_hex($page));
180 sub xapiandb () { #{{{
184 use Search::Xapian::WritableDatabase;
187 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
188 Search::Xapian::DB_CREATE_OR_OPEN());
195 sub setupfiles () { #{{{
196 if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
197 writefile("omega.conf", $config{wikistatedir}."/xapian",
199 "template_dir ./templates\n");
200 writefile("query", $config{wikistatedir}."/xapian/templates",
201 IkiWiki::misctemplate(gettext("search"),
202 readfile(IkiWiki::template_file("searchquery.tmpl"))));