3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
20 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason, 2009 Brent Garber, 2010 Brian Wolff
21 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
22 * @see http://exif.org/Exif2-2.PDF The Exif 2.2 specification
28 * Format Image metadata values into a human readable form.
30 * Note lots of these messages use the prefix 'exif' even though
31 * they may not be exif properties. For example 'exif-ImageDescription'
32 * can be the Exif ImageDescription, or it could be the iptc-iim caption
33 * property, or it could be the xmp dc:description property. This
34 * is because these messages should be independent of how the data is
35 * stored, sine the user doesn't care if the description is stored in xmp,
36 * exif, etc only that its a description. (Additionally many of these properties
37 * are merged together following the MWG standard, such that for example,
38 * exif properties override XMP properties that mean the same thing if
39 * there is a conflict).
41 * It should perhaps use a prefix like 'metadata' instead, but there
42 * is already a large number of messages using the 'exif' prefix.
46 class FormatMetadata
{
49 * Numbers given by Exif user agents are often magical, that is they
50 * should be replaced by a detailed explanation depending on their
51 * value which most of the time are plain integers. This function
52 * formats Exif (and other metadata) values into human readable form.
54 * @param $tags Array: the Exif data to format ( as returned by
55 * Exif::getFilteredData() or BitmapMetadataHandler )
58 public static function getFormattedData( $tags ) {
61 $resolutionunit = !isset( $tags['ResolutionUnit'] ) ||
$tags['ResolutionUnit'] == 2 ?
2 : 3;
62 unset( $tags['ResolutionUnit'] );
64 foreach ( $tags as $tag => &$vals ) {
66 // This seems ugly to wrap non-array's in an array just to unwrap again,
67 // especially when most of the time it is not an array
68 if ( !is_array( $tags[$tag] ) ) {
69 $vals = Array( $vals );
72 // _type is a special value to say what array type
73 if ( isset( $tags[$tag]['_type'] ) ) {
74 $type = $tags[$tag]['_type'];
75 unset( $vals['_type'] );
77 $type = 'ul'; // default unordered list.
80 //This is done differently as the tag is an array.
81 if ($tag == 'GPSTimeStamp' && count($vals) === 3) {
84 $h = explode('/', $vals[0]);
85 $m = explode('/', $vals[1]);
86 $s = explode('/', $vals[2]);
88 // this should already be validated
89 // when loaded from file, but it could
90 // come from a foreign repo, so be
101 $tags[$tag] = intval( $h[0] / $h[1] )
102 . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT
)
103 . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT
);
105 $time = wfTimestamp( TS_MW
, '1971:01:01 ' . $tags[$tag] );
106 // the 1971:01:01 is just a placeholder, and not shown to user.
108 $tags[$tag] = $wgLang->time( $time );
113 // The contact info is a multi-valued field
114 // instead of the other props which are single
115 // valued (mostly) so handle as a special case.
116 if ( $tag === 'Contact' ) {
117 $vals = self
::collapseContactInfo( $vals );
121 foreach ( $vals as &$val ) {
127 $val = self
::msg( $tag, $val );
130 /* If not recognized, display as is. */
135 case 'PhotometricInterpretation':
138 $val = self
::msg( $tag, $val );
141 /* If not recognized, display as is. */
148 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
149 $val = self
::msg( $tag, $val );
152 /* If not recognized, display as is. */
157 case 'PlanarConfiguration':
160 $val = self
::msg( $tag, $val );
163 /* If not recognized, display as is. */
168 // TODO: YCbCrSubSampling
169 case 'YCbCrPositioning':
173 $val = self
::msg( $tag, $val );
176 /* If not recognized, display as is. */
183 switch( $resolutionunit ) {
185 $val = self
::msg( 'XYResolution', 'i', self
::formatNum( $val ) );
188 $val = self
::msg( 'XYResolution', 'c', self
::formatNum( $val ) );
191 /* If not recognized, display as is. */
196 // TODO: YCbCrCoefficients #p27 (see annex E)
197 case 'ExifVersion': case 'FlashpixVersion':
204 $val = self
::msg( $tag, $val );
207 /* If not recognized, display as is. */
212 case 'ComponentsConfiguration':
214 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
215 $val = self
::msg( $tag, $val );
218 /* If not recognized, display as is. */
224 case 'DateTimeOriginal':
225 case 'DateTimeDigitized':
226 case 'DateTimeReleased':
227 case 'DateTimeExpires':
230 case 'DateTimeMetadata':
231 if ( $val == '0000:00:00 00:00:00' ||
$val == ' : : : : ' ) {
232 $val = wfMsg( 'exif-unknowndate' );
233 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
234 $time = wfTimestamp( TS_MW
, $val );
236 $val = $wgLang->timeanddate( $time );
238 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
239 // If only the date but not the time is filled in.
240 $time = wfTimestamp( TS_MW
, substr( $val, 0, 4 )
241 . substr( $val, 5, 2 )
242 . substr( $val, 8, 2 )
245 $val = $wgLang->date( $time );
248 // else it will just output $val without formatting it.
251 case 'ExposureProgram':
253 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
254 $val = self
::msg( $tag, $val );
257 /* If not recognized, display as is. */
262 case 'SubjectDistance':
263 $val = self
::msg( $tag, '', self
::formatNum( $val ) );
268 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
269 $val = self
::msg( $tag, $val );
272 /* If not recognized, display as is. */
279 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
280 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
281 case 21: case 22: case 23: case 24: case 255:
282 $val = self
::msg( $tag, $val );
285 /* If not recognized, display as is. */
291 $flashDecode = array(
292 'fired' => $val & bindec( '00000001' ),
293 'return' => ( $val & bindec( '00000110' ) ) >> 1,
294 'mode' => ( $val & bindec( '00011000' ) ) >> 3,
295 'function' => ( $val & bindec( '00100000' ) ) >> 5,
296 'redeye' => ( $val & bindec( '01000000' ) ) >> 6,
297 // 'reserved' => ($val & bindec( '10000000' )) >> 7,
300 # We do not need to handle unknown values since all are used.
301 foreach ( $flashDecode as $subTag => $subValue ) {
302 # We do not need any message for zeroed values.
303 if ( $subTag != 'fired' && $subValue == 0 ) {
306 $fullTag = $tag . '-' . $subTag ;
307 $flashMsgs[] = self
::msg( $fullTag, $subValue );
309 $val = $wgLang->commaList( $flashMsgs );
312 case 'FocalPlaneResolutionUnit':
315 $val = self
::msg( $tag, $val );
318 /* If not recognized, display as is. */
323 case 'SensingMethod':
325 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
326 $val = self
::msg( $tag, $val );
329 /* If not recognized, display as is. */
337 $val = self
::msg( $tag, $val );
340 /* If not recognized, display as is. */
348 $val = self
::msg( $tag, $val );
351 /* If not recognized, display as is. */
356 case 'CustomRendered':
359 $val = self
::msg( $tag, $val );
362 /* If not recognized, display as is. */
369 case 0: case 1: case 2:
370 $val = self
::msg( $tag, $val );
373 /* If not recognized, display as is. */
381 $val = self
::msg( $tag, $val );
384 /* If not recognized, display as is. */
389 case 'SceneCaptureType':
391 case 0: case 1: case 2: case 3:
392 $val = self
::msg( $tag, $val );
395 /* If not recognized, display as is. */
402 case 0: case 1: case 2: case 3: case 4:
403 $val = self
::msg( $tag, $val );
406 /* If not recognized, display as is. */
413 case 0: case 1: case 2:
414 $val = self
::msg( $tag, $val );
417 /* If not recognized, display as is. */
424 case 0: case 1: case 2:
425 $val = self
::msg( $tag, $val );
428 /* If not recognized, display as is. */
435 case 0: case 1: case 2:
436 $val = self
::msg( $tag, $val );
439 /* If not recognized, display as is. */
444 case 'SubjectDistanceRange':
446 case 0: case 1: case 2: case 3:
447 $val = self
::msg( $tag, $val );
450 /* If not recognized, display as is. */
455 //The GPS...Ref values are kept for compatibility, probably won't be reached.
456 case 'GPSLatitudeRef':
457 case 'GPSDestLatitudeRef':
460 $val = self
::msg( 'GPSLatitude', $val );
463 /* If not recognized, display as is. */
468 case 'GPSLongitudeRef':
469 case 'GPSDestLongitudeRef':
472 $val = self
::msg( 'GPSLongitude', $val );
475 /* If not recognized, display as is. */
482 $val = self
::msg( 'GPSAltitude', 'below-sealevel', self
::formatNum( -$val, 3 ) );
484 $val = self
::msg( 'GPSAltitude', 'above-sealevel', self
::formatNum( $val, 3 ) );
491 $val = self
::msg( $tag, $val );
494 /* If not recognized, display as is. */
499 case 'GPSMeasureMode':
502 $val = self
::msg( $tag, $val );
505 /* If not recognized, display as is. */
512 case 'GPSImgDirectionRef':
513 case 'GPSDestBearingRef':
516 $val = self
::msg( 'GPSDirection', $val );
519 /* If not recognized, display as is. */
525 case 'GPSDestLatitude':
526 $val = self
::formatCoords( $val, 'latitude' );
529 case 'GPSDestLongitude':
530 $val = self
::formatCoords( $val, 'longitude' );
535 case 'K': case 'M': case 'N':
536 $val = self
::msg( 'GPSSpeed', $val );
539 /* If not recognized, display as is. */
544 case 'GPSDestDistanceRef':
546 case 'K': case 'M': case 'N':
547 $val = self
::msg( 'GPSDestDistance', $val );
550 /* If not recognized, display as is. */
556 // See http://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
558 $val = self
::msg( $tag, 'excellent', self
::formatNum( $val ) );
559 } elseif ( $val <= 5 ) {
560 $val = self
::msg( $tag, 'good', self
::formatNum( $val ) );
561 } elseif ( $val <= 10 ) {
562 $val = self
::msg( $tag, 'moderate', self
::formatNum( $val ) );
563 } elseif ( $val <= 20 ) {
564 $val = self
::msg( $tag, 'fair', self
::formatNum( $val ) );
566 $val = self
::msg( $tag, 'poor', self
::formatNum( $val ) );
570 // This is not in the Exif standard, just a special
571 // case for our purposes which enables wikis to wikify
572 // the make, model and software name to link to their articles.
575 $val = self
::msg( $tag, '', $val );
579 if ( is_array( $val ) ) {
580 //if its a software, version array.
581 $val = wfMsg( 'exif-software-version-value', $val[0], $val[1] );
583 $val = self
::msg( $tag, '', $val );
588 // Show the pretty fraction as well as decimal version
589 $val = wfMsg( 'exif-exposuretime-format',
590 self
::formatFraction( $val ), self
::formatNum( $val ) );
592 case 'ISOSpeedRatings':
593 // If its = 65535 that means its at the
594 // limit of the size of Exif::short and
596 if ( $val == '65535' ) {
597 $val = self
::msg( $tag, 'overflow' );
599 $val = self
::formatNum( $val );
603 $val = wfMsg( 'exif-fnumber-format',
604 self
::formatNum( $val ) );
607 case 'FocalLength': case 'FocalLengthIn35mmFilm':
608 $val = wfMsg( 'exif-focallength-format',
609 self
::formatNum( $val ) );
612 case 'MaxApertureValue':
613 if ( strpos( $val, '/' ) !== false ) {
614 // need to expand this earlier to calculate fNumber
615 list($n, $d) = explode('/', $val);
616 if ( is_numeric( $n ) && is_numeric( $d ) ) {
620 if ( is_numeric( $val ) ) {
621 $fNumber = pow( 2, $val / 2 );
622 if ( $fNumber !== false ) {
623 $val = wfMsg( 'exif-maxaperturevalue-value',
624 self
::formatNum( $val ),
625 self
::formatNum( $fNumber, 2 )
632 switch( strtolower( $val ) ) {
633 // See pg 29 of IPTC photo
634 // metadata standard.
635 case 'ace': case 'clj':
636 case 'dis': case 'fin':
637 case 'edu': case 'evn':
638 case 'hth': case 'hum':
639 case 'lab': case 'lif':
640 case 'pol': case 'rel':
641 case 'sci': case 'soi':
642 case 'spo': case 'war':
650 case 'SubjectNewsCode':
651 // Essentially like iimCategory.
652 // 8 (numeric) digit hierarchical
653 // classification. We decode the
654 // first 2 digits, which provide
656 $val = self
::convertNewsCode( $val );
659 // 1-8 with 1 being highest, 5 normal
660 // 0 is reserved, and 9 is 'user-defined'.
662 if ( $val == 0 ||
$val == 9 ) {
664 } elseif ( $val < 5 && $val > 1 ) {
666 } elseif ( $val == 5 ) {
668 } elseif ( $val <= 8 && $val > 5) {
672 if ( $urgency !== '' ) {
673 $val = self
::msg( 'urgency',
679 // Things that have a unit of pixels.
680 case 'OriginalImageHeight':
681 case 'OriginalImageWidth':
682 case 'PixelXDimension':
683 case 'PixelYDimension':
686 $val = self
::formatNum( $val ) . ' ' . wfMsg( 'unit-pixel' );
689 // Do not transform fields with pure text.
690 // For some languages the formatNum()
691 // conversion results to wrong output like
692 // foo,bar@example,com or foo٫bar@example٫com.
693 // Also some 'numeric' things like Scene codes
694 // are included here as we really don't want
696 case 'ImageDescription':
699 case 'RelatedSoundFile':
700 case 'ImageUniqueID':
701 case 'SpectralSensitivity':
702 case 'GPSSatellites':
706 case 'WorldRegionDest':
708 case 'CountryCodeDest':
709 case 'ProvinceOrStateDest':
711 case 'SublocationDest':
712 case 'WorldRegionCreated':
713 case 'CountryCreated':
714 case 'CountryCodeCreated':
715 case 'ProvinceOrStateCreated':
717 case 'SublocationCreated':
719 case 'SpecialInstructions':
724 case 'FixtureIdentifier':
726 case 'LocationDestCode':
728 case 'JPEGFileComment':
729 case 'iimSupplementalCategory':
730 case 'OriginalTransmissionRef':
732 case 'dc-contributor':
741 case 'CameraOwnerName':
744 case 'RightsCertificate':
745 case 'CopyrightOwner':
748 case 'OriginalDocumentID':
750 case 'MorePermissionsUrl':
751 case 'AttributionUrl':
752 case 'PreferredAttributionName':
753 case 'PNGFileComment':
755 case 'ContentWarning':
756 case 'GIFFileComment':
758 case 'IntellectualGenre':
760 case 'OrginisationInImage':
761 case 'PersonInImage':
763 $val = htmlspecialchars( $val );
768 case 'a': case 'p': case 'b':
769 $val = self
::msg( $tag, $val );
772 $val = htmlspecialchars( $val );
778 case 'True': case 'False':
779 $val = self
::msg( $tag, $val );
784 if ( $val == '-1' ) {
785 $val = self
::msg( $tag, 'rejected' );
787 $val = self
::formatNum( $val );
792 $lang = $wgLang->getLanguageName( strtolower( $val ) );
794 $val = htmlspecialchars( $lang );
796 $val = htmlspecialchars( $val );
801 $val = self
::formatNum( $val );
805 // End formatting values, start flattening arrays.
806 $vals = self
::flattenArray( $vals, $type );
813 * A function to collapse multivalued tags into a single value.
814 * This turns an array of (for example) authors into a bulleted list.
816 * This is public on the basis it might be useful outside of this class.
818 * @param $vals Array array of values
819 * @param $type String Type of array (either lang, ul, ol).
820 * lang = language assoc array with keys being the lang code
821 * ul = unordered list, ol = ordered list
822 * type can also come from the '_type' member of $vals.
823 * @param $noHtml Boolean If to avoid returning anything resembling
824 * html. (Ugly hack for backwards compatibility with old mediawiki).
825 * @return String single value (in wiki-syntax).
827 public static function flattenArray( $vals, $type = 'ul', $noHtml = false ) {
828 if ( isset( $vals['_type'] ) ) {
829 $type = $vals['_type'];
830 unset( $vals['_type'] );
833 if ( !is_array( $vals ) ) {
834 return $vals; // do nothing if not an array;
836 elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
839 elseif ( count( $vals ) === 0 ) {
840 wfDebug( __METHOD__
. ' metadata array with 0 elements!' );
841 return ""; // paranoia. This should never happen
843 /* Fixme: This should hide some of the list entries if there are
844 * say more than four. Especially if a field is translated into 20
845 * languages, we don't want to show them all by default
851 // Display default, followed by ContLang,
852 // followed by the rest in no particular
855 // Todo: hide some items if really long list.
859 $cLang = $wgContLang->getCode();
860 $defaultItem = false;
861 $defaultLang = false;
863 // If default is set, save it for later,
864 // as we don't know if it's equal to
865 // one of the lang codes. (In xmp
866 // you specify the language for a
867 // default property by having both
868 // a default prop, and one in the language
869 // that are identical)
870 if ( isset( $vals['x-default'] ) ) {
871 $defaultItem = $vals['x-default'];
872 unset( $vals['x-default'] );
874 // Do contentLanguage.
875 if ( isset( $vals[$cLang] ) ) {
877 if ( $vals[$cLang] === $defaultItem ) {
878 $defaultItem = false;
881 $content .= self
::langItem(
882 $vals[$cLang], $cLang,
883 $isDefault, $noHtml );
885 unset( $vals[$cLang] );
889 foreach ( $vals as $lang => $item ) {
890 if ( $item === $defaultItem ) {
891 $defaultLang = $lang;
894 $content .= self
::langItem( $item,
895 $lang, false, $noHtml );
897 if ( $defaultItem !== false ) {
898 $content = self
::langItem( $defaultItem,
899 $defaultLang, true, $noHtml )
905 return '<ul class="metadata-langlist">' .
910 return "\n#" . implode( "\n#", $vals );
912 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
916 return "\n*" . implode( "\n*", $vals );
918 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
922 /** Helper function for creating lists of translations.
924 * @param $value String value (this is not escaped)
925 * @param $lang String lang code of item or false
926 * @param $default Boolean if it is default value.
927 * @param $noHtml Boolean If to avoid html (for back-compat)
928 * @return language item (Note: despite how this looks,
929 * this is treated as wikitext not html).
931 private static function langItem( $value, $lang, $default = false, $noHtml = false ) {
933 if ( $lang === false && $default === false) {
934 throw new MWException('$lang and $default cannot both '
939 $wrappedValue = $value;
941 $wrappedValue = '<span class="mw-metadata-lang-value">'
942 . $value . '</span>';
945 if ( $lang === false ) {
947 return wfMsg( 'metadata-langitem-default',
948 $wrappedValue ) . "\n\n";
950 return '<li class="mw-metadata-lang-default">'
951 . wfMsg( 'metadata-langitem-default',
956 $lowLang = strtolower( $lang );
957 $langName = $wgContLang->getLanguageName( $lowLang );
958 if ( $langName === '' ) {
959 //try just the base language name. (aka en-US -> en ).
960 list( $langPrefix ) = explode( '-', $lowLang, 2 );
961 $langName = $wgContLang->getLanguageName( $langPrefix );
962 if ( $langName === '' ) {
967 // else we have a language specified
970 return '*' . wfMsg( 'metadata-langitem',
971 $wrappedValue, $langName, $lang );
974 $item = '<li class="mw-metadata-lang-code-'
977 $item .= ' mw-metadata-lang-default';
979 $item .= '" lang="' . $lang . '">';
980 $item .= wfMsg( 'metadata-langitem',
981 $wrappedValue, $langName, $lang );
986 * Convenience function for getFormattedData()
990 * @param $tag String: the tag name to pass on
991 * @param $val String: the value of the tag
992 * @param $arg String: an argument to pass ($1)
993 * @param $arg2 String: a 2nd argument to pass ($2)
994 * @return string A wfMsg of "exif-$tag-$val" in lower case
996 static function msg( $tag, $val, $arg = null, $arg2 = null ) {
1001 return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 );
1005 * Format a number, convert numbers from fractions into floating point
1006 * numbers, joins arrays of numbers with commas.
1010 * @param $num Mixed: the value to format
1011 * @param $round digits to round to or false.
1012 * @return mixed A floating point number or whatever we were fed
1014 static function formatNum( $num, $round = false ) {
1017 if( is_array($num) ) {
1019 foreach( $num as $number ) {
1020 $out[] = self
::formatNum($number);
1022 return $wgLang->commaList( $out );
1024 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1026 $newNum = $m[1] / $m[2];
1027 if ( $round !== false ) {
1028 $newNum = round( $newNum, $round );
1034 return $wgLang->formatNum( $newNum );
1036 if ( is_numeric( $num ) && $round !== false ) {
1037 $num = round( $num, $round );
1039 return $wgLang->formatNum( $num );
1044 * Format a rational number, reducing fractions
1048 * @param $num Mixed: the value to format
1049 * @return mixed A floating point number or whatever we were fed
1051 static function formatFraction( $num ) {
1053 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1054 $numerator = intval( $m[1] );
1055 $denominator = intval( $m[2] );
1056 $gcd = self
::gcd( abs( $numerator ), $denominator );
1058 // 0 shouldn't happen! ;)
1059 return self
::formatNum( $numerator / $gcd ) . '/' . self
::formatNum( $denominator / $gcd );
1062 return self
::formatNum( $num );
1066 * Calculate the greatest common divisor of two integers.
1068 * @param $a Integer: Numerator
1069 * @param $b Integer: Denominator
1073 static function gcd( $a, $b ) {
1075 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1076 // Recursive form would be:
1080 return gcd( $b, $a % $b );
1083 $remainder = $a %
$b;
1085 // tail recursion...
1092 /** Fetch the human readable version of a news code.
1093 * A news code is an 8 digit code. The first two
1094 * digits are a general classification, so we just
1097 * Note, leading 0's are significant, so this is
1098 * a string, not an int.
1100 * @param $val String: The 8 digit news code.
1101 * @return The human readable form
1103 static private function convertNewsCode( $val ) {
1104 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1105 // Not a valid news code.
1109 switch( substr( $val , 0, 2 ) ) {
1162 if ( $cat !== '' ) {
1163 $catMsg = self
::msg( 'iimcategory', $cat );
1164 $val = self
::msg( 'subjectnewscode', '', $val, $catMsg );
1169 * Format a coordinate value, convert numbers from floating point
1170 * into degree minute second representation.
1172 * @param $coords Array: degrees, minutes and seconds
1173 * @param $type String: latitude or longitude (for if its a NWS or E)
1174 * @return mixed A floating point number or whatever we were fed
1176 static function formatCoords( $coord, $type ) {
1180 if ( $type === 'latitude' ) {
1183 elseif ( $type === 'longitude' ) {
1189 if ( $type === 'latitude' ) {
1192 elseif ( $type === 'longitude' ) {
1197 $deg = floor( $nCoord );
1198 $min = floor( ( $nCoord - $deg ) * 60.0 );
1199 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1201 $deg = self
::formatNum( $deg );
1202 $min = self
::formatNum( $min );
1203 $sec = self
::formatNum( $sec );
1205 return wfMsg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord );
1208 * Format the contact info field into a single value.
1210 * @param $vals Array array with fields of the ContactInfo
1211 * struct defined in the IPTC4XMP spec. Or potentially
1212 * an array with one element that is a free form text
1213 * value from the older iptc iim 1:118 prop.
1215 * This function might be called from
1216 * JpegHandler::convertMetadataVersion which is why it is
1219 * @return String of html-ish looking wikitext
1221 public static function collapseContactInfo( $vals ) {
1222 if( ! ( isset( $vals['CiAdrExtadr'] )
1223 ||
isset( $vals['CiAdrCity'] )
1224 ||
isset( $vals['CiAdrCtry'] )
1225 ||
isset( $vals['CiEmailWork'] )
1226 ||
isset( $vals['CiTelWork'] )
1227 ||
isset( $vals['CiAdrPcode'] )
1228 ||
isset( $vals['CiAdrRegion'] )
1229 ||
isset( $vals['CiUrlWork'] )
1231 // We don't have any sub-properties
1232 // This could happen if its using old
1233 // iptc that just had this as a free-form
1235 // Note: We run this through htmlspecialchars
1236 // partially to be consistent, and partially
1237 // because people often insert >, etc into
1238 // the metadata which should not be interpreted
1239 // but we still want to auto-link urls.
1240 foreach( $vals as &$val ) {
1241 $val = htmlspecialchars( $val );
1243 return self
::flattenArray( $vals );
1245 // We have a real ContactInfo field.
1246 // Its unclear if all these fields have to be
1247 // set, so assume they do not.
1248 $url = $tel = $street = $city = $country = '';
1249 $email = $postal = $region = '';
1251 // Also note, some of the class names this uses
1252 // are similar to those used by hCard. This is
1253 // mostly because they're sensible names. This
1254 // does not (and does not attempt to) output
1255 // stuff in the hCard microformat. However it
1256 // might output in the adr microformat.
1258 if ( isset( $vals['CiAdrExtadr'] ) ) {
1259 // Todo: This can potentially be multi-line.
1260 // Need to check how that works in XMP.
1261 $street = '<span class="extended-address">'
1263 $vals['CiAdrExtadr'] )
1266 if ( isset( $vals['CiAdrCity'] ) ) {
1267 $city = '<span class="locality">'
1268 . htmlspecialchars( $vals['CiAdrCity'] )
1271 if ( isset( $vals['CiAdrCtry'] ) ) {
1272 $country = '<span class="country-name">'
1273 . htmlspecialchars( $vals['CiAdrCtry'] )
1276 if ( isset( $vals['CiEmailWork'] ) ) {
1278 // Have to split multiple emails at commas/new lines.
1279 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1280 foreach ( $splitEmails as $e1 ) {
1281 // Also split on comma
1282 foreach ( explode( ',', $e1 ) as $e2 ) {
1283 $finalEmail = trim( $e2 );
1284 if ( $finalEmail == ',' ||
$finalEmail == '' ) {
1287 if ( strpos( $finalEmail, '<' ) !== false ) {
1288 // Don't do fancy formatting to
1289 // "My name" <foo@bar.com> style stuff
1290 $emails[] = $finalEmail;
1292 $emails[] = '[mailto:'
1294 . ' <span class="email">'
1300 $email = implode( ', ', $emails );
1302 if ( isset( $vals['CiTelWork'] ) ) {
1303 $tel = '<span class="tel">'
1304 . htmlspecialchars( $vals['CiTelWork'] )
1307 if ( isset( $vals['CiAdrPcode'] ) ) {
1308 $postal = '<span class="postal-code">'
1310 $vals['CiAdrPcode'] )
1313 if ( isset( $vals['CiAdrRegion'] ) ) {
1314 // Note this is province/state.
1315 $region = '<span class="region">'
1317 $vals['CiAdrRegion'] )
1320 if ( isset( $vals['CiUrlWork'] ) ) {
1321 $url = '<span class="url">'
1322 . htmlspecialchars( $vals['CiUrlWork'] )
1325 return wfMsg( 'exif-contact-value', $email, $url,
1326 $street, $city, $region, $postal, $country,
1332 /** For compatability with old FormatExif class
1333 * which some extensions use.
1340 function FormatExif ( $meta ) {
1341 wfDeprecated(__METHOD__
);
1342 $this->meta
= $meta;
1345 function getFormattedData ( ) {
1346 return FormatMetadata
::getFormattedData( $this->meta
);