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

Filename/var/www/foswiki11/lib/Foswiki/Macros/ICON.pm
StatementsExecuted 34 statements in 2.06ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11199µs708µsFoswiki::::_lookupIconFoswiki::_lookupIcon
11137µs66µsFoswiki::::BEGIN@4.61Foswiki::BEGIN@4.61
11126µs38µsFoswiki::::BEGIN@5.62Foswiki::BEGIN@5.62
11119µs119µsFoswiki::::_getIconUrlFoswiki::_getIconUrl
11113µs721µsFoswiki::::_findIconFoswiki::_findIcon
11111µs11µsFoswiki::::BEGIN@7.63Foswiki::BEGIN@7.63
0000s0sFoswiki::::ICONFoswiki::ICON
0000s0sFoswiki::::__ANON__[:85]Foswiki::__ANON__[:85]
0000s0sFoswiki::::__ANON__[:89]Foswiki::__ANON__[:89]
0000s0sFoswiki::::__ANON__[:93]Foswiki::__ANON__[:93]
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
4277µs295µs
# spent 66µs (37+29) within Foswiki::BEGIN@4.61 which was called: # once (37µs+29µs) by Foswiki::BEGIN@7.60 at line 4
use strict;
# spent 66µs making 1 call to Foswiki::BEGIN@4.61 # spent 29µs making 1 call to strict::import
5259µs250µs
# spent 38µs (26+12) within Foswiki::BEGIN@5.62 which was called: # once (26µs+12µs) by Foswiki::BEGIN@7.60 at line 5
use warnings;
# spent 38µs making 1 call to Foswiki::BEGIN@5.62 # spent 12µs making 1 call to warnings::import
6
721.81ms111µs
# spent 11µs within Foswiki::BEGIN@7.63 which was called: # once (11µs+0s) by Foswiki::BEGIN@7.60 at line 7
use Foswiki::Macros::ICONURL ();
# spent 11µs making 1 call to Foswiki::BEGIN@7.63
8
9# Uses:
10# _ICONSPACE to reference the meta object of the %ICONTOPIC%,
11# _EXT2ICON to record the mapping of file extensions to icon names
12# _KNOWNICON to record the mapping for icons already used
13# _ICONSTEMPLATE to reference the 'icons' template
14
15# Maps from a "filename or extension" to the path of the
16# attachment that contains the image for that file type.
17# If there is no such icon, returns undef.
18# The path returned is of the form web/topic/attachment, so can be
19# used relative to a base URL or as a file path.
20
# spent 708µs (99+609) within Foswiki::_lookupIcon which was called: # once (99µs+609µs) by Foswiki::_findIcon at line 118
sub _lookupIcon {
2111µs my ( $this, $choice ) = @_;
22
231500ns return undef unless defined $choice;
24
2511µs if ( !defined $this->{_ICONSPACE} ) {
2614µs177µs my $iconTopic = $this->{prefs}->getPreference('ICONTOPIC');
# spent 77µs making 1 call to Foswiki::Prefs::getPreference
271900ns if ( defined($iconTopic) ) {
2812µs $iconTopic =~ s/\s+$//;
2916µs1139µs my ( $w, $t ) =
# spent 139µs making 1 call to Foswiki::normalizeWebTopicName
30 $this->normalizeWebTopicName( $this->{webName}, $iconTopic );
31130µs2207µs if ( $this->topicExists( $w, $t ) ) {
# spent 156µs making 1 call to Foswiki::topicExists # spent 51µs making 1 call to Foswiki::Meta::new
32 $this->{_ICONSPACE} = new Foswiki::Meta( $this, $w, $t );
33 }
34 else {
35 $this->logger->log( 'warning',
36 'ICONTOPIC $w.$t does not exist' );
37 }
38 }
39 }
401700ns return undef unless $this->{_ICONSPACE};
41
42 # Have we seen it before?
4312µs $this->{_KNOWNICON} ||= {};
441700ns my $path = $this->{_KNOWNICON}->{$choice};
45
46 # First, try for a straight attachment name e.g. %ICON{"browse"}%
47 # -> "System/FamFamFamGraphics/browse.gif"
48115µs2186µs if ( defined $path ) {
# spent 178µs making 1 call to Foswiki::Meta::hasAttachment # spent 8µs making 1 call to Foswiki::Meta::getPath
49
50 # Already known
51 }
52 elsif ( $this->{_ICONSPACE}->hasAttachment("$choice.png") ) {
53
54 # Found .png attached to ICONTOPIC
55 $path = $this->{_ICONSPACE}->getPath() . "/$choice.png";
56 }
57 elsif ( $this->{_ICONSPACE}->hasAttachment("$choice.gif") ) {
58
59 # Found .gif attached to ICONTOPIC
60 $path = $this->{_ICONSPACE}->getPath() . "/$choice.gif";
61 }
62 elsif ( $choice =~ /\.([a-zA-Z0-9]+)$/ ) {
63
64 #TODO: need to give this usage a chance at tmpl based icons too
65 my $ext = $1;
66 if ( !defined $this->{_EXT2ICON} ) {
67
68 # Load the file extension mapping
69 $this->{_EXT2ICON} = {};
70 local $/;
71 try {
72 my $icons =
73 $this->{_ICONSPACE}->openAttachment( '_filetypes.txt', '<' );
74
75 # Validate the file types as we read them.
76 %{ $this->{_EXT2ICON} } = map {
77 Foswiki::Sandbox::untaint(
78 $_,
79 sub {
80 my $tok = shift;
81 die "Bad filetype $tok"
82 unless $tok =~
83 /^[$Foswiki::regex{mixedAlphaNum}]+$/o;
84 return $tok;
85 }
86 );
87 } split( /\s+/, <$icons> );
88 $icons->close();
89 }
90 catch Error with {
91 ASSERT( 0, $_[0] ) if DEBUG;
92 $this->{_EXT2ICON} = {};
93 };
94 }
95
96 my $icon = $this->{_EXT2ICON}->{$ext};
97 if ( defined $icon ) {
98 if ( $this->{_ICONSPACE}->hasAttachment("$icon.png") ) {
99
100 # Found .png attached to ICONTOPIC
101 $path = $this->{_ICONSPACE}->getPath() . "/$icon.png";
102 }
103 else {
104 $path = $this->{_ICONSPACE}->getPath() . "/$icon.gif";
105 }
106 }
107 }
108
10913µs $this->{_KNOWNICON}->{$choice} = $path if defined $path;
110
11119µs return $path;
112}
113
114
# spent 721µs (13+708) within Foswiki::_findIcon which was called: # once (13µs+708µs) by Foswiki::ICONURL at line 19 of /var/www/foswiki11/lib/Foswiki/Macros/ICONURL.pm
sub _findIcon {
1151700ns my $this = shift;
1161200ns my $params = shift;
117
11813µs1708µs my $path =
# spent 708µs making 1 call to Foswiki::_lookupIcon
119 $this->_lookupIcon( $params->{_DEFAULT} )
120 || $this->_lookupIcon( $params->{default} )
121 || $this->_lookupIcon('else');
12219µs return ($path);
123}
124
125
# spent 119µs (19+99) within Foswiki::_getIconUrl which was called: # once (19µs+99µs) by Foswiki::ICONURL at line 21 of /var/www/foswiki11/lib/Foswiki/Macros/ICONURL.pm
sub _getIconUrl {
1261500ns my $this = shift;
1271400ns my $absolute = shift;
1281500ns my $path = shift;
1291400ns return if ( !defined($path) );
13014µs my @path = split( '/', $path );
1311700ns my $a = pop(@path);
1321500ns my $t = pop(@path);
13311µs my $w = join( '/', @path );
134112µs199µs return $this->getPubUrl( $absolute, $w, $t, $a );
# spent 99µs making 1 call to Foswiki::getPubUrl
135}
136
137=begin TML
138
139---++ ObjectMethod ICON($params) -> $html
140
141ICONURLPATH macro implementation
142
143 * %ICON{ "filename or icon name" [ default="filename or icon name" ]
144 [ alt="alt text to be added to the HTML img tag" ] }%
145If the main parameter refers to a non-existent icon, and default is not
146given, or also refers to a non-existent icon, then the else icon (else)
147will be used. The HTML alt attribute for the image will be taken from
148the alt parameter. If alt is not given, the main parameter will be used.
149
150=cut
151
152sub ICON {
153 my ( $this, $params ) = @_;
154
155 if ( !defined( $this->{_ICONSTEMPLATE} ) ) {
156
157 #if we fail to load once, don't try again.
158 $this->{_ICONSTEMPLATE} = $this->templates->readTemplate('icons');
159 }
160
161 #use icons.tmpl
162 if ( defined( $this->{_ICONSTEMPLATE} ) ) {
163
164 #can't test for default&else here - need to allow the 'old' way a chance.
165 #foreach my $iconName ($params->{_DEFAULT}, $params->{default}, 'else') {
166 my $iconName =
167 $params->{_DEFAULT}
168 || $params->{default}
169 || 'else'; #can default the values if things are undefined though
170 #next unless (defined($iconName));
171 my $html = $this->templates->expandTemplate( "icon:" . $iconName );
172 return $html if ( defined($html) and $html ne '' );
173
174 #}
175 }
176
177 #fall back to using the traditional brute force attachment method.
178 my ($path) = $this->_findIcon($params);
179
180 return $this->renderer->renderIconImage( $this->_getIconUrl( 0, $path ),
181 $params->{alt} || $params->{_DEFAULT} || $params->{default} || 'else' );
182}
183
18415µs1;
185__END__