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