summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: aa0a8085e16867c3cc54fbe1aeacd3481ef9a6d3 (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. sub index (@) { #{{{
  49. my %params=@_;
  50. return $params{content} if $IkiWiki::preprocessing{$params{destpage}};
  51. my $db=xapiandb();
  52. my $doc=Search::Xapian::Document->new();
  53. my $title;
  54. if (exists $pagestate{$params{page}}{meta} &&
  55. exists $pagestate{$params{page}}{meta}{title}) {
  56. $title=$pagestate{$params{page}}{meta}{title};
  57. }
  58. else {
  59. $title=IkiWiki::pagetitle($params{page});
  60. }
  61. # Remove html from text to be indexed.
  62. if (! defined $scrubber) {
  63. eval q{use HTML::Scrubber};
  64. if (! $@) {
  65. $scrubber=HTML::Scrubber->new(allow => []);
  66. }
  67. }
  68. my $toindex = defined $scrubber ? $scrubber->scrub($params{content}) : $params{content};
  69. # Take 512 characters for a sample, then extend it out
  70. # if it stopped in the middle of a word.
  71. my $size=512;
  72. my ($sample)=substr($toindex, 0, $size);
  73. if (length($sample) == $size) {
  74. my $max=length($toindex);
  75. my $next;
  76. while ($size < $max &&
  77. ($next=substr($toindex, $size++, 1)) !~ /\s/) {
  78. $sample.=$next;
  79. }
  80. }
  81. $sample=~s/\n/ /g;
  82. # data used by omega
  83. $doc->set_data(
  84. "url=".urlto($params{page}, "")."\n".
  85. "sample=$sample\n".
  86. "caption=$title\n".
  87. "modtime=$IkiWiki::pagemtime{$params{page}}\n".
  88. "size=".length($params{content})."\n"
  89. );
  90. my $tg = Search::Xapian::TermGenerator->new();
  91. $tg->set_stemmer(new Search::Xapian::Stem("english"));
  92. $tg->set_document($doc);
  93. $tg->index_text($params{page}, 2);
  94. $tg->index_text($title, 2);
  95. $tg->index_text($toindex);
  96. my $pageterm=pageterm($params{page});
  97. $doc->add_term($pageterm);
  98. $db->replace_document_by_term($pageterm, $doc);
  99. return $params{content};
  100. } #}}}
  101. sub delete (@) { #{{{
  102. my $db=xapiandb();
  103. foreach my $page (@_) {
  104. $db->delete_document_by_term(pageterm($page));
  105. }
  106. } #}}}
  107. sub cgi ($) { #{{{
  108. my $cgi=shift;
  109. if (defined $cgi->param('P')) {
  110. # only works for GET requests
  111. chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
  112. $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
  113. $ENV{CGIURL}=$config{cgiurl},
  114. exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
  115. }
  116. } #}}}
  117. sub pageterm ($) { #{{{
  118. my $page=shift;
  119. # TODO: check if > 255 char page names overflow term
  120. # length; use sha1 if so?
  121. return "U:".$page;
  122. } #}}}
  123. my $db;
  124. sub xapiandb () { #{{{
  125. if (! defined $db) {
  126. eval q{
  127. use Search::Xapian;
  128. use Search::Xapian::WritableDatabase;
  129. };
  130. error($@) if $@;
  131. $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
  132. Search::Xapian::DB_CREATE_OR_OPEN());
  133. }
  134. return $db;
  135. } #}}}
  136. 1