Filename | /var/www/foswikidev/core/lib/Foswiki/Macros/ICON.pm |
Statements | Executed 28 statements in 865µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 48µs | 362µs | _lookupIcon | Foswiki::
1 | 1 | 1 | 24µs | 466µs | _getIconURL | Foswiki::
1 | 1 | 1 | 15µs | 27µs | BEGIN@4.93 | Foswiki::
1 | 1 | 1 | 15µs | 19µs | BEGIN@5.94 | Foswiki::
1 | 1 | 1 | 4µs | 4µs | BEGIN@7.95 | Foswiki::
0 | 0 | 0 | 0s | 0s | ICON | Foswiki::
0 | 0 | 0 | 0s | 0s | __ANON__[:89] | Foswiki::
0 | 0 | 0 | 0s | 0s | __ANON__[:93] | Foswiki::
0 | 0 | 0 | 0s | 0s | __ANON__[:97] | Foswiki::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # See bottom of file for license and copyright information | ||||
2 | package Foswiki; | ||||
3 | |||||
4 | 2 | 31µs | 2 | 38µ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 # spent 27µs making 1 call to Foswiki::BEGIN@4.93
# spent 12µs making 1 call to strict::import |
5 | 2 | 51µs | 2 | 23µ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 # 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 | ||||
8 | 1 | 5µs | if ( $Foswiki::cfg{UseLocale} ) { | ||
9 | require locale; | ||||
10 | import locale(); | ||||
11 | } | ||||
12 | 1 | 724µs | 1 | 4µ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 | ||||
26 | 1 | 600ns | my ( $this, $choice ) = @_; | ||
27 | |||||
28 | 1 | 300ns | return undef unless defined $choice; | ||
29 | |||||
30 | 1 | 2µs | if ( !defined $this->{_ICONSPACE} ) { | ||
31 | 1 | 2µs | 1 | 41µs | my $iconTopic = $this->{prefs}->getPreference('ICONTOPIC'); # spent 41µs making 1 call to Foswiki::Prefs::getPreference |
32 | 1 | 400ns | if ( defined($iconTopic) ) { | ||
33 | 1 | 3µs | $iconTopic =~ s/\s+$//; | ||
34 | 1 | 2µs | 1 | 62µs | my ( $w, $t ) = # spent 62µs making 1 call to Foswiki::normalizeWebTopicName |
35 | $this->normalizeWebTopicName( $this->{webName}, $iconTopic ); | ||||
36 | 1 | 5µs | 2 | 92µ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 | } | ||||
45 | 1 | 300ns | return undef unless $this->{_ICONSPACE}; | ||
46 | |||||
47 | # Have we seen it before? | ||||
48 | 1 | 800ns | $this->{_KNOWNICON} ||= {}; | ||
49 | 1 | 500ns | my $path = $this->{_KNOWNICON}->{$choice}; | ||
50 | |||||
51 | # First, try for a straight attachment name e.g. %ICON{"browse"}% | ||||
52 | # -> "System/FamFamFamGraphics/browse.gif" | ||||
53 | 1 | 10µs | 2 | 119µ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 | |||||
113 | 1 | 2µs | $this->{_KNOWNICON}->{$choice} = $path if defined $path; | ||
114 | |||||
115 | 1 | 4µ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 | ||||
120 | 1 | 500ns | my ( $this, $params ) = @_; | ||
121 | 1 | 1µs | 1 | 362µs | my $path = $this->_lookupIcon( $params->{_DEFAULT} ); # spent 362µs making 1 call to Foswiki::_lookupIcon |
122 | 1 | 100ns | $path ||= $this->_lookupIcon( $params->{default} ); | ||
123 | 1 | 200ns | $path ||= $this->_lookupIcon('else'); | ||
124 | 1 | 5µs | return unless $path && $path =~ s/\/([^\/]+)$//; | ||
125 | 1 | 1µs | my $a = $1; | ||
126 | 1 | 3µs | 1 | 7µs | my ( $w, $t ) = # spent 7µs making 1 call to Foswiki::normalizeWebTopicName |
127 | $this->normalizeWebTopicName( $Foswiki::cfg{SystemWebName}, $path ); | ||||
128 | 1 | 8µs | 1 | 73µ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 | |||||
135 | ICONURLPATH 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" ] }% | ||||
139 | If the main parameter refers to a non-existent icon, and default is not | ||||
140 | given, or also refers to a non-existent icon, then the else icon (else) | ||||
141 | will be used. The HTML alt attribute for the image will be taken from | ||||
142 | the alt parameter. If alt is not given, the main parameter will be used. | ||||
143 | |||||
144 | =cut | ||||
145 | |||||
146 | sub 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 | |||||
181 | 1 | 2µs | 1; | ||
182 | __END__ |