Skip to content

Commit

Permalink
Item8279:Item8228:
Browse files Browse the repository at this point in the history
   * fixed use in persistent perl envs
   * SETVAR and co has got higher prio than urlparams



git-svn-id: http://svn.foswiki.org/trunk/SetVariablePlugin@5552 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Nov 17, 2009
1 parent 8345a8f commit 1f1a022
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 36 deletions.
8 changes: 5 additions & 3 deletions data/System/SetVariablePlugin.txt
Expand Up @@ -113,10 +113,12 @@ Display a table of all definition rules as far as collected by SETVAR and UNSERV
| Plugin Author: | Foswiki:Main.MichaelDaum |
| Copyright: | © 2007-2009 Michael Daum http://michaeldaumconsulting.com |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Plugin Version: | 1.00 |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: |   |
| 17 Nov 2009: | SETVAR has higher priority than urlparams; fixed use in persistent perl envs |
| 23 Jun 2009: | initial release |
| Dependencies: | %$DEPENDENCIES% |
| Plugin Home: | Foswiki:Extensions/%TOPIC% |
| Feedback: | Foswiki:Extensions/%TOPIC%Dev |
| Home: | Foswiki:Extensions/%TOPIC% |
| Support: | Foswiki:Support/%TOPIC% |

12 changes: 4 additions & 8 deletions lib/Foswiki/Plugins/SetVariablePlugin.pm
Expand Up @@ -22,8 +22,8 @@ use vars qw(
$core
);

$VERSION = '$Rev$';
$RELEASE = '1.00';
$VERSION = '$Rev: 4287 (2009-06-23) $';
$RELEASE = '1.01';

$SHORTDESCRIPTION = 'Flexible handling of topic variables';
$NO_PREFS_IN_TOPIC = 1;
Expand All @@ -33,18 +33,14 @@ $NO_PREFS_IN_TOPIC = 1;
sub initPlugin {
my( $topic, $web, $user, $installWeb ) = @_;

# check for Plugins.pm versions
if( $Foswiki::Plugins::VERSION < 1.026 ) {
Foswiki::Func::writeWarning( "Version mismatch between SetVariablePlugin and Plugins.pm" );
return 0;
}

Foswiki::Func::registerTagHandler('SETVAR', \&handleSetVar);
Foswiki::Func::registerTagHandler('GETVAR', \&handleGetVar);
Foswiki::Func::registerTagHandler('DELVAR', \&handleUnsetVar);
Foswiki::Func::registerTagHandler('UNSETVAR', \&handleUnsetVar);
Foswiki::Func::registerTagHandler('DEBUGRULES', \&handleDebugRules);

undef $core;

return 1;
}

Expand Down
38 changes: 13 additions & 25 deletions lib/Foswiki/Plugins/SetVariablePlugin/Core.pm
Expand Up @@ -66,7 +66,7 @@ sub applyRules {
my @fields = $meta->find('FIELD');

# itterate over all rules in the given order
foreach my $record (@{$this->{rules}}) {
foreach my $record (sort {$b->{prio} <=> $a->{prio}} @{$this->{rules}}) {

# check conditions
if ($record->{field}) {
Expand All @@ -92,7 +92,7 @@ sub applyRules {
my $type = $record->{type} || 'Local';
my $value = expandVariables($record->{value});
if (defined $value) {
$value = entityEncode(Foswiki::Func::expandCommonVariables($value, $topic, $web));
$value = Foswiki::Func::expandCommonVariables($value, $topic, $web);
}

if ($record->{action} eq ACTION_SET && defined($value)) {
Expand All @@ -107,8 +107,6 @@ sub applyRules {
return $meta;
}

#my $setRegex = '((?:^|[\n\r])\t| +\*\s+)#?(?:Set\s+#?)('.$record->{var}.')\s+=\s+(.*?)($|[\n\r])';

###############################################################################
sub handleSetVar {
my ($this, $session, $params, $topic, $web) = @_;
Expand All @@ -121,6 +119,7 @@ sub handleSetVar {
type => ($params->{type} || 'Local'),
field => $params->{field},
regex => ($params->{match} || $params->{matches} || $params->{regex} || '.*'),
prio => 1,
);

return '';
Expand Down Expand Up @@ -166,7 +165,7 @@ sub handleGetVar {
return '';
}
@metas = $meta->find($theType);
writeDebug("found ".scalar(@metas)." metas");
#writeDebug("found ".scalar(@metas)." metas");
} elsif ($theScope eq 'web') {
my $value = Foswiki::Func::getPreferencesValue($theVar, $theWeb);
if (defined $value) {
Expand Down Expand Up @@ -221,6 +220,7 @@ sub handleUnsetVar {
var => ($params->{_DEFAULT} || $params->{var}),
field => $params->{field},
regex => ($params->{match} || $params->{matches} || $params->{regex} || '.*'),
prio => 1,
);

return '';
Expand Down Expand Up @@ -248,18 +248,14 @@ sub handleDebugRules {

###############################################################################
sub handleBeforeSave {
my $this = $_[0];
#my $text = $_[1];
my $topic = $_[2];
my $web = $_[3];
#my $meta = $_[4];
my ($this, $text, $topic, $web, $meta) = @_;

return if $this->{_insideBeforeSaveHandler};
$this->{_insideBeforeSaveHandler} = 1;
writeDebug("handleBeforeSave($web.$topic)");

# get the rules NOW
Foswiki::Func::expandCommonVariables($_[1], $topic, $web);
$text = Foswiki::Func::expandCommonVariables($text, $topic, $web);

# create rules from Set+VARNAME, Local+VARNAME, Unset+VARNAME and Default+VARNAME urlparams
my $request = Foswiki::Func::getCgiQuery();
Expand All @@ -270,17 +266,19 @@ sub handleBeforeSave {
my $name = $2;
my @values = $request->param($key);
next unless @values;
@values = grep {!/^$/} @values if @values > 1;
my $value = join(", ", @values);
writeDebug("key=$key, value=$value");

# convert a set to an unset if that's already default
if ($type =~ /Local|Set/) {
my @defaultValues = $request->param("Default+$name");
if (@defaultValues) {
@defaultValues = grep {!/^$/} @defaultValues if @defaultValues > 1;
my $defaultValue = join(', ', @defaultValues);
if ($defaultValue eq $value) {
$type = 'Unset';
writeDebug("found set to default/undef ... unsetting ".$name);
#writeDebug("found set to default/undef ... unsetting ".$name);
}
}
}
Expand All @@ -289,34 +287,24 @@ sub handleBeforeSave {
if ($type eq 'Unset') {
$this->addRule(ACTION_UNSET,
var => $name,
prio => 2,
);
} else {
$this->addRule(ACTION_SET,
var => $name,
value => $value,
type => $type,
prio => 2,
);
}
}

# execute rules in the given order
$this->applyRules($web, $topic, $_[4], $_[1]);
$this->applyRules($web, $topic, $meta, $text);

$this->{_insideBeforeSaveHandler} = 0;
}

###############################################################################
# private version :(
sub entityEncode {
my ($text, $extra) = @_;
$extra ||= '';

$text =~
s/([[\x01-\x09\x0b\x0c\x0e-\x1f"%&'*<=>@[_\|$extra])/'&#'.ord($1).';'/ge;

return $text;
}

###############################################################################
# static
sub expandVariables {
Expand Down

0 comments on commit 1f1a022

Please sign in to comment.