/
Compatibility.pm
499 lines (413 loc) · 14.5 KB
/
Compatibility.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# See bottom of file for license and copyright information
package Foswiki::Compatibility;
use strict;
use warnings;
use Assert;
BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
import locale();
}
}
=begin TML
---+ package Foswiki::Compatibility
Support for compatibility with old versions. Packaged
separately because 99.999999% of the time this won't be needed.
=cut
sub _upgradeCategoryItem {
my ( $catitems, $ctext ) = @_;
my $catname = '';
my $scatname = '';
my $catmodifier = '';
my $catvalue = '';
my @cmd = split( /\|/, $catitems );
my $src = '';
my $len = @cmd;
if ( $len < '2' ) {
# FIXME
return ( $catname, $catmodifier, $catvalue );
}
my $svalue = '';
my $i;
my $itemsPerLine;
# check for CategoryName=CategoryValue parameter
my $paramCmd = '';
my $cvalue = ''; # was$query->param( $cmd[1] );
if ($cvalue) {
$src = "<!---->$cvalue<!---->";
}
elsif ($ctext) {
foreach ( split( /\r?\n/, $ctext ) ) {
if (/$cmd[1]/) {
$src = $_;
last;
}
}
}
if ( $cmd[0] eq 'select' || $cmd[0] eq 'radio' ) {
$catname = $cmd[1];
$scatname = $catname;
#$scatname =~ s/[^a-zA-Z0-9]//g;
my $size = $cmd[2];
for ( $i = 3 ; $i < $len ; $i++ ) {
my $value = $cmd[$i];
$svalue = $value;
if ( $src =~ m/$value/ ) {
$catvalue = $svalue;
}
}
}
elsif ( $cmd[0] eq 'checkbox' ) {
$catname = $cmd[1];
$scatname = $catname;
#$scatname =~ s/[^a-zA-Z0-9]//g;
if ( $cmd[2] eq 'true' || $cmd[2] eq '1' ) {
$i = $len - 4;
$catmodifier = 1;
}
$itemsPerLine = $cmd[3];
for ( $i = 4 ; $i < $len ; $i++ ) {
my $value = $cmd[$i];
$svalue = $value;
# I18N: FIXME - need to look at this, but since it's upgrading
# old forms that probably didn't use I18N, it's not a high
# priority.
if ( $src =~ m/$value[^a-zA-Z0-9\.]/ ) {
$catvalue .= ", " if ($catvalue);
$catvalue .= $svalue;
}
}
}
elsif ( $cmd[0] eq 'text' ) {
$catname = $cmd[1];
$scatname = $catname;
#$scatname =~ s/[^a-zA-Z0-9]//g;
# SMELL: unchecked implicit untaint?
$src =~ m/<!---->(.*)<!---->/;
if ($1) {
$src = $1;
}
else {
$src = '';
}
$catvalue = $src;
}
return ( $catname, $catmodifier, $catvalue );
}
=begin TML
---++ StaticMethod upgradeCategoryTable( $session, $web, $topic, $meta, $text ) -> $text
Upgrade old style category table
May throw Foswiki::OopsException
=cut
sub upgradeCategoryTable {
my ( $session, $web, $topic, $meta, $text ) = @_;
my $icat =
$session->templates->readTemplate( 'twikicatitems', no_oops => 1 );
if ($icat) {
my @items = ();
# extract category section and build category form elements
my ( $before, $ctext, $after ) = split( /<!--TWikiCat-->/, $text );
# cut TWikiCat part
$text = $before || '';
$text .= $after if ($after);
$ctext = '' if ( !$ctext );
my $ttext = '';
foreach ( split( /\r?\n/, $icat ) ) {
my ( $catname, $catmod, $catvalue ) =
_upgradeCategoryItem( $_, $ctext );
if ($catname) {
push @items, ( [ $catname, $catmod, $catvalue ] );
}
}
my $prefs = $session->{prefs};
my $webObject = Foswiki::Meta->new( $session, $web );
my $listForms = $webObject->getPreference('WEBFORMS');
$listForms =~ s/^\s*//g;
$listForms =~ s/\s*$//g;
my @formTemplates = split( /\s*,\s*/, $listForms );
my $defaultFormTemplate = '';
$defaultFormTemplate = $formTemplates[0] if (@formTemplates);
if ( !$defaultFormTemplate ) {
$session->logger->log( 'warning',
"Form: can't get form definition to convert category table "
. " for topic $web.$topic" );
foreach my $oldCat (@items) {
my $name = $oldCat->[0];
my $value = $oldCat->[2];
$meta->put( 'FORM', { name => '' } );
$meta->putKeyed(
'FIELD',
{
name => $name,
title => $name,
value => $value
}
);
}
return;
}
require Foswiki::Form;
my $def = Foswiki::Form->load( $session, $web, $defaultFormTemplate );
$meta->put( 'FORM', { name => $defaultFormTemplate } );
foreach my $fieldDef ( @{ $def->getFields() } ) {
my $value = '';
foreach my $oldCatP (@items) {
my @oldCat = @$oldCatP;
my $name = $oldCat[0] || '';
$name =~ s/[^A-Za-z0-9_\.]//g;
if ( $name eq $fieldDef->{name} ) {
$value = $oldCat[2];
last;
}
}
$meta->putKeyed(
'FIELD',
{
name => $fieldDef->{name},
title => $fieldDef->{title},
value => $value,
}
);
}
}
else {
# We used to log a warning but it only made noise and trouble
# People will not need to be warned any longer. Item1440
}
return $text;
}
#Get file attachment attributes for old html
#format.
sub _getOldAttachAttr {
my ( $session, $atext ) = @_;
my $fileName = '';
my $filePath = '';
my $fileSize = '';
my $fileDate = '';
my $fileUser = '';
my $fileComment = '';
my $before = '';
my $item = '';
my $after = '';
my $users = $session->{users};
( $before, $fileName, $after ) = split( /<(?:\/)*TwkFileName>/, $atext );
if ( !$fileName ) { $fileName = ''; }
if ($fileName) {
( $before, $filePath, $after ) =
split( /<(?:\/)*TwkFilePath>/, $atext );
if ( !$filePath ) { $filePath = ''; }
# SMELL: unchecked implicit untaint
$filePath =~ s/<TwkData value="(.*)">//g;
if ($1) { $filePath = $1; }
else { $filePath = ''; }
$filePath =~ s/\%NOP\%//gi; # delete placeholder that prevents WikiLinks
( $before, $fileSize, $after ) =
split( /<(?:\/)*TwkFileSize>/, $atext );
if ( !$fileSize ) { $fileSize = '0'; }
( $before, $fileDate, $after ) =
split( /<(?:\/)*TwkFileDate>/, $atext );
if ( !$fileDate ) {
$fileDate = '';
}
else {
$fileDate =~ s/ / /g;
require Foswiki::Time;
$fileDate = Foswiki::Time::parseTime($fileDate);
}
( $before, $fileUser, $after ) =
split( /<(?:\/)*TwkFileUser>/, $atext );
if ( !$fileUser ) {
$fileUser = '';
}
else {
$fileUser = $users->getLoginName($fileUser) if $fileUser;
}
$fileUser ||= '';
$fileUser =~ s/ //g;
( $before, $fileComment, $after ) =
split( /<(?:\/)*TwkFileComment>/, $atext );
if ( !$fileComment ) { $fileComment = ''; }
}
return ( $fileName, $filePath, $fileSize, $fileDate, $fileUser,
$fileComment );
}
=begin TML
---++ migrateToFileAttachmentMacro ( $session, $meta, $text ) -> $text
Migrate old HTML format
=cut
sub migrateToFileAttachmentMacro {
my ( $session, $meta, $text ) = @_;
ASSERT( $meta->isa('Foswiki::Meta') ) if DEBUG;
my ( $before, $atext, $after ) = split( /<!--TWikiAttachment-->/, $text );
$text = $before || '';
$text .= $after if ($after);
$atext = '' if ( !$atext );
if ( $atext =~ m/<TwkNextItem>/ ) {
my $line = '';
foreach $line ( split( /<TwkNextItem>/, $atext ) ) {
my (
$fileName, $filePath, $fileSize,
$fileDate, $fileUser, $fileComment
) = _getOldAttachAttr( $session, $line );
if ($fileName) {
$meta->putKeyed(
'FILEATTACHMENT',
{
name => $fileName,
version => '',
path => $filePath,
size => $fileSize,
date => $fileDate,
user => $fileUser,
comment => $fileComment,
attr => ''
}
);
}
}
}
else {
# Format of macro that came before META:ATTACHMENT
my $line = '';
require Foswiki::Attrs;
foreach $line ( split( /\r?\n/, $atext ) ) {
if ( $line =~ m/%FILEATTACHMENT\{\s"([^"]*)"([^}]*)\}%/ ) {
my $name = $1;
my $values = new Foswiki::Attrs($2);
$values->{name} = $name;
$meta->putKeyed( 'FILEATTACHMENT', $values );
}
}
}
return $text;
}
=begin TML
---++ upgradeFrom1v0beta ( $session, $meta ) -> $text
=cut
sub upgradeFrom1v0beta {
my ( $session, $meta ) = @_;
my $users = $session->{users};
require Foswiki::Time;
my @attach = $meta->find('FILEATTACHMENT');
foreach my $att (@attach) {
my $date = $att->{date} || 0;
if ( $date =~ m/-/ ) {
$date =~ s/ / /g;
$date = Foswiki::Time::parseTime($date);
}
$att->{date} = $date;
$att->{user} = $users->webDotWikiName( $att->{user} );
}
}
# Read meta-data encoded using the discredited symmetrical encoding
# method from pre 1.1
sub readSymmetricallyEncodedMETA {
my ( $meta, $type, $args ) = @_;
my $keys = {};
$args =~ s/\s*([^=]+)="([^"]*)"/
_symmetricalDataDecode( $1, $2, $keys )/ge;
if ( defined( $keys->{name} ) ) {
# don't attempt to save it keyed unless it has a name
$meta->putKeyed( $type, $keys );
}
else {
$meta->put( $type, $keys );
}
return 1;
}
sub _symmetricalDataDecode {
my ( $key, $value, $res ) = @_;
# Old decoding retained for backward compatibility.
# This encoding is badly broken, because the encoded
# symbols are symmetrical, and use an encoded symbol (%).
$value =~ s/%_N_%/\n/g;
$value =~ s/%_Q_%/\"/g;
$value =~ s/%_P_%/%/g;
$res->{$key} = $value;
return '';
}
# IF cfg{RequireCompatibleAnchors}
# Return a list of alternative anchor names generated using old generations
# of anchor name generator
sub makeCompatibleAnchors {
my ($text) = @_;
my @anchors;
# Use the old algorithm to generate the old style, non-unique, anchor
# target.
my $badAnchor = _makeBadAnchorName( $text, 0 );
push( @anchors, $badAnchor ),
# There's an even older algorithm we have to allow for
my $worseAnchor = _makeBadAnchorName( $text, 1 );
if ( $worseAnchor ne $badAnchor ) {
push( @anchors, $worseAnchor ),;
}
return @anchors;
}
# Make an anchor name using the seriously flawed (tm)Wiki anchor generation
# algorithm(s). This code is taken verbatim from Foswiki 1.0.4.
sub _makeBadAnchorName {
my ( $anchorName, $compatibilityMode ) = @_;
if ( !$compatibilityMode
&& $anchorName =~ m/^$Foswiki::regex{anchorRegex}$/ )
{
# accept, already valid -- just remove leading #
return substr( $anchorName, 1 );
}
# strip out potential links so they don't get rendered.
# remove double bracket link
$anchorName =~ s/\[(?:\[.*?\])?\[(.*?)\]\s*\]/$1/g;
# add an _ before bare WikiWords
$anchorName =~ s/($Foswiki::regex{wikiWordRegex})/_$1/g;
if ($compatibilityMode) {
# remove leading/trailing underscores first, allowing them to be
# reintroduced
$anchorName =~ s/^[\s#_]*//;
$anchorName =~ s/[\s_]*$//;
}
$anchorName =~ s/<\/?[a-zA-Z][^>]*>//gi; # remove HTML tags
$anchorName =~ s/&#?[a-zA-Z0-9]+;//g; # remove HTML entities
$anchorName =~ s/&//g; # remove &
# filter TOC excludes if not at beginning
$anchorName =~ s/^(.+?)\s*$Foswiki::regex{headerPatternNoTOC}.*/$1/;
# filter '!!', '%NOTOC%'
$anchorName =~ s/$Foswiki::regex{headerPatternNoTOC}//;
# No matter what character set we use, the HTML standard does not allow
# anything else than English alphanum characters in anchors
# So we convert anything non A-Za-z0-9_ to underscores
# and limit the number consecutive of underscores to 1
# This means that pure non-English anchors will become A, A_AN1, A_AN2, ...
# We accept anchors starting with 0-9. It is non RFC but it works and it
# is very important for compatibility
$anchorName =~ s/[^A-Za-z0-9]+/_/g;
$anchorName =~ s/__+/_/g; # remove excessive '_' chars
if ( !$compatibilityMode ) {
$anchorName =~ s/^[\s#_]+//; # no leading space nor '#', '_'
}
$anchorName =~ s/^$/A/; # prevent empty anchor
# limit to 32 chars
$anchorName =~ s/^(.{32})(.*)$/$1/;
if ( !$compatibilityMode ) {
$anchorName =~ s/[\s_]+$//; # no trailing space, nor '_'
}
return $anchorName;
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
Additional copyrights apply to some or all of the code in this
file as follows:
Copyright (C) 1999-2007 TWiki Contributors. All Rights Reserved.
TWiki 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.