Skip to content

Commit

Permalink
Item14237: Implemented -expandable support
Browse files Browse the repository at this point in the history
In terms of the legacy specs code these elements are "pluggables". Since
the term is used for definition of specific class methods (see
Foswiki::Class) the new specs have adopted 'expandable' as replacement.

- Converted FINDEXTENSIONS, LANGUAGES, and PLUGINS pluggables into
expandable.

- Fixed a deep recursion in prepareDataHashClass when referred
uninitialized extMgr attribute on $app.

- As SCRIPTHASH legacy pluggable seems to be obsolete in v3 it's been
removed from Foswiki.spec

- Syntax fixes in Foswiki.spec
  • Loading branch information
vrurg committed May 25, 2017
1 parent fe9dd5d commit ac96190
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 32 deletions.
44 changes: 44 additions & 0 deletions UnitTestContrib/test/unit/ConfigTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,48 @@ sub test_defaultValue {
);
}

sub test_expandable {
my $this = shift;

my $data = $this->app->cfg->makeSpecsHash;

my $dataObj = tied %$data;

my $cfg = $this->app->cfg;

my $section = $this->create( 'Foswiki::Config::Section', name => 'Root', );

$cfg->spec(
source => __FILE__,
dataObj => $dataObj,
section => $section,
specs => [
-expandable => sub {
return (
-section => Section => [
'Test1.Key1' => [
-type => 'STRING',
-default => 'Default Key1',
],
'Test2.Key2' => [
-type => 'NUMBER',
-default => 3.1415926,
],
]
);
},
],
);

$this->assert_deep_equals(
{
Test1 => { Key1 => 'Default Key1', },
Test2 => { Key2 => 3.1415926, },
},
$data
);
}

my %keyStructs = (
Straigt => [ 'A' .. 'M' ],
ComplexDeep => [
Expand Down Expand Up @@ -755,6 +797,8 @@ sub test_ReadWriteLSC {

$this->app->cfg->data->{ThisKey}{Is}{Not}{From}{Specs} =
"But we will have it in the LSC";
$this->app->cfg->data->{Extensions}{DBConfigExtension}{Connection}{Table} =
"LSC_TEST";

my $lscFile = "./TestLocalSite.cfg";
$this->app->cfg->writeLSC( lscFile => $lscFile, );
Expand Down
7 changes: 2 additions & 5 deletions core/lib/Foswiki.spec
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ $Foswiki::cfg{PermittedRedirectHostUrls} = '';
# for more information.
# $Foswiki::cfg{PubUrlPath} = '/foswiki/pub';

#! The following plugin must follow all other {ScriptUrlPaths} items
# *SCRIPTHASH*

# ---++ File System Paths
# Configure the file system locations of key Foswiki directories here. These are usually guessed
# correctly during bootstrap. Other file locations are configured within their related sections.
Expand Down Expand Up @@ -2376,7 +2373,7 @@ $Foswiki::cfg{Plugins}{NatEditPlugin}{Enabled} = 1;
$Foswiki::cfg{Plugins}{NatEditPlugin}{Module} =
'Foswiki::Plugins::NatEditPlugin';

# **BOOLEAN LABEL="PreferencesPlugin"
# **BOOLEAN LABEL="PreferencesPlugin"**
$Foswiki::cfg{Plugins}{PreferencesPlugin}{Enabled} = 1;

# **STRING EXPERT LABEL="PreferencesPlugin Module"**
Expand All @@ -2397,7 +2394,7 @@ $Foswiki::cfg{Plugins}{SlideShowPlugin}{Enabled} = 1;
$Foswiki::cfg{Plugins}{SlideShowPlugin}{Module} =
'Foswiki::Plugins::SlideShowPlugin';

# **BOOLEAN LABEL="SmiliesPlugin"
# **BOOLEAN LABEL="SmiliesPlugin"**
$Foswiki::cfg{Plugins}{SmiliesPlugin}{Enabled} = 1;

# **STRING EXPERT LABEL="SmiliesPlugin Module"**
Expand Down
115 changes: 93 additions & 22 deletions core/lib/Foswiki/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3010,8 +3010,8 @@ sub prepareDataHashClass {
# Map the class only if app's extensions attribute is initialized. Otherwise
# avoid autovivification. This code would make sense when very early config
# loading would be implemented.
$hashClass = $this->app->extensions->mapClass($hashClass)
if $this->app->has_extensions;
$hashClass = $this->app->extMgr->mapClass($hashClass)
if $this->app->has_extMgr;

Foswiki::load_class($hashClass);

Expand Down Expand Up @@ -3140,7 +3140,72 @@ sub _specSectionBody {
}
else {
# Bool zero-arity options get values too.
$section->setOpt( $option, $values[0] );
if ( $option eq 'expandable' ) {

# Insert dynamically-generated specs.
my $specData;
my $expandable = $values[0];
if ( my $refType = ref($expandable) ) {
if ( $refType eq 'CODE' ) {
$specData =
[ $expandable->( $this, section => $section, ) ];
}
else {
Foswiki::Exception::Config::BadSpecData->throw(
text => "Unallowed reference type "
. $refType
. " for -expandable",
section => $section,
);
}
}
else {
my $expMod = $expandable;

$expMod = __PACKAGE__ . "::Expandable::$expMod"
unless $expMod =~ /::/;

try {
Foswiki::load_package($expMod);

my $composeSub = $expMod->can('compose');

Foswiki::Exception::Config::BadSpecData->throw(
text => "Module "
. $expMod
. " doesn't have 'compose' method",
section => $section,
) unless $composeSub;

if ( $expMod->can('new') ) {
my $expObj =
$this->create( $expMod, cfg => $this, );
$specData = [ $expObj->compose ];
}
else {
$specData = [
$composeSub->(
$this, section => $section,
)
];
}
}
catch {
my $e =
Foswiki::Exception::Fatal->transmute( $_, 0 );
$e->_set_text( "Processing of -expandable '"
. $expandable
. "' failed: "
. $e->text );
$e->rethrow;
};
}

$specs->inject( specDef => $specData );
}
else {
$section->setOpt( $option, $values[0] );
}
}

}
Expand Down Expand Up @@ -3190,23 +3255,23 @@ sub _specSection {
$this->_specSectionBody( specs => $secSpecs, );
}

sub _specModprefix {
my $this = shift;
my %params = @_;

my $specs = $params{specs};
my $section = $specs->section;

Foswiki::Exception::Config::BadSpecData->throw(
text => "Incomplete -modprefix option, missing value",
section => $section,

) unless $specs->hasNext;

my $prefix = $specs->fetch;

$section->setOpt( modprefix => $prefix );
}
#sub _specModprefix {
# my $this = shift;
# my %params = @_;
#
# my $specs = $params{specs};
# my $section = $specs->section;
#
# Foswiki::Exception::Config::BadSpecData->throw(
# text => "Incomplete -modprefix option, missing value",
# section => $section,
#
# ) unless $specs->hasNext;
#
# my $prefix = $specs->fetch;
#
# $section->setOpt( modprefix => $prefix );
#}

sub _isCompleteKeyDef {
my $this = shift;
Expand Down Expand Up @@ -3292,8 +3357,8 @@ sub _specCfgKey {

# $isLeafKey is undef until we decide if the key we're working with is leaf
# – i.e. defines a key storing value, not other keys.
# The node is non-leaf if it hold a hash ref. For a newly created node
# its value is undefined and thus
# The node is non-leaf if it holds a hash ref. For a newly created node
# its value is undefined.
my ( $isLeafKey, $isEnhancing, $keyType, $prevKeyType, $keySize, $keyText );
my $noSourceFromSpec = 0;

Expand Down Expand Up @@ -3327,6 +3392,12 @@ sub _specCfgKey {
while ( $keySpecs->hasNext ) {
my $elem = $keySpecs->fetch;

Foswiki::Exception::Config::BadSpecData->throw(
text =>
"Undefined value encountered where an element is expected for key '$keyFullName'",
section => $section,
) unless defined $elem;

Foswiki::Exception::Config::BadSpecData->throw(
text => "Unexpected reference to "
. ref($elem)
Expand Down
44 changes: 44 additions & 0 deletions core/lib/Foswiki/Config/Expandable/FINDEXTENSIONS.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# See bottom of file for license and copyright information

=begin TML
---+ package Foswiki::Config::Expandable::FINDEXTENSIONS
Pluggable for finding and downloading extensions. Implements
<nop>*FINDEXTENSIONS* in Foswiki.spec.
=cut

package Foswiki::Config::Expandable::FINDEXTENSIONS;

use Foswiki::Class qw(app);
extends qw(Foswiki::Object);
with qw(Foswiki::Config::CfgObject);

sub compose {
my $this = shift;

# *FINDEXTENSIONS* is already placed in an appropriate section in
# Foswiki.spec. There are no additional configuration items required.

return ();
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2017 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.
105 changes: 105 additions & 0 deletions core/lib/Foswiki/Config/Expandable/LANGUAGES.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# See bottom of file for license and copyright information

package Foswiki::Config::Expandable::LANGUAGES;

use Assert;
use Locale::Language;
use Locale::Country;

require Foswiki::Configure::FileUtil;

use Foswiki::Class qw(app);
extends qw(Foswiki::Object);
with qw(Foswiki::Config::CfgObject);

sub compose {
my $this = shift;

# Insert a bunch of configuration items based on what's in
# the locales dir

# Force initialize i18n
#$this->app->i18n->_lh;

my $cfg = $this->cfg;

my $d =
$cfg->data->{LocalesDir}
|| Foswiki::Configure::FileUtil::findFileOnPath('../locale')
|| '';

$d = $cfg->expandStr( str => $d );

my $dh;
opendir( $dh, $d )
or
Foswiki::Exception::FileOp->throw( op => "open directory", file => $d, );

my %langs;

foreach my $file ( readdir $dh ) {
next unless ( $file =~ m/^([\w-]+)\.po$/ );
my $lang = $1;
my $keys = $lang;

#$keys = "'$keys'" if $keys =~ m/\W/;

my $label;
if ( $lang =~ m/^(\w+)-(\w+)$/ ) {
my ( $lname, $cname ) = (
( Locale::Language::code2language($1) || '' ),
( Locale::Country::code2country($2) || '' )
);
if ( $lname && $cname ) {
$label = "$lname ($cname)";
}
elsif ($lname) {
$label = "$lname ($2)";
}
elsif ($cname) {
$label = "$1 ($cname)";
}
else {
$label = "$lang";
}
}
else {
$label = Locale::Language::code2language($lang) || "$lang";
}

$langs{$label} = [
"Languages.$keys.Enabled" => "BOOLEAN" => [
-label => $label,
-default => 0,
-checker => 'LANGUAGE',
-display_if => "{UserInterfaceInternationalisation}",
-check => "undefok emptyok",
-onsave => 1,
]
];

}
closedir($dh);

return map { @{ $langs{$_} } } sort keys %langs;
}

1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2017 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.
Loading

0 comments on commit ac96190

Please sign in to comment.