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