Filename | /var/www/foswiki11/lib/Foswiki/Plugins/RevCommentPlugin.pm |
Statements | Executed 75 statements in 1.48ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
13 | 1 | 1 | 197µs | 197µs | commonTagsHandler | Foswiki::Plugins::RevCommentPlugin::
1 | 1 | 1 | 31µs | 212µs | initPlugin | Foswiki::Plugins::RevCommentPlugin::
1 | 1 | 1 | 14µs | 27µs | BEGIN@20 | Foswiki::Plugins::RevCommentPlugin::
1 | 1 | 1 | 12µs | 48µs | BEGIN@30 | Foswiki::Plugins::RevCommentPlugin::
1 | 1 | 1 | 9µs | 68µs | BEGIN@25 | Foswiki::Plugins::RevCommentPlugin::
1 | 1 | 1 | 6µs | 6µs | BEGIN@22 | Foswiki::Plugins::RevCommentPlugin::
0 | 0 | 0 | 0s | 0s | _extractComments | Foswiki::Plugins::RevCommentPlugin::
0 | 0 | 0 | 0s | 0s | _putComments | Foswiki::Plugins::RevCommentPlugin::
0 | 0 | 0 | 0s | 0s | beforeAttachmentSaveHandler | Foswiki::Plugins::RevCommentPlugin::
0 | 0 | 0 | 0s | 0s | beforeSaveHandler | Foswiki::Plugins::RevCommentPlugin::
0 | 0 | 0 | 0s | 0s | handleRevComment | Foswiki::Plugins::RevCommentPlugin::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ | ||||
2 | # | ||||
3 | # Copyright (C) 2005 by TWiki:Main.JChristophFuchs | ||||
4 | # Copyright (C) 2008 Foswiki Contributors | ||||
5 | # | ||||
6 | # This program is free software; you can redistribute it and/or | ||||
7 | # modify it under the terms of the GNU General Public License | ||||
8 | # as published by the Free Software Foundation; either version 2 | ||||
9 | # of the License, or (at your option) any later version. | ||||
10 | # | ||||
11 | # This program is distributed in the hope that it will be useful, | ||||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
14 | # GNU General Public License for more details, published at | ||||
15 | # http://www.gnu.org/copyleft/gpl.html | ||||
16 | # | ||||
17 | |||||
18 | # ========================= | ||||
19 | package Foswiki::Plugins::RevCommentPlugin; | ||||
20 | 2 | 28µs | 2 | 41µs | # spent 27µs (14+14) within Foswiki::Plugins::RevCommentPlugin::BEGIN@20 which was called:
# once (14µs+14µs) by Foswiki::Plugin::BEGIN@2.28 at line 20 # spent 27µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@20
# spent 14µs making 1 call to strict::import |
21 | |||||
22 | 2 | 36µs | 1 | 6µs | # spent 6µs within Foswiki::Plugins::RevCommentPlugin::BEGIN@22 which was called:
# once (6µs+0s) by Foswiki::Plugin::BEGIN@2.28 at line 22 # spent 6µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@22 |
23 | |||||
24 | # ========================= | ||||
25 | 1 | 6µs | 1 | 59µs | # spent 68µs (9+59) within Foswiki::Plugins::RevCommentPlugin::BEGIN@25 which was called:
# once (9µs+59µs) by Foswiki::Plugin::BEGIN@2.28 at line 28 # spent 59µs making 1 call to vars::import |
26 | $web $topic $user $installWeb $VERSION $RELEASE $pluginName | ||||
27 | $debug | ||||
28 | 1 | 33µs | 1 | 68µs | ); # spent 68µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@25 |
29 | |||||
30 | 1 | 6µs | 1 | 36µs | # spent 48µs (12+36) within Foswiki::Plugins::RevCommentPlugin::BEGIN@30 which was called:
# once (12µs+36µs) by Foswiki::Plugin::BEGIN@2.28 at line 32 # spent 36µs making 1 call to vars::import |
31 | $commentFromUpload $attachmentComments $cachedCommentWeb $cachedCommentTopic $minorMark | ||||
32 | 1 | 1.16ms | 1 | 48µs | ); # spent 48µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@30 |
33 | |||||
34 | # This should always be $Rev: 2125 (22 Jan 2009) $ so that Foswiki can determine the checked-in | ||||
35 | # status of the plugin. It is used by the build automation tools, so | ||||
36 | # you should leave it alone. | ||||
37 | 1 | 700ns | $VERSION = '$Rev: 2125 (22 Jan 2009) $'; | ||
38 | |||||
39 | # This is a free-form string you can use to "name" your own plugin version. | ||||
40 | # It is *not* used by the build automation tools, but is reported as part | ||||
41 | # of the version number in PLUGINDESCRIPTIONS. | ||||
42 | 1 | 200ns | $RELEASE = '22 Jan 2009'; | ||
43 | |||||
44 | 1 | 100ns | $pluginName = 'RevCommentPlugin'; | ||
45 | |||||
46 | 1 | 200ns | $minorMark = '%MINOR%'; | ||
47 | |||||
48 | # ========================= | ||||
49 | # spent 212µs (31+180) within Foswiki::Plugins::RevCommentPlugin::initPlugin which was called:
# once (31µs+180µs) by Foswiki::Plugin::__ANON__[/var/www/foswiki11/lib/Foswiki/Plugin.pm:241] at line 234 of /var/www/foswiki11/lib/Foswiki/Plugin.pm | ||||
50 | 1 | 3µs | ( $topic, $web, $user, $installWeb ) = @_; | ||
51 | |||||
52 | # check for Plugins.pm versions | ||||
53 | 1 | 16µs | 1 | 9µs | if ( $Foswiki::Plugins::VERSION < 1.021 ) { # spent 9µs making 1 call to version::vxs::VCMP |
54 | Foswiki::Func::writeWarning( | ||||
55 | "Version mismatch between $pluginName and Plugins.pm"); | ||||
56 | return 0; | ||||
57 | } | ||||
58 | |||||
59 | 1 | 900ns | $commentFromUpload = undef; | ||
60 | |||||
61 | # Get plugin debug flag | ||||
62 | 1 | 3µs | 1 | 101µs | $debug = Foswiki::Func::getPluginPreferencesFlag("DEBUG"); # spent 101µs making 1 call to Foswiki::Func::getPluginPreferencesFlag |
63 | |||||
64 | # Get plugin preferences, the variable defined by: * Set EXAMPLE = ... | ||||
65 | 1 | 4µs | 1 | 70µs | $attachmentComments = # spent 70µs making 1 call to Foswiki::Func::getPluginPreferencesValue |
66 | Foswiki::Func::getPluginPreferencesValue("ATTACHMENT_COMMENTS") || 1; | ||||
67 | |||||
68 | 1 | 500ns | $cachedCommentWeb = ''; | ||
69 | 1 | 200ns | $cachedCommentTopic = ''; | ||
70 | |||||
71 | # Plugin correctly initialized | ||||
72 | 1 | 400ns | Foswiki::Func::writeDebug( | ||
73 | "- Foswiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK") | ||||
74 | if $debug; | ||||
75 | 1 | 200ns | Foswiki::Func::writeDebug( | ||
76 | "- --- attachmentComments = " . $attachmentComments ) | ||||
77 | if $debug; | ||||
78 | 1 | 4µs | return 1; | ||
79 | } | ||||
80 | |||||
81 | # spent 197µs within Foswiki::Plugins::RevCommentPlugin::commonTagsHandler which was called 13 times, avg 15µs/call:
# 13 times (197µs+0s) by Foswiki::Plugin::invoke at line 294 of /var/www/foswiki11/lib/Foswiki/Plugin.pm, avg 15µs/call | ||||
82 | ### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead | ||||
83 | |||||
84 | 13 | 5µs | Foswiki::Func::writeDebug("- ${pluginName}::commonTagsHandler( $_[2].$_[1] )") | ||
85 | if $debug; | ||||
86 | |||||
87 | # This is the place to define customized tags and variables | ||||
88 | # Called by Foswiki::handleCommonTags, after %INCLUDE:"..."% | ||||
89 | |||||
90 | 13 | 51µs | $_[0] =~ s/%REVCOMMENT%/&handleRevComment()/ge; | ||
91 | 13 | 39µs | $_[0] =~ s/%REVCOMMENT{(.*?)}%/&handleRevComment($1)/ge; | ||
92 | 13 | 77µs | $_[0] =~ s/%REVCOMMENT\[(.*?)\]%/&handleRevComment($1)/ge; | ||
93 | } | ||||
94 | |||||
95 | sub beforeSaveHandler { | ||||
96 | ### my ( $text, $topic, $web, $meta ) = @_; # do not uncomment, use $_[0], $_[1]... instead | ||||
97 | my ( $topic, $web, $meta ) = @_[ 1 .. 3 ]; | ||||
98 | |||||
99 | Foswiki::Func::writeDebug("- ${pluginName}::beforeSaveHandler( $_[2].$_[1] )") | ||||
100 | if $debug; | ||||
101 | |||||
102 | # This handler is called by Foswiki::Store::saveTopic just before the save action. | ||||
103 | # New hook in Foswiki::Plugins $VERSION = '1.010' | ||||
104 | |||||
105 | my $query = Foswiki::Func::getCgiQuery(); | ||||
106 | |||||
107 | # Get current revision | ||||
108 | my ( $date, $user, $currev ) = Foswiki::Func::getRevisionInfo( $_[2], $_[1] ); | ||||
109 | $currev ||= 0; | ||||
110 | |||||
111 | my @comments = _extractComments($meta); | ||||
112 | |||||
113 | # Set correct rev of comment | ||||
114 | foreach my $comment (@comments) { | ||||
115 | $comment->{rev} = $currev unless $comment->{rev} =~ /\d+/; | ||||
116 | } | ||||
117 | |||||
118 | # Delete old comments | ||||
119 | @comments = grep { $_->{rev} >= $currev } @comments; | ||||
120 | |||||
121 | # Check for new comments | ||||
122 | my $newComment; | ||||
123 | if ($commentFromUpload) { # File upload | ||||
124 | $newComment = { | ||||
125 | comment => $commentFromUpload, | ||||
126 | t => 'Upload' . time(), | ||||
127 | minor => 0, | ||||
128 | rev => undef, | ||||
129 | }; | ||||
130 | } | ||||
131 | elsif ($attachmentComments | ||||
132 | && $query->url( -relative ) =~ /upload/ ) | ||||
133 | { # Attachment changed | ||||
134 | $newComment = { | ||||
135 | comment => 'Changed properties for attachment !' | ||||
136 | . $query->param('filename'), | ||||
137 | t => 'PropChanged' . time(), | ||||
138 | minor => 0, | ||||
139 | rev => undef, | ||||
140 | }; | ||||
141 | } | ||||
142 | elsif ( $query->param('comment') || $query->param('dontnotify') ) { | ||||
143 | my $commentFromForm = $query->param('comment') || ' '; | ||||
144 | my $t = $query->param('t') || time(); | ||||
145 | |||||
146 | $newComment = { | ||||
147 | comment => $commentFromForm, | ||||
148 | t => $t, | ||||
149 | minor => defined $query->param('dontnotify'), | ||||
150 | rev => undef, | ||||
151 | }; | ||||
152 | } | ||||
153 | |||||
154 | if ( ( $newComment->{comment} || '' ) =~ /\S/ | ||||
155 | || ( $newComment->{minor} && !@comments ) ) | ||||
156 | { | ||||
157 | push @comments, $newComment; | ||||
158 | } | ||||
159 | $meta->remove('REVCOMMENT'); | ||||
160 | _putComments( $meta, @comments ); | ||||
161 | } | ||||
162 | |||||
163 | sub beforeAttachmentSaveHandler { | ||||
164 | Foswiki::Func::writeDebug( | ||||
165 | "- ${pluginName}::beforeAttachmentSaveHandler( $_[2].$_[1] )") | ||||
166 | if $debug; | ||||
167 | |||||
168 | return unless $attachmentComments; | ||||
169 | Foswiki::Func::writeDebug("--- still here") if $debug; | ||||
170 | |||||
171 | my $query = Foswiki::Func::getCgiQuery(); | ||||
172 | if ( defined( $query->param('filename') ) | ||||
173 | && $query->param('filename') eq $_[0]->{attachment} ) | ||||
174 | { | ||||
175 | |||||
176 | $commentFromUpload = 'Updated attachment !' . $_[0]->{attachment}; | ||||
177 | } | ||||
178 | else { | ||||
179 | $commentFromUpload = 'Attached file !' . $_[0]->{attachment}; | ||||
180 | } | ||||
181 | } | ||||
182 | |||||
183 | # ========================= | ||||
184 | |||||
185 | sub _extractComments { | ||||
186 | |||||
187 | my $meta = shift; | ||||
188 | my @comments = (); | ||||
189 | |||||
190 | if ( my $code = $meta->get('REVCOMMENT') ) { | ||||
191 | for ( my $i = 1 ; $i <= $code->{ncomments} ; ++$i ) { | ||||
192 | push @comments, | ||||
193 | { | ||||
194 | minor => $code->{ 'minor_' . $i }, | ||||
195 | comment => $code->{ 'comment_' . $i }, | ||||
196 | t => $code->{ 't_' . $i }, | ||||
197 | rev => $code->{ 'rev_' . $i }, | ||||
198 | }; | ||||
199 | } | ||||
200 | } | ||||
201 | |||||
202 | return @comments; | ||||
203 | } | ||||
204 | |||||
205 | sub _putComments { | ||||
206 | |||||
207 | my $meta = shift; | ||||
208 | my @comments = @_; | ||||
209 | my %args = ( ncomments => scalar @comments, ); | ||||
210 | |||||
211 | for ( my $i = 1 ; $i <= scalar @comments ; ++$i ) { | ||||
212 | $args{ 'comment_' . $i } = $comments[ $i - 1 ]->{comment}; | ||||
213 | $args{ 't_' . $i } = $comments[ $i - 1 ]->{t}; | ||||
214 | $args{ 'minor_' . $i } = $comments[ $i - 1 ]->{minor}; | ||||
215 | $args{ 'rev_' . $i } = $comments[ $i - 1 ]->{rev}; | ||||
216 | } | ||||
217 | |||||
218 | $meta->put( 'REVCOMMENT', \%args ); | ||||
219 | } | ||||
220 | |||||
221 | sub handleRevComment { | ||||
222 | |||||
223 | Foswiki::Func::writeDebug( | ||||
224 | "- Foswiki::Plugins::${pluginName}::handleRevComments: Args=>$_[0]<\n") | ||||
225 | if $debug; | ||||
226 | my $params = $_[0] || ''; | ||||
227 | |||||
228 | # SMELL: this "convenience" should probably be removed; you can \" in Attributes | ||||
229 | $params =~ s/''/"/g; | ||||
230 | |||||
231 | my %params = Foswiki::Func::extractParameters($params); | ||||
232 | |||||
233 | my $web = $params{web} || $web; | ||||
234 | my $topic = $params{topic} || $topic; | ||||
235 | my $rev = $params{rev} | ||||
236 | || $params{_DEFAULT} | ||||
237 | || ( Foswiki::Func::getRevisionInfo( $web, $topic ) )[2]; | ||||
238 | $rev =~ s/^1\.//; | ||||
239 | my $delimiter = $params{delimiter}; | ||||
240 | $delimiter = '</li><li style="margin-left:-1em;">' | ||||
241 | unless defined($delimiter); | ||||
242 | $delimiter =~ s/\\n/\n/g; | ||||
243 | $delimiter =~ s/\\t/\t/g; | ||||
244 | my $pre = $params{pre}; | ||||
245 | $pre = '<noautolink><ul><li style="margin-left:-1em;">' unless defined($pre); | ||||
246 | my $post = $params{post}; | ||||
247 | $post = '</li></ul></noautolink>' unless defined($post); | ||||
248 | my $minor = $params{minor}; | ||||
249 | $minor = '<i>(minor)</i> ' unless defined($minor); | ||||
250 | |||||
251 | unless ( Foswiki::Func::topicExists( $web, $topic ) ) { | ||||
252 | return "Topic $web.$topic does not exist"; | ||||
253 | } | ||||
254 | my @comments; | ||||
255 | |||||
256 | # SMELL: doesn't respect access permissions (too bad there isn't a version that does, like readTopic() does...) | ||||
257 | my ( $meta, undef ) = Foswiki::Func::readTopic( $web, $topic, $rev ); | ||||
258 | |||||
259 | @comments = _extractComments($meta); | ||||
260 | foreach my $comment (@comments) { | ||||
261 | $comment->{rev} = $rev unless $comment->{rev} =~ /\d+/; | ||||
262 | } | ||||
263 | @comments = grep { $_->{rev} == $rev } @comments; | ||||
264 | map { $_->{comment} = $minorMark . $_->{comment} if $_->{minor} } @comments; | ||||
265 | @comments = map { $_->{comment} } @comments; | ||||
266 | |||||
267 | my $text = | ||||
268 | scalar @comments > 0 | ||||
269 | ? $pre . join( $delimiter, @comments ) . $post | ||||
270 | : ''; | ||||
271 | $text =~ s/$minorMark/$minor/g; | ||||
272 | return $text; | ||||
273 | } | ||||
274 | |||||
275 | 1 | 3µs | 1; |