OMatic/ImageRef.pm

filter: Statementstmt 70.2% 158 / 225 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 20.0% 2 / 10 Podpod 0.0% 0 / 8 totaltotal 56.9% 160 / 281 CC 19 SCAR 31.6
CC
19
Cov
61%
getImageUrl
42.0
getImageRef
42.0
getType
20.0
CRAP 23.6
« OMatic/AuthLocal.pm ↑ Summary OMatic/submitCatToAns.pm »
1 ##############################################################################
2 # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. #
3 # #
4 # This program is free software; you can redistribute it and/or #
5 # modify it under the terms of the GNU General Public License #
6 # as published by the Free Software Foundation; either version 2 #
7 # of the License, or (at your option) any later version. #
8 # #
9 # This program is distributed in the hope that it will be useful, #
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
12 # GNU General Public License for more details. #
13 # #
14 # You should have received a copy of the GNU General Public License #
15 # along with this program; if not, write to the Free Software #
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#
17 # #
18 # Jon Howell can be contacted at: #
19 # 6211 Sudikoff Lab, Dartmouth College #
20 # Hanover, NH 03755-3510 #
21 # jonh@cs.dartmouth.edu #
22 # #
23 # An electronic copy of the GPL is available at: #
24 # http://www.gnu.org/copyleft/gpl.html #
25 # #
26 ##############################################################################
27  
28 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 ###
31 ### ImageRef.pm
32 ###
33 ### Generates references to the images encoded in ImageData.pm
34 ### (note this only makes sense for built-in images, not those sumbitted
35 ### into the serve/bags/ directory.)
36 ###
37  
38 package FAQ::OMatic::ImageRef;
39  
40 1 ▶use FAQ::OMatic::Bags;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 1 my %img_type = (); # type of each image. constant; no mod_perl
43 # cache problem
44 1 my %img_prop = (); # properties of each image. constant; no mod_perl
45 # cache problem
46  
47 0 ▶sub getImage {
Subroutine getImage: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my $name = shift;
49  
50 0 require FAQ::OMatic::ImageData; # put off loading this file unless someone
51 # actually requests data (meaning we're running
52 # img.pm). No reason any other invocation should
53 # have to load all that image data up.
54 0 my $data = $FAQ::OMatic::ImageData::img{$name};
55 0 ▶ return '' if (not defined $data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
56  
57 0 $data =~ s/\n//gs; # get rid of line terminators
58 0 return pack("H".length($data), $data);
59 }
60  
61 0 ▶sub validImage {
Subroutine validImage: not called CC=1 SCAR=6.9
Pod undocumented
62 0 my $name = shift;
63  
64 0 return defined($img_type{$name});
65 }
66  
67 0 ▶sub getType {
Subroutine getType: not called CC=4 SCAR=30.0
Pod undocumented
68 0 my $name = shift;
69  
70 0 my $typek = $img_type{$name};
71 0 ▶ return '' if (not defined $typek);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $typek 0 0 0
72  
73 0 ▶ return "jpeg" if ($typek eq 'jpg');
Branch: 1 branch
BranchTrueFalseTotal
if $typek eq "jpg" 0 0 0
74 0 ▶ return "gif" if ($typek eq 'gif');
Branch: 1 branch
BranchTrueFalseTotal
if $typek eq "gif" 0 0 0
75 0 return "beats-me";
76 }
77  
78 0 ▶sub getBagForImage {
Subroutine getBagForImage: not called CC=3 SCAR=24.8
Pod undocumented
79 0 my $name = shift;
80 0 my $typek = $img_type{$name};
81 0 ▶ die "undefined name" if (not defined $name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
82 0 ▶ die "undefined typek for img $name" if (not defined $typek);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $typek 0 0 0
83 0 return "$name.$typek";
84 }
85  
86 0 ▶sub getImageUrl {
Subroutine getImageUrl: not called CC=6 SCAR=37.4
Pod undocumented
87 0 my $name = shift;
88 0 my $params = shift;
89 0 ▶ my $forceBagWrite = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
90  
91 0 my $bagName = getBagForImage($name);
92  
93 0 my $bagUrl = FAQ::OMatic::makeBagRef($bagName, $params);
94  
95 0 my $bagPath = $FAQ::OMatic::Config::bagsDir.$bagName;
96 0 ▶ if (-f $bagPath and not $forceBagWrite) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $bagPath and not $forceBagWrite) 0 0 0
Condition0%0/3
-f $bagPath and not $forceBagWrite
!ll&&!rl&&r
000
MC/DC0%0/2
-f $bagPath and not $forceBagWrite
-f $bagPath not $forceBagWrite
Truth table0%0/3
A and not(B)
A= -f $bagPathB= not $forceBagWrite
ABresult
0X0
100
111
97 0 return $bagUrl;
98 }
99 0 ▶ if (not defined $FAQ::OMatic::Config::bagsDir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $FAQ::OMatic::Config::bagsDir) 0 0 0
100 # fail obviously if bagsDir not configured -- this
101 # happens when upgrading versions
102 0 return "x:";
103 }
104  
105 # attempt to cache this image file in $bagsDir
106 0 ▶ if (not open(CACHEIMAGE, ">$bagPath")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open CACHEIMAGE, ">$bagPath") 0 0 0
107 0 FAQ::OMatic::gripe('problem',
108 "write to $bagPath failed: $!");
109 # TODO: write to cache failed. Notify admin?
110 # in the meantime, supply user with dynamically-generated image file
111 0 return FAQ::OMatic::makeAref('-command'=>'img',
112 '-blastAll'=>1,
113 '-changedParams'=>{'name'=>$name},
114 '-refType'=>'url');
115 }
116  
117 # bag is saved, now write a .desc file
118 0 my $bagDesc = FAQ::OMatic::Bags::getBagDesc($bagName);
119  
120 0 $bagDesc->setProperty('SizeWidth', $img_prop{$name}{'SizeWidth'});
121 0 $bagDesc->setProperty('SizeHeight', $img_prop{$name}{'SizeHeight'});
122 0 $bagDesc->setProperty('SizeBytes', $img_prop{$name}{'SizeBytes'});
123 0 $bagDesc->setProperty('Alt', $img_prop{$name}{'Alt'});
124 0 FAQ::OMatic::Bags::saveBagDesc($bagDesc);
125  
126 0 print CACHEIMAGE getImage($name);
127 0 close CACHEIMAGE;
128 0 return $bagUrl;
129 }
130  
131 0 ▶sub getImageRef {
Subroutine getImageRef: not called CC=6 SCAR=37.4
Pod undocumented
132 0 my $name = shift;
133 0 my $imgargs = shift;
134 0 my $params = shift;
135  
136 0 ▶ return ' ' if (defined($FAQ::OMatic::Config::noImages));
Branch: 1 branch
BranchTrueFalseTotal
if defined $FAQ::OMatic::Config::noImages 0 0 0
137  
138 0 my $url = getImageUrl($name, $params);
139 0 ▶ return "[bad image $name]" if (not $url);
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
140  
141 # look up size information to append to img tag
142 0 my $bagName = getBagForImage($name);
143 0 my $sw = FAQ::OMatic::Bags::getBagProperty($bagName, 'SizeWidth', '');
144 0 ▶ my $swt = ($sw ne '') ? " width=$sw" : '';
Branch: 1 branch
BranchTrueFalseTotal
$sw ne '' ? : 0 0 0
145 0 my $sh = FAQ::OMatic::Bags::getBagProperty($bagName, 'SizeHeight', '');
146 0 ▶ my $sht = ($sh ne '') ? " height=$sh" : '';
Branch: 1 branch
BranchTrueFalseTotal
$sh ne '' ? : 0 0 0
147 0 my $alt = FAQ::OMatic::Bags::getBagProperty($bagName, 'Alt', $name);
148  
149 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
150 0 return ("<img src=\"$url\" alt=$alt $imgargs$swt$sht>", $sw, $sh);
151 } else {
152 0 return "<img src=\"$url\" alt=\"$alt \" $imgargs$swt$sht>";
153 }
154 }
155  
156 0 ▶sub getImageRefCA {
Subroutine getImageRefCA: not called CC=2 SCAR=17.9
Pod undocumented
157 0 my $name = shift;
158 0 my $imgargs = shift;
159 0 my $ca = shift; # Category or Answer
160 0 my $params = shift;
161  
162 0 ▶ $name = ($ca ? 'cat' : 'ans').$name;
Branch: 1 branch
BranchTrueFalseTotal
$ca ? : 0 0 0
163  
164 0 return getImageRef($name, $imgargs, $params);
165 }
166  
167 0 ▶sub bagAllImages {
Subroutine bagAllImages: not called CC=2 SCAR=17.9
Pod undocumented
168 # writes every image in ImageData to the bags dir
169 0 my $imgname;
170 0 foreach $imgname (sort keys %img_type) {
171 0 FAQ::OMatic::maintenance::hprint(
172 "<br>bagging ".getBagForImage($imgname)."\n");
173 0 FAQ::OMatic::maintenance::hflush();
174 0 getImageUrl($imgname, {}, 'forceBagWrite');
175 }
176 }
177  
178 # these properties are manually-defined, so they're not in the
179 # section of this file that gets automatically regenerated.
180 1 $img_prop{'ans-also'}{'Alt'} = '(Xref)';
181 1 $img_prop{'ans'}{'Alt'} = '(Answer)';
182 1 $img_prop{'ans-small'}{'Alt'} = '(Answer)';
183 1 $img_prop{'baglink'}{'Alt'} = '(Download)';
184 1 $img_prop{'cat-also'}{'Alt'} = '(Xref)';
185 1 $img_prop{'cat-small'}{'Alt'} = '(Category)';
186 1 $img_prop{'cat'}{'Alt'} = '(Category)';
187 1 $img_prop{'help-small'}{'Alt'} = '(?)';
188 1 $img_prop{'help'}{'Alt'} = '(?)';
189  
190 # to regenerate:
191 #:.,$-2!(cd ../../../img; ../dev-bin/encodeBin.pl -desc *)
192 1 $img_type{'ans-also'} = 'gif';
193  
194 1 $img_prop{'ans-also'}{'SizeWidth'} = '21';
195 1 $img_prop{'ans-also'}{'SizeHeight'} = '14';
196 1 $img_prop{'ans-also'}{'SizeBytes'} = '128';
197  
198 1 $img_type{'ans-del-part'} = 'gif';
199  
200 1 $img_prop{'ans-del-part'}{'SizeWidth'} = '23';
201 1 $img_prop{'ans-del-part'}{'SizeHeight'} = '28';
202 1 $img_prop{'ans-del-part'}{'SizeBytes'} = '183';
203  
204 1 $img_type{'ans-dup-ans'} = 'gif';
205  
206 1 $img_prop{'ans-dup-ans'}{'SizeWidth'} = '24';
207 1 $img_prop{'ans-dup-ans'}{'SizeHeight'} = '23';
208 1 $img_prop{'ans-dup-ans'}{'SizeBytes'} = '182';
209  
210 1 $img_type{'ans-dup-part'} = 'gif';
211  
212 1 $img_prop{'ans-dup-part'}{'SizeWidth'} = '23';
213 1 $img_prop{'ans-dup-part'}{'SizeHeight'} = '32';
214 1 $img_prop{'ans-dup-part'}{'SizeBytes'} = '194';
215  
216 1 $img_type{'ans-edit-part'} = 'gif';
217  
218 1 $img_prop{'ans-edit-part'}{'SizeWidth'} = '32';
219 1 $img_prop{'ans-edit-part'}{'SizeHeight'} = '31';
220 1 $img_prop{'ans-edit-part'}{'SizeBytes'} = '285';
221  
222 1 $img_type{'ans-ins-part'} = 'gif';
223  
224 1 $img_prop{'ans-ins-part'}{'SizeWidth'} = '32';
225 1 $img_prop{'ans-ins-part'}{'SizeHeight'} = '32';
226 1 $img_prop{'ans-ins-part'}{'SizeBytes'} = '261';
227  
228 1 $img_type{'ans-opts'} = 'gif';
229  
230 1 $img_prop{'ans-opts'}{'SizeWidth'} = '23';
231 1 $img_prop{'ans-opts'}{'SizeHeight'} = '28';
232 1 $img_prop{'ans-opts'}{'SizeBytes'} = '131';
233  
234 1 $img_type{'ans-reorder'} = 'gif';
235  
236 1 $img_prop{'ans-reorder'}{'SizeWidth'} = '23';
237 1 $img_prop{'ans-reorder'}{'SizeHeight'} = '28';
238 1 $img_prop{'ans-reorder'}{'SizeBytes'} = '191';
239  
240 1 $img_type{'ans-small'} = 'gif';
241  
242 1 $img_prop{'ans-small'}{'SizeWidth'} = '18';
243 1 $img_prop{'ans-small'}{'SizeHeight'} = '14';
244 1 $img_prop{'ans-small'}{'SizeBytes'} = '100';
245  
246 1 $img_type{'ans-title'} = 'gif';
247  
248 1 $img_prop{'ans-title'}{'SizeWidth'} = '32';
249 1 $img_prop{'ans-title'}{'SizeHeight'} = '32';
250 1 $img_prop{'ans-title'}{'SizeBytes'} = '257';
251  
252 1 $img_type{'ans-to-cat'} = 'gif';
253  
254 1 $img_prop{'ans-to-cat'}{'SizeWidth'} = '29';
255 1 $img_prop{'ans-to-cat'}{'SizeHeight'} = '26';
256 1 $img_prop{'ans-to-cat'}{'SizeBytes'} = '223';
257  
258 1 $img_type{'ans'} = 'gif';
259  
260 1 $img_prop{'ans'}{'SizeWidth'} = '23';
261 1 $img_prop{'ans'}{'SizeHeight'} = '28';
262 1 $img_prop{'ans'}{'SizeBytes'} = '150';
263  
264 1 $img_type{'baglink'} = 'gif';
265  
266 1 $img_prop{'baglink'}{'SizeWidth'} = '21';
267 1 $img_prop{'baglink'}{'SizeHeight'} = '14';
268 1 $img_prop{'baglink'}{'SizeBytes'} = '103';
269  
270 1 $img_type{'cat-also'} = 'gif';
271  
272 1 $img_prop{'cat-also'}{'SizeWidth'} = '25';
273 1 $img_prop{'cat-also'}{'SizeHeight'} = '14';
274 1 $img_prop{'cat-also'}{'SizeBytes'} = '139';
275  
276 1 $img_type{'cat-del-part'} = 'gif';
277  
278 1 $img_prop{'cat-del-part'}{'SizeWidth'} = '32';
279 1 $img_prop{'cat-del-part'}{'SizeHeight'} = '27';
280 1 $img_prop{'cat-del-part'}{'SizeBytes'} = '208';
281  
282 1 $img_type{'cat-dup-ans'} = 'gif';
283  
284 1 $img_prop{'cat-dup-ans'}{'SizeWidth'} = '26';
285 1 $img_prop{'cat-dup-ans'}{'SizeHeight'} = '23';
286 1 $img_prop{'cat-dup-ans'}{'SizeBytes'} = '201';
287  
288 1 $img_type{'cat-dup-part'} = 'gif';
289  
290 1 $img_prop{'cat-dup-part'}{'SizeWidth'} = '32';
291 1 $img_prop{'cat-dup-part'}{'SizeHeight'} = '27';
292 1 $img_prop{'cat-dup-part'}{'SizeBytes'} = '201';
293  
294 1 $img_type{'cat-edit-part'} = 'gif';
295  
296 1 $img_prop{'cat-edit-part'}{'SizeWidth'} = '32';
297 1 $img_prop{'cat-edit-part'}{'SizeHeight'} = '31';
298 1 $img_prop{'cat-edit-part'}{'SizeBytes'} = '286';
299  
300 1 $img_type{'cat-ins-part'} = 'gif';
301  
302 1 $img_prop{'cat-ins-part'}{'SizeWidth'} = '32';
303 1 $img_prop{'cat-ins-part'}{'SizeHeight'} = '27';
304 1 $img_prop{'cat-ins-part'}{'SizeBytes'} = '250';
305  
306 1 $img_type{'cat-new-ans'} = 'gif';
307  
308 1 $img_prop{'cat-new-ans'}{'SizeWidth'} = '32';
309 1 $img_prop{'cat-new-ans'}{'SizeHeight'} = '32';
310 1 $img_prop{'cat-new-ans'}{'SizeBytes'} = '253';
311  
312 1 $img_type{'cat-new-cat'} = 'gif';
313  
314 1 $img_prop{'cat-new-cat'}{'SizeWidth'} = '32';
315 1 $img_prop{'cat-new-cat'}{'SizeHeight'} = '32';
316 1 $img_prop{'cat-new-cat'}{'SizeBytes'} = '245';
317  
318 1 $img_type{'cat-opts'} = 'gif';
319  
320 1 $img_prop{'cat-opts'}{'SizeWidth'} = '32';
321 1 $img_prop{'cat-opts'}{'SizeHeight'} = '27';
322 1 $img_prop{'cat-opts'}{'SizeBytes'} = '165';
323  
324 1 $img_type{'cat-reorder'} = 'gif';
325  
326 1 $img_prop{'cat-reorder'}{'SizeWidth'} = '32';
327 1 $img_prop{'cat-reorder'}{'SizeHeight'} = '27';
328 1 $img_prop{'cat-reorder'}{'SizeBytes'} = '207';
329  
330 1 $img_type{'cat-small'} = 'gif';
331  
332 1 $img_prop{'cat-small'}{'SizeWidth'} = '18';
333 1 $img_prop{'cat-small'}{'SizeHeight'} = '14';
334 1 $img_prop{'cat-small'}{'SizeBytes'} = '104';
335  
336 1 $img_type{'cat-title'} = 'gif';
337  
338 1 $img_prop{'cat-title'}{'SizeWidth'} = '32';
339 1 $img_prop{'cat-title'}{'SizeHeight'} = '32';
340 1 $img_prop{'cat-title'}{'SizeBytes'} = '232';
341  
342 1 $img_type{'cat'} = 'gif';
343  
344 1 $img_prop{'cat'}{'SizeWidth'} = '32';
345 1 $img_prop{'cat'}{'SizeHeight'} = '27';
346 1 $img_prop{'cat'}{'SizeBytes'} = '185';
347  
348 1 $img_type{'checked-large'} = 'gif';
349  
350 1 $img_prop{'checked-large'}{'SizeWidth'} = '20';
351 1 $img_prop{'checked-large'}{'SizeHeight'} = '24';
352 1 $img_prop{'checked-large'}{'SizeBytes'} = '139';
353  
354 1 $img_type{'checked'} = 'gif';
355  
356 1 $img_prop{'checked'}{'SizeWidth'} = '16';
357 1 $img_prop{'checked'}{'SizeHeight'} = '17';
358 1 $img_prop{'checked'}{'SizeBytes'} = '104';
359  
360 1 $img_type{'help-small'} = 'gif';
361  
362 1 $img_prop{'help-small'}{'SizeWidth'} = '16';
363 1 $img_prop{'help-small'}{'SizeHeight'} = '12';
364 1 $img_prop{'help-small'}{'SizeBytes'} = '108';
365  
366 1 $img_type{'help'} = 'gif';
367  
368 1 $img_prop{'help'}{'SizeWidth'} = '32';
369 1 $img_prop{'help'}{'SizeHeight'} = '24';
370 1 $img_prop{'help'}{'SizeBytes'} = '181';
371  
372 1 $img_prop{'picker'}{'SizeWidth'} = '256';
373 1 $img_prop{'picker'}{'SizeHeight'} = '128';
374 1 $img_prop{'picker'}{'SizeBytes'} = '3189';
375  
376 1 $img_type{'picker'} = 'jpg';
377  
378 1 $img_type{'space-large'} = 'gif';
379  
380 1 $img_prop{'space-large'}{'SizeWidth'} = '20';
381 1 $img_prop{'space-large'}{'SizeHeight'} = '25';
382 1 $img_prop{'space-large'}{'SizeBytes'} = '61';
383  
384 1 $img_type{'space'} = 'gif';
385  
386 1 $img_prop{'space'}{'SizeWidth'} = '16';
387 1 $img_prop{'space'}{'SizeHeight'} = '16';
388 1 $img_prop{'space'}{'SizeBytes'} = '55';
389  
390 1 $img_type{'unchecked'} = 'gif';
391  
392 1 $img_prop{'unchecked'}{'SizeWidth'} = '16';
393 1 $img_prop{'unchecked'}{'SizeHeight'} = '16';
394 1 $img_prop{'unchecked'}{'SizeBytes'} = '79';
395  
396 1 $img_type{'cat-to-ans'} = 'gif';
397  
398 1 $img_prop{'cat-to-ans'}{'SizeWidth'} = '29';
399 1 $img_prop{'cat-to-ans'}{'SizeHeight'} = '26';
400 1 $img_prop{'cat-to-ans'}{'SizeBytes'} = '213';
401  
402  
403 1 1;
« OMatic/AuthLocal.pm ↑ Summary OMatic/submitCatToAns.pm »