summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: 55edf8752a8e04c4abbe9e320f36922ad13b0884 (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_depends("searchform.tmpl", $page, 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 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. error $@ if $@;
  99. $doc->set_data(
  100. "url=".urlto($params{page}, "")."\n".
  101. "sample=".decode_entities($sample)."\n".
  102. "caption=".decode_entities($caption)."\n".
  103. "modtime=$IkiWiki::pagemtime{$params{page}}\n".
  104. "size=".length($params{content})."\n"
  105. );
  106. # Index document and add terms for other metadata.
  107. my $tg = Search::Xapian::TermGenerator->new();
  108. if (! $stemmer) {
  109. my $langcode=$ENV{LANG} || "en";
  110. $langcode=~s/_.*//;
  111. # This whitelist is here to work around a xapian bug (#486138)
  112. my @whitelist=qw{da de en es fi fr hu it no pt ru ro sv tr};
  113. if (grep { $_ eq $langcode } @whitelist) {
  114. $stemmer=Search::Xapian::Stem->new($langcode);
  115. }
  116. else {
  117. $stemmer=Search::Xapian::Stem->new("english");
  118. }
  119. }
  120. $tg->set_stemmer($stemmer);
  121. $tg->set_document($doc);
  122. $tg->index_text($params{page}, 2);
  123. $tg->index_text($caption, 2);
  124. $tg->index_text($title, 2) if $title ne $caption;
  125. $tg->index_text($toindex);
  126. $tg->index_text(lc($title), 1, "S"); # for title:foo
  127. foreach my $link (@{$links{$params{page}}}) {
  128. $tg->index_text(lc($link), 1, "XLINK"); # for link:bar
  129. }
  130. $doc->add_term($pageterm);
  131. $db->replace_document_by_term($pageterm, $doc);
  132. }
  133. sub delete (@) {
  134. my $db=xapiandb();
  135. foreach my $page (@_) {
  136. my $pageterm=pageterm(pagename($page));
  137. $db->delete_document_by_term($pageterm) if defined $pageterm;
  138. }
  139. }
  140. sub cgi ($) {
  141. my $cgi=shift;
  142. if (defined $cgi->param('P')) {
  143. # only works for GET requests
  144. chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
  145. $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
  146. $ENV{CGIURL}=$config{cgiurl},
  147. IkiWiki::loadindex();
  148. $ENV{HELPLINK}=htmllink("", "", "ikiwiki/searching",
  149. noimageinline => 1, linktext => "Help");
  150. exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
  151. }
  152. }
  153. sub pageterm ($) {
  154. my $page=shift;
  155. # 240 is the number used by omindex to decide when to hash an
  156. # overlong term. This does not use a compatible hash method though.
  157. if (length $page > 240) {
  158. eval q{use Digest::SHA1};
  159. if ($@) {
  160. debug("search: ".sprintf(gettext("need Digest::SHA1 to index %s"), $page)) if $@;
  161. return undef;
  162. }
  163. # Note no colon, therefore it's guaranteed to not overlap
  164. # with a page with the same name as the hash..
  165. return "U".lc(Digest::SHA1::sha1_hex($page));
  166. }
  167. else {
  168. return "U:".$page;
  169. }
  170. }
  171. my $db;
  172. sub xapiandb () {
  173. if (! defined $db) {
  174. eval q{
  175. use Search::Xapian;
  176. use Search::Xapian::WritableDatabase;
  177. };
  178. error($@) if $@;
  179. $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
  180. Search::Xapian::DB_CREATE_OR_OPEN());
  181. }
  182. return $db;
  183. }
  184. {
  185. my $setup=0;
  186. sub setupfiles () {
  187. if (! $setup and (! -e $config{wikistatedir}."/xapian" || $config{rebuild})) {
  188. writefile("omega.conf", $config{wikistatedir}."/xapian",
  189. "database_dir .\n".
  190. "template_dir ./templates\n");
  191. # Avoid omega interpreting anything in the misctemplate
  192. # as an omegascript command.
  193. my $misctemplate=IkiWiki::misctemplate(gettext("search"), "\0");
  194. eval q{use HTML::Entities};
  195. error $@ if $@;
  196. $misctemplate=encode_entities($misctemplate, '\$');
  197. my $querytemplate=readfile(IkiWiki::template_file("searchquery.tmpl"));
  198. $misctemplate=~s/\0/$querytemplate/;
  199. writefile("query", $config{wikistatedir}."/xapian/templates",
  200. $misctemplate);
  201. $setup=1;
  202. }
  203. }
  204. }
  205. 1