Merge "SpecialBlock [Vue]: add NamespacesField and PagesField components"
[mediawiki.git] / includes / media / FormatMetadata.php
blob65fca8fedcced1d85f58e2c8ac521ae687e99014
1 <?php
2 /**
3 * Formatting of image metadata values into human readable form.
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.
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.
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
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 GPL-2.0-or-later
24 * @see http://exif.org/Exif2-2.PDF The Exif 2.2 specification
25 * @file
28 use MediaWiki\Api\ApiResult;
29 use MediaWiki\Context\ContextSource;
30 use MediaWiki\Context\IContextSource;
31 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
32 use MediaWiki\Html\Html;
33 use MediaWiki\Logger\LoggerFactory;
34 use MediaWiki\MediaWikiServices;
36 /**
37 * Format Image metadata values into a human readable form.
39 * Note lots of these messages use the prefix 'exif' even though
40 * they may not be exif properties. For example 'exif-ImageDescription'
41 * can be the Exif ImageDescription, or it could be the iptc-iim caption
42 * property, or it could be the xmp dc:description property. This
43 * is because these messages should be independent of how the data is
44 * stored, sine the user doesn't care if the description is stored in xmp,
45 * exif, etc only that its a description. (Additionally many of these properties
46 * are merged together following the MWG standard, such that for example,
47 * exif properties override XMP properties that mean the same thing if
48 * there is a conflict).
50 * It should perhaps use a prefix like 'metadata' instead, but there
51 * is already a large number of messages using the 'exif' prefix.
53 * @ingroup Media
54 * @since 1.23 the class extends ContextSource and various formerly-public
55 * internal methods are private
57 class FormatMetadata extends ContextSource {
58 use ProtectedHookAccessorTrait;
60 /**
61 * Only output a single language for multi-language fields
62 * @var bool
63 * @since 1.23
65 protected $singleLang = false;
67 /**
68 * Trigger only outputting single language for multilanguage fields
70 * @param bool $val
71 * @since 1.23
73 public function setSingleLanguage( $val ) {
74 $this->singleLang = $val;
77 /**
78 * Numbers given by Exif user agents are often magical, that is they
79 * should be replaced by a detailed explanation depending on their
80 * value which most of the time are plain integers. This function
81 * formats Exif (and other metadata) values into human readable form.
83 * This is the usual entry point for this class.
85 * @param array $tags The Exif data to format ( as returned by
86 * Exif::getFilteredData() or BitmapMetadataHandler )
87 * @param IContextSource|false $context
88 * @return array
90 public static function getFormattedData( $tags, $context = false ) {
91 $obj = new self;
92 if ( $context ) {
93 $obj->setContext( $context );
96 return $obj->makeFormattedData( $tags );
99 /**
100 * Numbers given by Exif user agents are often magical, that is they
101 * should be replaced by a detailed explanation depending on their
102 * value which most of the time are plain integers. This function
103 * formats Exif (and other metadata) values into human readable form.
105 * @param array $tags The Exif data to format ( as returned by
106 * Exif::getFilteredData() or BitmapMetadataHandler )
107 * @return array
108 * @since 1.23
110 public function makeFormattedData( $tags ) {
111 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
112 unset( $tags['ResolutionUnit'] );
114 // Ignore these complex values
115 unset( $tags['HasExtendedXMP'] );
116 unset( $tags['AuthorsPosition'] );
117 unset( $tags['LocationCreated'] );
118 unset( $tags['LocationShown'] );
119 unset( $tags['GPSAltitudeRef'] );
121 foreach ( $tags as $tag => &$vals ) {
122 // This seems ugly to wrap non-array's in an array just to unwrap again,
123 // especially when most of the time it is not an array
124 if ( !is_array( $vals ) ) {
125 $vals = [ $vals ];
128 // _type is a special value to say what array type
129 if ( isset( $vals['_type'] ) ) {
130 $type = $vals['_type'];
131 unset( $vals['_type'] );
132 } else {
133 $type = 'ul'; // default unordered list.
136 // _formatted is a special value to indicate the subclass
137 // already handled & formatted this tag as wikitext
138 if ( isset( $tags[$tag]['_formatted'] ) ) {
139 $tags[$tag] = $this->flattenArrayReal(
140 $tags[$tag]['_formatted'], $type
142 continue;
145 // This is done differently as the tag is an array.
146 if ( $tag === 'GPSTimeStamp' && count( $vals ) === 3 ) {
147 // hour min sec array
149 $h = explode( '/', $vals[0], 2 );
150 $m = explode( '/', $vals[1], 2 );
151 $s = explode( '/', $vals[2], 2 );
153 // this should already be validated
154 // when loaded from file, but it could
155 // come from a foreign repo, so be
156 // paranoid.
157 if ( !isset( $h[1] )
158 || !isset( $m[1] )
159 || !isset( $s[1] )
160 || $h[1] == 0
161 || $m[1] == 0
162 || $s[1] == 0
164 continue;
166 $vals = str_pad( (string)( (int)$h[0] / (int)$h[1] ), 2, '0', STR_PAD_LEFT )
167 . ':' . str_pad( (string)( (int)$m[0] / (int)$m[1] ), 2, '0', STR_PAD_LEFT )
168 . ':' . str_pad( (string)( (int)$s[0] / (int)$s[1] ), 2, '0', STR_PAD_LEFT );
170 $time = wfTimestamp( TS_MW, '1971:01:01 ' . $vals );
171 // the 1971:01:01 is just a placeholder, and not shown to user.
172 if ( $time && (int)$time > 0 ) {
173 $vals = $this->getLanguage()->time( $time );
175 continue;
178 // The contact info is a multi-valued field
179 // instead of the other props which are single
180 // valued (mostly) so handle as a special case.
181 if ( $tag === 'Contact' || $tag === 'CreatorContactInfo' ) {
182 $vals = $this->collapseContactInfo( $vals );
183 continue;
186 foreach ( $vals as &$val ) {
187 switch ( $tag ) {
188 case 'Compression':
189 switch ( $val ) {
190 case 1:
191 case 2:
192 case 3:
193 case 4:
194 case 5:
195 case 6:
196 case 7:
197 case 8:
198 case 32773:
199 case 32946:
200 case 34712:
201 $val = $this->exifMsg( $tag, $val );
202 break;
203 default:
204 /* If not recognized, display as is. */
205 $val = $this->literal( $val );
206 break;
208 break;
210 case 'PhotometricInterpretation':
211 switch ( $val ) {
212 case 0:
213 case 1:
214 case 2:
215 case 3:
216 case 4:
217 case 5:
218 case 6:
219 case 8:
220 case 9:
221 case 10:
222 case 32803:
223 case 34892:
224 $val = $this->exifMsg( $tag, $val );
225 break;
226 default:
227 /* If not recognized, display as is. */
228 $val = $this->literal( $val );
229 break;
231 break;
233 case 'Orientation':
234 switch ( $val ) {
235 case 1:
236 case 2:
237 case 3:
238 case 4:
239 case 5:
240 case 6:
241 case 7:
242 case 8:
243 $val = $this->exifMsg( $tag, $val );
244 break;
245 default:
246 /* If not recognized, display as is. */
247 $val = $this->literal( $val );
248 break;
250 break;
252 case 'PlanarConfiguration':
253 switch ( $val ) {
254 case 1:
255 case 2:
256 $val = $this->exifMsg( $tag, $val );
257 break;
258 default:
259 /* If not recognized, display as is. */
260 $val = $this->literal( $val );
261 break;
263 break;
265 // TODO: YCbCrSubSampling
266 case 'YCbCrPositioning':
267 switch ( $val ) {
268 case 1:
269 case 2:
270 $val = $this->exifMsg( $tag, $val );
271 break;
272 default:
273 /* If not recognized, display as is. */
274 $val = $this->literal( $val );
275 break;
277 break;
279 case 'XResolution':
280 case 'YResolution':
281 switch ( $resolutionunit ) {
282 case 2:
283 $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) );
284 break;
285 case 3:
286 $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) );
287 break;
288 default:
289 /* If not recognized, display as is. */
290 $val = $this->literal( $val );
291 break;
293 break;
295 // TODO: YCbCrCoefficients #p27 (see annex E)
296 case 'ExifVersion':
297 // PHP likes to be the odd one out with casing of FlashPixVersion;
298 // https://www.exif.org/Exif2-2.PDF#page=32 and
299 // https://www.digitalgalen.net/Documents/External/XMP/XMPSpecificationPart2.pdf#page=51
300 // both use FlashpixVersion. However, since at least 2002, PHP has used FlashPixVersion at
301 // https://github.com/php/php-src/blame/master/ext/exif/exif.c#L725
302 case 'FlashPixVersion':
303 // But we can still get the correct casing from
304 // Wikimedia\XMPReader on PDFs
305 case 'FlashpixVersion':
306 $val = $this->literal( (int)$val / 100 );
307 break;
309 case 'ColorSpace':
310 switch ( $val ) {
311 case 1:
312 case 65535:
313 $val = $this->exifMsg( $tag, $val );
314 break;
315 default:
316 /* If not recognized, display as is. */
317 $val = $this->literal( $val );
318 break;
320 break;
322 case 'ComponentsConfiguration':
323 switch ( $val ) {
324 case 0:
325 case 1:
326 case 2:
327 case 3:
328 case 4:
329 case 5:
330 case 6:
331 $val = $this->exifMsg( $tag, $val );
332 break;
333 default:
334 /* If not recognized, display as is. */
335 $val = $this->literal( $val );
336 break;
338 break;
340 case 'DateTime':
341 case 'DateTimeOriginal':
342 case 'DateTimeDigitized':
343 case 'DateTimeReleased':
344 case 'DateTimeExpires':
345 case 'GPSDateStamp':
346 case 'dc-date':
347 case 'DateTimeMetadata':
348 case 'FirstPhotoDate':
349 case 'LastPhotoDate':
350 if ( $val === '0000:00:00 00:00:00' || $val === ' : : : : ' ) {
351 $val = $this->msg( 'exif-unknowndate' )->text();
352 break;
354 if ( preg_match(
355 '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D',
356 $val
357 ) ) {
358 // Full date.
359 $time = wfTimestamp( TS_MW, $val );
360 if ( $time && (int)$time > 0 ) {
361 $val = $this->getLanguage()->timeanddate( $time );
362 break;
364 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
365 // No second field. Still format the same
366 // since timeanddate doesn't include seconds anyways,
367 // but second still available in api
368 $time = wfTimestamp( TS_MW, $val . ':00' );
369 if ( $time && (int)$time > 0 ) {
370 $val = $this->getLanguage()->timeanddate( $time );
371 break;
373 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
374 // If only the date but not the time is filled in.
375 $time = wfTimestamp( TS_MW, substr( $val, 0, 4 )
376 . substr( $val, 5, 2 )
377 . substr( $val, 8, 2 )
378 . '000000' );
379 if ( $time && (int)$time > 0 ) {
380 $val = $this->getLanguage()->date( $time );
381 break;
384 // else it will just output $val without formatting it.
385 $val = $this->literal( $val );
386 break;
388 case 'ExposureProgram':
389 switch ( $val ) {
390 case 0:
391 case 1:
392 case 2:
393 case 3:
394 case 4:
395 case 5:
396 case 6:
397 case 7:
398 case 8:
399 $val = $this->exifMsg( $tag, $val );
400 break;
401 default:
402 /* If not recognized, display as is. */
403 $val = $this->literal( $val );
404 break;
406 break;
408 case 'SubjectDistance':
409 $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) );
410 break;
412 case 'MeteringMode':
413 switch ( $val ) {
414 case 0:
415 case 1:
416 case 2:
417 case 3:
418 case 4:
419 case 5:
420 case 6:
421 case 7:
422 case 255:
423 $val = $this->exifMsg( $tag, $val );
424 break;
425 default:
426 /* If not recognized, display as is. */
427 $val = $this->literal( $val );
428 break;
430 break;
432 case 'LightSource':
433 switch ( $val ) {
434 case 0:
435 case 1:
436 case 2:
437 case 3:
438 case 4:
439 case 9:
440 case 10:
441 case 11:
442 case 12:
443 case 13:
444 case 14:
445 case 15:
446 case 17:
447 case 18:
448 case 19:
449 case 20:
450 case 21:
451 case 22:
452 case 23:
453 case 24:
454 case 255:
455 $val = $this->exifMsg( $tag, $val );
456 break;
457 default:
458 /* If not recognized, display as is. */
459 $val = $this->literal( $val );
460 break;
462 break;
464 case 'Flash':
465 if ( $val === '' ) {
466 $val = 0;
468 $flashDecode = [
469 'fired' => $val & 0b00000001,
470 'return' => ( $val & 0b00000110 ) >> 1,
471 'mode' => ( $val & 0b00011000 ) >> 3,
472 'function' => ( $val & 0b00100000 ) >> 5,
473 'redeye' => ( $val & 0b01000000 ) >> 6,
474 // 'reserved' => ( $val & 0b10000000 ) >> 7,
476 $flashMsgs = [];
477 # We do not need to handle unknown values since all are used.
478 foreach ( $flashDecode as $subTag => $subValue ) {
479 # We do not need any message for zeroed values.
480 if ( $subTag !== 'fired' && $subValue === 0 ) {
481 continue;
483 $fullTag = $tag . '-' . $subTag;
484 $flashMsgs[] = $this->exifMsg( $fullTag, $subValue );
486 $val = $this->getLanguage()->commaList( $flashMsgs );
487 break;
489 case 'FocalPlaneResolutionUnit':
490 switch ( $val ) {
491 case 2:
492 $val = $this->exifMsg( $tag, $val );
493 break;
494 default:
495 /* If not recognized, display as is. */
496 $val = $this->literal( $val );
497 break;
499 break;
501 case 'SensingMethod':
502 switch ( $val ) {
503 case 1:
504 case 2:
505 case 3:
506 case 4:
507 case 5:
508 case 7:
509 case 8:
510 $val = $this->exifMsg( $tag, $val );
511 break;
512 default:
513 /* If not recognized, display as is. */
514 $val = $this->literal( $val );
515 break;
517 break;
519 case 'FileSource':
520 switch ( $val ) {
521 case 3:
522 $val = $this->exifMsg( $tag, $val );
523 break;
524 default:
525 /* If not recognized, display as is. */
526 $val = $this->literal( $val );
527 break;
529 break;
531 case 'SceneType':
532 switch ( $val ) {
533 case 1:
534 $val = $this->exifMsg( $tag, $val );
535 break;
536 default:
537 /* If not recognized, display as is. */
538 $val = $this->literal( $val );
539 break;
541 break;
543 case 'CustomRendered':
544 switch ( $val ) {
545 case 0: /* normal */
546 case 1: /* custom */
547 /* The following are unofficial Apple additions */
548 case 2: /* HDR (no original saved) */
549 case 3: /* HDR (original saved) */
550 case 4: /* Original (for HDR) */
551 /* Yes 5 is not present ;) */
552 case 6: /* Panorama */
553 case 7: /* Portrait HDR */
554 case 8: /* Portrait */
555 $val = $this->exifMsg( $tag, $val );
556 break;
557 default:
558 /* If not recognized, display as is. */
559 $val = $this->literal( $val );
560 break;
562 break;
564 case 'ExposureMode':
565 switch ( $val ) {
566 case 0:
567 case 1:
568 case 2:
569 $val = $this->exifMsg( $tag, $val );
570 break;
571 default:
572 /* If not recognized, display as is. */
573 break;
575 break;
577 case 'WhiteBalance':
578 switch ( $val ) {
579 case 0:
580 case 1:
581 $val = $this->exifMsg( $tag, $val );
582 break;
583 default:
584 /* If not recognized, display as is. */
585 $val = $this->literal( $val );
586 break;
588 break;
590 case 'SceneCaptureType':
591 switch ( $val ) {
592 case 0:
593 case 1:
594 case 2:
595 case 3:
596 $val = $this->exifMsg( $tag, $val );
597 break;
598 default:
599 /* If not recognized, display as is. */
600 $val = $this->literal( $val );
601 break;
603 break;
605 case 'GainControl':
606 switch ( $val ) {
607 case 0:
608 case 1:
609 case 2:
610 case 3:
611 case 4:
612 $val = $this->exifMsg( $tag, $val );
613 break;
614 default:
615 /* If not recognized, display as is. */
616 $val = $this->literal( $val );
617 break;
619 break;
621 case 'Contrast':
622 switch ( $val ) {
623 case 0:
624 case 1:
625 case 2:
626 $val = $this->exifMsg( $tag, $val );
627 break;
628 default:
629 /* If not recognized, display as is. */
630 $val = $this->literal( $val );
631 break;
633 break;
635 case 'Saturation':
636 switch ( $val ) {
637 case 0:
638 case 1:
639 case 2:
640 $val = $this->exifMsg( $tag, $val );
641 break;
642 default:
643 /* If not recognized, display as is. */
644 $val = $this->literal( $val );
645 break;
647 break;
649 case 'Sharpness':
650 switch ( $val ) {
651 case 0:
652 case 1:
653 case 2:
654 $val = $this->exifMsg( $tag, $val );
655 break;
656 default:
657 /* If not recognized, display as is. */
658 $val = $this->literal( $val );
659 break;
661 break;
663 case 'SubjectDistanceRange':
664 switch ( $val ) {
665 case 0:
666 case 1:
667 case 2:
668 case 3:
669 $val = $this->exifMsg( $tag, $val );
670 break;
671 default:
672 /* If not recognized, display as is. */
673 $val = $this->literal( $val );
674 break;
676 break;
678 // The GPS...Ref values are kept for compatibility, probably won't be reached.
679 case 'GPSLatitudeRef':
680 case 'GPSDestLatitudeRef':
681 switch ( $val ) {
682 case 'N':
683 case 'S':
684 $val = $this->exifMsg( 'GPSLatitude', $val );
685 break;
686 default:
687 /* If not recognized, display as is. */
688 $val = $this->literal( $val );
689 break;
691 break;
693 case 'GPSLongitudeRef':
694 case 'GPSDestLongitudeRef':
695 switch ( $val ) {
696 case 'E':
697 case 'W':
698 $val = $this->exifMsg( 'GPSLongitude', $val );
699 break;
700 default:
701 /* If not recognized, display as is. */
702 $val = $this->literal( $val );
703 break;
705 break;
707 case 'GPSAltitude':
708 if ( $val < 0 ) {
709 $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) );
710 } else {
711 $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) );
713 break;
715 case 'GPSStatus':
716 switch ( $val ) {
717 case 'A':
718 case 'V':
719 $val = $this->exifMsg( $tag, $val );
720 break;
721 default:
722 /* If not recognized, display as is. */
723 $val = $this->literal( $val );
724 break;
726 break;
728 case 'GPSMeasureMode':
729 switch ( $val ) {
730 case 2:
731 case 3:
732 $val = $this->exifMsg( $tag, $val );
733 break;
734 default:
735 /* If not recognized, display as is. */
736 $val = $this->literal( $val );
737 break;
739 break;
741 case 'GPSTrackRef':
742 case 'GPSImgDirectionRef':
743 case 'GPSDestBearingRef':
744 switch ( $val ) {
745 case 'T':
746 case 'M':
747 $val = $this->exifMsg( 'GPSDirection', $val );
748 break;
749 default:
750 /* If not recognized, display as is. */
751 $val = $this->literal( $val );
752 break;
754 break;
756 case 'GPSLatitude':
757 case 'GPSDestLatitude':
758 $val = $this->formatCoords( $val, 'latitude' );
759 break;
760 case 'GPSLongitude':
761 case 'GPSDestLongitude':
762 $val = $this->formatCoords( $val, 'longitude' );
763 break;
765 case 'GPSSpeedRef':
766 switch ( $val ) {
767 case 'K':
768 case 'M':
769 case 'N':
770 $val = $this->exifMsg( 'GPSSpeed', $val );
771 break;
772 default:
773 /* If not recognized, display as is. */
774 $val = $this->literal( $val );
775 break;
777 break;
779 case 'GPSDestDistanceRef':
780 switch ( $val ) {
781 case 'K':
782 case 'M':
783 case 'N':
784 $val = $this->exifMsg( 'GPSDestDistance', $val );
785 break;
786 default:
787 /* If not recognized, display as is. */
788 $val = $this->literal( $val );
789 break;
791 break;
793 case 'GPSDOP':
794 // See https://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
795 if ( $val <= 2 ) {
796 $val = $this->exifMsg( $tag, 'excellent', $this->formatNum( $val ) );
797 } elseif ( $val <= 5 ) {
798 $val = $this->exifMsg( $tag, 'good', $this->formatNum( $val ) );
799 } elseif ( $val <= 10 ) {
800 $val = $this->exifMsg( $tag, 'moderate', $this->formatNum( $val ) );
801 } elseif ( $val <= 20 ) {
802 $val = $this->exifMsg( $tag, 'fair', $this->formatNum( $val ) );
803 } else {
804 $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) );
806 break;
808 // This is not in the Exif standard, just a special
809 // case for our purposes which enables wikis to wikify
810 // the make, model and software name to link to their articles.
811 case 'Make':
812 case 'Model':
813 $val = $this->exifMsg( $tag, '', $this->literal( $val ) );
814 break;
816 case 'Software':
817 if ( is_array( $val ) ) {
818 if ( count( $val ) > 1 ) {
819 // if its a software, version array.
820 $val = $this->msg(
821 'exif-software-version-value',
822 $this->literal( $val[0] ),
823 $this->literal( $val[1] )
824 )->text();
825 } else {
826 // https://phabricator.wikimedia.org/T178130
827 $val = $this->exifMsg( $tag, '', $this->literal( $val[0] ) );
829 } else {
830 $val = $this->exifMsg( $tag, '', $this->literal( $val ) );
832 break;
834 case 'ExposureTime':
835 // Show the pretty fraction as well as decimal version
836 $val = $this->msg( 'exif-exposuretime-format',
837 $this->formatFraction( $val ), $this->formatNum( $val ) )->text();
838 break;
839 case 'ISOSpeedRatings':
840 // If it's 65535 that means it's at the
841 // limit of the size of Exif::short and
842 // is really higher.
843 if ( $val === '65535' ) {
844 $val = $this->exifMsg( $tag, 'overflow' );
845 } else {
846 $val = $this->formatNum( $val );
848 break;
849 case 'FNumber':
850 $val = $this->msg( 'exif-fnumber-format',
851 $this->formatNum( $val ) )->text();
852 break;
854 case 'FocalLength':
855 case 'FocalLengthIn35mmFilm':
856 $val = $this->msg( 'exif-focallength-format',
857 $this->formatNum( $val ) )->text();
858 break;
860 case 'MaxApertureValue':
861 if ( strpos( $val, '/' ) !== false ) {
862 // need to expand this earlier to calculate fNumber
863 [ $n, $d ] = explode( '/', $val, 2 );
864 if ( is_numeric( $n ) && is_numeric( $d ) ) {
865 $val = (int)$n / (int)$d;
868 if ( is_numeric( $val ) ) {
869 $fNumber = 2 ** ( $val / 2 );
870 if ( is_finite( $fNumber ) ) {
871 $val = $this->msg( 'exif-maxaperturevalue-value',
872 $this->formatNum( $val ),
873 $this->formatNum( $fNumber, 2 )
874 )->text();
875 break;
878 $val = $this->literal( $val );
879 break;
881 case 'iimCategory':
882 switch ( strtolower( $val ) ) {
883 // See pg 29 of IPTC photo
884 // metadata standard.
885 case 'ace':
886 case 'clj':
887 case 'dis':
888 case 'fin':
889 case 'edu':
890 case 'evn':
891 case 'hth':
892 case 'hum':
893 case 'lab':
894 case 'lif':
895 case 'pol':
896 case 'rel':
897 case 'sci':
898 case 'soi':
899 case 'spo':
900 case 'war':
901 case 'wea':
902 $val = $this->exifMsg(
903 'iimcategory',
904 $val
906 break;
907 default:
908 $val = $this->literal( $val );
910 break;
911 case 'SubjectNewsCode':
912 // Essentially like iimCategory.
913 // 8 (numeric) digit hierarchical
914 // classification. We decode the
915 // first 2 digits, which provide
916 // a broad category.
917 $val = $this->convertNewsCode( $val );
918 break;
919 case 'Urgency':
920 // 1-8 with 1 being highest, 5 normal
921 // 0 is reserved, and 9 is 'user-defined'.
922 $urgency = '';
923 if ( $val === 0 || $val === 9 ) {
924 $urgency = 'other';
925 } elseif ( $val < 5 && $val > 1 ) {
926 $urgency = 'high';
927 } elseif ( $val === 5 ) {
928 $urgency = 'normal';
929 } elseif ( $val <= 8 && $val > 5 ) {
930 $urgency = 'low';
933 if ( $urgency !== '' ) {
934 $val = $this->exifMsg( 'urgency',
935 $urgency, $this->literal( $val )
937 } else {
938 $val = $this->literal( $val );
940 break;
942 // Things that have a unit of pixels.
943 case 'OriginalImageHeight':
944 case 'OriginalImageWidth':
945 case 'PixelXDimension':
946 case 'PixelYDimension':
947 case 'ImageWidth':
948 case 'ImageLength':
949 $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text();
950 break;
952 // Do not transform fields with pure text.
953 // For some languages the formatNum()
954 // conversion results to wrong output like
955 // foo,bar@example,com or foo٫bar@example٫com.
956 // Also some 'numeric' things like Scene codes
957 // are included here as we really don't want
958 // commas inserted.
959 case 'ImageDescription':
960 case 'UserComment':
961 case 'Artist':
962 case 'Copyright':
963 case 'RelatedSoundFile':
964 case 'ImageUniqueID':
965 case 'SpectralSensitivity':
966 case 'GPSSatellites':
967 case 'GPSVersionID':
968 case 'GPSMapDatum':
969 case 'Keywords':
970 case 'WorldRegionDest':
971 case 'CountryDest':
972 case 'CountryCodeDest':
973 case 'ProvinceOrStateDest':
974 case 'CityDest':
975 case 'SublocationDest':
976 case 'WorldRegionCreated':
977 case 'CountryCreated':
978 case 'CountryCodeCreated':
979 case 'ProvinceOrStateCreated':
980 case 'CityCreated':
981 case 'SublocationCreated':
982 case 'ObjectName':
983 case 'SpecialInstructions':
984 case 'Headline':
985 case 'Credit':
986 case 'Source':
987 case 'EditStatus':
988 case 'FixtureIdentifier':
989 case 'LocationDest':
990 case 'LocationDestCode':
991 case 'Writer':
992 case 'JPEGFileComment':
993 case 'iimSupplementalCategory':
994 case 'OriginalTransmissionRef':
995 case 'Identifier':
996 case 'dc-contributor':
997 case 'dc-coverage':
998 case 'dc-publisher':
999 case 'dc-relation':
1000 case 'dc-rights':
1001 case 'dc-source':
1002 case 'dc-type':
1003 case 'Lens':
1004 case 'SerialNumber':
1005 case 'CameraOwnerName':
1006 case 'Label':
1007 case 'Nickname':
1008 case 'RightsCertificate':
1009 case 'CopyrightOwner':
1010 case 'UsageTerms':
1011 case 'WebStatement':
1012 case 'OriginalDocumentID':
1013 case 'LicenseUrl':
1014 case 'MorePermissionsUrl':
1015 case 'AttributionUrl':
1016 case 'PreferredAttributionName':
1017 case 'PNGFileComment':
1018 case 'Disclaimer':
1019 case 'ContentWarning':
1020 case 'GIFFileComment':
1021 case 'SceneCode':
1022 case 'IntellectualGenre':
1023 case 'Event':
1024 case 'OrganisationInImage':
1025 case 'PersonInImage':
1026 case 'CaptureSoftware':
1027 case 'GPSAreaInformation':
1028 case 'GPSProcessingMethod':
1029 case 'StitchingSoftware':
1030 case 'SubSecTime':
1031 case 'SubSecTimeOriginal':
1032 case 'SubSecTimeDigitized':
1033 $val = $this->literal( $val );
1034 break;
1036 case 'ProjectionType':
1037 switch ( $val ) {
1038 case 'equirectangular':
1039 $val = $this->exifMsg( $tag, $val );
1040 break;
1041 default:
1042 $val = $this->literal( $val );
1043 break;
1045 break;
1046 case 'ObjectCycle':
1047 switch ( $val ) {
1048 case 'a':
1049 case 'p':
1050 case 'b':
1051 $val = $this->exifMsg( $tag, $val );
1052 break;
1053 default:
1054 $val = $this->literal( $val );
1055 break;
1057 break;
1058 case 'Copyrighted':
1059 case 'UsePanoramaViewer':
1060 case 'ExposureLockUsed':
1061 switch ( $val ) {
1062 case 'True':
1063 case 'False':
1064 $val = $this->exifMsg( $tag, $val );
1065 break;
1066 default:
1067 $val = $this->literal( $val );
1068 break;
1070 break;
1071 case 'Rating':
1072 if ( $val === '-1' ) {
1073 $val = $this->exifMsg( $tag, 'rejected' );
1074 } else {
1075 $val = $this->formatNum( $val );
1077 break;
1079 case 'LanguageCode':
1080 $lang = MediaWikiServices::getInstance()
1081 ->getLanguageNameUtils()
1082 ->getLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
1083 $val = $this->literal( $lang ?: $val );
1084 break;
1086 default:
1087 $val = $this->formatNum( $val, false, $tag );
1088 break;
1091 // End formatting values, start flattening arrays.
1092 $vals = $this->flattenArrayReal( $vals, $type );
1095 return $tags;
1099 * A function to collapse multivalued tags into a single value.
1100 * This turns an array of (for example) authors into a bulleted list.
1102 * This is public on the basis it might be useful outside of this class.
1104 * @param array $vals Array of values
1105 * @param string $type Type of array (either lang, ul, ol).
1106 * lang = language assoc array with keys being the lang code
1107 * ul = unordered list, ol = ordered list
1108 * type can also come from the '_type' member of $vals.
1109 * @param bool $noHtml If to avoid returning anything resembling HTML.
1110 * (Ugly hack for backwards compatibility with old mediawiki).
1111 * @return string Single value (in wiki-syntax).
1112 * @since 1.23
1113 * @internal
1115 public function flattenArrayReal( $vals, $type = 'ul', $noHtml = false ) {
1116 if ( !is_array( $vals ) ) {
1117 return $vals; // do nothing if not an array;
1120 if ( isset( $vals['_type'] ) ) {
1121 $type = $vals['_type'];
1122 unset( $vals['_type'] );
1125 if ( count( $vals ) === 1 && $type !== 'lang' && isset( $vals[0] ) ) {
1126 return $vals[0];
1128 if ( count( $vals ) === 0 ) {
1129 wfDebug( __METHOD__ . " metadata array with 0 elements!" );
1131 return ""; // paranoia. This should never happen
1133 // Check if $vals contains nested arrays
1134 $containsNestedArrays = in_array( true, array_map( 'is_array', $vals ), true );
1135 if ( $containsNestedArrays ) {
1136 wfLogWarning( __METHOD__ . ': Invalid $vals, contains nested arrays: ' . json_encode( $vals ) );
1139 /* @todo FIXME: This should hide some of the list entries if there are
1140 * say more than four. Especially if a field is translated into 20
1141 * languages, we don't want to show them all by default
1143 switch ( $type ) {
1144 case 'lang':
1145 // Display default, followed by ContentLanguage,
1146 // followed by the rest in no particular order.
1148 // Todo: hide some items if really long list.
1150 $content = '';
1152 $priorityLanguages = $this->getPriorityLanguages();
1153 $defaultItem = false;
1154 $defaultLang = false;
1156 // If default is set, save it for later,
1157 // as we don't know if it's equal to one of the lang codes.
1158 // (In xmp you specify the language for a default property by having
1159 // both a default prop, and one in the language that are identical)
1160 if ( isset( $vals['x-default'] ) ) {
1161 $defaultItem = $vals['x-default'];
1162 unset( $vals['x-default'] );
1164 foreach ( $priorityLanguages as $pLang ) {
1165 if ( isset( $vals[$pLang] ) ) {
1166 $isDefault = false;
1167 if ( $vals[$pLang] === $defaultItem ) {
1168 $defaultItem = false;
1169 $isDefault = true;
1171 $content .= $this->langItem( $vals[$pLang], $pLang, $isDefault, $noHtml );
1173 unset( $vals[$pLang] );
1175 if ( $this->singleLang ) {
1176 return Html::rawElement( 'span', [ 'lang' => $pLang ], $vals[$pLang] );
1181 // Now do the rest.
1182 foreach ( $vals as $lang => $item ) {
1183 if ( $item === $defaultItem ) {
1184 $defaultLang = $lang;
1185 continue;
1187 $content .= $this->langItem( $item, $lang, false, $noHtml );
1188 if ( $this->singleLang ) {
1189 return Html::rawElement( 'span', [ 'lang' => $lang ], $item );
1192 if ( $defaultItem !== false ) {
1193 $content = $this->langItem( $defaultItem, $defaultLang, true, $noHtml ) . $content;
1194 if ( $this->singleLang ) {
1195 return $defaultItem;
1198 if ( $noHtml ) {
1199 return $content;
1202 return '<ul class="metadata-langlist">' . $content . '</ul>';
1203 case 'ol':
1204 if ( $noHtml ) {
1205 return "\n#" . implode( "\n#", $vals );
1208 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
1209 case 'ul':
1210 default:
1211 if ( $noHtml ) {
1212 return "\n*" . implode( "\n*", $vals );
1215 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
1219 /** Helper function for creating lists of translations.
1221 * @param string $value Value (this is not escaped)
1222 * @param string $lang Lang code of item or false
1223 * @param bool $default If it is default value.
1224 * @param bool $noHtml If to avoid html (for back-compat)
1225 * @return string Language item (Note: despite how this looks, this is
1226 * treated as wikitext, not as HTML).
1228 private function langItem( $value, $lang, $default = false, $noHtml = false ) {
1229 if ( $lang === false && $default === false ) {
1230 throw new InvalidArgumentException( '$lang and $default cannot both be false.' );
1233 if ( $noHtml ) {
1234 $wrappedValue = $this->literal( $value );
1235 } else {
1236 $wrappedValue = '<span class="mw-metadata-lang-value">' . $this->literal( $value ) . '</span>';
1239 if ( $lang === false ) {
1240 $msg = $this->msg( 'metadata-langitem-default', $wrappedValue );
1241 if ( $noHtml ) {
1242 return $msg->text() . "\n\n";
1243 } /* else */
1245 return '<li class="mw-metadata-lang-default">' . $msg->text() . "</li>\n";
1248 $lowLang = strtolower( $lang );
1249 $languageNameUtils = MediaWikiServices::getInstance()->getLanguageNameUtils();
1250 $langName = $languageNameUtils->getLanguageName( $lowLang );
1251 if ( $langName === '' ) {
1252 // try just the base language name. (aka en-US -> en ).
1253 $langPrefix = explode( '-', $lowLang, 2 )[0];
1254 $langName = $languageNameUtils->getLanguageName( $langPrefix );
1255 if ( $langName === '' ) {
1256 // give up.
1257 $langName = $lang;
1260 // else we have a language specified
1262 $msg = $this->msg( 'metadata-langitem', $wrappedValue, $langName, $lang );
1263 if ( $noHtml ) {
1264 return '*' . $msg->text();
1265 } /* else: */
1267 $item = '<li class="mw-metadata-lang-code-' . $lang;
1268 if ( $default ) {
1269 $item .= ' mw-metadata-lang-default';
1271 $item .= '" lang="' . $lang . '">';
1272 $item .= $msg->text();
1273 $item .= "</li>\n";
1275 return $item;
1279 * Convenience function for getFormattedData()
1281 * @param string|int|null $val The literal value
1282 * @return string The value, properly escaped as wikitext -- with some
1283 * exceptions to allow auto-linking, etc.
1285 protected function literal( $val ): string {
1286 if ( $val === null ) {
1287 return '';
1289 // T266707: historically this has used htmlspecialchars to protect
1290 // the string contents, but it should probably be changed to use
1291 // wfEscapeWikitext() instead -- however, "we still want to auto-link
1292 // urls" so wfEscapeWikitext isn't *quite* right...
1293 return htmlspecialchars( $val );
1297 * Convenience function for getFormattedData()
1299 * @param string $tag The tag name to pass on
1300 * @param string|int $val The value of the tag
1301 * @param string|null $arg A wikitext argument to pass ($1)
1302 * @param string|null $arg2 A 2nd wikitext argument to pass ($2)
1303 * @return string The text content of "exif-$tag-$val" message in lower case
1305 private function exifMsg( $tag, $val, $arg = null, $arg2 = null ) {
1306 if ( $val === '' ) {
1307 $val = 'value';
1310 return $this->msg(
1311 MediaWikiServices::getInstance()->getContentLanguage()->lc( "exif-$tag-$val" ),
1312 $arg,
1313 $arg2
1314 )->text();
1318 * Format a number, convert numbers from fractions into floating point
1319 * numbers, joins arrays of numbers with commas.
1321 * @param mixed $num The value to format
1322 * @param float|int|false $round Digits to round to or false.
1323 * @param string|null $tagName (optional) The name of the tag (for debugging)
1324 * @return mixed A floating point number or whatever we were fed
1326 private function formatNum( $num, $round = false, $tagName = null ) {
1327 $m = [];
1328 if ( is_array( $num ) ) {
1329 $out = [];
1330 foreach ( $num as $number ) {
1331 $out[] = $this->formatNum( $number, $round, $tagName );
1334 return $this->getLanguage()->commaList( $out );
1336 if ( is_numeric( $num ) ) {
1337 if ( $round !== false ) {
1338 $num = round( $num, $round );
1340 return $this->getLanguage()->formatNum( $num );
1342 $num ??= '';
1343 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1344 if ( $m[2] !== 0 ) {
1345 $newNum = (int)$m[1] / (int)$m[2];
1346 if ( $round !== false ) {
1347 $newNum = round( $newNum, $round );
1349 } else {
1350 $newNum = $num;
1353 return $this->getLanguage()->formatNum( $newNum );
1355 # T267370: there are a lot of strange EXIF tags floating around.
1356 LoggerFactory::getInstance( 'formatnum' )->warning(
1357 'FormatMetadata::formatNum with non-numeric value',
1359 'tag' => $tagName,
1360 'value' => $num,
1363 return $this->literal( $num );
1367 * Format a rational number, reducing fractions
1369 * @param mixed $num The value to format
1370 * @return mixed A floating point number or whatever we were fed
1372 private function formatFraction( $num ) {
1373 $m = [];
1374 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1375 $numerator = (int)$m[1];
1376 $denominator = (int)$m[2];
1377 $gcd = $this->gcd( abs( $numerator ), $denominator );
1378 if ( $gcd !== 0 ) {
1379 // 0 shouldn't happen! ;)
1380 return $this->formatNum( $numerator / $gcd ) . '/' . $this->formatNum( $denominator / $gcd );
1384 return $this->formatNum( $num );
1388 * Calculate the greatest common divisor of two integers.
1390 * @param int $a Numerator
1391 * @param int $b Denominator
1392 * @return int
1394 private function gcd( $a, $b ) {
1396 // https://en.wikipedia.org/wiki/Euclidean_algorithm
1397 // Recursive form would be:
1398 if ( $b == 0 )
1399 return $a;
1400 else
1401 return gcd( $b, $a % $b );
1403 while ( $b != 0 ) {
1404 $remainder = $a % $b;
1406 // tail recursion...
1407 $a = $b;
1408 $b = $remainder;
1411 return $a;
1415 * Fetch the human readable version of a news code.
1416 * A news code is an 8 digit code. The first two
1417 * digits are a general classification, so we just
1418 * translate that.
1420 * Note, leading 0's are significant, so this is
1421 * a string, not an int.
1423 * @param string $val The 8 digit news code.
1424 * @return string The human readable form
1426 private function convertNewsCode( $val ) {
1427 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1428 // Not a valid news code.
1429 return $val;
1431 $cat = '';
1432 switch ( substr( $val, 0, 2 ) ) {
1433 case '01':
1434 $cat = 'ace';
1435 break;
1436 case '02':
1437 $cat = 'clj';
1438 break;
1439 case '03':
1440 $cat = 'dis';
1441 break;
1442 case '04':
1443 $cat = 'fin';
1444 break;
1445 case '05':
1446 $cat = 'edu';
1447 break;
1448 case '06':
1449 $cat = 'evn';
1450 break;
1451 case '07':
1452 $cat = 'hth';
1453 break;
1454 case '08':
1455 $cat = 'hum';
1456 break;
1457 case '09':
1458 $cat = 'lab';
1459 break;
1460 case '10':
1461 $cat = 'lif';
1462 break;
1463 case '11':
1464 $cat = 'pol';
1465 break;
1466 case '12':
1467 $cat = 'rel';
1468 break;
1469 case '13':
1470 $cat = 'sci';
1471 break;
1472 case '14':
1473 $cat = 'soi';
1474 break;
1475 case '15':
1476 $cat = 'spo';
1477 break;
1478 case '16':
1479 $cat = 'war';
1480 break;
1481 case '17':
1482 $cat = 'wea';
1483 break;
1485 if ( $cat !== '' ) {
1486 $catMsg = $this->exifMsg( 'iimcategory', $cat );
1487 $val = $this->exifMsg( 'subjectnewscode', '', $this->literal( $val ), $catMsg );
1490 return $val;
1494 * Format a coordinate value, convert numbers from floating point
1495 * into degree minute second representation.
1497 * @param float|string $coord Expected to be a number or numeric string in degrees
1498 * @param string $type "latitude" or "longitude"
1499 * @return string
1501 private function formatCoords( $coord, string $type ) {
1502 if ( !is_numeric( $coord ) ) {
1503 wfDebugLog( 'exif', __METHOD__ . ": \"$coord\" is not a number" );
1504 return $this->literal( (string)$coord );
1507 $ref = '';
1508 if ( $coord < 0 ) {
1509 $nCoord = -$coord;
1510 if ( $type === 'latitude' ) {
1511 $ref = 'S';
1512 } elseif ( $type === 'longitude' ) {
1513 $ref = 'W';
1515 } else {
1516 $nCoord = (float)$coord;
1517 if ( $type === 'latitude' ) {
1518 $ref = 'N';
1519 } elseif ( $type === 'longitude' ) {
1520 $ref = 'E';
1524 $deg = floor( $nCoord );
1525 $min = floor( ( $nCoord - $deg ) * 60 );
1526 $sec = round( ( ( $nCoord - $deg ) * 60 - $min ) * 60, 2 );
1528 $deg = $this->formatNum( $deg );
1529 $min = $this->formatNum( $min );
1530 $sec = $this->formatNum( $sec );
1532 // Note the default message "$1° $2′ $3″ $4" ignores the 5th parameter
1533 return $this->msg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $this->literal( $coord ) )->text();
1537 * Format the contact info field into a single value.
1539 * This function might be called from
1540 * ExifBitmapHandler::convertMetadataVersion which is why it is
1541 * public.
1543 * @param array $vals Array with fields of the ContactInfo
1544 * struct defined in the IPTC4XMP spec. Or potentially
1545 * an array with one element that is a free form text
1546 * value from the older iptc iim 1:118 prop.
1547 * @return string HTML-ish looking wikitext
1548 * @since 1.23 no longer static
1550 public function collapseContactInfo( array $vals ) {
1551 if ( !( isset( $vals['CiAdrExtadr'] )
1552 || isset( $vals['CiAdrCity'] )
1553 || isset( $vals['CiAdrCtry'] )
1554 || isset( $vals['CiEmailWork'] )
1555 || isset( $vals['CiTelWork'] )
1556 || isset( $vals['CiAdrPcode'] )
1557 || isset( $vals['CiAdrRegion'] )
1558 || isset( $vals['CiUrlWork'] )
1559 ) ) {
1560 // We don't have any sub-properties
1561 // This could happen if its using old
1562 // iptc that just had this as a free-form
1563 // text value.
1564 // Note: people often insert >, etc into
1565 // the metadata which should not be interpreted
1566 // but we still want to auto-link urls.
1567 foreach ( $vals as &$val ) {
1568 $val = $this->literal( $val );
1571 return $this->flattenArrayReal( $vals );
1574 // We have a real ContactInfo field.
1575 // Its unclear if all these fields have to be
1576 // set, so assume they do not.
1577 $url = $tel = $street = $city = $country = '';
1578 $email = $postal = $region = '';
1580 // Also note, some of the class names this uses
1581 // are similar to those used by hCard. This is
1582 // mostly because they're sensible names. This
1583 // does not (and does not attempt to) output
1584 // stuff in the hCard microformat. However it
1585 // might output in the adr microformat.
1587 if ( isset( $vals['CiAdrExtadr'] ) ) {
1588 // Todo: This can potentially be multi-line.
1589 // Need to check how that works in XMP.
1590 $street = '<span class="extended-address">'
1591 . $this->literal(
1592 $vals['CiAdrExtadr'] )
1593 . '</span>';
1595 if ( isset( $vals['CiAdrCity'] ) ) {
1596 $city = '<span class="locality">'
1597 . $this->literal( $vals['CiAdrCity'] )
1598 . '</span>';
1600 if ( isset( $vals['CiAdrCtry'] ) ) {
1601 $country = '<span class="country-name">'
1602 . $this->literal( $vals['CiAdrCtry'] )
1603 . '</span>';
1605 if ( isset( $vals['CiEmailWork'] ) ) {
1606 $emails = [];
1607 // Have to split multiple emails at commas/new lines.
1608 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1609 foreach ( $splitEmails as $e1 ) {
1610 // Also split on comma
1611 foreach ( explode( ',', $e1 ) as $e2 ) {
1612 $finalEmail = trim( $e2 );
1613 if ( $finalEmail === ',' || $finalEmail === '' ) {
1614 continue;
1616 if ( strpos( $finalEmail, '<' ) !== false ) {
1617 // Don't do fancy formatting to
1618 // "My name" <foo@bar.com> style stuff
1619 $emails[] = $this->literal( $finalEmail );
1620 } else {
1621 $emails[] = '[mailto:'
1622 . $finalEmail
1623 . ' <span class="email">'
1624 . $this->literal( $finalEmail )
1625 . '</span>]';
1629 $email = implode( ', ', $emails );
1631 if ( isset( $vals['CiTelWork'] ) ) {
1632 $tel = '<span class="tel">'
1633 . $this->literal( $vals['CiTelWork'] )
1634 . '</span>';
1636 if ( isset( $vals['CiAdrPcode'] ) ) {
1637 $postal = '<span class="postal-code">'
1638 . $this->literal( $vals['CiAdrPcode'] )
1639 . '</span>';
1641 if ( isset( $vals['CiAdrRegion'] ) ) {
1642 // Note this is province/state.
1643 $region = '<span class="region">'
1644 . $this->literal( $vals['CiAdrRegion'] )
1645 . '</span>';
1647 if ( isset( $vals['CiUrlWork'] ) ) {
1648 $url = '<span class="url">'
1649 . $this->literal( $vals['CiUrlWork'] )
1650 . '</span>';
1653 return $this->msg( 'exif-contact-value', $email, $url,
1654 $street, $city, $region, $postal, $country, $tel )->text();
1658 * Get a list of fields that are visible by default.
1660 * @return string[]
1661 * @since 1.23
1663 public static function getVisibleFields() {
1664 $fields = [];
1665 $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
1666 foreach ( $lines as $line ) {
1667 $matches = [];
1668 if ( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
1669 $fields[] = $matches[1];
1672 $fields = array_map( 'strtolower', $fields );
1674 return $fields;
1678 * Get an array of extended metadata. (See the imageinfo API for format.)
1680 * @param File $file File to use
1681 * @return array [<property name> => ['value' => <value>]], or [] on error
1682 * @since 1.23
1684 public function fetchExtendedMetadata( File $file ) {
1685 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1687 // If revision deleted, exit immediately
1688 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1689 return [];
1692 $cacheKey = $cache->makeKey(
1693 'getExtendedMetadata',
1694 $this->getLanguage()->getCode(),
1695 (int)$this->singleLang,
1696 $file->getSha1()
1699 $cachedValue = $cache->get( $cacheKey );
1700 if (
1701 $cachedValue
1702 && $this->getHookRunner()->onValidateExtendedMetadataCache( $cachedValue['timestamp'], $file )
1704 $extendedMetadata = $cachedValue['data'];
1705 } else {
1706 $maxCacheTime = ( $file instanceof ForeignAPIFile ) ? 60 * 60 * 12 : 60 * 60 * 24 * 30;
1707 $fileMetadata = $this->getExtendedMetadataFromFile( $file );
1708 $extendedMetadata = $this->getExtendedMetadataFromHook( $file, $fileMetadata, $maxCacheTime );
1709 if ( $this->singleLang ) {
1710 $this->resolveMultilangMetadata( $extendedMetadata );
1712 $this->discardMultipleValues( $extendedMetadata );
1713 // Make sure the metadata won't break the API when an XML format is used.
1714 // This is an API-specific function so it would be cleaner to call it from
1715 // outside fetchExtendedMetadata, but this way we don't need to redo the
1716 // computation on a cache hit.
1717 $this->sanitizeArrayForAPI( $extendedMetadata );
1718 $valueToCache = [ 'data' => $extendedMetadata, 'timestamp' => wfTimestampNow() ];
1719 $cache->set( $cacheKey, $valueToCache, $maxCacheTime );
1722 return $extendedMetadata;
1726 * Get file-based metadata in standardized format.
1728 * Note that for a remote file, this might return metadata supplied by extensions.
1730 * @param File $file File to use
1731 * @return array [<property name> => ['value' => <value>]], or [] on error
1732 * @since 1.23
1734 protected function getExtendedMetadataFromFile( File $file ) {
1735 // If this is a remote file accessed via an API request, we already
1736 // have remote metadata so we just ignore any local one
1737 if ( $file instanceof ForeignAPIFile ) {
1738 // In case of error we pretend no metadata - this will get cached.
1739 // Might or might not be a good idea.
1740 return $file->getExtendedMetadata() ?: [];
1743 $uploadDate = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
1745 $fileMetadata = [
1746 // This is modification time, which is close to "upload" time.
1747 'DateTime' => [
1748 'value' => $uploadDate,
1749 'source' => 'mediawiki-metadata',
1753 $title = $file->getTitle();
1754 if ( $title ) {
1755 $text = $title->getText();
1756 $pos = strrpos( $text, '.' );
1758 if ( $pos ) {
1759 $name = substr( $text, 0, $pos );
1760 } else {
1761 $name = $text;
1764 $fileMetadata['ObjectName'] = [
1765 'value' => $name,
1766 'source' => 'mediawiki-metadata',
1770 return $fileMetadata;
1774 * Get additional metadata from hooks in standardized format.
1776 * @param File $file File to use
1777 * @param array $extendedMetadata
1778 * @param int &$maxCacheTime Hook handlers might use this parameter to override cache time
1780 * @return array [<property name> => ['value' => <value>]], or [] on error
1781 * @since 1.23
1783 protected function getExtendedMetadataFromHook( File $file, array $extendedMetadata,
1784 &$maxCacheTime
1786 $this->getHookRunner()->onGetExtendedMetadata(
1787 $extendedMetadata,
1788 $file,
1789 $this->getContext(),
1790 $this->singleLang,
1791 $maxCacheTime
1794 $visible = array_fill_keys( self::getVisibleFields(), true );
1795 foreach ( $extendedMetadata as $key => $value ) {
1796 if ( !isset( $visible[strtolower( $key )] ) ) {
1797 $extendedMetadata[$key]['hidden'] = '';
1801 return $extendedMetadata;
1805 * Turns an XMP-style multilang array into a single value.
1806 * If the value is not a multilang array, it is returned unchanged.
1807 * See mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
1808 * @param mixed $value
1809 * @return mixed Value in best language, null if there were no languages at all
1810 * @since 1.23
1812 protected function resolveMultilangValue( $value ) {
1813 if (
1814 !is_array( $value )
1815 || !isset( $value['_type'] )
1816 || $value['_type'] !== 'lang'
1818 return $value; // do nothing if not a multilang array
1821 // choose the language best matching user or site settings
1822 $priorityLanguages = $this->getPriorityLanguages();
1823 foreach ( $priorityLanguages as $lang ) {
1824 if ( isset( $value[$lang] ) ) {
1825 return $value[$lang];
1829 // otherwise go with the default language, if set
1830 if ( isset( $value['x-default'] ) ) {
1831 return $value['x-default'];
1834 // otherwise just return any one language
1835 unset( $value['_type'] );
1836 if ( $value ) {
1837 return reset( $value );
1840 // this should not happen; signal error
1841 return null;
1845 * Turns an XMP-style multivalue array into a single value by dropping all but the first
1846 * value. If the value is not a multivalue array (or a multivalue array inside a multilang
1847 * array), it is returned unchanged.
1848 * See mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
1849 * @param mixed $value
1850 * @return mixed The value, or the first value if there were multiple ones
1851 * @since 1.25
1853 protected function resolveMultivalueValue( $value ) {
1854 if ( !is_array( $value ) ) {
1855 return $value;
1857 if ( isset( $value['_type'] ) && $value['_type'] === 'lang' ) {
1858 // if this is a multilang array, process fields separately
1859 $newValue = [];
1860 foreach ( $value as $k => $v ) {
1861 $newValue[$k] = $this->resolveMultivalueValue( $v );
1863 return $newValue;
1865 // _type is 'ul' or 'ol' or missing in which case it defaults to 'ul'
1866 $v = reset( $value );
1867 if ( key( $value ) === '_type' ) {
1868 $v = next( $value );
1870 return $v;
1874 * Takes an array returned by the getExtendedMetadata* functions,
1875 * and resolves multi-language values in it.
1876 * @param array &$metadata
1877 * @since 1.23
1879 protected function resolveMultilangMetadata( &$metadata ) {
1880 if ( !is_array( $metadata ) ) {
1881 return;
1883 foreach ( $metadata as &$field ) {
1884 if ( isset( $field['value'] ) ) {
1885 $field['value'] = $this->resolveMultilangValue( $field['value'] );
1891 * Takes an array returned by the getExtendedMetadata* functions,
1892 * and turns all fields into single-valued ones by dropping extra values.
1893 * @param array &$metadata
1894 * @since 1.25
1896 protected function discardMultipleValues( &$metadata ) {
1897 if ( !is_array( $metadata ) ) {
1898 return;
1900 foreach ( $metadata as $key => &$field ) {
1901 if ( $key === 'Software' || $key === 'Contact' ) {
1902 // we skip some fields which have composite values. They are not particularly interesting
1903 // and you can get them via the metadata / commonmetadata APIs anyway.
1904 continue;
1906 if ( isset( $field['value'] ) ) {
1907 $field['value'] = $this->resolveMultivalueValue( $field['value'] );
1913 * Makes sure the given array is a valid API response fragment
1914 * @param array &$arr
1916 protected function sanitizeArrayForAPI( &$arr ) {
1917 if ( !is_array( $arr ) ) {
1918 return;
1921 $counter = 1;
1922 foreach ( $arr as $key => &$value ) {
1923 $sanitizedKey = $this->sanitizeKeyForAPI( $key );
1924 if ( $sanitizedKey !== $key ) {
1925 if ( isset( $arr[$sanitizedKey] ) ) {
1926 // Make the sanitized keys hopefully unique.
1927 // To make it definitely unique would be too much effort, given that
1928 // sanitizing is only needed for misformatted metadata anyway, but
1929 // this at least covers the case when $arr is numeric.
1930 $sanitizedKey .= $counter;
1931 ++$counter;
1933 $arr[$sanitizedKey] = $arr[$key];
1934 unset( $arr[$key] );
1936 if ( is_array( $value ) ) {
1937 $this->sanitizeArrayForAPI( $value );
1940 unset( $value );
1942 // Handle API metadata keys (particularly "_type")
1943 $keys = array_filter( array_keys( $arr ), [ ApiResult::class, 'isMetadataKey' ] );
1944 if ( $keys ) {
1945 ApiResult::setPreserveKeysList( $arr, $keys );
1950 * Turns a string into a valid API identifier.
1951 * @param string $key
1952 * @return string
1953 * @since 1.23
1955 protected function sanitizeKeyForAPI( $key ) {
1956 // drop all characters which are not valid in an XML tag name
1957 // a bunch of non-ASCII letters would be valid but probably won't
1958 // be used so we take the easy way
1959 $key = preg_replace( '/[^a-zA-z0-9_:.\-]/', '', $key );
1960 // drop characters which are invalid at the first position
1961 $key = preg_replace( '/^[\d\-.]+/', '', $key );
1963 if ( $key === '' ) {
1964 $key = '_';
1965 // special case for an internal keyword
1966 } elseif ( $key === '_element' ) {
1967 $key = 'element';
1970 return $key;
1974 * Returns a list of languages (first is best) to use when formatting multilang fields,
1975 * based on user and site preferences.
1976 * @return array
1977 * @since 1.23
1979 protected function getPriorityLanguages() {
1980 $priorityLanguages = MediaWikiServices::getInstance()
1981 ->getLanguageFallback()
1982 ->getAllIncludingSiteLanguage( $this->getLanguage()->getCode() );
1983 $priorityLanguages = array_merge(
1984 (array)$this->getLanguage()->getCode(),
1985 $priorityLanguages[0],
1986 $priorityLanguages[1]
1989 return $priorityLanguages;