From d43ed5994e75cf76bcc42a2478a2b52ee59b3237 Mon Sep 17 00:00:00 2001 From: OlivierRaginel Date: Wed, 7 Jan 2009 21:33:03 +0000 Subject: [PATCH] Item689: Link %TWiki::cfg to %Foswiki::cfg in TWikiCompatibility plugin, and fix configure to handle %TWiki::cfg git-svn-id: http://svn.foswiki.org/trunk@1836 0b4bb1d4-4e5a-0410-9cc4-b2b747904278 --- TWikiCompatibilityPlugin/lib/TWiki.pm | 1 + core/lib/Foswiki/Configure/FoswikiCfg.pm | 6 +-- core/lib/Foswiki/Configure/Load.pm | 57 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/TWikiCompatibilityPlugin/lib/TWiki.pm b/TWikiCompatibilityPlugin/lib/TWiki.pm index 3800a6cdf4..a25e2fc40f 100644 --- a/TWikiCompatibilityPlugin/lib/TWiki.pm +++ b/TWikiCompatibilityPlugin/lib/TWiki.pm @@ -11,5 +11,6 @@ sub TWiki::new { } %TWiki::regex = %Foswiki::regex; +%TWiki::cfg = %Foswiki::cfg; 1; diff --git a/core/lib/Foswiki/Configure/FoswikiCfg.pm b/core/lib/Foswiki/Configure/FoswikiCfg.pm index ed5ec37c05..2923a3b486 100644 --- a/core/lib/Foswiki/Configure/FoswikiCfg.pm +++ b/core/lib/Foswiki/Configure/FoswikiCfg.pm @@ -200,9 +200,9 @@ sub _parse { $open = new Foswiki::Configure::Value( typename => $1, opts => $2 ); } - elsif ( $l =~ /^#?\s*\$(Foswiki::)?cfg([^=\s]*)\s*=(.*)$/ ) { - my $keys = $2; - my $tentativeVal = $3; + elsif ( $l =~ /^#?\s*\$(?:(?:Fosw|TW)iki::)?cfg([^=\s]*)\s*=(.*)$/ ) { + my $keys = $1; + my $tentativeVal = $2; if ( $open && $open->isa('SectionMarker') ) { pusht( \@settings, $open ); $open = undef; diff --git a/core/lib/Foswiki/Configure/Load.pm b/core/lib/Foswiki/Configure/Load.pm index 42a64484b3..a331bb01d1 100644 --- a/core/lib/Foswiki/Configure/Load.pm +++ b/core/lib/Foswiki/Configure/Load.pm @@ -149,6 +149,63 @@ sub readDefaults { _loadDefaultsFrom( "$dir/TWiki/Plugins", $root, \%read, \@errors ); _loadDefaultsFrom( "$dir/TWiki/Contrib", $root, \%read, \@errors ); } + if ( defined %TWiki::cfg ) { + + # We had some TWiki plugins, need to map their config to Foswiki + sub mergeHash { + + # Merges the keys in the right hashref to the ones in the left hashref + my ( $left, $right, $errors ) = @_; + while ( my ( $key, $value ) = each %$right ) { + if ( exists $left->{$key} ) { + if ( ref($value) ne ref( $left->{$key} ) ) { + push @$errors, + 'Trying to overwrite $Foswiki::cfg{' + . $key + . '} with its $TWiki::cfg version (' + . $value . ')'; + } + elsif ( ref($value) eq 'SCALAR' ) { + $left->{$key} = $value; + } + elsif ( ref($value) eq 'HASH' ) { + $left->{$key} = + mergeHash( $left->{$key}, $value, $errors ); + } + elsif ( ref($value) eq 'ARRAY' ) { + + # It's an array. try to be smart + # SMELL: Ideally, it should keep order too + foreach my $item (@$value) { + unless ( grep /^$item$/, @{ $left->{$key} } ) { + + # The item isn't in the current list, add it at the end + unshift @{ $left->{$key} }, $item; + } + } + } + else { + + # It's something else (GLOB, coderef, ...) + push @$errors, + '$TWiki::cfg{' + . $key + . '} is a reference to a' + . ref($value) + . '. No idea how to merge that, sorry.'; + } + } + else { + + # We don't already have such a key in the Foswiki scope + $left->{$key} = $value; + } + } + return $left; + } + mergeHash \%Foswiki::cfg, \%TWiki::cfg, \@errors; + } # define %TWiki::cfg + return \@errors; }