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