remove default values in getsetup
[ikiwiki] / IkiWiki / Plugin / search.pm
1 #!/usr/bin/perl
2 # xapian-omega search engine plugin
3 package IkiWiki::Plugin::search;
4
5 use warnings;
6 use strict;
7 use IkiWiki 2.00;
8
9 sub import { #{{{
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);
16 } # }}}
17
18 sub getsetup () { #{{{
19         return
20                 omega_cgi => {
21                         type => "string",
22                         description => "path to the omega cgi program",
23                         safe => 0, # external program
24                         rebuild => 0,
25                 },
26 } #}}}
27
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));
32                 }
33         }
34         
35         if (! exists $config{omega_cgi}) {
36                 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
37         }
38 } #}}}
39
40 my $form;
41 sub pagetemplate (@) { #{{{
42         my %params=@_;
43         my $page=$params{page};
44         my $template=$params{template};
45
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;
52                 }
53
54                 $template->param(searchform => $form);
55         }
56 } #}}}
57
58 my $scrubber;
59 my $stemmer;
60 sub index (@) { #{{{
61         my %params=@_;
62
63         setupfiles();
64
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;
68         
69         my $db=xapiandb();
70         my $doc=Search::Xapian::Document->new();
71         my $caption=IkiWiki::pagetitle($params{page});
72         my $title;
73         if (exists $pagestate{$params{page}}{meta} &&
74                 exists $pagestate{$params{page}}{meta}{title}) {
75                 $title=$pagestate{$params{page}}{meta}{title};
76         }
77         else {
78                 $title=$caption;
79         }
80
81         # Remove html from text to be indexed.
82         if (! defined $scrubber) {
83                 eval q{use HTML::Scrubber};
84                 if (! $@) {
85                         $scrubber=HTML::Scrubber->new(allow => []);
86                 }
87         }
88         my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
89         
90         # Take 512 characters for a sample, then extend it out
91         # if it stopped in the middle of a word.
92         my $size=512;
93         my ($sample)=substr($toindex, 0, $size);
94         if (length($sample) == $size) {
95                 my $max=length($toindex);
96                 my $next;
97                 while ($size < $max &&
98                        ($next=substr($toindex, $size++, 1)) !~ /\s/) {
99                         $sample.=$next;
100                 }
101         }
102         $sample=~s/\n/ /g;
103         
104         # data used by omega
105         # Decode html entities in it, since omega re-encodes them.
106         eval q{use HTML::Entities};
107         $doc->set_data(
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"
113         );
114
115         # Index document and add terms for other metadata.
116         my $tg = Search::Xapian::TermGenerator->new();
117         if (! $stemmer) {
118                 my $langcode=$ENV{LANG} || "en";
119                 $langcode=~s/_.*//;
120
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};
123
124                 if (grep { $_ eq $langcode } @whitelist) {
125                         $stemmer=Search::Xapian::Stem->new($langcode);
126                 }
127                 else {
128                         $stemmer=Search::Xapian::Stem->new("english");
129                 }
130         }
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
140         }
141
142         $doc->add_term($pageterm);
143         $db->replace_document_by_term($pageterm, $doc);
144 } #}}}
145
146 sub delete (@) { #{{{
147         my $db=xapiandb();
148         foreach my $page (@_) {
149                 my $pageterm=pageterm(pagename($page));
150                 $db->delete_document_by_term($pageterm) if defined $pageterm;
151         }
152 } #}}}
153
154 sub cgi ($) { #{{{
155         my $cgi=shift;
156
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: $!");
166         }
167 } #}}}
168
169 sub pageterm ($) { #{{{
170         my $page=shift;
171
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};
176                 if ($@) {
177                         debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
178                         return undef;
179                 }
180
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));
184         }
185         else {
186                 return "U:".$page;
187         }
188 } #}}}
189
190 my $db;
191 sub xapiandb () { #{{{
192         if (! defined $db) {
193                 eval q{
194                         use Search::Xapian;
195                         use Search::Xapian::WritableDatabase;
196                 };
197                 error($@) if $@;
198                 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
199                         Search::Xapian::DB_CREATE_OR_OPEN());
200         }
201         return $db;
202 } #}}}
203
204 {
205 my $setup=0;
206 sub setupfiles () { #{{{
207         if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
208                 writefile("omega.conf", $config{wikistatedir}."/xapian",
209                         "database_dir .\n".
210                         "template_dir ./templates\n");
211                 writefile("query", $config{wikistatedir}."/xapian/templates",
212                         IkiWiki::misctemplate(gettext("search"),
213                                 readfile(IkiWiki::template_file("searchquery.tmpl"))));
214                 $setup=1;
215         }
216 } #}}}
217 }
218
219 1