/
TestBootstrapPlugin.pm
471 lines (401 loc) · 16.3 KB
/
TestBootstrapPlugin.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# See bottom of file for default license and copyright information
=begin TML
---+ package Foswiki::Plugins::TestBootstrapPlugin
=cut
package Foswiki::Plugins::TestBootstrapPlugin;
# Always use strict to enforce variable scoping
use strict;
use warnings;
use Foswiki::Func (); # The plugins API
use Foswiki::Plugins (); # For the API version
use Cwd qw( abs_path );
use File::Basename;
use constant TRAUTO => 1;
our $VERSION = '1.0';
our $RELEASE = '04 Sep 2014';
# One line description of the module
our $SHORTDESCRIPTION = 'Test foswiki bootstrap code';
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 ) = @_;
# check for Plugins.pm versions
if ( $Foswiki::Plugins::VERSION < 2.0 ) {
Foswiki::Func::writeWarning( 'Version mismatch between ',
__PACKAGE__, ' and Plugins.pm' );
return 0;
}
Foswiki::Func::registerTagHandler( 'BOOTSTRAP', \&_BOOTSTRAP )
if ( Foswiki::Func::isAnAdmin() );
# Plugin correctly initialized
return 1;
}
sub _BOOTSTRAP {
my ( $session, $params, $topic, $web, $topicObject ) = @_;
my $msg = "<blockquote><verbatim>\n";
my $boot_cfg;
my $resp;
{
local *STDERR;
my $log;
open STDERR, '>', \$log;
( $boot_cfg, $resp ) = _runBootstrap();
close STDERR;
$msg .= $resp . "\n\n";
$msg .= $log;
}
$msg .= "\n\n";
foreach my $ek ( sort keys %ENV ) {
$msg .= "\$ENV{$ek} = $ENV{$ek} \n";
}
$msg .= "</verbatim>\n\n<noautolink>\n";
$msg .= "| *Key* | *Current* | *Bootstrap* |\n";
foreach my $key ( sort keys %$boot_cfg ) {
next if ( $key eq 'BOOTSTRAP' );
# SMELL: Not very pretty
if ( ref( $boot_cfg->{$key} ) eq 'HASH' ) {
foreach my $k2 ( keys %{ $boot_cfg->{$key} } ) {
my $cur =
( defined $Foswiki::cfg{$key}{$k2} )
? $Foswiki::cfg{$key}{$k2}
: '(undefined)';
$cur =
( length $Foswiki::cfg{$key}{$k2} )
? $Foswiki::cfg{$key}{$k2}
: '(empty)';
my $boot =
( defined $boot_cfg->{$key}{$k2} )
? $boot_cfg->{$key}{$k2}
: '(undefined)';
$boot =
( length $boot_cfg->{$key}{$k2} )
? $boot_cfg->{$key}{$k2}
: '(empty)';
$msg .= "| ={$key}{$k2}= | =$cur= | =$boot= |\n";
}
next;
}
else {
my $cur =
( defined $Foswiki::cfg{$key} )
? $Foswiki::cfg{$key}
: '(undefined)';
$cur =
( length $Foswiki::cfg{$key} ) ? $Foswiki::cfg{$key} : '(empty)';
my $boot =
( defined $boot_cfg->{$key} ) ? $boot_cfg->{$key} : '(undefined)';
$boot =
( length $boot_cfg->{$key} ) ? $boot_cfg->{$key} : '(empty)';
$msg .= "| =$key= | =$cur= | =$boot= |\n";
}
}
return "$msg</noautolink></blockquote>\n";
}
sub _runBootstrap {
local %Foswiki::cfg = ( Engine => $Foswiki::cfg{Engine} );
my $msg;
if ( Foswiki::Configure::Load->can('bootstrapConfig') ) {
$msg = Foswiki::Configure::Load::bootstrapConfig(1);
}
else {
$msg = _bootstrapConfig(1);
}
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];
$Foswiki::cfg{ScriptSuffix} = ''
if ( $Foswiki::cfg{ScriptSuffix} eq '.fcgi' );
print STDERR
"AUTOCONFIG: Found SCRIPT SUFFIX $Foswiki::cfg{ScriptSuffix} \n"
if ( TRAUTO && $Foswiki::cfg{ScriptSuffix} );
my $protocol = $ENV{HTTPS} ? 'https' : 'http';
# Figure out the DefaultUrlHost
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);
}
elsif ( $ENV{SCRIPT_URI} ) {
( $Foswiki::cfg{DefaultUrlHost} ) =
$ENV{SCRIPT_URI} =~ m#^(https?://[^/]+)/#;
print STDERR
"AUTOCONFIG: Set DefaultUrlHost $Foswiki::cfg{DefaultUrlHost} from SCRIPT_URI $ENV{SCRIPT_URI} \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.
my $path_info = $ENV{PATH_INFO} || ''; # SMELL Sometimes PATH_INFO in undef
print STDERR "AUTOCONFIG: REQUEST_URI is $ENV{REQUEST_URI} \n" if (TRAUTO);
print STDERR "AUTOCONFIG: SCRIPT_URI is "
. ( $ENV{SCRIPT_URI} || '(undef)' ) . " \n"
if (TRAUTO);
print STDERR "AUTOCONFIG: PATH_INFO is $path_info \n" if (TRAUTO);
print STDERR "AUTOCONFIG: ENGINE is $Foswiki::cfg{Engine}\n"
if (TRAUTO);
# This code tries to break the url up into <prefix><script><path> ... The script may or may not
# be present. Short URLs will omit the script from view operations, and *may* omit the
# <prefix> for all operations. Examples of URLs and shortening.
#
# Full: /foswiki/bin/view/Main/WebHome /foswiki/bin/edit/Main/WebHome
# Full: /bin/view/Main/WebHome /bin/edit/Main/WebHome omitting prefix
# Short: /foswiki/Main/WebHome /foswiki/bin/edit/Main/WebHome omitting bin/view
# Short: /Main/WebHome /bin/edit/Main/WebHome omitting prefix and bin/view
# Shorter: /Main/WebHome /edit/Main/WebHome omitting prefix and bin in all cases.
#
# Note that some of this can't be done as part of the view script. The only way to know if "bin" is omitted in
# all cases is when a script other than view runs, like jsonrpc.
my $pfx;
if ( $Foswiki::cfg{Engine} =~ m/FastCGI/ ) {
#PATH_INFO includes script /view/System/WebHome, REQUEST_URI is /System/WebHome.
($script) = $ENV{PATH_INFO} =~ m#^/([^/]+)/#;
$script ||= '';
print STDERR
"AUTOCONFIG: FCGI Parsed script $script from PATH_INFO $ENV{PATH_INFO} \n"
if (TRAUTO);
$pfx = $ENV{SCRIPT_NAME};
print STDERR
"AUTOCONFIG: FCGI set Prefix $pfx from \$ENV{SCRIPT_NAME}\n"
if (TRAUTO);
}
else {
my $suffix =
( length( $ENV{SCRIPT_URL} ) < length($path_info) )
? $ENV{SCRIPT_URL}
: $path_info;
# Try to Determine the prefix of the script part of the URI.
if ( $ENV{SCRIPT_URI} && $ENV{SCRIPT_URL} ) {
if ( index( $ENV{SCRIPT_URI}, $Foswiki::cfg{DefaultUrlHost} ) eq 0 )
{
$pfx =
substr( $ENV{SCRIPT_URI},
length( $Foswiki::cfg{DefaultUrlHost} ) );
$pfx =~ s#$suffix$##;
print STDERR
"AUTOCONFIG: Calculated prefix $pfx from SCRIPT_URI and SCRIPT_URL\n"
if (TRAUTO);
}
}
}
unless ( defined $pfx ) {
if ( my $idx = index( $ENV{REQUEST_URI}, $path_info ) ) {
$pfx = substr( $ENV{REQUEST_URI}, 0, $idx + 1 );
}
$pfx = '' unless ( defined $pfx );
print STDERR "AUTOCONFIG: URI Prefix is $pfx\n" if (TRAUTO);
}
# Work out the URL path for Short and standard URLs
if ( $ENV{REQUEST_URI} =~ m{^(.*?)/$script(\b|$)} ) {
print STDERR
"AUTOCONFIG: SCRIPT $script fully contained in REQUEST_URI $ENV{REQUEST_URI}, Not short URLs\n"
if (TRAUTO);
# 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 {
print STDERR "AUTOCONFIG: Building Short URL paths using prefix \n"
if (TRAUTO);
$Foswiki::cfg{ScriptUrlPath} = $pfx . '/bin';
$Foswiki::cfg{ScriptUrlPaths}{view} = $pfx;
$Foswiki::cfg{PubUrlPath} = $pfx . '/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-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.
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. For
more details read LICENSE in the root of this distribution.
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.
As per the GPL, removal of this notice is prohibited.