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