Filename | /usr/local/src/github.com/foswiki/core/lib/Foswiki/Plugins/AutoViewTemplatePlugin.pm |
Statements | Executed 21 statements in 1.38ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 55µs | 273µs | initPlugin | Foswiki::Plugins::AutoViewTemplatePlugin::
1 | 1 | 1 | 29µs | 37µs | BEGIN@14 | Foswiki::Plugins::AutoViewTemplatePlugin::
1 | 1 | 1 | 18µs | 36µs | BEGIN@15 | Foswiki::Plugins::AutoViewTemplatePlugin::
1 | 1 | 1 | 16µs | 307µ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 | 48µs | 2 | 45µs | # spent 37µs (29+8) within Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@14 which was called:
# once (29µs+8µs) by Foswiki::Plugin::BEGIN@2.4 at line 14 # spent 37µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@14
# spent 8µs making 1 call to strict::import |
15 | 2 | 61µs | 2 | 55µs | # spent 36µs (18+19) within Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@15 which was called:
# once (18µs+19µs) by Foswiki::Plugin::BEGIN@2.4 at line 15 # spent 36µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@15
# spent 19µs making 1 call to warnings::import |
16 | 1 | 291µs | # spent 307µs (16+291) within Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@16 which was called:
# once (16µs+291µs) by Foswiki::Plugin::BEGIN@2.4 at line 18 # spent 291µs making 1 call to vars::import | ||
17 | $debug $mode $override $isEditAction | ||||
18 | 2 | 1.22ms | 1 | 307µs | $pluginName $NO_PREFS_IN_TOPIC ); # spent 307µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@16 |
19 | |||||
20 | 1 | 2µs | $VERSION = '$Rev$'; | ||
21 | 1 | 2µs | $RELEASE = '2010-12-10'; | ||
22 | 1 | 1µs | $SHORTDESCRIPTION = 'Automatically sets VIEW_TEMPLATE and EDIT_TEMPLATE'; | ||
23 | 1 | 1µs | $NO_PREFS_IN_TOPIC = 1; | ||
24 | |||||
25 | 1 | 1µs | $pluginName = 'AutoViewTemplatePlugin'; | ||
26 | |||||
27 | # spent 273µs (55+218) within Foswiki::Plugins::AutoViewTemplatePlugin::initPlugin which was called:
# once (55µs+218µs) by Foswiki::Plugin::__ANON__[/usr/local/src/github.com/foswiki/core/lib/Foswiki/Plugin.pm:235] at line 228 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Plugin.pm | ||||
28 | 9 | 38µs | my ( $topic, $web, $user, $installWeb ) = @_; | ||
29 | |||||
30 | # check for Plugins.pm versions | ||||
31 | if ( $Foswiki::Plugins::VERSION < 1.026 ) { | ||||
32 | Foswiki::Func::writeWarning( | ||||
33 | "Version mismatch between $pluginName and Plugins.pm"); | ||||
34 | return 0; | ||||
35 | } | ||||
36 | |||||
37 | # get configuration | ||||
38 | $debug = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Debug} || 0; | ||||
39 | $mode = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Mode} || "exist"; | ||||
40 | $override = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Override} || 0; | ||||
41 | |||||
42 | # is this an edit action? | ||||
43 | 1 | 19µs | $isEditAction = Foswiki::Func::getContext()->{edit}; # spent 19µs making 1 call to Foswiki::Func::getContext | ||
44 | my $templateVar = $isEditAction ? 'EDIT_TEMPLATE' : 'VIEW_TEMPLATE'; | ||||
45 | |||||
46 | # back off if there is a view template already and we are not in override mode | ||||
47 | 1 | 199µs | my $currentTemplate = Foswiki::Func::getPreferencesValue($templateVar); # spent 199µs making 1 call to Foswiki::Func::getPreferencesValue | ||
48 | return 1 if $currentTemplate && !$override; | ||||
49 | |||||
50 | # check if this is a new topic and - if so - try to derive the templateName from | ||||
51 | # the WebTopicEditTemplate | ||||
52 | if ( !Foswiki::Func::topicExists( $web, $topic ) ) { | ||||
53 | if ( Foswiki::Func::topicExists( $web, 'WebTopicEditTemplate' ) ) { | ||||
54 | $topic = 'WebTopicEditTemplate'; | ||||
55 | } | ||||
56 | else { | ||||
57 | return 1; | ||||
58 | } | ||||
59 | } | ||||
60 | |||||
61 | # get form-name | ||||
62 | my ( $meta, $text ) = Foswiki::Func::readTopic( $web, $topic ); | ||||
63 | my $form = $meta->get("FORM"); | ||||
64 | my $formName = $form->{"name"} if $form; | ||||
65 | |||||
66 | # is it a structured topic? | ||||
67 | return 1 unless $formName; | ||||
68 | Foswiki::Func::writeDebug( | ||||
69 | "- ${pluginName}: formfields detected ($formName)") | ||||
70 | if $debug; | ||||
71 | |||||
72 | # get it | ||||
73 | my $templateName = ""; | ||||
74 | MODE: { | ||||
75 | if ( $mode eq "section" ) { | ||||
76 | $templateName = | ||||
77 | _getTemplateFromSectionInclude( $formName, $topic, $web ); | ||||
78 | last MODE; | ||||
79 | } | ||||
80 | if ( $mode eq "exist" ) { | ||||
81 | $templateName = | ||||
82 | _getTemplateFromTemplateExistence( $formName, $topic, $web ); | ||||
83 | last MODE; | ||||
84 | } | ||||
85 | } | ||||
86 | |||||
87 | # only set the view template if there is anything to set | ||||
88 | return 1 unless $templateName; | ||||
89 | |||||
90 | # in edit mode, try to read the template to check if it exists | ||||
91 | if ( $isEditAction && !Foswiki::Func::readTemplate($templateName) ) { | ||||
92 | Foswiki::Func::writeDebug("- ${pluginName}: edit template not found") | ||||
93 | if $debug; | ||||
94 | return 1; | ||||
95 | } | ||||
96 | |||||
97 | # do it | ||||
98 | if ($debug) { | ||||
99 | if ($currentTemplate) { | ||||
100 | if ($override) { | ||||
101 | Foswiki::Func::writeDebug( | ||||
102 | "- ${pluginName}: $templateVar already set, overriding with: $templateName" | ||||
103 | ); | ||||
104 | } | ||||
105 | else { | ||||
106 | Foswiki::Func::writeDebug( | ||||
107 | "- ${pluginName}: $templateVar not changed/set."); | ||||
108 | } | ||||
109 | } | ||||
110 | else { | ||||
111 | Foswiki::Func::writeDebug( | ||||
112 | "- ${pluginName}: $templateVar set to: $templateName"); | ||||
113 | } | ||||
114 | } | ||||
115 | if ( $Foswiki::Plugins::VERSION >= 2.1 ) { | ||||
116 | Foswiki::Func::setPreferencesValue( $templateVar, $templateName ); | ||||
117 | } | ||||
118 | else { | ||||
119 | $Foswiki::Plugins::SESSION->{prefs}->pushPreferenceValues( 'SESSION', | ||||
120 | { $templateVar => $templateName } ); | ||||
121 | } | ||||
122 | |||||
123 | # Plugin correctly initialized | ||||
124 | return 1; | ||||
125 | } | ||||
126 | |||||
127 | sub _getTemplateFromSectionInclude { | ||||
128 | my $formName = $_[0]; | ||||
129 | my $topic = $_[1]; | ||||
130 | my $web = $_[2]; | ||||
131 | |||||
132 | Foswiki::Func::writeDebug( | ||||
133 | "- ${pluginName}: called _getTemplateFromSectionInclude($formName, $topic, $web)" | ||||
134 | ) if $debug; | ||||
135 | |||||
136 | my ( $formweb, $formtopic ) = | ||||
137 | Foswiki::Func::normalizeWebTopicName( $web, $formName ); | ||||
138 | |||||
139 | # SMELL: This can be done much faster, if the formdefinition topic is read directly | ||||
140 | my $sectionName = $isEditAction ? 'edittemplate' : 'viewtemplate'; | ||||
141 | my $templateName = | ||||
142 | "%INCLUDE{ \"$formweb.$formtopic\" section=\"$sectionName\"}%"; | ||||
143 | $templateName = | ||||
144 | Foswiki::Func::expandCommonVariables( $templateName, $topic, $web ); | ||||
145 | |||||
146 | return $templateName; | ||||
147 | } | ||||
148 | |||||
149 | # replaces Web.MyForm with Web.MyViewTemplate and returns Web.MyViewTemplate or Web.MyEditTemplate | ||||
150 | sub _getTemplateFromTemplateExistence { | ||||
151 | my $formName = $_[0]; | ||||
152 | my $topic = $_[1]; | ||||
153 | my $web = $_[2]; | ||||
154 | |||||
155 | Foswiki::Func::writeDebug( | ||||
156 | "- ${pluginName}: called _getTemplateFromTemplateExistence($formName, $topic, $web)" | ||||
157 | ) if $debug; | ||||
158 | my ( $templateWeb, $templateTopic ) = | ||||
159 | Foswiki::Func::normalizeWebTopicName( $web, $formName ); | ||||
160 | |||||
161 | $templateWeb =~ s/\//\./go; | ||||
162 | my $templateName = $templateWeb . '.' . $templateTopic; | ||||
163 | $templateName =~ s/Form$//; | ||||
164 | $templateName .= $isEditAction ? 'Edit' : 'View'; | ||||
165 | |||||
166 | return $templateName; | ||||
167 | } | ||||
168 | |||||
169 | 1 | 6µs | 1; | ||
170 | __END__ |