962e099a6b47fafb47b59e2918e4f8a340e4d090
[ikiwiki.git] / 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 > The use of sessioncgi here seems undesirable: on wikis where anonymity is
8 > not allowed, you'll be asked to log in. Couldn't you achieve the same thing
9 > by loading the index with IkiWiki::loadindex, like [[plugins/goto]] does?
10 > --[[smcv]]
11
12 [[!template id=gitbranch branch=smcv/getsource
13   author="[[Will]]/[[smcv]]"]]
14
15 >> I've applied the patch below in a git branch, fixed my earlier criticism,
16 >> and also fixed a couple of other issues I noticed:
17 >>
18 >> * missing pages could be presented better as a real 404 page
19 >> * the default Content-type should probably be UTF-8 since the rest of
20 >>   IkiWiki tends to assume that
21 >> * emitting attachments (images, etc.) as text/plain isn't going to work :-)
22 >>
23 >> Any opinions on my branch? I think it's ready for merge, if Joey approves.
24 >>
25 >> --[[smcv]]
26
27 ----
28
29     diff --git a/templates/page.tmpl b/templates/page.tmpl
30     index f2f9c34..3176bed 100644
31     --- a/templates/page.tmpl
32     +++ b/templates/page.tmpl
33     @@ -46,6 +46,9 @@
34      <TMPL_IF NAME="HISTORYURL">
35      <li><a href="<TMPL_VAR HISTORYURL>">History</a></li>
36      </TMPL_IF>
37     +<TMPL_IF NAME="GETSOURCEURL">
38     +<li><a href="<TMPL_VAR GETSOURCEURL>">Get Source</a></li>
39     +</TMPL_IF>
40      <TMPL_IF NAME="PREFSURL">
41      <li><a href="<TMPL_VAR PREFSURL>">Preferences</a></li>
42      </TMPL_IF>
43
44 ----
45
46     #!/usr/bin/perl
47     package IkiWiki::Plugin::getsource;
48     
49     use warnings;
50     use strict;
51     use IkiWiki;
52     use open qw{:utf8 :std};
53     
54     sub import {
55         hook(type => "getsetup", id => "getsource", call => \&getsetup);
56         hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate);
57         hook(type => "sessioncgi", id => "getsource", call => \&cgi_getsource);
58     }
59     
60     sub getsetup () {
61         return
62                 plugin => {
63                         safe => 1,
64                         rebuild => 1,
65                 },
66                 getsource_mimetype => {
67                         type => "string",
68                         example => "application/octet-stream",
69                         description => "Mime type for returned source.",
70                         safe => 1,
71                         rebuild => 0,
72                 },
73     }
74     
75     sub pagetemplate (@) {
76         my %params=@_;
77     
78         my $page=$params{page};
79         my $template=$params{template};
80     
81         if (length $config{cgiurl}) {
82                 $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page));
83                 $template->param(have_actions => 1);
84         }
85     }
86     
87     sub cgi_getsource ($$) {
88         my $cgi=shift;
89         my $session=shift;
90     
91         # Note: we use sessioncgi rather than just cgi
92         # because we need $IkiWiki::pagesources{} to be
93         # populated.
94         
95         return unless (defined $cgi->param('do') &&
96                                         $cgi->param("do") eq "getsource");
97     
98         IkiWiki::decode_cgi_utf8($cgi);
99     
100         my $page=$cgi->param('page');
101     
102         if ($IkiWiki::pagesources{$page}) {
103                 
104                 my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page}));
105                 
106                 if (! $config{getsource_mimetype}) {
107                         $config{getsource_mimetype} = "text/plain";
108                 }
109                 
110                 print "Content-Type: $config{getsource_mimetype}\r\n";
111                 
112                 print ("\r\n");
113                 
114                 print $data;
115                 
116                 exit 0;
117         }
118         
119         error("Unable to find page source for page: $page");
120     
121         exit 0;
122     }
123     
124     1