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 http://www.gnu.org/copyleft/gpl.html GNU General Public License
24 * @see http://exif.org/Exif2-2.PDF The Exif 2.2 specification
29 * Format Image metadata values into a human readable form.
31 * Note lots of these messages use the prefix 'exif' even though
32 * they may not be exif properties. For example 'exif-ImageDescription'
33 * can be the Exif ImageDescription, or it could be the iptc-iim caption
34 * property, or it could be the xmp dc:description property. This
35 * is because these messages should be independent of how the data is
36 * stored, sine the user doesn't care if the description is stored in xmp,
37 * exif, etc only that its a description. (Additionally many of these properties
38 * are merged together following the MWG standard, such that for example,
39 * exif properties override XMP properties that mean the same thing if
40 * there is a conflict).
42 * It should perhaps use a prefix like 'metadata' instead, but there
43 * is already a large number of messages using the 'exif' prefix.
46 * @since 1.23 the class extends ContextSource and various formerly-public internal methods are private
48 class FormatMetadata
extends ContextSource
{
51 * Numbers given by Exif user agents are often magical, that is they
52 * should be replaced by a detailed explanation depending on their
53 * value which most of the time are plain integers. This function
54 * formats Exif (and other metadata) values into human readable form.
56 * This is the usual entry point for this class.
58 * @param array $tags the Exif data to format ( as returned by
59 * Exif::getFilteredData() or BitmapMetadataHandler )
60 * @param IContextSource $context Context to use (optional)
63 public static function getFormattedData( $tags, $context = false ) {
64 $obj = new FormatMetadata
;
66 $obj->setContext( $context );
68 return $obj->makeFormattedData( $tags );
72 * Numbers given by Exif user agents are often magical, that is they
73 * should be replaced by a detailed explanation depending on their
74 * value which most of the time are plain integers. This function
75 * formats Exif (and other metadata) values into human readable form.
77 * @param array $tags the Exif data to format ( as returned by
78 * Exif::getFilteredData() or BitmapMetadataHandler )
82 public function makeFormattedData( $tags ) {
83 $resolutionunit = !isset( $tags['ResolutionUnit'] ) ||
$tags['ResolutionUnit'] == 2 ?
2 : 3;
84 unset( $tags['ResolutionUnit'] );
86 foreach ( $tags as $tag => &$vals ) {
88 // This seems ugly to wrap non-array's in an array just to unwrap again,
89 // especially when most of the time it is not an array
90 if ( !is_array( $tags[$tag] ) ) {
91 $vals = Array( $vals );
94 // _type is a special value to say what array type
95 if ( isset( $tags[$tag]['_type'] ) ) {
96 $type = $tags[$tag]['_type'];
97 unset( $vals['_type'] );
99 $type = 'ul'; // default unordered list.
102 //This is done differently as the tag is an array.
103 if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) {
106 $h = explode( '/', $vals[0] );
107 $m = explode( '/', $vals[1] );
108 $s = explode( '/', $vals[2] );
110 // this should already be validated
111 // when loaded from file, but it could
112 // come from a foreign repo, so be
123 $tags[$tag] = str_pad( intval( $h[0] / $h[1] ), 2, '0', STR_PAD_LEFT
)
124 . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT
)
125 . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT
);
128 $time = wfTimestamp( TS_MW
, '1971:01:01 ' . $tags[$tag] );
129 // the 1971:01:01 is just a placeholder, and not shown to user.
130 if ( $time && intval( $time ) > 0 ) {
131 $tags[$tag] = $this->getLanguage()->time( $time );
133 } catch ( TimestampException
$e ) {
134 // This shouldn't happen, but we've seen bad formats
135 // such as 4-digit seconds in the wild.
136 // leave $tags[$tag] as-is
141 // The contact info is a multi-valued field
142 // instead of the other props which are single
143 // valued (mostly) so handle as a special case.
144 if ( $tag === 'Contact' ) {
145 $vals = $this->collapseContactInfo( $vals );
149 foreach ( $vals as &$val ) {
154 case 1: case 2: case 3: case 4:
155 case 5: case 6: case 7: case 8:
156 case 32773: case 32946: case 34712:
157 $val = $this->exifMsg( $tag, $val );
160 /* If not recognized, display as is. */
165 case 'PhotometricInterpretation':
168 $val = $this->exifMsg( $tag, $val );
171 /* If not recognized, display as is. */
178 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
179 $val = $this->exifMsg( $tag, $val );
182 /* If not recognized, display as is. */
187 case 'PlanarConfiguration':
190 $val = $this->exifMsg( $tag, $val );
193 /* If not recognized, display as is. */
198 // TODO: YCbCrSubSampling
199 case 'YCbCrPositioning':
203 $val = $this->exifMsg( $tag, $val );
206 /* If not recognized, display as is. */
213 switch ( $resolutionunit ) {
215 $val = $this->exifMsg( 'XYResolution', 'i', $this->formatNum( $val ) );
218 $val = $this->exifMsg( 'XYResolution', 'c', $this->formatNum( $val ) );
221 /* If not recognized, display as is. */
226 // TODO: YCbCrCoefficients #p27 (see annex E)
227 case 'ExifVersion': case 'FlashpixVersion':
234 $val = $this->exifMsg( $tag, $val );
237 /* If not recognized, display as is. */
242 case 'ComponentsConfiguration':
244 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
245 $val = $this->exifMsg( $tag, $val );
248 /* If not recognized, display as is. */
254 case 'DateTimeOriginal':
255 case 'DateTimeDigitized':
256 case 'DateTimeReleased':
257 case 'DateTimeExpires':
260 case 'DateTimeMetadata':
261 if ( $val == '0000:00:00 00:00:00' ||
$val == ' : : : : ' ) {
262 $val = $this->msg( 'exif-unknowndate' )->text();
263 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
265 $time = wfTimestamp( TS_MW
, $val );
266 if ( $time && intval( $time ) > 0 ) {
267 $val = $this->getLanguage()->timeanddate( $time );
269 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
270 // No second field. Still format the same
271 // since timeanddate doesn't include seconds anyways,
272 // but second still available in api
273 $time = wfTimestamp( TS_MW
, $val . ':00' );
274 if ( $time && intval( $time ) > 0 ) {
275 $val = $this->getLanguage()->timeanddate( $time );
277 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
278 // If only the date but not the time is filled in.
279 $time = wfTimestamp( TS_MW
, substr( $val, 0, 4 )
280 . substr( $val, 5, 2 )
281 . substr( $val, 8, 2 )
283 if ( $time && intval( $time ) > 0 ) {
284 $val = $this->getLanguage()->date( $time );
287 // else it will just output $val without formatting it.
290 case 'ExposureProgram':
292 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
293 $val = $this->exifMsg( $tag, $val );
296 /* If not recognized, display as is. */
301 case 'SubjectDistance':
302 $val = $this->exifMsg( $tag, '', $this->formatNum( $val ) );
307 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
308 $val = $this->exifMsg( $tag, $val );
311 /* If not recognized, display as is. */
318 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
319 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
320 case 21: case 22: case 23: case 24: case 255:
321 $val = $this->exifMsg( $tag, $val );
324 /* If not recognized, display as is. */
330 $flashDecode = array(
331 'fired' => $val & bindec( '00000001' ),
332 'return' => ( $val & bindec( '00000110' ) ) >> 1,
333 'mode' => ( $val & bindec( '00011000' ) ) >> 3,
334 'function' => ( $val & bindec( '00100000' ) ) >> 5,
335 'redeye' => ( $val & bindec( '01000000' ) ) >> 6,
336 // 'reserved' => ($val & bindec( '10000000' )) >> 7,
338 $flashMsgs = array();
339 # We do not need to handle unknown values since all are used.
340 foreach ( $flashDecode as $subTag => $subValue ) {
341 # We do not need any message for zeroed values.
342 if ( $subTag != 'fired' && $subValue == 0 ) {
345 $fullTag = $tag . '-' . $subTag;
346 $flashMsgs[] = $this->exifMsg( $fullTag, $subValue );
348 $val = $this->getLanguage()->commaList( $flashMsgs );
351 case 'FocalPlaneResolutionUnit':
354 $val = $this->exifMsg( $tag, $val );
357 /* If not recognized, display as is. */
362 case 'SensingMethod':
364 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
365 $val = $this->exifMsg( $tag, $val );
368 /* If not recognized, display as is. */
376 $val = $this->exifMsg( $tag, $val );
379 /* If not recognized, display as is. */
387 $val = $this->exifMsg( $tag, $val );
390 /* If not recognized, display as is. */
395 case 'CustomRendered':
398 $val = $this->exifMsg( $tag, $val );
401 /* If not recognized, display as is. */
408 case 0: case 1: case 2:
409 $val = $this->exifMsg( $tag, $val );
412 /* If not recognized, display as is. */
420 $val = $this->exifMsg( $tag, $val );
423 /* If not recognized, display as is. */
428 case 'SceneCaptureType':
430 case 0: case 1: case 2: case 3:
431 $val = $this->exifMsg( $tag, $val );
434 /* If not recognized, display as is. */
441 case 0: case 1: case 2: case 3: case 4:
442 $val = $this->exifMsg( $tag, $val );
445 /* If not recognized, display as is. */
452 case 0: case 1: case 2:
453 $val = $this->exifMsg( $tag, $val );
456 /* If not recognized, display as is. */
463 case 0: case 1: case 2:
464 $val = $this->exifMsg( $tag, $val );
467 /* If not recognized, display as is. */
474 case 0: case 1: case 2:
475 $val = $this->exifMsg( $tag, $val );
478 /* If not recognized, display as is. */
483 case 'SubjectDistanceRange':
485 case 0: case 1: case 2: case 3:
486 $val = $this->exifMsg( $tag, $val );
489 /* If not recognized, display as is. */
494 //The GPS...Ref values are kept for compatibility, probably won't be reached.
495 case 'GPSLatitudeRef':
496 case 'GPSDestLatitudeRef':
499 $val = $this->exifMsg( 'GPSLatitude', $val );
502 /* If not recognized, display as is. */
507 case 'GPSLongitudeRef':
508 case 'GPSDestLongitudeRef':
511 $val = $this->exifMsg( 'GPSLongitude', $val );
514 /* If not recognized, display as is. */
521 $val = $this->exifMsg( 'GPSAltitude', 'below-sealevel', $this->formatNum( -$val, 3 ) );
523 $val = $this->exifMsg( 'GPSAltitude', 'above-sealevel', $this->formatNum( $val, 3 ) );
530 $val = $this->exifMsg( $tag, $val );
533 /* If not recognized, display as is. */
538 case 'GPSMeasureMode':
541 $val = $this->exifMsg( $tag, $val );
544 /* If not recognized, display as is. */
550 case 'GPSImgDirectionRef':
551 case 'GPSDestBearingRef':
554 $val = $this->exifMsg( 'GPSDirection', $val );
557 /* If not recognized, display as is. */
563 case 'GPSDestLatitude':
564 $val = $this->formatCoords( $val, 'latitude' );
567 case 'GPSDestLongitude':
568 $val = $this->formatCoords( $val, 'longitude' );
573 case 'K': case 'M': case 'N':
574 $val = $this->exifMsg( 'GPSSpeed', $val );
577 /* If not recognized, display as is. */
582 case 'GPSDestDistanceRef':
584 case 'K': case 'M': case 'N':
585 $val = $this->exifMsg( 'GPSDestDistance', $val );
588 /* If not recognized, display as is. */
594 // See http://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
596 $val = $this->exifMsg( $tag, 'excellent', $this->formatNum( $val ) );
597 } elseif ( $val <= 5 ) {
598 $val = $this->exifMsg( $tag, 'good', $this->formatNum( $val ) );
599 } elseif ( $val <= 10 ) {
600 $val = $this->exifMsg( $tag, 'moderate', $this->formatNum( $val ) );
601 } elseif ( $val <= 20 ) {
602 $val = $this->exifMsg( $tag, 'fair', $this->formatNum( $val ) );
604 $val = $this->exifMsg( $tag, 'poor', $this->formatNum( $val ) );
608 // This is not in the Exif standard, just a special
609 // case for our purposes which enables wikis to wikify
610 // the make, model and software name to link to their articles.
613 $val = $this->exifMsg( $tag, '', $val );
617 if ( is_array( $val ) ) {
618 //if its a software, version array.
619 $val = $this->msg( 'exif-software-version-value', $val[0], $val[1] )->text();
621 $val = $this->exifMsg( $tag, '', $val );
626 // Show the pretty fraction as well as decimal version
627 $val = $this->msg( 'exif-exposuretime-format',
628 $this->formatFraction( $val ), $this->formatNum( $val ) )->text();
630 case 'ISOSpeedRatings':
631 // If its = 65535 that means its at the
632 // limit of the size of Exif::short and
634 if ( $val == '65535' ) {
635 $val = $this->exifMsg( $tag, 'overflow' );
637 $val = $this->formatNum( $val );
641 $val = $this->msg( 'exif-fnumber-format',
642 $this->formatNum( $val ) )->text();
645 case 'FocalLength': case 'FocalLengthIn35mmFilm':
646 $val = $this->msg( 'exif-focallength-format',
647 $this->formatNum( $val ) )->text();
650 case 'MaxApertureValue':
651 if ( strpos( $val, '/' ) !== false ) {
652 // need to expand this earlier to calculate fNumber
653 list( $n, $d ) = explode( '/', $val );
654 if ( is_numeric( $n ) && is_numeric( $d ) ) {
658 if ( is_numeric( $val ) ) {
659 $fNumber = pow( 2, $val / 2 );
660 if ( $fNumber !== false ) {
661 $val = $this->msg( 'exif-maxaperturevalue-value',
662 $this->formatNum( $val ),
663 $this->formatNum( $fNumber, 2 )
670 switch ( strtolower( $val ) ) {
671 // See pg 29 of IPTC photo
672 // metadata standard.
673 case 'ace': case 'clj':
674 case 'dis': case 'fin':
675 case 'edu': case 'evn':
676 case 'hth': case 'hum':
677 case 'lab': case 'lif':
678 case 'pol': case 'rel':
679 case 'sci': case 'soi':
680 case 'spo': case 'war':
682 $val = $this->exifMsg(
688 case 'SubjectNewsCode':
689 // Essentially like iimCategory.
690 // 8 (numeric) digit hierarchical
691 // classification. We decode the
692 // first 2 digits, which provide
694 $val = $this->convertNewsCode( $val );
697 // 1-8 with 1 being highest, 5 normal
698 // 0 is reserved, and 9 is 'user-defined'.
700 if ( $val == 0 ||
$val == 9 ) {
702 } elseif ( $val < 5 && $val > 1 ) {
704 } elseif ( $val == 5 ) {
706 } elseif ( $val <= 8 && $val > 5 ) {
710 if ( $urgency !== '' ) {
711 $val = $this->exifMsg( 'urgency',
717 // Things that have a unit of pixels.
718 case 'OriginalImageHeight':
719 case 'OriginalImageWidth':
720 case 'PixelXDimension':
721 case 'PixelYDimension':
724 $val = $this->formatNum( $val ) . ' ' . $this->msg( 'unit-pixel' )->text();
727 // Do not transform fields with pure text.
728 // For some languages the formatNum()
729 // conversion results to wrong output like
730 // foo,bar@example,com or foo٫bar@example٫com.
731 // Also some 'numeric' things like Scene codes
732 // are included here as we really don't want
734 case 'ImageDescription':
737 case 'RelatedSoundFile':
738 case 'ImageUniqueID':
739 case 'SpectralSensitivity':
740 case 'GPSSatellites':
744 case 'WorldRegionDest':
746 case 'CountryCodeDest':
747 case 'ProvinceOrStateDest':
749 case 'SublocationDest':
750 case 'WorldRegionCreated':
751 case 'CountryCreated':
752 case 'CountryCodeCreated':
753 case 'ProvinceOrStateCreated':
755 case 'SublocationCreated':
757 case 'SpecialInstructions':
762 case 'FixtureIdentifier':
764 case 'LocationDestCode':
766 case 'JPEGFileComment':
767 case 'iimSupplementalCategory':
768 case 'OriginalTransmissionRef':
770 case 'dc-contributor':
779 case 'CameraOwnerName':
782 case 'RightsCertificate':
783 case 'CopyrightOwner':
786 case 'OriginalDocumentID':
788 case 'MorePermissionsUrl':
789 case 'AttributionUrl':
790 case 'PreferredAttributionName':
791 case 'PNGFileComment':
793 case 'ContentWarning':
794 case 'GIFFileComment':
796 case 'IntellectualGenre':
798 case 'OrginisationInImage':
799 case 'PersonInImage':
801 $val = htmlspecialchars( $val );
806 case 'a': case 'p': case 'b':
807 $val = $this->exifMsg( $tag, $val );
810 $val = htmlspecialchars( $val );
816 case 'True': case 'False':
817 $val = $this->exifMsg( $tag, $val );
822 if ( $val == '-1' ) {
823 $val = $this->exifMsg( $tag, 'rejected' );
825 $val = $this->formatNum( $val );
830 $lang = Language
::fetchLanguageName( strtolower( $val ), $this->getLanguage()->getCode() );
832 $val = htmlspecialchars( $lang );
834 $val = htmlspecialchars( $val );
839 $val = $this->formatNum( $val );
843 // End formatting values, start flattening arrays.
844 $vals = $this->flattenArrayReal( $vals, $type );
851 * Flatten an array, using the content language for any messages.
853 * @param array $vals array of values
854 * @param string $type Type of array (either lang, ul, ol).
855 * lang = language assoc array with keys being the lang code
856 * ul = unordered list, ol = ordered list
857 * type can also come from the '_type' member of $vals.
858 * @param $noHtml Boolean If to avoid returning anything resembling
859 * html. (Ugly hack for backwards compatibility with old mediawiki).
860 * @param IContextSource $context
861 * @return String single value (in wiki-syntax).
864 public static function flattenArrayContentLang( $vals, $type = 'ul', $noHtml = false, $context = false ) {
866 $obj = new FormatMetadata
;
868 $obj->setContext( $context );
870 $context = new DerivativeContext( $obj->getContext() );
871 $context->setLanguage( $wgContLang );
872 $obj->setContext( $context );
873 return $obj->flattenArrayReal( $vals, $type, $noHtml );
877 * Flatten an array, using the user language for any messages.
879 * @param array $vals array of values
880 * @param string $type Type of array (either lang, ul, ol).
881 * lang = language assoc array with keys being the lang code
882 * ul = unordered list, ol = ordered list
883 * type can also come from the '_type' member of $vals.
884 * @param $noHtml Boolean If to avoid returning anything resembling
885 * html. (Ugly hack for backwards compatibility with old mediawiki).
886 * @param IContextSource $context
887 * @return String single value (in wiki-syntax).
889 public static function flattenArray( $vals, $type = 'ul', $noHtml = false, $context = false ) {
890 $obj = new FormatMetadata
;
892 $obj->setContext( $context );
894 return $obj->flattenArrayReal( $vals, $type, $noHtml );
898 * A function to collapse multivalued tags into a single value.
899 * This turns an array of (for example) authors into a bulleted list.
901 * This is public on the basis it might be useful outside of this class.
903 * @param array $vals array of values
904 * @param string $type Type of array (either lang, ul, ol).
905 * lang = language assoc array with keys being the lang code
906 * ul = unordered list, ol = ordered list
907 * type can also come from the '_type' member of $vals.
908 * @param $noHtml Boolean If to avoid returning anything resembling
909 * html. (Ugly hack for backwards compatibility with old mediawiki).
910 * @return String single value (in wiki-syntax).
913 public function flattenArrayReal( $vals, $type = 'ul', $noHtml = false ) {
914 if ( !is_array( $vals ) ) {
915 return $vals; // do nothing if not an array;
918 if ( isset( $vals['_type'] ) ) {
919 $type = $vals['_type'];
920 unset( $vals['_type'] );
923 if ( !is_array( $vals ) ) {
924 return $vals; // do nothing if not an array;
926 elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
929 elseif ( count( $vals ) === 0 ) {
930 wfDebug( __METHOD__
. " metadata array with 0 elements!\n" );
931 return ""; // paranoia. This should never happen
933 /* @todo FIXME: This should hide some of the list entries if there are
934 * say more than four. Especially if a field is translated into 20
935 * languages, we don't want to show them all by default
941 // Display default, followed by ContLang,
942 // followed by the rest in no particular
945 // Todo: hide some items if really long list.
949 $cLang = $wgContLang->getCode();
950 $defaultItem = false;
951 $defaultLang = false;
953 // If default is set, save it for later,
954 // as we don't know if it's equal to
955 // one of the lang codes. (In xmp
956 // you specify the language for a
957 // default property by having both
958 // a default prop, and one in the language
959 // that are identical)
960 if ( isset( $vals['x-default'] ) ) {
961 $defaultItem = $vals['x-default'];
962 unset( $vals['x-default'] );
964 // Do contentLanguage.
965 if ( isset( $vals[$cLang] ) ) {
967 if ( $vals[$cLang] === $defaultItem ) {
968 $defaultItem = false;
971 $content .= $this->langItem(
972 $vals[$cLang], $cLang,
973 $isDefault, $noHtml );
975 unset( $vals[$cLang] );
979 foreach ( $vals as $lang => $item ) {
980 if ( $item === $defaultItem ) {
981 $defaultLang = $lang;
984 $content .= $this->langItem( $item,
985 $lang, false, $noHtml );
987 if ( $defaultItem !== false ) {
988 $content = $this->langItem( $defaultItem,
989 $defaultLang, true, $noHtml ) .
995 return '<ul class="metadata-langlist">' .
1000 return "\n#" . implode( "\n#", $vals );
1002 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
1006 return "\n*" . implode( "\n*", $vals );
1008 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
1013 /** Helper function for creating lists of translations.
1015 * @param string $value value (this is not escaped)
1016 * @param string $lang lang code of item or false
1017 * @param $default Boolean if it is default value.
1018 * @param $noHtml Boolean If to avoid html (for back-compat)
1019 * @throws MWException
1020 * @return string language item (Note: despite how this looks,
1021 * this is treated as wikitext not html).
1023 private function langItem( $value, $lang, $default = false, $noHtml = false ) {
1024 if ( $lang === false && $default === false ) {
1025 throw new MWException( '$lang and $default cannot both '
1030 $wrappedValue = $value;
1032 $wrappedValue = '<span class="mw-metadata-lang-value">'
1033 . $value . '</span>';
1036 if ( $lang === false ) {
1037 $msg = $this->msg( 'metadata-langitem-default', $wrappedValue );
1039 return $msg->text() . "\n\n";
1041 return '<li class="mw-metadata-lang-default">'
1046 $lowLang = strtolower( $lang );
1047 $langName = Language
::fetchLanguageName( $lowLang );
1048 if ( $langName === '' ) {
1049 //try just the base language name. (aka en-US -> en ).
1050 list( $langPrefix ) = explode( '-', $lowLang, 2 );
1051 $langName = Language
::fetchLanguageName( $langPrefix );
1052 if ( $langName === '' ) {
1057 // else we have a language specified
1059 $msg = $this->msg( 'metadata-langitem', $wrappedValue, $langName, $lang );
1061 return '*' . $msg->text();
1064 $item = '<li class="mw-metadata-lang-code-'
1067 $item .= ' mw-metadata-lang-default';
1069 $item .= '" lang="' . $lang . '">';
1070 $item .= $msg->text();
1076 * Convenience function for getFormattedData()
1080 * @param string $tag the tag name to pass on
1081 * @param string $val the value of the tag
1082 * @param string $arg an argument to pass ($1)
1083 * @param string $arg2 a 2nd argument to pass ($2)
1084 * @return string The text content of "exif-$tag-$val" message in lower case
1086 private function exifMsg( $tag, $val, $arg = null, $arg2 = null ) {
1089 if ( $val === '' ) {
1092 return $this->msg( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 )->text();
1096 * Format a number, convert numbers from fractions into floating point
1097 * numbers, joins arrays of numbers with commas.
1099 * @param $num Mixed: the value to format
1100 * @param $round float|int|bool digits to round to or false.
1101 * @return mixed A floating point number or whatever we were fed
1103 private function formatNum( $num, $round = false ) {
1105 if ( is_array( $num ) ) {
1107 foreach ( $num as $number ) {
1108 $out[] = $this->formatNum( $number );
1110 return $this->getLanguage()->commaList( $out );
1112 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1114 $newNum = $m[1] / $m[2];
1115 if ( $round !== false ) {
1116 $newNum = round( $newNum, $round );
1122 return $this->getLanguage()->formatNum( $newNum );
1124 if ( is_numeric( $num ) && $round !== false ) {
1125 $num = round( $num, $round );
1127 return $this->getLanguage()->formatNum( $num );
1132 * Format a rational number, reducing fractions
1136 * @param $num Mixed: the value to format
1137 * @return mixed A floating point number or whatever we were fed
1139 private function formatFraction( $num ) {
1141 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1142 $numerator = intval( $m[1] );
1143 $denominator = intval( $m[2] );
1144 $gcd = $this->gcd( abs( $numerator ), $denominator );
1146 // 0 shouldn't happen! ;)
1147 return $this->formatNum( $numerator / $gcd ) . '/' . $this->formatNum( $denominator / $gcd );
1150 return $this->formatNum( $num );
1154 * Calculate the greatest common divisor of two integers.
1156 * @param $a Integer: Numerator
1157 * @param $b Integer: Denominator
1161 private function gcd( $a, $b ) {
1163 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1164 // Recursive form would be:
1168 return gcd( $b, $a % $b );
1171 $remainder = $a %
$b;
1173 // tail recursion...
1181 * Fetch the human readable version of a news code.
1182 * A news code is an 8 digit code. The first two
1183 * digits are a general classification, so we just
1186 * Note, leading 0's are significant, so this is
1187 * a string, not an int.
1189 * @param string $val The 8 digit news code.
1190 * @return string The human readable form
1192 private function convertNewsCode( $val ) {
1193 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1194 // Not a valid news code.
1198 switch ( substr( $val, 0, 2 ) ) {
1251 if ( $cat !== '' ) {
1252 $catMsg = $this->exifMsg( 'iimcategory', $cat );
1253 $val = $this->exifMsg( 'subjectnewscode', '', $val, $catMsg );
1259 * Format a coordinate value, convert numbers from floating point
1260 * into degree minute second representation.
1262 * @param int $coord degrees, minutes and seconds
1263 * @param string $type latitude or longitude (for if its a NWS or E)
1264 * @return mixed A floating point number or whatever we were fed
1266 private function formatCoords( $coord, $type ) {
1270 if ( $type === 'latitude' ) {
1272 } elseif ( $type === 'longitude' ) {
1277 if ( $type === 'latitude' ) {
1279 } elseif ( $type === 'longitude' ) {
1284 $deg = floor( $nCoord );
1285 $min = floor( ( $nCoord - $deg ) * 60.0 );
1286 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1288 $deg = $this->formatNum( $deg );
1289 $min = $this->formatNum( $min );
1290 $sec = $this->formatNum( $sec );
1292 return $this->msg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord )->text();
1296 * Format the contact info field into a single value.
1298 * @param array $vals array with fields of the ContactInfo
1299 * struct defined in the IPTC4XMP spec. Or potentially
1300 * an array with one element that is a free form text
1301 * value from the older iptc iim 1:118 prop.
1303 * This function might be called from
1304 * JpegHandler::convertMetadataVersion which is why it is
1307 * @return String of html-ish looking wikitext
1308 * @since 1.23 no longer static
1310 public function collapseContactInfo( $vals ) {
1311 if ( !( isset( $vals['CiAdrExtadr'] )
1312 ||
isset( $vals['CiAdrCity'] )
1313 ||
isset( $vals['CiAdrCtry'] )
1314 ||
isset( $vals['CiEmailWork'] )
1315 ||
isset( $vals['CiTelWork'] )
1316 ||
isset( $vals['CiAdrPcode'] )
1317 ||
isset( $vals['CiAdrRegion'] )
1318 ||
isset( $vals['CiUrlWork'] )
1320 // We don't have any sub-properties
1321 // This could happen if its using old
1322 // iptc that just had this as a free-form
1324 // Note: We run this through htmlspecialchars
1325 // partially to be consistent, and partially
1326 // because people often insert >, etc into
1327 // the metadata which should not be interpreted
1328 // but we still want to auto-link urls.
1329 foreach ( $vals as &$val ) {
1330 $val = htmlspecialchars( $val );
1332 return $this->flattenArrayReal( $vals );
1334 // We have a real ContactInfo field.
1335 // Its unclear if all these fields have to be
1336 // set, so assume they do not.
1337 $url = $tel = $street = $city = $country = '';
1338 $email = $postal = $region = '';
1340 // Also note, some of the class names this uses
1341 // are similar to those used by hCard. This is
1342 // mostly because they're sensible names. This
1343 // does not (and does not attempt to) output
1344 // stuff in the hCard microformat. However it
1345 // might output in the adr microformat.
1347 if ( isset( $vals['CiAdrExtadr'] ) ) {
1348 // Todo: This can potentially be multi-line.
1349 // Need to check how that works in XMP.
1350 $street = '<span class="extended-address">'
1352 $vals['CiAdrExtadr'] )
1355 if ( isset( $vals['CiAdrCity'] ) ) {
1356 $city = '<span class="locality">'
1357 . htmlspecialchars( $vals['CiAdrCity'] )
1360 if ( isset( $vals['CiAdrCtry'] ) ) {
1361 $country = '<span class="country-name">'
1362 . htmlspecialchars( $vals['CiAdrCtry'] )
1365 if ( isset( $vals['CiEmailWork'] ) ) {
1367 // Have to split multiple emails at commas/new lines.
1368 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1369 foreach ( $splitEmails as $e1 ) {
1370 // Also split on comma
1371 foreach ( explode( ',', $e1 ) as $e2 ) {
1372 $finalEmail = trim( $e2 );
1373 if ( $finalEmail == ',' ||
$finalEmail == '' ) {
1376 if ( strpos( $finalEmail, '<' ) !== false ) {
1377 // Don't do fancy formatting to
1378 // "My name" <foo@bar.com> style stuff
1379 $emails[] = $finalEmail;
1381 $emails[] = '[mailto:'
1383 . ' <span class="email">'
1389 $email = implode( ', ', $emails );
1391 if ( isset( $vals['CiTelWork'] ) ) {
1392 $tel = '<span class="tel">'
1393 . htmlspecialchars( $vals['CiTelWork'] )
1396 if ( isset( $vals['CiAdrPcode'] ) ) {
1397 $postal = '<span class="postal-code">'
1399 $vals['CiAdrPcode'] )
1402 if ( isset( $vals['CiAdrRegion'] ) ) {
1403 // Note this is province/state.
1404 $region = '<span class="region">'
1406 $vals['CiAdrRegion'] )
1409 if ( isset( $vals['CiUrlWork'] ) ) {
1410 $url = '<span class="url">'
1411 . htmlspecialchars( $vals['CiUrlWork'] )
1414 return $this->msg( 'exif-contact-value', $email, $url,
1415 $street, $city, $region, $postal, $country,
1421 /** For compatability with old FormatExif class
1422 * which some extensions use.
1424 * @deprecated since 1.18
1431 * @param $meta array
1433 function FormatExif( $meta ) {
1434 wfDeprecated( __METHOD__
, '1.18' );
1435 $this->meta
= $meta;
1441 function getFormattedData() {
1442 return FormatMetadata
::getFormattedData( $this->meta
);