~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Configure/Valuer.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
 
 
3
#
 
4
# This class is used to refer to two hashes of configuration values.
 
5
# The first is a hash of default values, and the second (which will
 
6
# have mostly the same keys) contains the *current* value (i.e. the
 
7
# value after edits have been applied).
 
8
#
 
9
# $defaults is a reference to the hash of defaults
 
10
# $values is a reference to the hash of current values
 
11
package Foswiki::Configure::Valuer;
 
12
 
 
13
use strict;
 
14
 
 
15
use Foswiki::Configure::Type;
 
16
 
 
17
sub new {
 
18
    my ( $class, $defaults, $values ) = @_;
 
19
 
 
20
    my $this = bless( {}, $class );
 
21
    $this->{defaults} = $defaults;
 
22
    $this->{values}   = $values;
 
23
 
 
24
    return $this;
 
25
}
 
26
 
 
27
# Get a value from one of the value sets (defaults or values)
 
28
sub _getValue {
 
29
    my ( $this, $value, $set ) = @_;
 
30
    my $keys = $value->getKeys();
 
31
    my $var  = '$this->{' . $set . '}->' . $keys;
 
32
    my $val;
 
33
    eval '$val = ' . $var . ' if exists(' . $var . ')';
 
34
    if ( defined $val ) {
 
35
 
 
36
        # SMELL: Really shouldn't do this unless we are sure it's an RE,
 
37
        # but the probability of this string occurring elsewhere than an
 
38
        # RE is so low that we can afford to take the risk.
 
39
        while ( $val =~ s/^\(\?-xism:(.*)\)$/$1/ ) { }
 
40
    }
 
41
    return $val;
 
42
}
 
43
 
 
44
# get the current value
 
45
sub currentValue {
 
46
    my ( $this, $value ) = @_;
 
47
    return $this->_getValue( $value, 'values' );
 
48
}
 
49
 
 
50
# get the default value
 
51
sub defaultValue {
 
52
    my ( $this, $value ) = @_;
 
53
    return $this->_getValue( $value, 'defaults' );
 
54
}
 
55
 
 
56
# Get changed values from CGI. Each parameter is identified by a
 
57
# TYPEOF: param that specifies the keys e.g. ?TYPEOF:{Kiss}=Smooch. The
 
58
# type is used to determine if the value of {Kiss} in CGI is different to
 
59
# the value known to the Valuer (i.e. has been updated). If it is, the keys
 
60
# are added to the $updated hash.
 
61
sub loadCGIParams {
 
62
    my ( $this, $query, $updated ) = @_;
 
63
    my $param;
 
64
    my $changed = 0;
 
65
 
 
66
    # Each config param has an associated TYPEOF: param, so we only
 
67
    # pick up those things that we really want
 
68
    foreach $param ( $query->param ) {
 
69
 
 
70
        # the - (and therefore the ' and ") is required for languages
 
71
        # e.g. {Languages}{'zh-cn'}.
 
72
        next unless $param =~ /^TYPEOF:((?:{[-\w'"]+})*)/;
 
73
        my $keys = $1;
 
74
 
 
75
        # The value of TYPEOF: is the type name
 
76
        my $typename = $query->param($param);
 
77
        $typename =~ /(\w+)/;
 
78
        $typename = $1;    # check and untaint
 
79
        my $type   = Foswiki::Configure::Type::load($typename);
 
80
        my $newval = $type->string2value( $query->param($keys) );
 
81
        my $xpr    = '$this->{values}->' . $keys;
 
82
        my $curval = eval $xpr;
 
83
        if ( !$type->equals( $newval, $curval ) ) {
 
84
 
 
85
            #print "<br>$typename $keys '$newval' != '$curval'\n";
 
86
            eval $xpr . ' = $newval';
 
87
            $changed++;
 
88
            $updated->{$keys} = 1;
 
89
        }
 
90
    }
 
91
    return $changed;
 
92
}
 
93
 
 
94
1;
 
95
__DATA__
 
96
#
 
97
# Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
98
#
 
99
# Copyright (C) 2008 Foswiki Contributors. All Rights Reserved.
 
100
# Foswiki Contributors are listed in the AUTHORS file in the root
 
101
# of this distribution. NOTE: Please extend that file, not this notice.
 
102
#
 
103
# Additional copyrights apply to some or all of the code in this
 
104
# file as follows:
 
105
#
 
106
# Copyright (C) 2000-2006 TWiki Contributors. All Rights Reserved.
 
107
# TWiki Contributors are listed in the AUTHORS file in the root
 
108
# of this distribution. NOTE: Please extend that file, not this notice.
 
109
#
 
110
# This program is free software; you can redistribute it and/or
 
111
# modify it under the terms of the GNU General Public License
 
112
# as published by the Free Software Foundation; either version 2
 
113
# of the License, or (at your option) any later version. For
 
114
# more details read LICENSE in the root of this distribution.
 
115
#
 
116
# This program is distributed in the hope that it will be useful,
 
117
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
118
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
119
#
 
120
# As per the GPL, removal of this notice is prohibited.