7109f426f17a8940028ed3025e703c775e37577d
3 * Formating 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
30 * Format Image metadata values into a human readable form.
32 * Note lots of these messages use the prefix 'exif' even though
33 * they may not be exif properties. For example 'exif-ImageDescription'
34 * can be the Exif ImageDescription, or it could be the iptc-iim caption
35 * property, or it could be the xmp dc:description property. This
36 * is because these messages should be independent of how the data is
37 * stored, sine the user doesn't care if the description is stored in xmp,
38 * exif, etc only that its a description. (Additionally many of these properties
39 * are merged together following the MWG standard, such that for example,
40 * exif properties override XMP properties that mean the same thing if
41 * there is a conflict).
43 * It should perhaps use a prefix like 'metadata' instead, but there
44 * is already a large number of messages using the 'exif' prefix.
48 class FormatMetadata
{
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 * @param $tags Array: the Exif data to format ( as returned by
57 * Exif::getFilteredData() or BitmapMetadataHandler )
60 public static function getFormattedData( $tags ) {
63 $resolutionunit = !isset( $tags['ResolutionUnit'] ) ||
$tags['ResolutionUnit'] == 2 ?
2 : 3;
64 unset( $tags['ResolutionUnit'] );
66 foreach ( $tags as $tag => &$vals ) {
68 // This seems ugly to wrap non-array's in an array just to unwrap again,
69 // especially when most of the time it is not an array
70 if ( !is_array( $tags[$tag] ) ) {
71 $vals = Array( $vals );
74 // _type is a special value to say what array type
75 if ( isset( $tags[$tag]['_type'] ) ) {
76 $type = $tags[$tag]['_type'];
77 unset( $vals['_type'] );
79 $type = 'ul'; // default unordered list.
82 //This is done differently as the tag is an array.
83 if ( $tag == 'GPSTimeStamp' && count( $vals ) === 3) {
86 $h = explode( '/', $vals[0] );
87 $m = explode( '/', $vals[1] );
88 $s = explode( '/', $vals[2] );
90 // this should already be validated
91 // when loaded from file, but it could
92 // come from a foreign repo, so be
103 $tags[$tag] = str_pad( intval( $h[0] / $h[1] ), 2, '0', STR_PAD_LEFT
)
104 . ':' . str_pad( intval( $m[0] / $m[1] ), 2, '0', STR_PAD_LEFT
)
105 . ':' . str_pad( intval( $s[0] / $s[1] ), 2, '0', STR_PAD_LEFT
);
108 $time = wfTimestamp( TS_MW
, '1971:01:01 ' . $tags[$tag] );
109 // the 1971:01:01 is just a placeholder, and not shown to user.
110 if ( $time && intval( $time ) > 0 ) {
111 $tags[$tag] = $wgLang->time( $time );
113 } catch ( TimestampException
$e ) {
114 // This shouldn't happen, but we've seen bad formats
115 // such as 4-digit seconds in the wild.
116 // leave $tags[$tag] as-is
121 // The contact info is a multi-valued field
122 // instead of the other props which are single
123 // valued (mostly) so handle as a special case.
124 if ( $tag === 'Contact' ) {
125 $vals = self
::collapseContactInfo( $vals );
129 foreach ( $vals as &$val ) {
134 case 1: case 2: case 3: case 4:
135 case 5: case 6: case 7: case 8:
136 case 32773: case 32946: case 34712:
137 $val = self
::msg( $tag, $val );
140 /* If not recognized, display as is. */
145 case 'PhotometricInterpretation':
148 $val = self
::msg( $tag, $val );
151 /* If not recognized, display as is. */
158 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
159 $val = self
::msg( $tag, $val );
162 /* If not recognized, display as is. */
167 case 'PlanarConfiguration':
170 $val = self
::msg( $tag, $val );
173 /* If not recognized, display as is. */
178 // TODO: YCbCrSubSampling
179 case 'YCbCrPositioning':
183 $val = self
::msg( $tag, $val );
186 /* If not recognized, display as is. */
193 switch( $resolutionunit ) {
195 $val = self
::msg( 'XYResolution', 'i', self
::formatNum( $val ) );
198 $val = self
::msg( 'XYResolution', 'c', self
::formatNum( $val ) );
201 /* If not recognized, display as is. */
206 // TODO: YCbCrCoefficients #p27 (see annex E)
207 case 'ExifVersion': case 'FlashpixVersion':
214 $val = self
::msg( $tag, $val );
217 /* If not recognized, display as is. */
222 case 'ComponentsConfiguration':
224 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
225 $val = self
::msg( $tag, $val );
228 /* If not recognized, display as is. */
234 case 'DateTimeOriginal':
235 case 'DateTimeDigitized':
236 case 'DateTimeReleased':
237 case 'DateTimeExpires':
240 case 'DateTimeMetadata':
241 if ( $val == '0000:00:00 00:00:00' ||
$val == ' : : : : ' ) {
242 $val = wfMessage( 'exif-unknowndate' )->text();
243 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
245 $time = wfTimestamp( TS_MW
, $val );
246 if ( $time && intval( $time ) > 0 ) {
247 $val = $wgLang->timeanddate( $time );
249 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
250 // No second field. Still format the same
251 // since timeanddate doesn't include seconds anyways,
252 // but second still available in api
253 $time = wfTimestamp( TS_MW
, $val . ':00' );
254 if ( $time && intval( $time ) > 0 ) {
255 $val = $wgLang->timeanddate( $time );
257 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
258 // If only the date but not the time is filled in.
259 $time = wfTimestamp( TS_MW
, substr( $val, 0, 4 )
260 . substr( $val, 5, 2 )
261 . substr( $val, 8, 2 )
263 if ( $time && intval( $time ) > 0 ) {
264 $val = $wgLang->date( $time );
267 // else it will just output $val without formatting it.
270 case 'ExposureProgram':
272 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
273 $val = self
::msg( $tag, $val );
276 /* If not recognized, display as is. */
281 case 'SubjectDistance':
282 $val = self
::msg( $tag, '', self
::formatNum( $val ) );
287 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
288 $val = self
::msg( $tag, $val );
291 /* If not recognized, display as is. */
298 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
299 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
300 case 21: case 22: case 23: case 24: case 255:
301 $val = self
::msg( $tag, $val );
304 /* If not recognized, display as is. */
310 $flashDecode = array(
311 'fired' => $val & bindec( '00000001' ),
312 'return' => ( $val & bindec( '00000110' ) ) >> 1,
313 'mode' => ( $val & bindec( '00011000' ) ) >> 3,
314 'function' => ( $val & bindec( '00100000' ) ) >> 5,
315 'redeye' => ( $val & bindec( '01000000' ) ) >> 6,
316 // 'reserved' => ($val & bindec( '10000000' )) >> 7,
318 $flashMsgs = array();
319 # We do not need to handle unknown values since all are used.
320 foreach ( $flashDecode as $subTag => $subValue ) {
321 # We do not need any message for zeroed values.
322 if ( $subTag != 'fired' && $subValue == 0 ) {
325 $fullTag = $tag . '-' . $subTag ;
326 $flashMsgs[] = self
::msg( $fullTag, $subValue );
328 $val = $wgLang->commaList( $flashMsgs );
331 case 'FocalPlaneResolutionUnit':
334 $val = self
::msg( $tag, $val );
337 /* If not recognized, display as is. */
342 case 'SensingMethod':
344 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
345 $val = self
::msg( $tag, $val );
348 /* If not recognized, display as is. */
356 $val = self
::msg( $tag, $val );
359 /* If not recognized, display as is. */
367 $val = self
::msg( $tag, $val );
370 /* If not recognized, display as is. */
375 case 'CustomRendered':
378 $val = self
::msg( $tag, $val );
381 /* If not recognized, display as is. */
388 case 0: case 1: case 2:
389 $val = self
::msg( $tag, $val );
392 /* If not recognized, display as is. */
400 $val = self
::msg( $tag, $val );
403 /* If not recognized, display as is. */
408 case 'SceneCaptureType':
410 case 0: case 1: case 2: case 3:
411 $val = self
::msg( $tag, $val );
414 /* If not recognized, display as is. */
421 case 0: case 1: case 2: case 3: case 4:
422 $val = self
::msg( $tag, $val );
425 /* If not recognized, display as is. */
432 case 0: case 1: case 2:
433 $val = self
::msg( $tag, $val );
436 /* If not recognized, display as is. */
443 case 0: case 1: case 2:
444 $val = self
::msg( $tag, $val );
447 /* If not recognized, display as is. */
454 case 0: case 1: case 2:
455 $val = self
::msg( $tag, $val );
458 /* If not recognized, display as is. */
463 case 'SubjectDistanceRange':
465 case 0: case 1: case 2: case 3:
466 $val = self
::msg( $tag, $val );
469 /* If not recognized, display as is. */
474 //The GPS...Ref values are kept for compatibility, probably won't be reached.
475 case 'GPSLatitudeRef':
476 case 'GPSDestLatitudeRef':
479 $val = self
::msg( 'GPSLatitude', $val );
482 /* If not recognized, display as is. */
487 case 'GPSLongitudeRef':
488 case 'GPSDestLongitudeRef':
491 $val = self
::msg( 'GPSLongitude', $val );
494 /* If not recognized, display as is. */
501 $val = self
::msg( 'GPSAltitude', 'below-sealevel', self
::formatNum( -$val, 3 ) );
503 $val = self
::msg( 'GPSAltitude', 'above-sealevel', self
::formatNum( $val, 3 ) );
510 $val = self
::msg( $tag, $val );
513 /* If not recognized, display as is. */
518 case 'GPSMeasureMode':
521 $val = self
::msg( $tag, $val );
524 /* If not recognized, display as is. */
531 case 'GPSImgDirectionRef':
532 case 'GPSDestBearingRef':
535 $val = self
::msg( 'GPSDirection', $val );
538 /* If not recognized, display as is. */
544 case 'GPSDestLatitude':
545 $val = self
::formatCoords( $val, 'latitude' );
548 case 'GPSDestLongitude':
549 $val = self
::formatCoords( $val, 'longitude' );
554 case 'K': case 'M': case 'N':
555 $val = self
::msg( 'GPSSpeed', $val );
558 /* If not recognized, display as is. */
563 case 'GPSDestDistanceRef':
565 case 'K': case 'M': case 'N':
566 $val = self
::msg( 'GPSDestDistance', $val );
569 /* If not recognized, display as is. */
575 // See http://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
577 $val = self
::msg( $tag, 'excellent', self
::formatNum( $val ) );
578 } elseif ( $val <= 5 ) {
579 $val = self
::msg( $tag, 'good', self
::formatNum( $val ) );
580 } elseif ( $val <= 10 ) {
581 $val = self
::msg( $tag, 'moderate', self
::formatNum( $val ) );
582 } elseif ( $val <= 20 ) {
583 $val = self
::msg( $tag, 'fair', self
::formatNum( $val ) );
585 $val = self
::msg( $tag, 'poor', self
::formatNum( $val ) );
589 // This is not in the Exif standard, just a special
590 // case for our purposes which enables wikis to wikify
591 // the make, model and software name to link to their articles.
594 $val = self
::msg( $tag, '', $val );
598 if ( is_array( $val ) ) {
599 //if its a software, version array.
600 $val = wfMessage( 'exif-software-version-value', $val[0], $val[1] )->text();
602 $val = self
::msg( $tag, '', $val );
607 // Show the pretty fraction as well as decimal version
608 $val = wfMessage( 'exif-exposuretime-format',
609 self
::formatFraction( $val ), self
::formatNum( $val ) )->text();
611 case 'ISOSpeedRatings':
612 // If its = 65535 that means its at the
613 // limit of the size of Exif::short and
615 if ( $val == '65535' ) {
616 $val = self
::msg( $tag, 'overflow' );
618 $val = self
::formatNum( $val );
622 $val = wfMessage( 'exif-fnumber-format',
623 self
::formatNum( $val ) )->text();
626 case 'FocalLength': case 'FocalLengthIn35mmFilm':
627 $val = wfMessage( 'exif-focallength-format',
628 self
::formatNum( $val ) )->text();
631 case 'MaxApertureValue':
632 if ( strpos( $val, '/' ) !== false ) {
633 // need to expand this earlier to calculate fNumber
634 list( $n, $d ) = explode( '/', $val );
635 if ( is_numeric( $n ) && is_numeric( $d ) ) {
639 if ( is_numeric( $val ) ) {
640 $fNumber = pow( 2, $val / 2 );
641 if ( $fNumber !== false ) {
642 $val = wfMessage( 'exif-maxaperturevalue-value',
643 self
::formatNum( $val ),
644 self
::formatNum( $fNumber, 2 )
651 switch( strtolower( $val ) ) {
652 // See pg 29 of IPTC photo
653 // metadata standard.
654 case 'ace': case 'clj':
655 case 'dis': case 'fin':
656 case 'edu': case 'evn':
657 case 'hth': case 'hum':
658 case 'lab': case 'lif':
659 case 'pol': case 'rel':
660 case 'sci': case 'soi':
661 case 'spo': case 'war':
669 case 'SubjectNewsCode':
670 // Essentially like iimCategory.
671 // 8 (numeric) digit hierarchical
672 // classification. We decode the
673 // first 2 digits, which provide
675 $val = self
::convertNewsCode( $val );
678 // 1-8 with 1 being highest, 5 normal
679 // 0 is reserved, and 9 is 'user-defined'.
681 if ( $val == 0 ||
$val == 9 ) {
683 } elseif ( $val < 5 && $val > 1 ) {
685 } elseif ( $val == 5 ) {
687 } elseif ( $val <= 8 && $val > 5) {
691 if ( $urgency !== '' ) {
692 $val = self
::msg( 'urgency',
698 // Things that have a unit of pixels.
699 case 'OriginalImageHeight':
700 case 'OriginalImageWidth':
701 case 'PixelXDimension':
702 case 'PixelYDimension':
705 $val = self
::formatNum( $val ) . ' ' . wfMessage( 'unit-pixel' )->text();
708 // Do not transform fields with pure text.
709 // For some languages the formatNum()
710 // conversion results to wrong output like
711 // foo,bar@example,com or foo٫bar@example٫com.
712 // Also some 'numeric' things like Scene codes
713 // are included here as we really don't want
715 case 'ImageDescription':
718 case 'RelatedSoundFile':
719 case 'ImageUniqueID':
720 case 'SpectralSensitivity':
721 case 'GPSSatellites':
725 case 'WorldRegionDest':
727 case 'CountryCodeDest':
728 case 'ProvinceOrStateDest':
730 case 'SublocationDest':
731 case 'WorldRegionCreated':
732 case 'CountryCreated':
733 case 'CountryCodeCreated':
734 case 'ProvinceOrStateCreated':
736 case 'SublocationCreated':
738 case 'SpecialInstructions':
743 case 'FixtureIdentifier':
745 case 'LocationDestCode':
747 case 'JPEGFileComment':
748 case 'iimSupplementalCategory':
749 case 'OriginalTransmissionRef':
751 case 'dc-contributor':
760 case 'CameraOwnerName':
763 case 'RightsCertificate':
764 case 'CopyrightOwner':
767 case 'OriginalDocumentID':
769 case 'MorePermissionsUrl':
770 case 'AttributionUrl':
771 case 'PreferredAttributionName':
772 case 'PNGFileComment':
774 case 'ContentWarning':
775 case 'GIFFileComment':
777 case 'IntellectualGenre':
779 case 'OrginisationInImage':
780 case 'PersonInImage':
782 $val = htmlspecialchars( $val );
787 case 'a': case 'p': case 'b':
788 $val = self
::msg( $tag, $val );
791 $val = htmlspecialchars( $val );
797 case 'True': case 'False':
798 $val = self
::msg( $tag, $val );
803 if ( $val == '-1' ) {
804 $val = self
::msg( $tag, 'rejected' );
806 $val = self
::formatNum( $val );
811 $lang = Language
::fetchLanguageName( strtolower( $val ), $wgLang->getCode() );
813 $val = htmlspecialchars( $lang );
815 $val = htmlspecialchars( $val );
820 $val = self
::formatNum( $val );
824 // End formatting values, start flattening arrays.
825 $vals = self
::flattenArray( $vals, $type );
832 * A function to collapse multivalued tags into a single value.
833 * This turns an array of (for example) authors into a bulleted list.
835 * This is public on the basis it might be useful outside of this class.
837 * @param $vals Array array of values
838 * @param $type String Type of array (either lang, ul, ol).
839 * lang = language assoc array with keys being the lang code
840 * ul = unordered list, ol = ordered list
841 * type can also come from the '_type' member of $vals.
842 * @param $noHtml Boolean If to avoid returning anything resembling
843 * html. (Ugly hack for backwards compatibility with old mediawiki).
844 * @return String single value (in wiki-syntax).
846 public static function flattenArray( $vals, $type = 'ul', $noHtml = false ) {
847 if ( isset( $vals['_type'] ) ) {
848 $type = $vals['_type'];
849 unset( $vals['_type'] );
852 if ( !is_array( $vals ) ) {
853 return $vals; // do nothing if not an array;
855 elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
858 elseif ( count( $vals ) === 0 ) {
859 wfDebug( __METHOD__
. ' metadata array with 0 elements!' );
860 return ""; // paranoia. This should never happen
862 /* @todo FIXME: This should hide some of the list entries if there are
863 * say more than four. Especially if a field is translated into 20
864 * languages, we don't want to show them all by default
870 // Display default, followed by ContLang,
871 // followed by the rest in no particular
874 // Todo: hide some items if really long list.
878 $cLang = $wgContLang->getCode();
879 $defaultItem = false;
880 $defaultLang = false;
882 // If default is set, save it for later,
883 // as we don't know if it's equal to
884 // one of the lang codes. (In xmp
885 // you specify the language for a
886 // default property by having both
887 // a default prop, and one in the language
888 // that are identical)
889 if ( isset( $vals['x-default'] ) ) {
890 $defaultItem = $vals['x-default'];
891 unset( $vals['x-default'] );
893 // Do contentLanguage.
894 if ( isset( $vals[$cLang] ) ) {
896 if ( $vals[$cLang] === $defaultItem ) {
897 $defaultItem = false;
900 $content .= self
::langItem(
901 $vals[$cLang], $cLang,
902 $isDefault, $noHtml );
904 unset( $vals[$cLang] );
908 foreach ( $vals as $lang => $item ) {
909 if ( $item === $defaultItem ) {
910 $defaultLang = $lang;
913 $content .= self
::langItem( $item,
914 $lang, false, $noHtml );
916 if ( $defaultItem !== false ) {
917 $content = self
::langItem( $defaultItem,
918 $defaultLang, true, $noHtml ) .
924 return '<ul class="metadata-langlist">' .
929 return "\n#" . implode( "\n#", $vals );
931 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
935 return "\n*" . implode( "\n*", $vals );
937 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
942 /** Helper function for creating lists of translations.
944 * @param $value String value (this is not escaped)
945 * @param $lang String lang code of item or false
946 * @param $default Boolean if it is default value.
947 * @param $noHtml Boolean If to avoid html (for back-compat)
948 * @throws MWException
949 * @return string language item (Note: despite how this looks,
950 * this is treated as wikitext not html).
952 private static function langItem( $value, $lang, $default = false, $noHtml = false ) {
953 if ( $lang === false && $default === false) {
954 throw new MWException( '$lang and $default cannot both '
959 $wrappedValue = $value;
961 $wrappedValue = '<span class="mw-metadata-lang-value">'
962 . $value . '</span>';
965 if ( $lang === false ) {
967 return wfMessage( 'metadata-langitem-default',
968 $wrappedValue )->text() . "\n\n";
970 return '<li class="mw-metadata-lang-default">'
971 . wfMessage( 'metadata-langitem-default',
972 $wrappedValue )->text()
976 $lowLang = strtolower( $lang );
977 $langName = Language
::fetchLanguageName( $lowLang );
978 if ( $langName === '' ) {
979 //try just the base language name. (aka en-US -> en ).
980 list( $langPrefix ) = explode( '-', $lowLang, 2 );
981 $langName = Language
::fetchLanguageName( $langPrefix );
982 if ( $langName === '' ) {
987 // else we have a language specified
990 return '*' . wfMessage( 'metadata-langitem',
991 $wrappedValue, $langName, $lang )->text();
994 $item = '<li class="mw-metadata-lang-code-'
997 $item .= ' mw-metadata-lang-default';
999 $item .= '" lang="' . $lang . '">';
1000 $item .= wfMessage( 'metadata-langitem',
1001 $wrappedValue, $langName, $lang )->text();
1007 * Convenience function for getFormattedData()
1011 * @param $tag String: the tag name to pass on
1012 * @param $val String: the value of the tag
1013 * @param $arg String: an argument to pass ($1)
1014 * @param $arg2 String: a 2nd argument to pass ($2)
1015 * @return string A wfMessage of "exif-$tag-$val" in lower case
1017 static function msg( $tag, $val, $arg = null, $arg2 = null ) {
1022 return wfMessage( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 )->text();
1026 * Format a number, convert numbers from fractions into floating point
1027 * numbers, joins arrays of numbers with commas.
1029 * @param $num Mixed: the value to format
1030 * @param $round float|int|bool digits to round to or false.
1031 * @return mixed A floating point number or whatever we were fed
1033 static function formatNum( $num, $round = false ) {
1036 if( is_array( $num ) ) {
1038 foreach( $num as $number ) {
1039 $out[] = self
::formatNum( $number );
1041 return $wgLang->commaList( $out );
1043 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1045 $newNum = $m[1] / $m[2];
1046 if ( $round !== false ) {
1047 $newNum = round( $newNum, $round );
1053 return $wgLang->formatNum( $newNum );
1055 if ( is_numeric( $num ) && $round !== false ) {
1056 $num = round( $num, $round );
1058 return $wgLang->formatNum( $num );
1063 * Format a rational number, reducing fractions
1067 * @param $num Mixed: the value to format
1068 * @return mixed A floating point number or whatever we were fed
1070 static function formatFraction( $num ) {
1072 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1073 $numerator = intval( $m[1] );
1074 $denominator = intval( $m[2] );
1075 $gcd = self
::gcd( abs( $numerator ), $denominator );
1077 // 0 shouldn't happen! ;)
1078 return self
::formatNum( $numerator / $gcd ) . '/' . self
::formatNum( $denominator / $gcd );
1081 return self
::formatNum( $num );
1085 * Calculate the greatest common divisor of two integers.
1087 * @param $a Integer: Numerator
1088 * @param $b Integer: Denominator
1092 static function gcd( $a, $b ) {
1094 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1095 // Recursive form would be:
1099 return gcd( $b, $a % $b );
1102 $remainder = $a %
$b;
1104 // tail recursion...
1112 * Fetch the human readable version of a news code.
1113 * A news code is an 8 digit code. The first two
1114 * digits are a general classification, so we just
1117 * Note, leading 0's are significant, so this is
1118 * a string, not an int.
1120 * @param $val String: The 8 digit news code.
1121 * @return string The human readable form
1123 private static function convertNewsCode( $val ) {
1124 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1125 // Not a valid news code.
1129 switch( substr( $val, 0, 2 ) ) {
1182 if ( $cat !== '' ) {
1183 $catMsg = self
::msg( 'iimcategory', $cat );
1184 $val = self
::msg( 'subjectnewscode', '', $val, $catMsg );
1190 * Format a coordinate value, convert numbers from floating point
1191 * into degree minute second representation.
1193 * @param $coord int degrees, minutes and seconds
1194 * @param $type String: latitude or longitude (for if its a NWS or E)
1195 * @return mixed A floating point number or whatever we were fed
1197 static function formatCoords( $coord, $type ) {
1201 if ( $type === 'latitude' ) {
1203 } elseif ( $type === 'longitude' ) {
1208 if ( $type === 'latitude' ) {
1210 } elseif ( $type === 'longitude' ) {
1215 $deg = floor( $nCoord );
1216 $min = floor( ( $nCoord - $deg ) * 60.0 );
1217 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1219 $deg = self
::formatNum( $deg );
1220 $min = self
::formatNum( $min );
1221 $sec = self
::formatNum( $sec );
1223 return wfMessage( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord )->text();
1227 * Format the contact info field into a single value.
1229 * @param $vals Array array with fields of the ContactInfo
1230 * struct defined in the IPTC4XMP spec. Or potentially
1231 * an array with one element that is a free form text
1232 * value from the older iptc iim 1:118 prop.
1234 * This function might be called from
1235 * JpegHandler::convertMetadataVersion which is why it is
1238 * @return String of html-ish looking wikitext
1240 public static function collapseContactInfo( $vals ) {
1241 if( !( isset( $vals['CiAdrExtadr'] )
1242 ||
isset( $vals['CiAdrCity'] )
1243 ||
isset( $vals['CiAdrCtry'] )
1244 ||
isset( $vals['CiEmailWork'] )
1245 ||
isset( $vals['CiTelWork'] )
1246 ||
isset( $vals['CiAdrPcode'] )
1247 ||
isset( $vals['CiAdrRegion'] )
1248 ||
isset( $vals['CiUrlWork'] )
1250 // We don't have any sub-properties
1251 // This could happen if its using old
1252 // iptc that just had this as a free-form
1254 // Note: We run this through htmlspecialchars
1255 // partially to be consistent, and partially
1256 // because people often insert >, etc into
1257 // the metadata which should not be interpreted
1258 // but we still want to auto-link urls.
1259 foreach( $vals as &$val ) {
1260 $val = htmlspecialchars( $val );
1262 return self
::flattenArray( $vals );
1264 // We have a real ContactInfo field.
1265 // Its unclear if all these fields have to be
1266 // set, so assume they do not.
1267 $url = $tel = $street = $city = $country = '';
1268 $email = $postal = $region = '';
1270 // Also note, some of the class names this uses
1271 // are similar to those used by hCard. This is
1272 // mostly because they're sensible names. This
1273 // does not (and does not attempt to) output
1274 // stuff in the hCard microformat. However it
1275 // might output in the adr microformat.
1277 if ( isset( $vals['CiAdrExtadr'] ) ) {
1278 // Todo: This can potentially be multi-line.
1279 // Need to check how that works in XMP.
1280 $street = '<span class="extended-address">'
1282 $vals['CiAdrExtadr'] )
1285 if ( isset( $vals['CiAdrCity'] ) ) {
1286 $city = '<span class="locality">'
1287 . htmlspecialchars( $vals['CiAdrCity'] )
1290 if ( isset( $vals['CiAdrCtry'] ) ) {
1291 $country = '<span class="country-name">'
1292 . htmlspecialchars( $vals['CiAdrCtry'] )
1295 if ( isset( $vals['CiEmailWork'] ) ) {
1297 // Have to split multiple emails at commas/new lines.
1298 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1299 foreach ( $splitEmails as $e1 ) {
1300 // Also split on comma
1301 foreach ( explode( ',', $e1 ) as $e2 ) {
1302 $finalEmail = trim( $e2 );
1303 if ( $finalEmail == ',' ||
$finalEmail == '' ) {
1306 if ( strpos( $finalEmail, '<' ) !== false ) {
1307 // Don't do fancy formatting to
1308 // "My name" <foo@bar.com> style stuff
1309 $emails[] = $finalEmail;
1311 $emails[] = '[mailto:'
1313 . ' <span class="email">'
1319 $email = implode( ', ', $emails );
1321 if ( isset( $vals['CiTelWork'] ) ) {
1322 $tel = '<span class="tel">'
1323 . htmlspecialchars( $vals['CiTelWork'] )
1326 if ( isset( $vals['CiAdrPcode'] ) ) {
1327 $postal = '<span class="postal-code">'
1329 $vals['CiAdrPcode'] )
1332 if ( isset( $vals['CiAdrRegion'] ) ) {
1333 // Note this is province/state.
1334 $region = '<span class="region">'
1336 $vals['CiAdrRegion'] )
1339 if ( isset( $vals['CiUrlWork'] ) ) {
1340 $url = '<span class="url">'
1341 . htmlspecialchars( $vals['CiUrlWork'] )
1344 return wfMessage( 'exif-contact-value', $email, $url,
1345 $street, $city, $region, $postal, $country,
1351 /** For compatability with old FormatExif class
1352 * which some extensions use.
1354 * @deprecated since 1.18
1361 * @param $meta array
1363 function FormatExif( $meta ) {
1364 wfDeprecated( __METHOD__
, '1.18' );
1365 $this->meta
= $meta;
1371 function getFormattedData() {
1372 return FormatMetadata
::getFormattedData( $this->meta
);