summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: c8e0dd580224a7dc810208c1ac3f294633b1f5ab (plain)
  1. #!/usr/bin/perl
  2. # hyperestraier search engine plugin
  3. package IkiWiki::Plugin::search;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. sub import { #{{{
  8. IkiWiki::hook(type => "checkconfig", id => "hyperestraier",
  9. call => \&checkconfig);
  10. IkiWiki::hook(type => "pagetemplate", id => "hyperestraier",
  11. call => \&pagetemplate);
  12. IkiWiki::hook(type => "delete", id => "hyperestraier",
  13. call => \&delete);
  14. IkiWiki::hook(type => "change", id => "hyperestraier",
  15. call => \&change);
  16. IkiWiki::hook(type => "cgi", id => "hyperestraier",
  17. call => \&cgi);
  18. } # }}}
  19. sub checkconfig () { #{{{
  20. foreach my $required (qw(url cgiurl)) {
  21. if (! length $IkiWiki::config{$required}) {
  22. IkiWiki::error("Must specify $required when using the search plugin\n");
  23. }
  24. }
  25. } #}}}
  26. my $form;
  27. sub pagetemplate (@) { #{{{
  28. my %params=@_;
  29. my $page=$params{page};
  30. my $template=$params{template};
  31. # Add search box to page header.
  32. if ($template->query(name => "searchform")) {
  33. if (! defined $form) {
  34. my $searchform = IkiWiki::template("searchform.tmpl", blind_cache => 1);
  35. $searchform->param(searchaction => $IkiWiki::config{cgiurl});
  36. $form=$searchform->output;
  37. }
  38. $template->param(searchform => $form);
  39. }
  40. } #}}}
  41. sub delete (@) { #{{{
  42. IkiWiki::debug("cleaning hyperestraier search index");
  43. IkiWiki::estcmd("purge -cl");
  44. IkiWiki::estcfg();
  45. } #}}}
  46. sub change (@) { #{{{
  47. IkiWiki::debug("updating hyperestraier search index");
  48. IkiWiki::estcmd("gather -cm -bc -cl -sd",
  49. map {
  50. Encode::encode_utf8($IkiWiki::config{destdir}."/".$IkiWiki::renderedfiles{IkiWiki::pagename($_)})
  51. } @_
  52. );
  53. IkiWiki::estcfg();
  54. } #}}}
  55. sub cgi ($) { #{{{
  56. my $cgi=shift;
  57. if (defined $cgi->param('phrase')) {
  58. # only works for GET requests
  59. chdir("$IkiWiki::config{wikistatedir}/hyperestraier") || IkiWiki::error("chdir: $!");
  60. exec("./".IkiWiki::basename($IkiWiki::config{cgiurl})) || IkiWiki::error("estseek.cgi failed");
  61. }
  62. } #}}}
  63. # Easier to keep these in the IkiWiki namespace.
  64. package IkiWiki;
  65. my $configured=0;
  66. sub estcfg () { #{{{
  67. return if $configured;
  68. $configured=1;
  69. my $estdir="$config{wikistatedir}/hyperestraier";
  70. my $cgi=basename($config{cgiurl});
  71. $cgi=~s/\..*$//;
  72. open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
  73. error("write $estdir/$cgi.tmpl: $!");
  74. print TEMPLATE misctemplate("search",
  75. "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
  76. close TEMPLATE;
  77. open(TEMPLATE, ">$estdir/$cgi.conf") ||
  78. error("write $estdir/$cgi.conf: $!");
  79. my $template=template("estseek.conf");
  80. eval q{use Cwd 'abs_path'};
  81. $template->param(
  82. index => $estdir,
  83. tmplfile => "$estdir/$cgi.tmpl",
  84. destdir => abs_path($config{destdir}),
  85. url => $config{url},
  86. );
  87. print TEMPLATE $template->output;
  88. close TEMPLATE;
  89. $cgi="$estdir/".basename($config{cgiurl});
  90. unlink($cgi);
  91. symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
  92. error("symlink $cgi: $!");
  93. } # }}}
  94. sub estcmd ($;@) { #{{{
  95. my @params=split(' ', shift);
  96. push @params, "-cl", "$config{wikistatedir}/hyperestraier";
  97. if (@_) {
  98. push @params, "-";
  99. }
  100. my $pid=open(CHILD, "|-");
  101. if ($pid) {
  102. # parent
  103. foreach (@_) {
  104. print CHILD "$_\n";
  105. }
  106. close(CHILD) || error("estcmd @params exited nonzero: $?");
  107. }
  108. else {
  109. # child
  110. open(STDOUT, "/dev/null"); # shut it up (closing won't work)
  111. exec("estcmd", @params) || error("can't run estcmd");
  112. }
  113. } #}}}
  114. 1