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
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
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
;
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.
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
;
61 * Only output a single language for multi-language fields
65 protected $singleLang = false;
68 * Trigger only outputting single language for multilanguage fields
73 public function setSingleLanguage( $val ) {
74 $this->singleLang
= $val;
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
90 public static function getFormattedData( $tags, $context = false ) {
93 $obj->setContext( $context );
96 return $obj->makeFormattedData( $tags );
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 )
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 ) ) {
128 // _type is a special value to say what array type
129 if ( isset( $vals['_type'] ) ) {
130 $type = $vals['_type'];
131 unset( $vals['_type'] );
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
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
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 );
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 );
186 foreach ( $vals as &$val ) {
201 $val = $this->exifMsg( $tag, $val );
204 /* If not recognized, display as is. */
205 $val = $this->literal( $val );
210 case 'PhotometricInterpretation':
224 $val = $this->exifMsg( $tag, $val );
227 /* If not recognized, display as is. */
228 $val = $this->literal( $val );
243 $val = $this->exifMsg( $tag, $val );
246 /* If not recognized, display as is. */
247 $val = $this->literal( $val );
252 case 'PlanarConfiguration':
256 $val = $this->exifMsg( $tag, $val );
259 /* If not recognized, display as is. */
260 $val = $this->literal( $val );
265 // TODO: YCbCrSubSampling
266 case 'YCbCrPositioning':
270 $val = $this->exifMsg( $tag, $val );
273 /* If not recognized, display as is. */
274 $val = $this->literal( $val );
281 switch ( $resolutionunit ) {
283 $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) );
286 $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) );
289 /* If not recognized, display as is. */
290 $val = $this->literal( $val );
295 // TODO: YCbCrCoefficients #p27 (see annex E)
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 );
313 $val = $this->exifMsg( $tag, $val );
316 /* If not recognized, display as is. */
317 $val = $this->literal( $val );
322 case 'ComponentsConfiguration':
331 $val = $this->exifMsg( $tag, $val );
334 /* If not recognized, display as is. */
335 $val = $this->literal( $val );
341 case 'DateTimeOriginal':
342 case 'DateTimeDigitized':
343 case 'DateTimeReleased':
344 case 'DateTimeExpires':
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();
355 '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D',
359 $time = wfTimestamp( TS_MW
, $val );
360 if ( $time && (int)$time > 0 ) {
361 $val = $this->getLanguage()->timeanddate( $time );
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 );
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 )
379 if ( $time && (int)$time > 0 ) {
380 $val = $this->getLanguage()->date( $time );
384 // else it will just output $val without formatting it.
385 $val = $this->literal( $val );
388 case 'ExposureProgram':
399 $val = $this->exifMsg( $tag, $val );
402 /* If not recognized, display as is. */
403 $val = $this->literal( $val );
408 case 'SubjectDistance':
409 $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) );
423 $val = $this->exifMsg( $tag, $val );
426 /* If not recognized, display as is. */
427 $val = $this->literal( $val );
455 $val = $this->exifMsg( $tag, $val );
458 /* If not recognized, display as is. */
459 $val = $this->literal( $val );
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,
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 ) {
483 $fullTag = $tag . '-' . $subTag;
484 $flashMsgs[] = $this->exifMsg( $fullTag, $subValue );
486 $val = $this->getLanguage()->commaList( $flashMsgs );
489 case 'FocalPlaneResolutionUnit':
492 $val = $this->exifMsg( $tag, $val );
495 /* If not recognized, display as is. */
496 $val = $this->literal( $val );
501 case 'SensingMethod':
510 $val = $this->exifMsg( $tag, $val );
513 /* If not recognized, display as is. */
514 $val = $this->literal( $val );
522 $val = $this->exifMsg( $tag, $val );
525 /* If not recognized, display as is. */
526 $val = $this->literal( $val );
534 $val = $this->exifMsg( $tag, $val );
537 /* If not recognized, display as is. */
538 $val = $this->literal( $val );
543 case 'CustomRendered':
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 );
558 /* If not recognized, display as is. */
559 $val = $this->literal( $val );
569 $val = $this->exifMsg( $tag, $val );
572 /* If not recognized, display as is. */
581 $val = $this->exifMsg( $tag, $val );
584 /* If not recognized, display as is. */
585 $val = $this->literal( $val );
590 case 'SceneCaptureType':
596 $val = $this->exifMsg( $tag, $val );
599 /* If not recognized, display as is. */
600 $val = $this->literal( $val );
612 $val = $this->exifMsg( $tag, $val );
615 /* If not recognized, display as is. */
616 $val = $this->literal( $val );
626 $val = $this->exifMsg( $tag, $val );
629 /* If not recognized, display as is. */
630 $val = $this->literal( $val );
640 $val = $this->exifMsg( $tag, $val );
643 /* If not recognized, display as is. */
644 $val = $this->literal( $val );
654 $val = $this->exifMsg( $tag, $val );
657 /* If not recognized, display as is. */
658 $val = $this->literal( $val );
663 case 'SubjectDistanceRange':
669 $val = $this->exifMsg( $tag, $val );
672 /* If not recognized, display as is. */
673 $val = $this->literal( $val );
678 // The GPS...Ref values are kept for compatibility, probably won't be reached.
679 case 'GPSLatitudeRef':
680 case 'GPSDestLatitudeRef':
684 $val = $this->exifMsg( 'GPSLatitude', $val );
687 /* If not recognized, display as is. */
688 $val = $this->literal( $val );
693 case 'GPSLongitudeRef':
694 case 'GPSDestLongitudeRef':
698 $val = $this->exifMsg( 'GPSLongitude', $val );
701 /* If not recognized, display as is. */
702 $val = $this->literal( $val );
709 $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) );
711 $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) );
719 $val = $this->exifMsg( $tag, $val );
722 /* If not recognized, display as is. */
723 $val = $this->literal( $val );
728 case 'GPSMeasureMode':
732 $val = $this->exifMsg( $tag, $val );
735 /* If not recognized, display as is. */
736 $val = $this->literal( $val );
742 case 'GPSImgDirectionRef':
743 case 'GPSDestBearingRef':
747 $val = $this->exifMsg( 'GPSDirection', $val );
750 /* If not recognized, display as is. */
751 $val = $this->literal( $val );
757 case 'GPSDestLatitude':
758 $val = $this->formatCoords( $val, 'latitude' );
761 case 'GPSDestLongitude':
762 $val = $this->formatCoords( $val, 'longitude' );
770 $val = $this->exifMsg( 'GPSSpeed', $val );
773 /* If not recognized, display as is. */
774 $val = $this->literal( $val );
779 case 'GPSDestDistanceRef':
784 $val = $this->exifMsg( 'GPSDestDistance', $val );
787 /* If not recognized, display as is. */
788 $val = $this->literal( $val );
794 // See https://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
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 ) );
804 $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) );
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.
813 $val = $this->exifMsg( $tag, '', $this->literal( $val ) );
817 if ( is_array( $val ) ) {
818 if ( count( $val ) > 1 ) {
819 // if its a software, version array.
821 'exif-software-version-value',
822 $this->literal( $val[0] ),
823 $this->literal( $val[1] )
826 // https://phabricator.wikimedia.org/T178130
827 $val = $this->exifMsg( $tag, '', $this->literal( $val[0] ) );
830 $val = $this->exifMsg( $tag, '', $this->literal( $val ) );
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();
839 case 'ISOSpeedRatings':
840 // If it's 65535 that means it's at the
841 // limit of the size of Exif::short and
843 if ( $val === '65535' ) {
844 $val = $this->exifMsg( $tag, 'overflow' );
846 $val = $this->formatNum( $val );
850 $val = $this->msg( 'exif-fnumber-format',
851 $this->formatNum( $val ) )->text();
855 case 'FocalLengthIn35mmFilm':
856 $val = $this->msg( 'exif-focallength-format',
857 $this->formatNum( $val ) )->text();
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 )
878 $val = $this->literal( $val );
882 switch ( strtolower( $val ) ) {
883 // See pg 29 of IPTC photo
884 // metadata standard.
902 $val = $this->exifMsg(
908 $val = $this->literal( $val );
911 case 'SubjectNewsCode':
912 // Essentially like iimCategory.
913 // 8 (numeric) digit hierarchical
914 // classification. We decode the
915 // first 2 digits, which provide
917 $val = $this->convertNewsCode( $val );
920 // 1-8 with 1 being highest, 5 normal
921 // 0 is reserved, and 9 is 'user-defined'.
923 if ( $val === 0 ||
$val === 9 ) {
925 } elseif ( $val < 5 && $val > 1 ) {
927 } elseif ( $val === 5 ) {
929 } elseif ( $val <= 8 && $val > 5 ) {
933 if ( $urgency !== '' ) {
934 $val = $this->exifMsg( 'urgency',
935 $urgency, $this->literal( $val )
938 $val = $this->literal( $val );
942 // Things that have a unit of pixels.
943 case 'OriginalImageHeight':
944 case 'OriginalImageWidth':
945 case 'PixelXDimension':
946 case 'PixelYDimension':
949 $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text();
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
959 case 'ImageDescription':
963 case 'RelatedSoundFile':
964 case 'ImageUniqueID':
965 case 'SpectralSensitivity':
966 case 'GPSSatellites':
970 case 'WorldRegionDest':
972 case 'CountryCodeDest':
973 case 'ProvinceOrStateDest':
975 case 'SublocationDest':
976 case 'WorldRegionCreated':
977 case 'CountryCreated':
978 case 'CountryCodeCreated':
979 case 'ProvinceOrStateCreated':
981 case 'SublocationCreated':
983 case 'SpecialInstructions':
988 case 'FixtureIdentifier':
990 case 'LocationDestCode':
992 case 'JPEGFileComment':
993 case 'iimSupplementalCategory':
994 case 'OriginalTransmissionRef':
996 case 'dc-contributor':
1004 case 'SerialNumber':
1005 case 'CameraOwnerName':
1008 case 'RightsCertificate':
1009 case 'CopyrightOwner':
1011 case 'WebStatement':
1012 case 'OriginalDocumentID':
1014 case 'MorePermissionsUrl':
1015 case 'AttributionUrl':
1016 case 'PreferredAttributionName':
1017 case 'PNGFileComment':
1019 case 'ContentWarning':
1020 case 'GIFFileComment':
1022 case 'IntellectualGenre':
1024 case 'OrganisationInImage':
1025 case 'PersonInImage':
1026 case 'CaptureSoftware':
1027 case 'GPSAreaInformation':
1028 case 'GPSProcessingMethod':
1029 case 'StitchingSoftware':
1031 case 'SubSecTimeOriginal':
1032 case 'SubSecTimeDigitized':
1033 $val = $this->literal( $val );
1036 case 'ProjectionType':
1038 case 'equirectangular':
1039 $val = $this->exifMsg( $tag, $val );
1042 $val = $this->literal( $val );
1051 $val = $this->exifMsg( $tag, $val );
1054 $val = $this->literal( $val );
1059 case 'UsePanoramaViewer':
1060 case 'ExposureLockUsed':
1064 $val = $this->exifMsg( $tag, $val );
1067 $val = $this->literal( $val );
1072 if ( $val === '-1' ) {
1073 $val = $this->exifMsg( $tag, 'rejected' );
1075 $val = $this->formatNum( $val );
1079 case 'LanguageCode':
1080 $lang = MediaWikiServices
::getInstance()
1081 ->getLanguageNameUtils()
1082 ->getLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
1083 $val = $this->literal( $lang ?
: $val );
1087 $val = $this->formatNum( $val, false, $tag );
1091 // End formatting values, start flattening arrays.
1092 $vals = $this->flattenArrayReal( $vals, $type );
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).
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] ) ) {
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
1145 // Display default, followed by ContentLanguage,
1146 // followed by the rest in no particular order.
1148 // Todo: hide some items if really long list.
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] ) ) {
1167 if ( $vals[$pLang] === $defaultItem ) {
1168 $defaultItem = false;
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] );
1182 foreach ( $vals as $lang => $item ) {
1183 if ( $item === $defaultItem ) {
1184 $defaultLang = $lang;
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;
1202 return '<ul class="metadata-langlist">' . $content . '</ul>';
1205 return "\n#" . implode( "\n#", $vals );
1208 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
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.' );
1234 $wrappedValue = $this->literal( $value );
1236 $wrappedValue = '<span class="mw-metadata-lang-value">' . $this->literal( $value ) . '</span>';
1239 if ( $lang === false ) {
1240 $msg = $this->msg( 'metadata-langitem-default', $wrappedValue );
1242 return $msg->text() . "\n\n";
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 === '' ) {
1260 // else we have a language specified
1262 $msg = $this->msg( 'metadata-langitem', $wrappedValue, $langName, $lang );
1264 return '*' . $msg->text();
1267 $item = '<li class="mw-metadata-lang-code-' . $lang;
1269 $item .= ' mw-metadata-lang-default';
1271 $item .= '" lang="' . $lang . '">';
1272 $item .= $msg->text();
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 ) {
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 === '' ) {
1311 MediaWikiServices
::getInstance()->getContentLanguage()->lc( "exif-$tag-$val" ),
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 ) {
1328 if ( is_array( $num ) ) {
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 );
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 );
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',
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 ) {
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 );
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
1394 private function gcd( $a, $b ) {
1396 // https://en.wikipedia.org/wiki/Euclidean_algorithm
1397 // Recursive form would be:
1401 return gcd( $b, $a % $b );
1404 $remainder = $a %
$b;
1406 // tail recursion...
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
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.
1432 switch ( substr( $val, 0, 2 ) ) {
1485 if ( $cat !== '' ) {
1486 $catMsg = $this->exifMsg( 'iimcategory', $cat );
1487 $val = $this->exifMsg( 'subjectnewscode', '', $this->literal( $val ), $catMsg );
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"
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 );
1510 if ( $type === 'latitude' ) {
1512 } elseif ( $type === 'longitude' ) {
1516 $nCoord = (float)$coord;
1517 if ( $type === 'latitude' ) {
1519 } elseif ( $type === 'longitude' ) {
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
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'] )
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
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">'
1592 $vals['CiAdrExtadr'] )
1595 if ( isset( $vals['CiAdrCity'] ) ) {
1596 $city = '<span class="locality">'
1597 . $this->literal( $vals['CiAdrCity'] )
1600 if ( isset( $vals['CiAdrCtry'] ) ) {
1601 $country = '<span class="country-name">'
1602 . $this->literal( $vals['CiAdrCtry'] )
1605 if ( isset( $vals['CiEmailWork'] ) ) {
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 === '' ) {
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 );
1621 $emails[] = '[mailto:'
1623 . ' <span class="email">'
1624 . $this->literal( $finalEmail )
1629 $email = implode( ', ', $emails );
1631 if ( isset( $vals['CiTelWork'] ) ) {
1632 $tel = '<span class="tel">'
1633 . $this->literal( $vals['CiTelWork'] )
1636 if ( isset( $vals['CiAdrPcode'] ) ) {
1637 $postal = '<span class="postal-code">'
1638 . $this->literal( $vals['CiAdrPcode'] )
1641 if ( isset( $vals['CiAdrRegion'] ) ) {
1642 // Note this is province/state.
1643 $region = '<span class="region">'
1644 . $this->literal( $vals['CiAdrRegion'] )
1647 if ( isset( $vals['CiUrlWork'] ) ) {
1648 $url = '<span class="url">'
1649 . $this->literal( $vals['CiUrlWork'] )
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.
1663 public static function getVisibleFields() {
1665 $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
1666 foreach ( $lines as $line ) {
1668 if ( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
1669 $fields[] = $matches[1];
1672 $fields = array_map( 'strtolower', $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
1684 public function fetchExtendedMetadata( File
$file ) {
1685 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
1687 // If revision deleted, exit immediately
1688 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1692 $cacheKey = $cache->makeKey(
1693 'getExtendedMetadata',
1694 $this->getLanguage()->getCode(),
1695 (int)$this->singleLang
,
1699 $cachedValue = $cache->get( $cacheKey );
1702 && $this->getHookRunner()->onValidateExtendedMetadataCache( $cachedValue['timestamp'], $file )
1704 $extendedMetadata = $cachedValue['data'];
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
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() );
1746 // This is modification time, which is close to "upload" time.
1748 'value' => $uploadDate,
1749 'source' => 'mediawiki-metadata',
1753 $title = $file->getTitle();
1755 $text = $title->getText();
1756 $pos = strrpos( $text, '.' );
1759 $name = substr( $text, 0, $pos );
1764 $fileMetadata['ObjectName'] = [
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
1783 protected function getExtendedMetadataFromHook( File
$file, array $extendedMetadata,
1786 $this->getHookRunner()->onGetExtendedMetadata(
1789 $this->getContext(),
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
1812 protected function resolveMultilangValue( $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'] );
1837 return reset( $value );
1840 // this should not happen; signal error
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
1853 protected function resolveMultivalueValue( $value ) {
1854 if ( !is_array( $value ) ) {
1857 if ( isset( $value['_type'] ) && $value['_type'] === 'lang' ) {
1858 // if this is a multilang array, process fields separately
1860 foreach ( $value as $k => $v ) {
1861 $newValue[$k] = $this->resolveMultivalueValue( $v );
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 );
1874 * Takes an array returned by the getExtendedMetadata* functions,
1875 * and resolves multi-language values in it.
1876 * @param array &$metadata
1879 protected function resolveMultilangMetadata( &$metadata ) {
1880 if ( !is_array( $metadata ) ) {
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
1896 protected function discardMultipleValues( &$metadata ) {
1897 if ( !is_array( $metadata ) ) {
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.
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 ) ) {
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;
1933 $arr[$sanitizedKey] = $arr[$key];
1934 unset( $arr[$key] );
1936 if ( is_array( $value ) ) {
1937 $this->sanitizeArrayForAPI( $value );
1942 // Handle API metadata keys (particularly "_type")
1943 $keys = array_filter( array_keys( $arr ), [ ApiResult
::class, 'isMetadataKey' ] );
1945 ApiResult
::setPreserveKeysList( $arr, $keys );
1950 * Turns a string into a valid API identifier.
1951 * @param string $key
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 === '' ) {
1965 // special case for an internal keyword
1966 } elseif ( $key === '_element' ) {
1974 * Returns a list of languages (first is best) to use when formatting multilang fields,
1975 * based on user and site preferences.
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;