package TWiki::Contrib::Publish;
#
# Publish site (generate static HTML)
#
# Based on GenHTMLPlugin
# Copyright (C) 2001 Motorola
#
# Revisions Copyright (C) 2002, Eric Scouten
# Cairo updates Copyright (C) 2004 Crawford Currie http://c-dot.co.uk
#
# TWiki WikiClone (see TWiki.pm for $wikiversion and other info)
#
# Copyright (C) 2001 Peter Thoeny, Peter@Thoeny.com
# Copyright (C) 2001 Sven Dowideit, svenud@ozemail.com.au
# Copyright (C) 2001 Motorola Ltd.
# Copyright (C) 2005 Crawford Currie, http://c-dot.co.uk
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
#
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use File::Copy;
use File::Path;
use lib ('.');
use lib ('../lib');
use TWiki;
use TWiki::Func;
use strict;
use vars qw( $ZipPubUrl $VERSION $PUBLISH_DIR $PUBLISH_URL );
BEGIN {
require "TWiki/Contrib/PublishAddOn/PublishAddOn.cfg";
die "Could not configure PublishAddOn: $!" if $!;
}
$VERSION = 1.301;
# Main rendering loop.
sub main {
# Read command-line arguments and make
# Fill in default environment variables if invoked from command-line.
$ENV{HTTP_USER_AGENT} = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" unless(exists $ENV{HTTP_USER_AGENT});
$ENV{REMOTE_ADDR} = "127.0.0.1" unless(exists $ENV{REMOTE_ADDR});
$ENV{REMOTE_PORT} = "2509" unless(exists $ENV{REMOTE_PORT});
$ENV{REMOTE_USER} = "TWikiGuest" unless(exists $ENV{REMOTE_USER});
$ENV{REQUEST_METHOD} = "GET" unless(exists $ENV{REQUEST_METHOD});
$ENV{QUERY_STRING} = "" unless(exists $ENV{QUERY_STRING});
# Tweak environment variables depending on command-line arguments.
foreach my $arg (@ARGV) {
if ($arg =~ /^\-/) {
if ($arg eq "-y") {
if ($ENV{QUERY_STRING}) {
$ENV{QUERY_STRING} .= "&goAhead=yes";
} else {
$ENV{QUERY_STRING} = "goAhead=yes";
}
} else {
die "Unknown command-line option $arg\n";
}
} else {
$ENV{PATH_INFO} = "/$arg" unless(exists $ENV{PATH_INFO});
}
}
# Now do normal CGI init.
my $query = new CGI;
$| = 0;
my $pi = $query->path_info();
if( $query->param('web')) {
$pi = '/'.$query->param('web');
}
my ($topic, $web, $scriptUrlPath, $userName, $dataDir) =
TWiki::initialize($pi, $query->remote_user(), '',
$query->url(), $query);
my ($inclusions, $exclusions, $topicsearch, $skin);
my $notify="off";
my $wikiName = TWiki::Func::userToWikiName($userName);
# Load defaults from a config topic if one was specified
my $configtopic = $query->param('configtopic');
if ( $configtopic ) {
unless( TWiki::Func::topicExists($web, $configtopic) ) {
die "Specified configuration topic does not exist in $web!\n";
}
my $text = TWiki::Func::readTopicText($web, $configtopic);
unless( TWiki::Func::checkAccessPermission( "VIEW", $wikiName,
$text, $configtopic,
$web)) {
die "Access to $configtopic denied";
}
$text =~ s/\r//g;
while ( $text =~ s/^\s+\*\s+Set\s+([A-Z]+)\s*=(.*?)$//m ) {
my $k = $1;
my $v = $2;
$v =~ s/^\s*(.*?)\s*$/$1/go;
if ( $k eq "INCLUSIONS" ) {
$inclusions = $v;
} elsif ( $k eq "EXCLUSIONS" ) {
$exclusions = $v;
} elsif ( $k eq "TOPICSEARCH" ) {
$topicsearch = $v;
} elsif ( $k eq "PUBLISHSKIN" ) {
$skin = $v;
} elsif ( $k eq "SKIN" ) {
$skin = $v;
}
}
} else {
if ( defined($query->param('notify')) ) {
$notify = $query->param('notify');
}
if ( defined($query->param('inclusions')) ) {
$inclusions = $query->param('inclusions');
} else {
$inclusions = '*';
}
$exclusions = $query->param('exclusions') || '';
$topicsearch = $query->param('topicsearch') || '';
}
# convert wildcard pattern to RE
$inclusions =~ s/([*?])/.$1/g;
$inclusions =~ s/,/|/g;
$exclusions =~ s/([*?])/.$1/g;
$exclusions =~ s/,/|/g;
$skin ||= $query->param('publishskin') ||
TWiki::Func::getPreferencesValue("PUBLISHSKIN");
my $ok = 1;
if ( ! -d $PUBLISH_DIR && ! -e $PUBLISH_DIR) {
mkdir($PUBLISH_DIR, 0777);
$ok = !($!);
}
die "Can't publish because no useable publish directory was found. Please notify your TWiki administrator" unless -d $PUBLISH_DIR;
die "Can't publish because publish URL was not set. Please notify your TWiki administrator" unless $PUBLISH_URL;
my $tmp = TWiki::Func::formatTime(time());
$tmp =~s/^(\d+)\s+(\w+)\s+(\d+).*/$1_$2_$3/g;
my $zipfilename = $wikiName . "_" . $web . "_" . $tmp .".zip";
# Has user selected topic(s) yet?
my $goAhead = $query->param('goAhead');
if (!$goAhead) {
# redirect to the "publish" topic
my $url = TWiki::Func::getScriptUrl('TWiki', 'PublishAddOn', "view");
$url .= '?publishweb='.$web.'&publishtopic='.$topic;
TWiki::Func::redirectCgiQuery($query, $url);
} else {
TWiki::Func::writeHeader($query);
my $tmpl = TWiki::Store::readTemplate("view", "print.pattern");
$tmpl =~ s/%META{.*?}%//g;
my($header, $footer) = split(/%TEXT%/, $tmpl);
$header = TWiki::Func::expandCommonVariables( $header, $topic, $web );
$header = TWiki::Func::renderText( $header, $web );
print $header;
print "PUBLISH_DIR: $PUBLISH_DIR
\n";
print "PUBLISH_URL_PATH: $PUBLISH_URL
\n";
print "Config: $configtopic
\n" if $configtopic;
print "Skin: $skin
\n";
print "Inclusions: $inclusions
\n";
print "Exclusions: $exclusions
\n";
print "Content Filter: $topicsearch