← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 19:05:14 2015
Reported on Fri Jul 31 19:08:10 2015

Filename/var/www/foswiki11/lib/Foswiki/Plugins/AutoViewTemplatePlugin.pm
StatementsExecuted 29 statements in 856µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11177µs1.01msFoswiki::Plugins::AutoViewTemplatePlugin::::initPluginFoswiki::Plugins::AutoViewTemplatePlugin::initPlugin
11114µs28µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@14Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@14
11112µs17µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@15Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@15
11110µs36µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@18Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@18
11110µs56µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@16Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@16
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# 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
12package Foswiki::Plugins::AutoViewTemplatePlugin;
13
14228µs241µ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
use strict;
# spent 28µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@14 # spent 13µs making 1 call to strict::import
15230µs223µ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
use warnings;
# spent 17µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@15 # spent 6µs making 1 call to warnings::import
16228µs2103µ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
use vars qw( $debug $mode $override $isEditAction $pluginName);
# spent 56µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@16 # spent 46µs making 1 call to vars::import
17
183691µs367µ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
use version; our $VERSION = version->declare("v1.1.7");
# 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
191300nsour $RELEASE = '2012-12-16';
201100nsour $SHORTDESCRIPTION = 'Automatically sets VIEW_TEMPLATE and EDIT_TEMPLATE';
211200nsour $NO_PREFS_IN_TOPIC = 1;
22
231200ns$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
sub initPlugin {
2613µs my ( $topic, $web, $user, $installWeb ) = @_;
27
28 # check for Plugins.pm versions
29122µs113µ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
3613µs $debug = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Debug} || 0;
3712µs $mode = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Mode} || "exist";
3811µs $override = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Override} || 0;
39
40 # is this an edit action?
4117µs114µs $isEditAction = Foswiki::Func::getContext()->{edit};
# spent 14µs making 1 call to Foswiki::Func::getContext
4212µ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
4513µs141µs my $currentTemplate = Foswiki::Func::getPreferencesValue($templateVar);
# spent 41µs making 1 call to Foswiki::Func::getPreferencesValue
461500ns 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
5016µs1169µ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
6015µs1692µs my ( $meta, $text ) = Foswiki::Func::readTopic( $web, $topic );
# spent 692µs making 1 call to Foswiki::Func::readTopic
6112µs17µs my $form = $meta->get("FORM");
# spent 7µs making 1 call to Foswiki::Meta::get
621400ns my $formName;
631300ns $formName = $form->{"name"} if $form;
64
65 # is it a structured topic?
66115µ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
126sub _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
149sub _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
16814µs1;
169__END__