Skip to content

Commit

Permalink
Item13023: Tested on Foswiki 1.0.9 and 1.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
gac410 committed Sep 13, 2014
1 parent cca43f5 commit a95eec5
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 4 deletions.
6 changes: 5 additions & 1 deletion data/System/TestBootstrapPlugin.txt
Expand Up @@ -9,7 +9,7 @@

%TOC%

This plugin runs the new Bootstrap code developed for Foswiki 1.2. It
This plugin runs the new configuration Bootstrap code developed for Foswiki 1.2. It
generates a report describing the settings that this feature will generate.

The Bootstrap code allows foswiki to run in a minimal configuration without
Expand All @@ -21,6 +21,10 @@ variety of systems. This plugin has no settings or preferences and implements
a single BOOTSTRAP macro which reports what would have been auto configured
had this been a 1.2 system running without LocalSite.cfg

This has been tested on 1.0.9, 1.1.9 and 1.2 alpha. It uses the real 1.2 bootstrap
code on 1.2-alpha, and a local copy of the bootstrap code when running on
1.1.x.

Please send the following bootstrap report to the foswiki developers:
*This report is only available to administrators*

Expand Down
262 changes: 259 additions & 3 deletions lib/Foswiki/Plugins/TestBootstrapPlugin.pm
Expand Up @@ -31,6 +31,10 @@ our $NO_PREFS_IN_TOPIC = 1;

our %boot_cfg;

my @BOOTSTRAP =
qw( {DataDir} {DefaultUrlHost} {PubUrlPath} {ToolsDir} {WorkingDir}
{PubDir} {TemplateDir} {ScriptDir} {ScriptUrlPath} {ScriptUrlPaths}{view} {ScriptSuffix} {LocalesDir} );

sub initPlugin {
my ( $topic, $web, $user, $installWeb ) = @_;

Expand Down Expand Up @@ -115,17 +119,269 @@ sub _BOOTSTRAP {

sub _runBootstrap {
local %Foswiki::cfg = ( Engine => $Foswiki::cfg{Engine} );
my $msg = Foswiki::Configure::Load::bootstrapConfig(1);
my $msg;

if ( Foswiki::Configure::Load->can('bootstrapConfig') ) {
$msg = Foswiki::Configure::Load::bootstrapConfig(1);
}
else {
$msg = _bootstrapConfig(1);
}

#my %boot_cfg = %Foswiki::cfg;
return ( \%Foswiki::cfg, $msg );
}

=begin TML
---++ StaticMethod bootstrapConfig()
This routine is a copy of the bootstrapConfig() that is shipped with version 1.2
Foswiki::Configure::Load. It is included here so that this plugin can also be used on a 1.1 system.
When syncing over an updated version from Foswiki.pm:
* Delete the code that dies if ConfigurePlugin is not installed
* Also pull in _workOutOS if that has changed.
* Change the filename used to validate templates from configure.tmpl to foswiki.tmpl
=cut

sub _bootstrapConfig {
my $noload = shift;

# Failed to read LocalSite.cfg
# Clear out $Foswiki::cfg to allow variable expansion to work
# when reloading Foswiki.spec et al.
# SMELL: have to keep {Engine} as this is defined by the
# script (smells of a hack).
%Foswiki::cfg = ( Engine => $Foswiki::cfg{Engine} );

# Try to repair $Foswiki::cfg to a minimal configuration,
# using paths and URLs relative to this request. If URL
# rewriting is happening in the web server this is likely
# to go down in flames, but it gives us the best chance of
# recovering. We need to guess values for all the vars that
# woudl trigger "undefined" errors
eval "require FindBin";
die "Could not load FindBin to support configuration recovery: $@"
if $@;
FindBin::again(); # in case we are under mod_perl or similar
$FindBin::Bin =~ /^(.*)$/;
my $bin = $1;
$FindBin::Script =~ /^(.*)$/;
my $script = $1;
print STDERR
"AUTOCONFIG: Found Bin dir: $bin, Script name: $script using FindBin\n"
if (TRAUTO);

$Foswiki::cfg{ScriptSuffix} = ( fileparse( $script, qr/\.[^.]*/ ) )[2];
print STDERR
"AUTOCONFIG: Found SCRIPT SUFFIX $Foswiki::cfg{ScriptSuffix} \n"
if ( TRAUTO && $Foswiki::cfg{ScriptSuffix} );

my $protocol = $ENV{HTTPS} ? 'https' : 'http';
if ( $ENV{HTTP_HOST} ) {
$Foswiki::cfg{DefaultUrlHost} = "$protocol://$ENV{HTTP_HOST}";
print STDERR
"AUTOCONFIG: Set DefaultUrlHost $Foswiki::cfg{DefaultUrlHost} from HTTP_HOST $ENV{HTTP_HOST} \n"
if (TRAUTO);
}
elsif ( $ENV{SERVER_NAME} ) {
$Foswiki::cfg{DefaultUrlHost} = "$protocol://$ENV{SERVER_NAME}";
print STDERR
"AUTOCONFIG: Set DefaultUrlHost $Foswiki::cfg{DefaultUrlHost} from SERVER_NAME $ENV{SERVER_NAME} \n"
if (TRAUTO);
}
else {
# OK, so this is barfilicious. Think of something better.
$Foswiki::cfg{DefaultUrlHost} = "$protocol://localhost";
print STDERR
"AUTOCONFIG: barfilicious: Set DefaultUrlHost $Foswiki::cfg{DefaultUrlHost} \n"
if (TRAUTO);
}

# Examine the CGI path. The 'view' script it typically removed from the
# URL when using "Short URLs. If this BEGIN block is being run by
# 'view', then $Foswiki::cfg{ScriptUrlPaths}{view} will be correctly
# bootstrapped. If run for any other script, it will be set to a
# reasonable though probably incorrect default.
#
# In order to recover the correct view path when the script is 'configure',
# the ConfigurePlugin stashes the path to the view script into a session variable.
# and then recovers it. When the jsonrpc script is called to save the configuration
# it then has the VIEWPATH parameter available. If "view" was never called during
# configuration, then it will not be set correctly.
if ( $ENV{SCRIPT_NAME} ) {
print STDERR "AUTOCONFIG: Found SCRIPT $ENV{SCRIPT_NAME} \n"
if (TRAUTO);

if ( $ENV{SCRIPT_NAME} =~ m{^(.*?)/$script(\b|$)} ) {

# Conventional URLs with path and script
$Foswiki::cfg{ScriptUrlPath} = $1;
$Foswiki::cfg{ScriptUrlPaths}{view} =
$1 . '/view' . $Foswiki::cfg{ScriptSuffix};

# This might not work, depending on the websrver config,
# but it's the best we can do
$Foswiki::cfg{PubUrlPath} = "$1/../pub";
}
else {
# Short URLs but with a path
print STDERR "AUTOCONFIG: Found path, but no script. short URLs \n"
if (TRAUTO);
$Foswiki::cfg{ScriptUrlPath} = $ENV{SCRIPT_NAME} . '/bin';
$Foswiki::cfg{ScriptUrlPaths}{view} = $ENV{SCRIPT_NAME};
$Foswiki::cfg{PubUrlPath} = $ENV{SCRIPT_NAME} . '/pub';
}
}
else {
# No script, no path, shortest URLs
print STDERR "AUTOCONFIG: No path, No script, probably shorter URLs \n"
if (TRAUTO);
$Foswiki::cfg{ScriptUrlPaths}{view} = '';
$Foswiki::cfg{ScriptUrlPath} = '/bin';
$Foswiki::cfg{PubUrlPath} = '/pub';
}

if (TRAUTO) {
print STDERR
"AUTOCONFIG: Using ScriptUrlPath $Foswiki::cfg{ScriptUrlPath} \n";
print STDERR "AUTOCONFIG: Using {ScriptUrlPaths}{view} "
. (
( defined $Foswiki::cfg{ScriptUrlPaths}{view} )
? $Foswiki::cfg{ScriptUrlPaths}{view}
: 'undef'
) . "\n";
print STDERR
"AUTOCONFIG: Using PubUrlPath: $Foswiki::cfg{PubUrlPath} \n";
}

my %rel_to_root = (
DataDir => { dir => 'data', required => 0 },
LocalesDir => { dir => 'locale', required => 0 },
PubDir => { dir => 'pub', required => 0 },
ToolsDir => { dir => 'tools', required => 0 },
WorkingDir => {
dir => 'working',
required => 1,
validate_file => 'README'
},
TemplateDir => {
dir => 'templates',
required => 1,
validate_file => 'foswiki.tmpl'
},
ScriptDir => {
dir => 'bin',
required => 1,
validate_file => 'setlib.cfg'
}
);

# Note that we don't resolve x/../y to y, as this might
# confuse soft links
my $root = File::Spec->catdir( $bin, File::Spec->updir() );
$root =~ s{\\}{/}g;
my $fatal = '';
my $warn = '';
while ( my ( $key, $def ) = each %rel_to_root ) {
$Foswiki::cfg{$key} = File::Spec->rel2abs( $def->{dir}, $root );
$Foswiki::cfg{$key} = abs_path( $Foswiki::cfg{$key} );
( $Foswiki::cfg{$key} ) = $Foswiki::cfg{$key} =~ m/^(.*)$/; # untaint

print STDERR "AUTOCONFIG: $key = $Foswiki::cfg{$key} \n"
if (TRAUTO);

if ( -d $Foswiki::cfg{$key} ) {
if ( $def->{validate_file}
&& !-e "$Foswiki::cfg{$key}/$def->{validate_file}" )
{
$fatal .=
"\n{$key} (guessed $Foswiki::cfg{$key}) $Foswiki::cfg{$key}/$def->{validate_file} not found";
}
}
elsif ( $def->{required} ) {
$fatal .= "\n{$key} (guessed $Foswiki::cfg{$key})";
}
else {
$warn .=
"\n * Note: {$key} could not be guessed. Set it manually!";
}
}
if ($fatal) {
die <<EPITAPH;
Unable to bootstrap configuration. LocalSite.cfg could not be loaded,
and Foswiki was unable to guess the locations of the following critical
directories: $fatal
EPITAPH
}

# Re-read Foswiki.spec *and Config.spec*. We need the Config.spec's
# to get a true picture of our defaults (notably those from
# JQueryPlugin. Without the Config.spec, no plugins get registered)
Foswiki::Configure::Load::readConfig( 0, 0, 1 ) unless ($noload);

_workOutOS();

$Foswiki::cfg{isVALID} = 1;
$Foswiki::cfg{isBOOTSTRAPPING} = 1;
push( @{ $Foswiki::cfg{BOOTSTRAP} }, @BOOTSTRAP );

# Note: message is not I18N'd because there is no point; there
# is no localisation in a default cfg derived from Foswiki.spec
my $system_message = <<BOOTS;
*WARNING !LocalSite.cfg could not be found, or failed to load.* %BR%This
Foswiki is running using a bootstrap configuration worked
out by detecting the layout of the installation. Any requests made to this
Foswiki will be treated as requests made by an administrator with full rights
to make changes! You should either:
* correct any permissions problems with an existing !LocalSite.cfg (see the webserver error logs for details), or
* visit [[%SCRIPTURL{configure}%?VIEWPATH=$Foswiki::cfg{ScriptUrlPaths}{view}][configure]] as soon as possible to generate a new one.
BOOTS

if ($warn) {
chomp $system_message;
$system_message .= $warn . "\n";
}
return ( $system_message || '' );

}

sub _workOutOS {
unless ( ( $Foswiki::cfg{DetailedOS} = $^O ) ) {
require Config;
$Foswiki::cfg{DetailedOS} = $Config::Config{'osname'};
}
$Foswiki::cfg{OS} = 'UNIX';
if ( $Foswiki::cfg{DetailedOS} =~ /darwin/i ) { # MacOS X
$Foswiki::cfg{OS} = 'UNIX';
}
elsif ( $Foswiki::cfg{DetailedOS} =~ /Win/i ) {
$Foswiki::cfg{OS} = 'WINDOWS';
}
elsif ( $Foswiki::cfg{DetailedOS} =~ /vms/i ) {
$Foswiki::cfg{OS} = 'VMS';
}
elsif ( $Foswiki::cfg{DetailedOS} =~ /bsdos/i ) {
$Foswiki::cfg{OS} = 'UNIX';
}
elsif ( $Foswiki::cfg{DetailedOS} =~ /dos/i ) {
$Foswiki::cfg{OS} = 'DOS';
}
elsif ( $Foswiki::cfg{DetailedOS} =~ /^MacOS$/i ) { # MacOS 9 or earlier
$Foswiki::cfg{OS} = 'MACINTOSH';
}
elsif ( $Foswiki::cfg{DetailedOS} =~ /os2/i ) {
$Foswiki::cfg{OS} = 'OS2';
}
}

1;

__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2013 Foswiki Contributors. Foswiki Contributors
Copyright (C) 2008-2014 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Expand Down

0 comments on commit a95eec5

Please sign in to comment.