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