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