]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/search.pm
avoid warning
[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         # run last so other needsbuild hooks can modify the list
13         hook(type => "needsbuild", id => "search", call => \&needsbuild,
14                 last => 1);
15         hook(type => "filter", id => "search", call => \&filter);
16         hook(type => "delete", id => "search", call => \&delete);
17         hook(type => "cgi", id => "search", call => \&cgi);
18 } # }}}
19
20 sub checkconfig () { #{{{
21         foreach my $required (qw(url cgiurl)) {
22                 if (! length $config{$required}) {
23                         error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
24                 }
25         }
26
27         if (! exists $config{omega_cgi}) {
28                 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
29         }
30         
31         if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
32                 writefile("omega.conf", $config{wikistatedir}."/xapian",
33                         "database_dir .\n".
34                         "template_dir ./templates\n");
35                 writefile("query", $config{wikistatedir}."/xapian/templates",
36                         IkiWiki::misctemplate(gettext("search"),
37                                 readfile(IkiWiki::template_file("searchquery.tmpl"))));
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 %toindex;
60 sub needsbuild ($) { #{{{
61         %toindex = map { pagename($_) => 1 } @{shift()};
62 } #}}}
63
64 my $scrubber;
65 sub filter (@) { #{{{
66         my %params=@_;
67         
68         if ($params{page} eq $params{destpage} && $toindex{$params{page}}) {
69                 # index page
70                 my $db=xapiandb();
71                 my $doc=Search::Xapian::Document->new();
72                 my $title;
73                 if (exists $pagestate{$params{page}}{meta} &&
74                     exists $pagestate{$params{page}}{meta}{title}) {
75                         $title=$pagestate{$params{page}}{meta}{title};
76                 }
77                 else {
78                         $title=IkiWiki::pagetitle($params{page});
79                 }
80
81                 # Remove any html from text to be indexed.
82                 # TODO: This removes html that is in eg, a markdown pre,
83                 # which should not be removed.
84                 if (! defined $scrubber) {
85                         eval q{use HTML::Scrubber};
86                         error($@) if $@;
87                         $scrubber=HTML::Scrubber->new(allow => []);
88                 }
89                 my $toindex=$scrubber->scrub($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 $next=substr($toindex, $size++, 1);
97                         while (defined $next && $next !~ /\s/) {
98                                 $sample.=$next;
99                                 $next=substr($toindex, $size++, 1);
100                         }
101                 }
102                 $sample=~s/\n/ /g;
103
104                 # data used by omega
105                 $doc->set_data(
106                         "url=".urlto($params{page}, "")."\n".
107                         "sample=$sample\n".
108                         "caption=$title\n".
109                         "modtime=$IkiWiki::pagemtime{$params{page}}\n".
110                         "size=".length($params{content})."\n"
111                 );
112
113                 my $tg = Search::Xapian::TermGenerator->new();
114                 $tg->set_stemmer(new Search::Xapian::Stem("english"));
115                 $tg->set_document($doc);
116                 $tg->index_text($params{page}, 2);
117                 $tg->index_text($title, 2);
118                 $tg->index_text($toindex);
119
120                 my $pageterm=pageterm($params{page});
121                 $doc->add_term($pageterm);
122                 $db->replace_document_by_term($pageterm, $doc);
123         }
124
125         return $params{content};
126 } #}}}
127
128 sub delete (@) { #{{{
129         my $db=xapiandb();
130         foreach my $page (@_) {
131                 $db->delete_document_by_term(pageterm($page));
132         }
133 } #}}}
134
135 sub cgi ($) { #{{{
136         my $cgi=shift;
137
138         if (defined $cgi->param('P')) {
139                 # only works for GET requests
140                 chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
141                 $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
142                 $ENV{CGIURL}=$config{cgiurl},
143                 exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
144         }
145 } #}}}
146
147 sub pageterm ($) { #{{{
148         my $page=shift;
149
150         # TODO: check if > 255 char page names overflow term
151         # length; use sha1 if so?
152         return "U".$page;
153 } #}}}
154
155 my $db;
156 sub xapiandb () { #{{{
157         if (! defined $db) {
158                 eval q{
159                         use Search::Xapian;
160                         use Search::Xapian::WritableDatabase;
161                 };
162                 error($@) if $@;
163                 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
164                         Search::Xapian::DB_CREATE_OR_OPEN());
165         }
166         return $db;
167 } #}}}
168
169 1