search: Converted to use xapian-omega.
[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 => "delete", id => "search", call => \&delete);
13         hook(type => "change", id => "search", call => \&change);
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 sub delete (@) { #{{{
57         debug(gettext("cleaning xapian search index"));
58 } #}}}
59
60 sub change (@) { #{{{
61         debug(gettext("updating xapian search index"));
62 } #}}}
63
64 sub cgi ($) { #{{{
65         my $cgi=shift;
66
67         if (defined $cgi->param('P')) {
68                 # only works for GET requests
69                 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
70                 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
71                 $ENV{CGIURL}=$config{cgiurl},
72                 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
73         }
74 } #}}}
75
76 1