<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::getsource;

use warnings;
use strict;
use IkiWiki;
use open qw{:utf8 :std};

sub import {
	hook(type =&gt; "getsetup", id =&gt; "getsource", call =&gt; \&amp;getsetup);
	hook(type =&gt; "pagetemplate", id =&gt; "getsource", call =&gt; \&amp;pagetemplate);
	hook(type =&gt; "cgi", id =&gt; "getsource", call =&gt; \&amp;cgi_getsource);
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
		getsource_mimetype =&gt; {
			type =&gt; "string",
			example =&gt; "text/plain; charset=utf-8",
			description =&gt; "Mime type for returned source.",
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
}

sub pagetemplate (@) {
	my %params=@_;

	my $page=$params{page};
	my $template=$params{template};

	if (length $config{cgiurl}) {
		$template-&gt;param(getsourceurl =&gt; IkiWiki::cgiurl(do =&gt; "getsource", page =&gt; $page));
		$template-&gt;param(have_actions =&gt; 1);
	}
}

sub cgi_getsource ($) {
	my $cgi=shift;

	return unless defined $cgi-&gt;param('do') &amp;&amp;
	              $cgi-&gt;param("do") eq "getsource";

	IkiWiki::decode_cgi_utf8($cgi);

	my $page=$cgi-&gt;param('page');

	if (! defined $page || $page !~ /$config{wiki_file_regexp}/) {
		error("invalid page parameter");
	}

	# For %pagesources.
	IkiWiki::loadindex();

	if (! exists $pagesources{$page}) {
		IkiWiki::cgi_custom_failure(
			$cgi,
			"404 Not Found",
			IkiWiki::misctemplate(gettext("missing page"),
				"&lt;p&gt;".
				sprintf(gettext("The page %s does not exist."),
					htmllink("", "", $page)).
				"&lt;/p&gt;"));
		exit;
	}

	if (! defined pagetype($pagesources{$page})) {
		IkiWiki::cgi_custom_failure(
			$cgi-&gt;header(-status =&gt; "403 Forbidden"),
			IkiWiki::misctemplate(gettext("not a page"),
				"&lt;p&gt;".
				sprintf(gettext("%s is an attachment, not a page."),
					htmllink("", "", $page)).
				"&lt;/p&gt;"));
		exit;
	}

	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}));

	exit 0;
}

1
</pre></body></html>