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