← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 18:42:36 2015
Reported on Fri Jul 31 18:48:14 2015

Filename/var/www/foswikidev/core/lib/Foswiki/Plugins/RevCommentPlugin.pm
StatementsExecuted 462 statements in 1.85ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
10811485µs485µsFoswiki::Plugins::RevCommentPlugin::::commonTagsHandlerFoswiki::Plugins::RevCommentPlugin::commonTagsHandler
11128µs176µsFoswiki::Plugins::RevCommentPlugin::::initPluginFoswiki::Plugins::RevCommentPlugin::initPlugin
11114µs28µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@20Foswiki::Plugins::RevCommentPlugin::BEGIN@20
11113µs49µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@33Foswiki::Plugins::RevCommentPlugin::BEGIN@33
11110µs69µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@28Foswiki::Plugins::RevCommentPlugin::BEGIN@28
1118µs13µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@21Foswiki::Plugins::RevCommentPlugin::BEGIN@21
1114µs4µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@23Foswiki::Plugins::RevCommentPlugin::BEGIN@23
1113µs3µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@25Foswiki::Plugins::RevCommentPlugin::BEGIN@25
1113µs3µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@24Foswiki::Plugins::RevCommentPlugin::BEGIN@24
0000s0sFoswiki::Plugins::RevCommentPlugin::::_extractCommentsFoswiki::Plugins::RevCommentPlugin::_extractComments
0000s0sFoswiki::Plugins::RevCommentPlugin::::_putCommentsFoswiki::Plugins::RevCommentPlugin::_putComments
0000s0sFoswiki::Plugins::RevCommentPlugin::::beforeAttachmentSaveHandlerFoswiki::Plugins::RevCommentPlugin::beforeAttachmentSaveHandler
0000s0sFoswiki::Plugins::RevCommentPlugin::::beforeSaveHandlerFoswiki::Plugins::RevCommentPlugin::beforeSaveHandler
0000s0sFoswiki::Plugins::RevCommentPlugin::::handleRevCommentFoswiki::Plugins::RevCommentPlugin::handleRevComment
Call graph for these subroutines as a Graphviz dot language file.
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# =========================
19package Foswiki::Plugins::RevCommentPlugin;
20228µs242µ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
use strict;
# spent 28µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@20 # spent 14µs making 1 call to strict::import
21224µs217µ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
use warnings;
# spent 13µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@21 # spent 4µs making 1 call to warnings::import
22
23219µs14µ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
use Foswiki::Func ();
# spent 4µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@23
24218µs13µ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
use Foswiki::Plugins ();
# spent 3µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@24
25231µs13µ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
use Foswiki::Meta ();
# spent 3µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@25
26
27# =========================
2816µs159µ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
use vars qw(
# spent 59µs making 1 call to vars::import
29 $web $topic $user $installWeb $VERSION $RELEASE $pluginName
30 $debug
31128µs169µs);
# spent 69µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@28
32
3316µs136µ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
use vars qw(
# spent 36µs making 1 call to vars::import
34 $commentFromUpload $attachmentComments $cachedCommentWeb $cachedCommentTopic $minorMark
3511.20ms149µ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.
401600ns$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.
451200ns$RELEASE = '22 Jan 2009';
46
471100ns$pluginName = 'RevCommentPlugin';
48
491200ns$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
sub initPlugin {
5312µs ( $topic, $web, $user, $installWeb ) = @_;
54
55 # check for Plugins.pm versions
56113µs18µ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
621600ns $commentFromUpload = undef;
63
64 # Get plugin debug flag
6513µs187µ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 = ...
6812µs147µs $attachmentComments =
# spent 47µs making 1 call to Foswiki::Func::getPluginPreferencesValue
69 Foswiki::Func::getPluginPreferencesValue("ATTACHMENT_COMMENTS") || 1;
70
711600ns $cachedCommentWeb = '';
721500ns $cachedCommentTopic = '';
73
74 # Need to register meta, Item11249
7514µs16µ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(
821300ns "- Foswiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK")
83 if $debug;
841300ns Foswiki::Func::writeDebug(
85 "- --- attachmentComments = " . $attachmentComments )
86 if $debug;
8713µ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
sub commonTagsHandler {
91### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead
92
9310820µ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
10010897µs $_[0] =~ s/%REVCOMMENT%/&handleRevComment()/ge;
10110875µs $_[0] =~ s/%REVCOMMENT{(.*?)}%/&handleRevComment($1)/ge;
102108271µs $_[0] =~ s/%REVCOMMENT\[(.*?)\]%/&handleRevComment($1)/ge;
103}
104
105sub 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
175sub 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
197sub _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
217sub _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
233sub 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
28914µs1;