-
Notifications
You must be signed in to change notification settings - Fork 38
/
HistoryPlugin.pm
executable file
·270 lines (216 loc) · 8.29 KB
/
HistoryPlugin.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
# See bottom of file for license and copyright information
package Foswiki::Plugins::HistoryPlugin;
use strict;
use warnings;
use Foswiki::Func ();
use Error qw(:try);
use Foswiki::AccessControlException ();
# =========================
use vars qw( $VERSION $RELEASE $NO_PREFS_IN_TOPIC $SHORTDESCRIPTION);
$VERSION = '$Rev: 15950 $';
$RELEASE = '1.9';
$NO_PREFS_IN_TOPIC = 1;
$SHORTDESCRIPTION = 'Shows a complete history of a topic';
# =========================
sub initPlugin {
# check for Plugins.pm versions
if ( $Foswiki::Plugins::VERSION < 1.021 ) {
Foswiki::Func::writeWarning(
"Version mismatch between HistoryPlugin and Plugins.pm");
return 0;
}
Foswiki::Func::registerTagHandler( 'HISTORY', \&_handleHistory );
return 1;
}
sub _handleHistory {
my ( $session, $params, $theTopic, $theWeb ) = @_;
my $web = $params->{web} || $theWeb;
my $topic = $params->{topic} || $theTopic;
( $web, $topic ) = Foswiki::Func::normalizeWebTopicName( $web, $topic );
# check topic exists
unless ( Foswiki::Func::topicExists( $web, $topic ) ) {
return
"<noautolink><span class='foswikiAlert'>HistoryPlugin error: Topic $web.$topic does not exist</noautolink>";
}
# check access permissions
unless (
Foswiki::Func::checkAccessPermission(
"VIEW", $session->{user}, undef, $topic, $web
)
)
{
throw Foswiki::AccessControlException( "VIEW", $session->{user}, $web,
$topic, $Foswiki::Meta::reason );
}
my $reverse = Foswiki::Func::isTrue( $params->{reverse}, 1 );
my $versions = $params->{versions};
my $versionStart;
my $versionEnd;
my $maxrev = ( Foswiki::Func::getRevisionInfo( $web, $topic ) )[2];
my $rev1 = $params->{rev1} ? $params->{rev1} : 1;
my $rev2 = $params->{rev2} ? $params->{rev2} : $maxrev;
my $nrev = $params->{nrev} ? $params->{nrev} : 10;
if ($versions) {
$versions =~ /([0-9\-]*)(\.\.)*([0-9\-]*)/;
if ( defined $1 && length $1 ) {
$rev1 = $1;
}
if ( defined $2 && length $2 ) {
# dots
$rev2 = $3 if ( defined $3 && length $3 );
}
else {
$rev2 = $rev1;
}
# wrap
$rev1 = $maxrev + $rev1 if $rev1 < 0;
$rev2 = $maxrev + $rev2 if $rev2 < 0;
# normalize
$rev1 = 1 if $rev1 == 0;
$rev2 = 1 if $rev2 == 0;
$rev1 = $maxrev if $rev1 > $maxrev;
$rev2 = $maxrev if $rev2 > $maxrev;
if ( $rev1 > $rev2 ) {
$reverse = 0;
( $rev1, $rev2 ) = ( $rev2, $rev1 );
}
}
else {
# deprecated syntax
$rev1 = $params->{rev1};
$rev1 =~ s/1\.// if $rev1;
$rev2 = $params->{rev2};
$rev2 =~ s/1\.// if $rev2;
$nrev = $params->{nrev} || 10;
$rev2 ||= $rev1 ? $rev1 + $nrev - 1 : $maxrev;
$rev1 ||= $rev2 - $nrev + 1;
( $rev1, $rev2 ) = ( $rev2, $rev1 ) if $rev1 > $rev2;
$rev1 = $maxrev if $rev1 > $maxrev;
$rev1 = 1 if $rev1 < 1;
$rev2 = $maxrev if $rev2 > $maxrev;
$rev2 = 1 if $rev2 < 1;
}
my $format =
$params->{format}
|| $params->{_DEFAULT}
|| 'r$rev - $date - $wikiusername%BR%';
my $header = $params->{header};
$header = "\$next{'...'}%BR%" unless defined($header);
my $footer = $params->{footer};
$footer = "\$previous{'...'}" unless defined($footer);
Foswiki::Func::setPreferencesValue( "HISTORY_MAXREV", $maxrev );
Foswiki::Func::setPreferencesValue( "HISTORY_REV1", $rev1 );
Foswiki::Func::setPreferencesValue( "HISTORY_REV2", $rev2 );
Foswiki::Func::setPreferencesValue( "HISTORY_NREV", $nrev );
# Start the output
my $out = _handleHeadFoot( $header, $rev1, $rev2, $nrev, $maxrev );
# Print revision info
my @revs = ( $rev1 .. $rev2 );
@revs = reverse(@revs) if $reverse;
my $mixedAlphaNum = Foswiki::Func::getRegularExpression('mixedAlphaNum');
my $checkFlag = 0;
foreach my $rev (@revs) {
my ( $date, $user, $revout, $comment ) =
Foswiki::Func::getRevisionInfo( $web, $topic, $rev );
my $wikiName = Foswiki::Func::userToWikiName( $user, 1 );
my $wikiUserName = Foswiki::Func::userToWikiName( $user, 0 );
my $revinfo = $format;
my $checked1 = '';
my $checked2 = '';
$checked1 = 'checked' if $checkFlag == 0;
$checked2 = 'checked' if $checkFlag == 1;
$checkFlag++;
$revinfo =~ s/\$web/$web/g;
$revinfo =~ s/\$topic/$topic/g;
$revinfo =~ s/\$rev/$rev/g;
$revinfo =~ s/\$date/Foswiki::Func::formatTime($date)/ge;
$revinfo =~
s/\$(year|ye|week|web|wday|tz|topic|time|seconds|rev|rcs|month|mo|minutes|longdate|isotz|iso|http|hours|epoch|email|dow|day)/_formatTime("\$$1", $topic, $web)/ge;
$revinfo =~ s/\$username/$user/g;
$revinfo =~ s/\$wikiname/$wikiName/g;
$revinfo =~ s/\$wikiusername/$wikiUserName/g;
$revinfo =~ s/\$checked1/$checked1/g;
$revinfo =~ s/\$checked2/$checked2/g;
# This space to tabs conversion must be for Cairo compatibility
$revinfo =~ s|^(( )+)|"\t" x (length($1)/3)|e;
$out .= $revinfo . "\n";
$rev--;
}
$out .= _handleHeadFoot( $footer, $rev1, $rev2, $nrev, $maxrev );
$out = Foswiki::Func::decodeFormatTokens($out);
return $out;
}
sub _formatTime {
my ( $format, $topic, $web ) = @_;
return Foswiki::Func::expandCommonVariables( '%REVINFO{"' . $format . '"}%',
$topic, $web );
}
sub _handleHeadFoot {
my ( $text, $rev1, $rev2, $nrev, $maxrev ) = @_;
if ( $rev2 >= $maxrev ) {
$text =~ s/\$next({.*?})//g;
}
else {
while ( $text =~ /\$next({(.*?)})/ ) {
my $args = $2 || '';
my $newrev1 = $rev2 < $maxrev ? $rev2 + 1 : $rev2;
my $newrev2 = $newrev1 + $nrev - 1;
$newrev2 = $maxrev if $newrev2 > $maxrev;
$args =~ s/'/"/g;
$args =~ s/\$rev1/$newrev1/g;
$args =~ s/\$rev2/$newrev2/g;
$args =~ s/\$nrev/$nrev/g;
my %params = Foswiki::Func::extractParameters($args);
my $newtext = $params{text} || $params{_DEFAULT} || '';
my $url = $params{url} || '';
my $replace =
$url
? "<a href='$url' class='foswikiButton'>$newtext</a>"
: $newtext;
$text =~ s/\$next({.*?})/$replace/;
}
}
if ( $rev1 <= 1 ) {
$text =~ s/\$previous({.*?})//g;
}
else {
while ( $text =~ /\$previous({(.*?)})/ ) {
my $args = $2 || '';
my $newrev2 = $rev1 > 1 ? $rev1 - 1 : 1;
my $newrev1 = $newrev2 - $nrev + 1;
$newrev1 = 1 if $newrev1 < 1;
$args =~ s/'/"/g;
$args =~ s/\$rev1/$newrev1/g;
$args =~ s/\$rev2/$newrev2/g;
$args =~ s/\$nrev/$nrev/g;
my %params = Foswiki::Func::extractParameters($args);
my $newtext = $params{text} || $params{_DEFAULT} || '';
my $url = $params{url} || '';
my $replace =
$url
? "<a href='$url' class='foswikiButton'>$newtext</a>"
: $newtext;
$text =~ s/\$previous({.*?})/$replace/;
}
}
return $text;
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2011 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) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it
Copyright (C) 2001-2007 Peter Thoeny, peter@thoeny.com
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.