2 package IkiWiki::Plugin::getsource;
7 use open qw{:utf8 :std};
10 hook(type => "getsetup", id => "getsource", call => \&getsetup);
11 hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate);
12 hook(type => "cgi", id => "getsource", call => \&cgi_getsource);
21 getsource_mimetype => {
23 example => "text/plain; charset=utf-8",
24 description => "Mime type for returned source.",
30 sub pagetemplate (@) {
33 my $page=$params{page};
34 my $template=$params{template};
36 if (length $config{cgiurl}) {
37 $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page));
38 $template->param(have_actions => 1);
42 sub cgi_getsource ($) {
45 # Note: we use sessioncgi rather than just cgi
46 # because we need %pagesources to be
49 return unless (defined $cgi->param('do') &&
50 $cgi->param("do") eq "getsource");
52 IkiWiki::decode_cgi_utf8($cgi);
54 my $page=$cgi->param('page');
58 if (! exists $pagesources{$page}) {
59 IkiWiki::cgi_custom_failure(
60 $cgi->header(-status => "404 Not Found"),
61 IkiWiki::misctemplate(gettext("missing page"),
63 sprintf(gettext("The page %s does not exist."),
64 htmllink("", "", $page)).
69 if (! defined pagetype($pagesources{$page})) {
70 IkiWiki::cgi_custom_failure(
71 $cgi->header(-status => "403 Forbidden"),
72 IkiWiki::misctemplate(gettext("not a page"),
74 sprintf(gettext("%s is an attachment, not a page."),
75 htmllink("", "", $page)).
80 if (! $config{getsource_mimetype}) {
81 $config{getsource_mimetype} = "text/plain; charset=utf-8";
84 print "Content-Type: $config{getsource_mimetype}\r\n";
86 print readfile(srcfile($pagesources{$page}));