← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 19:05:14 2015
Reported on Fri Jul 31 19:08:10 2015

Filename/var/www/foswiki11/lib/Foswiki/Plugins/RevCommentPlugin.pm
StatementsExecuted 75 statements in 1.48ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1311197µs197µsFoswiki::Plugins::RevCommentPlugin::::commonTagsHandlerFoswiki::Plugins::RevCommentPlugin::commonTagsHandler
11131µs212µsFoswiki::Plugins::RevCommentPlugin::::initPluginFoswiki::Plugins::RevCommentPlugin::initPlugin
11114µs27µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@20Foswiki::Plugins::RevCommentPlugin::BEGIN@20
11112µs48µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@30Foswiki::Plugins::RevCommentPlugin::BEGIN@30
1119µs68µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@25Foswiki::Plugins::RevCommentPlugin::BEGIN@25
1116µs6µsFoswiki::Plugins::RevCommentPlugin::::BEGIN@22Foswiki::Plugins::RevCommentPlugin::BEGIN@22
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µs241µ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
use strict;
# spent 27µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@20 # spent 14µs making 1 call to strict::import
21
22236µs16µ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
use Foswiki::Func;
# spent 6µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@22
23
24# =========================
2516µs159µ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
use vars qw(
# spent 59µs making 1 call to vars::import
26 $web $topic $user $installWeb $VERSION $RELEASE $pluginName
27 $debug
28133µs168µs);
# spent 68µs making 1 call to Foswiki::Plugins::RevCommentPlugin::BEGIN@25
29
3016µs136µ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
use vars qw(
# spent 36µs making 1 call to vars::import
31 $commentFromUpload $attachmentComments $cachedCommentWeb $cachedCommentTopic $minorMark
3211.16ms148µ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.
371700ns$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.
421200ns$RELEASE = '22 Jan 2009';
43
441100ns$pluginName = 'RevCommentPlugin';
45
461200ns$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
sub initPlugin {
5013µs ( $topic, $web, $user, $installWeb ) = @_;
51
52 # check for Plugins.pm versions
53116µs19µ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
591900ns $commentFromUpload = undef;
60
61 # Get plugin debug flag
6213µs1101µ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 = ...
6514µs170µs $attachmentComments =
# spent 70µs making 1 call to Foswiki::Func::getPluginPreferencesValue
66 Foswiki::Func::getPluginPreferencesValue("ATTACHMENT_COMMENTS") || 1;
67
681500ns $cachedCommentWeb = '';
691200ns $cachedCommentTopic = '';
70
71 # Plugin correctly initialized
721400ns Foswiki::Func::writeDebug(
73 "- Foswiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK")
74 if $debug;
751200ns Foswiki::Func::writeDebug(
76 "- --- attachmentComments = " . $attachmentComments )
77 if $debug;
7814µ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
sub commonTagsHandler {
82### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead
83
84135µ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
901351µs $_[0] =~ s/%REVCOMMENT%/&handleRevComment()/ge;
911339µs $_[0] =~ s/%REVCOMMENT{(.*?)}%/&handleRevComment($1)/ge;
921377µs $_[0] =~ s/%REVCOMMENT\[(.*?)\]%/&handleRevComment($1)/ge;
93}
94
95sub 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
163sub 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
185sub _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
205sub _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
221sub 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
27513µs1;