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