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