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
27 use MediaWiki\MediaWikiServices
;
28 use Wikimedia\Timestamp\TimestampException
;
31 * Format Image metadata values into a human readable form.
33 * Note lots of these messages use the prefix 'exif' even though
34 * they may not be exif properties. For example 'exif-ImageDescription'
35 * can be the Exif ImageDescription, or it could be the iptc-iim caption
36 * property, or it could be the xmp dc:description property. This
37 * is because these messages should be independent of how the data is
38 * stored, sine the user doesn't care if the description is stored in xmp,
39 * exif, etc only that its a description. (Additionally many of these properties
40 * are merged together following the MWG standard, such that for example,
41 * exif properties override XMP properties that mean the same thing if
42 * there is a conflict).
44 * It should perhaps use a prefix like 'metadata' instead, but there
45 * is already a large number of messages using the 'exif' prefix.
48 * @since 1.23 the class extends ContextSource and various formerly-public
49 * internal methods are private
51 class FormatMetadata
extends ContextSource
{
53 * Only output a single language for multi-language fields
57 protected $singleLang = false;
60 * Trigger only outputting single language for multilanguage fields
65 public function setSingleLanguage( $val ) {
66 $this->singleLang
= $val;
70 * Numbers given by Exif user agents are often magical, that is they
71 * should be replaced by a detailed explanation depending on their
72 * value which most of the time are plain integers. This function
73 * formats Exif (and other metadata) values into human readable form.
75 * This is the usual entry point for this class.
77 * @param array $tags The Exif data to format ( as returned by
78 * Exif::getFilteredData() or BitmapMetadataHandler )
79 * @param bool|IContextSource $context Context to use (optional)
82 public static function getFormattedData( $tags, $context = false ) {
83 $obj = new FormatMetadata
;
85 $obj->setContext( $context );
88 return $obj->makeFormattedData( $tags );
92 * Numbers given by Exif user agents are often magical, that is they
93 * should be replaced by a detailed explanation depending on their
94 * value which most of the time are plain integers. This function
95 * formats Exif (and other metadata) values into human readable form.
97 * @param array $tags The Exif data to format ( as returned by
98 * Exif::getFilteredData() or BitmapMetadataHandler )
102 public function makeFormattedData( $tags ) {
103 $resolutionunit = !isset( $tags['ResolutionUnit'] ) ||
$tags['ResolutionUnit'] == 2 ?
2 : 3;
104 unset( $tags['ResolutionUnit'] );
106 foreach ( $tags as $tag => &$vals ) {
107 // This seems ugly to wrap non-array's in an array just to unwrap again,
108 // especially when most of the time it is not an array
109 if ( !is_array( $tags[$tag] ) ) {
113 // _type is a special value to say what array type
114 if ( isset( $tags[$tag]['_type'] ) ) {
115 $type = $tags[$tag]['_type'];
116 unset( $vals['_type'] );
118 $type = 'ul'; // default unordered list.
121 // This is done differently as the tag is an array.
122 if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) {
123 // hour min sec array
125 $h = explode( '/', $vals[0] );
126 $m = explode( '/', $vals[1] );
127 $s = explode( '/', $vals[2] );
129 // this should already be validated
130 // when loaded from file, but it could
131 // come from a foreign repo, so be
142 $tags[$tag] = str_pad( intval( $h[0] / $h[1] ), 2, '0', STR_PAD_LEFT
)
143 . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT
)
144 . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT
);
147 $time = wfTimestamp( TS_MW
, '1971:01:01 ' . $tags[$tag] );
148 // the 1971:01:01 is just a placeholder, and not shown to user.
149 if ( $time && intval( $time ) > 0 ) {
150 $tags[$tag] = $this->getLanguage()->time( $time );
152 } catch ( TimestampException
$e ) {
153 // This shouldn't happen, but we've seen bad formats
154 // such as 4-digit seconds in the wild.
155 // leave $tags[$tag] as-is
160 // The contact info is a multi-valued field
161 // instead of the other props which are single
162 // valued (mostly) so handle as a special case.
163 if ( $tag === 'Contact' ) {
164 $vals = $this->collapseContactInfo( $vals );
168 foreach ( $vals as &$val ) {
183 $val = $this->exifMsg( $tag, $val );
186 /* If not recognized, display as is. */
191 case 'PhotometricInterpretation':
205 $val = $this->exifMsg( $tag, $val );
208 /* If not recognized, display as is. */
223 $val = $this->exifMsg( $tag, $val );
226 /* If not recognized, display as is. */
231 case 'PlanarConfiguration':
235 $val = $this->exifMsg( $tag, $val );
238 /* If not recognized, display as is. */
243 // TODO: YCbCrSubSampling
244 case 'YCbCrPositioning':
248 $val = $this->exifMsg( $tag, $val );
251 /* If not recognized, display as is. */
258 switch ( $resolutionunit ) {
260 $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) );
263 $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) );
266 /* If not recognized, display as is. */
271 // TODO: YCbCrCoefficients #p27 (see annex E)
273 case 'FlashpixVersion':
281 $val = $this->exifMsg( $tag, $val );
284 /* If not recognized, display as is. */
289 case 'ComponentsConfiguration':
298 $val = $this->exifMsg( $tag, $val );
301 /* If not recognized, display as is. */
307 case 'DateTimeOriginal':
308 case 'DateTimeDigitized':
309 case 'DateTimeReleased':
310 case 'DateTimeExpires':
313 case 'DateTimeMetadata':
314 if ( $val == '0000:00:00 00:00:00' ||
$val == ' : : : : ' ) {
315 $val = $this->msg( 'exif-unknowndate' )->text();
316 } elseif ( preg_match(
317 '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D',
321 $time = wfTimestamp( TS_MW
, $val );
322 if ( $time && intval( $time ) > 0 ) {
323 $val = $this->getLanguage()->timeanddate( $time );
325 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
326 // No second field. Still format the same
327 // since timeanddate doesn't include seconds anyways,
328 // but second still available in api
329 $time = wfTimestamp( TS_MW
, $val . ':00' );
330 if ( $time && intval( $time ) > 0 ) {
331 $val = $this->getLanguage()->timeanddate( $time );
333 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
334 // If only the date but not the time is filled in.
335 $time = wfTimestamp( TS_MW
, substr( $val, 0, 4 )
336 . substr( $val, 5, 2 )
337 . substr( $val, 8, 2 )
339 if ( $time && intval( $time ) > 0 ) {
340 $val = $this->getLanguage()->date( $time );
343 // else it will just output $val without formatting it.
346 case 'ExposureProgram':
357 $val = $this->exifMsg( $tag, $val );
360 /* If not recognized, display as is. */
365 case 'SubjectDistance':
366 $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) );
380 $val = $this->exifMsg( $tag, $val );
383 /* If not recognized, display as is. */
411 $val = $this->exifMsg( $tag, $val );
414 /* If not recognized, display as is. */
421 'fired' => $val & 0b00000001
,
422 'return' => ( $val & 0b00000110
) >> 1,
423 'mode' => ( $val & 0b00011000
) >> 3,
424 'function' => ( $val & 0b00100000
) >> 5,
425 'redeye' => ( $val & 0b01000000
) >> 6,
426 // 'reserved' => ( $val & 0b10000000 ) >> 7,
429 # We do not need to handle unknown values since all are used.
430 foreach ( $flashDecode as $subTag => $subValue ) {
431 # We do not need any message for zeroed values.
432 if ( $subTag != 'fired' && $subValue == 0 ) {
435 $fullTag = $tag . '-' . $subTag;
436 $flashMsgs[] = $this->exifMsg( $fullTag, $subValue );
438 $val = $this->getLanguage()->commaList( $flashMsgs );
441 case 'FocalPlaneResolutionUnit':
444 $val = $this->exifMsg( $tag, $val );
447 /* If not recognized, display as is. */
452 case 'SensingMethod':
461 $val = $this->exifMsg( $tag, $val );
464 /* If not recognized, display as is. */
472 $val = $this->exifMsg( $tag, $val );
475 /* If not recognized, display as is. */
483 $val = $this->exifMsg( $tag, $val );
486 /* If not recognized, display as is. */
491 case 'CustomRendered':
495 $val = $this->exifMsg( $tag, $val );
498 /* If not recognized, display as is. */
508 $val = $this->exifMsg( $tag, $val );
511 /* If not recognized, display as is. */
520 $val = $this->exifMsg( $tag, $val );
523 /* If not recognized, display as is. */
528 case 'SceneCaptureType':
534 $val = $this->exifMsg( $tag, $val );
537 /* If not recognized, display as is. */
549 $val = $this->exifMsg( $tag, $val );
552 /* If not recognized, display as is. */
562 $val = $this->exifMsg( $tag, $val );
565 /* If not recognized, display as is. */
575 $val = $this->exifMsg( $tag, $val );
578 /* If not recognized, display as is. */
588 $val = $this->exifMsg( $tag, $val );
591 /* If not recognized, display as is. */
596 case 'SubjectDistanceRange':
602 $val = $this->exifMsg( $tag, $val );
605 /* If not recognized, display as is. */
610 // The GPS...Ref values are kept for compatibility, probably won't be reached.
611 case 'GPSLatitudeRef':
612 case 'GPSDestLatitudeRef':
616 $val = $this->exifMsg( 'GPSLatitude', $val );
619 /* If not recognized, display as is. */
624 case 'GPSLongitudeRef':
625 case 'GPSDestLongitudeRef':
629 $val = $this->exifMsg( 'GPSLongitude', $val );
632 /* If not recognized, display as is. */
639 $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) );
641 $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) );
649 $val = $this->exifMsg( $tag, $val );
652 /* If not recognized, display as is. */
657 case 'GPSMeasureMode':
661 $val = $this->exifMsg( $tag, $val );
664 /* If not recognized, display as is. */
670 case 'GPSImgDirectionRef':
671 case 'GPSDestBearingRef':
675 $val = $this->exifMsg( 'GPSDirection', $val );
678 /* If not recognized, display as is. */
684 case 'GPSDestLatitude':
685 $val = $this->formatCoords( $val, 'latitude' );
688 case 'GPSDestLongitude':
689 $val = $this->formatCoords( $val, 'longitude' );
697 $val = $this->exifMsg( 'GPSSpeed', $val );
700 /* If not recognized, display as is. */
705 case 'GPSDestDistanceRef':
710 $val = $this->exifMsg( 'GPSDestDistance', $val );
713 /* If not recognized, display as is. */
719 // See https://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
721 $val = $this->exifMsg( $tag, 'excellent', $this->formatNum( $val ) );
722 } elseif ( $val <= 5 ) {
723 $val = $this->exifMsg( $tag, 'good', $this->formatNum( $val ) );
724 } elseif ( $val <= 10 ) {
725 $val = $this->exifMsg( $tag, 'moderate', $this->formatNum( $val ) );
726 } elseif ( $val <= 20 ) {
727 $val = $this->exifMsg( $tag, 'fair', $this->formatNum( $val ) );
729 $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) );
733 // This is not in the Exif standard, just a special
734 // case for our purposes which enables wikis to wikify
735 // the make, model and software name to link to their articles.
738 $val = $this->exifMsg( $tag, '', $val );
742 if ( is_array( $val ) ) {
743 if ( count( $val ) > 1 ) {
744 // if its a software, version array.
745 $val = $this->msg( 'exif-software-version-value', $val[0], $val[1] )->text();
747 // https://phabricator.wikimedia.org/T178130
748 $val = $this->exifMsg( $tag, '', $val[0] );
751 $val = $this->exifMsg( $tag, '', $val );
756 // Show the pretty fraction as well as decimal version
757 $val = $this->msg( 'exif-exposuretime-format',
758 $this->formatFraction( $val ), $this->formatNum( $val ) )->text();
760 case 'ISOSpeedRatings':
761 // If its = 65535 that means its at the
762 // limit of the size of Exif::short and
764 if ( $val == '65535' ) {
765 $val = $this->exifMsg( $tag, 'overflow' );
767 $val = $this->formatNum( $val );
771 $val = $this->msg( 'exif-fnumber-format',
772 $this->formatNum( $val ) )->text();
776 case 'FocalLengthIn35mmFilm':
777 $val = $this->msg( 'exif-focallength-format',
778 $this->formatNum( $val ) )->text();
781 case 'MaxApertureValue':
782 if ( strpos( $val, '/' ) !== false ) {
783 // need to expand this earlier to calculate fNumber
784 list( $n, $d ) = explode( '/', $val );
785 if ( is_numeric( $n ) && is_numeric( $d ) ) {
789 if ( is_numeric( $val ) ) {
790 $fNumber = 2 ** ( $val / 2 );
791 if ( $fNumber !== false ) {
792 $val = $this->msg( 'exif-maxaperturevalue-value',
793 $this->formatNum( $val ),
794 $this->formatNum( $fNumber, 2 )
801 switch ( strtolower( $val ) ) {
802 // See pg 29 of IPTC photo
803 // metadata standard.
821 $val = $this->exifMsg(
827 case 'SubjectNewsCode':
828 // Essentially like iimCategory.
829 // 8 (numeric) digit hierarchical
830 // classification. We decode the
831 // first 2 digits, which provide
833 $val = $this->convertNewsCode( $val );
836 // 1-8 with 1 being highest, 5 normal
837 // 0 is reserved, and 9 is 'user-defined'.
839 if ( $val == 0 ||
$val == 9 ) {
841 } elseif ( $val < 5 && $val > 1 ) {
843 } elseif ( $val == 5 ) {
845 } elseif ( $val <= 8 && $val > 5 ) {
849 if ( $urgency !== '' ) {
850 $val = $this->exifMsg( 'urgency',
856 // Things that have a unit of pixels.
857 case 'OriginalImageHeight':
858 case 'OriginalImageWidth':
859 case 'PixelXDimension':
860 case 'PixelYDimension':
863 $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text();
866 // Do not transform fields with pure text.
867 // For some languages the formatNum()
868 // conversion results to wrong output like
869 // foo,bar@example,com or foo٫bar@example٫com.
870 // Also some 'numeric' things like Scene codes
871 // are included here as we really don't want
873 case 'ImageDescription':
877 case 'RelatedSoundFile':
878 case 'ImageUniqueID':
879 case 'SpectralSensitivity':
880 case 'GPSSatellites':
884 case 'WorldRegionDest':
886 case 'CountryCodeDest':
887 case 'ProvinceOrStateDest':
889 case 'SublocationDest':
890 case 'WorldRegionCreated':
891 case 'CountryCreated':
892 case 'CountryCodeCreated':
893 case 'ProvinceOrStateCreated':
895 case 'SublocationCreated':
897 case 'SpecialInstructions':
902 case 'FixtureIdentifier':
904 case 'LocationDestCode':
906 case 'JPEGFileComment':
907 case 'iimSupplementalCategory':
908 case 'OriginalTransmissionRef':
910 case 'dc-contributor':
919 case 'CameraOwnerName':
922 case 'RightsCertificate':
923 case 'CopyrightOwner':
926 case 'OriginalDocumentID':
928 case 'MorePermissionsUrl':
929 case 'AttributionUrl':
930 case 'PreferredAttributionName':
931 case 'PNGFileComment':
933 case 'ContentWarning':
934 case 'GIFFileComment':
936 case 'IntellectualGenre':
938 case 'OrginisationInImage':
939 case 'PersonInImage':
941 $val = htmlspecialchars( $val );
949 $val = $this->exifMsg( $tag, $val );
952 $val = htmlspecialchars( $val );
960 $val = $this->exifMsg( $tag, $val );
965 if ( $val == '-1' ) {
966 $val = $this->exifMsg( $tag, 'rejected' );
968 $val = $this->formatNum( $val );
973 $lang = Language
::fetchLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
975 $val = htmlspecialchars( $lang );
977 $val = htmlspecialchars( $val );
982 $val = $this->formatNum( $val );
986 // End formatting values, start flattening arrays.
987 $vals = $this->flattenArrayReal( $vals, $type );
994 * Flatten an array, using the content language for any messages.
996 * @param array $vals Array of values
997 * @param string $type Type of array (either lang, ul, ol).
998 * lang = language assoc array with keys being the lang code
999 * ul = unordered list, ol = ordered list
1000 * type can also come from the '_type' member of $vals.
1001 * @param bool $noHtml If to avoid returning anything resembling HTML.
1002 * (Ugly hack for backwards compatibility with old MediaWiki).
1003 * @param bool|IContextSource $context
1004 * @return string Single value (in wiki-syntax).
1007 public static function flattenArrayContentLang( $vals, $type = 'ul',
1008 $noHtml = false, $context = false
1011 $obj = new FormatMetadata
;
1013 $obj->setContext( $context );
1015 $context = new DerivativeContext( $obj->getContext() );
1016 $context->setLanguage( $wgContLang );
1017 $obj->setContext( $context );
1019 return $obj->flattenArrayReal( $vals, $type, $noHtml );
1023 * A function to collapse multivalued tags into a single value.
1024 * This turns an array of (for example) authors into a bulleted list.
1026 * This is public on the basis it might be useful outside of this class.
1028 * @param array $vals Array of values
1029 * @param string $type Type of array (either lang, ul, ol).
1030 * lang = language assoc array with keys being the lang code
1031 * ul = unordered list, ol = ordered list
1032 * type can also come from the '_type' member of $vals.
1033 * @param bool $noHtml If to avoid returning anything resembling HTML.
1034 * (Ugly hack for backwards compatibility with old mediawiki).
1035 * @return string Single value (in wiki-syntax).
1038 public function flattenArrayReal( $vals, $type = 'ul', $noHtml = false ) {
1039 if ( !is_array( $vals ) ) {
1040 return $vals; // do nothing if not an array;
1043 if ( isset( $vals['_type'] ) ) {
1044 $type = $vals['_type'];
1045 unset( $vals['_type'] );
1048 if ( !is_array( $vals ) ) {
1049 return $vals; // do nothing if not an array;
1050 } elseif ( count( $vals ) === 1 && $type !== 'lang' && isset( $vals[0] ) ) {
1052 } elseif ( count( $vals ) === 0 ) {
1053 wfDebug( __METHOD__
. " metadata array with 0 elements!\n" );
1055 return ""; // paranoia. This should never happen
1057 /* @todo FIXME: This should hide some of the list entries if there are
1058 * say more than four. Especially if a field is translated into 20
1059 * languages, we don't want to show them all by default
1063 // Display default, followed by ContLang,
1064 // followed by the rest in no particular
1067 // Todo: hide some items if really long list.
1071 $priorityLanguages = $this->getPriorityLanguages();
1072 $defaultItem = false;
1073 $defaultLang = false;
1075 // If default is set, save it for later,
1076 // as we don't know if it's equal to
1077 // one of the lang codes. (In xmp
1078 // you specify the language for a
1079 // default property by having both
1080 // a default prop, and one in the language
1081 // that are identical)
1082 if ( isset( $vals['x-default'] ) ) {
1083 $defaultItem = $vals['x-default'];
1084 unset( $vals['x-default'] );
1086 foreach ( $priorityLanguages as $pLang ) {
1087 if ( isset( $vals[$pLang] ) ) {
1089 if ( $vals[$pLang] === $defaultItem ) {
1090 $defaultItem = false;
1093 $content .= $this->langItem(
1094 $vals[$pLang], $pLang,
1095 $isDefault, $noHtml );
1097 unset( $vals[$pLang] );
1099 if ( $this->singleLang
) {
1100 return Html
::rawElement( 'span',
1101 [ 'lang' => $pLang ], $vals[$pLang] );
1107 foreach ( $vals as $lang => $item ) {
1108 if ( $item === $defaultItem ) {
1109 $defaultLang = $lang;
1112 $content .= $this->langItem( $item,
1113 $lang, false, $noHtml );
1114 if ( $this->singleLang
) {
1115 return Html
::rawElement( 'span',
1116 [ 'lang' => $lang ], $item );
1119 if ( $defaultItem !== false ) {
1120 $content = $this->langItem( $defaultItem,
1121 $defaultLang, true, $noHtml ) .
1123 if ( $this->singleLang
) {
1124 return $defaultItem;
1131 return '<ul class="metadata-langlist">' .
1136 return "\n#" . implode( "\n#", $vals );
1139 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
1143 return "\n*" . implode( "\n*", $vals );
1146 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
1151 /** Helper function for creating lists of translations.
1153 * @param string $value Value (this is not escaped)
1154 * @param string $lang Lang code of item or false
1155 * @param bool $default If it is default value.
1156 * @param bool $noHtml If to avoid html (for back-compat)
1157 * @throws MWException
1158 * @return string Language item (Note: despite how this looks, this is
1159 * treated as wikitext, not as HTML).
1161 private function langItem( $value, $lang, $default = false, $noHtml = false ) {
1162 if ( $lang === false && $default === false ) {
1163 throw new MWException( '$lang and $default cannot both '
1168 $wrappedValue = $value;
1170 $wrappedValue = '<span class="mw-metadata-lang-value">'
1171 . $value . '</span>';
1174 if ( $lang === false ) {
1175 $msg = $this->msg( 'metadata-langitem-default', $wrappedValue );
1177 return $msg->text() . "\n\n";
1180 return '<li class="mw-metadata-lang-default">'
1185 $lowLang = strtolower( $lang );
1186 $langName = Language
::fetchLanguageName( $lowLang );
1187 if ( $langName === '' ) {
1188 // try just the base language name. (aka en-US -> en ).
1189 list( $langPrefix ) = explode( '-', $lowLang, 2 );
1190 $langName = Language
::fetchLanguageName( $langPrefix );
1191 if ( $langName === '' ) {
1196 // else we have a language specified
1198 $msg = $this->msg( 'metadata-langitem', $wrappedValue, $langName, $lang );
1200 return '*' . $msg->text();
1203 $item = '<li class="mw-metadata-lang-code-'
1206 $item .= ' mw-metadata-lang-default';
1208 $item .= '" lang="' . $lang . '">';
1209 $item .= $msg->text();
1216 * Convenience function for getFormattedData()
1218 * @param string $tag The tag name to pass on
1219 * @param string $val The value of the tag
1220 * @param string $arg An argument to pass ($1)
1221 * @param string $arg2 A 2nd argument to pass ($2)
1222 * @return string The text content of "exif-$tag-$val" message in lower case
1224 private function exifMsg( $tag, $val, $arg = null, $arg2 = null ) {
1227 if ( $val === '' ) {
1231 return $this->msg( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 )->text();
1235 * Format a number, convert numbers from fractions into floating point
1236 * numbers, joins arrays of numbers with commas.
1238 * @param mixed $num The value to format
1239 * @param float|int|bool $round Digits to round to or false.
1240 * @return mixed A floating point number or whatever we were fed
1242 private function formatNum( $num, $round = false ) {
1244 if ( is_array( $num ) ) {
1246 foreach ( $num as $number ) {
1247 $out[] = $this->formatNum( $number );
1250 return $this->getLanguage()->commaList( $out );
1252 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1254 $newNum = $m[1] / $m[2];
1255 if ( $round !== false ) {
1256 $newNum = round( $newNum, $round );
1262 return $this->getLanguage()->formatNum( $newNum );
1264 if ( is_numeric( $num ) && $round !== false ) {
1265 $num = round( $num, $round );
1268 return $this->getLanguage()->formatNum( $num );
1273 * Format a rational number, reducing fractions
1275 * @param mixed $num The value to format
1276 * @return mixed A floating point number or whatever we were fed
1278 private function formatFraction( $num ) {
1280 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1281 $numerator = intval( $m[1] );
1282 $denominator = intval( $m[2] );
1283 $gcd = $this->gcd( abs( $numerator ), $denominator );
1285 // 0 shouldn't happen! ;)
1286 return $this->formatNum( $numerator / $gcd ) . '/' . $this->formatNum( $denominator / $gcd );
1290 return $this->formatNum( $num );
1294 * Calculate the greatest common divisor of two integers.
1296 * @param int $a Numerator
1297 * @param int $b Denominator
1300 private function gcd( $a, $b ) {
1302 // https://en.wikipedia.org/wiki/Euclidean_algorithm
1303 // Recursive form would be:
1307 return gcd( $b, $a % $b );
1310 $remainder = $a %
$b;
1312 // tail recursion...
1321 * Fetch the human readable version of a news code.
1322 * A news code is an 8 digit code. The first two
1323 * digits are a general classification, so we just
1326 * Note, leading 0's are significant, so this is
1327 * a string, not an int.
1329 * @param string $val The 8 digit news code.
1330 * @return string The human readable form
1332 private function convertNewsCode( $val ) {
1333 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1334 // Not a valid news code.
1338 switch ( substr( $val, 0, 2 ) ) {
1391 if ( $cat !== '' ) {
1392 $catMsg = $this->exifMsg( 'iimcategory', $cat );
1393 $val = $this->exifMsg( 'subjectnewscode', '', $val, $catMsg );
1400 * Format a coordinate value, convert numbers from floating point
1401 * into degree minute second representation.
1403 * @param int $coord Degrees, minutes and seconds
1404 * @param string $type Latitude or longitude (for if its a NWS or E)
1405 * @return mixed A floating point number or whatever we were fed
1407 private function formatCoords( $coord, $type ) {
1411 if ( $type === 'latitude' ) {
1413 } elseif ( $type === 'longitude' ) {
1418 if ( $type === 'latitude' ) {
1420 } elseif ( $type === 'longitude' ) {
1425 $deg = floor( $nCoord );
1426 $min = floor( ( $nCoord - $deg ) * 60.0 );
1427 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1429 $deg = $this->formatNum( $deg );
1430 $min = $this->formatNum( $min );
1431 $sec = $this->formatNum( $sec );
1433 return $this->msg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord )->text();
1437 * Format the contact info field into a single value.
1439 * This function might be called from
1440 * JpegHandler::convertMetadataVersion which is why it is
1443 * @param array $vals Array with fields of the ContactInfo
1444 * struct defined in the IPTC4XMP spec. Or potentially
1445 * an array with one element that is a free form text
1446 * value from the older iptc iim 1:118 prop.
1447 * @return string HTML-ish looking wikitext
1448 * @since 1.23 no longer static
1450 public function collapseContactInfo( $vals ) {
1451 if ( !( isset( $vals['CiAdrExtadr'] )
1452 ||
isset( $vals['CiAdrCity'] )
1453 ||
isset( $vals['CiAdrCtry'] )
1454 ||
isset( $vals['CiEmailWork'] )
1455 ||
isset( $vals['CiTelWork'] )
1456 ||
isset( $vals['CiAdrPcode'] )
1457 ||
isset( $vals['CiAdrRegion'] )
1458 ||
isset( $vals['CiUrlWork'] )
1460 // We don't have any sub-properties
1461 // This could happen if its using old
1462 // iptc that just had this as a free-form
1464 // Note: We run this through htmlspecialchars
1465 // partially to be consistent, and partially
1466 // because people often insert >, etc into
1467 // the metadata which should not be interpreted
1468 // but we still want to auto-link urls.
1469 foreach ( $vals as &$val ) {
1470 $val = htmlspecialchars( $val );
1473 return $this->flattenArrayReal( $vals );
1475 // We have a real ContactInfo field.
1476 // Its unclear if all these fields have to be
1477 // set, so assume they do not.
1478 $url = $tel = $street = $city = $country = '';
1479 $email = $postal = $region = '';
1481 // Also note, some of the class names this uses
1482 // are similar to those used by hCard. This is
1483 // mostly because they're sensible names. This
1484 // does not (and does not attempt to) output
1485 // stuff in the hCard microformat. However it
1486 // might output in the adr microformat.
1488 if ( isset( $vals['CiAdrExtadr'] ) ) {
1489 // Todo: This can potentially be multi-line.
1490 // Need to check how that works in XMP.
1491 $street = '<span class="extended-address">'
1493 $vals['CiAdrExtadr'] )
1496 if ( isset( $vals['CiAdrCity'] ) ) {
1497 $city = '<span class="locality">'
1498 . htmlspecialchars( $vals['CiAdrCity'] )
1501 if ( isset( $vals['CiAdrCtry'] ) ) {
1502 $country = '<span class="country-name">'
1503 . htmlspecialchars( $vals['CiAdrCtry'] )
1506 if ( isset( $vals['CiEmailWork'] ) ) {
1508 // Have to split multiple emails at commas/new lines.
1509 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1510 foreach ( $splitEmails as $e1 ) {
1511 // Also split on comma
1512 foreach ( explode( ',', $e1 ) as $e2 ) {
1513 $finalEmail = trim( $e2 );
1514 if ( $finalEmail == ',' ||
$finalEmail == '' ) {
1517 if ( strpos( $finalEmail, '<' ) !== false ) {
1518 // Don't do fancy formatting to
1519 // "My name" <foo@bar.com> style stuff
1520 $emails[] = $finalEmail;
1522 $emails[] = '[mailto:'
1524 . ' <span class="email">'
1530 $email = implode( ', ', $emails );
1532 if ( isset( $vals['CiTelWork'] ) ) {
1533 $tel = '<span class="tel">'
1534 . htmlspecialchars( $vals['CiTelWork'] )
1537 if ( isset( $vals['CiAdrPcode'] ) ) {
1538 $postal = '<span class="postal-code">'
1540 $vals['CiAdrPcode'] )
1543 if ( isset( $vals['CiAdrRegion'] ) ) {
1544 // Note this is province/state.
1545 $region = '<span class="region">'
1547 $vals['CiAdrRegion'] )
1550 if ( isset( $vals['CiUrlWork'] ) ) {
1551 $url = '<span class="url">'
1552 . htmlspecialchars( $vals['CiUrlWork'] )
1556 return $this->msg( 'exif-contact-value', $email, $url,
1557 $street, $city, $region, $postal, $country,
1563 * Get a list of fields that are visible by default.
1568 public static function getVisibleFields() {
1570 $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
1571 foreach ( $lines as $line ) {
1573 if ( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
1574 $fields[] = $matches[1];
1577 $fields = array_map( 'strtolower', $fields );
1583 * Get an array of extended metadata. (See the imageinfo API for format.)
1585 * @param File $file File to use
1586 * @return array [<property name> => ['value' => <value>]], or [] on error
1589 public function fetchExtendedMetadata( File
$file ) {
1590 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
1592 // If revision deleted, exit immediately
1593 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1597 $cacheKey = $cache->makeKey(
1598 'getExtendedMetadata',
1599 $this->getLanguage()->getCode(),
1600 (int)$this->singleLang
,
1604 $cachedValue = $cache->get( $cacheKey );
1607 && Hooks
::run( 'ValidateExtendedMetadataCache', [ $cachedValue['timestamp'], $file ] )
1609 $extendedMetadata = $cachedValue['data'];
1611 $maxCacheTime = ( $file instanceof ForeignAPIFile
) ?
60 * 60 * 12 : 60 * 60 * 24 * 30;
1612 $fileMetadata = $this->getExtendedMetadataFromFile( $file );
1613 $extendedMetadata = $this->getExtendedMetadataFromHook( $file, $fileMetadata, $maxCacheTime );
1614 if ( $this->singleLang
) {
1615 $this->resolveMultilangMetadata( $extendedMetadata );
1617 $this->discardMultipleValues( $extendedMetadata );
1618 // Make sure the metadata won't break the API when an XML format is used.
1619 // This is an API-specific function so it would be cleaner to call it from
1620 // outside fetchExtendedMetadata, but this way we don't need to redo the
1621 // computation on a cache hit.
1622 $this->sanitizeArrayForAPI( $extendedMetadata );
1623 $valueToCache = [ 'data' => $extendedMetadata, 'timestamp' => wfTimestampNow() ];
1624 $cache->set( $cacheKey, $valueToCache, $maxCacheTime );
1627 return $extendedMetadata;
1631 * Get file-based metadata in standardized format.
1633 * Note that for a remote file, this might return metadata supplied by extensions.
1635 * @param File $file File to use
1636 * @return array [<property name> => ['value' => <value>]], or [] on error
1639 protected function getExtendedMetadataFromFile( File
$file ) {
1640 // If this is a remote file accessed via an API request, we already
1641 // have remote metadata so we just ignore any local one
1642 if ( $file instanceof ForeignAPIFile
) {
1643 // In case of error we pretend no metadata - this will get cached.
1644 // Might or might not be a good idea.
1645 return $file->getExtendedMetadata() ?
: [];
1648 $uploadDate = wfTimestamp( TS_ISO_8601
, $file->getTimestamp() );
1651 // This is modification time, which is close to "upload" time.
1653 'value' => $uploadDate,
1654 'source' => 'mediawiki-metadata',
1658 $title = $file->getTitle();
1660 $text = $title->getText();
1661 $pos = strrpos( $text, '.' );
1664 $name = substr( $text, 0, $pos );
1669 $fileMetadata['ObjectName'] = [
1671 'source' => 'mediawiki-metadata',
1675 return $fileMetadata;
1679 * Get additional metadata from hooks in standardized format.
1681 * @param File $file File to use
1682 * @param array $extendedMetadata
1683 * @param int &$maxCacheTime Hook handlers might use this parameter to override cache time
1685 * @return array [<property name> => ['value' => <value>]], or [] on error
1688 protected function getExtendedMetadataFromHook( File
$file, array $extendedMetadata,
1691 Hooks
::run( 'GetExtendedMetadata', [
1694 $this->getContext(),
1699 $visible = array_flip( self
::getVisibleFields() );
1700 foreach ( $extendedMetadata as $key => $value ) {
1701 if ( !isset( $visible[strtolower( $key )] ) ) {
1702 $extendedMetadata[$key]['hidden'] = '';
1706 return $extendedMetadata;
1710 * Turns an XMP-style multilang array into a single value.
1711 * If the value is not a multilang array, it is returned unchanged.
1712 * See mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
1713 * @param mixed $value
1714 * @return mixed Value in best language, null if there were no languages at all
1717 protected function resolveMultilangValue( $value ) {
1720 ||
!isset( $value['_type'] )
1721 ||
$value['_type'] != 'lang'
1723 return $value; // do nothing if not a multilang array
1726 // choose the language best matching user or site settings
1727 $priorityLanguages = $this->getPriorityLanguages();
1728 foreach ( $priorityLanguages as $lang ) {
1729 if ( isset( $value[$lang] ) ) {
1730 return $value[$lang];
1734 // otherwise go with the default language, if set
1735 if ( isset( $value['x-default'] ) ) {
1736 return $value['x-default'];
1739 // otherwise just return any one language
1740 unset( $value['_type'] );
1741 if ( !empty( $value ) ) {
1742 return reset( $value );
1745 // this should not happen; signal error
1750 * Turns an XMP-style multivalue array into a single value by dropping all but the first
1751 * value. If the value is not a multivalue array (or a multivalue array inside a multilang
1752 * array), it is returned unchanged.
1753 * See mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
1754 * @param mixed $value
1755 * @return mixed The value, or the first value if there were multiple ones
1758 protected function resolveMultivalueValue( $value ) {
1759 if ( !is_array( $value ) ) {
1761 } elseif ( isset( $value['_type'] ) && $value['_type'] === 'lang' ) {
1762 // if this is a multilang array, process fields separately
1764 foreach ( $value as $k => $v ) {
1765 $newValue[$k] = $this->resolveMultivalueValue( $v );
1768 } else { // _type is 'ul' or 'ol' or missing in which case it defaults to 'ul'
1769 $v = reset( $value );
1770 if ( key( $value ) === '_type' ) {
1771 $v = next( $value );
1778 * Takes an array returned by the getExtendedMetadata* functions,
1779 * and resolves multi-language values in it.
1780 * @param array &$metadata
1783 protected function resolveMultilangMetadata( &$metadata ) {
1784 if ( !is_array( $metadata ) ) {
1787 foreach ( $metadata as &$field ) {
1788 if ( isset( $field['value'] ) ) {
1789 $field['value'] = $this->resolveMultilangValue( $field['value'] );
1795 * Takes an array returned by the getExtendedMetadata* functions,
1796 * and turns all fields into single-valued ones by dropping extra values.
1797 * @param array &$metadata
1800 protected function discardMultipleValues( &$metadata ) {
1801 if ( !is_array( $metadata ) ) {
1804 foreach ( $metadata as $key => &$field ) {
1805 if ( $key === 'Software' ||
$key === 'Contact' ) {
1806 // we skip some fields which have composite values. They are not particularly interesting
1807 // and you can get them via the metadata / commonmetadata APIs anyway.
1810 if ( isset( $field['value'] ) ) {
1811 $field['value'] = $this->resolveMultivalueValue( $field['value'] );
1817 * Makes sure the given array is a valid API response fragment
1818 * @param array &$arr
1820 protected function sanitizeArrayForAPI( &$arr ) {
1821 if ( !is_array( $arr ) ) {
1826 foreach ( $arr as $key => &$value ) {
1827 $sanitizedKey = $this->sanitizeKeyForAPI( $key );
1828 if ( $sanitizedKey !== $key ) {
1829 if ( isset( $arr[$sanitizedKey] ) ) {
1830 // Make the sanitized keys hopefully unique.
1831 // To make it definitely unique would be too much effort, given that
1832 // sanitizing is only needed for misformatted metadata anyway, but
1833 // this at least covers the case when $arr is numeric.
1834 $sanitizedKey .= $counter;
1837 $arr[$sanitizedKey] = $arr[$key];
1838 unset( $arr[$key] );
1840 if ( is_array( $value ) ) {
1841 $this->sanitizeArrayForAPI( $value );
1845 // Handle API metadata keys (particularly "_type")
1846 $keys = array_filter( array_keys( $arr ), 'ApiResult::isMetadataKey' );
1848 ApiResult
::setPreserveKeysList( $arr, $keys );
1853 * Turns a string into a valid API identifier.
1854 * @param string $key
1858 protected function sanitizeKeyForAPI( $key ) {
1859 // drop all characters which are not valid in an XML tag name
1860 // a bunch of non-ASCII letters would be valid but probably won't
1861 // be used so we take the easy way
1862 $key = preg_replace( '/[^a-zA-z0-9_:.-]/', '', $key );
1863 // drop characters which are invalid at the first position
1864 $key = preg_replace( '/^[\d-.]+/', '', $key );
1870 // special case for an internal keyword
1871 if ( $key == '_element' ) {
1879 * Returns a list of languages (first is best) to use when formatting multilang fields,
1880 * based on user and site preferences.
1884 protected function getPriorityLanguages() {
1885 $priorityLanguages =
1886 Language
::getFallbacksIncludingSiteLanguage( $this->getLanguage()->getCode() );
1887 $priorityLanguages = array_merge(
1888 (array)$this->getLanguage()->getCode(),
1889 $priorityLanguages[0],
1890 $priorityLanguages[1]
1893 return $priorityLanguages;