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

Filename/var/www/foswikidev/core/lib/Foswiki/Macros/ICON.pm
StatementsExecuted 28 statements in 865µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11148µs362µsFoswiki::::_lookupIconFoswiki::_lookupIcon
11124µs466µsFoswiki::::_getIconURLFoswiki::_getIconURL
11115µs27µsFoswiki::::BEGIN@4.93Foswiki::BEGIN@4.93
11115µs19µsFoswiki::::BEGIN@5.94Foswiki::BEGIN@5.94
1114µs4µsFoswiki::::BEGIN@7.95Foswiki::BEGIN@7.95
0000s0sFoswiki::::ICONFoswiki::ICON
0000s0sFoswiki::::__ANON__[:89]Foswiki::__ANON__[:89]
0000s0sFoswiki::::__ANON__[:93]Foswiki::__ANON__[:93]
0000s0sFoswiki::::__ANON__[:97]Foswiki::__ANON__[:97]
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;
3
4231µs238µs
# spent 27µs (15+12) within Foswiki::BEGIN@4.93 which was called: # once (15µs+12µs) by Foswiki::BEGIN@7.92 at line 4
use strict;
# spent 27µs making 1 call to Foswiki::BEGIN@4.93 # spent 12µs making 1 call to strict::import
5251µs223µs
# spent 19µs (15+4) within Foswiki::BEGIN@5.94 which was called: # once (15µs+4µs) by Foswiki::BEGIN@7.92 at line 5
use warnings;
# spent 19µs making 1 call to Foswiki::BEGIN@5.94 # spent 4µs making 1 call to warnings::import
6
7
# spent 4µs within Foswiki::BEGIN@7.95 which was called: # once (4µs+0s) by Foswiki::BEGIN@7.92 at line 12
BEGIN {
815µs if ( $Foswiki::cfg{UseLocale} ) {
9 require locale;
10 import locale();
11 }
121724µs14µs}
# spent 4µs making 1 call to Foswiki::BEGIN@7.95
13
14# Uses:
15# _ICONSPACE to reference the meta object of the %ICONTOPIC%,
16# _EXT2ICON to record the mapping of file extensions to icon names
17# _KNOWNICON to record the mapping for icons already used
18# _ICONSTEMPLATE to reference the 'icons' template
19
20# Maps from a "filename or extension" to the path of the
21# attachment that contains the image for that file type.
22# If there is no such icon, returns undef.
23# The path returned is of the form web/topic/attachment, so can be
24# used relative to a base URL or as a file path.
25
# spent 362µs (48+314) within Foswiki::_lookupIcon which was called: # once (48µs+314µs) by Foswiki::_getIconURL at line 121
sub _lookupIcon {
261600ns my ( $this, $choice ) = @_;
27
281300ns return undef unless defined $choice;
29
3012µs if ( !defined $this->{_ICONSPACE} ) {
3112µs141µs my $iconTopic = $this->{prefs}->getPreference('ICONTOPIC');
# spent 41µs making 1 call to Foswiki::Prefs::getPreference
321400ns if ( defined($iconTopic) ) {
3313µs $iconTopic =~ s/\s+$//;
3412µs162µs my ( $w, $t ) =
# spent 62µs making 1 call to Foswiki::normalizeWebTopicName
35 $this->normalizeWebTopicName( $this->{webName}, $iconTopic );
3615µs292µs if ( $this->topicExists( $w, $t ) ) {
# spent 80µs making 1 call to Foswiki::topicExists # spent 12µs making 1 call to Foswiki::Meta::new
37 $this->{_ICONSPACE} = new Foswiki::Meta( $this, $w, $t );
38 }
39 else {
40 $this->logger->log( 'warning',
41 'ICONTOPIC $w.$t does not exist' );
42 }
43 }
44 }
451300ns return undef unless $this->{_ICONSPACE};
46
47 # Have we seen it before?
481800ns $this->{_KNOWNICON} ||= {};
491500ns my $path = $this->{_KNOWNICON}->{$choice};
50
51 # First, try for a straight attachment name e.g. %ICON{"browse"}%
52 # -> "System/FamFamFamGraphics/browse.gif"
53110µs2119µs if ( defined $path ) {
# spent 116µs making 1 call to Foswiki::Meta::hasAttachment # spent 3µs making 1 call to Foswiki::Meta::getPath
54
55 # Already known
56 }
57 elsif ( $this->{_ICONSPACE}->hasAttachment("$choice.png") ) {
58
59 # Found .png attached to ICONTOPIC
60 $path = $this->{_ICONSPACE}->getPath() . "/$choice.png";
61 }
62 elsif ( $this->{_ICONSPACE}->hasAttachment("$choice.gif") ) {
63
64 # Found .gif attached to ICONTOPIC
65 $path = $this->{_ICONSPACE}->getPath() . "/$choice.gif";
66 }
67 elsif ( $choice =~ m/\.([a-zA-Z0-9]+)$/ ) {
68
69 #TODO: need to give this usage a chance at tmpl based icons too
70 my $ext = $1;
71 if ( !defined $this->{_EXT2ICON} ) {
72
73 # Load the file extension mapping
74 $this->{_EXT2ICON} = {};
75 local $/;
76 try {
77 my $icons =
78 $this->{_ICONSPACE}->openAttachment( '_filetypes.txt', '<' );
79
80 # Validate the file types as we read them.
81 %{ $this->{_EXT2ICON} } = map {
82 Foswiki::Sandbox::untaint(
83 $_,
84 sub {
85 my $tok = shift;
86 die "Bad filetype $tok"
87 unless $tok =~ m/^[[:alnum:]]+$/;
88 return $tok;
89 }
90 );
91 } split( /\s+/, <$icons> );
92 $icons->close();
93 }
94 catch Error with {
95 ASSERT( 0, $_[0] ) if DEBUG;
96 $this->{_EXT2ICON} = {};
97 };
98 }
99
100 my $icon = $this->{_EXT2ICON}->{$ext};
101 if ( defined $icon ) {
102 if ( $this->{_ICONSPACE}->hasAttachment("$icon.png") ) {
103
104 # Found .png attached to ICONTOPIC
105 $path = $this->{_ICONSPACE}->getPath() . "/$icon.png";
106 }
107 else {
108 $path = $this->{_ICONSPACE}->getPath() . "/$icon.gif";
109 }
110 }
111 }
112
11312µs $this->{_KNOWNICON}->{$choice} = $path if defined $path;
114
11514µs return $path;
116}
117
118# Private method shared with ICONURL and ICONURLPATH
119
# spent 466µs (24+442) within Foswiki::_getIconURL which was called: # once (24µs+442µs) by Foswiki::ICONURL at line 27 of /var/www/foswikidev/core/lib/Foswiki/Macros/ICONURL.pm
sub _getIconURL {
1201500ns my ( $this, $params ) = @_;
12111µs1362µs my $path = $this->_lookupIcon( $params->{_DEFAULT} );
# spent 362µs making 1 call to Foswiki::_lookupIcon
1221100ns $path ||= $this->_lookupIcon( $params->{default} );
1231200ns $path ||= $this->_lookupIcon('else');
12415µs return unless $path && $path =~ s/\/([^\/]+)$//;
12511µs my $a = $1;
12613µs17µs my ( $w, $t ) =
# spent 7µs making 1 call to Foswiki::normalizeWebTopicName
127 $this->normalizeWebTopicName( $Foswiki::cfg{SystemWebName}, $path );
12818µs173µs return $this->getPubURL( $w, $t, $a, %$params );
# spent 73µs making 1 call to Foswiki::getPubURL
129}
130
131=begin TML
132
133---++ ObjectMethod ICON($params) -> $html
134
135ICONURLPATH macro implementation
136
137 * %ICON{ "filename or icon name" [ default="filename or icon name" ]
138 [ alt="alt text to be added to the HTML img tag" ] }%
139If the main parameter refers to a non-existent icon, and default is not
140given, or also refers to a non-existent icon, then the else icon (else)
141will be used. The HTML alt attribute for the image will be taken from
142the alt parameter. If alt is not given, the main parameter will be used.
143
144=cut
145
146sub ICON {
147 my ( $this, $params ) = @_;
148
149 if ( !defined( $this->{_ICONSTEMPLATE} ) ) {
150
151 #if we fail to load once, don't try again.
152 $this->{_ICONSTEMPLATE} = $this->templates->readTemplate('icons');
153 }
154
155 #use icons.tmpl
156 if ( defined( $this->{_ICONSTEMPLATE} ) ) {
157
158 #can't test for default&else here - need to allow the 'old' way a chance.
159 #foreach my $iconName ($params->{_DEFAULT}, $params->{default}, 'else') {
160 my $iconName =
161 $params->{_DEFAULT}
162 || $params->{default}
163 || 'else'; #can default the values if things are undefined though
164 #next unless (defined($iconName));
165 my $html = $this->templates->expandTemplate( "icon:" . $iconName );
166 return $html if ( defined($html) and $html ne '' );
167
168 #}
169 }
170
171 #fall back to using the traditional brute force attachment method.
172 require Foswiki::Render::IconImage;
173 return Foswiki::Render::IconImage::render(
174 $this,
175 $this->_getIconURL($params),
176 $params->{alt} || $params->{_DEFAULT} || $params->{default} || 'else',
177 $params->{quote},
178 );
179}
180
18112µs1;
182__END__