2 # xapian-omega search engine plugin
3 package IkiWiki::Plugin::search;
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,
15 hook(type => "filter", id => "search", call => \&filter);
16 hook(type => "delete", id => "search", call => \&delete);
17 hook(type => "cgi", id => "search", call => \&cgi);
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));
27 if (! exists $config{omega_cgi}) {
28 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
31 if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
32 writefile("omega.conf", $config{wikistatedir}."/xapian",
34 "template_dir ./templates\n");
35 writefile("query", $config{wikistatedir}."/xapian/templates",
36 IkiWiki::misctemplate(gettext("search"),
37 readfile(IkiWiki::template_file("searchquery.tmpl"))));
42 sub pagetemplate (@) { #{{{
44 my $page=$params{page};
45 my $template=$params{template};
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;
55 $template->param(searchform => $form);
60 sub needsbuild ($) { #{{{
61 %toindex = map { pagename($_) => 1 } @{shift()};
68 if ($params{page} eq $params{destpage} && $toindex{$params{page}}) {
71 my $doc=Search::Xapian::Document->new();
73 if (exists $pagestate{$params{page}}{meta} &&
74 exists $pagestate{$params{page}}{meta}{title}) {
75 $title=$pagestate{$params{page}}{meta}{title};
78 $title=IkiWiki::pagetitle($params{page});
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, really.
84 if (! defined $scrubber) {
85 eval q{use HTML::Scrubber};
87 $scrubber=HTML::Scrubber->new(allow => []);
90 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
92 # Take 512 characters for a sample, then extend it out
93 # if it stopped in the middle of a word.
95 my ($sample)=substr($toindex, 0, $size);
96 if (length($sample) == $size) {
97 my $max=length($toindex);
99 while ($size < $max &&
100 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
108 "url=".urlto($params{page}, "")."\n".
111 "modtime=$IkiWiki::pagemtime{$params{page}}\n".
112 "size=".length($params{content})."\n"
115 my $tg = Search::Xapian::TermGenerator->new();
116 $tg->set_stemmer(new Search::Xapian::Stem("english"));
117 $tg->set_document($doc);
118 $tg->index_text($params{page}, 2);
119 $tg->index_text($title, 2);
120 $tg->index_text($toindex);
122 my $pageterm=pageterm($params{page});
123 $doc->add_term($pageterm);
124 $db->replace_document_by_term($pageterm, $doc);
127 return $params{content};
130 sub delete (@) { #{{{
132 foreach my $page (@_) {
133 $db->delete_document_by_term(pageterm($page));
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: $!");
149 sub pageterm ($) { #{{{
152 # TODO: check if > 255 char page names overflow term
153 # length; use sha1 if so?
158 sub xapiandb () { #{{{
162 use Search::Xapian::WritableDatabase;
165 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
166 Search::Xapian::DB_CREATE_OR_OPEN());