← 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/AttachContentPlugin.pm
StatementsExecuted 25 statements in 1.05ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
71167µs67µsFoswiki::Plugins::AttachContentPlugin::::beforeCommonTagsHandlerFoswiki::Plugins::AttachContentPlugin::beforeCommonTagsHandler
11141µs128µsFoswiki::Plugins::AttachContentPlugin::::initPluginFoswiki::Plugins::AttachContentPlugin::initPlugin
11114µs28µsFoswiki::Plugins::AttachContentPlugin::::BEGIN@25Foswiki::Plugins::AttachContentPlugin::BEGIN@25
1118µs8µsFoswiki::Plugins::AttachContentPlugin::::BEGIN@26Foswiki::Plugins::AttachContentPlugin::BEGIN@26
1115µs5µsFoswiki::Plugins::AttachContentPlugin::::_initVariablesFoswiki::Plugins::AttachContentPlugin::_initVariables
0000s0sFoswiki::Plugins::AttachContentPlugin::::_debugFoswiki::Plugins::AttachContentPlugin::_debug
0000s0sFoswiki::Plugins::AttachContentPlugin::::_endAttachFoswiki::Plugins::AttachContentPlugin::_endAttach
0000s0sFoswiki::Plugins::AttachContentPlugin::::_handleAttachFoswiki::Plugins::AttachContentPlugin::_handleAttach
0000s0sFoswiki::Plugins::AttachContentPlugin::::_handleAttachBeforeRenderingFoswiki::Plugins::AttachContentPlugin::_handleAttachBeforeRendering
0000s0sFoswiki::Plugins::AttachContentPlugin::::_startAttachFoswiki::Plugins::AttachContentPlugin::_startAttach
0000s0sFoswiki::Plugins::AttachContentPlugin::::afterSaveHandlerFoswiki::Plugins::AttachContentPlugin::afterSaveHandler
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) 2008 Foswiki Contributors
4# Copyright (c) 2007,2009 Arthur Clemens
5# Copyright (c) 2006 Meredith Lesly, Kenneth Lavrsen
6# and TWiki Contributors. All Rights Reserved.
7# Contributors are listed in the AUTHORS file in the root of this distribution.
8# NOTE: Please extend that file, not this notice.
9#
10# This program is free software; you can redistribute it and/or
11# modify it under the terms of the GNU General Public License
12# as published by the Free Software Foundation; either version 2
13# of the License, or (at your option) any later version. For
14# more details read LICENSE in the root of this distribution.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19#
20# For licensing info read LICENSE file in the Foswiki root.
21
22package Foswiki::Plugins::AttachContentPlugin;
23
24# Always use strict to enforce variable scoping
25228µs241µs
# spent 28µs (14+13) within Foswiki::Plugins::AttachContentPlugin::BEGIN@25 which was called: # once (14µs+13µs) by Foswiki::Plugin::BEGIN@2.4 at line 25
use strict;
# spent 28µs making 1 call to Foswiki::Plugins::AttachContentPlugin::BEGIN@25 # spent 13µs making 1 call to strict::import
262910µs18µs
# spent 8µs within Foswiki::Plugins::AttachContentPlugin::BEGIN@26 which was called: # once (8µs+0s) by Foswiki::Plugin::BEGIN@2.4 at line 26
use Foswiki::Func;
# spent 8µs making 1 call to Foswiki::Plugins::AttachContentPlugin::BEGIN@26
27
28# This should always be $Rev: 8629 (2010-08-21) $ so that Foswiki can determine the checked-in
29# status of the plugin. It is used by the build automation tools, so
30# you should leave it alone.
311600nsour $VERSION = '$Rev: 8629 (2010-08-21) $';
321100nsour $RELEASE = '2.3.3';
33
34# Short description of this plugin
35# One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
361300nsour $SHORTDESCRIPTION = 'Saves dynamic topic text to an attachment';
371100nsour $NO_PREFS_IN_TOPIC = 1;
38
39# Name of this Plugin, only used in this module
401300nsmy $pluginName = 'AttachContentPlugin';
411100nsmy $savedAlready;
42
43
# spent 128µs (41+87) within Foswiki::Plugins::AttachContentPlugin::initPlugin which was called: # once (41µs+87µ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 {
4412µs my ( $topic, $web, $user, $installWeb ) = @_;
45
46 # check for Plugins.pm versions
47128µs119µs if ( $Foswiki::Plugins::VERSION < 1.026 ) {
# spent 19µs making 1 call to version::vxs::VCMP
48 Foswiki::Func::writeWarning(
49 "Version mismatch between $pluginName and Plugins.pm");
50 return 0;
51 }
52
5315µs15µs _initVariables();
54
5514µs137µs Foswiki::Func::registerTagHandler( 'STARTATTACH', \&_startAttach );
# spent 37µs making 1 call to Foswiki::Func::registerTagHandler
5613µs126µs Foswiki::Func::registerTagHandler( 'ENDATTACH', \&_endAttach );
# spent 26µs making 1 call to Foswiki::Func::registerTagHandler
57
5816µs return 1;
59}
60
61sub _startAttach {}
62sub _endAttach {}
63
64=pod
65
66=cut
67
68
# spent 5µs within Foswiki::Plugins::AttachContentPlugin::_initVariables which was called: # once (5µs+0s) by Foswiki::Plugins::AttachContentPlugin::initPlugin at line 53
sub _initVariables {
6917µs $savedAlready = 0;
70}
71
72=pod
73
74=cut
75
76
# spent 67µs within Foswiki::Plugins::AttachContentPlugin::beforeCommonTagsHandler which was called 7 times, avg 10µs/call: # 7 times (67µs+0s) by Foswiki::Plugin::invoke at line 294 of /var/www/foswiki11/lib/Foswiki/Plugin.pm, avg 10µs/call
sub beforeCommonTagsHandler {
77
78 #my ($text, $topic, $web, $meta ) = @_;
79
80 $_[0] =~
81752µss/%STARTATTACH{(.*?)}%(.*?)%ENDATTACH%/&_handleAttachBeforeRendering($1, $2, $_[2], $_[1])/ges;
82}
83
84=pod
85
86_handleAttachBeforeRendering($attributes, $content, $web, $topic)
87
88Removes content if param hidecontent is true.
89
90=cut
91
92sub _handleAttachBeforeRendering {
93 my ( $inAttr, $inContent, $inWeb, $inTopic ) = @_;
94
95 my $attrs =
96 Foswiki::Func::expandCommonVariables( $inAttr, $inTopic, $inWeb );
97 my %params = Foswiki::Func::extractParameters($attrs);
98 return '' if Foswiki::Func::isTrue( $params{'hidecontent'} );
99 return $inContent;
100}
101
102=pod
103
104---++ afterSaveHandler($text, $topic, $web, $error, $meta )
105
106=cut
107
108sub afterSaveHandler {
109
110 # do not uncomment, use $_[0], $_[1]... instead
111 ### my ( $text, $topic, $web, $error, $meta ) = @_;
112
113 _debug("afterSaveHandler");
114
115 my $query = Foswiki::Func::getCgiQuery();
116
117 # Do not run plugin when managing attachments.
118 # SMELL: does afterSaveHandler get called in this situation?
119 return if Foswiki::Func::getContext()->{'upload'};
120
121 return if $savedAlready;
122 $savedAlready = 1;
123
124 _debug("sub afterSaveHandler( $_[2].$_[1] )");
125
126 $_[0] =~
127s/%STARTATTACH{(.*?)}%(.*?)%ENDATTACH%/&_handleAttach($1, $2, $_[2], $_[1])/ges;
128 $savedAlready = 0;
129
130 return;
131}
132
133=pod
134
135_handleAttach($attributes, $content, $web, $topic)
136
137=cut
138
139sub _handleAttach {
140 my ( $inAttr, $inContent, $inWeb, $inTopic ) = @_;
141
142 _debug("sub handleAttach; attr=$inAttr; content=$inContent");
143
144 my $attrs =
145 Foswiki::Func::expandCommonVariables( $inAttr, $inTopic, $inWeb );
146 my %params = Foswiki::Func::extractParameters($attrs);
147
148 my $attrFileName = $params{_DEFAULT};
149 return '' unless $attrFileName;
150
151 my $web = $params{'web'} || $inWeb;
152 my $topic = $params{'topic'} || $inTopic;
153 my $comment = $params{'comment'}
154 || $Foswiki::cfg{Plugins}{AttachContentPlugin}{AttachmentComment};
155 my $hide = Foswiki::Func::isTrue( $params{'hide'} );
156 my $keepPars =
157 Foswiki::Func::isTrue( $params{'keeppars'}
158 || $Foswiki::cfg{Plugins}{AttachContentPlugin}{KeepPars} );
159 my $workArea = Foswiki::Func::getWorkArea($pluginName);
160
161 ($web) ? _debug("\t web: $web") : _debug("\t no web");
162 ($topic) ? _debug("\t topic: $topic") : _debug("\t no topic");
163 ($comment) ? _debug("\t comment: $comment") : _debug("\t no comment");
164 ($hide) ? _debug("\t hide: $hide") : _debug("\t no hide");
165 ($keepPars) ? _debug("\t keepPars: $keepPars") : _debug("\t no keepPars");
166 ($workArea) ? _debug("\t workArea: $workArea") : _debug("\t no workArea");
167
168 # Protect against evil filenames - especially for out temp file.
169 my ( $fileName, $orgName ) =
170 Foswiki::Func::sanitizeAttachmentName($attrFileName);
171 _debug("\t fileName=$fileName");
172
173 # Temp file in workarea - Filename + 9 digits to avoid race condition
174 my $tempName = $workArea . '/' . $fileName . int( rand(1000000000) );
175 _debug("\t tempName: $tempName");
176
177 # Turn most TML to text
178 my $content =
179 Foswiki::Func::expandCommonVariables( $inContent, $topic, $web );
180
181 # Turn paragraphs, nops, and bracket links into plain text
182 unless ($keepPars) {
183 $content =~ s/<p\s*\/>/\n/go;
184 $content =~ s/<nop>//goi;
185 $content =~ s/\[\[.+?\]\[(.+?)\]\]/$1/go;
186 $content =~ s/\[\[(.+?)\]\]/$1/go;
187 }
188
189 # strip spaces from content
190 $content =~ s/^[[:space:]]+//s; # trim at start
191 $content =~ s/[[:space:]]+$//s; # trim at end
192 ($content) ? _debug("\t content: $content") : _debug("\t no content");
193
194 # Saving temporary file
195 Foswiki::Func::saveFile( $tempName, $content );
196
197 my @stats = stat $tempName;
198 my $fileSize = $stats[7];
199 my $fileDate = $stats[9];
200
201 Foswiki::Func::saveAttachment(
202 $web, $topic,
203 $fileName,
204 {
205 file => $tempName,
206 filedate => $fileDate,
207 filesize => $fileSize,
208 filepath => $fileName,
209 comment => $comment,
210 hide => $hide
211 }
212 );
213
214 # Delete temporary file
215 unlink($tempName) if ( $tempName && -e $tempName );
216
217 return '';
218}
219
220=pod
221
222writes a debug message if the $debug flag is set
223
224=cut
225
226sub _debug {
227 my ($text) = @_;
228
229 Foswiki::Func::writeDebug("$pluginName; $text")
230 if $Foswiki::cfg{Plugins}{AttachContentPlugin}{Debug};
231}
232
23314µs1;