Filename | /var/www/foswiki11/lib/Foswiki/Macros/ICON.pm |
Statements | Executed 34 statements in 2.06ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 99µs | 708µs | _lookupIcon | Foswiki::
1 | 1 | 1 | 37µs | 66µs | BEGIN@4.61 | Foswiki::
1 | 1 | 1 | 26µs | 38µs | BEGIN@5.62 | Foswiki::
1 | 1 | 1 | 19µs | 119µs | _getIconUrl | Foswiki::
1 | 1 | 1 | 13µs | 721µs | _findIcon | Foswiki::
1 | 1 | 1 | 11µs | 11µs | BEGIN@7.63 | Foswiki::
0 | 0 | 0 | 0s | 0s | ICON | Foswiki::
0 | 0 | 0 | 0s | 0s | __ANON__[:85] | Foswiki::
0 | 0 | 0 | 0s | 0s | __ANON__[:89] | Foswiki::
0 | 0 | 0 | 0s | 0s | __ANON__[:93] | 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 | 77µs | 2 | 95µ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 # spent 66µs making 1 call to Foswiki::BEGIN@4.61
# spent 29µs making 1 call to strict::import |
5 | 2 | 59µs | 2 | 50µ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 # spent 38µs making 1 call to Foswiki::BEGIN@5.62
# spent 12µs making 1 call to warnings::import |
6 | |||||
7 | 2 | 1.81ms | 1 | 11µs | # spent 11µs within Foswiki::BEGIN@7.63 which was called:
# once (11µs+0s) by Foswiki::BEGIN@7.60 at line 7 # 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 | ||||
21 | 1 | 1µs | my ( $this, $choice ) = @_; | ||
22 | |||||
23 | 1 | 500ns | return undef unless defined $choice; | ||
24 | |||||
25 | 1 | 1µs | if ( !defined $this->{_ICONSPACE} ) { | ||
26 | 1 | 4µs | 1 | 77µs | my $iconTopic = $this->{prefs}->getPreference('ICONTOPIC'); # spent 77µs making 1 call to Foswiki::Prefs::getPreference |
27 | 1 | 900ns | if ( defined($iconTopic) ) { | ||
28 | 1 | 2µs | $iconTopic =~ s/\s+$//; | ||
29 | 1 | 6µs | 1 | 139µs | my ( $w, $t ) = # spent 139µs making 1 call to Foswiki::normalizeWebTopicName |
30 | $this->normalizeWebTopicName( $this->{webName}, $iconTopic ); | ||||
31 | 1 | 30µs | 2 | 207µ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 | } | ||||
40 | 1 | 700ns | return undef unless $this->{_ICONSPACE}; | ||
41 | |||||
42 | # Have we seen it before? | ||||
43 | 1 | 2µs | $this->{_KNOWNICON} ||= {}; | ||
44 | 1 | 700ns | my $path = $this->{_KNOWNICON}->{$choice}; | ||
45 | |||||
46 | # First, try for a straight attachment name e.g. %ICON{"browse"}% | ||||
47 | # -> "System/FamFamFamGraphics/browse.gif" | ||||
48 | 1 | 15µs | 2 | 186µ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 | |||||
109 | 1 | 3µs | $this->{_KNOWNICON}->{$choice} = $path if defined $path; | ||
110 | |||||
111 | 1 | 9µ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 | ||||
115 | 1 | 700ns | my $this = shift; | ||
116 | 1 | 200ns | my $params = shift; | ||
117 | |||||
118 | 1 | 3µs | 1 | 708µ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'); | ||||
122 | 1 | 9µ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 | ||||
126 | 1 | 500ns | my $this = shift; | ||
127 | 1 | 400ns | my $absolute = shift; | ||
128 | 1 | 500ns | my $path = shift; | ||
129 | 1 | 400ns | return if ( !defined($path) ); | ||
130 | 1 | 4µs | my @path = split( '/', $path ); | ||
131 | 1 | 700ns | my $a = pop(@path); | ||
132 | 1 | 500ns | my $t = pop(@path); | ||
133 | 1 | 1µs | my $w = join( '/', @path ); | ||
134 | 1 | 12µs | 1 | 99µ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 | |||||
141 | ICONURLPATH 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" ] }% | ||||
145 | If the main parameter refers to a non-existent icon, and default is not | ||||
146 | given, or also refers to a non-existent icon, then the else icon (else) | ||||
147 | will be used. The HTML alt attribute for the image will be taken from | ||||
148 | the alt parameter. If alt is not given, the main parameter will be used. | ||||
149 | |||||
150 | =cut | ||||
151 | |||||
152 | sub 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 | |||||
184 | 1 | 5µs | 1; | ||
185 | __END__ |