Make sure deleted tag pages don't get recreated.
[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 => "cgi", 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 => "text/plain; charset=utf-8",
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
45         return unless defined $cgi->param('do') &&
46                       $cgi->param("do") eq "getsource";
47
48         IkiWiki::decode_cgi_utf8($cgi);
49
50         my $page=$cgi->param('page');
51
52         if (! defined $page || $page !~ /$config{wiki_file_regexp}/) {
53                 error("invalid page parameter");
54         }
55
56         # For %pagesources.
57         IkiWiki::loadindex();
58
59         if (! exists $pagesources{$page}) {
60                 IkiWiki::cgi_custom_failure(
61                         $cgi,
62                         "404 Not Found",
63                         IkiWiki::misctemplate(gettext("missing page"),
64                                 "<p>".
65                                 sprintf(gettext("The page %s does not exist."),
66                                         htmllink("", "", $page)).
67                                 "</p>"));
68                 exit;
69         }
70
71         if (! defined pagetype($pagesources{$page})) {
72                 IkiWiki::cgi_custom_failure(
73                         $cgi->header(-status => "403 Forbidden"),
74                         IkiWiki::misctemplate(gettext("not a page"),
75                                 "<p>".
76                                 sprintf(gettext("%s is an attachment, not a page."),
77                                         htmllink("", "", $page)).
78                                 "</p>"));
79                 exit;
80         }
81
82         if (! $config{getsource_mimetype}) {
83                 $config{getsource_mimetype} = "text/plain; charset=utf-8";
84         }
85
86         print "Content-Type: $config{getsource_mimetype}\r\n";
87         print ("\r\n");
88         print readfile(srcfile($pagesources{$page}));
89
90         exit 0;
91 }
92
93 1