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