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