~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Compatibility.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
package Foswiki::Compatibility;
 
3
 
 
4
use Assert;
 
5
 
 
6
=begin TML
 
7
 
 
8
---+ package Foswiki::Compatibility
 
9
 
 
10
Support for compatibility with old versions. Packaged
 
11
separately because 99.999999% of the time this won't be needed.
 
12
 
 
13
=cut
 
14
 
 
15
sub _upgradeCategoryItem {
 
16
    my ( $catitems, $ctext ) = @_;
 
17
    my $catname     = '';
 
18
    my $scatname    = '';
 
19
    my $catmodifier = '';
 
20
    my $catvalue    = '';
 
21
    my @cmd         = split( /\|/, $catitems );
 
22
    my $src         = '';
 
23
    my $len         = @cmd;
 
24
    if ( $len < '2' ) {
 
25
 
 
26
        # FIXME
 
27
        return ( $catname, $catmodifier, $catvalue );
 
28
    }
 
29
    my $svalue = '';
 
30
 
 
31
    my $i;
 
32
    my $itemsPerLine;
 
33
 
 
34
    # check for CategoryName=CategoryValue parameter
 
35
    my $paramCmd = '';
 
36
    my $cvalue   = '';    # was$query->param( $cmd[1] );
 
37
    if ($cvalue) {
 
38
        $src = "<!---->$cvalue<!---->";
 
39
    }
 
40
    elsif ($ctext) {
 
41
        foreach ( split( /\r?\n/, $ctext ) ) {
 
42
            if (/$cmd[1]/) {
 
43
                $src = $_;
 
44
                last;
 
45
            }
 
46
        }
 
47
    }
 
48
 
 
49
    if ( $cmd[0] eq 'select' || $cmd[0] eq 'radio' ) {
 
50
        $catname  = $cmd[1];
 
51
        $scatname = $catname;
 
52
 
 
53
        #$scatname =~ s/[^a-zA-Z0-9]//g;
 
54
        my $size = $cmd[2];
 
55
        for ( $i = 3 ; $i < $len ; $i++ ) {
 
56
            my $value = $cmd[$i];
 
57
            $svalue = $value;
 
58
            if ( $src =~ /$value/ ) {
 
59
                $catvalue = $svalue;
 
60
            }
 
61
        }
 
62
 
 
63
    }
 
64
    elsif ( $cmd[0] eq 'checkbox' ) {
 
65
        $catname  = $cmd[1];
 
66
        $scatname = $catname;
 
67
 
 
68
        #$scatname =~ s/[^a-zA-Z0-9]//g;
 
69
        if ( $cmd[2] eq 'true' || $cmd[2] eq '1' ) {
 
70
            $i           = $len - 4;
 
71
            $catmodifier = 1;
 
72
        }
 
73
        $itemsPerLine = $cmd[3];
 
74
        for ( $i = 4 ; $i < $len ; $i++ ) {
 
75
            my $value = $cmd[$i];
 
76
            $svalue = $value;
 
77
 
 
78
            # I18N: FIXME - need to look at this, but since it's upgrading
 
79
            # old forms that probably didn't use I18N, it's not a high
 
80
            # priority.
 
81
            if ( $src =~ /$value[^a-zA-Z0-9\.]/ ) {
 
82
                $catvalue .= ", " if ($catvalue);
 
83
                $catvalue .= $svalue;
 
84
            }
 
85
        }
 
86
 
 
87
    }
 
88
    elsif ( $cmd[0] eq 'text' ) {
 
89
        $catname  = $cmd[1];
 
90
        $scatname = $catname;
 
91
 
 
92
        #$scatname =~ s/[^a-zA-Z0-9]//g;
 
93
        # SMELL: unchecked implicit untaint?
 
94
        $src =~ /<!---->(.*)<!---->/;
 
95
        if ($1) {
 
96
            $src = $1;
 
97
        }
 
98
        else {
 
99
            $src = '';
 
100
        }
 
101
        $catvalue = $src;
 
102
    }
 
103
 
 
104
    return ( $catname, $catmodifier, $catvalue );
 
105
}
 
106
 
 
107
=begin TML
 
108
 
 
109
---++ StaticMethod upgradeCategoryTable( $session, $web, $topic, $meta, $text ) -> $text
 
110
 
 
111
Upgrade old style category table
 
112
 
 
113
May throw Foswiki::OopsException
 
114
 
 
115
=cut
 
116
 
 
117
sub upgradeCategoryTable {
 
118
    my ( $session, $web, $topic, $meta, $text ) = @_;
 
119
 
 
120
    my $icat = $session->templates->readTemplate('twikicatitems');
 
121
 
 
122
    if ($icat) {
 
123
        my @items = ();
 
124
 
 
125
        # extract category section and build category form elements
 
126
        my ( $before, $ctext, $after ) = split( /<!--TWikiCat-->/, $text );
 
127
 
 
128
        # cut TWikiCat part
 
129
        $text = $before || '';
 
130
        $text .= $after if ($after);
 
131
        $ctext = '' if ( !$ctext );
 
132
 
 
133
        my $ttext = '';
 
134
        foreach ( split( /\r?\n/, $icat ) ) {
 
135
            my ( $catname, $catmod, $catvalue ) =
 
136
              _upgradeCategoryItem( $_, $ctext );
 
137
            if ($catname) {
 
138
                push @items, ( [ $catname, $catmod, $catvalue ] );
 
139
            }
 
140
        }
 
141
        my $prefs = $session->{prefs};
 
142
        my $listForms = $prefs->getWebPreferencesValue( 'WEBFORMS', $web );
 
143
        $listForms =~ s/^\s*//go;
 
144
        $listForms =~ s/\s*$//go;
 
145
        my @formTemplates = split( /\s*,\s*/, $listForms );
 
146
        my $defaultFormTemplate = '';
 
147
        $defaultFormTemplate = $formTemplates[0] if (@formTemplates);
 
148
 
 
149
        if ( !$defaultFormTemplate ) {
 
150
            $session->logger->log(
 
151
                'warning',
 
152
                "Form: can't get form definition to convert category table "
 
153
                  . " for topic $web.$topic" );
 
154
            foreach my $oldCat (@items) {
 
155
                my $name  = $oldCat->[0];
 
156
                my $value = $oldCat->[2];
 
157
                $meta->put( 'FORM', { name => '' } );
 
158
                $meta->putKeyed(
 
159
                    'FIELD',
 
160
                    {
 
161
                        name  => $name,
 
162
                        title => $name,
 
163
                        value => $value
 
164
                    }
 
165
                );
 
166
            }
 
167
            return;
 
168
        }
 
169
 
 
170
        require Foswiki::Form;
 
171
        my $def = new Foswiki::Form( $session, $web, $defaultFormTemplate );
 
172
        $meta->put( 'FORM', { name => $defaultFormTemplate } );
 
173
 
 
174
        foreach my $fieldDef ( @{ $def->getFields() } ) {
 
175
            my $value = '';
 
176
            foreach my $oldCatP (@items) {
 
177
                my @oldCat = @$oldCatP;
 
178
                my $name = $oldCat[0] || '';
 
179
                $name =~ s/[^A-Za-z0-9_\.]//go;
 
180
                if ( $name eq $fieldDef->{name} ) {
 
181
                    $value = $oldCat[2];
 
182
                    last;
 
183
                }
 
184
            }
 
185
            $meta->putKeyed(
 
186
                'FIELD',
 
187
                {
 
188
                    name  => $fieldDef->{name},
 
189
                    title => $fieldDef->{title},
 
190
                    value => $value,
 
191
                }
 
192
            );
 
193
        }
 
194
 
 
195
    }
 
196
    else {
 
197
        $session->logger->log('warning',
 
198
            "Form: get find category template twikicatitems for Web $web");
 
199
    }
 
200
    return $text;
 
201
}
 
202
 
 
203
#Get file attachment attributes for old html
 
204
#format.
 
205
sub _getOldAttachAttr {
 
206
    my ( $session, $atext ) = @_;
 
207
    my $fileName    = '';
 
208
    my $filePath    = '';
 
209
    my $fileSize    = '';
 
210
    my $fileDate    = '';
 
211
    my $fileUser    = '';
 
212
    my $fileComment = '';
 
213
    my $before      = '';
 
214
    my $item        = '';
 
215
    my $after       = '';
 
216
    my $users       = $session->{users};
 
217
 
 
218
    ( $before, $fileName, $after ) = split( /<(?:\/)*TwkFileName>/, $atext );
 
219
    if ( !$fileName ) { $fileName = ''; }
 
220
    if ($fileName) {
 
221
        ( $before, $filePath, $after ) =
 
222
          split( /<(?:\/)*TwkFilePath>/, $atext );
 
223
        if ( !$filePath ) { $filePath = ''; }
 
224
        # SMELL: unchecked implicit untaint
 
225
        $filePath =~ s/<TwkData value="(.*)">//go;
 
226
        if   ($1) { $filePath = $1; }
 
227
        else      { $filePath = ''; }
 
228
        $filePath =~
 
229
          s/\%NOP\%//goi;    # delete placeholder that prevents WikiLinks
 
230
        ( $before, $fileSize, $after ) =
 
231
          split( /<(?:\/)*TwkFileSize>/, $atext );
 
232
        if ( !$fileSize ) { $fileSize = '0'; }
 
233
        ( $before, $fileDate, $after ) =
 
234
          split( /<(?:\/)*TwkFileDate>/, $atext );
 
235
 
 
236
        if ( !$fileDate ) {
 
237
            $fileDate = '';
 
238
        }
 
239
        else {
 
240
            $fileDate =~ s/&nbsp;/ /go;
 
241
            require Foswiki::Time;
 
242
            $fileDate = Foswiki::Time::parseTime($fileDate);
 
243
        }
 
244
        ( $before, $fileUser, $after ) =
 
245
          split( /<(?:\/)*TwkFileUser>/, $atext );
 
246
        if ( !$fileUser ) {
 
247
            $fileUser = '';
 
248
        }
 
249
        else {
 
250
            $fileUser = $users->getLoginName($fileUser) if $fileUser;
 
251
        }
 
252
        $fileUser ||= '';
 
253
        $fileUser =~ s/ //go;
 
254
        ( $before, $fileComment, $after ) =
 
255
          split( /<(?:\/)*TwkFileComment>/, $atext );
 
256
        if ( !$fileComment ) { $fileComment = ''; }
 
257
    }
 
258
 
 
259
    return ( $fileName, $filePath, $fileSize, $fileDate, $fileUser,
 
260
        $fileComment );
 
261
}
 
262
 
 
263
=begin TML
 
264
 
 
265
---++ migrateToFileAttachmentMacro ( $session, $meta, $text  ) -> $text
 
266
 
 
267
Migrate old HTML format
 
268
 
 
269
=cut
 
270
 
 
271
sub migrateToFileAttachmentMacro {
 
272
    my ( $session, $meta, $text ) = @_;
 
273
    ASSERT( $meta->isa('Foswiki::Meta') ) if DEBUG;
 
274
 
 
275
    my ( $before, $atext, $after ) = split( /<!--TWikiAttachment-->/, $text );
 
276
    $text = $before || '';
 
277
    $text .= $after if ($after);
 
278
    $atext = '' if ( !$atext );
 
279
 
 
280
    if ( $atext =~ /<TwkNextItem>/ ) {
 
281
        my $line = '';
 
282
        foreach $line ( split( /<TwkNextItem>/, $atext ) ) {
 
283
            my (
 
284
                $fileName, $filePath, $fileSize,
 
285
                $fileDate, $fileUser, $fileComment
 
286
            ) = _getOldAttachAttr( $session, $line );
 
287
 
 
288
            if ($fileName) {
 
289
                $meta->putKeyed(
 
290
                    'FILEATTACHMENT',
 
291
                    {
 
292
                        name    => $fileName,
 
293
                        version => '',
 
294
                        path    => $filePath,
 
295
                        size    => $fileSize,
 
296
                        date    => $fileDate,
 
297
                        user    => $fileUser,
 
298
                        comment => $fileComment,
 
299
                        attr    => ''
 
300
                    }
 
301
                );
 
302
            }
 
303
        }
 
304
    }
 
305
    else {
 
306
 
 
307
        # Format of macro that came before META:ATTACHMENT
 
308
        my $line = '';
 
309
        require Foswiki::Attrs;
 
310
        foreach $line ( split( /\r?\n/, $atext ) ) {
 
311
            if ( $line =~ /%FILEATTACHMENT{\s"([^"]*)"([^}]*)}%/ ) {
 
312
                my $name   = $1;
 
313
                my $values = new Foswiki::Attrs($2);
 
314
                $values->{name} = $name;
 
315
                $meta->putKeyed( 'FILEATTACHMENT', $values );
 
316
            }
 
317
        }
 
318
    }
 
319
 
 
320
    return $text;
 
321
}
 
322
 
 
323
=begin TML
 
324
 
 
325
---++ upgradeFrom1v0beta ( $session, $meta  ) -> $text
 
326
 
 
327
=cut
 
328
 
 
329
sub upgradeFrom1v0beta {
 
330
    my ( $session, $meta ) = @_;
 
331
    my $users = $session->{users};
 
332
    require Foswiki::Time;
 
333
 
 
334
    my @attach = $meta->find('FILEATTACHMENT');
 
335
    foreach my $att (@attach) {
 
336
        my $date = $att->{date} || 0;
 
337
        if ( $date =~ /-/ ) {
 
338
            $date =~ s/&nbsp;/ /go;
 
339
            $date = Foswiki::Time::parseTime($date);
 
340
        }
 
341
        $att->{date} = $date;
 
342
        $att->{user} = $users->webDotWikiName( $att->{user} );
 
343
    }
 
344
}
 
345
 
 
346
1;
 
347
__DATA__
 
348
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
349
#
 
350
# Copyright (C) 2008-2009 Foswiki Contributors. All Rights Reserved.
 
351
# Foswiki Contributors are listed in the AUTHORS file in the root
 
352
# of this distribution. NOTE: Please extend that file, not this notice.
 
353
#
 
354
# Additional copyrights apply to some or all of the code in this
 
355
# file as follows:
 
356
#
 
357
# Copyright (C) 1999-2007 TWiki Contributors. All Rights Reserved.
 
358
# TWiki Contributors are listed in the AUTHORS file in the root
 
359
# of this distribution. NOTE: Please extend that file, not this notice.
 
360
#
 
361
# This program is free software; you can redistribute it and/or
 
362
# modify it under the terms of the GNU General Public License
 
363
# as published by the Free Software Foundation; either version 2
 
364
# of the License, or (at your option) any later version. For
 
365
# more details read LICENSE in the root of this distribution.
 
366
#
 
367
# This program is distributed in the hope that it will be useful,
 
368
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
369
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
370
#
 
371
# As per the GPL, removal of this notice is prohibited.
 
372
#