summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: dd44855bd067185f5fdbe9ecd2d380e45bed8cc2 (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. # run last so other needsbuild hooks can modify the list
  11. hook(type => "needsbuild", id => "search", call => \&needsbuild,
  12. last => 1);
  13. hook(type => "filter", id => "search", call => \&filter);
  14. hook(type => "delete", id => "search", call => \&delete);
  15. hook(type => "cgi", id => "search", call => \&cgi);
  16. } # }}}
  17. sub checkconfig () { #{{{
  18. foreach my $required (qw(url cgiurl)) {
  19. if (! length $config{$required}) {
  20. error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
  21. }
  22. }
  23. if (! exists $config{omega_cgi}) {
  24. $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
  25. }
  26. if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
  27. writefile("omega.conf", $config{wikistatedir}."/xapian",
  28. "database_dir .\n".
  29. "template_dir ./templates\n");
  30. writefile("query", $config{wikistatedir}."/xapian/templates",
  31. IkiWiki::misctemplate(gettext("search"),
  32. readfile(IkiWiki::template_file("searchquery.tmpl"))));
  33. }
  34. } #}}}
  35. my $form;
  36. sub pagetemplate (@) { #{{{
  37. my %params=@_;
  38. my $page=$params{page};
  39. my $template=$params{template};
  40. # Add search box to page header.
  41. if ($template->query(name => "searchform")) {
  42. if (! defined $form) {
  43. my $searchform = template("searchform.tmpl", blind_cache => 1);
  44. $searchform->param(searchaction => $config{cgiurl});
  45. $form=$searchform->output;
  46. }
  47. $template->param(searchform => $form);
  48. }
  49. } #}}}
  50. my %toindex;
  51. sub needsbuild ($) { #{{{
  52. %toindex = map { pagename($_) => 1 } @{shift()};
  53. } #}}}
  54. my $scrubber;
  55. sub filter (@) { #{{{
  56. my %params=@_;
  57. if ($params{page} eq $params{destpage} && $toindex{$params{page}}) {
  58. # index page
  59. my $db=xapiandb();
  60. my $doc=Search::Xapian::Document->new();
  61. my $title;
  62. if (exists $pagestate{$params{page}}{meta} &&
  63. exists $pagestate{$params{page}}{meta}{title}) {
  64. $title=$pagestate{$params{page}}{meta}{title};
  65. }
  66. else {
  67. $title=IkiWiki::pagetitle($params{page});
  68. }
  69. # Remove any html from text to be indexed.
  70. # TODO: This removes html that is in eg, a markdown pre,
  71. # which should not be removed.
  72. if (! defined $scrubber) {
  73. eval q{use HTML::Scrubber};
  74. error($@) if $@;
  75. $scrubber=HTML::Scrubber->new(allow => []);
  76. }
  77. my $toindex=$scrubber->scrub($params{content});
  78. # Take 512 characters for a sample, then extend it out
  79. # if it stopped in the middle of a word.
  80. my $size=512;
  81. my ($sample)=substr($toindex, 0, $size);
  82. my $next=substr($toindex, $size++, 1);
  83. while (defined $next && $next !~ /\s/) {
  84. $sample.=$next;
  85. $next=substr($toindex, $size++, 1);
  86. }
  87. $sample=~s/\n/ /g;
  88. # data used by omega
  89. $doc->set_data(
  90. "url=".urlto($params{page}, "")."\n".
  91. "sample=$sample\n".
  92. "caption=$title\n".
  93. "modtime=$IkiWiki::pagemtime{$params{page}}\n".
  94. "size=".length($params{content})."\n"
  95. );
  96. my $tg = Search::Xapian::TermGenerator->new();
  97. $tg->set_stemmer(new Search::Xapian::Stem("english"));
  98. $tg->set_document($doc);
  99. $tg->index_text($params{page}, 2);
  100. $tg->index_text($title, 2);
  101. $tg->index_text($toindex);
  102. my $pageterm=pageterm($params{page});
  103. $doc->add_term($pageterm);
  104. $db->replace_document_by_term($pageterm, $doc);
  105. }
  106. return $params{content};
  107. } #}}}
  108. sub delete (@) { #{{{
  109. my $db=xapiandb();
  110. foreach my $page (@_) {
  111. $db->delete_document_by_term(pageterm($page));
  112. }
  113. } #}}}
  114. sub cgi ($) { #{{{
  115. my $cgi=shift;
  116. if (defined $cgi->param('P')) {
  117. # only works for GET requests
  118. chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
  119. $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
  120. $ENV{CGIURL}=$config{cgiurl},
  121. exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
  122. }
  123. } #}}}
  124. sub pageterm ($) { #{{{
  125. my $page=shift;
  126. # TODO: check if > 255 char page names overflow term
  127. # length; use sha1 if so?
  128. return "P".$page;
  129. } #}}}
  130. my $db;
  131. sub xapiandb () { #{{{
  132. if (! defined $db) {
  133. eval q{
  134. use Search::Xapian;
  135. use Search::Xapian::WritableDatabase;
  136. };
  137. error($@) if $@;
  138. $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
  139. Search::Xapian::DB_CREATE_OR_OPEN());
  140. }
  141. return $db;
  142. } #}}}
  143. 1