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);
27 example => "/usr/lib/cgi-bin/omega/omega",
28 description => "path to the omega cgi program",
29 safe => 0, # external program
35 foreach my $required (qw(url cgiurl)) {
36 if (! length $config{$required}) {
37 error(sprintf(gettext("Must specify %s when using the %s plugin"), $required, 'search'));
41 if (! defined $config{omega_cgi}) {
42 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
45 # This is a mass dependency, so if the search form template
46 # changes, every page is rebuilt.
47 add_depends("", "templates/searchform.tmpl");
51 sub pagetemplate (@) {
53 my $page=$params{page};
54 my $template=$params{template};
56 # Add search box to page header.
57 if ($template->query(name => "searchform")) {
58 if (! defined $form) {
59 my $searchform = template("searchform.tmpl", blind_cache => 1);
60 $searchform->param(searchaction => $config{cgiurl});
61 $searchform->param(html5 => $config{html5});
62 $form=$searchform->output;
65 $template->param(searchform => $form);
76 # A unique pageterm is used to identify the document for a page.
77 my $pageterm=pageterm($params{page});
78 return unless defined $pageterm;
81 my $doc=Search::Xapian::Document->new();
82 my $caption=pagetitle($params{page});
84 if (exists $pagestate{$params{page}}{meta} &&
85 exists $pagestate{$params{page}}{meta}{title}) {
86 $title=$pagestate{$params{page}}{meta}{title};
92 # Remove html from text to be indexed.
93 if (! defined $scrubber) {
94 eval q{use HTML::Scrubber};
96 $scrubber=HTML::Scrubber->new(allow => []);
99 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
101 # Take 512 characters for a sample, then extend it out
102 # if it stopped in the middle of a word.
104 my ($sample)=substr($toindex, 0, $size);
105 if (length($sample) == $size) {
106 my $max=length($toindex);
108 while ($size < $max &&
109 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
116 # Decode html entities in it, since omega re-encodes them.
117 eval q{use HTML::Entities};
120 "url=".urlto($params{page}, "")."\n".
121 "sample=".decode_entities($sample)."\n".
122 "caption=".decode_entities($caption)."\n".
123 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
124 "size=".length($params{content})."\n"
127 # Index document and add terms for other metadata.
128 my $tg = Search::Xapian::TermGenerator->new();
130 my $langcode=$ENV{LANG} || "en";
133 # This whitelist is here to work around a xapian bug (#486138)
134 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
136 if (grep { $_ eq $langcode } @whitelist) {
137 $stemmer=Search::Xapian::Stem->new($langcode);
140 $stemmer=Search::Xapian::Stem->new("english");
143 $tg->set_stemmer($stemmer);
144 $tg->set_document($doc);
145 $tg->index_text($params{page}, 2);
146 $tg->index_text($caption, 2);
147 $tg->index_text($title, 2) if $title ne $caption;
148 $tg->index_text($toindex);
149 $tg->index_text(lc($title), 1, "S"); # for title:foo
150 foreach my $link (@{$links{$params{page}}}) {
151 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
154 $doc->add_term($pageterm);
155 $db->replace_document_by_term($pageterm, $doc);
160 foreach my $page (@_) {
161 my $pageterm=pageterm(pagename($page));
162 $db->delete_document_by_term($pageterm) if defined $pageterm;
169 if (defined $cgi->param('P')) {
170 # only works for GET requests
171 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
172 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
173 $ENV{CGIURL}=$config{cgiurl},
174 IkiWiki::loadindex();
175 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
176 noimageinline => 1, linktext => "Help");
177 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
184 # 240 is the number used by omindex to decide when to hash an
185 # overlong term. This does not use a compatible hash method though.
186 if (length $page > 240) {
187 eval q{use Digest::SHA1};
189 debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
193 # Note no colon, therefore it's guaranteed to not overlap
194 # with a page with the same name as the hash..
195 return "U".lc(Digest::SHA1::sha1_hex($page));
207 use Search::Xapian::WritableDatabase;
210 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
211 Search::Xapian::DB_CREATE_OR_OPEN());
219 if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
220 writefile("omega.conf", $config{wikistatedir}."/xapian",
222 "template_dir ./templates\n");
224 # Avoid omega interpreting anything in the misctemplate
225 # as an omegascript command.
226 my $misctemplate=IkiWiki::misctemplate(gettext("search"), "\0");
227 eval q{use HTML::Entities};
229 $misctemplate=encode_entities($misctemplate, '\$');
231 my $querytemplate=readfile(IkiWiki::template_file("searchquery.tmpl"));
232 $misctemplate=~s/\0/$querytemplate/;
234 writefile("query", $config{wikistatedir}."/xapian/templates",