-
Notifications
You must be signed in to change notification settings - Fork 0
/
RevCommentPlugin.pm
executable file
·285 lines (232 loc) · 8.67 KB
/
RevCommentPlugin.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
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2005 by TWiki:Main.JChristophFuchs
# Copyright (C) 2008-2015 Foswiki Contributors
#
# 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.
#
# 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. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
#
# =========================
package Foswiki::Plugins::RevCommentPlugin;
use strict;
use warnings;
use Foswiki::Func ();
use Foswiki::Plugins ();
use Foswiki::Meta ();
# =========================
use vars qw(
$web $topic $user $installWeb $pluginName $debug
);
use vars qw(
$commentFromUpload $attachmentComments $cachedCommentWeb $cachedCommentTopic $minorMark
);
our $VERSION = '2.01';
our $RELEASE = '01 Sep 2015';
our $SHORTDESCRIPTION =
'Allows a short summary of changes to be entered for a new revision.';
$pluginName = 'RevCommentPlugin';
$minorMark = '%MINOR%';
# =========================
sub initPlugin {
( $topic, $web, $user, $installWeb ) = @_;
# check for Plugins.pm versions
if ( $Foswiki::Plugins::VERSION < 1.021 ) {
Foswiki::Func::writeWarning(
"Version mismatch between $pluginName and Plugins.pm");
return 0;
}
$commentFromUpload = undef;
# Get plugin debug flag
$debug = Foswiki::Func::getPluginPreferencesFlag("DEBUG");
# Get plugin preferences, the variable defined by: * Set EXAMPLE = ...
$attachmentComments =
Foswiki::Func::getPluginPreferencesValue("ATTACHMENT_COMMENTS") || 1;
$cachedCommentWeb = '';
$cachedCommentTopic = '';
# Need to register meta, Item11249
# SMELL: Foswiki 2.0 meta strictly enforces attribut names. This plugin indexes these names
# with relative comment number: comment_1, comment_2, etc. and the META:REVCOMMENT lines are
# ignored. Needed to remove the definition: allow => [qw(comment t minor rev ncomments)] );
if ( defined &Foswiki::Meta::registerMETA ) {
Foswiki::Meta::registerMETA('REVCOMMENT');
}
# Plugin correctly initialized
Foswiki::Func::writeDebug(
"- Foswiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK")
if $debug;
Foswiki::Func::writeDebug(
"- --- attachmentComments = " . $attachmentComments )
if $debug;
return 1;
}
sub commonTagsHandler {
### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead
Foswiki::Func::writeDebug(
"- ${pluginName}::commonTagsHandler( $_[2].$_[1] )")
if $debug;
# This is the place to define customized tags and variables
# Called by Foswiki::handleCommonTags, after %INCLUDE:"..."%
$_[0] =~ s/%REVCOMMENT%/&handleRevComment()/ge;
$_[0] =~ s/%REVCOMMENT\{(.*?)\}%/&handleRevComment($1)/ge;
$_[0] =~ s/%REVCOMMENT\[(.*?)\]%/&handleRevComment($1)/ge;
}
sub beforeSaveHandler {
### my ( $text, $topic, $web, $meta ) = @_; # do not uncomment, use $_[0], $_[1]... instead
my ( $topic, $web, $meta ) = @_[ 1 .. 3 ];
Foswiki::Func::writeDebug(
"- ${pluginName}::beforeSaveHandler( $_[2].$_[1] )")
if $debug;
# This handler is called by Foswiki::Store::saveTopic just before the save action.
# New hook in Foswiki::Plugins $VERSION = '1.010'
my $query = Foswiki::Func::getCgiQuery();
# Get current revision
my ( $date, $user, $currev ) =
Foswiki::Func::getRevisionInfo( $_[2], $_[1] );
$currev ||= 0;
my @comments = _extractComments($meta);
# Set correct rev of comment
foreach my $comment (@comments) {
$comment->{rev} = $currev unless $comment->{rev} =~ /\d+/;
}
# Delete old comments
@comments = grep { $_->{rev} >= $currev } @comments;
# Check for new comments
my $newComment;
if ($commentFromUpload) { # File upload
$newComment = {
comment => $commentFromUpload,
t => 'Upload' . time(),
minor => 0,
rev => undef,
};
}
elsif ($attachmentComments
&& $query->url( -relative ) =~ /upload/ )
{ # Attachment changed
$newComment = {
comment => 'Changed properties for attachment !'
. $query->param('filename'),
t => 'PropChanged' . time(),
minor => 0,
rev => undef,
};
}
elsif ( $query->param('revcomment') || $query->param('dontnotify') ) {
my $commentFromForm = $query->param('revcomment') || ' ';
my $t = $query->param('t') || time();
$newComment = {
comment => $commentFromForm,
t => $t,
minor => defined $query->param('dontnotify'),
rev => undef,
};
}
if ( ( $newComment->{comment} || '' ) =~ /\S/
|| ( $newComment->{minor} && !@comments ) )
{
push @comments, $newComment;
}
$meta->remove('REVCOMMENT');
_putComments( $meta, @comments );
}
sub beforeAttachmentSaveHandler {
Foswiki::Func::writeDebug(
"- ${pluginName}::beforeAttachmentSaveHandler( $_[2].$_[1] )")
if $debug;
return unless $attachmentComments;
Foswiki::Func::writeDebug("--- still here") if $debug;
my $query = Foswiki::Func::getCgiQuery();
if ( defined( $query->param('filename') )
&& $query->param('filename') eq $_[0]->{attachment} )
{
$commentFromUpload = 'Updated attachment !' . $_[0]->{attachment};
}
else {
$commentFromUpload = 'Attached file !' . $_[0]->{attachment};
}
}
# =========================
sub _extractComments {
my $meta = shift;
my @comments = ();
if ( my $code = $meta->get('REVCOMMENT') ) {
for ( my $i = 1 ; $i <= $code->{ncomments} ; ++$i ) {
push @comments,
{
minor => $code->{ 'minor_' . $i },
comment => $code->{ 'comment_' . $i },
t => $code->{ 't_' . $i },
rev => $code->{ 'rev_' . $i },
};
}
}
return @comments;
}
sub _putComments {
my $meta = shift;
my @comments = @_;
my %args = ( ncomments => scalar @comments, );
for ( my $i = 1 ; $i <= scalar @comments ; ++$i ) {
$args{ 'comment_' . $i } = $comments[ $i - 1 ]->{comment};
$args{ 't_' . $i } = $comments[ $i - 1 ]->{t};
$args{ 'minor_' . $i } = $comments[ $i - 1 ]->{minor};
$args{ 'rev_' . $i } = $comments[ $i - 1 ]->{rev};
}
$meta->put( 'REVCOMMENT', \%args );
}
sub handleRevComment {
Foswiki::Func::writeDebug(
"- Foswiki::Plugins::${pluginName}::handleRevComments: Args=>$_[0]<\n")
if $debug;
my $params = $_[0] || '';
# SMELL: this "convenience" should probably be removed; you can \" in Attributes
$params =~ s/''/"/g;
my %params = Foswiki::Func::extractParameters($params);
my $web = $params{web} || $web;
my $topic = $params{topic} || $topic;
my $rev =
$params{rev}
|| $params{_DEFAULT}
|| ( Foswiki::Func::getRevisionInfo( $web, $topic ) )[2];
$rev =~ s/^1\.//;
my $delimiter = $params{delimiter};
$delimiter = '</li><li style="margin-left:-1em;">'
unless defined($delimiter);
$delimiter =~ s/\\n/\n/g;
$delimiter =~ s/\\t/\t/g;
my $pre = $params{pre};
$pre = '<noautolink><ul><li style="margin-left:-1em;">'
unless defined($pre);
my $post = $params{post};
$post = '</li></ul></noautolink>' unless defined($post);
my $minor = $params{minor};
$minor = '<i>(minor)</i> ' unless defined($minor);
unless ( Foswiki::Func::topicExists( $web, $topic ) ) {
return "Topic $web.$topic does not exist";
}
my @comments;
# SMELL: doesn't respect access permissions (too bad there isn't a version that does, like readTopic() does...)
my ( $meta, undef ) = Foswiki::Func::readTopic( $web, $topic, $rev );
@comments = _extractComments($meta);
foreach my $comment (@comments) {
$comment->{rev} = $rev unless $comment->{rev} =~ /\d+/;
}
@comments = grep { $_->{rev} == $rev } @comments;
map { $_->{comment} = $minorMark . $_->{comment} if $_->{minor} } @comments;
@comments = map { $_->{comment} } @comments;
my $text =
scalar @comments > 0
? $pre . join( $delimiter, @comments ) . $post
: '';
$text =~ s/$minorMark/$minor/g;
return $text;
}
1;