f79284f0c7eaa9265734f4f5621650c8523556c9
[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 * Only output a single language for multi-language fields
51 * @var boolean
52 * @since 1.23
53 */
54 protected $singleLang = false;
55
56 /**
57 * Trigger only outputting single language for multilanguage fields
58 *
59 * @param Boolean $val
60 * @since 1.23
61 */
62 public function setSingleLanguage( $val ) {
63 $this->singleLang = $val;
64 }
65
66 /**
67 * Numbers given by Exif user agents are often magical, that is they
68 * should be replaced by a detailed explanation depending on their
69 * value which most of the time are plain integers. This function
70 * formats Exif (and other metadata) values into human readable form.
71 *
72 * This is the usual entry point for this class.
73 *
74 * @param array $tags the Exif data to format ( as returned by
75 * Exif::getFilteredData() or BitmapMetadataHandler )
76 * @param IContextSource $context Context to use (optional)
77 * @return array
78 */
79 public static function getFormattedData( $tags, $context = false ) {
80 $obj = new FormatMetadata;
81 if ( $context ) {
82 $obj->setContext( $context );
83 }
84
85 return $obj->makeFormattedData( $tags );
86 }
87
88 /**
89 * Numbers given by Exif user agents are often magical, that is they
90 * should be replaced by a detailed explanation depending on their
91 * value which most of the time are plain integers. This function
92 * formats Exif (and other metadata) values into human readable form.
93 *
94 * @param array $tags the Exif data to format ( as returned by
95 * Exif::getFilteredData() or BitmapMetadataHandler )
96 * @return array
97 * @since 1.23
98 */
99 public function makeFormattedData( $tags ) {
100 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
101 unset( $tags['ResolutionUnit'] );
102
103 foreach ( $tags as $tag => &$vals ) {
104
105 // This seems ugly to wrap non-array's in an array just to unwrap again,
106 // especially when most of the time it is not an array
107 if ( !is_array( $tags[$tag] ) ) {
108 $vals = array( $vals );
109 }
110
111 // _type is a special value to say what array type
112 if ( isset( $tags[$tag]['_type'] ) ) {
113 $type = $tags[$tag]['_type'];
114 unset( $vals['_type'] );
115 } else {
116 $type = 'ul'; // default unordered list.
117 }
118
119 //This is done differently as the tag is an array.
120 if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) {
121 //hour min sec array
122
123 $h = explode( '/', $vals[0] );
124 $m = explode( '/', $vals[1] );
125 $s = explode( '/', $vals[2] );
126
127 // this should already be validated
128 // when loaded from file, but it could
129 // come from a foreign repo, so be
130 // paranoid.
131 if ( !isset( $h[1] )
132 || !isset( $m[1] )
133 || !isset( $s[1] )
134 || $h[1] == 0
135 || $m[1] == 0
136 || $s[1] == 0
137 ) {
138 continue;
139 }
140 $tags[$tag] = str_pad( intval( $h[0] / $h[1] ), 2, '0', STR_PAD_LEFT )
141 . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT )
142 . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT );
143
144 try {
145 $time = wfTimestamp( TS_MW, '1971:01:01 ' . $tags[$tag] );
146 // the 1971:01:01 is just a placeholder, and not shown to user.
147 if ( $time && intval( $time ) > 0 ) {
148 $tags[$tag] = $this->getLanguage()->time( $time );
149 }
150 } catch ( TimestampException $e ) {
151 // This shouldn't happen, but we've seen bad formats
152 // such as 4-digit seconds in the wild.
153 // leave $tags[$tag] as-is
154 }
155 continue;
156 }
157
158 // The contact info is a multi-valued field
159 // instead of the other props which are single
160 // valued (mostly) so handle as a special case.
161 if ( $tag === 'Contact' ) {
162 $vals = $this->collapseContactInfo( $vals );
163 continue;
164 }
165
166 foreach ( $vals as &$val ) {
167
168 switch ( $tag ) {
169 case 'Compression':
170 switch ( $val ) {
171 case 1:
172 case 2:
173 case 3:
174 case 4:
175 case 5:
176 case 6:
177 case 7:
178 case 8:
179 case 32773:
180 case 32946:
181 case 34712:
182 $val = $this->exifMsg( $tag, $val );
183 break;
184 default:
185 /* If not recognized, display as is. */
186 break;
187 }
188 break;
189
190 case 'PhotometricInterpretation':
191 switch ( $val ) {
192 case 2:
193 case 6:
194 $val = $this->exifMsg( $tag, $val );
195 break;
196 default:
197 /* If not recognized, display as is. */
198 break;
199 }
200 break;
201
202 case 'Orientation':
203 switch ( $val ) {
204 case 1:
205 case 2:
206 case 3:
207 case 4:
208 case 5:
209 case 6:
210 case 7:
211 case 8:
212 $val = $this->exifMsg( $tag, $val );
213 break;
214 default:
215 /* If not recognized, display as is. */
216 break;
217 }
218 break;
219
220 case 'PlanarConfiguration':
221 switch ( $val ) {
222 case 1:
223 case 2:
224 $val = $this->exifMsg( $tag, $val );
225 break;
226 default:
227 /* If not recognized, display as is. */
228 break;
229 }
230 break;
231
232 // TODO: YCbCrSubSampling
233 case 'YCbCrPositioning':
234 switch ( $val ) {
235 case 1:
236 case 2:
237 $val = $this->exifMsg( $tag, $val );
238 break;
239 default:
240 /* If not recognized, display as is. */
241 break;
242 }
243 break;
244
245 case 'XResolution':
246 case 'YResolution':
247 switch ( $resolutionunit ) {
248 case 2:
249 $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) );
250 break;
251 case 3:
252 $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) );
253 break;
254 default:
255 /* If not recognized, display as is. */
256 break;
257 }
258 break;
259
260 // TODO: YCbCrCoefficients #p27 (see annex E)
261 case 'ExifVersion':
262 case 'FlashpixVersion':
263 $val = "$val" / 100;
264 break;
265
266 case 'ColorSpace':
267 switch ( $val ) {
268 case 1:
269 case 65535:
270 $val = $this->exifMsg( $tag, $val );
271 break;
272 default:
273 /* If not recognized, display as is. */
274 break;
275 }
276 break;
277
278 case 'ComponentsConfiguration':
279 switch ( $val ) {
280 case 0:
281 case 1:
282 case 2:
283 case 3:
284 case 4:
285 case 5:
286 case 6:
287 $val = $this->exifMsg( $tag, $val );
288 break;
289 default:
290 /* If not recognized, display as is. */
291 break;
292 }
293 break;
294
295 case 'DateTime':
296 case 'DateTimeOriginal':
297 case 'DateTimeDigitized':
298 case 'DateTimeReleased':
299 case 'DateTimeExpires':
300 case 'GPSDateStamp':
301 case 'dc-date':
302 case 'DateTimeMetadata':
303 if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) {
304 $val = $this->msg( 'exif-unknowndate' )->text();
305 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
306 // Full date.
307 $time = wfTimestamp( TS_MW, $val );
308 if ( $time && intval( $time ) > 0 ) {
309 $val = $this->getLanguage()->timeanddate( $time );
310 }
311 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
312 // No second field. Still format the same
313 // since timeanddate doesn't include seconds anyways,
314 // but second still available in api
315 $time = wfTimestamp( TS_MW, $val . ':00' );
316 if ( $time && intval( $time ) > 0 ) {
317 $val = $this->getLanguage()->timeanddate( $time );
318 }
319 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
320 // If only the date but not the time is filled in.
321 $time = wfTimestamp( TS_MW, substr( $val, 0, 4 )
322 . substr( $val, 5, 2 )
323 . substr( $val, 8, 2 )
324 . '000000' );
325 if ( $time && intval( $time ) > 0 ) {
326 $val = $this->getLanguage()->date( $time );
327 }
328 }
329 // else it will just output $val without formatting it.
330 break;
331
332 case 'ExposureProgram':
333 switch ( $val ) {
334 case 0:
335 case 1:
336 case 2:
337 case 3:
338 case 4:
339 case 5:
340 case 6:
341 case 7:
342 case 8:
343 $val = $this->exifMsg( $tag, $val );
344 break;
345 default:
346 /* If not recognized, display as is. */
347 break;
348 }
349 break;
350
351 case 'SubjectDistance':
352 $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) );
353 break;
354
355 case 'MeteringMode':
356 switch ( $val ) {
357 case 0:
358 case 1:
359 case 2:
360 case 3:
361 case 4:
362 case 5:
363 case 6:
364 case 7:
365 case 255:
366 $val = $this->exifMsg( $tag, $val );
367 break;
368 default:
369 /* If not recognized, display as is. */
370 break;
371 }
372 break;
373
374 case 'LightSource':
375 switch ( $val ) {
376 case 0:
377 case 1:
378 case 2:
379 case 3:
380 case 4:
381 case 9:
382 case 10:
383 case 11:
384 case 12:
385 case 13:
386 case 14:
387 case 15:
388 case 17:
389 case 18:
390 case 19:
391 case 20:
392 case 21:
393 case 22:
394 case 23:
395 case 24:
396 case 255:
397 $val = $this->exifMsg( $tag, $val );
398 break;
399 default:
400 /* If not recognized, display as is. */
401 break;
402 }
403 break;
404
405 case 'Flash':
406 $flashDecode = array(
407 'fired' => $val & bindec( '00000001' ),
408 'return' => ( $val & bindec( '00000110' ) ) >> 1,
409 'mode' => ( $val & bindec( '00011000' ) ) >> 3,
410 'function' => ( $val & bindec( '00100000' ) ) >> 5,
411 'redeye' => ( $val & bindec( '01000000' ) ) >> 6,
412 // 'reserved' => ($val & bindec( '10000000' )) >> 7,
413 );
414 $flashMsgs = array();
415 # We do not need to handle unknown values since all are used.
416 foreach ( $flashDecode as $subTag => $subValue ) {
417 # We do not need any message for zeroed values.
418 if ( $subTag != 'fired' && $subValue == 0 ) {
419 continue;
420 }
421 $fullTag = $tag . '-' . $subTag;
422 $flashMsgs[] = $this->exifMsg( $fullTag, $subValue );
423 }
424 $val = $this->getLanguage()->commaList( $flashMsgs );
425 break;
426
427 case 'FocalPlaneResolutionUnit':
428 switch ( $val ) {
429 case 2:
430 $val = $this->exifMsg( $tag, $val );
431 break;
432 default:
433 /* If not recognized, display as is. */
434 break;
435 }
436 break;
437
438 case 'SensingMethod':
439 switch ( $val ) {
440 case 1:
441 case 2:
442 case 3:
443 case 4:
444 case 5:
445 case 7:
446 case 8:
447 $val = $this->exifMsg( $tag, $val );
448 break;
449 default:
450 /* If not recognized, display as is. */
451 break;
452 }
453 break;
454
455 case 'FileSource':
456 switch ( $val ) {
457 case 3:
458 $val = $this->exifMsg( $tag, $val );
459 break;
460 default:
461 /* If not recognized, display as is. */
462 break;
463 }
464 break;
465
466 case 'SceneType':
467 switch ( $val ) {
468 case 1:
469 $val = $this->exifMsg( $tag, $val );
470 break;
471 default:
472 /* If not recognized, display as is. */
473 break;
474 }
475 break;
476
477 case 'CustomRendered':
478 switch ( $val ) {
479 case 0:
480 case 1:
481 $val = $this->exifMsg( $tag, $val );
482 break;
483 default:
484 /* If not recognized, display as is. */
485 break;
486 }
487 break;
488
489 case 'ExposureMode':
490 switch ( $val ) {
491 case 0:
492 case 1:
493 case 2:
494 $val = $this->exifMsg( $tag, $val );
495 break;
496 default:
497 /* If not recognized, display as is. */
498 break;
499 }
500 break;
501
502 case 'WhiteBalance':
503 switch ( $val ) {
504 case 0:
505 case 1:
506 $val = $this->exifMsg( $tag, $val );
507 break;
508 default:
509 /* If not recognized, display as is. */
510 break;
511 }
512 break;
513
514 case 'SceneCaptureType':
515 switch ( $val ) {
516 case 0:
517 case 1:
518 case 2:
519 case 3:
520 $val = $this->exifMsg( $tag, $val );
521 break;
522 default:
523 /* If not recognized, display as is. */
524 break;
525 }
526 break;
527
528 case 'GainControl':
529 switch ( $val ) {
530 case 0:
531 case 1:
532 case 2:
533 case 3:
534 case 4:
535 $val = $this->exifMsg( $tag, $val );
536 break;
537 default:
538 /* If not recognized, display as is. */
539 break;
540 }
541 break;
542
543 case 'Contrast':
544 switch ( $val ) {
545 case 0:
546 case 1:
547 case 2:
548 $val = $this->exifMsg( $tag, $val );
549 break;
550 default:
551 /* If not recognized, display as is. */
552 break;
553 }
554 break;
555
556 case 'Saturation':
557 switch ( $val ) {
558 case 0:
559 case 1:
560 case 2:
561 $val = $this->exifMsg( $tag, $val );
562 break;
563 default:
564 /* If not recognized, display as is. */
565 break;
566 }
567 break;
568
569 case 'Sharpness':
570 switch ( $val ) {
571 case 0:
572 case 1:
573 case 2:
574 $val = $this->exifMsg( $tag, $val );
575 break;
576 default:
577 /* If not recognized, display as is. */
578 break;
579 }
580 break;
581
582 case 'SubjectDistanceRange':
583 switch ( $val ) {
584 case 0:
585 case 1:
586 case 2:
587 case 3:
588 $val = $this->exifMsg( $tag, $val );
589 break;
590 default:
591 /* If not recognized, display as is. */
592 break;
593 }
594 break;
595
596 //The GPS...Ref values are kept for compatibility, probably won't be reached.
597 case 'GPSLatitudeRef':
598 case 'GPSDestLatitudeRef':
599 switch ( $val ) {
600 case 'N':
601 case 'S':
602 $val = $this->exifMsg( 'GPSLatitude', $val );
603 break;
604 default:
605 /* If not recognized, display as is. */
606 break;
607 }
608 break;
609
610 case 'GPSLongitudeRef':
611 case 'GPSDestLongitudeRef':
612 switch ( $val ) {
613 case 'E':
614 case 'W':
615 $val = $this->exifMsg( 'GPSLongitude', $val );
616 break;
617 default:
618 /* If not recognized, display as is. */
619 break;
620 }
621 break;
622
623 case 'GPSAltitude':
624 if ( $val < 0 ) {
625 $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) );
626 } else {
627 $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) );
628 }
629 break;
630
631 case 'GPSStatus':
632 switch ( $val ) {
633 case 'A':
634 case 'V':
635 $val = $this->exifMsg( $tag, $val );
636 break;
637 default:
638 /* If not recognized, display as is. */
639 break;
640 }
641 break;
642
643 case 'GPSMeasureMode':
644 switch ( $val ) {
645 case 2:
646 case 3:
647 $val = $this->exifMsg( $tag, $val );
648 break;
649 default:
650 /* If not recognized, display as is. */
651 break;
652 }
653 break;
654
655 case 'GPSTrackRef':
656 case 'GPSImgDirectionRef':
657 case 'GPSDestBearingRef':
658 switch ( $val ) {
659 case 'T':
660 case 'M':
661 $val = $this->exifMsg( 'GPSDirection', $val );
662 break;
663 default:
664 /* If not recognized, display as is. */
665 break;
666 }
667 break;
668
669 case 'GPSLatitude':
670 case 'GPSDestLatitude':
671 $val = $this->formatCoords( $val, 'latitude' );
672 break;
673 case 'GPSLongitude':
674 case 'GPSDestLongitude':
675 $val = $this->formatCoords( $val, 'longitude' );
676 break;
677
678 case 'GPSSpeedRef':
679 switch ( $val ) {
680 case 'K':
681 case 'M':
682 case 'N':
683 $val = $this->exifMsg( 'GPSSpeed', $val );
684 break;
685 default:
686 /* If not recognized, display as is. */
687 break;
688 }
689 break;
690
691 case 'GPSDestDistanceRef':
692 switch ( $val ) {
693 case 'K':
694 case 'M':
695 case 'N':
696 $val = $this->exifMsg( 'GPSDestDistance', $val );
697 break;
698 default:
699 /* If not recognized, display as is. */
700 break;
701 }
702 break;
703
704 case 'GPSDOP':
705 // See http://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
706 if ( $val <= 2 ) {
707 $val = $this->exifMsg( $tag, 'excellent', $this->formatNum( $val ) );
708 } elseif ( $val <= 5 ) {
709 $val = $this->exifMsg( $tag, 'good', $this->formatNum( $val ) );
710 } elseif ( $val <= 10 ) {
711 $val = $this->exifMsg( $tag, 'moderate', $this->formatNum( $val ) );
712 } elseif ( $val <= 20 ) {
713 $val = $this->exifMsg( $tag, 'fair', $this->formatNum( $val ) );
714 } else {
715 $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) );
716 }
717 break;
718
719 // This is not in the Exif standard, just a special
720 // case for our purposes which enables wikis to wikify
721 // the make, model and software name to link to their articles.
722 case 'Make':
723 case 'Model':
724 $val = $this->exifMsg( $tag, '', $val );
725 break;
726
727 case 'Software':
728 if ( is_array( $val ) ) {
729 //if its a software, version array.
730 $val = $this->msg( 'exif-software-version-value', $val[0], $val[1] )->text();
731 } else {
732 $val = $this->exifMsg( $tag, '', $val );
733 }
734 break;
735
736 case 'ExposureTime':
737 // Show the pretty fraction as well as decimal version
738 $val = $this->msg( 'exif-exposuretime-format',
739 $this->formatFraction( $val ), $this->formatNum( $val ) )->text();
740 break;
741 case 'ISOSpeedRatings':
742 // If its = 65535 that means its at the
743 // limit of the size of Exif::short and
744 // is really higher.
745 if ( $val == '65535' ) {
746 $val = $this->exifMsg( $tag, 'overflow' );
747 } else {
748 $val = $this->formatNum( $val );
749 }
750 break;
751 case 'FNumber':
752 $val = $this->msg( 'exif-fnumber-format',
753 $this->formatNum( $val ) )->text();
754 break;
755
756 case 'FocalLength':
757 case 'FocalLengthIn35mmFilm':
758 $val = $this->msg( 'exif-focallength-format',
759 $this->formatNum( $val ) )->text();
760 break;
761
762 case 'MaxApertureValue':
763 if ( strpos( $val, '/' ) !== false ) {
764 // need to expand this earlier to calculate fNumber
765 list( $n, $d ) = explode( '/', $val );
766 if ( is_numeric( $n ) && is_numeric( $d ) ) {
767 $val = $n / $d;
768 }
769 }
770 if ( is_numeric( $val ) ) {
771 $fNumber = pow( 2, $val / 2 );
772 if ( $fNumber !== false ) {
773 $val = $this->msg( 'exif-maxaperturevalue-value',
774 $this->formatNum( $val ),
775 $this->formatNum( $fNumber, 2 )
776 )->text();
777 }
778 }
779 break;
780
781 case 'iimCategory':
782 switch ( strtolower( $val ) ) {
783 // See pg 29 of IPTC photo
784 // metadata standard.
785 case 'ace':
786 case 'clj':
787 case 'dis':
788 case 'fin':
789 case 'edu':
790 case 'evn':
791 case 'hth':
792 case 'hum':
793 case 'lab':
794 case 'lif':
795 case 'pol':
796 case 'rel':
797 case 'sci':
798 case 'soi':
799 case 'spo':
800 case 'war':
801 case 'wea':
802 $val = $this->exifMsg(
803 'iimcategory',
804 $val
805 );
806 }
807 break;
808 case 'SubjectNewsCode':
809 // Essentially like iimCategory.
810 // 8 (numeric) digit hierarchical
811 // classification. We decode the
812 // first 2 digits, which provide
813 // a broad category.
814 $val = $this->convertNewsCode( $val );
815 break;
816 case 'Urgency':
817 // 1-8 with 1 being highest, 5 normal
818 // 0 is reserved, and 9 is 'user-defined'.
819 $urgency = '';
820 if ( $val == 0 || $val == 9 ) {
821 $urgency = 'other';
822 } elseif ( $val < 5 && $val > 1 ) {
823 $urgency = 'high';
824 } elseif ( $val == 5 ) {
825 $urgency = 'normal';
826 } elseif ( $val <= 8 && $val > 5 ) {
827 $urgency = 'low';
828 }
829
830 if ( $urgency !== '' ) {
831 $val = $this->exifMsg( 'urgency',
832 $urgency, $val
833 );
834 }
835 break;
836
837 // Things that have a unit of pixels.
838 case 'OriginalImageHeight':
839 case 'OriginalImageWidth':
840 case 'PixelXDimension':
841 case 'PixelYDimension':
842 case 'ImageWidth':
843 case 'ImageLength':
844 $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text();
845 break;
846
847 // Do not transform fields with pure text.
848 // For some languages the formatNum()
849 // conversion results to wrong output like
850 // foo,bar@example,com or foo٫bar@example٫com.
851 // Also some 'numeric' things like Scene codes
852 // are included here as we really don't want
853 // commas inserted.
854 case 'ImageDescription':
855 case 'Artist':
856 case 'Copyright':
857 case 'RelatedSoundFile':
858 case 'ImageUniqueID':
859 case 'SpectralSensitivity':
860 case 'GPSSatellites':
861 case 'GPSVersionID':
862 case 'GPSMapDatum':
863 case 'Keywords':
864 case 'WorldRegionDest':
865 case 'CountryDest':
866 case 'CountryCodeDest':
867 case 'ProvinceOrStateDest':
868 case 'CityDest':
869 case 'SublocationDest':
870 case 'WorldRegionCreated':
871 case 'CountryCreated':
872 case 'CountryCodeCreated':
873 case 'ProvinceOrStateCreated':
874 case 'CityCreated':
875 case 'SublocationCreated':
876 case 'ObjectName':
877 case 'SpecialInstructions':
878 case 'Headline':
879 case 'Credit':
880 case 'Source':
881 case 'EditStatus':
882 case 'FixtureIdentifier':
883 case 'LocationDest':
884 case 'LocationDestCode':
885 case 'Writer':
886 case 'JPEGFileComment':
887 case 'iimSupplementalCategory':
888 case 'OriginalTransmissionRef':
889 case 'Identifier':
890 case 'dc-contributor':
891 case 'dc-coverage':
892 case 'dc-publisher':
893 case 'dc-relation':
894 case 'dc-rights':
895 case 'dc-source':
896 case 'dc-type':
897 case 'Lens':
898 case 'SerialNumber':
899 case 'CameraOwnerName':
900 case 'Label':
901 case 'Nickname':
902 case 'RightsCertificate':
903 case 'CopyrightOwner':
904 case 'UsageTerms':
905 case 'WebStatement':
906 case 'OriginalDocumentID':
907 case 'LicenseUrl':
908 case 'MorePermissionsUrl':
909 case 'AttributionUrl':
910 case 'PreferredAttributionName':
911 case 'PNGFileComment':
912 case 'Disclaimer':
913 case 'ContentWarning':
914 case 'GIFFileComment':
915 case 'SceneCode':
916 case 'IntellectualGenre':
917 case 'Event':
918 case 'OrginisationInImage':
919 case 'PersonInImage':
920
921 $val = htmlspecialchars( $val );
922 break;
923
924 case 'ObjectCycle':
925 switch ( $val ) {
926 case 'a':
927 case 'p':
928 case 'b':
929 $val = $this->exifMsg( $tag, $val );
930 break;
931 default:
932 $val = htmlspecialchars( $val );
933 break;
934 }
935 break;
936 case 'Copyrighted':
937 switch ( $val ) {
938 case 'True':
939 case 'False':
940 $val = $this->exifMsg( $tag, $val );
941 break;
942 }
943 break;
944 case 'Rating':
945 if ( $val == '-1' ) {
946 $val = $this->exifMsg( $tag, 'rejected' );
947 } else {
948 $val = $this->formatNum( $val );
949 }
950 break;
951
952 case 'LanguageCode':
953 $lang = Language::fetchLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
954 if ( $lang ) {
955 $val = htmlspecialchars( $lang );
956 } else {
957 $val = htmlspecialchars( $val );
958 }
959 break;
960
961 default:
962 $val = $this->formatNum( $val );
963 break;
964 }
965 }
966 // End formatting values, start flattening arrays.
967 $vals = $this->flattenArrayReal( $vals, $type );
968 }
969
970 return $tags;
971 }
972
973 /**
974 * Flatten an array, using the content language for any messages.
975 *
976 * @param array $vals array of values
977 * @param string $type Type of array (either lang, ul, ol).
978 * lang = language assoc array with keys being the lang code
979 * ul = unordered list, ol = ordered list
980 * type can also come from the '_type' member of $vals.
981 * @param $noHtml Boolean If to avoid returning anything resembling
982 * html. (Ugly hack for backwards compatibility with old mediawiki).
983 * @param IContextSource $context
984 * @return String single value (in wiki-syntax).
985 * @since 1.23
986 */
987 public static function flattenArrayContentLang( $vals, $type = 'ul', $noHtml = false, $context = false ) {
988 global $wgContLang;
989 $obj = new FormatMetadata;
990 if ( $context ) {
991 $obj->setContext( $context );
992 }
993 $context = new DerivativeContext( $obj->getContext() );
994 $context->setLanguage( $wgContLang );
995 $obj->setContext( $context );
996
997 return $obj->flattenArrayReal( $vals, $type, $noHtml );
998 }
999
1000 /**
1001 * Flatten an array, using the user language for any messages.
1002 *
1003 * @param array $vals array of values
1004 * @param string $type Type of array (either lang, ul, ol).
1005 * lang = language assoc array with keys being the lang code
1006 * ul = unordered list, ol = ordered list
1007 * type can also come from the '_type' member of $vals.
1008 * @param $noHtml Boolean If to avoid returning anything resembling
1009 * html. (Ugly hack for backwards compatibility with old mediawiki).
1010 * @param IContextSource $context
1011 * @return String single value (in wiki-syntax).
1012 */
1013 public static function flattenArray( $vals, $type = 'ul', $noHtml = false, $context = false ) {
1014 $obj = new FormatMetadata;
1015 if ( $context ) {
1016 $obj->setContext( $context );
1017 }
1018
1019 return $obj->flattenArrayReal( $vals, $type, $noHtml );
1020 }
1021
1022 /**
1023 * A function to collapse multivalued tags into a single value.
1024 * This turns an array of (for example) authors into a bulleted list.
1025 *
1026 * This is public on the basis it might be useful outside of this class.
1027 *
1028 * @param array $vals array of values
1029 * @param string $type Type of array (either lang, ul, ol).
1030 * lang = language assoc array with keys being the lang code
1031 * ul = unordered list, ol = ordered list
1032 * type can also come from the '_type' member of $vals.
1033 * @param $noHtml Boolean If to avoid returning anything resembling
1034 * html. (Ugly hack for backwards compatibility with old mediawiki).
1035 * @return String single value (in wiki-syntax).
1036 * @since 1.23
1037 */
1038 public function flattenArrayReal( $vals, $type = 'ul', $noHtml = false ) {
1039 if ( !is_array( $vals ) ) {
1040 return $vals; // do nothing if not an array;
1041 }
1042
1043 if ( isset( $vals['_type'] ) ) {
1044 $type = $vals['_type'];
1045 unset( $vals['_type'] );
1046 }
1047
1048 if ( !is_array( $vals ) ) {
1049 return $vals; // do nothing if not an array;
1050 } elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
1051 return $vals[0];
1052 } elseif ( count( $vals ) === 0 ) {
1053 wfDebug( __METHOD__ . " metadata array with 0 elements!\n" );
1054
1055 return ""; // paranoia. This should never happen
1056 } else {
1057 /* @todo FIXME: This should hide some of the list entries if there are
1058 * say more than four. Especially if a field is translated into 20
1059 * languages, we don't want to show them all by default
1060 */
1061 switch ( $type ) {
1062 case 'lang':
1063 // Display default, followed by ContLang,
1064 // followed by the rest in no particular
1065 // order.
1066
1067 // Todo: hide some items if really long list.
1068
1069 $content = '';
1070
1071 $priorityLanguages = $this->getPriorityLanguages();
1072 $defaultItem = false;
1073 $defaultLang = false;
1074
1075 // If default is set, save it for later,
1076 // as we don't know if it's equal to
1077 // one of the lang codes. (In xmp
1078 // you specify the language for a
1079 // default property by having both
1080 // a default prop, and one in the language
1081 // that are identical)
1082 if ( isset( $vals['x-default'] ) ) {
1083 $defaultItem = $vals['x-default'];
1084 unset( $vals['x-default'] );
1085 }
1086 foreach ( $priorityLanguages as $pLang ) {
1087 if ( isset( $vals[$pLang] ) ) {
1088 $isDefault = false;
1089 if ( $vals[$pLang] === $defaultItem ) {
1090 $defaultItem = false;
1091 $isDefault = true;
1092 }
1093 $content .= $this->langItem(
1094 $vals[$pLang], $pLang,
1095 $isDefault, $noHtml );
1096
1097 unset( $vals[$pLang] );
1098
1099 if ( $this->singleLang ) {
1100 return Html::rawElement( 'span',
1101 array( 'lang' => $pLang ), $vals[$pLang] );
1102 }
1103 }
1104 }
1105
1106 // Now do the rest.
1107 foreach ( $vals as $lang => $item ) {
1108 if ( $item === $defaultItem ) {
1109 $defaultLang = $lang;
1110 continue;
1111 }
1112 $content .= $this->langItem( $item,
1113 $lang, false, $noHtml );
1114 if ( $this->singleLang ) {
1115 return Html::rawElement( 'span',
1116 array( 'lang' => $lang ), $item );
1117 }
1118 }
1119 if ( $defaultItem !== false ) {
1120 $content = $this->langItem( $defaultItem,
1121 $defaultLang, true, $noHtml ) .
1122 $content;
1123 if ( $this->singleLang ) {
1124 return $defaultItem;
1125 }
1126 }
1127 if ( $noHtml ) {
1128 return $content;
1129 }
1130
1131 return '<ul class="metadata-langlist">' .
1132 $content .
1133 '</ul>';
1134 case 'ol':
1135 if ( $noHtml ) {
1136 return "\n#" . implode( "\n#", $vals );
1137 }
1138
1139 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
1140 case 'ul':
1141 default:
1142 if ( $noHtml ) {
1143 return "\n*" . implode( "\n*", $vals );
1144 }
1145
1146 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
1147 }
1148 }
1149 }
1150
1151 /** Helper function for creating lists of translations.
1152 *
1153 * @param string $value value (this is not escaped)
1154 * @param string $lang lang code of item or false
1155 * @param $default Boolean if it is default value.
1156 * @param $noHtml Boolean If to avoid html (for back-compat)
1157 * @throws MWException
1158 * @return string language item (Note: despite how this looks,
1159 * this is treated as wikitext not html).
1160 */
1161 private function langItem( $value, $lang, $default = false, $noHtml = false ) {
1162 if ( $lang === false && $default === false ) {
1163 throw new MWException( '$lang and $default cannot both '
1164 . 'be false.' );
1165 }
1166
1167 if ( $noHtml ) {
1168 $wrappedValue = $value;
1169 } else {
1170 $wrappedValue = '<span class="mw-metadata-lang-value">'
1171 . $value . '</span>';
1172 }
1173
1174 if ( $lang === false ) {
1175 $msg = $this->msg( 'metadata-langitem-default', $wrappedValue );
1176 if ( $noHtml ) {
1177 return $msg->text() . "\n\n";
1178 } /* else */
1179
1180 return '<li class="mw-metadata-lang-default">'
1181 . $msg->text()
1182 . "</li>\n";
1183 }
1184
1185 $lowLang = strtolower( $lang );
1186 $langName = Language::fetchLanguageName( $lowLang );
1187 if ( $langName === '' ) {
1188 //try just the base language name. (aka en-US -> en ).
1189 list( $langPrefix ) = explode( '-', $lowLang, 2 );
1190 $langName = Language::fetchLanguageName( $langPrefix );
1191 if ( $langName === '' ) {
1192 // give up.
1193 $langName = $lang;
1194 }
1195 }
1196 // else we have a language specified
1197
1198 $msg = $this->msg( 'metadata-langitem', $wrappedValue, $langName, $lang );
1199 if ( $noHtml ) {
1200 return '*' . $msg->text();
1201 } /* else: */
1202
1203 $item = '<li class="mw-metadata-lang-code-'
1204 . $lang;
1205 if ( $default ) {
1206 $item .= ' mw-metadata-lang-default';
1207 }
1208 $item .= '" lang="' . $lang . '">';
1209 $item .= $msg->text();
1210 $item .= "</li>\n";
1211
1212 return $item;
1213 }
1214
1215 /**
1216 * Convenience function for getFormattedData()
1217 *
1218 * @private
1219 *
1220 * @param string $tag the tag name to pass on
1221 * @param string $val the value of the tag
1222 * @param string $arg an argument to pass ($1)
1223 * @param string $arg2 a 2nd argument to pass ($2)
1224 * @return string The text content of "exif-$tag-$val" message in lower case
1225 */
1226 private function exifMsg( $tag, $val, $arg = null, $arg2 = null ) {
1227 global $wgContLang;
1228
1229 if ( $val === '' ) {
1230 $val = 'value';
1231 }
1232
1233 return $this->msg( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 )->text();
1234 }
1235
1236 /**
1237 * Format a number, convert numbers from fractions into floating point
1238 * numbers, joins arrays of numbers with commas.
1239 *
1240 * @param $num Mixed: the value to format
1241 * @param $round float|int|bool digits to round to or false.
1242 * @return mixed A floating point number or whatever we were fed
1243 */
1244 private function formatNum( $num, $round = false ) {
1245 $m = array();
1246 if ( is_array( $num ) ) {
1247 $out = array();
1248 foreach ( $num as $number ) {
1249 $out[] = $this->formatNum( $number );
1250 }
1251
1252 return $this->getLanguage()->commaList( $out );
1253 }
1254 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1255 if ( $m[2] != 0 ) {
1256 $newNum = $m[1] / $m[2];
1257 if ( $round !== false ) {
1258 $newNum = round( $newNum, $round );
1259 }
1260 } else {
1261 $newNum = $num;
1262 }
1263
1264 return $this->getLanguage()->formatNum( $newNum );
1265 } else {
1266 if ( is_numeric( $num ) && $round !== false ) {
1267 $num = round( $num, $round );
1268 }
1269
1270 return $this->getLanguage()->formatNum( $num );
1271 }
1272 }
1273
1274 /**
1275 * Format a rational number, reducing fractions
1276 *
1277 * @private
1278 *
1279 * @param $num Mixed: the value to format
1280 * @return mixed A floating point number or whatever we were fed
1281 */
1282 private function formatFraction( $num ) {
1283 $m = array();
1284 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1285 $numerator = intval( $m[1] );
1286 $denominator = intval( $m[2] );
1287 $gcd = $this->gcd( abs( $numerator ), $denominator );
1288 if ( $gcd != 0 ) {
1289 // 0 shouldn't happen! ;)
1290 return $this->formatNum( $numerator / $gcd ) . '/' . $this->formatNum( $denominator / $gcd );
1291 }
1292 }
1293
1294 return $this->formatNum( $num );
1295 }
1296
1297 /**
1298 * Calculate the greatest common divisor of two integers.
1299 *
1300 * @param $a Integer: Numerator
1301 * @param $b Integer: Denominator
1302 * @return int
1303 * @private
1304 */
1305 private function gcd( $a, $b ) {
1306 /*
1307 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1308 // Recursive form would be:
1309 if( $b == 0 )
1310 return $a;
1311 else
1312 return gcd( $b, $a % $b );
1313 */
1314 while ( $b != 0 ) {
1315 $remainder = $a % $b;
1316
1317 // tail recursion...
1318 $a = $b;
1319 $b = $remainder;
1320 }
1321
1322 return $a;
1323 }
1324
1325 /**
1326 * Fetch the human readable version of a news code.
1327 * A news code is an 8 digit code. The first two
1328 * digits are a general classification, so we just
1329 * translate that.
1330 *
1331 * Note, leading 0's are significant, so this is
1332 * a string, not an int.
1333 *
1334 * @param string $val The 8 digit news code.
1335 * @return string The human readable form
1336 */
1337 private function convertNewsCode( $val ) {
1338 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1339 // Not a valid news code.
1340 return $val;
1341 }
1342 $cat = '';
1343 switch ( substr( $val, 0, 2 ) ) {
1344 case '01':
1345 $cat = 'ace';
1346 break;
1347 case '02':
1348 $cat = 'clj';
1349 break;
1350 case '03':
1351 $cat = 'dis';
1352 break;
1353 case '04':
1354 $cat = 'fin';
1355 break;
1356 case '05':
1357 $cat = 'edu';
1358 break;
1359 case '06':
1360 $cat = 'evn';
1361 break;
1362 case '07':
1363 $cat = 'hth';
1364 break;
1365 case '08':
1366 $cat = 'hum';
1367 break;
1368 case '09':
1369 $cat = 'lab';
1370 break;
1371 case '10':
1372 $cat = 'lif';
1373 break;
1374 case '11':
1375 $cat = 'pol';
1376 break;
1377 case '12':
1378 $cat = 'rel';
1379 break;
1380 case '13':
1381 $cat = 'sci';
1382 break;
1383 case '14':
1384 $cat = 'soi';
1385 break;
1386 case '15':
1387 $cat = 'spo';
1388 break;
1389 case '16':
1390 $cat = 'war';
1391 break;
1392 case '17':
1393 $cat = 'wea';
1394 break;
1395 }
1396 if ( $cat !== '' ) {
1397 $catMsg = $this->exifMsg( 'iimcategory', $cat );
1398 $val = $this->exifMsg( 'subjectnewscode', '', $val, $catMsg );
1399 }
1400
1401 return $val;
1402 }
1403
1404 /**
1405 * Format a coordinate value, convert numbers from floating point
1406 * into degree minute second representation.
1407 *
1408 * @param int $coord degrees, minutes and seconds
1409 * @param string $type latitude or longitude (for if its a NWS or E)
1410 * @return mixed A floating point number or whatever we were fed
1411 */
1412 private function formatCoords( $coord, $type ) {
1413 $ref = '';
1414 if ( $coord < 0 ) {
1415 $nCoord = -$coord;
1416 if ( $type === 'latitude' ) {
1417 $ref = 'S';
1418 } elseif ( $type === 'longitude' ) {
1419 $ref = 'W';
1420 }
1421 } else {
1422 $nCoord = $coord;
1423 if ( $type === 'latitude' ) {
1424 $ref = 'N';
1425 } elseif ( $type === 'longitude' ) {
1426 $ref = 'E';
1427 }
1428 }
1429
1430 $deg = floor( $nCoord );
1431 $min = floor( ( $nCoord - $deg ) * 60.0 );
1432 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1433
1434 $deg = $this->formatNum( $deg );
1435 $min = $this->formatNum( $min );
1436 $sec = $this->formatNum( $sec );
1437
1438 return $this->msg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord )->text();
1439 }
1440
1441 /**
1442 * Format the contact info field into a single value.
1443 *
1444 * @param array $vals array with fields of the ContactInfo
1445 * struct defined in the IPTC4XMP spec. Or potentially
1446 * an array with one element that is a free form text
1447 * value from the older iptc iim 1:118 prop.
1448 *
1449 * This function might be called from
1450 * JpegHandler::convertMetadataVersion which is why it is
1451 * public.
1452 *
1453 * @return String of html-ish looking wikitext
1454 * @since 1.23 no longer static
1455 */
1456 public function collapseContactInfo( $vals ) {
1457 if ( !( isset( $vals['CiAdrExtadr'] )
1458 || isset( $vals['CiAdrCity'] )
1459 || isset( $vals['CiAdrCtry'] )
1460 || isset( $vals['CiEmailWork'] )
1461 || isset( $vals['CiTelWork'] )
1462 || isset( $vals['CiAdrPcode'] )
1463 || isset( $vals['CiAdrRegion'] )
1464 || isset( $vals['CiUrlWork'] )
1465 ) ) {
1466 // We don't have any sub-properties
1467 // This could happen if its using old
1468 // iptc that just had this as a free-form
1469 // text value.
1470 // Note: We run this through htmlspecialchars
1471 // partially to be consistent, and partially
1472 // because people often insert >, etc into
1473 // the metadata which should not be interpreted
1474 // but we still want to auto-link urls.
1475 foreach ( $vals as &$val ) {
1476 $val = htmlspecialchars( $val );
1477 }
1478
1479 return $this->flattenArrayReal( $vals );
1480 } else {
1481 // We have a real ContactInfo field.
1482 // Its unclear if all these fields have to be
1483 // set, so assume they do not.
1484 $url = $tel = $street = $city = $country = '';
1485 $email = $postal = $region = '';
1486
1487 // Also note, some of the class names this uses
1488 // are similar to those used by hCard. This is
1489 // mostly because they're sensible names. This
1490 // does not (and does not attempt to) output
1491 // stuff in the hCard microformat. However it
1492 // might output in the adr microformat.
1493
1494 if ( isset( $vals['CiAdrExtadr'] ) ) {
1495 // Todo: This can potentially be multi-line.
1496 // Need to check how that works in XMP.
1497 $street = '<span class="extended-address">'
1498 . htmlspecialchars(
1499 $vals['CiAdrExtadr'] )
1500 . '</span>';
1501 }
1502 if ( isset( $vals['CiAdrCity'] ) ) {
1503 $city = '<span class="locality">'
1504 . htmlspecialchars( $vals['CiAdrCity'] )
1505 . '</span>';
1506 }
1507 if ( isset( $vals['CiAdrCtry'] ) ) {
1508 $country = '<span class="country-name">'
1509 . htmlspecialchars( $vals['CiAdrCtry'] )
1510 . '</span>';
1511 }
1512 if ( isset( $vals['CiEmailWork'] ) ) {
1513 $emails = array();
1514 // Have to split multiple emails at commas/new lines.
1515 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1516 foreach ( $splitEmails as $e1 ) {
1517 // Also split on comma
1518 foreach ( explode( ',', $e1 ) as $e2 ) {
1519 $finalEmail = trim( $e2 );
1520 if ( $finalEmail == ',' || $finalEmail == '' ) {
1521 continue;
1522 }
1523 if ( strpos( $finalEmail, '<' ) !== false ) {
1524 // Don't do fancy formatting to
1525 // "My name" <foo@bar.com> style stuff
1526 $emails[] = $finalEmail;
1527 } else {
1528 $emails[] = '[mailto:'
1529 . $finalEmail
1530 . ' <span class="email">'
1531 . $finalEmail
1532 . '</span>]';
1533 }
1534 }
1535 }
1536 $email = implode( ', ', $emails );
1537 }
1538 if ( isset( $vals['CiTelWork'] ) ) {
1539 $tel = '<span class="tel">'
1540 . htmlspecialchars( $vals['CiTelWork'] )
1541 . '</span>';
1542 }
1543 if ( isset( $vals['CiAdrPcode'] ) ) {
1544 $postal = '<span class="postal-code">'
1545 . htmlspecialchars(
1546 $vals['CiAdrPcode'] )
1547 . '</span>';
1548 }
1549 if ( isset( $vals['CiAdrRegion'] ) ) {
1550 // Note this is province/state.
1551 $region = '<span class="region">'
1552 . htmlspecialchars(
1553 $vals['CiAdrRegion'] )
1554 . '</span>';
1555 }
1556 if ( isset( $vals['CiUrlWork'] ) ) {
1557 $url = '<span class="url">'
1558 . htmlspecialchars( $vals['CiUrlWork'] )
1559 . '</span>';
1560 }
1561
1562 return $this->msg( 'exif-contact-value', $email, $url,
1563 $street, $city, $region, $postal, $country,
1564 $tel )->text();
1565 }
1566 }
1567
1568 /**
1569 * Get a list of fields that are visible by default.
1570 *
1571 * @return array
1572 * @since 1.23
1573 */
1574 public static function getVisibleFields() {
1575 $fields = array();
1576 $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
1577 foreach ( $lines as $line ) {
1578 $matches = array();
1579 if ( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
1580 $fields[] = $matches[1];
1581 }
1582 }
1583 $fields = array_map( 'strtolower', $fields );
1584
1585 return $fields;
1586 }
1587
1588 /**
1589 * Get an array of extended metadata. (See the imageinfo API for format.)
1590 *
1591 * @param File $file File to use
1592 * @return array [<property name> => ['value' => <value>]], or [] on error
1593 * @since 1.23
1594 */
1595 public function fetchExtendedMetadata( File $file ) {
1596 global $wgMemc;
1597
1598 wfProfileIn( __METHOD__ );
1599
1600 // If revision deleted, exit immediately
1601 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1602 wfProfileOut( __METHOD__ );
1603
1604 return array();
1605 }
1606
1607 $cacheKey = wfMemcKey(
1608 'getExtendedMetadata',
1609 $this->getLanguage()->getCode(),
1610 (int)$this->singleLang,
1611 $file->getSha1()
1612 );
1613
1614 $cachedValue = $wgMemc->get( $cacheKey );
1615 if (
1616 $cachedValue
1617 && wfRunHooks( 'ValidateExtendedMetadataCache', array( $cachedValue['timestamp'], $file ) )
1618 ) {
1619 $extendedMetadata = $cachedValue['data'];
1620 } else {
1621 $maxCacheTime = ( $file instanceof ForeignAPIFile ) ? 60 * 60 * 12 : 60 * 60 * 24 * 30;
1622 $fileMetadata = $this->getExtendedMetadataFromFile( $file );
1623 $extendedMetadata = $this->getExtendedMetadataFromHook( $file, $fileMetadata, $maxCacheTime );
1624 if ( $this->singleLang ) {
1625 $this->resolveMultilangMetadata( $extendedMetadata );
1626 }
1627 // Make sure the metadata won't break the API when an XML format is used.
1628 // This is an API-specific function so it would be cleaner to call it from
1629 // outside fetchExtendedMetadata, but this way we don't need to redo the
1630 // computation on a cache hit.
1631 $this->sanitizeArrayForXml( $extendedMetadata );
1632 $valueToCache = array( 'data' => $extendedMetadata, 'timestamp' => wfTimestampNow() );
1633 $wgMemc->set( $cacheKey, $valueToCache, $maxCacheTime );
1634 }
1635
1636 wfProfileOut( __METHOD__ );
1637
1638 return $extendedMetadata;
1639 }
1640
1641 /**
1642 * Get file-based metadata in standardized format.
1643 *
1644 * Note that for a remote file, this might return metadata supplied by extensions.
1645 *
1646 * @param File $file File to use
1647 * @return array [<property name> => ['value' => <value>]], or [] on error
1648 * @since 1.23
1649 */
1650 protected function getExtendedMetadataFromFile( File $file ) {
1651 // If this is a remote file accessed via an API request, we already
1652 // have remote metadata so we just ignore any local one
1653 if ( $file instanceof ForeignAPIFile ) {
1654 // in case of error we pretend no metadata - this will get cached. Might or might not be a good idea.
1655 return $file->getExtendedMetadata() ?: array();
1656 }
1657
1658 wfProfileIn( __METHOD__ );
1659
1660 $uploadDate = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
1661
1662 $fileMetadata = array(
1663 // This is modification time, which is close to "upload" time.
1664 'DateTime' => array(
1665 'value' => $uploadDate,
1666 'source' => 'mediawiki-metadata',
1667 ),
1668 );
1669
1670 $title = $file->getTitle();
1671 if ( $title ) {
1672 $text = $title->getText();
1673 $pos = strrpos( $text, '.' );
1674
1675 if ( $pos ) {
1676 $name = substr( $text, 0, $pos );
1677 } else {
1678 $name = $text;
1679 }
1680
1681 $fileMetadata['ObjectName'] = array(
1682 'value' => $name,
1683 'source' => 'mediawiki-metadata',
1684 );
1685 }
1686
1687 $common = $file->getCommonMetaArray();
1688
1689 if ( $common !== false ) {
1690 foreach ( $common as $key => $value ) {
1691 $fileMetadata[$key] = array(
1692 'value' => $value,
1693 'source' => 'file-metadata',
1694 );
1695 }
1696 }
1697
1698 wfProfileOut( __METHOD__ );
1699
1700 return $fileMetadata;
1701 }
1702
1703 /**
1704 * Get additional metadata from hooks in standardized format.
1705 *
1706 * @param File $file File to use
1707 * @param array $extendedMetadata
1708 * @param int $maxCacheTime hook handlers might use this parameter to override cache time
1709 *
1710 * @return array [<property name> => ['value' => <value>]], or [] on error
1711 * @since 1.23
1712 */
1713 protected function getExtendedMetadataFromHook( File $file, array $extendedMetadata, &$maxCacheTime ) {
1714 wfProfileIn( __METHOD__ );
1715
1716 wfRunHooks( 'GetExtendedMetadata', array(
1717 &$extendedMetadata,
1718 $file,
1719 $this->getContext(),
1720 $this->singleLang,
1721 &$maxCacheTime
1722 ) );
1723
1724 $visible = array_flip( self::getVisibleFields() );
1725 foreach ( $extendedMetadata as $key => $value ) {
1726 if ( !isset( $visible[strtolower( $key )] ) ) {
1727 $extendedMetadata[$key]['hidden'] = '';
1728 }
1729 }
1730
1731 wfProfileOut( __METHOD__ );
1732
1733 return $extendedMetadata;
1734 }
1735
1736 /**
1737 * Turns an XMP-style multilang array into a single value.
1738 * If the value is not a multilang array, it is returned unchanged.
1739 * See mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
1740 * @param mixed $value
1741 * @return mixed value in best language, null if there were no languages at all
1742 * @since 1.23
1743 */
1744 protected function resolveMultilangValue( $value ) {
1745 if (
1746 !is_array( $value )
1747 || !isset( $value['_type'] )
1748 || $value['_type'] != 'lang'
1749 ) {
1750 return $value; // do nothing if not a multilang array
1751 }
1752
1753 // choose the language best matching user or site settings
1754 $priorityLanguages = $this->getPriorityLanguages();
1755 foreach ( $priorityLanguages as $lang ) {
1756 if ( isset( $value[$lang] ) ) {
1757 return $value[$lang];
1758 }
1759 }
1760
1761 // otherwise go with the default language, if set
1762 if ( isset( $value['x-default'] ) ) {
1763 return $value['x-default'];
1764 }
1765
1766 // otherwise just return any one language
1767 unset( $value['_type'] );
1768 if ( !empty( $value ) ) {
1769 return reset( $value );
1770 }
1771
1772 // this should not happen; signal error
1773 return null;
1774 }
1775
1776 /**
1777 * Takes an array returned by the getExtendedMetadata* functions,
1778 * and resolves multi-language values in it.
1779 * @param array $metadata
1780 * @since 1.23
1781 */
1782 protected function resolveMultilangMetadata( &$metadata ) {
1783 if ( !is_array( $metadata ) ) {
1784 return;
1785 }
1786 foreach ( $metadata as &$field ) {
1787 if ( isset( $field['value'] ) ) {
1788 $field['value'] = $this->resolveMultilangValue( $field['value'] );
1789 }
1790 }
1791 }
1792
1793 /**
1794 * Makes sure the given array is a valid API response fragment
1795 * (can be transformed into XML)
1796 * @param array $arr
1797 */
1798 protected function sanitizeArrayForXml( &$arr ) {
1799 if ( !is_array( $arr ) ) {
1800 return;
1801 }
1802
1803 $counter = 1;
1804 foreach ( $arr as $key => &$value ) {
1805 $sanitizedKey = $this->sanitizeKeyForXml( $key );
1806 if ( $sanitizedKey !== $key ) {
1807 if ( isset( $arr[$sanitizedKey] ) ) {
1808 // Make the sanitized keys hopefully unique.
1809 // To make it definitely unique would be too much effort, given that
1810 // sanitizing is only needed for misformatted metadata anyway, but
1811 // this at least covers the case when $arr is numeric.
1812 $sanitizedKey .= $counter;
1813 ++$counter;
1814 }
1815 $arr[$sanitizedKey] = $arr[$key];
1816 unset( $arr[$key] );
1817 }
1818 if ( is_array( $value ) ) {
1819 $this->sanitizeArrayForXml( $value );
1820 }
1821 }
1822 }
1823
1824 /**
1825 * Turns a string into a valid XML identifier.
1826 * Used to ensure that keys of an associative array in the
1827 * API response do not break the XML formatter.
1828 * @param string $key
1829 * @return string
1830 * @since 1.23
1831 */
1832 protected function sanitizeKeyForXml( $key ) {
1833 // drop all characters which are not valid in an XML tag name
1834 // a bunch of non-ASCII letters would be valid but probably won't
1835 // be used so we take the easy way
1836 $key = preg_replace( '/[^a-zA-z0-9_:.-]/', '', $key );
1837 // drop characters which are invalid at the first position
1838 $key = preg_replace( '/^[\d-.]+/', '', $key );
1839
1840 if ( $key == '' ) {
1841 $key = '_';
1842 }
1843
1844 // special case for an internal keyword
1845 if ( $key == '_element' ) {
1846 $key = 'element';
1847 }
1848
1849 return $key;
1850 }
1851
1852 /**
1853 * Returns a list of languages (first is best) to use when formatting multilang fields,
1854 * based on user and site preferences.
1855 * @return array
1856 * @since 1.23
1857 */
1858 protected function getPriorityLanguages() {
1859 $priorityLanguages = Language::getFallbacksIncludingSiteLanguage( $this->getLanguage()->getCode() );
1860 $priorityLanguages = array_merge( (array)$this->getLanguage()->getCode(), $priorityLanguages[0], $priorityLanguages[1] );
1861
1862 return $priorityLanguages;
1863 }
1864 }
1865
1866 /** For compatability with old FormatExif class
1867 * which some extensions use.
1868 *
1869 * @deprecated since 1.18
1870 *
1871 */
1872 class FormatExif {
1873 var $meta;
1874
1875 /**
1876 * @param $meta array
1877 */
1878 function FormatExif( $meta ) {
1879 wfDeprecated( __METHOD__, '1.18' );
1880 $this->meta = $meta;
1881 }
1882
1883 /**
1884 * @return array
1885 */
1886 function getFormattedData() {
1887 return FormatMetadata::getFormattedData( $this->meta );
1888 }
1889 }