Filename | /var/www/foswiki11/lib/Foswiki/Plugins/AttachContentPlugin.pm |
Statements | Executed 25 statements in 1.05ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
7 | 1 | 1 | 67µs | 67µs | beforeCommonTagsHandler | Foswiki::Plugins::AttachContentPlugin::
1 | 1 | 1 | 41µs | 128µs | initPlugin | Foswiki::Plugins::AttachContentPlugin::
1 | 1 | 1 | 14µs | 28µs | BEGIN@25 | Foswiki::Plugins::AttachContentPlugin::
1 | 1 | 1 | 8µs | 8µs | BEGIN@26 | Foswiki::Plugins::AttachContentPlugin::
1 | 1 | 1 | 5µs | 5µs | _initVariables | Foswiki::Plugins::AttachContentPlugin::
0 | 0 | 0 | 0s | 0s | _debug | Foswiki::Plugins::AttachContentPlugin::
0 | 0 | 0 | 0s | 0s | _endAttach | Foswiki::Plugins::AttachContentPlugin::
0 | 0 | 0 | 0s | 0s | _handleAttach | Foswiki::Plugins::AttachContentPlugin::
0 | 0 | 0 | 0s | 0s | _handleAttachBeforeRendering | Foswiki::Plugins::AttachContentPlugin::
0 | 0 | 0 | 0s | 0s | _startAttach | Foswiki::Plugins::AttachContentPlugin::
0 | 0 | 0 | 0s | 0s | afterSaveHandler | Foswiki::Plugins::AttachContentPlugin::
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 | |||||
22 | package Foswiki::Plugins::AttachContentPlugin; | ||||
23 | |||||
24 | # Always use strict to enforce variable scoping | ||||
25 | 2 | 28µs | 2 | 41µ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 # spent 28µs making 1 call to Foswiki::Plugins::AttachContentPlugin::BEGIN@25
# spent 13µs making 1 call to strict::import |
26 | 2 | 910µs | 1 | 8µ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 # 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. | ||||
31 | 1 | 600ns | our $VERSION = '$Rev: 8629 (2010-08-21) $'; | ||
32 | 1 | 100ns | our $RELEASE = '2.3.3'; | ||
33 | |||||
34 | # Short description of this plugin | ||||
35 | # One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic: | ||||
36 | 1 | 300ns | our $SHORTDESCRIPTION = 'Saves dynamic topic text to an attachment'; | ||
37 | 1 | 100ns | our $NO_PREFS_IN_TOPIC = 1; | ||
38 | |||||
39 | # Name of this Plugin, only used in this module | ||||
40 | 1 | 300ns | my $pluginName = 'AttachContentPlugin'; | ||
41 | 1 | 100ns | my $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 | ||||
44 | 1 | 2µs | my ( $topic, $web, $user, $installWeb ) = @_; | ||
45 | |||||
46 | # check for Plugins.pm versions | ||||
47 | 1 | 28µs | 1 | 19µ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 | |||||
53 | 1 | 5µs | 1 | 5µs | _initVariables(); # spent 5µs making 1 call to Foswiki::Plugins::AttachContentPlugin::_initVariables |
54 | |||||
55 | 1 | 4µs | 1 | 37µs | Foswiki::Func::registerTagHandler( 'STARTATTACH', \&_startAttach ); # spent 37µs making 1 call to Foswiki::Func::registerTagHandler |
56 | 1 | 3µs | 1 | 26µs | Foswiki::Func::registerTagHandler( 'ENDATTACH', \&_endAttach ); # spent 26µs making 1 call to Foswiki::Func::registerTagHandler |
57 | |||||
58 | 1 | 6µs | return 1; | ||
59 | } | ||||
60 | |||||
61 | sub _startAttach {} | ||||
62 | sub _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 | ||||
69 | 1 | 7µ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 | ||||
77 | |||||
78 | #my ($text, $topic, $web, $meta ) = @_; | ||||
79 | |||||
80 | $_[0] =~ | ||||
81 | 7 | 52µs | s/%STARTATTACH{(.*?)}%(.*?)%ENDATTACH%/&_handleAttachBeforeRendering($1, $2, $_[2], $_[1])/ges; | ||
82 | } | ||||
83 | |||||
84 | =pod | ||||
85 | |||||
86 | _handleAttachBeforeRendering($attributes, $content, $web, $topic) | ||||
87 | |||||
88 | Removes content if param hidecontent is true. | ||||
89 | |||||
90 | =cut | ||||
91 | |||||
92 | sub _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 | |||||
108 | sub 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] =~ | ||||
127 | s/%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 | |||||
139 | sub _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 | |||||
222 | writes a debug message if the $debug flag is set | ||||
223 | |||||
224 | =cut | ||||
225 | |||||
226 | sub _debug { | ||||
227 | my ($text) = @_; | ||||
228 | |||||
229 | Foswiki::Func::writeDebug("$pluginName; $text") | ||||
230 | if $Foswiki::cfg{Plugins}{AttachContentPlugin}{Debug}; | ||||
231 | } | ||||
232 | |||||
233 | 1 | 4µs | 1; |