summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: 5a4dfd491a896047e354b46af7f389ff3b994db5 (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}."/".$renderedfiles{pagename($_)})
  58. } @_
  59. );
  60. estcfg();
  61. } #}}}
  62. sub cgi ($) { #{{{
  63. my $cgi=shift;
  64. if (defined $cgi->param('phrase')) {
  65. # only works for GET requests
  66. chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
  67. exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
  68. }
  69. } #}}}
  70. my $configured=0;
  71. sub estcfg () { #{{{
  72. return if $configured;
  73. $configured=1;
  74. my $estdir="$config{wikistatedir}/hyperestraier";
  75. my $cgi=IkiWiki::basename($config{cgiurl});
  76. $cgi=~s/\..*$//;
  77. open(TEMPLATE, ">:utf8", "$estdir/$cgi.tmpl") ||
  78. error("write $estdir/$cgi.tmpl: $!");
  79. print TEMPLATE IkiWiki::misctemplate("search",
  80. "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
  81. baseurl => IkiWiki::dirname($config{cgiurl})."/");
  82. close TEMPLATE;
  83. open(TEMPLATE, ">$estdir/$cgi.conf") ||
  84. error("write $estdir/$cgi.conf: $!");
  85. my $template=template("estseek.conf");
  86. eval q{use Cwd 'abs_path'};
  87. $template->param(
  88. index => $estdir,
  89. tmplfile => "$estdir/$cgi.tmpl",
  90. destdir => abs_path($config{destdir}),
  91. url => $config{url},
  92. );
  93. print TEMPLATE $template->output;
  94. close TEMPLATE;
  95. $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
  96. unlink($cgi);
  97. my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
  98. symlink($estseek, $cgi) ||
  99. error("symlink $estseek $cgi: $!");
  100. } # }}}
  101. sub estcmd ($;@) { #{{{
  102. my @params=split(' ', shift);
  103. push @params, "-cl", "$config{wikistatedir}/hyperestraier";
  104. if (@_) {
  105. push @params, "-";
  106. }
  107. my $pid=open(CHILD, "|-");
  108. if ($pid) {
  109. # parent
  110. foreach (@_) {
  111. print CHILD "$_\n";
  112. }
  113. close(CHILD) || error("estcmd @params exited nonzero: $?");
  114. }
  115. else {
  116. # child
  117. open(STDOUT, "/dev/null"); # shut it up (closing won't work)
  118. exec("estcmd", @params) || error("can't run estcmd");
  119. }
  120. } #}}}
  121. 1