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