summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: a28d58a1d4b5403583d0cd5207f6c7bf2ada0183 (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 => "checkconfig", id => "search", call => \&checkconfig);
  9. hook(type => "pagetemplate", id => "search", call => \&pagetemplate);
  10. # run last so other needsbuild hooks can modify the list
  11. hook(type => "needsbuild", id => "search", call => \&needsbuild,
  12. last => 1);
  13. hook(type => "filter", id => "search", call => \&filter);
  14. hook(type => "delete", id => "search", call => \&delete);
  15. hook(type => "cgi", id => "search", call => \&cgi);
  16. } # }}}
  17. sub checkconfig () { #{{{
  18. foreach my $required (qw(url cgiurl)) {
  19. if (! length $config{$required}) {
  20. error(sprintf(gettext("Must specify %s when using the search plugin"), $required));
  21. }
  22. }
  23. if (! exists $config{omega_cgi}) {
  24. $config{omega_cgi}="/usr/lib/cgi-bin/omega/omega";
  25. }
  26. if (! -e $config{wikistatedir}."/xapian" || $config{rebuild}) {
  27. writefile("omega.conf", $config{wikistatedir}."/xapian",
  28. "database_dir .\n".
  29. "template_dir ./templates\n");
  30. writefile("query", $config{wikistatedir}."/xapian/templates",
  31. IkiWiki::misctemplate(gettext("search"),
  32. readfile(IkiWiki::template_file("searchquery.tmpl"))));
  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 %toindex;
  51. sub needsbuild ($) { #{{{
  52. %toindex = map { $_ => 1 } @{shift()};
  53. } #}}}
  54. sub filter (@) { #{{{
  55. my %params=@_;
  56. if ($params{page} eq $params{destpage} && $toindex{$params{page}}) {
  57. # index page
  58. my $db=xapiandb();
  59. my $doc=Search::Xapian::Document->new();
  60. my $title=$params{page};
  61. if (exists $pagestate{$params{page}}{meta} &&
  62. exists $pagestate{$params{page}}{meta}{title}) {
  63. $title=$pagestate{$params{page}}{meta}{title};
  64. }
  65. # data used by omega
  66. $doc->set_data(
  67. "url=".urlto($params{page}, "")."\n".
  68. "sample=\n". # TODO
  69. "caption=$title\n".
  70. "modtime=$IkiWiki::pagemtime{$params{page}}\n".
  71. "size=".length($params{content})."\n"
  72. );
  73. my $tg = Search::Xapian::TermGenerator->new();
  74. $tg->set_stemmer(new Search::Xapian::Stem("english"));
  75. $tg->set_document($doc);
  76. $tg->index_text($params{page}, 2);
  77. $tg->index_text($title, 2);
  78. $tg->index_text($params{content}); # TODO html strip; preprocessor too
  79. my $pageterm=pageterm($params{page});
  80. $doc->add_term($pageterm);
  81. $db->replace_document_by_term($pageterm, $doc);
  82. }
  83. return $params{content};
  84. } #}}}
  85. sub delete (@) { #{{{
  86. my $db=xapiandb();
  87. foreach my $page (@_) {
  88. $db->delete_document_by_term(pageterm($page));
  89. }
  90. } #}}}
  91. sub cgi ($) { #{{{
  92. my $cgi=shift;
  93. if (defined $cgi->param('P')) {
  94. # only works for GET requests
  95. chdir("$config{wikistatedir}/xapian") || error("chdir: $!");
  96. $ENV{OMEGA_CONFIG_FILE}="./omega.conf";
  97. $ENV{CGIURL}=$config{cgiurl},
  98. exec($config{omega_cgi}) || error("$config{omega_cgi} failed: $!");
  99. }
  100. } #}}}
  101. sub pageterm ($) { #{{{
  102. my $page=shift;
  103. # TODO: check if > 255 char page names overflow term
  104. # length; use sha1 if so?
  105. return "P".$page;
  106. } #}}}
  107. my $db;
  108. sub xapiandb () { #{{{
  109. if (! defined $db) {
  110. eval q{
  111. use Search::Xapian;
  112. use Search::Xapian::WritableDatabase;
  113. };
  114. error($@) if $@;
  115. $db=Search::Xapian::WritableDatabase->new($config{wikistatedir}."/xapian/default",
  116. Search::Xapian::DB_CREATE_OR_OPEN());
  117. }
  118. return $db;
  119. } #}}}
  120. 1