Filename | /var/www/foswiki11/lib/Foswiki/Plugins/PreferencesPlugin.pm |
Statements | Executed 49 statements in 1.67ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
7 | 1 | 1 | 60µs | 60µs | beforeCommonTagsHandler | Foswiki::Plugins::PreferencesPlugin::
5 | 1 | 1 | 57µs | 57µs | postRenderingHandler | Foswiki::Plugins::PreferencesPlugin::
1 | 1 | 1 | 23µs | 34µs | initPlugin | Foswiki::Plugins::PreferencesPlugin::
1 | 1 | 1 | 14µs | 28µs | BEGIN@10 | Foswiki::Plugins::PreferencesPlugin::
1 | 1 | 1 | 10µs | 21µs | BEGIN@11 | Foswiki::Plugins::PreferencesPlugin::
1 | 1 | 1 | 10µs | 29µs | BEGIN@18 | Foswiki::Plugins::PreferencesPlugin::
1 | 1 | 1 | 9µs | 28µs | BEGIN@16 | Foswiki::Plugins::PreferencesPlugin::
1 | 1 | 1 | 4µs | 4µs | BEGIN@13 | Foswiki::Plugins::PreferencesPlugin::
1 | 1 | 1 | 3µs | 3µs | BEGIN@14 | Foswiki::Plugins::PreferencesPlugin::
0 | 0 | 0 | 0s | 0s | _generateControlButtons | Foswiki::Plugins::PreferencesPlugin::
0 | 0 | 0 | 0s | 0s | _generateEditButton | Foswiki::Plugins::PreferencesPlugin::
0 | 0 | 0 | 0s | 0s | _generateEditField | Foswiki::Plugins::PreferencesPlugin::
0 | 0 | 0 | 0s | 0s | _getField | Foswiki::Plugins::PreferencesPlugin::
0 | 0 | 0 | 0s | 0s | _saveSet | Foswiki::Plugins::PreferencesPlugin::
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 | |||||
8 | package Foswiki::Plugins::PreferencesPlugin; | ||||
9 | |||||
10 | 2 | 29µs | 2 | 42µ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 # spent 28µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@10
# spent 14µs making 1 call to strict::import |
11 | 2 | 27µs | 2 | 32µ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 # spent 21µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@11
# spent 11µs making 1 call to warnings::import |
12 | |||||
13 | 2 | 20µs | 1 | 4µ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 # spent 4µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@13 |
14 | 2 | 23µs | 1 | 3µ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 # spent 3µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@14 |
15 | |||||
16 | 2 | 32µs | 2 | 47µ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 # spent 28µs making 1 call to Foswiki::Plugins::PreferencesPlugin::BEGIN@16
# spent 19µs making 1 call to vars::import |
17 | |||||
18 | 3 | 1.39ms | 3 | 54µ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 # 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 |
19 | 1 | 300ns | our $RELEASE = '1.1.5'; | ||
20 | 1 | 200ns | our $SHORTDESCRIPTION = | ||
21 | 'Allows editing of preferences using fields predefined in a form'; | ||||
22 | 1 | 100ns | our $NO_PREFS_IN_TOPIC = 1; | ||
23 | |||||
24 | 1 | 300ns | my $MARKER = "\007"; | ||
25 | |||||
26 | # Markers used during form generation | ||||
27 | 1 | 600ns | my $START_MARKER = $MARKER . 'STARTPREF' . $MARKER; | ||
28 | 1 | 400ns | my $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 | ||||
31 | |||||
32 | # check for Plugins.pm versions | ||||
33 | 1 | 24µs | 1 | 10µ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 | } | ||||
38 | 1 | 2µs | @shelter = (); | ||
39 | |||||
40 | 1 | 7µ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 | ||||
44 | ### my ( $text, $topic, $web ) = @_; | ||||
45 | 7 | 5µs | my $topic = $_[1]; | ||
46 | 7 | 3µs | my $web = $_[2]; | ||
47 | 7 | 45µ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 =~ | ||||
92 | s/^($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 =~ | ||||
132 | s/^($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 | ||||
151 | ### my ( $text ) = @_; | ||||
152 | |||||
153 | 5 | 58µs | $_[0] =~ s/SHELTER$MARKER(\d+)/$shelter[$1]/g; | ||
154 | } | ||||
155 | |||||
156 | # Pluck the default value of a named field from a form definition | ||||
157 | sub _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. | ||||
170 | sub _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 | ||||
224 | sub _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 | ||||
251 | sub _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 .= ' '; | ||||
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. | ||||
274 | sub _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 | |||||
310 | 1 | 5µs | 1; | ||
311 | __END__ |