summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/getsource.pm
blob: e8aea2c3901ba36c22f2991f0e4a4c2795a9a981 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::getsource;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use open qw{:utf8 :std};
  7. sub import {
  8. hook(type => "getsetup", id => "getsource", call => \&getsetup);
  9. hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate);
  10. hook(type => "cgi", id => "getsource", call => \&cgi_getsource);
  11. }
  12. sub getsetup () {
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => 1,
  17. },
  18. getsource_mimetype => {
  19. type => "string",
  20. example => "text/plain; charset=utf-8",
  21. description => "Mime type for returned source.",
  22. safe => 1,
  23. rebuild => 0,
  24. },
  25. }
  26. sub pagetemplate (@) {
  27. my %params=@_;
  28. my $page=$params{page};
  29. my $template=$params{template};
  30. if (length $config{cgiurl}) {
  31. $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page));
  32. $template->param(have_actions => 1);
  33. }
  34. }
  35. sub cgi_getsource ($) {
  36. my $cgi=shift;
  37. # Note: we use sessioncgi rather than just cgi
  38. # because we need %pagesources to be
  39. # populated.
  40. return unless (defined $cgi->param('do') &&
  41. $cgi->param("do") eq "getsource");
  42. IkiWiki::decode_cgi_utf8($cgi);
  43. my $page=$cgi->param('page');
  44. IkiWiki::loadindex();
  45. if (! exists $pagesources{$page}) {
  46. IkiWiki::cgi_custom_failure(
  47. $cgi->header(-status => "404 Not Found"),
  48. IkiWiki::misctemplate(gettext("missing page"),
  49. "<p>".
  50. sprintf(gettext("The page %s does not exist."),
  51. htmllink("", "", $page)).
  52. "</p>"));
  53. exit;
  54. }
  55. if (! defined pagetype($pagesources{$page})) {
  56. IkiWiki::cgi_custom_failure(
  57. $cgi->header(-status => "403 Forbidden"),
  58. IkiWiki::misctemplate(gettext("not a page"),
  59. "<p>".
  60. sprintf(gettext("%s is an attachment, not a page."),
  61. htmllink("", "", $page)).
  62. "</p>"));
  63. exit;
  64. }
  65. if (! $config{getsource_mimetype}) {
  66. $config{getsource_mimetype} = "text/plain; charset=utf-8";
  67. }
  68. print "Content-Type: $config{getsource_mimetype}\r\n";
  69. print ("\r\n");
  70. print readfile(srcfile($pagesources{$page}));
  71. exit 0;
  72. }
  73. 1