]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/search.pm
comments: collect metadata in a scan-phase preprocess hook
[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         hook(type => "disable", id => "search", call => \&disable);
17         hook(type => "needsbuild", id => "search", call => \&needsbuild);
18 }
19
20 sub getsetup () {
21         return
22                 plugin => {
23                         safe => 1,
24                         rebuild => 1,
25                         section => "web",
26                 },
27                 omega_cgi => {
28                         type => "string",
29                         example => "/usr/lib/cgi-bin/omega/omega",
30                         description => "path to the omega cgi program",
31                         safe => 0, # external program
32                         rebuild => 0,
33                 },
34 }
35
36 sub checkconfig () {
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'));
40                 }
41         }
42         
43         if (! defined $config{omega_cgi}) {
44                 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
45         }
46
47         # This is a mass dependency, so if the search form template
48         # changes, every page is rebuilt.
49         add_depends("", "templates/searchform.tmpl");
50 }
51
52 my $form;
53 sub pagetemplate (@) {
54         my %params=@_;
55         my $page=$params{page};
56         my $template=$params{template};
57
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;
65                 }
66
67                 $template->param(searchform => $form);
68         }
69 }
70
71 my $scrubber;
72 my $stemmer;
73 sub indexhtml (@) {
74         my %params=@_;
75
76         setupfiles();
77
78         # A unique pageterm is used to identify the document for a page.
79         my $pageterm=pageterm($params{page});
80         return unless defined $pageterm;
81         
82         my $db=xapiandb();
83         my $doc=Search::Xapian::Document->new();
84         my $caption=pagetitle($params{page});
85         my $title;
86         if (exists $pagestate{$params{page}}{meta} &&
87                 exists $pagestate{$params{page}}{meta}{title}) {
88                 $title=$pagestate{$params{page}}{meta}{title};
89         }
90         else {
91                 $title=$caption;
92         }
93
94         # Remove html from text to be indexed.
95         if (! defined $scrubber) {
96                 eval q{use HTML::Scrubber};
97                 if (! $@) {
98                         $scrubber=HTML::Scrubber->new(allow => []);
99                 }
100         }
101         my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
102         
103         # Take 512 characters for a sample, then extend it out
104         # if it stopped in the middle of a word.
105         my $size=512;
106         my ($sample)=substr($toindex, 0, $size);
107         if (length($sample) == $size) {
108                 my $max=length($toindex);
109                 my $next;
110                 while ($size < $max &&
111                        ($next=substr($toindex, $size++, 1)) !~ /\s/) {
112                         $sample.=$next;
113                 }
114         }
115         $sample=~s/\n/ /g;
116         
117         my $url=urlto($params{destpage}, "");
118         if (defined $pagestate{$params{page}}{meta}{permalink}) {
119                 $url=$pagestate{$params{page}}{meta}{permalink}
120         }
121
122         # data used by omega
123         # Decode html entities in it, since omega re-encodes them.
124         eval q{use HTML::Entities};
125         error $@ if $@;
126         $doc->set_data(
127                 "url=".$url."\n".
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"
132         );
133
134         # Index document and add terms for other metadata.
135         my $tg = Search::Xapian::TermGenerator->new();
136         if (! $stemmer) {
137                 my $langcode=$ENV{LANG} || "en";
138                 $langcode=~s/_.*//;
139
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};
142
143                 if (grep { $_ eq $langcode } @whitelist) {
144                         $stemmer=Search::Xapian::Stem->new($langcode);
145                 }
146                 else {
147                         $stemmer=Search::Xapian::Stem->new("english");
148                 }
149         }
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
159         }
160
161         $doc->add_term($pageterm);
162         $db->replace_document_by_term($pageterm, $doc);
163 }
164
165 sub delete (@) {
166         my $db=xapiandb();
167         foreach my $page (@_) {
168                 my $pageterm=pageterm(pagename($page));
169                 $db->delete_document_by_term($pageterm) if defined $pageterm;
170         }
171 }
172
173 sub cgi ($) {
174         my $cgi=shift;
175
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: $!");
185         }
186 }
187
188 sub pageterm ($) {
189         my $page=shift;
190
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};
195                 if ($@) {
196                         debug("search: ".sprintf(gettext("need Digest::SHA to index %s"), $page)) if $@;
197                         return undef;
198                 }
199
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));
203         }
204         else {
205                 return "U:".$page;
206         }
207 }
208
209 my $db;
210 sub xapiandb () {
211         if (! defined $db) {
212                 eval q{
213                         use Search::Xapian;
214                         use Search::Xapian::WritableDatabase;
215                 };
216                 error($@) if $@;
217                 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
218                         Search::Xapian::DB_CREATE_OR_OPEN());
219         }
220         return $db;
221 }
222
223 {
224 my $setup=0;
225 sub setupfiles () {
226         if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
227                 writefile("omega.conf", $config{wikistatedir}."/xapian",
228                         "database_dir .\n".
229                         "template_dir ./templates\n");
230                 omega_template();       
231                 $setup=1;
232         }
233 }
234 }
235
236 sub needsbuild {
237         my $list=shift;
238         if (grep {
239                 $_ eq "templates/page.tmpl" ||
240                 $_ eq "templates/searchquery.tmpl"
241         } @$list) {
242                 omega_template();
243         }
244 }
245
246 sub omega_template {
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
252         );
253         eval q{use HTML::Entities};
254         error $@ if $@;
255         $template=encode_entities($template, '\$');
256
257         my $querytemplate=readfile(IkiWiki::template_file("searchquery.tmpl"));
258         $template=~s/\0/$querytemplate/;
259         writefile("query", $config{wikistatedir}."/xapian/templates",
260                 $template);
261 }
262
263 sub disable () {
264         if (-d $config{wikistatedir}."/xapian") {
265                 system("rm", "-rf", $config{wikistatedir}."/xapian");
266         }
267 }
268
269 1