← Index
NYTProf Performance Profile   « block view • line view • sub view »
For /usr/local/src/github.com/foswiki/core/bin/view
  Run on Sun Dec 4 17:17:59 2011
Reported on Sun Dec 4 17:27:13 2011

Filename/usr/local/src/github.com/foswiki/core/lib/Foswiki/Plugins/AutoViewTemplatePlugin.pm
StatementsExecuted 21 statements in 1.38ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11155µs273µsFoswiki::Plugins::AutoViewTemplatePlugin::::initPluginFoswiki::Plugins::AutoViewTemplatePlugin::initPlugin
11129µs37µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@14Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@14
11118µs36µsFoswiki::Plugins::AutoViewTemplatePlugin::::BEGIN@15Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@15
11116µs307µ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
14248µs245µ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
use strict;
# spent 37µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@14 # spent 8µs making 1 call to strict::import
15261µs255µ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
use warnings;
# spent 36µs making 1 call to Foswiki::Plugins::AutoViewTemplatePlugin::BEGIN@15 # spent 19µs making 1 call to warnings::import
16115µs1291µ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
use vars qw( $VERSION $RELEASE $SHORTDESCRIPTION
# spent 291µs making 1 call to vars::import
17 $debug $mode $override $isEditAction
1811.20ms1307µs $pluginName $NO_PREFS_IN_TOPIC );
19
2012µs$VERSION = '$Rev$';
2112µs$RELEASE = '2010-12-10';
2211µs$SHORTDESCRIPTION = 'Automatically sets VIEW_TEMPLATE and EDIT_TEMPLATE';
2311µs$NO_PREFS_IN_TOPIC = 1;
24
2511µ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
sub initPlugin {
2813µs my ( $topic, $web, $user, $installWeb ) = @_;
29
30 # check for Plugins.pm versions
3112µs 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
3815µs $debug = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Debug} || 0;
3912µs $mode = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Mode} || "exist";
4012µs $override = $Foswiki::cfg{Plugins}{AutoViewTemplatePlugin}{Override} || 0;
41
42 # is this an edit action?
4316µs119µs $isEditAction = Foswiki::Func::getContext()->{edit};
# spent 19µs making 1 call to Foswiki::Func::getContext
4413µs 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
4715µs1199µs my $currentTemplate = Foswiki::Func::getPreferencesValue($templateVar);
# spent 199µs making 1 call to Foswiki::Func::getPreferencesValue
4818µs 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
127sub _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
150sub _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
16916µs1;
170__END__