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 => "indexhtml", id => "search", call => \&indexhtml);
14 hook(type => "delete", id => "search", call => \&delete);
15 hook(type => "cgi", id => "search", call => \&cgi);
16 hook(type => "disable", id => "search", call => \&disable);
28 example => "/usr/lib/cgi-bin/omega/omega",
29 description => "path to the omega cgi program",
30 safe => 0, # external program
36 foreach my $required (qw(url cgiurl)) {
37 if (! length $config{$required}) {
38 error(sprintf(gettext("Must specify %s when using the %s plugin"), $required, 'search'));
42 if (! defined $config{omega_cgi}) {
43 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
46 # This is a mass dependency, so if the search form template
47 # changes, every page is rebuilt.
48 add_depends("", "templates/searchform.tmpl");
52 sub pagetemplate (@) {
54 my $page=$params{page};
55 my $template=$params{template};
57 # Add search box to page header.
58 if ($template->query(name => "searchform")) {
59 if (! defined $form) {
60 my $searchform = template("searchform.tmpl", blind_cache => 1);
61 $searchform->param(searchaction => IkiWiki::cgiurl());
62 $searchform->param(html5 => $config{html5});
63 $form=$searchform->output;
66 $template->param(searchform => $form);
77 # A unique pageterm is used to identify the document for a page.
78 my $pageterm=pageterm($params{page});
79 return unless defined $pageterm;
82 my $doc=Search::Xapian::Document->new();
83 my $caption=pagetitle($params{page});
85 if (exists $pagestate{$params{page}}{meta} &&
86 exists $pagestate{$params{page}}{meta}{title}) {
87 $title=$pagestate{$params{page}}{meta}{title};
93 # Remove html from text to be indexed.
94 if (! defined $scrubber) {
95 eval q{use HTML::Scrubber};
97 $scrubber=HTML::Scrubber->new(allow => []);
100 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
102 # Take 512 characters for a sample, then extend it out
103 # if it stopped in the middle of a word.
105 my ($sample)=substr($toindex, 0, $size);
106 if (length($sample) == $size) {
107 my $max=length($toindex);
109 while ($size < $max &&
110 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
116 my $url=urlto($params{destpage}, "");
117 if (defined $pagestate{$params{page}}{meta}{permalink}) {
118 $url=$pagestate{$params{page}}{meta}{permalink}
122 # Decode html entities in it, since omega re-encodes them.
123 eval q{use HTML::Entities};
127 "sample=".decode_entities($sample)."\n".
128 "caption=".decode_entities($caption)."\n".
129 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
130 "size=".length($params{content})."\n"
133 # Index document and add terms for other metadata.
134 my $tg = Search::Xapian::TermGenerator->new();
136 my $langcode=$ENV{LANG} || "en";
139 # This whitelist is here to work around a xapian bug (#486138)
140 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
142 if (grep { $_ eq $langcode } @whitelist) {
143 $stemmer=Search::Xapian::Stem->new($langcode);
146 $stemmer=Search::Xapian::Stem->new("english");
149 $tg->set_stemmer($stemmer);
150 $tg->set_document($doc);
151 $tg->index_text($params{page}, 2);
152 $tg->index_text($caption, 2);
153 $tg->index_text($title, 2) if $title ne $caption;
154 $tg->index_text($toindex);
155 $tg->index_text(lc($title), 1, "S"); # for title:foo
156 foreach my $link (@{$links{$params{page}}}) {
157 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
160 $doc->add_term($pageterm);
161 $db->replace_document_by_term($pageterm, $doc);
166 foreach my $page (@_) {
167 my $pageterm=pageterm(pagename($page));
168 $db->delete_document_by_term($pageterm) if defined $pageterm;
175 if (defined $cgi->param('P')) {
176 # only works for GET requests
177 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
178 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
179 $ENV{CGIURL}=IkiWiki::cgiurl();
180 IkiWiki::loadindex();
181 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
182 noimageinline => 1, linktext => "Help");
183 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
190 # 240 is the number used by omindex to decide when to hash an
191 # overlong term. This does not use a compatible hash method though.
192 if (length $page > 240) {
193 eval q{use Digest::SHA};
195 debug("search: ".sprintf(gettext("need Digest::SHA to index %s"), $page)) if $@;
199 # Note no colon, therefore it's guaranteed to not overlap
200 # with a page with the same name as the hash..
201 return "U".lc(Digest::SHA::sha1_hex($page));
213 use Search::Xapian::WritableDatabase;
216 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
217 Search::Xapian::DB_CREATE_OR_OPEN());
225 if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
226 writefile("omega.conf", $config{wikistatedir}."/xapian",
228 "template_dir ./templates\n");
230 # Avoid omega interpreting anything in the cgitemplate
231 # as an omegascript command.
232 eval q{use IkiWiki::CGI};
233 my $template=IkiWiki::cgitemplate(undef, gettext("search"), "\0",
234 searchform => "", # avoid showing the small search form
236 eval q{use HTML::Entities};
238 $template=encode_entities($template, '\$');
240 my $querytemplate=readfile(IkiWiki::template_file("searchquery.tmpl"));
241 $template=~s/\0/$querytemplate/;
243 writefile("query", $config{wikistatedir}."/xapian/templates",
251 if (-d $config{wikistatedir}."/xapian") {
252 system("rm", "-rf", $config{wikistatedir}."/xapian");