summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/search.pm
blob: 3cfc301e2e7815c4ff36f181fa38d01f319afb58 (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("Must specify $required when using the search plugin\n");
  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("cleaning hyperestraier search index");
  51. estcmd("purge -cl");
  52. estcfg();
  53. } #}}}
  54. sub change (@) { #{{{
  55. debug("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')) {
  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. open(TEMPLATE, ">:utf8", "$estdir/$cgi.tmpl") ||
  80. error("write $estdir/$cgi.tmpl: $!");
  81. print TEMPLATE IkiWiki::misctemplate("search",
  82. "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
  83. baseurl => IkiWiki::dirname($config{cgiurl})."/");
  84. close TEMPLATE;
  85. open(TEMPLATE, ">$estdir/$cgi.conf") ||
  86. error("write $estdir/$cgi.conf: $!");
  87. my $template=template("estseek.conf");
  88. eval q{use Cwd 'abs_path'};
  89. $template->param(
  90. index => $estdir,
  91. tmplfile => "$estdir/$cgi.tmpl",
  92. destdir => abs_path($config{destdir}),
  93. url => $config{url},
  94. );
  95. print TEMPLATE $template->output;
  96. close TEMPLATE;
  97. $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
  98. unlink($cgi);
  99. my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
  100. symlink($estseek, $cgi) ||
  101. error("symlink $estseek $cgi: $!");
  102. } # }}}
  103. sub estcmd ($;@) { #{{{
  104. my @params=split(' ', shift);
  105. push @params, "-cl", "$config{wikistatedir}/hyperestraier";
  106. if (@_) {
  107. push @params, "-";
  108. }
  109. my $pid=open(CHILD, "|-");
  110. if ($pid) {
  111. # parent
  112. foreach (@_) {
  113. print CHILD "$_\n";
  114. }
  115. close(CHILD) || error("estcmd @params exited nonzero: $?");
  116. }
  117. else {
  118. # child
  119. open(STDOUT, "/dev/null"); # shut it up (closing won't work)
  120. exec("estcmd", @params) || error("can't run estcmd");
  121. }
  122. } #}}}
  123. 1