summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: 3ac4351979e7b96bd81c334d7d873cb40d612498 (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. eval { $stemmer=Search::Xapian::Stem->new($langcode) };
  96. if ($@) {
  97. $stemmer=Search::Xapian::Stem->new("english");
  98. }
  99. }
  100. $tg->set_stemmer($stemmer);
  101. $tg->set_document($doc);
  102. $tg->index_text($params{page}, 2);
  103. $tg->index_text($caption, 2);
  104. $tg->index_text($title, 2) if $title ne $caption;
  105. $tg->index_text($toindex);
  106. $tg->index_text(lc($title), 1, "ZS"); # for title:foo
  107. foreach my $link (@{$links{$params{page}}}) {
  108. $tg->index_text(lc($link), 1, "ZLINK"); # for link:bar
  109. }
  110. $doc->add_term($pageterm);
  111. $db->replace_document_by_term($pageterm, $doc);
  112. return $params{content};
  113. } #}}}
  114. sub delete (@) { #{{{
  115. my $db=xapiandb();
  116. foreach my $page (@_) {
  117. my $pageterm=pageterm(pagename($page));
  118. $db->delete_document_by_term($pageterm) if defined $pageterm;
  119. }
  120. } #}}}
  121. sub cgi ($) { #{{{
  122. my $cgi=shift;
  123. if (defined $cgi->param('P')) {
  124. # only works for GET requests
  125. chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
  126. $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
  127. $ENV{CGIURL}=$config{cgiurl},
  128. IkiWiki::loadindex();
  129. $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
  130. noimageinline => 1, linktext => "Help");
  131. exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
  132. }
  133. } #}}}
  134. sub pageterm ($) { #{{{
  135. my $page=shift;
  136. # 240 is the number used by omindex to decide when to hash an
  137. # overlong term. This does not use a compatible hash method though.
  138. if (length $page > 240) {
  139. eval q{use Digest::SHA1};
  140. if ($@) {
  141. debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
  142. return undef;
  143. }
  144. # Note no colon, therefore it's guaranteed to not overlap
  145. # with a page with the same name as the hash..
  146. return "U".lc(Digest::SHA1::sha1_hex($page));
  147. }
  148. else {
  149. return "U:".$page;
  150. }
  151. } #}}}
  152. my $db;
  153. sub xapiandb () { #{{{
  154. if (! defined $db) {
  155. eval q{
  156. use Search::Xapian;
  157. use Search::Xapian::WritableDatabase;
  158. };
  159. error($@) if $@;
  160. $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
  161. Search::Xapian::DB_CREATE_OR_OPEN());
  162. }
  163. return $db;
  164. } #}}}
  165. sub setupfiles () { #{{{
  166. if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
  167. writefile("omega.conf", $config{wikistatedir}."/xapian",
  168. "database_dir .\n".
  169. "template_dir ./templates\n");
  170. writefile("query", $config{wikistatedir}."/xapian/templates",
  171. IkiWiki::misctemplate(gettext("search"),
  172. readfile(IkiWiki::template_file("searchquery.tmpl"))));
  173. }
  174. } #}}}
  175. 1