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);
17 hook(type => "needsbuild", id => "search", call => \&needsbuild);
29 example => "/usr/lib/cgi-bin/omega/omega",
30 description => "path to the omega cgi program",
31 safe => 0, # external program
37 foreach my $required (qw(url cgiurl)) {
38 if (! length $config{$required}) {
39 error(sprintf(gettext("Must specify %s when using the %s plugin"), $required, 'search'));
43 if (! defined $config{omega_cgi}) {
44 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
47 # This is a mass dependency, so if the search form template
48 # changes, every page is rebuilt.
49 add_depends("", "templates/searchform.tmpl");
53 sub pagetemplate (@) {
55 my $page=$params{page};
56 my $template=$params{template};
58 # Add search box to page header.
59 if ($template->query(name => "searchform")) {
60 if (! defined $form) {
61 my $searchform = template("searchform.tmpl", blind_cache => 1);
62 $searchform->param(searchaction => IkiWiki::cgiurl());
63 $searchform->param(html5 => $config{html5});
64 $form=$searchform->output;
67 $template->param(searchform => $form);
78 # A unique pageterm is used to identify the document for a page.
79 my $pageterm=pageterm($params{page});
80 return unless defined $pageterm;
83 my $doc=Search::Xapian::Document->new();
84 my $caption=pagetitle($params{page});
86 if (exists $pagestate{$params{page}}{meta} &&
87 exists $pagestate{$params{page}}{meta}{title}) {
88 $title=$pagestate{$params{page}}{meta}{title};
94 # Remove html from text to be indexed.
95 if (! defined $scrubber) {
96 eval q{use HTML::Scrubber};
98 $scrubber=HTML::Scrubber->new(allow => []);
101 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
103 # Take 512 characters for a sample, then extend it out
104 # if it stopped in the middle of a word.
106 my ($sample)=substr($toindex, 0, $size);
107 if (length($sample) == $size) {
108 my $max=length($toindex);
110 while ($size < $max &&
111 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
117 my $url=urlto($params{destpage}, "");
118 if (defined $pagestate{$params{page}}{meta}{permalink}) {
119 $url=$pagestate{$params{page}}{meta}{permalink}
123 # Decode html entities in it, since omega re-encodes them.
124 eval q{use HTML::Entities};
128 "sample=".decode_entities($sample)."\n".
129 "caption=".decode_entities($caption)."\n".
130 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
131 "size=".length($params{content})."\n"
134 # Index document and add terms for other metadata.
135 my $tg = Search::Xapian::TermGenerator->new();
137 my $langcode=$ENV{LANG} || "en";
140 # This whitelist is here to work around a xapian bug (#486138)
141 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
143 if (grep { $_ eq $langcode } @whitelist) {
144 $stemmer=Search::Xapian::Stem->new($langcode);
147 $stemmer=Search::Xapian::Stem->new("english");
150 $tg->set_stemmer($stemmer);
151 $tg->set_document($doc);
152 $tg->index_text($params{page}, 2);
153 $tg->index_text($caption, 2);
154 $tg->index_text($title, 2) if $title ne $caption;
155 $tg->index_text($toindex);
156 $tg->index_text(lc($title), 1, "S"); # for title:foo
157 foreach my $link (@{$links{$params{page}}}) {
158 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
161 $doc->add_term($pageterm);
162 $db->replace_document_by_term($pageterm, $doc);
167 foreach my $page (@_) {
168 my $pageterm=pageterm(pagename($page));
169 $db->delete_document_by_term($pageterm) if defined $pageterm;
176 if (defined $cgi->param('P')) {
177 # only works for GET requests
178 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
179 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
180 $ENV{CGIURL}=IkiWiki::cgiurl();
181 IkiWiki::loadindex();
182 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
183 noimageinline => 1, linktext => "Help");
184 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
191 # 240 is the number used by omindex to decide when to hash an
192 # overlong term. This does not use a compatible hash method though.
193 if (length $page > 240) {
194 eval q{use Digest::SHA};
196 debug("search: ".sprintf(gettext("need Digest::SHA to index %s"), $page)) if $@;
200 # Note no colon, therefore it's guaranteed to not overlap
201 # with a page with the same name as the hash..
202 return "U".lc(Digest::SHA::sha1_hex($page));
214 use Search::Xapian::WritableDatabase;
217 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
218 Search::Xapian::DB_CREATE_OR_OPEN());
226 if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
227 writefile("omega.conf", $config{wikistatedir}."/xapian",
229 "template_dir ./templates\n");
239 $_ eq "templates/page.tmpl" ||
240 $_ eq "templates/searchquery.tmpl"
247 # Avoid omega interpreting anything in the cgitemplate
248 # as an omegascript command.
249 eval q{use IkiWiki::CGI};
250 my $template=IkiWiki::cgitemplate(undef, gettext("search"), "\0",
251 searchform => "", # avoid showing the small search form
253 eval q{use HTML::Entities};
255 $template=encode_entities($template, '\$');
257 my $querytemplate=readfile(IkiWiki::template_file("searchquery.tmpl"));
258 $template=~s/\0/$querytemplate/;
259 writefile("query", $config{wikistatedir}."/xapian/templates",
264 if (-d $config{wikistatedir}."/xapian") {
265 system("rm", "-rf", $config{wikistatedir}."/xapian");