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