From 01e4cb1464d07afb22d22bf98116a3ed9126612b Mon Sep 17 00:00:00 2001 From: Will Uther Date: Sun, 26 Jul 2009 16:22:56 +0100 Subject: Add getsource plugin --- IkiWiki/Plugin/getsource.pm | 79 +++++++++++++++++++++++++++++++++++++++++++++ templates/page.tmpl | 3 ++ 2 files changed, 82 insertions(+) create mode 100644 IkiWiki/Plugin/getsource.pm diff --git a/IkiWiki/Plugin/getsource.pm b/IkiWiki/Plugin/getsource.pm new file mode 100644 index 000000000..4e74eaea0 --- /dev/null +++ b/IkiWiki/Plugin/getsource.pm @@ -0,0 +1,79 @@ +#!/usr/bin/perl +package IkiWiki::Plugin::getsource; + +use warnings; +use strict; +use IkiWiki; +use open qw{:utf8 :std}; + +sub import { + hook(type => "getsetup", id => "getsource", call => \&getsetup); + hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate); + hook(type => "sessioncgi", id => "getsource", call => \&cgi_getsource); +} + +sub getsetup () { + return + plugin => { + safe => 1, + rebuild => 1, + }, + getsource_mimetype => { + type => "string", + example => "application/octet-stream", + description => "Mime type for returned source.", + safe => 1, + rebuild => 0, + }, +} + +sub pagetemplate (@) { + my %params=@_; + + my $page=$params{page}; + my $template=$params{template}; + + if (length $config{cgiurl}) { + $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page)); + $template->param(have_actions => 1); + } +} + +sub cgi_getsource ($$) { + my $cgi=shift; + my $session=shift; + + # Note: we use sessioncgi rather than just cgi + # because we need $IkiWiki::pagesources{} to be + # populated. + + return unless (defined $cgi->param('do') && + $cgi->param("do") eq "getsource"); + + IkiWiki::decode_cgi_utf8($cgi); + + my $page=$cgi->param('page'); + + if ($IkiWiki::pagesources{$page}) { + + my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page})); + + if (! $config{getsource_mimetype}) { + $config{getsource_mimetype} = "text/plain"; + } + + print "Content-Type: $config{getsource_mimetype}\r\n"; + + print ("\r\n"); + + print $data; + + exit 0; + } + + error("Unable to find page source for page: $page"); + + exit 0; +} + +1 diff --git a/templates/page.tmpl b/templates/page.tmpl index 8622d1a01..599758cc7 100644 --- a/templates/page.tmpl +++ b/templates/page.tmpl @@ -50,6 +50,9 @@
  • History
  • + +
  • Get Source
  • +
  • Preferences
  • -- cgit v1.2.3 From eaf59e5ba940b883814c19ae64e68dea4530d992 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sun, 26 Jul 2009 16:33:12 +0100 Subject: getsource: run as plain CGI, rather than sessioncgi As I suggested when reviewing Will's code, calling loadindex() should be sufficient. --- IkiWiki/Plugin/getsource.pm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/IkiWiki/Plugin/getsource.pm b/IkiWiki/Plugin/getsource.pm index 4e74eaea0..2e65df950 100644 --- a/IkiWiki/Plugin/getsource.pm +++ b/IkiWiki/Plugin/getsource.pm @@ -9,7 +9,7 @@ use open qw{:utf8 :std}; sub import { hook(type => "getsetup", id => "getsource", call => \&getsetup); hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate); - hook(type => "sessioncgi", id => "getsource", call => \&cgi_getsource); + hook(type => "cgi", id => "getsource", call => \&cgi_getsource); } sub getsetup () { @@ -39,9 +39,8 @@ sub pagetemplate (@) { } } -sub cgi_getsource ($$) { +sub cgi_getsource ($) { my $cgi=shift; - my $session=shift; # Note: we use sessioncgi rather than just cgi # because we need $IkiWiki::pagesources{} to be @@ -54,6 +53,8 @@ sub cgi_getsource ($$) { my $page=$cgi->param('page'); + IkiWiki::loadindex(); + if ($IkiWiki::pagesources{$page}) { my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page})); -- cgit v1.2.3 From 3f520da78aeda38e47b43b28023b52f321f71291 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sun, 26 Jul 2009 16:36:17 +0100 Subject: getsource: default to saying page source is in UTF-8, and make the example match the default IkiWiki mostly assumes that pages are in UTF-8; anyone this doesn't work for can override it in the setup file. --- IkiWiki/Plugin/getsource.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/IkiWiki/Plugin/getsource.pm b/IkiWiki/Plugin/getsource.pm index 2e65df950..08d9d110c 100644 --- a/IkiWiki/Plugin/getsource.pm +++ b/IkiWiki/Plugin/getsource.pm @@ -20,7 +20,7 @@ sub getsetup () { }, getsource_mimetype => { type => "string", - example => "application/octet-stream", + example => "text/plain; charset=utf-8", description => "Mime type for returned source.", safe => 1, rebuild => 0, @@ -60,7 +60,7 @@ sub cgi_getsource ($) { my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page})); if (! $config{getsource_mimetype}) { - $config{getsource_mimetype} = "text/plain"; + $config{getsource_mimetype} = "text/plain; charset=utf-8"; } print "Content-Type: $config{getsource_mimetype}\r\n"; -- cgit v1.2.3 From 0afcec734622811b8910d3df5d102df58d429a51 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sun, 26 Jul 2009 16:45:01 +0100 Subject: getsource: turn missing pages into a 404 Also restructure so we return early on missing pages. --- IkiWiki/Plugin/getsource.pm | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/IkiWiki/Plugin/getsource.pm b/IkiWiki/Plugin/getsource.pm index 08d9d110c..6a208f1e7 100644 --- a/IkiWiki/Plugin/getsource.pm +++ b/IkiWiki/Plugin/getsource.pm @@ -55,25 +55,29 @@ sub cgi_getsource ($) { IkiWiki::loadindex(); - if ($IkiWiki::pagesources{$page}) { - - my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page})); - - if (! $config{getsource_mimetype}) { - $config{getsource_mimetype} = "text/plain; charset=utf-8"; - } - - print "Content-Type: $config{getsource_mimetype}\r\n"; - - print ("\r\n"); - - print $data; - - exit 0; + if (! exists $IkiWiki::pagesources{$page}) { + IkiWiki::cgi_custom_failure( + $cgi->header(-status => "404 Not Found"), + IkiWiki::misctemplate(gettext("missing page"), + "

    ". + sprintf(gettext("The page %s does not exist."), + htmllink("", "", $page)). + "

    ")); + exit; + } + + my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page})); + + if (! $config{getsource_mimetype}) { + $config{getsource_mimetype} = "text/plain; charset=utf-8"; } - - error("Unable to find page source for page: $page"); + print "Content-Type: $config{getsource_mimetype}\r\n"; + + print ("\r\n"); + + print $data; + exit 0; } -- cgit v1.2.3 From ea244ab7b53afbd710dab267ca9a8fb9f17cfb00 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sun, 26 Jul 2009 16:48:25 +0100 Subject: getsource: don't allow getting the source of an attachment Serving up images etc. as text/plain; charset=utf-8 is unlikely to work very well, and there's no point in having this CGI action for attachments (since they're copied into the output as-is anyway). --- IkiWiki/Plugin/getsource.pm | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/IkiWiki/Plugin/getsource.pm b/IkiWiki/Plugin/getsource.pm index 6a208f1e7..1b7eb56c6 100644 --- a/IkiWiki/Plugin/getsource.pm +++ b/IkiWiki/Plugin/getsource.pm @@ -66,6 +66,17 @@ sub cgi_getsource ($) { exit; } + if (! defined pagetype($IkiWiki::pagesources{$page})) { + IkiWiki::cgi_custom_failure( + $cgi->header(-status => "403 Forbidden"), + IkiWiki::misctemplate(gettext("not a page"), + "

    ". + sprintf(gettext("%s is an attachment, not a page."), + htmllink("", "", $page)). + "

    ")); + exit; + } + my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page})); if (! $config{getsource_mimetype}) { -- cgit v1.2.3 From b1f31ab7cbc87a572886673e7809d5e2fc5ee491 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sun, 26 Jul 2009 16:49:48 +0100 Subject: getsource: in the default template, just say "Source" All the other actions are single words (apart from RecentChanges), and are nouns (apart from Edit); saying "Source" is consistent with "History", for instance. --- templates/page.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/page.tmpl b/templates/page.tmpl index 599758cc7..653179e5d 100644 --- a/templates/page.tmpl +++ b/templates/page.tmpl @@ -51,7 +51,7 @@
  • History
  • -
  • Get Source
  • +
  • Source
  • Preferences
  • -- cgit v1.2.3 From 2ef53b128d9f40fa998215b2809e676207050902 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sun, 26 Jul 2009 16:59:26 +0100 Subject: getsource: remove unnecessary IkiWiki:: prefixes Many variables and functions are exported. --- IkiWiki/Plugin/getsource.pm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/IkiWiki/Plugin/getsource.pm b/IkiWiki/Plugin/getsource.pm index 1b7eb56c6..db5614ec1 100644 --- a/IkiWiki/Plugin/getsource.pm +++ b/IkiWiki/Plugin/getsource.pm @@ -43,7 +43,7 @@ sub cgi_getsource ($) { my $cgi=shift; # Note: we use sessioncgi rather than just cgi - # because we need $IkiWiki::pagesources{} to be + # because we need %pagesources to be # populated. return unless (defined $cgi->param('do') && @@ -55,7 +55,7 @@ sub cgi_getsource ($) { IkiWiki::loadindex(); - if (! exists $IkiWiki::pagesources{$page}) { + if (! exists $pagesources{$page}) { IkiWiki::cgi_custom_failure( $cgi->header(-status => "404 Not Found"), IkiWiki::misctemplate(gettext("missing page"), @@ -66,7 +66,7 @@ sub cgi_getsource ($) { exit; } - if (! defined pagetype($IkiWiki::pagesources{$page})) { + if (! defined pagetype($pagesources{$page})) { IkiWiki::cgi_custom_failure( $cgi->header(-status => "403 Forbidden"), IkiWiki::misctemplate(gettext("not a page"), @@ -77,7 +77,7 @@ sub cgi_getsource ($) { exit; } - my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page})); + my $data = readfile(srcfile($pagesources{$page})); if (! $config{getsource_mimetype}) { $config{getsource_mimetype} = "text/plain; charset=utf-8"; -- cgit v1.2.3 From 70b1c2aabd0d591cbdb30765c5a7e000e993f343 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 27 Jul 2009 11:58:36 +0100 Subject: getsource: remove temporary variable --- IkiWiki/Plugin/getsource.pm | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/IkiWiki/Plugin/getsource.pm b/IkiWiki/Plugin/getsource.pm index db5614ec1..e8aea2c39 100644 --- a/IkiWiki/Plugin/getsource.pm +++ b/IkiWiki/Plugin/getsource.pm @@ -77,18 +77,14 @@ sub cgi_getsource ($) { exit; } - my $data = readfile(srcfile($pagesources{$page})); - if (! $config{getsource_mimetype}) { $config{getsource_mimetype} = "text/plain; charset=utf-8"; } print "Content-Type: $config{getsource_mimetype}\r\n"; - print ("\r\n"); + print readfile(srcfile($pagesources{$page})); - print $data; - exit 0; } -- cgit v1.2.3 From 3f39e69b13851d3bed8e1cae0525d6ad8ac768cf Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 1 Aug 2009 12:31:34 +0100 Subject: Document the getsource plugin --- doc/plugins/getsource.mdwn | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 doc/plugins/getsource.mdwn diff --git a/doc/plugins/getsource.mdwn b/doc/plugins/getsource.mdwn new file mode 100644 index 000000000..4fbf4be98 --- /dev/null +++ b/doc/plugins/getsource.mdwn @@ -0,0 +1,13 @@ +[[!template id=plugin name=getsource author="[[Will_Uther|Will]]"]] + +This plugin adds a `getsource` action to the IkiWiki CGI, and a "Source" link +that uses it to display pages' source. + +Configuration for this plugin in the setup file: + +* `getsource_mimetype => "text/plain; charset=utf-8"` + + Sets the MIME type used when page source is requested. The default is + usually appropriate, but you could set this to `application/octet-stream` + to encourage browsers to download the source to a file rather than showing + it in the browser. -- cgit v1.2.3 From f5322fa912250cb2859bb63aeae419a405f74544 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 1 Aug 2009 12:31:53 +0100 Subject: Mark todo/source_link as done --- debian/changelog | 1 + doc/todo/source_link.mdwn | 2 ++ 2 files changed, 3 insertions(+) diff --git a/debian/changelog b/debian/changelog index 565f19c7c..90ec2ddac 100644 --- a/debian/changelog +++ b/debian/changelog @@ -8,6 +8,7 @@ ikiwiki (3.15) UNRELEASED; urgency=low * Add further build machinery to generate translated underlays from the po file, for use by wikis whose primary language is not English. * Add Danish basewiki translation by Jonas Smedegaard. + * Add getsource plugin (Will, smcv) -- Joey Hess Tue, 02 Jun 2009 17:03:41 -0400 diff --git a/doc/todo/source_link.mdwn b/doc/todo/source_link.mdwn index 9d9ec9697..a7203d06c 100644 --- a/doc/todo/source_link.mdwn +++ b/doc/todo/source_link.mdwn @@ -107,3 +107,5 @@ I just implemented this. There is one [[patch]] to the default page template, a } 1 + +[[done]] --[[smcv]] -- cgit v1.2.3