← 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/PreferencesPlugin.pm
StatementsExecuted 49 statements in 1.67ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
71160µs60µsFoswiki::Plugins::PreferencesPlugin::::beforeCommonTagsHandlerFoswiki::Plugins::PreferencesPlugin::beforeCommonTagsHandler
51157µs57µsFoswiki::Plugins::PreferencesPlugin::::postRenderingHandlerFoswiki::Plugins::PreferencesPlugin::postRenderingHandler
11123µs34µsFoswiki::Plugins::PreferencesPlugin::::initPluginFoswiki::Plugins::PreferencesPlugin::initPlugin
11114µs28µsFoswiki::Plugins::PreferencesPlugin::::BEGIN@10Foswiki::Plugins::PreferencesPlugin::BEGIN@10
11110µs21µsFoswiki::Plugins::PreferencesPlugin::::BEGIN@11Foswiki::Plugins::PreferencesPlugin::BEGIN@11
11110µs29µsFoswiki::Plugins::PreferencesPlugin::::BEGIN@18Foswiki::Plugins::PreferencesPlugin::BEGIN@18
1119µs28µsFoswiki::Plugins::PreferencesPlugin::::BEGIN@16Foswiki::Plugins::PreferencesPlugin::BEGIN@16
1114µs4µsFoswiki::Plugins::PreferencesPlugin::::BEGIN@13Foswiki::Plugins::PreferencesPlugin::BEGIN@13
1113µs3µsFoswiki::Plugins::PreferencesPlugin::::BEGIN@14Foswiki::Plugins::PreferencesPlugin::BEGIN@14
0000s0sFoswiki::Plugins::PreferencesPlugin::::_generateControlButtonsFoswiki::Plugins::PreferencesPlugin::_generateControlButtons
0000s0sFoswiki::Plugins::PreferencesPlugin::::_generateEditButtonFoswiki::Plugins::PreferencesPlugin::_generateEditButton
0000s0sFoswiki::Plugins::PreferencesPlugin::::_generateEditFieldFoswiki::Plugins::PreferencesPlugin::_generateEditField
0000s0sFoswiki::Plugins::PreferencesPlugin::::_getFieldFoswiki::Plugins::PreferencesPlugin::_getField
0000s0sFoswiki::Plugins::PreferencesPlugin::::_saveSetFoswiki::Plugins::PreferencesPlugin::_saveSet
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1# See bottom of file for license and copyright information
2# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
3
4# Still do to:
5# Handle continuation lines (see Prefs::parseText). These should always
6# go into a text area.
7
8package Foswiki::Plugins::PreferencesPlugin;
9
10229µs242µs
# spent 28µs (14+14) within Foswiki::Plugins::PreferencesPlugin::BEGIN@10 which was called: # once (14µs+14µs) by Foswiki::Plugin::BEGIN@2.25 at line 10
use strict;
# spent 28µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@10 # spent 14µs making 1 call to strict::import
11227µs232µs
# spent 21µs (10+11) within Foswiki::Plugins::PreferencesPlugin::BEGIN@11 which was called: # once (10µs+11µs) by Foswiki::Plugin::BEGIN@2.25 at line 11
use warnings;
# spent 21µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@11 # spent 11µs making 1 call to warnings::import
12
13220µs14µs
# spent 4µs within Foswiki::Plugins::PreferencesPlugin::BEGIN@13 which was called: # once (4µs+0s) by Foswiki::Plugin::BEGIN@2.25 at line 13
use Foswiki::Func (); # The plugins API
# spent 4µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@13
14223µs13µs
# spent 3µs within Foswiki::Plugins::PreferencesPlugin::BEGIN@14 which was called: # once (3µs+0s) by Foswiki::Plugin::BEGIN@2.25 at line 14
use Foswiki::Plugins (); # For the API version
# spent 3µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@14
15
16232µs247µs
# spent 28µs (9+19) within Foswiki::Plugins::PreferencesPlugin::BEGIN@16 which was called: # once (9µs+19µs) by Foswiki::Plugin::BEGIN@2.25 at line 16
use vars qw( @shelter );
# spent 28µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@16 # spent 19µs making 1 call to vars::import
17
1831.39ms354µs
# spent 29µs (10+19) within Foswiki::Plugins::PreferencesPlugin::BEGIN@18 which was called: # once (10µs+19µs) by Foswiki::Plugin::BEGIN@2.25 at line 18
use version; our $VERSION = version->declare("v1.1.5");
# spent 29µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@18 # spent 19µs making 1 call to version::import # spent 6µs making 1 call to version::vxs::declare
191300nsour $RELEASE = '1.1.5';
201200nsour $SHORTDESCRIPTION =
21 'Allows editing of preferences using fields predefined in a form';
221100nsour $NO_PREFS_IN_TOPIC = 1;
23
241300nsmy $MARKER = "\007";
25
26# Markers used during form generation
271600nsmy $START_MARKER = $MARKER . 'STARTPREF' . $MARKER;
281400nsmy $END_MARKER = $MARKER . 'ENDPREF' . $MARKER;
29
30
# spent 34µs (23+10) within Foswiki::Plugins::PreferencesPlugin::initPlugin which was called: # once (23µs+10µ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 {
31
32 # check for Plugins.pm versions
33124µs110µs if ( $Foswiki::Plugins::VERSION < 1.026 ) {
# spent 10µs making 1 call to version::vxs::VCMP
34 Foswiki::Func::writeWarning(
35 'Version mismatch between PreferencesPlugin and Plugins.pm');
36 return 0;
37 }
3812µs @shelter = ();
39
4017µs return 1;
41}
42
43
# spent 60µs within Foswiki::Plugins::PreferencesPlugin::beforeCommonTagsHandler which was called 7 times, avg 9µs/call: # 7 times (60µs+0s) by Foswiki::Plugin::invoke at line 294 of /var/www/foswiki11/lib/Foswiki/Plugin.pm, avg 9µs/call
sub beforeCommonTagsHandler {
44 ### my ( $text, $topic, $web ) = @_;
4575µs my $topic = $_[1];
4673µs my $web = $_[2];
47745µs return unless ( $_[0] =~ m/%EDITPREFERENCES(?:{(.*?)})?%/ );
48
49 require CGI;
50 require Foswiki::Attrs;
51 my $formDef;
52 my $attrs = new Foswiki::Attrs($1);
53 if ( defined( $attrs->{_DEFAULT} ) ) {
54 my ( $formWeb, $form ) =
55 Foswiki::Func::normalizeWebTopicName( $web, $attrs->{_DEFAULT} );
56
57 # SMELL: Unpublished API. No choice, though :-(
58 require Foswiki::Form; # SMELL
59 $formDef =
60 new Foswiki::Form( $Foswiki::Plugins::SESSION, $formWeb, $form );
61 }
62
63 my $query = Foswiki::Func::getCgiQuery();
64
65 my $action = lc( $query->param('prefsaction') || '' );
66 $query->Delete('prefsaction');
67 $action =~ s/\s.*$//;
68
69 if ( $action eq 'edit' ) {
70 Foswiki::Func::setTopicEditLock( $web, $topic, 1 );
71
72 # Replace setting values by form fields but not inside comments Item4816
73 # and also not inside verbatim blocks Item1117
74 my $outtext = '';
75 my $insidecomment = 0;
76 my $insideverbatim = 0;
77 foreach my $token ( split /(<!--|-->|<\/?verbatim\b[^>]*>)/, $_[0] ) {
78 if ( !$insideverbatim and $token =~ /<!--/ ) {
79 $insidecomment++;
80 }
81 elsif ( !$insideverbatim and $token =~ /-->/ ) {
82 $insidecomment-- if ( $insidecomment > 0 );
83 }
84 elsif ( $token =~ /<verbatim/ ) {
85 $insideverbatim++;
86 }
87 elsif ( $token =~ /<\/verbatim/ ) {
88 $insideverbatim-- if ( $insideverbatim > 0 );
89 }
90 elsif ( !$insidecomment and !$insideverbatim ) {
91 $token =~
92s/^($Foswiki::regex{setRegex})($Foswiki::regex{tagNameRegex})\s*\=(.*$(?:\n[ \t]+[^\s*].*$)*)/
93 $1._generateEditField($web, $topic, $3, $4, $formDef)/gem;
94 }
95 $outtext .= $token;
96 }
97 $_[0] = $outtext;
98
99 $_[0] =~ s/%EDITPREFERENCES({.*?})?%/
100 _generateControlButtons($web, $topic)/ge;
101 my $viewUrl = Foswiki::Func::getScriptUrl( $web, $topic, 'viewauth' );
102 my $startForm = CGI::start_form(
103 -name => 'editpreferences',
104 -method => 'post',
105 -action => $viewUrl
106 );
107 $startForm =~ s/\s+$//s;
108 my $endForm = CGI::end_form();
109 $endForm =~ s/\s+$//s;
110 $_[0] =~
111 s/^(.*?)$START_MARKER(.*)$END_MARKER(.*?)$/$1$startForm$2$endForm$3/s;
112 $_[0] =~ s/$START_MARKER|$END_MARKER//gs;
113 }
114
115 if ( $action eq 'cancel' ) {
116 Foswiki::Func::setTopicEditLock( $web, $topic, 0 );
117
118 }
119 elsif ( $action eq 'save' ) {
120
121 # Make sure the request came from POST
122 if ( $query && $query->method() && uc( $query->method() ) ne 'POST' ) {
123
124 # silently ignore it if the request didn't come from a POST
125 }
126 else {
127 my ( $meta, $text ) = Foswiki::Func::readTopic( $web, $topic );
128
129 # SMELL: unchecked implicit untaint of value?
130 # $text =~ s/($Foswiki::regex{setVarRegex})/
131 $text =~
132s/^($Foswiki::regex{setRegex})($Foswiki::regex{tagNameRegex})\s*\=(.*$(?:\n[ \t]+[^\s*].*$)*)/
133 $1._saveSet($query, $web, $topic, $3, $4, $formDef)/mgeo;
134 Foswiki::Func::saveTopic( $web, $topic, $meta, $text );
135 }
136 Foswiki::Func::setTopicEditLock( $web, $topic, 0 );
137
138 # Finish with a redirect so that the *new* values are seen
139 my $viewUrl = Foswiki::Func::getScriptUrl( $web, $topic, 'view' );
140 Foswiki::Func::redirectCgiQuery( undef, $viewUrl );
141 return;
142 }
143
144 # implicit action="view", or drop through from "save" or "cancel"
145 $_[0] =~ s/%EDITPREFERENCES({.*?})?%/_generateEditButton($web, $topic)/ge;
146}
147
148# Use the post-rendering handler to plug our formatted editor units
149# into the text
150
# spent 57µs within Foswiki::Plugins::PreferencesPlugin::postRenderingHandler which was called 5 times, avg 11µs/call: # 5 times (57µs+0s) by Foswiki::Plugin::invoke at line 294 of /var/www/foswiki11/lib/Foswiki/Plugin.pm, avg 11µs/call
sub postRenderingHandler {
151 ### my ( $text ) = @_;
152
153558µs $_[0] =~ s/SHELTER$MARKER(\d+)/$shelter[$1]/g;
154}
155
156# Pluck the default value of a named field from a form definition
157sub _getField {
158 my ( $formDef, $name ) = @_;
159 foreach my $f ( @{ $formDef->{fields} } ) {
160 if ( $f->{name} eq $name ) {
161 return $f;
162 }
163 }
164 return;
165}
166
167# Generate a field suitable for editing this type. Use of the core
168# function 'renderFieldForEdit' ensures that we will pick up
169# extra edit types defined in other plugins.
170sub _generateEditField {
171 my ( $web, $topic, $name, $value, $formDef ) = @_;
172 $value =~ s/^\s*(.*?)\s*$/$1/ge;
173
174 my ( $extras, $html );
175
176 if ($formDef) {
177 my $fieldDef = $formDef->getField($name);
178 if ($fieldDef) {
179 my ($topicObject) = Foswiki::Func::readTopic( $web, $topic );
180 ( $extras, $html ) =
181 $fieldDef->renderForEdit( $topicObject, $value );
182 }
183 }
184 unless ($html) {
185
186 if ( $value =~ /\n/ ) {
187 my $rows = 1;
188 $rows++ while $value =~ /\n/g;
189
190 # No form definition and there are newlines, default to textarea
191 $html = CGI::textarea(
192 -class => 'foswikiAlert foswikiInputField',
193 -name => $name,
194 -cols => 80,
195 -rows => $rows,
196 -default => $value
197 );
198 }
199 else {
200
201 # No form definition and no newlines, default to text field.
202 $html = CGI::textfield(
203 -class => 'foswikiAlert foswikiInputField',
204 -name => $name,
205 -size => 80,
206 -value => $value
207 );
208 }
209 }
210
211 push( @shelter, $html );
212
213 return $START_MARKER
214 . CGI::span(
215 {
216 class => 'foswikiAlert',
217 style => 'font-weight:bold;'
218 },
219 $name . ' = SHELTER' . $MARKER . $#shelter
220 ) . $END_MARKER;
221}
222
223# Generate the button that replaces the EDITPREFERENCES tag in view mode
224sub _generateEditButton {
225 my ( $web, $topic ) = @_;
226
227 my $viewUrl = Foswiki::Func::getScriptUrl( $web, $topic, 'viewauth' );
228 my $text = CGI::start_form(
229 -name => 'editpreferences',
230 -method => 'post',
231 -action => $viewUrl
232 );
233 $text .= CGI::input(
234 {
235 type => 'hidden',
236 name => 'prefsaction',
237 value => 'edit'
238 }
239 );
240 $text .= CGI::submit(
241 -name => 'edit',
242 -value => 'Edit Preferences',
243 -class => 'foswikiButton'
244 );
245 $text .= CGI::end_form();
246 $text =~ s/\n//sg;
247 return $text;
248}
249
250# Generate the buttons that replace the EDITPREFERENCES tag in edit mode
251sub _generateControlButtons {
252 my ( $web, $topic ) = @_;
253
254 my $text = $START_MARKER
255 . CGI::submit(
256 -name => 'prefsaction',
257 -value => 'Save new settings',
258 -class => 'foswikiSubmit',
259 -accesskey => 's'
260 );
261 $text .= '&nbsp;';
262 $text .= CGI::submit(
263 -name => 'prefsaction',
264 -value => 'Cancel',
265 -class => 'foswikiButtonCancel',
266 -accesskey => 'c'
267 ) . $END_MARKER;
268 return $text;
269}
270
271# Given a Set in the topic being saved, look in the query to see
272# if there is a new value for the Set and generate a new
273# Set statement.
274sub _saveSet {
275 my ( $query, $web, $topic, $name, $value, $formDef ) = @_;
276
277 my $newValue = $query->param($name);
278 if ( not defined $newValue ) {
279 $newValue = $value;
280 $newValue =~ s/^\s+//; # strip leading whitespace
281 }
282
283 if ($formDef) {
284 my $fieldDef = _getField( $formDef, $name );
285 my $type = $fieldDef->{type} || '';
286 if ( $type && $type =~ /^checkbox/ ) {
287 my $val = '';
288 my $vals = $fieldDef->{value};
289 foreach my $item (@$vals) {
290 my $cvalue = $query->param( $name . $item );
291 if ( defined($cvalue) ) {
292 if ( !$val ) {
293 $val = '';
294 }
295 else {
296 $val .= ', ' if ($cvalue);
297 }
298 $val .= $item if ($cvalue);
299 }
300 }
301 $newValue = $val;
302 }
303 }
304
305 # if no form def, it's just treated as text
306
307 return $name . ' = ' . $newValue;
308}
309
31015µs1;
311__END__