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