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