2 # xapian-omega search engine plugin
3 package IkiWiki::Plugin::search;
10 hook(type => "getsetup", id => "search", call => \&getsetup);
11 hook(type => "checkconfig", id => "search", call => \&checkconfig);
12 hook(type => "pagetemplate", id => "search", call => \&pagetemplate);
13 hook(type => "postscan", id => "search", call => \&index);
14 hook(type => "delete", id => "search", call => \&delete);
15 hook(type => "cgi", id => "search", call => \&cgi);
18 sub getsetup () { #{{{
22 default => "/usr/lib/cgi-bin/omega/omega",
23 description => "path to the omega cgi program",
24 safe => 0, # external program
29 sub checkconfig () { #{{{
30 foreach my $required (qw(url cgiurl)) {
31 if (! length $config{$required}) {
32 error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
36 if (! exists $config{omega_cgi}) {
37 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
42 sub pagetemplate (@) { #{{{
44 my $page=$params{page};
45 my $template=$params{template};
47 # Add search box to page header.
48 if ($template->query(name => "searchform")) {
49 if (! defined $form) {
50 my $searchform = template("searchform.tmpl", blind_cache => 1);
51 $searchform->param(searchaction => $config{cgiurl});
52 $form=$searchform->output;
55 $template->param(searchform => $form);
66 # A unique pageterm is used to identify the document for a page.
67 my $pageterm=pageterm($params{page});
68 return $params{content} unless defined $pageterm;
71 my $doc=Search::Xapian::Document->new();
72 my $caption=IkiWiki::pagetitle($params{page});
74 if (exists $pagestate{$params{page}}{meta} &&
75 exists $pagestate{$params{page}}{meta}{title}) {
76 $title=$pagestate{$params{page}}{meta}{title};
82 # Remove html from text to be indexed.
83 if (! defined $scrubber) {
84 eval q{use HTML::Scrubber};
86 $scrubber=HTML::Scrubber->new(allow => []);
89 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
91 # Take 512 characters for a sample, then extend it out
92 # if it stopped in the middle of a word.
94 my ($sample)=substr($toindex, 0, $size);
95 if (length($sample) == $size) {
96 my $max=length($toindex);
98 while ($size < $max &&
99 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
106 # Decode html entities in it, since omega re-encodes them.
107 eval q{use HTML::Entities};
109 "url=".urlto($params{page}, "")."\n".
110 "sample=".decode_entities($sample)."\n".
111 "caption=".decode_entities($caption)."\n".
112 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
113 "size=".length($params{content})."\n"
116 # Index document and add terms for other metadata.
117 my $tg = Search::Xapian::TermGenerator->new();
119 my $langcode=$ENV{LANG} || "en";
122 # This whitelist is here to work around a xapian bug (#486138)
123 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
125 if (grep { $_ eq $langcode } @whitelist) {
126 $stemmer=Search::Xapian::Stem->new($langcode);
129 $stemmer=Search::Xapian::Stem->new("english");
132 $tg->set_stemmer($stemmer);
133 $tg->set_document($doc);
134 $tg->index_text($params{page}, 2);
135 $tg->index_text($caption, 2);
136 $tg->index_text($title, 2) if $title ne $caption;
137 $tg->index_text($toindex);
138 $tg->index_text(lc($title), 1, "S"); # for title:foo
139 foreach my $link (@{$links{$params{page}}}) {
140 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
143 $doc->add_term($pageterm);
144 $db->replace_document_by_term($pageterm, $doc);
147 sub delete (@) { #{{{
149 foreach my $page (@_) {
150 my $pageterm=pageterm(pagename($page));
151 $db->delete_document_by_term($pageterm) if defined $pageterm;
158 if (defined $cgi->param('P')) {
159 # only works for GET requests
160 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
161 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
162 $ENV{CGIURL}=$config{cgiurl},
163 IkiWiki::loadindex();
164 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
165 noimageinline => 1, linktext => "Help");
166 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
170 sub pageterm ($) { #{{{
173 # 240 is the number used by omindex to decide when to hash an
174 # overlong term. This does not use a compatible hash method though.
175 if (length $page > 240) {
176 eval q{use Digest::SHA1};
178 debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
182 # Note no colon, therefore it's guaranteed to not overlap
183 # with a page with the same name as the hash..
184 return "U".lc(Digest::SHA1::sha1_hex($page));
192 sub xapiandb () { #{{{
196 use Search::Xapian::WritableDatabase;
199 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
200 Search::Xapian::DB_CREATE_OR_OPEN());
207 sub setupfiles () { #{{{
208 if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
209 writefile("omega.conf", $config{wikistatedir}."/xapian",
211 "template_dir ./templates\n");
212 writefile("query", $config{wikistatedir}."/xapian/templates",
213 IkiWiki::misctemplate(gettext("search"),
214 readfile(IkiWiki::template_file("searchquery.tmpl"))));