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 hook(type => "sanitize", id => "search", call => \&index);
13 hook(type => "delete", id => "search", call => \&delete);
14 hook(type => "cgi", id => "search", call => \&cgi);
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));
24 if (! exists $config{omega_cgi}) {
25 $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
30 sub pagetemplate (@) { #{{{
32 my $page=$params{page};
33 my $template=$params{template};
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;
43 $template->param(searchform => $form);
52 return $params{content} if $IkiWiki::preprocessing{$params{destpage}};
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;
61 my $doc=Search::Xapian::Document->new();
62 my $caption=IkiWiki::pagetitle($params{page});
64 if (exists $pagestate{$params{page}}{meta} &&
65 exists $pagestate{$params{page}}{meta}{title}) {
66 $title=$pagestate{$params{page}}{meta}{title};
72 # Remove html from text to be indexed.
73 if (! defined $scrubber) {
74 eval q{use HTML::Scrubber};
76 $scrubber=HTML::Scrubber->new(allow => []);
79 my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
81 # Take 512 characters for a sample, then extend it out
82 # if it stopped in the middle of a word.
84 my ($sample)=substr($toindex, 0, $size);
85 if (length($sample) == $size) {
86 my $max=length($toindex);
88 while ($size < $max &&
89 ($next=substr($toindex, $size++, 1)) !~ /\s/) {
96 # Decode html entities in it, since omega re-encodes them.
97 eval q{use HTML::Entities};
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"
106 # Index document and add terms for other metadata.
107 my $tg = Search::Xapian::TermGenerator->new();
109 my $langcode=$ENV{LANG} || "en";
111 eval { $stemmer=Search::Xapian::Stem->new($langcode) };
113 $stemmer=Search::Xapian::Stem->new("english");
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
127 $doc->add_term($pageterm);
128 $db->replace_document_by_term($pageterm, $doc);
130 return $params{content};
133 sub delete (@) { #{{{
135 foreach my $page (@_) {
136 my $pageterm=pageterm(pagename($page));
137 $db->delete_document_by_term($pageterm) if defined $pageterm;
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: $!");
156 sub pageterm ($) { #{{{
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};
164 debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
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));
178 sub xapiandb () { #{{{
182 use Search::Xapian::WritableDatabase;
185 $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
186 Search::Xapian::DB_CREATE_OR_OPEN());
191 sub setupfiles () { #{{{
192 if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
193 writefile("omega.conf", $config{wikistatedir}."/xapian",
195 "template_dir ./templates\n");
196 writefile("query", $config{wikistatedir}."/xapian/templates",
197 IkiWiki::misctemplate(gettext("search"),
198 readfile(IkiWiki::template_file("searchquery.tmpl"))));