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 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 IkiWiki/Plugin/getsource.pm (limited to 'IkiWiki/Plugin') 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 -- 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(-) (limited to 'IkiWiki/Plugin') 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(-) (limited to 'IkiWiki/Plugin') 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(-) (limited to 'IkiWiki/Plugin') 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(+) (limited to 'IkiWiki/Plugin') 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 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(-) (limited to 'IkiWiki/Plugin') 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(-) (limited to 'IkiWiki/Plugin') 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