have the xapian stemmer use a language based on LANG
[ikiwiki] / 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 => "sanitize", 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         if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
29                 writefile("omega.conf", $config{wikistatedir}."/xapian",
30                         "database_dir .\n".
31                         "template_dir ./templates\n");
32                 writefile("query", $config{wikistatedir}."/xapian/templates",
33                         IkiWiki::misctemplate(gettext("search"),
34                                 readfile(IkiWiki::template_file("searchquery.tmpl"))));
35         }
36 } #}}}
37
38 my $form;
39 sub pagetemplate (@) { #{{{
40         my %params=@_;
41         my $page=$params{page};
42         my $template=$params{template};
43
44         # Add search box to page header.
45         if ($template->query(name => "searchform")) {
46                 if (! defined $form) {
47                         my $searchform = template("searchform.tmpl", blind_cache => 1);
48                         $searchform->param(searchaction => $config{cgiurl});
49                         $form=$searchform->output;
50                 }
51
52                 $template->param(searchform => $form);
53         }
54 } #}}}
55
56 my $scrubber;
57 my $stemmer;
58 sub index (@) { #{{{
59         my %params=@_;
60         
61         return $params{content} if $IkiWiki::preprocessing{$params{destpage}};
62         
63         my $db=xapiandb();
64         my $doc=Search::Xapian::Document->new();
65         my $title;
66         if (exists $pagestate{$params{page}}{meta} &&
67             exists $pagestate{$params{page}}{meta}{title}) {
68                 $title=$pagestate{$params{page}}{meta}{title};
69         }
70         else {
71                 $title=IkiWiki::pagetitle($params{page});
72         }
73
74         # Remove html from text to be indexed.
75         if (! defined $scrubber) {
76                 eval q{use HTML::Scrubber};
77                 if (! $@) {
78                         $scrubber=HTML::Scrubber->new(allow => []);
79                 }
80         }
81         my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
82         
83         # Take 512 characters for a sample, then extend it out
84         # if it stopped in the middle of a word.
85         my $size=512;
86         my ($sample)=substr($toindex, 0, $size);
87         if (length($sample) == $size) {
88                 my $max=length($toindex);
89                 my $next;
90                 while ($size < $max &&
91                        ($next=substr($toindex, $size++, 1)) !~ /\s/) {
92                         $sample.=$next;
93                 }
94         }
95         $sample=~s/\n/ /g;
96         
97         # data used by omega
98         # Decode html entities in it, since omega re-encodes them.
99         eval q{use HTML::Entities};
100         $doc->set_data(
101                 "url=".urlto($params{page}, "")."\n".
102                 "sample=".decode_entities($sample)."\n".
103                 "caption=".decode_entities($title)."\n".
104                 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
105                 "size=".length($params{content})."\n"
106         );
107
108         my $tg = Search::Xapian::TermGenerator->new();
109         if (! $stemmer) {
110                 my $langcode=$ENV{LANG} || "en";
111                 $langcode=~s/_.*//;
112                 eval { $stemmer=Search::Xapian::Stem->new($langcode) };
113                 if ($@) {
114                         $stemmer=Search::Xapian::Stem->new("english");
115                 }
116         }
117         $tg->set_stemmer($stemmer);
118         $tg->set_document($doc);
119         $tg->index_text($params{page}, 2);
120         $tg->index_text($title, 2);
121         $tg->index_text($toindex);
122
123         my $pageterm=pageterm($params{page});
124         $doc->add_term($pageterm);
125         $db->replace_document_by_term($pageterm, $doc);
126
127         return $params{content};
128 } #}}}
129
130 sub delete (@) { #{{{
131         my $db=xapiandb();
132         foreach my $page (@_) {
133                 $db->delete_document_by_term(pageterm(pagename($page)));
134         }
135 } #}}}
136
137 sub cgi ($) { #{{{
138         my $cgi=shift;
139
140         if (defined $cgi->param('P')) {
141                 # only works for GET requests
142                 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
143                 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
144                 $ENV{CGIURL}=$config{cgiurl},
145                 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
146         }
147 } #}}}
148
149 sub pageterm ($) { #{{{
150         my $page=shift;
151
152         # TODO: check if > 255 char page names overflow term
153         # length; use sha1 if so?
154         return "U:".$page;
155 } #}}}
156
157 my $db;
158 sub xapiandb () { #{{{
159         if (! defined $db) {
160                 eval q{
161                         use Search::Xapian;
162                         use Search::Xapian::WritableDatabase;
163                 };
164                 error($@) if $@;
165                 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
166                         Search::Xapian::DB_CREATE_OR_OPEN());
167         }
168         return $db;
169 } #}}}
170
171 1