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 () { #{{{
26 example => "/usr/lib/cgi-bin/omega/omega",
27 description => "path to the omega cgi program",
28 safe => 0, # external program
33 sub checkconfig () { #{{{
34 foreach my $required (qw(url cgiurl)) {
35 if (! length $config{$required}) {
36 error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
40 if (! defined $config{omega_cgi}) {
41 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
46 sub pagetemplate (@) { #{{{
48 my $page=$params{page};
49 my $template=$params{template};
51 # Add search box to page header.
52 if ($template->query(name => "searchform")) {
53 if (! defined $form) {
54 my $searchform = template("searchform.tmpl", blind_cache => 1);
55 $searchform->param(searchaction => $config{cgiurl});
56 $form=$searchform->output;
59 $template->param(searchform => $form);
70 # A unique pageterm is used to identify the document for a page.
71 my $pageterm=pageterm($params{page});
72 return $params{content} unless defined $pageterm;
75 my $doc=Search::Xapian::Document->new();
76 my $caption=IkiWiki::pagetitle($params{page});
78 if (exists $pagestate{$params{page}}{meta} &&
79 exists $pagestate{$params{page}}{meta}{title}) {
80 $title=$pagestate{$params{page}}{meta}{title};
86 # Remove html from text to be indexed.
87 if (! defined $scrubber) {
88 eval q{use HTML::Scrubber};
90 $scrubber=HTML::Scrubber->new(allow => []);
93 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
95 # Take 512 characters for a sample, then extend it out
96 # if it stopped in the middle of a word.
98 my ($sample)=substr($toindex, 0, $size);
99 if (length($sample) == $size) {
100 my $max=length($toindex);
102 while ($size < $max &&
103 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
110 # Decode html entities in it, since omega re-encodes them.
111 eval q{use HTML::Entities};
113 "url=".urlto($params{page}, "")."\n".
114 "sample=".decode_entities($sample)."\n".
115 "caption=".decode_entities($caption)."\n".
116 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
117 "size=".length($params{content})."\n"
120 # Index document and add terms for other metadata.
121 my $tg = Search::Xapian::TermGenerator->new();
123 my $langcode=$ENV{LANG} || "en";
126 # This whitelist is here to work around a xapian bug (#486138)
127 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
129 if (grep { $_ eq $langcode } @whitelist) {
130 $stemmer=Search::Xapian::Stem->new($langcode);
133 $stemmer=Search::Xapian::Stem->new("english");
136 $tg->set_stemmer($stemmer);
137 $tg->set_document($doc);
138 $tg->index_text($params{page}, 2);
139 $tg->index_text($caption, 2);
140 $tg->index_text($title, 2) if $title ne $caption;
141 $tg->index_text($toindex);
142 $tg->index_text(lc($title), 1, "S"); # for title:foo
143 foreach my $link (@{$links{$params{page}}}) {
144 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
147 $doc->add_term($pageterm);
148 $db->replace_document_by_term($pageterm, $doc);
151 sub delete (@) { #{{{
153 foreach my $page (@_) {
154 my $pageterm=pageterm(pagename($page));
155 $db->delete_document_by_term($pageterm) if defined $pageterm;
162 if (defined $cgi->param('P')) {
163 # only works for GET requests
164 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
165 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
166 $ENV{CGIURL}=$config{cgiurl},
167 IkiWiki::loadindex();
168 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
169 noimageinline => 1, linktext => "Help");
170 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
174 sub pageterm ($) { #{{{
177 # 240 is the number used by omindex to decide when to hash an
178 # overlong term. This does not use a compatible hash method though.
179 if (length $page > 240) {
180 eval q{use Digest::SHA1};
182 debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
186 # Note no colon, therefore it's guaranteed to not overlap
187 # with a page with the same name as the hash..
188 return "U".lc(Digest::SHA1::sha1_hex($page));
196 sub xapiandb () { #{{{
200 use Search::Xapian::WritableDatabase;
203 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
204 Search::Xapian::DB_CREATE_OR_OPEN());
211 sub setupfiles () { #{{{
212 if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
213 writefile("omega.conf", $config{wikistatedir}."/xapian",
215 "template_dir ./templates\n");
216 writefile("query", $config{wikistatedir}."/xapian/templates",
217 IkiWiki::misctemplate(gettext("search"),
218 readfile(IkiWiki::template_file("searchquery.tmpl"))));