* @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason, 2009 Brent Garber, 2010 Brian Wolff * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License * @see http://exif.org/Exif2-2.PDF The Exif 2.2 specification * @file */ /** * Format Image metadata values into a human readable form. * * Note lots of these messages use the prefix 'exif' even though * they may not be exif properties. For example 'exif-ImageDescription' * can be the Exif ImageDescription, or it could be the iptc-iim caption * property, or it could be the xmp dc:description property. This * is because these messages should be independent of how the data is * stored, sine the user doesn't care if the description is stored in xmp, * exif, etc only that its a description. (Additionally many of these properties * are merged together following the MWG standard, such that for example, * exif properties override XMP properties that mean the same thing if * there is a conflict). * * It should perhaps use a prefix like 'metadata' instead, but there * is already a large number of messages using the 'exif' prefix. * * @ingroup Media * @since 1.23 the class extends ContextSource and various formerly-public * internal methods are private */ class FormatMetadata extends ContextSource { /** * Only output a single language for multi-language fields * @var bool * @since 1.23 */ protected $singleLang = false; /** * Trigger only outputting single language for multilanguage fields * * @param bool $val * @since 1.23 */ public function setSingleLanguage( $val ) { $this->singleLang = $val; } /** * Numbers given by Exif user agents are often magical, that is they * should be replaced by a detailed explanation depending on their * value which most of the time are plain integers. This function * formats Exif (and other metadata) values into human readable form. * * This is the usual entry point for this class. * * @param array $tags The Exif data to format ( as returned by * Exif::getFilteredData() or BitmapMetadataHandler ) * @param bool|IContextSource $context Context to use (optional) * @return array */ public static function getFormattedData( $tags, $context = false ) { $obj = new FormatMetadata; if ( $context ) { $obj->setContext( $context ); } return $obj->makeFormattedData( $tags ); } /** * Numbers given by Exif user agents are often magical, that is they * should be replaced by a detailed explanation depending on their * value which most of the time are plain integers. This function * formats Exif (and other metadata) values into human readable form. * * @param array $tags The Exif data to format ( as returned by * Exif::getFilteredData() or BitmapMetadataHandler ) * @return array * @since 1.23 */ public function makeFormattedData( $tags ) { $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3; unset( $tags['ResolutionUnit'] ); foreach ( $tags as $tag => &$vals ) { // This seems ugly to wrap non-array's in an array just to unwrap again, // especially when most of the time it is not an array if ( !is_array( $tags[$tag] ) ) { $vals = array( $vals ); } // _type is a special value to say what array type if ( isset( $tags[$tag]['_type'] ) ) { $type = $tags[$tag]['_type']; unset( $vals['_type'] ); } else { $type = 'ul'; // default unordered list. } //This is done differently as the tag is an array. if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) { //hour min sec array $h = explode( '/', $vals[0] ); $m = explode( '/', $vals[1] ); $s = explode( '/', $vals[2] ); // this should already be validated // when loaded from file, but it could // come from a foreign repo, so be // paranoid. if ( !isset( $h[1] ) || !isset( $m[1] ) || !isset( $s[1] ) || $h[1] == 0 || $m[1] == 0 || $s[1] == 0 ) { continue; } $tags[$tag] = str_pad( intval( $h[0] / $h[1] ), 2, '0', STR_PAD_LEFT ) . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT ) . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT ); try { $time = wfTimestamp( TS_MW, '1971:01:01 ' . $tags[$tag] ); // the 1971:01:01 is just a placeholder, and not shown to user. if ( $time && intval( $time ) > 0 ) { $tags[$tag] = $this->getLanguage()->time( $time ); } } catch ( TimestampException $e ) { // This shouldn't happen, but we've seen bad formats // such as 4-digit seconds in the wild. // leave $tags[$tag] as-is } continue; } // The contact info is a multi-valued field // instead of the other props which are single // valued (mostly) so handle as a special case. if ( $tag === 'Contact' ) { $vals = $this->collapseContactInfo( $vals ); continue; } foreach ( $vals as &$val ) { switch ( $tag ) { case 'Compression': switch ( $val ) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 32773: case 32946: case 34712: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'PhotometricInterpretation': switch ( $val ) { case 2: case 6: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'Orientation': switch ( $val ) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'PlanarConfiguration': switch ( $val ) { case 1: case 2: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; // TODO: YCbCrSubSampling case 'YCbCrPositioning': switch ( $val ) { case 1: case 2: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'XResolution': case 'YResolution': switch ( $resolutionunit ) { case 2: $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) ); break; case 3: $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) ); break; default: /* If not recognized, display as is. */ break; } break; // TODO: YCbCrCoefficients #p27 (see annex E) case 'ExifVersion': case 'FlashpixVersion': $val = "$val" / 100; break; case 'ColorSpace': switch ( $val ) { case 1: case 65535: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'ComponentsConfiguration': switch ( $val ) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'DateTime': case 'DateTimeOriginal': case 'DateTimeDigitized': case 'DateTimeReleased': case 'DateTimeExpires': case 'GPSDateStamp': case 'dc-date': case 'DateTimeMetadata': if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) { $val = $this->msg( 'exif-unknowndate' )->text(); } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) { // Full date. $time = wfTimestamp( TS_MW, $val ); if ( $time && intval( $time ) > 0 ) { $val = $this->getLanguage()->timeanddate( $time ); } } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) { // No second field. Still format the same // since timeanddate doesn't include seconds anyways, // but second still available in api $time = wfTimestamp( TS_MW, $val . ':00' ); if ( $time && intval( $time ) > 0 ) { $val = $this->getLanguage()->timeanddate( $time ); } } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) { // If only the date but not the time is filled in. $time = wfTimestamp( TS_MW, substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' ); if ( $time && intval( $time ) > 0 ) { $val = $this->getLanguage()->date( $time ); } } // else it will just output $val without formatting it. break; case 'ExposureProgram': switch ( $val ) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'SubjectDistance': $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) ); break; case 'MeteringMode': switch ( $val ) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'LightSource': switch ( $val ) { case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 255: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'Flash': $flashDecode = array( 'fired' => $val & bindec( '00000001' ), 'return' => ( $val & bindec( '00000110' ) ) >> 1, 'mode' => ( $val & bindec( '00011000' ) ) >> 3, 'function' => ( $val & bindec( '00100000' ) ) >> 5, 'redeye' => ( $val & bindec( '01000000' ) ) >> 6, // 'reserved' => ($val & bindec( '10000000' )) >> 7, ); $flashMsgs = array(); # We do not need to handle unknown values since all are used. foreach ( $flashDecode as $subTag => $subValue ) { # We do not need any message for zeroed values. if ( $subTag != 'fired' && $subValue == 0 ) { continue; } $fullTag = $tag . '-' . $subTag; $flashMsgs[] = $this->exifMsg( $fullTag, $subValue ); } $val = $this->getLanguage()->commaList( $flashMsgs ); break; case 'FocalPlaneResolutionUnit': switch ( $val ) { case 2: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'SensingMethod': switch ( $val ) { case 1: case 2: case 3: case 4: case 5: case 7: case 8: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'FileSource': switch ( $val ) { case 3: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'SceneType': switch ( $val ) { case 1: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'CustomRendered': switch ( $val ) { case 0: case 1: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'ExposureMode': switch ( $val ) { case 0: case 1: case 2: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'WhiteBalance': switch ( $val ) { case 0: case 1: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'SceneCaptureType': switch ( $val ) { case 0: case 1: case 2: case 3: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'GainControl': switch ( $val ) { case 0: case 1: case 2: case 3: case 4: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'Contrast': switch ( $val ) { case 0: case 1: case 2: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'Saturation': switch ( $val ) { case 0: case 1: case 2: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'Sharpness': switch ( $val ) { case 0: case 1: case 2: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'SubjectDistanceRange': switch ( $val ) { case 0: case 1: case 2: case 3: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; //The GPS...Ref values are kept for compatibility, probably won't be reached. case 'GPSLatitudeRef': case 'GPSDestLatitudeRef': switch ( $val ) { case 'N': case 'S': $val = $this->exifMsg( 'GPSLatitude', $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'GPSLongitudeRef': case 'GPSDestLongitudeRef': switch ( $val ) { case 'E': case 'W': $val = $this->exifMsg( 'GPSLongitude', $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'GPSAltitude': if ( $val < 0 ) { $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) ); } else { $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) ); } break; case 'GPSStatus': switch ( $val ) { case 'A': case 'V': $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'GPSMeasureMode': switch ( $val ) { case 2: case 3: $val = $this->exifMsg( $tag, $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'GPSTrackRef': case 'GPSImgDirectionRef': case 'GPSDestBearingRef': switch ( $val ) { case 'T': case 'M': $val = $this->exifMsg( 'GPSDirection', $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'GPSLatitude': case 'GPSDestLatitude': $val = $this->formatCoords( $val, 'latitude' ); break; case 'GPSLongitude': case 'GPSDestLongitude': $val = $this->formatCoords( $val, 'longitude' ); break; case 'GPSSpeedRef': switch ( $val ) { case 'K': case 'M': case 'N': $val = $this->exifMsg( 'GPSSpeed', $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'GPSDestDistanceRef': switch ( $val ) { case 'K': case 'M': case 'N': $val = $this->exifMsg( 'GPSDestDistance', $val ); break; default: /* If not recognized, display as is. */ break; } break; case 'GPSDOP': // See https://en.wikipedia.org/wiki/Dilution_of_precision_(GPS) if ( $val <= 2 ) { $val = $this->exifMsg( $tag, 'excellent', $this->formatNum( $val ) ); } elseif ( $val <= 5 ) { $val = $this->exifMsg( $tag, 'good', $this->formatNum( $val ) ); } elseif ( $val <= 10 ) { $val = $this->exifMsg( $tag, 'moderate', $this->formatNum( $val ) ); } elseif ( $val <= 20 ) { $val = $this->exifMsg( $tag, 'fair', $this->formatNum( $val ) ); } else { $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) ); } break; // This is not in the Exif standard, just a special // case for our purposes which enables wikis to wikify // the make, model and software name to link to their articles. case 'Make': case 'Model': $val = $this->exifMsg( $tag, '', $val ); break; case 'Software': if ( is_array( $val ) ) { //if its a software, version array. $val = $this->msg( 'exif-software-version-value', $val[0], $val[1] )->text(); } else { $val = $this->exifMsg( $tag, '', $val ); } break; case 'ExposureTime': // Show the pretty fraction as well as decimal version $val = $this->msg( 'exif-exposuretime-format', $this->formatFraction( $val ), $this->formatNum( $val ) )->text(); break; case 'ISOSpeedRatings': // If its = 65535 that means its at the // limit of the size of Exif::short and // is really higher. if ( $val == '65535' ) { $val = $this->exifMsg( $tag, 'overflow' ); } else { $val = $this->formatNum( $val ); } break; case 'FNumber': $val = $this->msg( 'exif-fnumber-format', $this->formatNum( $val ) )->text(); break; case 'FocalLength': case 'FocalLengthIn35mmFilm': $val = $this->msg( 'exif-focallength-format', $this->formatNum( $val ) )->text(); break; case 'MaxApertureValue': if ( strpos( $val, '/' ) !== false ) { // need to expand this earlier to calculate fNumber list( $n, $d ) = explode( '/', $val ); if ( is_numeric( $n ) && is_numeric( $d ) ) { $val = $n / $d; } } if ( is_numeric( $val ) ) { $fNumber = pow( 2, $val / 2 ); if ( $fNumber !== false ) { $val = $this->msg( 'exif-maxaperturevalue-value', $this->formatNum( $val ), $this->formatNum( $fNumber, 2 ) )->text(); } } break; case 'iimCategory': switch ( strtolower( $val ) ) { // See pg 29 of IPTC photo // metadata standard. case 'ace': case 'clj': case 'dis': case 'fin': case 'edu': case 'evn': case 'hth': case 'hum': case 'lab': case 'lif': case 'pol': case 'rel': case 'sci': case 'soi': case 'spo': case 'war': case 'wea': $val = $this->exifMsg( 'iimcategory', $val ); } break; case 'SubjectNewsCode': // Essentially like iimCategory. // 8 (numeric) digit hierarchical // classification. We decode the // first 2 digits, which provide // a broad category. $val = $this->convertNewsCode( $val ); break; case 'Urgency': // 1-8 with 1 being highest, 5 normal // 0 is reserved, and 9 is 'user-defined'. $urgency = ''; if ( $val == 0 || $val == 9 ) { $urgency = 'other'; } elseif ( $val < 5 && $val > 1 ) { $urgency = 'high'; } elseif ( $val == 5 ) { $urgency = 'normal'; } elseif ( $val <= 8 && $val > 5 ) { $urgency = 'low'; } if ( $urgency !== '' ) { $val = $this->exifMsg( 'urgency', $urgency, $val ); } break; // Things that have a unit of pixels. case 'OriginalImageHeight': case 'OriginalImageWidth': case 'PixelXDimension': case 'PixelYDimension': case 'ImageWidth': case 'ImageLength': $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text(); break; // Do not transform fields with pure text. // For some languages the formatNum() // conversion results to wrong output like // foo,bar@example,com or foo٫bar@example٫com. // Also some 'numeric' things like Scene codes // are included here as we really don't want // commas inserted. case 'ImageDescription': case 'Artist': case 'Copyright': case 'RelatedSoundFile': case 'ImageUniqueID': case 'SpectralSensitivity': case 'GPSSatellites': case 'GPSVersionID': case 'GPSMapDatum': case 'Keywords': case 'WorldRegionDest': case 'CountryDest': case 'CountryCodeDest': case 'ProvinceOrStateDest': case 'CityDest': case 'SublocationDest': case 'WorldRegionCreated': case 'CountryCreated': case 'CountryCodeCreated': case 'ProvinceOrStateCreated': case 'CityCreated': case 'SublocationCreated': case 'ObjectName': case 'SpecialInstructions': case 'Headline': case 'Credit': case 'Source': case 'EditStatus': case 'FixtureIdentifier': case 'LocationDest': case 'LocationDestCode': case 'Writer': case 'JPEGFileComment': case 'iimSupplementalCategory': case 'OriginalTransmissionRef': case 'Identifier': case 'dc-contributor': case 'dc-coverage': case 'dc-publisher': case 'dc-relation': case 'dc-rights': case 'dc-source': case 'dc-type': case 'Lens': case 'SerialNumber': case 'CameraOwnerName': case 'Label': case 'Nickname': case 'RightsCertificate': case 'CopyrightOwner': case 'UsageTerms': case 'WebStatement': case 'OriginalDocumentID': case 'LicenseUrl': case 'MorePermissionsUrl': case 'AttributionUrl': case 'PreferredAttributionName': case 'PNGFileComment': case 'Disclaimer': case 'ContentWarning': case 'GIFFileComment': case 'SceneCode': case 'IntellectualGenre': case 'Event': case 'OrginisationInImage': case 'PersonInImage': $val = htmlspecialchars( $val ); break; case 'ObjectCycle': switch ( $val ) { case 'a': case 'p': case 'b': $val = $this->exifMsg( $tag, $val ); break; default: $val = htmlspecialchars( $val ); break; } break; case 'Copyrighted': switch ( $val ) { case 'True': case 'False': $val = $this->exifMsg( $tag, $val ); break; } break; case 'Rating': if ( $val == '-1' ) { $val = $this->exifMsg( $tag, 'rejected' ); } else { $val = $this->formatNum( $val ); } break; case 'LanguageCode': $lang = Language::fetchLanguageName( strtolower( $val ), $this->getLanguage()->getCode() ); if ( $lang ) { $val = htmlspecialchars( $lang ); } else { $val = htmlspecialchars( $val ); } break; default: $val = $this->formatNum( $val ); break; } } // End formatting values, start flattening arrays. $vals = $this->flattenArrayReal( $vals, $type ); } return $tags; } /** * Flatten an array, using the content language for any messages. * * @param array $vals Array of values * @param string $type Type of array (either lang, ul, ol). * lang = language assoc array with keys being the lang code * ul = unordered list, ol = ordered list * type can also come from the '_type' member of $vals. * @param bool $noHtml If to avoid returning anything resembling HTML. * (Ugly hack for backwards compatibility with old MediaWiki). * @param bool|IContextSource $context * @return string Single value (in wiki-syntax). * @since 1.23 */ public static function flattenArrayContentLang( $vals, $type = 'ul', $noHtml = false, $context = false ) { global $wgContLang; $obj = new FormatMetadata; if ( $context ) { $obj->setContext( $context ); } $context = new DerivativeContext( $obj->getContext() ); $context->setLanguage( $wgContLang ); $obj->setContext( $context ); return $obj->flattenArrayReal( $vals, $type, $noHtml ); } /** * A function to collapse multivalued tags into a single value. * This turns an array of (for example) authors into a bulleted list. * * This is public on the basis it might be useful outside of this class. * * @param array $vals Array of values * @param string $type Type of array (either lang, ul, ol). * lang = language assoc array with keys being the lang code * ul = unordered list, ol = ordered list * type can also come from the '_type' member of $vals. * @param bool $noHtml If to avoid returning anything resembling HTML. * (Ugly hack for backwards compatibility with old mediawiki). * @return string Single value (in wiki-syntax). * @since 1.23 */ public function flattenArrayReal( $vals, $type = 'ul', $noHtml = false ) { if ( !is_array( $vals ) ) { return $vals; // do nothing if not an array; } if ( isset( $vals['_type'] ) ) { $type = $vals['_type']; unset( $vals['_type'] ); } if ( !is_array( $vals ) ) { return $vals; // do nothing if not an array; } elseif ( count( $vals ) === 1 && $type !== 'lang' ) { return $vals[0]; } elseif ( count( $vals ) === 0 ) { wfDebug( __METHOD__ . " metadata array with 0 elements!\n" ); return ""; // paranoia. This should never happen } else { /* @todo FIXME: This should hide some of the list entries if there are * say more than four. Especially if a field is translated into 20 * languages, we don't want to show them all by default */ switch ( $type ) { case 'lang': // Display default, followed by ContLang, // followed by the rest in no particular // order. // Todo: hide some items if really long list. $content = ''; $priorityLanguages = $this->getPriorityLanguages(); $defaultItem = false; $defaultLang = false; // If default is set, save it for later, // as we don't know if it's equal to // one of the lang codes. (In xmp // you specify the language for a // default property by having both // a default prop, and one in the language // that are identical) if ( isset( $vals['x-default'] ) ) { $defaultItem = $vals['x-default']; unset( $vals['x-default'] ); } foreach ( $priorityLanguages as $pLang ) { if ( isset( $vals[$pLang] ) ) { $isDefault = false; if ( $vals[$pLang] === $defaultItem ) { $defaultItem = false; $isDefault = true; } $content .= $this->langItem( $vals[$pLang], $pLang, $isDefault, $noHtml ); unset( $vals[$pLang] ); if ( $this->singleLang ) { return Html::rawElement( 'span', array( 'lang' => $pLang ), $vals[$pLang] ); } } } // Now do the rest. foreach ( $vals as $lang => $item ) { if ( $item === $defaultItem ) { $defaultLang = $lang; continue; } $content .= $this->langItem( $item, $lang, false, $noHtml ); if ( $this->singleLang ) { return Html::rawElement( 'span', array( 'lang' => $lang ), $item ); } } if ( $defaultItem !== false ) { $content = $this->langItem( $defaultItem, $defaultLang, true, $noHtml ) . $content; if ( $this->singleLang ) { return $defaultItem; } } if ( $noHtml ) { return $content; } return '
'; case 'ol': if ( $noHtml ) { return "\n#" . implode( "\n#", $vals ); } return "