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