summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/getsource.pm
blob: d1555430e82d83bbe61064d04b335cdf48a85317 (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. return unless defined $cgi->param('do') &&
  38. $cgi->param("do") eq "getsource";
  39. IkiWiki::decode_cgi_utf8($cgi);
  40. my $page=$cgi->param('page');
  41. if (! defined $page || $page !~ /$config{wiki_file_regexp}/) {
  42. error("invalid page parameter");
  43. }
  44. # For %pagesources.
  45. IkiWiki::loadindex();
  46. if (! exists $pagesources{$page}) {
  47. IkiWiki::cgi_custom_failure(
  48. $cgi,
  49. "404 Not Found",
  50. IkiWiki::misctemplate(gettext("missing page"),
  51. "<p>".
  52. sprintf(gettext("The page %s does not exist."),
  53. htmllink("", "", $page)).
  54. "</p>"));
  55. exit;
  56. }
  57. if (! defined pagetype($pagesources{$page})) {
  58. IkiWiki::cgi_custom_failure(
  59. $cgi->header(-status => "403 Forbidden"),
  60. IkiWiki::misctemplate(gettext("not a page"),
  61. "<p>".
  62. sprintf(gettext("%s is an attachment, not a page."),
  63. htmllink("", "", $page)).
  64. "</p>"));
  65. exit;
  66. }
  67. if (! $config{getsource_mimetype}) {
  68. $config{getsource_mimetype} = "text/plain; charset=utf-8";
  69. }
  70. print "Content-Type: $config{getsource_mimetype}\r\n";
  71. print ("\r\n");
  72. print readfile(srcfile($pagesources{$page}));
  73. exit 0;
  74. }
  75. 1