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.
47 class FormatMetadata
{
50 * Numbers given by Exif user agents are often magical, that is they
51 * should be replaced by a detailed explanation depending on their
52 * value which most of the time are plain integers. This function
53 * formats Exif (and other metadata) values into human readable form.
55 * @param array $tags the Exif data to format ( as returned by
56 * Exif::getFilteredData() or BitmapMetadataHandler )
59 public static function getFormattedData( $tags ) {
62 $resolutionunit = !isset( $tags['ResolutionUnit'] ) ||
$tags['ResolutionUnit'] == 2 ?
2 : 3;
63 unset( $tags['ResolutionUnit'] );
65 foreach ( $tags as $tag => &$vals ) {
67 // This seems ugly to wrap non-array's in an array just to unwrap again,
68 // especially when most of the time it is not an array
69 if ( !is_array( $tags[$tag] ) ) {
70 $vals = Array( $vals );
73 // _type is a special value to say what array type
74 if ( isset( $tags[$tag]['_type'] ) ) {
75 $type = $tags[$tag]['_type'];
76 unset( $vals['_type'] );
78 $type = 'ul'; // default unordered list.
81 //This is done differently as the tag is an array.
82 if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3 ) {
85 $h = explode( '/', $vals[0] );
86 $m = explode( '/', $vals[1] );
87 $s = explode( '/', $vals[2] );
89 // this should already be validated
90 // when loaded from file, but it could
91 // come from a foreign repo, so be
102 $tags[$tag] = str_pad( intval( $h[0] / $h[1] ), 2, '0', STR_PAD_LEFT
)
103 . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT
)
104 . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT
);
107 $time = wfTimestamp( TS_MW
, '1971:01:01 ' . $tags[$tag] );
108 // the 1971:01:01 is just a placeholder, and not shown to user.
109 if ( $time && intval( $time ) > 0 ) {
110 $tags[$tag] = $wgLang->time( $time );
112 } catch ( TimestampException
$e ) {
113 // This shouldn't happen, but we've seen bad formats
114 // such as 4-digit seconds in the wild.
115 // leave $tags[$tag] as-is
120 // The contact info is a multi-valued field
121 // instead of the other props which are single
122 // valued (mostly) so handle as a special case.
123 if ( $tag === 'Contact' ) {
124 $vals = self
::collapseContactInfo( $vals );
128 foreach ( $vals as &$val ) {
133 case 1: case 2: case 3: case 4:
134 case 5: case 6: case 7: case 8:
135 case 32773: case 32946: case 34712:
136 $val = self
::msg( $tag, $val );
139 /* If not recognized, display as is. */
144 case 'PhotometricInterpretation':
147 $val = self
::msg( $tag, $val );
150 /* If not recognized, display as is. */
157 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
158 $val = self
::msg( $tag, $val );
161 /* If not recognized, display as is. */
166 case 'PlanarConfiguration':
169 $val = self
::msg( $tag, $val );
172 /* If not recognized, display as is. */
177 // TODO: YCbCrSubSampling
178 case 'YCbCrPositioning':
182 $val = self
::msg( $tag, $val );
185 /* If not recognized, display as is. */
192 switch ( $resolutionunit ) {
194 $val = self
::msg( 'XYResolution', 'i', self
::formatNum( $val ) );
197 $val = self
::msg( 'XYResolution', 'c', self
::formatNum( $val ) );
200 /* If not recognized, display as is. */
205 // TODO: YCbCrCoefficients #p27 (see annex E)
206 case 'ExifVersion': case 'FlashpixVersion':
213 $val = self
::msg( $tag, $val );
216 /* If not recognized, display as is. */
221 case 'ComponentsConfiguration':
223 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
224 $val = self
::msg( $tag, $val );
227 /* If not recognized, display as is. */
233 case 'DateTimeOriginal':
234 case 'DateTimeDigitized':
235 case 'DateTimeReleased':
236 case 'DateTimeExpires':
239 case 'DateTimeMetadata':
240 if ( $val == '0000:00:00 00:00:00' ||
$val == ' : : : : ' ) {
241 $val = wfMessage( 'exif-unknowndate' )->text();
242 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
244 $time = wfTimestamp( TS_MW
, $val );
245 if ( $time && intval( $time ) > 0 ) {
246 $val = $wgLang->timeanddate( $time );
248 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
249 // No second field. Still format the same
250 // since timeanddate doesn't include seconds anyways,
251 // but second still available in api
252 $time = wfTimestamp( TS_MW
, $val . ':00' );
253 if ( $time && intval( $time ) > 0 ) {
254 $val = $wgLang->timeanddate( $time );
256 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
257 // If only the date but not the time is filled in.
258 $time = wfTimestamp( TS_MW
, substr( $val, 0, 4 )
259 . substr( $val, 5, 2 )
260 . substr( $val, 8, 2 )
262 if ( $time && intval( $time ) > 0 ) {
263 $val = $wgLang->date( $time );
266 // else it will just output $val without formatting it.
269 case 'ExposureProgram':
271 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
272 $val = self
::msg( $tag, $val );
275 /* If not recognized, display as is. */
280 case 'SubjectDistance':
281 $val = self
::msg( $tag, '', self
::formatNum( $val ) );
286 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
287 $val = self
::msg( $tag, $val );
290 /* If not recognized, display as is. */
297 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
298 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
299 case 21: case 22: case 23: case 24: case 255:
300 $val = self
::msg( $tag, $val );
303 /* If not recognized, display as is. */
309 $flashDecode = array(
310 'fired' => $val & bindec( '00000001' ),
311 'return' => ( $val & bindec( '00000110' ) ) >> 1,
312 'mode' => ( $val & bindec( '00011000' ) ) >> 3,
313 'function' => ( $val & bindec( '00100000' ) ) >> 5,
314 'redeye' => ( $val & bindec( '01000000' ) ) >> 6,
315 // 'reserved' => ($val & bindec( '10000000' )) >> 7,
317 $flashMsgs = array();
318 # We do not need to handle unknown values since all are used.
319 foreach ( $flashDecode as $subTag => $subValue ) {
320 # We do not need any message for zeroed values.
321 if ( $subTag != 'fired' && $subValue == 0 ) {
324 $fullTag = $tag . '-' . $subTag;
325 $flashMsgs[] = self
::msg( $fullTag, $subValue );
327 $val = $wgLang->commaList( $flashMsgs );
330 case 'FocalPlaneResolutionUnit':
333 $val = self
::msg( $tag, $val );
336 /* If not recognized, display as is. */
341 case 'SensingMethod':
343 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
344 $val = self
::msg( $tag, $val );
347 /* If not recognized, display as is. */
355 $val = self
::msg( $tag, $val );
358 /* If not recognized, display as is. */
366 $val = self
::msg( $tag, $val );
369 /* If not recognized, display as is. */
374 case 'CustomRendered':
377 $val = self
::msg( $tag, $val );
380 /* If not recognized, display as is. */
387 case 0: case 1: case 2:
388 $val = self
::msg( $tag, $val );
391 /* If not recognized, display as is. */
399 $val = self
::msg( $tag, $val );
402 /* If not recognized, display as is. */
407 case 'SceneCaptureType':
409 case 0: case 1: case 2: case 3:
410 $val = self
::msg( $tag, $val );
413 /* If not recognized, display as is. */
420 case 0: case 1: case 2: case 3: case 4:
421 $val = self
::msg( $tag, $val );
424 /* If not recognized, display as is. */
431 case 0: case 1: case 2:
432 $val = self
::msg( $tag, $val );
435 /* If not recognized, display as is. */
442 case 0: case 1: case 2:
443 $val = self
::msg( $tag, $val );
446 /* If not recognized, display as is. */
453 case 0: case 1: case 2:
454 $val = self
::msg( $tag, $val );
457 /* If not recognized, display as is. */
462 case 'SubjectDistanceRange':
464 case 0: case 1: case 2: case 3:
465 $val = self
::msg( $tag, $val );
468 /* If not recognized, display as is. */
473 //The GPS...Ref values are kept for compatibility, probably won't be reached.
474 case 'GPSLatitudeRef':
475 case 'GPSDestLatitudeRef':
478 $val = self
::msg( 'GPSLatitude', $val );
481 /* If not recognized, display as is. */
486 case 'GPSLongitudeRef':
487 case 'GPSDestLongitudeRef':
490 $val = self
::msg( 'GPSLongitude', $val );
493 /* If not recognized, display as is. */
500 $val = self
::msg( 'GPSAltitude', 'below-sealevel', self
::formatNum( -$val, 3 ) );
502 $val = self
::msg( 'GPSAltitude', 'above-sealevel', self
::formatNum( $val, 3 ) );
509 $val = self
::msg( $tag, $val );
512 /* If not recognized, display as is. */
517 case 'GPSMeasureMode':
520 $val = self
::msg( $tag, $val );
523 /* If not recognized, display as is. */
529 case 'GPSImgDirectionRef':
530 case 'GPSDestBearingRef':
533 $val = self
::msg( 'GPSDirection', $val );
536 /* If not recognized, display as is. */
542 case 'GPSDestLatitude':
543 $val = self
::formatCoords( $val, 'latitude' );
546 case 'GPSDestLongitude':
547 $val = self
::formatCoords( $val, 'longitude' );
552 case 'K': case 'M': case 'N':
553 $val = self
::msg( 'GPSSpeed', $val );
556 /* If not recognized, display as is. */
561 case 'GPSDestDistanceRef':
563 case 'K': case 'M': case 'N':
564 $val = self
::msg( 'GPSDestDistance', $val );
567 /* If not recognized, display as is. */
573 // See http://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
575 $val = self
::msg( $tag, 'excellent', self
::formatNum( $val ) );
576 } elseif ( $val <= 5 ) {
577 $val = self
::msg( $tag, 'good', self
::formatNum( $val ) );
578 } elseif ( $val <= 10 ) {
579 $val = self
::msg( $tag, 'moderate', self
::formatNum( $val ) );
580 } elseif ( $val <= 20 ) {
581 $val = self
::msg( $tag, 'fair', self
::formatNum( $val ) );
583 $val = self
::msg( $tag, 'poor', self
::formatNum( $val ) );
587 // This is not in the Exif standard, just a special
588 // case for our purposes which enables wikis to wikify
589 // the make, model and software name to link to their articles.
592 $val = self
::msg( $tag, '', $val );
596 if ( is_array( $val ) ) {
597 //if its a software, version array.
598 $val = wfMessage( 'exif-software-version-value', $val[0], $val[1] )->text();
600 $val = self
::msg( $tag, '', $val );
605 // Show the pretty fraction as well as decimal version
606 $val = wfMessage( 'exif-exposuretime-format',
607 self
::formatFraction( $val ), self
::formatNum( $val ) )->text();
609 case 'ISOSpeedRatings':
610 // If its = 65535 that means its at the
611 // limit of the size of Exif::short and
613 if ( $val == '65535' ) {
614 $val = self
::msg( $tag, 'overflow' );
616 $val = self
::formatNum( $val );
620 $val = wfMessage( 'exif-fnumber-format',
621 self
::formatNum( $val ) )->text();
624 case 'FocalLength': case 'FocalLengthIn35mmFilm':
625 $val = wfMessage( 'exif-focallength-format',
626 self
::formatNum( $val ) )->text();
629 case 'MaxApertureValue':
630 if ( strpos( $val, '/' ) !== false ) {
631 // need to expand this earlier to calculate fNumber
632 list( $n, $d ) = explode( '/', $val );
633 if ( is_numeric( $n ) && is_numeric( $d ) ) {
637 if ( is_numeric( $val ) ) {
638 $fNumber = pow( 2, $val / 2 );
639 if ( $fNumber !== false ) {
640 $val = wfMessage( 'exif-maxaperturevalue-value',
641 self
::formatNum( $val ),
642 self
::formatNum( $fNumber, 2 )
649 switch ( strtolower( $val ) ) {
650 // See pg 29 of IPTC photo
651 // metadata standard.
652 case 'ace': case 'clj':
653 case 'dis': case 'fin':
654 case 'edu': case 'evn':
655 case 'hth': case 'hum':
656 case 'lab': case 'lif':
657 case 'pol': case 'rel':
658 case 'sci': case 'soi':
659 case 'spo': case 'war':
667 case 'SubjectNewsCode':
668 // Essentially like iimCategory.
669 // 8 (numeric) digit hierarchical
670 // classification. We decode the
671 // first 2 digits, which provide
673 $val = self
::convertNewsCode( $val );
676 // 1-8 with 1 being highest, 5 normal
677 // 0 is reserved, and 9 is 'user-defined'.
679 if ( $val == 0 ||
$val == 9 ) {
681 } elseif ( $val < 5 && $val > 1 ) {
683 } elseif ( $val == 5 ) {
685 } elseif ( $val <= 8 && $val > 5 ) {
689 if ( $urgency !== '' ) {
690 $val = self
::msg( 'urgency',
696 // Things that have a unit of pixels.
697 case 'OriginalImageHeight':
698 case 'OriginalImageWidth':
699 case 'PixelXDimension':
700 case 'PixelYDimension':
703 $val = self
::formatNum( $val ) . ' ' . wfMessage( 'unit-pixel' )->text();
706 // Do not transform fields with pure text.
707 // For some languages the formatNum()
708 // conversion results to wrong output like
709 // foo,bar@example,com or foo٫bar@example٫com.
710 // Also some 'numeric' things like Scene codes
711 // are included here as we really don't want
713 case 'ImageDescription':
716 case 'RelatedSoundFile':
717 case 'ImageUniqueID':
718 case 'SpectralSensitivity':
719 case 'GPSSatellites':
723 case 'WorldRegionDest':
725 case 'CountryCodeDest':
726 case 'ProvinceOrStateDest':
728 case 'SublocationDest':
729 case 'WorldRegionCreated':
730 case 'CountryCreated':
731 case 'CountryCodeCreated':
732 case 'ProvinceOrStateCreated':
734 case 'SublocationCreated':
736 case 'SpecialInstructions':
741 case 'FixtureIdentifier':
743 case 'LocationDestCode':
745 case 'JPEGFileComment':
746 case 'iimSupplementalCategory':
747 case 'OriginalTransmissionRef':
749 case 'dc-contributor':
758 case 'CameraOwnerName':
761 case 'RightsCertificate':
762 case 'CopyrightOwner':
765 case 'OriginalDocumentID':
767 case 'MorePermissionsUrl':
768 case 'AttributionUrl':
769 case 'PreferredAttributionName':
770 case 'PNGFileComment':
772 case 'ContentWarning':
773 case 'GIFFileComment':
775 case 'IntellectualGenre':
777 case 'OrginisationInImage':
778 case 'PersonInImage':
780 $val = htmlspecialchars( $val );
785 case 'a': case 'p': case 'b':
786 $val = self
::msg( $tag, $val );
789 $val = htmlspecialchars( $val );
795 case 'True': case 'False':
796 $val = self
::msg( $tag, $val );
801 if ( $val == '-1' ) {
802 $val = self
::msg( $tag, 'rejected' );
804 $val = self
::formatNum( $val );
809 $lang = Language
::fetchLanguageName( strtolower( $val ), $wgLang->getCode() );
811 $val = htmlspecialchars( $lang );
813 $val = htmlspecialchars( $val );
818 $val = self
::formatNum( $val );
822 // End formatting values, start flattening arrays.
823 $vals = self
::flattenArray( $vals, $type );
830 * A function to collapse multivalued tags into a single value.
831 * This turns an array of (for example) authors into a bulleted list.
833 * This is public on the basis it might be useful outside of this class.
835 * @param array $vals array of values
836 * @param string $type Type of array (either lang, ul, ol).
837 * lang = language assoc array with keys being the lang code
838 * ul = unordered list, ol = ordered list
839 * type can also come from the '_type' member of $vals.
840 * @param $noHtml Boolean If to avoid returning anything resembling
841 * html. (Ugly hack for backwards compatibility with old mediawiki).
842 * @return String single value (in wiki-syntax).
844 public static function flattenArray( $vals, $type = 'ul', $noHtml = false ) {
845 if ( isset( $vals['_type'] ) ) {
846 $type = $vals['_type'];
847 unset( $vals['_type'] );
850 if ( !is_array( $vals ) ) {
851 return $vals; // do nothing if not an array;
853 elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
856 elseif ( count( $vals ) === 0 ) {
857 wfDebug( __METHOD__
. " metadata array with 0 elements!\n" );
858 return ""; // paranoia. This should never happen
860 /* @todo FIXME: This should hide some of the list entries if there are
861 * say more than four. Especially if a field is translated into 20
862 * languages, we don't want to show them all by default
868 // Display default, followed by ContLang,
869 // followed by the rest in no particular
872 // Todo: hide some items if really long list.
876 $cLang = $wgContLang->getCode();
877 $defaultItem = false;
878 $defaultLang = false;
880 // If default is set, save it for later,
881 // as we don't know if it's equal to
882 // one of the lang codes. (In xmp
883 // you specify the language for a
884 // default property by having both
885 // a default prop, and one in the language
886 // that are identical)
887 if ( isset( $vals['x-default'] ) ) {
888 $defaultItem = $vals['x-default'];
889 unset( $vals['x-default'] );
891 // Do contentLanguage.
892 if ( isset( $vals[$cLang] ) ) {
894 if ( $vals[$cLang] === $defaultItem ) {
895 $defaultItem = false;
898 $content .= self
::langItem(
899 $vals[$cLang], $cLang,
900 $isDefault, $noHtml );
902 unset( $vals[$cLang] );
906 foreach ( $vals as $lang => $item ) {
907 if ( $item === $defaultItem ) {
908 $defaultLang = $lang;
911 $content .= self
::langItem( $item,
912 $lang, false, $noHtml );
914 if ( $defaultItem !== false ) {
915 $content = self
::langItem( $defaultItem,
916 $defaultLang, true, $noHtml ) .
922 return '<ul class="metadata-langlist">' .
927 return "\n#" . implode( "\n#", $vals );
929 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
933 return "\n*" . implode( "\n*", $vals );
935 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
940 /** Helper function for creating lists of translations.
942 * @param string $value value (this is not escaped)
943 * @param string $lang lang code of item or false
944 * @param $default Boolean if it is default value.
945 * @param $noHtml Boolean If to avoid html (for back-compat)
946 * @throws MWException
947 * @return string language item (Note: despite how this looks,
948 * this is treated as wikitext not html).
950 private static function langItem( $value, $lang, $default = false, $noHtml = false ) {
951 if ( $lang === false && $default === false ) {
952 throw new MWException( '$lang and $default cannot both '
957 $wrappedValue = $value;
959 $wrappedValue = '<span class="mw-metadata-lang-value">'
960 . $value . '</span>';
963 if ( $lang === false ) {
965 return wfMessage( 'metadata-langitem-default',
966 $wrappedValue )->text() . "\n\n";
968 return '<li class="mw-metadata-lang-default">'
969 . wfMessage( 'metadata-langitem-default',
970 $wrappedValue )->text()
974 $lowLang = strtolower( $lang );
975 $langName = Language
::fetchLanguageName( $lowLang );
976 if ( $langName === '' ) {
977 //try just the base language name. (aka en-US -> en ).
978 list( $langPrefix ) = explode( '-', $lowLang, 2 );
979 $langName = Language
::fetchLanguageName( $langPrefix );
980 if ( $langName === '' ) {
985 // else we have a language specified
988 return '*' . wfMessage( 'metadata-langitem',
989 $wrappedValue, $langName, $lang )->text();
992 $item = '<li class="mw-metadata-lang-code-'
995 $item .= ' mw-metadata-lang-default';
997 $item .= '" lang="' . $lang . '">';
998 $item .= wfMessage( 'metadata-langitem',
999 $wrappedValue, $langName, $lang )->text();
1005 * Convenience function for getFormattedData()
1009 * @param string $tag the tag name to pass on
1010 * @param string $val the value of the tag
1011 * @param string $arg an argument to pass ($1)
1012 * @param string $arg2 a 2nd argument to pass ($2)
1013 * @return string A wfMessage of "exif-$tag-$val" in lower case
1015 static function msg( $tag, $val, $arg = null, $arg2 = null ) {
1018 if ( $val === '' ) {
1021 return wfMessage( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 )->text();
1025 * Format a number, convert numbers from fractions into floating point
1026 * numbers, joins arrays of numbers with commas.
1028 * @param $num Mixed: the value to format
1029 * @param $round float|int|bool digits to round to or false.
1030 * @return mixed A floating point number or whatever we were fed
1032 static function formatNum( $num, $round = false ) {
1035 if ( is_array( $num ) ) {
1037 foreach ( $num as $number ) {
1038 $out[] = self
::formatNum( $number );
1040 return $wgLang->commaList( $out );
1042 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1044 $newNum = $m[1] / $m[2];
1045 if ( $round !== false ) {
1046 $newNum = round( $newNum, $round );
1052 return $wgLang->formatNum( $newNum );
1054 if ( is_numeric( $num ) && $round !== false ) {
1055 $num = round( $num, $round );
1057 return $wgLang->formatNum( $num );
1062 * Format a rational number, reducing fractions
1066 * @param $num Mixed: the value to format
1067 * @return mixed A floating point number or whatever we were fed
1069 static function formatFraction( $num ) {
1071 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1072 $numerator = intval( $m[1] );
1073 $denominator = intval( $m[2] );
1074 $gcd = self
::gcd( abs( $numerator ), $denominator );
1076 // 0 shouldn't happen! ;)
1077 return self
::formatNum( $numerator / $gcd ) . '/' . self
::formatNum( $denominator / $gcd );
1080 return self
::formatNum( $num );
1084 * Calculate the greatest common divisor of two integers.
1086 * @param $a Integer: Numerator
1087 * @param $b Integer: Denominator
1091 static function gcd( $a, $b ) {
1093 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1094 // Recursive form would be:
1098 return gcd( $b, $a % $b );
1101 $remainder = $a %
$b;
1103 // tail recursion...
1111 * Fetch the human readable version of a news code.
1112 * A news code is an 8 digit code. The first two
1113 * digits are a general classification, so we just
1116 * Note, leading 0's are significant, so this is
1117 * a string, not an int.
1119 * @param string $val The 8 digit news code.
1120 * @return string The human readable form
1122 private static function convertNewsCode( $val ) {
1123 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1124 // Not a valid news code.
1128 switch ( substr( $val, 0, 2 ) ) {
1181 if ( $cat !== '' ) {
1182 $catMsg = self
::msg( 'iimcategory', $cat );
1183 $val = self
::msg( 'subjectnewscode', '', $val, $catMsg );
1189 * Format a coordinate value, convert numbers from floating point
1190 * into degree minute second representation.
1192 * @param int $coord degrees, minutes and seconds
1193 * @param string $type latitude or longitude (for if its a NWS or E)
1194 * @return mixed A floating point number or whatever we were fed
1196 static function formatCoords( $coord, $type ) {
1200 if ( $type === 'latitude' ) {
1202 } elseif ( $type === 'longitude' ) {
1207 if ( $type === 'latitude' ) {
1209 } elseif ( $type === 'longitude' ) {
1214 $deg = floor( $nCoord );
1215 $min = floor( ( $nCoord - $deg ) * 60.0 );
1216 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1218 $deg = self
::formatNum( $deg );
1219 $min = self
::formatNum( $min );
1220 $sec = self
::formatNum( $sec );
1222 return wfMessage( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord )->text();
1226 * Format the contact info field into a single value.
1228 * @param array $vals array with fields of the ContactInfo
1229 * struct defined in the IPTC4XMP spec. Or potentially
1230 * an array with one element that is a free form text
1231 * value from the older iptc iim 1:118 prop.
1233 * This function might be called from
1234 * JpegHandler::convertMetadataVersion which is why it is
1237 * @return String of html-ish looking wikitext
1239 public static function collapseContactInfo( $vals ) {
1240 if ( !( isset( $vals['CiAdrExtadr'] )
1241 ||
isset( $vals['CiAdrCity'] )
1242 ||
isset( $vals['CiAdrCtry'] )
1243 ||
isset( $vals['CiEmailWork'] )
1244 ||
isset( $vals['CiTelWork'] )
1245 ||
isset( $vals['CiAdrPcode'] )
1246 ||
isset( $vals['CiAdrRegion'] )
1247 ||
isset( $vals['CiUrlWork'] )
1249 // We don't have any sub-properties
1250 // This could happen if its using old
1251 // iptc that just had this as a free-form
1253 // Note: We run this through htmlspecialchars
1254 // partially to be consistent, and partially
1255 // because people often insert >, etc into
1256 // the metadata which should not be interpreted
1257 // but we still want to auto-link urls.
1258 foreach ( $vals as &$val ) {
1259 $val = htmlspecialchars( $val );
1261 return self
::flattenArray( $vals );
1263 // We have a real ContactInfo field.
1264 // Its unclear if all these fields have to be
1265 // set, so assume they do not.
1266 $url = $tel = $street = $city = $country = '';
1267 $email = $postal = $region = '';
1269 // Also note, some of the class names this uses
1270 // are similar to those used by hCard. This is
1271 // mostly because they're sensible names. This
1272 // does not (and does not attempt to) output
1273 // stuff in the hCard microformat. However it
1274 // might output in the adr microformat.
1276 if ( isset( $vals['CiAdrExtadr'] ) ) {
1277 // Todo: This can potentially be multi-line.
1278 // Need to check how that works in XMP.
1279 $street = '<span class="extended-address">'
1281 $vals['CiAdrExtadr'] )
1284 if ( isset( $vals['CiAdrCity'] ) ) {
1285 $city = '<span class="locality">'
1286 . htmlspecialchars( $vals['CiAdrCity'] )
1289 if ( isset( $vals['CiAdrCtry'] ) ) {
1290 $country = '<span class="country-name">'
1291 . htmlspecialchars( $vals['CiAdrCtry'] )
1294 if ( isset( $vals['CiEmailWork'] ) ) {
1296 // Have to split multiple emails at commas/new lines.
1297 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1298 foreach ( $splitEmails as $e1 ) {
1299 // Also split on comma
1300 foreach ( explode( ',', $e1 ) as $e2 ) {
1301 $finalEmail = trim( $e2 );
1302 if ( $finalEmail == ',' ||
$finalEmail == '' ) {
1305 if ( strpos( $finalEmail, '<' ) !== false ) {
1306 // Don't do fancy formatting to
1307 // "My name" <foo@bar.com> style stuff
1308 $emails[] = $finalEmail;
1310 $emails[] = '[mailto:'
1312 . ' <span class="email">'
1318 $email = implode( ', ', $emails );
1320 if ( isset( $vals['CiTelWork'] ) ) {
1321 $tel = '<span class="tel">'
1322 . htmlspecialchars( $vals['CiTelWork'] )
1325 if ( isset( $vals['CiAdrPcode'] ) ) {
1326 $postal = '<span class="postal-code">'
1328 $vals['CiAdrPcode'] )
1331 if ( isset( $vals['CiAdrRegion'] ) ) {
1332 // Note this is province/state.
1333 $region = '<span class="region">'
1335 $vals['CiAdrRegion'] )
1338 if ( isset( $vals['CiUrlWork'] ) ) {
1339 $url = '<span class="url">'
1340 . htmlspecialchars( $vals['CiUrlWork'] )
1343 return wfMessage( 'exif-contact-value', $email, $url,
1344 $street, $city, $region, $postal, $country,
1350 /** For compatability with old FormatExif class
1351 * which some extensions use.
1353 * @deprecated since 1.18
1360 * @param $meta array
1362 function FormatExif( $meta ) {
1363 wfDeprecated( __METHOD__
, '1.18' );
1364 $this->meta
= $meta;
1370 function getFormattedData() {
1371 return FormatMetadata
::getFormattedData( $this->meta
);