]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/getsource.pm
Add getsource plugin
[ikiwiki.git] / IkiWiki / Plugin / getsource.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::getsource;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use open qw{:utf8 :std};
8
9 sub import {
10         hook(type => "getsetup", id => "getsource", call => \&getsetup);
11         hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate);
12         hook(type => "sessioncgi", id => "getsource", call => \&cgi_getsource);
13 }
14
15 sub getsetup () {
16         return
17                 plugin => {
18                         safe => 1,
19                         rebuild => 1,
20                 },
21                 getsource_mimetype => {
22                         type => "string",
23                         example => "application/octet-stream",
24                         description => "Mime type for returned source.",
25                         safe => 1,
26                         rebuild => 0,
27                 },
28 }
29
30 sub pagetemplate (@) {
31         my %params=@_;
32
33         my $page=$params{page};
34         my $template=$params{template};
35
36         if (length $config{cgiurl}) {
37                 $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page));
38                 $template->param(have_actions => 1);
39         }
40 }
41
42 sub cgi_getsource ($$) {
43         my $cgi=shift;
44         my $session=shift;
45
46         # Note: we use sessioncgi rather than just cgi
47         # because we need $IkiWiki::pagesources{} to be
48         # populated.
49
50         return unless (defined $cgi->param('do') &&
51                                         $cgi->param("do") eq "getsource");
52
53         IkiWiki::decode_cgi_utf8($cgi);
54
55         my $page=$cgi->param('page');
56
57         if ($IkiWiki::pagesources{$page}) {
58                 
59                 my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page}));
60                 
61                 if (! $config{getsource_mimetype}) {
62                         $config{getsource_mimetype} = "text/plain";
63                 }
64                 
65                 print "Content-Type: $config{getsource_mimetype}\r\n";
66                 
67                 print ("\r\n");
68                 
69                 print $data;
70                 
71                 exit 0;
72         }
73         
74         error("Unable to find page source for page: $page");
75
76         exit 0;
77 }
78
79 1