newlines in wfDebug() calls
[lhc/web/wiklou.git] / includes / media / FormatMetadata.php
1 <?php
2 /**
3 * Formatting of image metadata values into human readable form.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Media
21 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
22 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason, 2009 Brent Garber, 2010 Brian Wolff
23 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
24 * @see http://exif.org/Exif2-2.PDF The Exif 2.2 specification
25 * @file
26 */
27
28 /**
29 * Format Image metadata values into a human readable form.
30 *
31 * Note lots of these messages use the prefix 'exif' even though
32 * they may not be exif properties. For example 'exif-ImageDescription'
33 * can be the Exif ImageDescription, or it could be the iptc-iim caption
34 * property, or it could be the xmp dc:description property. This
35 * is because these messages should be independent of how the data is
36 * stored, sine the user doesn't care if the description is stored in xmp,
37 * exif, etc only that its a description. (Additionally many of these properties
38 * are merged together following the MWG standard, such that for example,
39 * exif properties override XMP properties that mean the same thing if
40 * there is a conflict).
41 *
42 * It should perhaps use a prefix like 'metadata' instead, but there
43 * is already a large number of messages using the 'exif' prefix.
44 *
45 * @ingroup Media
46 */
47 class FormatMetadata {
48
49 /**
50 * Numbers given by Exif user agents are often magical, that is they
51 * should be replaced by a detailed explanation depending on their
52 * value which most of the time are plain integers. This function
53 * formats Exif (and other metadata) values into human readable form.
54 *
55 * @param array $tags the Exif data to format ( as returned by
56 * Exif::getFilteredData() or BitmapMetadataHandler )
57 * @return array
58 */
59 public static function getFormattedData( $tags ) {
60 global $wgLang;
61
62 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
63 unset( $tags['ResolutionUnit'] );
64
65 foreach ( $tags as $tag => &$vals ) {
66
67 // This seems ugly to wrap non-array's in an array just to unwrap again,
68 // especially when most of the time it is not an array
69 if ( !is_array( $tags[$tag] ) ) {
70 $vals = Array( $vals );
71 }
72
73 // _type is a special value to say what array type
74 if ( isset( $tags[$tag]['_type'] ) ) {
75 $type = $tags[$tag]['_type'];
76 unset( $vals['_type'] );
77 } else {
78 $type = 'ul'; // default unordered list.
79 }
80
81 //This is done differently as the tag is an array.
82 if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) {
83 //hour min sec array
84
85 $h = explode( '/', $vals[0] );
86 $m = explode( '/', $vals[1] );
87 $s = explode( '/', $vals[2] );
88
89 // this should already be validated
90 // when loaded from file, but it could
91 // come from a foreign repo, so be
92 // paranoid.
93 if ( !isset( $h[1] )
94 || !isset( $m[1] )
95 || !isset( $s[1] )
96 || $h[1] == 0
97 || $m[1] == 0
98 || $s[1] == 0
99 ) {
100 continue;
101 }
102 $tags[$tag] = str_pad( intval( $h[0] / $h[1] ), 2, '0', STR_PAD_LEFT )
103 . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT )
104 . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT );
105
106 try {
107 $time = wfTimestamp( TS_MW, '1971:01:01 ' . $tags[$tag] );
108 // the 1971:01:01 is just a placeholder, and not shown to user.
109 if ( $time && intval( $time ) > 0 ) {
110 $tags[$tag] = $wgLang->time( $time );
111 }
112 } catch ( TimestampException $e ) {
113 // This shouldn't happen, but we've seen bad formats
114 // such as 4-digit seconds in the wild.
115 // leave $tags[$tag] as-is
116 }
117 continue;
118 }
119
120 // The contact info is a multi-valued field
121 // instead of the other props which are single
122 // valued (mostly) so handle as a special case.
123 if ( $tag === 'Contact' ) {
124 $vals = self::collapseContactInfo( $vals );
125 continue;
126 }
127
128 foreach ( $vals as &$val ) {
129
130 switch ( $tag ) {
131 case 'Compression':
132 switch ( $val ) {
133 case 1: case 2: case 3: case 4:
134 case 5: case 6: case 7: case 8:
135 case 32773: case 32946: case 34712:
136 $val = self::msg( $tag, $val );
137 break;
138 default:
139 /* If not recognized, display as is. */
140 break;
141 }
142 break;
143
144 case 'PhotometricInterpretation':
145 switch ( $val ) {
146 case 2: case 6:
147 $val = self::msg( $tag, $val );
148 break;
149 default:
150 /* If not recognized, display as is. */
151 break;
152 }
153 break;
154
155 case 'Orientation':
156 switch ( $val ) {
157 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
158 $val = self::msg( $tag, $val );
159 break;
160 default:
161 /* If not recognized, display as is. */
162 break;
163 }
164 break;
165
166 case 'PlanarConfiguration':
167 switch ( $val ) {
168 case 1: case 2:
169 $val = self::msg( $tag, $val );
170 break;
171 default:
172 /* If not recognized, display as is. */
173 break;
174 }
175 break;
176
177 // TODO: YCbCrSubSampling
178 case 'YCbCrPositioning':
179 switch ( $val ) {
180 case 1:
181 case 2:
182 $val = self::msg( $tag, $val );
183 break;
184 default:
185 /* If not recognized, display as is. */
186 break;
187 }
188 break;
189
190 case 'XResolution':
191 case 'YResolution':
192 switch ( $resolutionunit ) {
193 case 2:
194 $val = self::msg( 'XYResolution', 'i', self::formatNum( $val ) );
195 break;
196 case 3:
197 $val = self::msg( 'XYResolution', 'c', self::formatNum( $val ) );
198 break;
199 default:
200 /* If not recognized, display as is. */
201 break;
202 }
203 break;
204
205 // TODO: YCbCrCoefficients #p27 (see annex E)
206 case 'ExifVersion': case 'FlashpixVersion':
207 $val = "$val" / 100;
208 break;
209
210 case 'ColorSpace':
211 switch ( $val ) {
212 case 1: case 65535:
213 $val = self::msg( $tag, $val );
214 break;
215 default:
216 /* If not recognized, display as is. */
217 break;
218 }
219 break;
220
221 case 'ComponentsConfiguration':
222 switch ( $val ) {
223 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
224 $val = self::msg( $tag, $val );
225 break;
226 default:
227 /* If not recognized, display as is. */
228 break;
229 }
230 break;
231
232 case 'DateTime':
233 case 'DateTimeOriginal':
234 case 'DateTimeDigitized':
235 case 'DateTimeReleased':
236 case 'DateTimeExpires':
237 case 'GPSDateStamp':
238 case 'dc-date':
239 case 'DateTimeMetadata':
240 if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) {
241 $val = wfMessage( 'exif-unknowndate' )->text();
242 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
243 // Full date.
244 $time = wfTimestamp( TS_MW, $val );
245 if ( $time && intval( $time ) > 0 ) {
246 $val = $wgLang->timeanddate( $time );
247 }
248 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
249 // No second field. Still format the same
250 // since timeanddate doesn't include seconds anyways,
251 // but second still available in api
252 $time = wfTimestamp( TS_MW, $val . ':00' );
253 if ( $time && intval( $time ) > 0 ) {
254 $val = $wgLang->timeanddate( $time );
255 }
256 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
257 // If only the date but not the time is filled in.
258 $time = wfTimestamp( TS_MW, substr( $val, 0, 4 )
259 . substr( $val, 5, 2 )
260 . substr( $val, 8, 2 )
261 . '000000' );
262 if ( $time && intval( $time ) > 0 ) {
263 $val = $wgLang->date( $time );
264 }
265 }
266 // else it will just output $val without formatting it.
267 break;
268
269 case 'ExposureProgram':
270 switch ( $val ) {
271 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
272 $val = self::msg( $tag, $val );
273 break;
274 default:
275 /* If not recognized, display as is. */
276 break;
277 }
278 break;
279
280 case 'SubjectDistance':
281 $val = self::msg( $tag, '', self::formatNum( $val ) );
282 break;
283
284 case 'MeteringMode':
285 switch ( $val ) {
286 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
287 $val = self::msg( $tag, $val );
288 break;
289 default:
290 /* If not recognized, display as is. */
291 break;
292 }
293 break;
294
295 case 'LightSource':
296 switch ( $val ) {
297 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
298 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
299 case 21: case 22: case 23: case 24: case 255:
300 $val = self::msg( $tag, $val );
301 break;
302 default:
303 /* If not recognized, display as is. */
304 break;
305 }
306 break;
307
308 case 'Flash':
309 $flashDecode = array(
310 'fired' => $val & bindec( '00000001' ),
311 'return' => ( $val & bindec( '00000110' ) ) >> 1,
312 'mode' => ( $val & bindec( '00011000' ) ) >> 3,
313 'function' => ( $val & bindec( '00100000' ) ) >> 5,
314 'redeye' => ( $val & bindec( '01000000' ) ) >> 6,
315 // 'reserved' => ($val & bindec( '10000000' )) >> 7,
316 );
317 $flashMsgs = array();
318 # We do not need to handle unknown values since all are used.
319 foreach ( $flashDecode as $subTag => $subValue ) {
320 # We do not need any message for zeroed values.
321 if ( $subTag != 'fired' && $subValue == 0 ) {
322 continue;
323 }
324 $fullTag = $tag . '-' . $subTag;
325 $flashMsgs[] = self::msg( $fullTag, $subValue );
326 }
327 $val = $wgLang->commaList( $flashMsgs );
328 break;
329
330 case 'FocalPlaneResolutionUnit':
331 switch ( $val ) {
332 case 2:
333 $val = self::msg( $tag, $val );
334 break;
335 default:
336 /* If not recognized, display as is. */
337 break;
338 }
339 break;
340
341 case 'SensingMethod':
342 switch ( $val ) {
343 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
344 $val = self::msg( $tag, $val );
345 break;
346 default:
347 /* If not recognized, display as is. */
348 break;
349 }
350 break;
351
352 case 'FileSource':
353 switch ( $val ) {
354 case 3:
355 $val = self::msg( $tag, $val );
356 break;
357 default:
358 /* If not recognized, display as is. */
359 break;
360 }
361 break;
362
363 case 'SceneType':
364 switch ( $val ) {
365 case 1:
366 $val = self::msg( $tag, $val );
367 break;
368 default:
369 /* If not recognized, display as is. */
370 break;
371 }
372 break;
373
374 case 'CustomRendered':
375 switch ( $val ) {
376 case 0: case 1:
377 $val = self::msg( $tag, $val );
378 break;
379 default:
380 /* If not recognized, display as is. */
381 break;
382 }
383 break;
384
385 case 'ExposureMode':
386 switch ( $val ) {
387 case 0: case 1: case 2:
388 $val = self::msg( $tag, $val );
389 break;
390 default:
391 /* If not recognized, display as is. */
392 break;
393 }
394 break;
395
396 case 'WhiteBalance':
397 switch ( $val ) {
398 case 0: case 1:
399 $val = self::msg( $tag, $val );
400 break;
401 default:
402 /* If not recognized, display as is. */
403 break;
404 }
405 break;
406
407 case 'SceneCaptureType':
408 switch ( $val ) {
409 case 0: case 1: case 2: case 3:
410 $val = self::msg( $tag, $val );
411 break;
412 default:
413 /* If not recognized, display as is. */
414 break;
415 }
416 break;
417
418 case 'GainControl':
419 switch ( $val ) {
420 case 0: case 1: case 2: case 3: case 4:
421 $val = self::msg( $tag, $val );
422 break;
423 default:
424 /* If not recognized, display as is. */
425 break;
426 }
427 break;
428
429 case 'Contrast':
430 switch ( $val ) {
431 case 0: case 1: case 2:
432 $val = self::msg( $tag, $val );
433 break;
434 default:
435 /* If not recognized, display as is. */
436 break;
437 }
438 break;
439
440 case 'Saturation':
441 switch ( $val ) {
442 case 0: case 1: case 2:
443 $val = self::msg( $tag, $val );
444 break;
445 default:
446 /* If not recognized, display as is. */
447 break;
448 }
449 break;
450
451 case 'Sharpness':
452 switch ( $val ) {
453 case 0: case 1: case 2:
454 $val = self::msg( $tag, $val );
455 break;
456 default:
457 /* If not recognized, display as is. */
458 break;
459 }
460 break;
461
462 case 'SubjectDistanceRange':
463 switch ( $val ) {
464 case 0: case 1: case 2: case 3:
465 $val = self::msg( $tag, $val );
466 break;
467 default:
468 /* If not recognized, display as is. */
469 break;
470 }
471 break;
472
473 //The GPS...Ref values are kept for compatibility, probably won't be reached.
474 case 'GPSLatitudeRef':
475 case 'GPSDestLatitudeRef':
476 switch ( $val ) {
477 case 'N': case 'S':
478 $val = self::msg( 'GPSLatitude', $val );
479 break;
480 default:
481 /* If not recognized, display as is. */
482 break;
483 }
484 break;
485
486 case 'GPSLongitudeRef':
487 case 'GPSDestLongitudeRef':
488 switch ( $val ) {
489 case 'E': case 'W':
490 $val = self::msg( 'GPSLongitude', $val );
491 break;
492 default:
493 /* If not recognized, display as is. */
494 break;
495 }
496 break;
497
498 case 'GPSAltitude':
499 if ( $val < 0 ) {
500 $val = self::msg( 'GPSAltitude', 'below-sealevel', self::formatNum( -$val, 3 ) );
501 } else {
502 $val = self::msg( 'GPSAltitude', 'above-sealevel', self::formatNum( $val, 3 ) );
503 }
504 break;
505
506 case 'GPSStatus':
507 switch ( $val ) {
508 case 'A': case 'V':
509 $val = self::msg( $tag, $val );
510 break;
511 default:
512 /* If not recognized, display as is. */
513 break;
514 }
515 break;
516
517 case 'GPSMeasureMode':
518 switch ( $val ) {
519 case 2: case 3:
520 $val = self::msg( $tag, $val );
521 break;
522 default:
523 /* If not recognized, display as is. */
524 break;
525 }
526 break;
527
528 case 'GPSTrackRef':
529 case 'GPSImgDirectionRef':
530 case 'GPSDestBearingRef':
531 switch ( $val ) {
532 case 'T': case 'M':
533 $val = self::msg( 'GPSDirection', $val );
534 break;
535 default:
536 /* If not recognized, display as is. */
537 break;
538 }
539 break;
540
541 case 'GPSLatitude':
542 case 'GPSDestLatitude':
543 $val = self::formatCoords( $val, 'latitude' );
544 break;
545 case 'GPSLongitude':
546 case 'GPSDestLongitude':
547 $val = self::formatCoords( $val, 'longitude' );
548 break;
549
550 case 'GPSSpeedRef':
551 switch ( $val ) {
552 case 'K': case 'M': case 'N':
553 $val = self::msg( 'GPSSpeed', $val );
554 break;
555 default:
556 /* If not recognized, display as is. */
557 break;
558 }
559 break;
560
561 case 'GPSDestDistanceRef':
562 switch ( $val ) {
563 case 'K': case 'M': case 'N':
564 $val = self::msg( 'GPSDestDistance', $val );
565 break;
566 default:
567 /* If not recognized, display as is. */
568 break;
569 }
570 break;
571
572 case 'GPSDOP':
573 // See http://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
574 if ( $val <= 2 ) {
575 $val = self::msg( $tag, 'excellent', self::formatNum( $val ) );
576 } elseif ( $val <= 5 ) {
577 $val = self::msg( $tag, 'good', self::formatNum( $val ) );
578 } elseif ( $val <= 10 ) {
579 $val = self::msg( $tag, 'moderate', self::formatNum( $val ) );
580 } elseif ( $val <= 20 ) {
581 $val = self::msg( $tag, 'fair', self::formatNum( $val ) );
582 } else {
583 $val = self::msg( $tag, 'poor', self::formatNum( $val ) );
584 }
585 break;
586
587 // This is not in the Exif standard, just a special
588 // case for our purposes which enables wikis to wikify
589 // the make, model and software name to link to their articles.
590 case 'Make':
591 case 'Model':
592 $val = self::msg( $tag, '', $val );
593 break;
594
595 case 'Software':
596 if ( is_array( $val ) ) {
597 //if its a software, version array.
598 $val = wfMessage( 'exif-software-version-value', $val[0], $val[1] )->text();
599 } else {
600 $val = self::msg( $tag, '', $val );
601 }
602 break;
603
604 case 'ExposureTime':
605 // Show the pretty fraction as well as decimal version
606 $val = wfMessage( 'exif-exposuretime-format',
607 self::formatFraction( $val ), self::formatNum( $val ) )->text();
608 break;
609 case 'ISOSpeedRatings':
610 // If its = 65535 that means its at the
611 // limit of the size of Exif::short and
612 // is really higher.
613 if ( $val == '65535' ) {
614 $val = self::msg( $tag, 'overflow' );
615 } else {
616 $val = self::formatNum( $val );
617 }
618 break;
619 case 'FNumber':
620 $val = wfMessage( 'exif-fnumber-format',
621 self::formatNum( $val ) )->text();
622 break;
623
624 case 'FocalLength': case 'FocalLengthIn35mmFilm':
625 $val = wfMessage( 'exif-focallength-format',
626 self::formatNum( $val ) )->text();
627 break;
628
629 case 'MaxApertureValue':
630 if ( strpos( $val, '/' ) !== false ) {
631 // need to expand this earlier to calculate fNumber
632 list( $n, $d ) = explode( '/', $val );
633 if ( is_numeric( $n ) && is_numeric( $d ) ) {
634 $val = $n / $d;
635 }
636 }
637 if ( is_numeric( $val ) ) {
638 $fNumber = pow( 2, $val / 2 );
639 if ( $fNumber !== false ) {
640 $val = wfMessage( 'exif-maxaperturevalue-value',
641 self::formatNum( $val ),
642 self::formatNum( $fNumber, 2 )
643 )->text();
644 }
645 }
646 break;
647
648 case 'iimCategory':
649 switch ( strtolower( $val ) ) {
650 // See pg 29 of IPTC photo
651 // metadata standard.
652 case 'ace': case 'clj':
653 case 'dis': case 'fin':
654 case 'edu': case 'evn':
655 case 'hth': case 'hum':
656 case 'lab': case 'lif':
657 case 'pol': case 'rel':
658 case 'sci': case 'soi':
659 case 'spo': case 'war':
660 case 'wea':
661 $val = self::msg(
662 'iimcategory',
663 $val
664 );
665 }
666 break;
667 case 'SubjectNewsCode':
668 // Essentially like iimCategory.
669 // 8 (numeric) digit hierarchical
670 // classification. We decode the
671 // first 2 digits, which provide
672 // a broad category.
673 $val = self::convertNewsCode( $val );
674 break;
675 case 'Urgency':
676 // 1-8 with 1 being highest, 5 normal
677 // 0 is reserved, and 9 is 'user-defined'.
678 $urgency = '';
679 if ( $val == 0 || $val == 9 ) {
680 $urgency = 'other';
681 } elseif ( $val < 5 && $val > 1 ) {
682 $urgency = 'high';
683 } elseif ( $val == 5 ) {
684 $urgency = 'normal';
685 } elseif ( $val <= 8 && $val > 5 ) {
686 $urgency = 'low';
687 }
688
689 if ( $urgency !== '' ) {
690 $val = self::msg( 'urgency',
691 $urgency, $val
692 );
693 }
694 break;
695
696 // Things that have a unit of pixels.
697 case 'OriginalImageHeight':
698 case 'OriginalImageWidth':
699 case 'PixelXDimension':
700 case 'PixelYDimension':
701 case 'ImageWidth':
702 case 'ImageLength':
703 $val = self::formatNum( $val ) . ' ' . wfMessage( 'unit-pixel' )->text();
704 break;
705
706 // Do not transform fields with pure text.
707 // For some languages the formatNum()
708 // conversion results to wrong output like
709 // foo,bar@example,com or foo٫bar@example٫com.
710 // Also some 'numeric' things like Scene codes
711 // are included here as we really don't want
712 // commas inserted.
713 case 'ImageDescription':
714 case 'Artist':
715 case 'Copyright':
716 case 'RelatedSoundFile':
717 case 'ImageUniqueID':
718 case 'SpectralSensitivity':
719 case 'GPSSatellites':
720 case 'GPSVersionID':
721 case 'GPSMapDatum':
722 case 'Keywords':
723 case 'WorldRegionDest':
724 case 'CountryDest':
725 case 'CountryCodeDest':
726 case 'ProvinceOrStateDest':
727 case 'CityDest':
728 case 'SublocationDest':
729 case 'WorldRegionCreated':
730 case 'CountryCreated':
731 case 'CountryCodeCreated':
732 case 'ProvinceOrStateCreated':
733 case 'CityCreated':
734 case 'SublocationCreated':
735 case 'ObjectName':
736 case 'SpecialInstructions':
737 case 'Headline':
738 case 'Credit':
739 case 'Source':
740 case 'EditStatus':
741 case 'FixtureIdentifier':
742 case 'LocationDest':
743 case 'LocationDestCode':
744 case 'Writer':
745 case 'JPEGFileComment':
746 case 'iimSupplementalCategory':
747 case 'OriginalTransmissionRef':
748 case 'Identifier':
749 case 'dc-contributor':
750 case 'dc-coverage':
751 case 'dc-publisher':
752 case 'dc-relation':
753 case 'dc-rights':
754 case 'dc-source':
755 case 'dc-type':
756 case 'Lens':
757 case 'SerialNumber':
758 case 'CameraOwnerName':
759 case 'Label':
760 case 'Nickname':
761 case 'RightsCertificate':
762 case 'CopyrightOwner':
763 case 'UsageTerms':
764 case 'WebStatement':
765 case 'OriginalDocumentID':
766 case 'LicenseUrl':
767 case 'MorePermissionsUrl':
768 case 'AttributionUrl':
769 case 'PreferredAttributionName':
770 case 'PNGFileComment':
771 case 'Disclaimer':
772 case 'ContentWarning':
773 case 'GIFFileComment':
774 case 'SceneCode':
775 case 'IntellectualGenre':
776 case 'Event':
777 case 'OrginisationInImage':
778 case 'PersonInImage':
779
780 $val = htmlspecialchars( $val );
781 break;
782
783 case 'ObjectCycle':
784 switch ( $val ) {
785 case 'a': case 'p': case 'b':
786 $val = self::msg( $tag, $val );
787 break;
788 default:
789 $val = htmlspecialchars( $val );
790 break;
791 }
792 break;
793 case 'Copyrighted':
794 switch ( $val ) {
795 case 'True': case 'False':
796 $val = self::msg( $tag, $val );
797 break;
798 }
799 break;
800 case 'Rating':
801 if ( $val == '-1' ) {
802 $val = self::msg( $tag, 'rejected' );
803 } else {
804 $val = self::formatNum( $val );
805 }
806 break;
807
808 case 'LanguageCode':
809 $lang = Language::fetchLanguageName( strtolower( $val ), $wgLang->getCode() );
810 if ( $lang ) {
811 $val = htmlspecialchars( $lang );
812 } else {
813 $val = htmlspecialchars( $val );
814 }
815 break;
816
817 default:
818 $val = self::formatNum( $val );
819 break;
820 }
821 }
822 // End formatting values, start flattening arrays.
823 $vals = self::flattenArray( $vals, $type );
824
825 }
826 return $tags;
827 }
828
829 /**
830 * A function to collapse multivalued tags into a single value.
831 * This turns an array of (for example) authors into a bulleted list.
832 *
833 * This is public on the basis it might be useful outside of this class.
834 *
835 * @param array $vals array of values
836 * @param string $type Type of array (either lang, ul, ol).
837 * lang = language assoc array with keys being the lang code
838 * ul = unordered list, ol = ordered list
839 * type can also come from the '_type' member of $vals.
840 * @param $noHtml Boolean If to avoid returning anything resembling
841 * html. (Ugly hack for backwards compatibility with old mediawiki).
842 * @return String single value (in wiki-syntax).
843 */
844 public static function flattenArray( $vals, $type = 'ul', $noHtml = false ) {
845 if ( isset( $vals['_type'] ) ) {
846 $type = $vals['_type'];
847 unset( $vals['_type'] );
848 }
849
850 if ( !is_array( $vals ) ) {
851 return $vals; // do nothing if not an array;
852 }
853 elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
854 return $vals[0];
855 }
856 elseif ( count( $vals ) === 0 ) {
857 wfDebug( __METHOD__ . " metadata array with 0 elements!\n" );
858 return ""; // paranoia. This should never happen
859 }
860 /* @todo FIXME: This should hide some of the list entries if there are
861 * say more than four. Especially if a field is translated into 20
862 * languages, we don't want to show them all by default
863 */
864 else {
865 global $wgContLang;
866 switch ( $type ) {
867 case 'lang':
868 // Display default, followed by ContLang,
869 // followed by the rest in no particular
870 // order.
871
872 // Todo: hide some items if really long list.
873
874 $content = '';
875
876 $cLang = $wgContLang->getCode();
877 $defaultItem = false;
878 $defaultLang = false;
879
880 // If default is set, save it for later,
881 // as we don't know if it's equal to
882 // one of the lang codes. (In xmp
883 // you specify the language for a
884 // default property by having both
885 // a default prop, and one in the language
886 // that are identical)
887 if ( isset( $vals['x-default'] ) ) {
888 $defaultItem = $vals['x-default'];
889 unset( $vals['x-default'] );
890 }
891 // Do contentLanguage.
892 if ( isset( $vals[$cLang] ) ) {
893 $isDefault = false;
894 if ( $vals[$cLang] === $defaultItem ) {
895 $defaultItem = false;
896 $isDefault = true;
897 }
898 $content .= self::langItem(
899 $vals[$cLang], $cLang,
900 $isDefault, $noHtml );
901
902 unset( $vals[$cLang] );
903 }
904
905 // Now do the rest.
906 foreach ( $vals as $lang => $item ) {
907 if ( $item === $defaultItem ) {
908 $defaultLang = $lang;
909 continue;
910 }
911 $content .= self::langItem( $item,
912 $lang, false, $noHtml );
913 }
914 if ( $defaultItem !== false ) {
915 $content = self::langItem( $defaultItem,
916 $defaultLang, true, $noHtml ) .
917 $content;
918 }
919 if ( $noHtml ) {
920 return $content;
921 }
922 return '<ul class="metadata-langlist">' .
923 $content .
924 '</ul>';
925 case 'ol':
926 if ( $noHtml ) {
927 return "\n#" . implode( "\n#", $vals );
928 }
929 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
930 case 'ul':
931 default:
932 if ( $noHtml ) {
933 return "\n*" . implode( "\n*", $vals );
934 }
935 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
936 }
937 }
938 }
939
940 /** Helper function for creating lists of translations.
941 *
942 * @param string $value value (this is not escaped)
943 * @param string $lang lang code of item or false
944 * @param $default Boolean if it is default value.
945 * @param $noHtml Boolean If to avoid html (for back-compat)
946 * @throws MWException
947 * @return string language item (Note: despite how this looks,
948 * this is treated as wikitext not html).
949 */
950 private static function langItem( $value, $lang, $default = false, $noHtml = false ) {
951 if ( $lang === false && $default === false ) {
952 throw new MWException( '$lang and $default cannot both '
953 . 'be false.' );
954 }
955
956 if ( $noHtml ) {
957 $wrappedValue = $value;
958 } else {
959 $wrappedValue = '<span class="mw-metadata-lang-value">'
960 . $value . '</span>';
961 }
962
963 if ( $lang === false ) {
964 if ( $noHtml ) {
965 return wfMessage( 'metadata-langitem-default',
966 $wrappedValue )->text() . "\n\n";
967 } /* else */
968 return '<li class="mw-metadata-lang-default">'
969 . wfMessage( 'metadata-langitem-default',
970 $wrappedValue )->text()
971 . "</li>\n";
972 }
973
974 $lowLang = strtolower( $lang );
975 $langName = Language::fetchLanguageName( $lowLang );
976 if ( $langName === '' ) {
977 //try just the base language name. (aka en-US -> en ).
978 list( $langPrefix ) = explode( '-', $lowLang, 2 );
979 $langName = Language::fetchLanguageName( $langPrefix );
980 if ( $langName === '' ) {
981 // give up.
982 $langName = $lang;
983 }
984 }
985 // else we have a language specified
986
987 if ( $noHtml ) {
988 return '*' . wfMessage( 'metadata-langitem',
989 $wrappedValue, $langName, $lang )->text();
990 } /* else: */
991
992 $item = '<li class="mw-metadata-lang-code-'
993 . $lang;
994 if ( $default ) {
995 $item .= ' mw-metadata-lang-default';
996 }
997 $item .= '" lang="' . $lang . '">';
998 $item .= wfMessage( 'metadata-langitem',
999 $wrappedValue, $langName, $lang )->text();
1000 $item .= "</li>\n";
1001 return $item;
1002 }
1003
1004 /**
1005 * Convenience function for getFormattedData()
1006 *
1007 * @private
1008 *
1009 * @param string $tag the tag name to pass on
1010 * @param string $val the value of the tag
1011 * @param string $arg an argument to pass ($1)
1012 * @param string $arg2 a 2nd argument to pass ($2)
1013 * @return string A wfMessage of "exif-$tag-$val" in lower case
1014 */
1015 static function msg( $tag, $val, $arg = null, $arg2 = null ) {
1016 global $wgContLang;
1017
1018 if ( $val === '' ) {
1019 $val = 'value';
1020 }
1021 return wfMessage( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 )->text();
1022 }
1023
1024 /**
1025 * Format a number, convert numbers from fractions into floating point
1026 * numbers, joins arrays of numbers with commas.
1027 *
1028 * @param $num Mixed: the value to format
1029 * @param $round float|int|bool digits to round to or false.
1030 * @return mixed A floating point number or whatever we were fed
1031 */
1032 static function formatNum( $num, $round = false ) {
1033 global $wgLang;
1034 $m = array();
1035 if ( is_array( $num ) ) {
1036 $out = array();
1037 foreach ( $num as $number ) {
1038 $out[] = self::formatNum( $number );
1039 }
1040 return $wgLang->commaList( $out );
1041 }
1042 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1043 if ( $m[2] != 0 ) {
1044 $newNum = $m[1] / $m[2];
1045 if ( $round !== false ) {
1046 $newNum = round( $newNum, $round );
1047 }
1048 } else {
1049 $newNum = $num;
1050 }
1051
1052 return $wgLang->formatNum( $newNum );
1053 } else {
1054 if ( is_numeric( $num ) && $round !== false ) {
1055 $num = round( $num, $round );
1056 }
1057 return $wgLang->formatNum( $num );
1058 }
1059 }
1060
1061 /**
1062 * Format a rational number, reducing fractions
1063 *
1064 * @private
1065 *
1066 * @param $num Mixed: the value to format
1067 * @return mixed A floating point number or whatever we were fed
1068 */
1069 static function formatFraction( $num ) {
1070 $m = array();
1071 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1072 $numerator = intval( $m[1] );
1073 $denominator = intval( $m[2] );
1074 $gcd = self::gcd( abs( $numerator ), $denominator );
1075 if ( $gcd != 0 ) {
1076 // 0 shouldn't happen! ;)
1077 return self::formatNum( $numerator / $gcd ) . '/' . self::formatNum( $denominator / $gcd );
1078 }
1079 }
1080 return self::formatNum( $num );
1081 }
1082
1083 /**
1084 * Calculate the greatest common divisor of two integers.
1085 *
1086 * @param $a Integer: Numerator
1087 * @param $b Integer: Denominator
1088 * @return int
1089 * @private
1090 */
1091 static function gcd( $a, $b ) {
1092 /*
1093 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1094 // Recursive form would be:
1095 if( $b == 0 )
1096 return $a;
1097 else
1098 return gcd( $b, $a % $b );
1099 */
1100 while ( $b != 0 ) {
1101 $remainder = $a % $b;
1102
1103 // tail recursion...
1104 $a = $b;
1105 $b = $remainder;
1106 }
1107 return $a;
1108 }
1109
1110 /**
1111 * Fetch the human readable version of a news code.
1112 * A news code is an 8 digit code. The first two
1113 * digits are a general classification, so we just
1114 * translate that.
1115 *
1116 * Note, leading 0's are significant, so this is
1117 * a string, not an int.
1118 *
1119 * @param string $val The 8 digit news code.
1120 * @return string The human readable form
1121 */
1122 private static function convertNewsCode( $val ) {
1123 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1124 // Not a valid news code.
1125 return $val;
1126 }
1127 $cat = '';
1128 switch ( substr( $val, 0, 2 ) ) {
1129 case '01':
1130 $cat = 'ace';
1131 break;
1132 case '02':
1133 $cat = 'clj';
1134 break;
1135 case '03':
1136 $cat = 'dis';
1137 break;
1138 case '04':
1139 $cat = 'fin';
1140 break;
1141 case '05':
1142 $cat = 'edu';
1143 break;
1144 case '06':
1145 $cat = 'evn';
1146 break;
1147 case '07':
1148 $cat = 'hth';
1149 break;
1150 case '08':
1151 $cat = 'hum';
1152 break;
1153 case '09':
1154 $cat = 'lab';
1155 break;
1156 case '10':
1157 $cat = 'lif';
1158 break;
1159 case '11':
1160 $cat = 'pol';
1161 break;
1162 case '12':
1163 $cat = 'rel';
1164 break;
1165 case '13':
1166 $cat = 'sci';
1167 break;
1168 case '14':
1169 $cat = 'soi';
1170 break;
1171 case '15':
1172 $cat = 'spo';
1173 break;
1174 case '16':
1175 $cat = 'war';
1176 break;
1177 case '17':
1178 $cat = 'wea';
1179 break;
1180 }
1181 if ( $cat !== '' ) {
1182 $catMsg = self::msg( 'iimcategory', $cat );
1183 $val = self::msg( 'subjectnewscode', '', $val, $catMsg );
1184 }
1185 return $val;
1186 }
1187
1188 /**
1189 * Format a coordinate value, convert numbers from floating point
1190 * into degree minute second representation.
1191 *
1192 * @param int $coord degrees, minutes and seconds
1193 * @param string $type latitude or longitude (for if its a NWS or E)
1194 * @return mixed A floating point number or whatever we were fed
1195 */
1196 static function formatCoords( $coord, $type ) {
1197 $ref = '';
1198 if ( $coord < 0 ) {
1199 $nCoord = -$coord;
1200 if ( $type === 'latitude' ) {
1201 $ref = 'S';
1202 } elseif ( $type === 'longitude' ) {
1203 $ref = 'W';
1204 }
1205 } else {
1206 $nCoord = $coord;
1207 if ( $type === 'latitude' ) {
1208 $ref = 'N';
1209 } elseif ( $type === 'longitude' ) {
1210 $ref = 'E';
1211 }
1212 }
1213
1214 $deg = floor( $nCoord );
1215 $min = floor( ( $nCoord - $deg ) * 60.0 );
1216 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1217
1218 $deg = self::formatNum( $deg );
1219 $min = self::formatNum( $min );
1220 $sec = self::formatNum( $sec );
1221
1222 return wfMessage( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord )->text();
1223 }
1224
1225 /**
1226 * Format the contact info field into a single value.
1227 *
1228 * @param array $vals array with fields of the ContactInfo
1229 * struct defined in the IPTC4XMP spec. Or potentially
1230 * an array with one element that is a free form text
1231 * value from the older iptc iim 1:118 prop.
1232 *
1233 * This function might be called from
1234 * JpegHandler::convertMetadataVersion which is why it is
1235 * public.
1236 *
1237 * @return String of html-ish looking wikitext
1238 */
1239 public static function collapseContactInfo( $vals ) {
1240 if ( !( isset( $vals['CiAdrExtadr'] )
1241 || isset( $vals['CiAdrCity'] )
1242 || isset( $vals['CiAdrCtry'] )
1243 || isset( $vals['CiEmailWork'] )
1244 || isset( $vals['CiTelWork'] )
1245 || isset( $vals['CiAdrPcode'] )
1246 || isset( $vals['CiAdrRegion'] )
1247 || isset( $vals['CiUrlWork'] )
1248 ) ) {
1249 // We don't have any sub-properties
1250 // This could happen if its using old
1251 // iptc that just had this as a free-form
1252 // text value.
1253 // Note: We run this through htmlspecialchars
1254 // partially to be consistent, and partially
1255 // because people often insert >, etc into
1256 // the metadata which should not be interpreted
1257 // but we still want to auto-link urls.
1258 foreach ( $vals as &$val ) {
1259 $val = htmlspecialchars( $val );
1260 }
1261 return self::flattenArray( $vals );
1262 } else {
1263 // We have a real ContactInfo field.
1264 // Its unclear if all these fields have to be
1265 // set, so assume they do not.
1266 $url = $tel = $street = $city = $country = '';
1267 $email = $postal = $region = '';
1268
1269 // Also note, some of the class names this uses
1270 // are similar to those used by hCard. This is
1271 // mostly because they're sensible names. This
1272 // does not (and does not attempt to) output
1273 // stuff in the hCard microformat. However it
1274 // might output in the adr microformat.
1275
1276 if ( isset( $vals['CiAdrExtadr'] ) ) {
1277 // Todo: This can potentially be multi-line.
1278 // Need to check how that works in XMP.
1279 $street = '<span class="extended-address">'
1280 . htmlspecialchars(
1281 $vals['CiAdrExtadr'] )
1282 . '</span>';
1283 }
1284 if ( isset( $vals['CiAdrCity'] ) ) {
1285 $city = '<span class="locality">'
1286 . htmlspecialchars( $vals['CiAdrCity'] )
1287 . '</span>';
1288 }
1289 if ( isset( $vals['CiAdrCtry'] ) ) {
1290 $country = '<span class="country-name">'
1291 . htmlspecialchars( $vals['CiAdrCtry'] )
1292 . '</span>';
1293 }
1294 if ( isset( $vals['CiEmailWork'] ) ) {
1295 $emails = array();
1296 // Have to split multiple emails at commas/new lines.
1297 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1298 foreach ( $splitEmails as $e1 ) {
1299 // Also split on comma
1300 foreach ( explode( ',', $e1 ) as $e2 ) {
1301 $finalEmail = trim( $e2 );
1302 if ( $finalEmail == ',' || $finalEmail == '' ) {
1303 continue;
1304 }
1305 if ( strpos( $finalEmail, '<' ) !== false ) {
1306 // Don't do fancy formatting to
1307 // "My name" <foo@bar.com> style stuff
1308 $emails[] = $finalEmail;
1309 } else {
1310 $emails[] = '[mailto:'
1311 . $finalEmail
1312 . ' <span class="email">'
1313 . $finalEmail
1314 . '</span>]';
1315 }
1316 }
1317 }
1318 $email = implode( ', ', $emails );
1319 }
1320 if ( isset( $vals['CiTelWork'] ) ) {
1321 $tel = '<span class="tel">'
1322 . htmlspecialchars( $vals['CiTelWork'] )
1323 . '</span>';
1324 }
1325 if ( isset( $vals['CiAdrPcode'] ) ) {
1326 $postal = '<span class="postal-code">'
1327 . htmlspecialchars(
1328 $vals['CiAdrPcode'] )
1329 . '</span>';
1330 }
1331 if ( isset( $vals['CiAdrRegion'] ) ) {
1332 // Note this is province/state.
1333 $region = '<span class="region">'
1334 . htmlspecialchars(
1335 $vals['CiAdrRegion'] )
1336 . '</span>';
1337 }
1338 if ( isset( $vals['CiUrlWork'] ) ) {
1339 $url = '<span class="url">'
1340 . htmlspecialchars( $vals['CiUrlWork'] )
1341 . '</span>';
1342 }
1343 return wfMessage( 'exif-contact-value', $email, $url,
1344 $street, $city, $region, $postal, $country,
1345 $tel )->text();
1346 }
1347 }
1348 }
1349
1350 /** For compatability with old FormatExif class
1351 * which some extensions use.
1352 *
1353 * @deprecated since 1.18
1354 *
1355 */
1356 class FormatExif {
1357 var $meta;
1358
1359 /**
1360 * @param $meta array
1361 */
1362 function FormatExif( $meta ) {
1363 wfDeprecated( __METHOD__, '1.18' );
1364 $this->meta = $meta;
1365 }
1366
1367 /**
1368 * @return array
1369 */
1370 function getFormattedData() {
1371 return FormatMetadata::getFormattedData( $this->meta );
1372 }
1373 }