Filename | /var/www/foswiki11/lib/Foswiki/Plugins/AutoViewTemplatePlugin.pm |
Statements | Executed 29 statements in 856µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 77µs | 1.01ms | initPlugin | Foswiki::Plugins::AutoViewTemplatePlugin::
1 | 1 | 1 | 14µs | 28µs | BEGIN@14 | Foswiki::Plugins::AutoViewTemplatePlugin::
1 | 1 | 1 | 12µs | 17µs | BEGIN@15 | Foswiki::Plugins::AutoViewTemplatePlugin::
1 | 1 | 1 | 10µs | 36µs | BEGIN@18 | Foswiki::Plugins::AutoViewTemplatePlugin::
1 | 1 | 1 | 10µs | 56µs | BEGIN@16 | Foswiki::Plugins::AutoViewTemplatePlugin::
0 | 0 | 0 | 0s | 0s | _getTemplateFromSectionInclude | Foswiki::Plugins::AutoViewTemplatePlugin::
0 | 0 | 0 | 0s | 0s | _getTemplateFromTemplateExistence | Foswiki::Plugins::AutoViewTemplatePlugin::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # Plugin for Foswiki | ||||
2 | # | ||||
3 | # Copyright (C) 2008 Oliver Krueger <oliver@wiki-one.net> | ||||
4 | # All Rights Reserved. | ||||
5 | # | ||||
6 | # This program is distributed in the hope that it will be useful, | ||||
7 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
8 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||
9 | # | ||||
10 | # This piece of software is licensed under the GPLv2. | ||||
11 | |||||
12 | package Foswiki::Plugins::AutoViewTemplatePlugin; | ||||
13 | |||||
14 | 2 | 28µs | 2 | 41µs | # spent 28µs (14+13) within Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@14 which was called:
# once (14µs+13µs) by Foswiki::Plugin::BEGIN@2.5 at line 14 # spent 28µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@14
# spent 13µs making 1 call to strict::import |
15 | 2 | 30µs | 2 | 23µs | # spent 17µs (12+5) within Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@15 which was called:
# once (12µs+5µs) by Foswiki::Plugin::BEGIN@2.5 at line 15 # spent 17µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@15
# spent 6µs making 1 call to warnings::import |
16 | 2 | 28µs | 2 | 103µs | # spent 56µs (10+46) within Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@16 which was called:
# once (10µs+46µs) by Foswiki::Plugin::BEGIN@2.5 at line 16 # spent 56µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@16
# spent 46µs making 1 call to vars::import |
17 | |||||
18 | 3 | 691µs | 3 | 67µs | # spent 36µs (10+26) within Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@18 which was called:
# once (10µs+26µs) by Foswiki::Plugin::BEGIN@2.5 at line 18 # spent 36µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@18
# spent 26µs making 1 call to version::import
# spent 5µs making 1 call to version::vxs::declare |
19 | 1 | 300ns | our $RELEASE = '2012-12-16'; | ||
20 | 1 | 100ns | our $SHORTDESCRIPTION = 'Automatically sets VIEW_TEMPLATE and EDIT_TEMPLATE'; | ||
21 | 1 | 200ns | our $NO_PREFS_IN_TOPIC = 1; | ||
22 | |||||
23 | 1 | 200ns | $pluginName = 'AutoViewTemplatePlugin'; | ||
24 | |||||
25 | # spent 1.01ms (77µs+936µs) within Foswiki::Plugins::AutoViewTemplatePlugin::initPlugin which was called:
# once (77µs+936µs) by Foswiki::Plugin::__ANON__[/var/www/foswiki11/lib/Foswiki/Plugin.pm:241] at line 234 of /var/www/foswiki11/lib/Foswiki/Plugin.pm | ||||
26 | 1 | 3µs | my ( $topic, $web, $user, $installWeb ) = @_; | ||
27 | |||||
28 | # check for Plugins.pm versions | ||||
29 | 1 | 22µs | 1 | 13µs | if ( $Foswiki::Plugins::VERSION < 1.026 ) { # spent 13µs making 1 call to version::vxs::VCMP |
30 | Foswiki::Func::writeWarning( | ||||
31 | "Version mismatch between $pluginName and Plugins.pm"); | ||||
32 | return 0; | ||||
33 | } | ||||
34 | |||||
35 | # get configuration | ||||
36 | 1 | 3µs | $debug = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Debug} || 0; | ||
37 | 1 | 2µs | $mode = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Mode} || "exist"; | ||
38 | 1 | 1µs | $override = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Override} || 0; | ||
39 | |||||
40 | # is this an edit action? | ||||
41 | 1 | 7µs | 1 | 14µs | $isEditAction = Foswiki::Func::getContext()->{edit}; # spent 14µs making 1 call to Foswiki::Func::getContext |
42 | 1 | 2µs | my $templateVar = $isEditAction ? 'EDIT_TEMPLATE' : 'VIEW_TEMPLATE'; | ||
43 | |||||
44 | # back off if there is a view template already and we are not in override mode | ||||
45 | 1 | 3µs | 1 | 41µs | my $currentTemplate = Foswiki::Func::getPreferencesValue($templateVar); # spent 41µs making 1 call to Foswiki::Func::getPreferencesValue |
46 | 1 | 500ns | return 1 if $currentTemplate && !$override; | ||
47 | |||||
48 | # check if this is a new topic and - if so - try to derive the templateName from | ||||
49 | # the WebTopicEditTemplate | ||||
50 | 1 | 6µs | 1 | 169µs | if ( !Foswiki::Func::topicExists( $web, $topic ) ) { # spent 169µs making 1 call to Foswiki::Func::topicExists |
51 | if ( Foswiki::Func::topicExists( $web, 'WebTopicEditTemplate' ) ) { | ||||
52 | $topic = 'WebTopicEditTemplate'; | ||||
53 | } | ||||
54 | else { | ||||
55 | return 1; | ||||
56 | } | ||||
57 | } | ||||
58 | |||||
59 | # get form-name | ||||
60 | 1 | 5µs | 1 | 692µs | my ( $meta, $text ) = Foswiki::Func::readTopic( $web, $topic ); # spent 692µs making 1 call to Foswiki::Func::readTopic |
61 | 1 | 2µs | 1 | 7µs | my $form = $meta->get("FORM"); # spent 7µs making 1 call to Foswiki::Meta::get |
62 | 1 | 400ns | my $formName; | ||
63 | 1 | 300ns | $formName = $form->{"name"} if $form; | ||
64 | |||||
65 | # is it a structured topic? | ||||
66 | 1 | 15µs | return 1 unless $formName; | ||
67 | Foswiki::Func::writeDebug( | ||||
68 | "- ${pluginName}: formfields detected ($formName)") | ||||
69 | if $debug; | ||||
70 | |||||
71 | # get it | ||||
72 | my $templateName = ""; | ||||
73 | MODE: { | ||||
74 | if ( $mode eq "section" ) { | ||||
75 | $templateName = | ||||
76 | _getTemplateFromSectionInclude( $formName, $topic, $web ); | ||||
77 | last MODE; | ||||
78 | } | ||||
79 | if ( $mode eq "exist" ) { | ||||
80 | $templateName = | ||||
81 | _getTemplateFromTemplateExistence( $formName, $topic, $web ); | ||||
82 | last MODE; | ||||
83 | } | ||||
84 | } | ||||
85 | |||||
86 | # only set the view template if there is anything to set | ||||
87 | return 1 unless $templateName; | ||||
88 | |||||
89 | # in edit mode, try to read the template to check if it exists | ||||
90 | if ( $isEditAction && !Foswiki::Func::readTemplate($templateName) ) { | ||||
91 | Foswiki::Func::writeDebug("- ${pluginName}: edit template not found") | ||||
92 | if $debug; | ||||
93 | return 1; | ||||
94 | } | ||||
95 | |||||
96 | # do it | ||||
97 | if ($debug) { | ||||
98 | if ($currentTemplate) { | ||||
99 | if ($override) { | ||||
100 | Foswiki::Func::writeDebug( | ||||
101 | "- ${pluginName}: $templateVar already set, overriding with: $templateName" | ||||
102 | ); | ||||
103 | } | ||||
104 | else { | ||||
105 | Foswiki::Func::writeDebug( | ||||
106 | "- ${pluginName}: $templateVar not changed/set."); | ||||
107 | } | ||||
108 | } | ||||
109 | else { | ||||
110 | Foswiki::Func::writeDebug( | ||||
111 | "- ${pluginName}: $templateVar set to: $templateName"); | ||||
112 | } | ||||
113 | } | ||||
114 | if ( $Foswiki::Plugins::VERSION >= 2.1 ) { | ||||
115 | Foswiki::Func::setPreferencesValue( $templateVar, $templateName ); | ||||
116 | } | ||||
117 | else { | ||||
118 | $Foswiki::Plugins::SESSION->{prefs}->pushPreferenceValues( 'SESSION', | ||||
119 | { $templateVar => $templateName } ); | ||||
120 | } | ||||
121 | |||||
122 | # Plugin correctly initialized | ||||
123 | return 1; | ||||
124 | } | ||||
125 | |||||
126 | sub _getTemplateFromSectionInclude { | ||||
127 | my $formName = $_[0]; | ||||
128 | my $topic = $_[1]; | ||||
129 | my $web = $_[2]; | ||||
130 | |||||
131 | Foswiki::Func::writeDebug( | ||||
132 | "- ${pluginName}: called _getTemplateFromSectionInclude($formName, $topic, $web)" | ||||
133 | ) if $debug; | ||||
134 | |||||
135 | my ( $formweb, $formtopic ) = | ||||
136 | Foswiki::Func::normalizeWebTopicName( $web, $formName ); | ||||
137 | |||||
138 | # SMELL: This can be done much faster, if the formdefinition topic is read directly | ||||
139 | my $sectionName = $isEditAction ? 'edittemplate' : 'viewtemplate'; | ||||
140 | my $templateName = | ||||
141 | "%INCLUDE{ \"$formweb.$formtopic\" section=\"$sectionName\"}%"; | ||||
142 | $templateName = | ||||
143 | Foswiki::Func::expandCommonVariables( $templateName, $topic, $web ); | ||||
144 | |||||
145 | return $templateName; | ||||
146 | } | ||||
147 | |||||
148 | # replaces Web.MyForm with Web.MyViewTemplate and returns Web.MyViewTemplate or Web.MyEditTemplate | ||||
149 | sub _getTemplateFromTemplateExistence { | ||||
150 | my $formName = $_[0]; | ||||
151 | my $topic = $_[1]; | ||||
152 | my $web = $_[2]; | ||||
153 | |||||
154 | Foswiki::Func::writeDebug( | ||||
155 | "- ${pluginName}: called _getTemplateFromTemplateExistence($formName, $topic, $web)" | ||||
156 | ) if $debug; | ||||
157 | my ( $templateWeb, $templateTopic ) = | ||||
158 | Foswiki::Func::normalizeWebTopicName( $web, $formName ); | ||||
159 | |||||
160 | $templateWeb =~ s/\//\./go; | ||||
161 | my $templateName = $templateWeb . '.' . $templateTopic; | ||||
162 | $templateName =~ s/Form$//; | ||||
163 | $templateName .= $isEditAction ? 'Edit' : 'View'; | ||||
164 | |||||
165 | return $templateName; | ||||
166 | } | ||||
167 | |||||
168 | 1 | 4µs | 1; | ||
169 | __END__ |