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 description => "path to the omega cgi program",
23 safe => 0, # external program
28 sub checkconfig () { #{{{
29 foreach my $required (qw(url cgiurl)) {
30 if (! length $config{$required}) {
31 error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
35 if (! exists $config{omega_cgi}) {
36 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
41 sub pagetemplate (@) { #{{{
43 my $page=$params{page};
44 my $template=$params{template};
46 # Add search box to page header.
47 if ($template->query(name => "searchform")) {
48 if (! defined $form) {
49 my $searchform = template("searchform.tmpl", blind_cache => 1);
50 $searchform->param(searchaction => $config{cgiurl});
51 $form=$searchform->output;
54 $template->param(searchform => $form);
65 # A unique pageterm is used to identify the document for a page.
66 my $pageterm=pageterm($params{page});
67 return $params{content} unless defined $pageterm;
70 my $doc=Search::Xapian::Document->new();
71 my $caption=IkiWiki::pagetitle($params{page});
73 if (exists $pagestate{$params{page}}{meta} &&
74 exists $pagestate{$params{page}}{meta}{title}) {
75 $title=$pagestate{$params{page}}{meta}{title};
81 # Remove html from text to be indexed.
82 if (! defined $scrubber) {
83 eval q{use HTML::Scrubber};
85 $scrubber=HTML::Scrubber->new(allow => []);
88 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
90 # Take 512 characters for a sample, then extend it out
91 # if it stopped in the middle of a word.
93 my ($sample)=substr($toindex, 0, $size);
94 if (length($sample) == $size) {
95 my $max=length($toindex);
97 while ($size < $max &&
98 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
105 # Decode html entities in it, since omega re-encodes them.
106 eval q{use HTML::Entities};
108 "url=".urlto($params{page}, "")."\n".
109 "sample=".decode_entities($sample)."\n".
110 "caption=".decode_entities($caption)."\n".
111 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
112 "size=".length($params{content})."\n"
115 # Index document and add terms for other metadata.
116 my $tg = Search::Xapian::TermGenerator->new();
118 my $langcode=$ENV{LANG} || "en";
121 # This whitelist is here to work around a xapian bug (#486138)
122 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
124 if (grep { $_ eq $langcode } @whitelist) {
125 $stemmer=Search::Xapian::Stem->new($langcode);
128 $stemmer=Search::Xapian::Stem->new("english");
131 $tg->set_stemmer($stemmer);
132 $tg->set_document($doc);
133 $tg->index_text($params{page}, 2);
134 $tg->index_text($caption, 2);
135 $tg->index_text($title, 2) if $title ne $caption;
136 $tg->index_text($toindex);
137 $tg->index_text(lc($title), 1, "S"); # for title:foo
138 foreach my $link (@{$links{$params{page}}}) {
139 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
142 $doc->add_term($pageterm);
143 $db->replace_document_by_term($pageterm, $doc);
146 sub delete (@) { #{{{
148 foreach my $page (@_) {
149 my $pageterm=pageterm(pagename($page));
150 $db->delete_document_by_term($pageterm) if defined $pageterm;
157 if (defined $cgi->param('P')) {
158 # only works for GET requests
159 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
160 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
161 $ENV{CGIURL}=$config{cgiurl},
162 IkiWiki::loadindex();
163 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
164 noimageinline => 1, linktext => "Help");
165 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
169 sub pageterm ($) { #{{{
172 # 240 is the number used by omindex to decide when to hash an
173 # overlong term. This does not use a compatible hash method though.
174 if (length $page > 240) {
175 eval q{use Digest::SHA1};
177 debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
181 # Note no colon, therefore it's guaranteed to not overlap
182 # with a page with the same name as the hash..
183 return "U".lc(Digest::SHA1::sha1_hex($page));
191 sub xapiandb () { #{{{
195 use Search::Xapian::WritableDatabase;
198 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
199 Search::Xapian::DB_CREATE_OR_OPEN());
206 sub setupfiles () { #{{{
207 if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
208 writefile("omega.conf", $config{wikistatedir}."/xapian",
210 "template_dir ./templates\n");
211 writefile("query", $config{wikistatedir}."/xapian/templates",
212 IkiWiki::misctemplate(gettext("search"),
213 readfile(IkiWiki::template_file("searchquery.tmpl"))));