]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/search.pm
needsbuild hook is passed an array ref
[ikiwiki.git] / 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 3.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 => "indexhtml", id => "search", call => \&indexhtml);
14         hook(type => "delete", id => "search", call => \&delete);
15         hook(type => "cgi", id => "search", call => \&cgi);
16 }
17
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => 1,
23                         section => "web",
24                 },
25                 omega_cgi => {
26                         type => "string",
27                         example => "/usr/lib/cgi-bin/omega/omega",
28                         description => "path to the omega cgi program",
29                         safe => 0, # external program
30                         rebuild => 0,
31                 },
32 }
33
34 sub checkconfig () {
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'));
38                 }
39         }
40         
41         if (! defined $config{omega_cgi}) {
42                 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
43         }
44
45         # This is a mass dependency, so if the search form template
46         # changes, every page is rebuilt.
47         add_depends("", "templates/searchform.tmpl");
48 }
49
50 my $form;
51 sub pagetemplate (@) {
52         my %params=@_;
53         my $page=$params{page};
54         my $template=$params{template};
55
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;
63                 }
64
65                 $template->param(searchform => $form);
66         }
67 }
68
69 my $scrubber;
70 my $stemmer;
71 sub indexhtml (@) {
72         my %params=@_;
73
74         setupfiles();
75
76         # A unique pageterm is used to identify the document for a page.
77         my $pageterm=pageterm($params{page});
78         return unless defined $pageterm;
79         
80         my $db=xapiandb();
81         my $doc=Search::Xapian::Document->new();
82         my $caption=pagetitle($params{page});
83         my $title;
84         if (exists $pagestate{$params{page}}{meta} &&
85                 exists $pagestate{$params{page}}{meta}{title}) {
86                 $title=$pagestate{$params{page}}{meta}{title};
87         }
88         else {
89                 $title=$caption;
90         }
91
92         # Remove html from text to be indexed.
93         if (! defined $scrubber) {
94                 eval q{use HTML::Scrubber};
95                 if (! $@) {
96                         $scrubber=HTML::Scrubber->new(allow => []);
97                 }
98         }
99         my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
100         
101         # Take 512 characters for a sample, then extend it out
102         # if it stopped in the middle of a word.
103         my $size=512;
104         my ($sample)=substr($toindex, 0, $size);
105         if (length($sample) == $size) {
106                 my $max=length($toindex);
107                 my $next;
108                 while ($size < $max &&
109                        ($next=substr($toindex, $size++, 1)) !~ /\s/) {
110                         $sample.=$next;
111                 }
112         }
113         $sample=~s/\n/ /g;
114         
115         my $url=urlto($params{destpage}, "");
116         if (defined $pagestate{$params{page}}{meta}{permalink}) {
117                 $url=$pagestate{$params{page}}{meta}{permalink}
118         }
119
120         # data used by omega
121         # Decode html entities in it, since omega re-encodes them.
122         eval q{use HTML::Entities};
123         error $@ if $@;
124         $doc->set_data(
125                 "url=".$url."\n".
126                 "sample=".decode_entities($sample)."\n".
127                 "caption=".decode_entities($caption)."\n".
128                 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
129                 "size=".length($params{content})."\n"
130         );
131
132         # Index document and add terms for other metadata.
133         my $tg = Search::Xapian::TermGenerator->new();
134         if (! $stemmer) {
135                 my $langcode=$ENV{LANG} || "en";
136                 $langcode=~s/_.*//;
137
138                 # This whitelist is here to work around a xapian bug (#486138)
139                 my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
140
141                 if (grep { $_ eq $langcode } @whitelist) {
142                         $stemmer=Search::Xapian::Stem->new($langcode);
143                 }
144                 else {
145                         $stemmer=Search::Xapian::Stem->new("english");
146                 }
147         }
148         $tg->set_stemmer($stemmer);
149         $tg->set_document($doc);
150         $tg->index_text($params{page}, 2);
151         $tg->index_text($caption, 2);
152         $tg->index_text($title, 2) if $title ne $caption;
153         $tg->index_text($toindex);
154         $tg->index_text(lc($title), 1, "S"); # for title:foo
155         foreach my $link (@{$links{$params{page}}}) {
156                 $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
157         }
158
159         $doc->add_term($pageterm);
160         $db->replace_document_by_term($pageterm, $doc);
161 }
162
163 sub delete (@) {
164         my $db=xapiandb();
165         foreach my $page (@_) {
166                 my $pageterm=pageterm(pagename($page));
167                 $db->delete_document_by_term($pageterm) if defined $pageterm;
168         }
169 }
170
171 sub cgi ($) {
172         my $cgi=shift;
173
174         if (defined $cgi->param('P')) {
175                 # only works for GET requests
176                 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
177                 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
178                 $ENV{CGIURL}=$config{cgiurl},
179                 IkiWiki::loadindex();
180                 $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
181                         noimageinline => 1, linktext => "Help");
182                 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
183         }
184 }
185
186 sub pageterm ($) {
187         my $page=shift;
188
189         # 240 is the number used by omindex to decide when to hash an
190         # overlong term. This does not use a compatible hash method though.
191         if (length $page > 240) {
192                 eval q{use Digest::SHA1};
193                 if ($@) {
194                         debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
195                         return undef;
196                 }
197
198                 # Note no colon, therefore it's guaranteed to not overlap
199                 # with a page with the same name as the hash..
200                 return "U".lc(Digest::SHA1::sha1_hex($page));
201         }
202         else {
203                 return "U:".$page;
204         }
205 }
206
207 my $db;
208 sub xapiandb () {
209         if (! defined $db) {
210                 eval q{
211                         use Search::Xapian;
212                         use Search::Xapian::WritableDatabase;
213                 };
214                 error($@) if $@;
215                 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
216                         Search::Xapian::DB_CREATE_OR_OPEN());
217         }
218         return $db;
219 }
220
221 {
222 my $setup=0;
223 sub setupfiles () {
224         if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
225                 writefile("omega.conf", $config{wikistatedir}."/xapian",
226                         "database_dir .\n".
227                         "template_dir ./templates\n");
228                 
229                 # Avoid omega interpreting anything in the misctemplate
230                 # as an omegascript command.
231                 my $misctemplate=IkiWiki::misctemplate(gettext("search"), "\0",
232                         searchform => "", # avoid showing the small search form
233                 );
234                 eval q{use HTML::Entities};
235                 error $@ if $@;
236                 $misctemplate=encode_entities($misctemplate, '\$');
237
238                 my $querytemplate=readfile(IkiWiki::template_file("searchquery.tmpl"));
239                 $misctemplate=~s/\0/$querytemplate/;
240
241                 writefile("query", $config{wikistatedir}."/xapian/templates",
242                         $misctemplate);
243                 $setup=1;
244         }
245 }
246 }
247
248 1