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