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