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