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