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