| 1 |
#!/usr/bin/perl |
| 2 |
# Special purpose plugin that causes a site to be parked. A parked site |
| 3 |
# cannot be branched, and has no accessible content. Remote VCS access is |
| 4 |
# disabled too. |
| 5 |
package IkiWiki::Plugin::parked; |
| 6 |
|
| 7 |
use warnings; |
| 8 |
use strict; |
| 9 |
use IkiWiki 3.00; |
| 10 |
use IkiWiki::Hosting; |
| 11 |
|
| 12 |
sub import { |
| 13 |
hook(type => "getsetup", id => 'parked', call => \&getsetup); |
| 14 |
hook(type => "refresh", id => 'parked', call => \&refresh); |
| 15 |
hook(type => "genwrapper", id => "parked", call => \&genwrapper, |
| 16 |
first => 1); |
| 17 |
hook(type => "disable", id => 'parked', call => \&disable); |
| 18 |
hook(type => "cgi", id => 'parked', call => \&cgi); |
| 19 |
|
| 20 |
# Cache both versions of the parked message. |
| 21 |
parked_message(); |
| 22 |
parked_message(1); |
| 23 |
} |
| 24 |
|
| 25 |
sub getsetup () { |
| 26 |
return |
| 27 |
plugin => { |
| 28 |
safe => 0, |
| 29 |
rebuild => 1, |
| 30 |
}, |
| 31 |
parked_message => { |
| 32 |
type => "string", |
| 33 |
description => "An optional message explaining why this site is parked.", |
| 34 |
safe => 0, |
| 35 |
rebuild => 1, |
| 36 |
}, |
| 37 |
} |
| 38 |
|
| 39 |
my %cached_message; |
| 40 |
sub parked_message ($) { |
| 41 |
my $html=shift() ? 1 : 0; |
| 42 |
|
| 43 |
if (! defined $cached_message{$html}) { |
| 44 |
my $template=IkiWiki::Hosting::ikisite_template("parkedsite.tmpl"); |
| 45 |
$template->param(html => $html); |
| 46 |
$template->param(wikiname => $config{wikiname}); |
| 47 |
$template->param(parked_message => $config{parked_message}) |
| 48 |
if defined $config{parked_message} && length $config{parked_message}; |
| 49 |
$cached_message{$html}=$template->output; |
| 50 |
} |
| 51 |
return $cached_message{$html}; |
| 52 |
} |
| 53 |
|
| 54 |
my $real_writefile; |
| 55 |
sub refresh { |
| 56 |
# Intercept file writes when building the site. |
| 57 |
if (! defined $real_writefile) { |
| 58 |
$real_writefile=\&IkiWiki::writefile; |
| 59 |
inject(name => "IkiWiki::writefile", call => \&writefile_parked); |
| 60 |
} |
| 61 |
|
| 62 |
# Avoid hardlinking raw files, it bypasses our writefile. |
| 63 |
$config{hardlink}=0; |
| 64 |
} |
| 65 |
|
| 66 |
sub genwrapper { |
| 67 |
# Write flag file to disable remote git push and pull. |
| 68 |
writefile("$ENV{HOME}/source.git/parked", "", parked_message(0)); |
| 69 |
|
| 70 |
# Set here so the branchable plugin will disable branchability |
| 71 |
# for parked sites, even when the setup has branchable enabled. |
| 72 |
# (For it to take effect, this hook is run first.) |
| 73 |
$config{branchable}=0; |
| 74 |
|
| 75 |
# C code to add to wrapper. |
| 76 |
return "" |
| 77 |
} |
| 78 |
|
| 79 |
sub disable { |
| 80 |
# Enable remote git push and pull. |
| 81 |
unlink("$ENV{HOME}/source.git/parked"); |
| 82 |
} |
| 83 |
|
| 84 |
# Replacing these files with a parked message could cause bad things to |
| 85 |
# happen, so don't. |
| 86 |
my @keyfiles=qw{robots.txt favicon.ico}; |
| 87 |
|
| 88 |
# This is a wrapper injected around ikiwiki's real writefile. |
| 89 |
# It causes the parked message to be output to files rendered |
| 90 |
# to the destdir. |
| 91 |
sub writefile_parked ($$$;$$) { |
| 92 |
my $file=shift; |
| 93 |
my $destdir=shift; |
| 94 |
my $content=shift; |
| 95 |
my $binary=shift; |
| 96 |
my $writer=shift; |
| 97 |
|
| 98 |
if ($destdir eq $config{destdir} && exists $destsources{$file} && |
| 99 |
! grep { $_ eq $file } @keyfiles) { |
| 100 |
my $html=($file=~/\.html$/); |
| 101 |
$real_writefile->($file, $destdir, parked_message($html)); |
| 102 |
} |
| 103 |
else { |
| 104 |
$real_writefile->($file, $destdir, $content, $binary, $writer); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
sub cgi ($) { |
| 109 |
my $cgi=shift; |
| 110 |
|
| 111 |
print "Content-type: text/html\n\n"; |
| 112 |
print parked_message(1); |
| 113 |
exit; |
| 114 |
} |
| 115 |
|
| 116 |
1; |