summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/getsource.pm
blob: ae9ea3cc7af4ffe27b46d65528d380420ac5b368 (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->header(-status => "404 Not Found"),
  49. IkiWiki::misctemplate(gettext("missing page"),
  50. "<p>".
  51. sprintf(gettext("The page %s does not exist."),
  52. htmllink("", "", $page)).
  53. "</p>"));
  54. exit;
  55. }
  56. if (! defined pagetype($pagesources{$page})) {
  57. IkiWiki::cgi_custom_failure(
  58. $cgi->header(-status => "403 Forbidden"),
  59. IkiWiki::misctemplate(gettext("not a page"),
  60. "<p>".
  61. sprintf(gettext("%s is an attachment, not a page."),
  62. htmllink("", "", $page)).
  63. "</p>"));
  64. exit;
  65. }
  66. if (! $config{getsource_mimetype}) {
  67. $config{getsource_mimetype} = "text/plain; charset=utf-8";
  68. }
  69. print "Content-Type: $config{getsource_mimetype}\r\n";
  70. print ("\r\n");
  71. print readfile(srcfile($pagesources{$page}));
  72. exit 0;
  73. }
  74. 1