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