summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: 29d31dfdc15d0e00ab326a5f9cce0aeeb54eb35c (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. if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
  24. writefile("omega.conf", $config{wikistatedir}."/xapian",
  25. "database_dir .\n".
  26. "template_dir ./templates\n");
  27. writefile("query", $config{wikistatedir}."/xapian/templates",
  28. IkiWiki::misctemplate(gettext("search"),
  29. readfile(IkiWiki::template_file("searchquery.tmpl"))));
  30. }
  31. } #}}}
  32. my $form;
  33. sub pagetemplate (@) { #{{{
  34. my %params=@_;
  35. my $page=$params{page};
  36. my $template=$params{template};
  37. # Add search box to page header.
  38. if ($template->query(name => "searchform")) {
  39. if (! defined $form) {
  40. my $searchform = template("searchform.tmpl", blind_cache => 1);
  41. $searchform->param(searchaction => $config{cgiurl});
  42. $form=$searchform->output;
  43. }
  44. $template->param(searchform => $form);
  45. }
  46. } #}}}
  47. my $scrubber;
  48. my $stemmer;
  49. sub index (@) { #{{{
  50. my %params=@_;
  51. return $params{content} if $IkiWiki::preprocessing{$params{destpage}};
  52. my $db=xapiandb();
  53. my $doc=Search::Xapian::Document->new();
  54. my $title=IkiWiki::pagetitle($params{page});
  55. # Remove html from text to be indexed.
  56. if (! defined $scrubber) {
  57. eval q{use HTML::Scrubber};
  58. if (! $@) {
  59. $scrubber=HTML::Scrubber->new(allow => []);
  60. }
  61. }
  62. my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
  63. # Take 512 characters for a sample, then extend it out
  64. # if it stopped in the middle of a word.
  65. my $size=512;
  66. my ($sample)=substr($toindex, 0, $size);
  67. if (length($sample) == $size) {
  68. my $max=length($toindex);
  69. my $next;
  70. while ($size < $max &&
  71. ($next=substr($toindex, $size++, 1)) !~ /\s/) {
  72. $sample.=$next;
  73. }
  74. }
  75. $sample=~s/\n/ /g;
  76. # data used by omega
  77. # Decode html entities in it, since omega re-encodes them.
  78. eval q{use HTML::Entities};
  79. $doc->set_data(
  80. "url=".urlto($params{page}, "")."\n".
  81. "sample=".decode_entities($sample)."\n".
  82. "caption=".decode_entities($title)."\n".
  83. "modtime=$IkiWiki::pagemtime{$params{page}}\n".
  84. "size=".length($params{content})."\n"
  85. );
  86. my $tg = Search::Xapian::TermGenerator->new();
  87. if (! $stemmer) {
  88. my $langcode=$ENV{LANG} || "en";
  89. $langcode=~s/_.*//;
  90. eval { $stemmer=Search::Xapian::Stem->new($langcode) };
  91. if ($@) {
  92. $stemmer=Search::Xapian::Stem->new("english");
  93. }
  94. }
  95. $tg->set_stemmer($stemmer);
  96. $tg->set_document($doc);
  97. $tg->index_text($params{page}, 2);
  98. $tg->index_text($title, 2);
  99. $tg->index_text($toindex);
  100. my $pageterm=pageterm($params{page});
  101. $doc->add_term($pageterm);
  102. $db->replace_document_by_term($pageterm, $doc);
  103. return $params{content};
  104. } #}}}
  105. sub delete (@) { #{{{
  106. my $db=xapiandb();
  107. foreach my $page (@_) {
  108. $db->delete_document_by_term(pageterm(pagename($page)));
  109. }
  110. } #}}}
  111. sub cgi ($) { #{{{
  112. my $cgi=shift;
  113. if (defined $cgi->param('P')) {
  114. # only works for GET requests
  115. chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
  116. $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
  117. $ENV{CGIURL}=$config{cgiurl},
  118. exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
  119. }
  120. } #}}}
  121. sub pageterm ($) { #{{{
  122. my $page=shift;
  123. # TODO: check if > 255 char page names overflow term
  124. # length; use sha1 if so?
  125. return "U:".$page;
  126. } #}}}
  127. my $db;
  128. sub xapiandb () { #{{{
  129. if (! defined $db) {
  130. eval q{
  131. use Search::Xapian;
  132. use Search::Xapian::WritableDatabase;
  133. };
  134. error($@) if $@;
  135. $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
  136. Search::Xapian::DB_CREATE_OR_OPEN());
  137. }
  138. return $db;
  139. } #}}}
  140. 1