← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 18:42:36 2015
Reported on Fri Jul 31 18:48:14 2015

Filename/var/www/foswikidev/core/lib/Foswiki/Plugins/AutoViewTemplatePlugin.pm
StatementsExecuted 27 statements in 844µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11144µs794µsFoswiki::Plugins::AutoViewTemplatePlugin::::initPluginFoswiki::Plugins::AutoViewTemplatePlugin::initPlugin
11114µs27µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@4Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@4
1119µs14µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@5Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@5
1119µs52µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@6Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@6
0000s0sFoswiki::Plugins::AutoViewTemplatePlugin::::_getTemplateFromSectionIncludeFoswiki::Plugins::AutoViewTemplatePlugin::_getTemplateFromSectionInclude
0000s0sFoswiki::Plugins::AutoViewTemplatePlugin::::_getTemplateFromTemplateExistenceFoswiki::Plugins::AutoViewTemplatePlugin::_getTemplateFromTemplateExistence
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
2package Foswiki::Plugins::AutoViewTemplatePlugin;
3
4228µs241µ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
use strict;
# spent 27µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@4 # spent 13µs making 1 call to strict::import
5229µs218µ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
use warnings;
# spent 14µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@5 # spent 4µs making 1 call to warnings::import
62738µs295µ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
use vars qw( $debug $mode $override $isEditAction $pluginName);
# spent 52µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@6 # spent 43µs making 1 call to vars::import
7
81500nsour $VERSION = '1.21';
91400nsour $RELEASE = '2015-07-24';
101200nsour $SHORTDESCRIPTION = 'Automatically sets VIEW_TEMPLATE and EDIT_TEMPLATE';
111100nsour $NO_PREFS_IN_TOPIC = 1;
12
131200ns$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
sub initPlugin {
1612µs my ( $topic, $web, $user, $installWeb ) = @_;
17
18 # check for Plugins.pm versions
19113µs18µ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
2612µs $debug = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Debug} || 0;
2712µs $mode = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Mode} || "exist";
2811µs $override = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Override} || 0;
29
30 # is this an edit action?
3113µs13µs $isEditAction = Foswiki::Func::getContext()->{edit};
# spent 3µs making 1 call to Foswiki::Func::getContext
321800ns 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
3511µs122µs my $currentTemplate = Foswiki::Func::getPreferencesValue($templateVar);
# spent 22µs making 1 call to Foswiki::Func::getPreferencesValue
361200ns 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
4014µs1106µ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
5014µs1609µs my ( $meta, $text ) = Foswiki::Func::readTopic( $web, $topic );
# spent 609µs making 1 call to Foswiki::Func::readTopic
5112µs12µs my $form = $meta->get("FORM");
# spent 2µs making 1 call to Foswiki::Meta::get
521400ns my $formName;
531400ns $formName = $form->{"name"} if $form;
54
55 # is it a structured topic?
5618µ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
130sub _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
153sub _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
17214µs1;
173__END__