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