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