]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/getsource.pm
6a208f1e735d19791da4c5a0821fc124e9bee3a7
[git.ikiwiki.info.git] / IkiWiki / Plugin / getsource.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::getsource;
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use open qw{:utf8 :std};
9 sub import {
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);
13 }
15 sub getsetup () {
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => 1,
20                 },
21                 getsource_mimetype => {
22                         type => "string",
23                         example => "text/plain; charset=utf-8",
24                         description => "Mime type for returned source.",
25                         safe => 1,
26                         rebuild => 0,
27                 },
28 }
30 sub pagetemplate (@) {
31         my %params=@_;
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);
39         }
40 }
42 sub cgi_getsource ($) {
43         my $cgi=shift;
45         # Note: we use sessioncgi rather than just cgi
46         # because we need $IkiWiki::pagesources{} to be
47         # populated.
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');
56         IkiWiki::loadindex();
58         if (! exists $IkiWiki::pagesources{$page}) {
59                 IkiWiki::cgi_custom_failure(
60                         $cgi->header(-status => "404 Not Found"),
61                         IkiWiki::misctemplate(gettext("missing page"),
62                                 "<p>".
63                                 sprintf(gettext("The page %s does not exist."),
64                                         htmllink("", "", $page)).
65                                 "</p>"));
66                 exit;
67         }
69         my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page}));
71         if (! $config{getsource_mimetype}) {
72                 $config{getsource_mimetype} = "text/plain; charset=utf-8";
73         }
75         print "Content-Type: $config{getsource_mimetype}\r\n";
77         print ("\r\n");
79         print $data;
80         
81         exit 0;
82 }
84 1