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