finish merging getsource
[ikiwiki] / doc / todo / source_link.mdwn
1 How about a direct link from the page header to the source of the latest version, to avoid the need to either use edit or navigate to the current version via the history link?
2
3  I'd like this too (and might try to implement it). -- [[users/jon]]
4
5 I just implemented this.  There is one [[patch]] to the default page template, and a new plugin.  -- [[Will]]
6
7 All of this code is licensed under the GPLv2+. -- [[Will]]
8
9 > The use of sessioncgi here seems undesirable: on wikis where anonymity is
10 > not allowed, you'll be asked to log in. Couldn't you achieve the same thing
11 > by loading the index with IkiWiki::loadindex, like [[plugins/goto]] does?
12 > --[[smcv]]
13
14 [[!template id=gitbranch branch=smcv/ready/getsource
15   author="[[Will]]/[[smcv]]"]]
16 [[done]]
17
18 >> I've applied the patch below in a git branch, fixed my earlier criticism,
19 >> and also fixed a couple of other issues I noticed:
20 >>
21 >> * missing pages could be presented better as a real 404 page
22 >> * the default Content-type should probably be UTF-8 since the rest of
23 >>   IkiWiki tends to assume that
24 >> * emitting attachments (images, etc.) as text/plain isn't going to work :-)
25 >>
26 >> Any opinions on my branch? I think it's ready for merge, if Joey approves.
27 >>
28 >> --[[smcv]]
29
30 >>> I need a copyright&license statement, so debian/copyright can be updated for
31 >>> the plugin, before I can merge this. Otherwise ready. --[[Joey]]
32
33 >>> That looks like a nice set of fixes.  One more that might be worthwhile: instead of reading the page source into a var, and then writing it out later, it might be nice to just
34 >>>  `print readfile(srcfile(pagesources{$page}));` at the appropriate point. -- [[Will]]
35
36 >>>> OK, I've committed that. --[[smcv]]
37
38 ----
39
40     diff --git a/templates/page.tmpl b/templates/page.tmpl
41     index f2f9c34..3176bed 100644
42     --- a/templates/page.tmpl
43     +++ b/templates/page.tmpl
44     @@ -46,6 +46,9 @@
45      <TMPL_IF NAME="HISTORYURL">
46      <li><a href="<TMPL_VAR HISTORYURL>">History</a></li>
47      </TMPL_IF>
48     +<TMPL_IF NAME="GETSOURCEURL">
49     +<li><a href="<TMPL_VAR GETSOURCEURL>">Get Source</a></li>
50     +</TMPL_IF>
51      <TMPL_IF NAME="PREFSURL">
52      <li><a href="<TMPL_VAR PREFSURL>">Preferences</a></li>
53      </TMPL_IF>
54
55 ----
56
57     #!/usr/bin/perl
58     package IkiWiki::Plugin::getsource;
59     
60     use warnings;
61     use strict;
62     use IkiWiki;
63     use open qw{:utf8 :std};
64     
65     sub import {
66         hook(type => "getsetup", id => "getsource", call => \&getsetup);
67         hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate);
68         hook(type => "sessioncgi", id => "getsource", call => \&cgi_getsource);
69     }
70     
71     sub getsetup () {
72         return
73                 plugin => {
74                         safe => 1,
75                         rebuild => 1,
76                 },
77                 getsource_mimetype => {
78                         type => "string",
79                         example => "application/octet-stream",
80                         description => "Mime type for returned source.",
81                         safe => 1,
82                         rebuild => 0,
83                 },
84     }
85     
86     sub pagetemplate (@) {
87         my %params=@_;
88     
89         my $page=$params{page};
90         my $template=$params{template};
91     
92         if (length $config{cgiurl}) {
93                 $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page));
94                 $template->param(have_actions => 1);
95         }
96     }
97     
98     sub cgi_getsource ($$) {
99         my $cgi=shift;
100         my $session=shift;
101     
102         # Note: we use sessioncgi rather than just cgi
103         # because we need $IkiWiki::pagesources{} to be
104         # populated.
105         
106         return unless (defined $cgi->param('do') &&
107                                         $cgi->param("do") eq "getsource");
108     
109         IkiWiki::decode_cgi_utf8($cgi);
110     
111         my $page=$cgi->param('page');
112     
113         if ($IkiWiki::pagesources{$page}) {
114                 
115                 my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page}));
116                 
117                 if (! $config{getsource_mimetype}) {
118                         $config{getsource_mimetype} = "text/plain";
119                 }
120                 
121                 print "Content-Type: $config{getsource_mimetype}\r\n";
122                 
123                 print ("\r\n");
124                 
125                 print $data;
126                 
127                 exit 0;
128         }
129         
130         error("Unable to find page source for page: $page");
131     
132         exit 0;
133     }
134     
135     1
136
137 [[done]] --[[smcv]]