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] = intval( $h[0] / $h[1] )
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
);
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 );
115 // The contact info is a multi-valued field
116 // instead of the other props which are single
117 // valued (mostly) so handle as a special case.
118 if ( $tag === 'Contact' ) {
119 $vals = self
::collapseContactInfo( $vals );
123 foreach ( $vals as &$val ) {
128 case 1: case 2: case 3: case 4:
129 case 5: case 6: case 7: case 8:
130 case 32773: case 32946: case 34712:
131 $val = self
::msg( $tag, $val );
134 /* If not recognized, display as is. */
139 case 'PhotometricInterpretation':
142 $val = self
::msg( $tag, $val );
145 /* If not recognized, display as is. */
152 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
153 $val = self
::msg( $tag, $val );
156 /* If not recognized, display as is. */
161 case 'PlanarConfiguration':
164 $val = self
::msg( $tag, $val );
167 /* If not recognized, display as is. */
172 // TODO: YCbCrSubSampling
173 case 'YCbCrPositioning':
177 $val = self
::msg( $tag, $val );
180 /* If not recognized, display as is. */
187 switch( $resolutionunit ) {
189 $val = self
::msg( 'XYResolution', 'i', self
::formatNum( $val ) );
192 $val = self
::msg( 'XYResolution', 'c', self
::formatNum( $val ) );
195 /* If not recognized, display as is. */
200 // TODO: YCbCrCoefficients #p27 (see annex E)
201 case 'ExifVersion': case 'FlashpixVersion':
208 $val = self
::msg( $tag, $val );
211 /* If not recognized, display as is. */
216 case 'ComponentsConfiguration':
218 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
219 $val = self
::msg( $tag, $val );
222 /* If not recognized, display as is. */
228 case 'DateTimeOriginal':
229 case 'DateTimeDigitized':
230 case 'DateTimeReleased':
231 case 'DateTimeExpires':
234 case 'DateTimeMetadata':
235 if ( $val == '0000:00:00 00:00:00' ||
$val == ' : : : : ' ) {
236 $val = wfMsg( 'exif-unknowndate' );
237 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
239 $time = wfTimestamp( TS_MW
, $val );
240 if ( $time && intval( $time ) > 0 ) {
241 $val = $wgLang->timeanddate( $time );
243 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
244 // No second field. Still format the same
245 // since timeanddate doesn't include seconds anyways,
246 // but second still available in api
247 $time = wfTimestamp( TS_MW
, $val . ':00' );
248 if ( $time && intval( $time ) > 0 ) {
249 $val = $wgLang->timeanddate( $time );
251 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
252 // If only the date but not the time is filled in.
253 $time = wfTimestamp( TS_MW
, substr( $val, 0, 4 )
254 . substr( $val, 5, 2 )
255 . substr( $val, 8, 2 )
257 if ( $time && intval( $time ) > 0 ) {
258 $val = $wgLang->date( $time );
261 // else it will just output $val without formatting it.
264 case 'ExposureProgram':
266 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
267 $val = self
::msg( $tag, $val );
270 /* If not recognized, display as is. */
275 case 'SubjectDistance':
276 $val = self
::msg( $tag, '', self
::formatNum( $val ) );
281 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
282 $val = self
::msg( $tag, $val );
285 /* If not recognized, display as is. */
292 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
293 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
294 case 21: case 22: case 23: case 24: case 255:
295 $val = self
::msg( $tag, $val );
298 /* If not recognized, display as is. */
304 $flashDecode = array(
305 'fired' => $val & bindec( '00000001' ),
306 'return' => ( $val & bindec( '00000110' ) ) >> 1,
307 'mode' => ( $val & bindec( '00011000' ) ) >> 3,
308 'function' => ( $val & bindec( '00100000' ) ) >> 5,
309 'redeye' => ( $val & bindec( '01000000' ) ) >> 6,
310 // 'reserved' => ($val & bindec( '10000000' )) >> 7,
313 # We do not need to handle unknown values since all are used.
314 foreach ( $flashDecode as $subTag => $subValue ) {
315 # We do not need any message for zeroed values.
316 if ( $subTag != 'fired' && $subValue == 0 ) {
319 $fullTag = $tag . '-' . $subTag ;
320 $flashMsgs[] = self
::msg( $fullTag, $subValue );
322 $val = $wgLang->commaList( $flashMsgs );
325 case 'FocalPlaneResolutionUnit':
328 $val = self
::msg( $tag, $val );
331 /* If not recognized, display as is. */
336 case 'SensingMethod':
338 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
339 $val = self
::msg( $tag, $val );
342 /* If not recognized, display as is. */
350 $val = self
::msg( $tag, $val );
353 /* If not recognized, display as is. */
361 $val = self
::msg( $tag, $val );
364 /* If not recognized, display as is. */
369 case 'CustomRendered':
372 $val = self
::msg( $tag, $val );
375 /* If not recognized, display as is. */
382 case 0: case 1: case 2:
383 $val = self
::msg( $tag, $val );
386 /* If not recognized, display as is. */
394 $val = self
::msg( $tag, $val );
397 /* If not recognized, display as is. */
402 case 'SceneCaptureType':
404 case 0: case 1: case 2: case 3:
405 $val = self
::msg( $tag, $val );
408 /* If not recognized, display as is. */
415 case 0: case 1: case 2: case 3: case 4:
416 $val = self
::msg( $tag, $val );
419 /* If not recognized, display as is. */
426 case 0: case 1: case 2:
427 $val = self
::msg( $tag, $val );
430 /* If not recognized, display as is. */
437 case 0: case 1: case 2:
438 $val = self
::msg( $tag, $val );
441 /* If not recognized, display as is. */
448 case 0: case 1: case 2:
449 $val = self
::msg( $tag, $val );
452 /* If not recognized, display as is. */
457 case 'SubjectDistanceRange':
459 case 0: case 1: case 2: case 3:
460 $val = self
::msg( $tag, $val );
463 /* If not recognized, display as is. */
468 //The GPS...Ref values are kept for compatibility, probably won't be reached.
469 case 'GPSLatitudeRef':
470 case 'GPSDestLatitudeRef':
473 $val = self
::msg( 'GPSLatitude', $val );
476 /* If not recognized, display as is. */
481 case 'GPSLongitudeRef':
482 case 'GPSDestLongitudeRef':
485 $val = self
::msg( 'GPSLongitude', $val );
488 /* If not recognized, display as is. */
495 $val = self
::msg( 'GPSAltitude', 'below-sealevel', self
::formatNum( -$val, 3 ) );
497 $val = self
::msg( 'GPSAltitude', 'above-sealevel', self
::formatNum( $val, 3 ) );
504 $val = self
::msg( $tag, $val );
507 /* If not recognized, display as is. */
512 case 'GPSMeasureMode':
515 $val = self
::msg( $tag, $val );
518 /* If not recognized, display as is. */
525 case 'GPSImgDirectionRef':
526 case 'GPSDestBearingRef':
529 $val = self
::msg( 'GPSDirection', $val );
532 /* If not recognized, display as is. */
538 case 'GPSDestLatitude':
539 $val = self
::formatCoords( $val, 'latitude' );
542 case 'GPSDestLongitude':
543 $val = self
::formatCoords( $val, 'longitude' );
548 case 'K': case 'M': case 'N':
549 $val = self
::msg( 'GPSSpeed', $val );
552 /* If not recognized, display as is. */
557 case 'GPSDestDistanceRef':
559 case 'K': case 'M': case 'N':
560 $val = self
::msg( 'GPSDestDistance', $val );
563 /* If not recognized, display as is. */
569 // See http://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
571 $val = self
::msg( $tag, 'excellent', self
::formatNum( $val ) );
572 } elseif ( $val <= 5 ) {
573 $val = self
::msg( $tag, 'good', self
::formatNum( $val ) );
574 } elseif ( $val <= 10 ) {
575 $val = self
::msg( $tag, 'moderate', self
::formatNum( $val ) );
576 } elseif ( $val <= 20 ) {
577 $val = self
::msg( $tag, 'fair', self
::formatNum( $val ) );
579 $val = self
::msg( $tag, 'poor', self
::formatNum( $val ) );
583 // This is not in the Exif standard, just a special
584 // case for our purposes which enables wikis to wikify
585 // the make, model and software name to link to their articles.
588 $val = self
::msg( $tag, '', $val );
592 if ( is_array( $val ) ) {
593 //if its a software, version array.
594 $val = wfMsg( 'exif-software-version-value', $val[0], $val[1] );
596 $val = self
::msg( $tag, '', $val );
601 // Show the pretty fraction as well as decimal version
602 $val = wfMsg( 'exif-exposuretime-format',
603 self
::formatFraction( $val ), self
::formatNum( $val ) );
605 case 'ISOSpeedRatings':
606 // If its = 65535 that means its at the
607 // limit of the size of Exif::short and
609 if ( $val == '65535' ) {
610 $val = self
::msg( $tag, 'overflow' );
612 $val = self
::formatNum( $val );
616 $val = wfMsg( 'exif-fnumber-format',
617 self
::formatNum( $val ) );
620 case 'FocalLength': case 'FocalLengthIn35mmFilm':
621 $val = wfMsg( 'exif-focallength-format',
622 self
::formatNum( $val ) );
625 case 'MaxApertureValue':
626 if ( strpos( $val, '/' ) !== false ) {
627 // need to expand this earlier to calculate fNumber
628 list($n, $d) = explode('/', $val);
629 if ( is_numeric( $n ) && is_numeric( $d ) ) {
633 if ( is_numeric( $val ) ) {
634 $fNumber = pow( 2, $val / 2 );
635 if ( $fNumber !== false ) {
636 $val = wfMsg( 'exif-maxaperturevalue-value',
637 self
::formatNum( $val ),
638 self
::formatNum( $fNumber, 2 )
645 switch( strtolower( $val ) ) {
646 // See pg 29 of IPTC photo
647 // metadata standard.
648 case 'ace': case 'clj':
649 case 'dis': case 'fin':
650 case 'edu': case 'evn':
651 case 'hth': case 'hum':
652 case 'lab': case 'lif':
653 case 'pol': case 'rel':
654 case 'sci': case 'soi':
655 case 'spo': case 'war':
663 case 'SubjectNewsCode':
664 // Essentially like iimCategory.
665 // 8 (numeric) digit hierarchical
666 // classification. We decode the
667 // first 2 digits, which provide
669 $val = self
::convertNewsCode( $val );
672 // 1-8 with 1 being highest, 5 normal
673 // 0 is reserved, and 9 is 'user-defined'.
675 if ( $val == 0 ||
$val == 9 ) {
677 } elseif ( $val < 5 && $val > 1 ) {
679 } elseif ( $val == 5 ) {
681 } elseif ( $val <= 8 && $val > 5) {
685 if ( $urgency !== '' ) {
686 $val = self
::msg( 'urgency',
692 // Things that have a unit of pixels.
693 case 'OriginalImageHeight':
694 case 'OriginalImageWidth':
695 case 'PixelXDimension':
696 case 'PixelYDimension':
699 $val = self
::formatNum( $val ) . ' ' . wfMsg( 'unit-pixel' );
702 // Do not transform fields with pure text.
703 // For some languages the formatNum()
704 // conversion results to wrong output like
705 // foo,bar@example,com or foo٫bar@example٫com.
706 // Also some 'numeric' things like Scene codes
707 // are included here as we really don't want
709 case 'ImageDescription':
712 case 'RelatedSoundFile':
713 case 'ImageUniqueID':
714 case 'SpectralSensitivity':
715 case 'GPSSatellites':
719 case 'WorldRegionDest':
721 case 'CountryCodeDest':
722 case 'ProvinceOrStateDest':
724 case 'SublocationDest':
725 case 'WorldRegionCreated':
726 case 'CountryCreated':
727 case 'CountryCodeCreated':
728 case 'ProvinceOrStateCreated':
730 case 'SublocationCreated':
732 case 'SpecialInstructions':
737 case 'FixtureIdentifier':
739 case 'LocationDestCode':
741 case 'JPEGFileComment':
742 case 'iimSupplementalCategory':
743 case 'OriginalTransmissionRef':
745 case 'dc-contributor':
754 case 'CameraOwnerName':
757 case 'RightsCertificate':
758 case 'CopyrightOwner':
761 case 'OriginalDocumentID':
763 case 'MorePermissionsUrl':
764 case 'AttributionUrl':
765 case 'PreferredAttributionName':
766 case 'PNGFileComment':
768 case 'ContentWarning':
769 case 'GIFFileComment':
771 case 'IntellectualGenre':
773 case 'OrginisationInImage':
774 case 'PersonInImage':
776 $val = htmlspecialchars( $val );
781 case 'a': case 'p': case 'b':
782 $val = self
::msg( $tag, $val );
785 $val = htmlspecialchars( $val );
791 case 'True': case 'False':
792 $val = self
::msg( $tag, $val );
797 if ( $val == '-1' ) {
798 $val = self
::msg( $tag, 'rejected' );
800 $val = self
::formatNum( $val );
805 $lang = Language
::fetchLanguageName( strtolower( $val ), $wgLang );
807 $val = htmlspecialchars( $lang );
809 $val = htmlspecialchars( $val );
814 $val = self
::formatNum( $val );
818 // End formatting values, start flattening arrays.
819 $vals = self
::flattenArray( $vals, $type );
826 * A function to collapse multivalued tags into a single value.
827 * This turns an array of (for example) authors into a bulleted list.
829 * This is public on the basis it might be useful outside of this class.
831 * @param $vals Array array of values
832 * @param $type String Type of array (either lang, ul, ol).
833 * lang = language assoc array with keys being the lang code
834 * ul = unordered list, ol = ordered list
835 * type can also come from the '_type' member of $vals.
836 * @param $noHtml Boolean If to avoid returning anything resembling
837 * html. (Ugly hack for backwards compatibility with old mediawiki).
838 * @return String single value (in wiki-syntax).
840 public static function flattenArray( $vals, $type = 'ul', $noHtml = false ) {
841 if ( isset( $vals['_type'] ) ) {
842 $type = $vals['_type'];
843 unset( $vals['_type'] );
846 if ( !is_array( $vals ) ) {
847 return $vals; // do nothing if not an array;
849 elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
852 elseif ( count( $vals ) === 0 ) {
853 wfDebug( __METHOD__
. ' metadata array with 0 elements!' );
854 return ""; // paranoia. This should never happen
856 /* @todo FIXME: This should hide some of the list entries if there are
857 * say more than four. Especially if a field is translated into 20
858 * languages, we don't want to show them all by default
864 // Display default, followed by ContLang,
865 // followed by the rest in no particular
868 // Todo: hide some items if really long list.
872 $cLang = $wgContLang->getCode();
873 $defaultItem = false;
874 $defaultLang = false;
876 // If default is set, save it for later,
877 // as we don't know if it's equal to
878 // one of the lang codes. (In xmp
879 // you specify the language for a
880 // default property by having both
881 // a default prop, and one in the language
882 // that are identical)
883 if ( isset( $vals['x-default'] ) ) {
884 $defaultItem = $vals['x-default'];
885 unset( $vals['x-default'] );
887 // Do contentLanguage.
888 if ( isset( $vals[$cLang] ) ) {
890 if ( $vals[$cLang] === $defaultItem ) {
891 $defaultItem = false;
894 $content .= self
::langItem(
895 $vals[$cLang], $cLang,
896 $isDefault, $noHtml );
898 unset( $vals[$cLang] );
902 foreach ( $vals as $lang => $item ) {
903 if ( $item === $defaultItem ) {
904 $defaultLang = $lang;
907 $content .= self
::langItem( $item,
908 $lang, false, $noHtml );
910 if ( $defaultItem !== false ) {
911 $content = self
::langItem( $defaultItem,
912 $defaultLang, true, $noHtml )
918 return '<ul class="metadata-langlist">' .
923 return "\n#" . implode( "\n#", $vals );
925 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
929 return "\n*" . implode( "\n*", $vals );
931 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
936 /** Helper function for creating lists of translations.
938 * @param $value String value (this is not escaped)
939 * @param $lang String lang code of item or false
940 * @param $default Boolean if it is default value.
941 * @param $noHtml Boolean If to avoid html (for back-compat)
942 * @return language item (Note: despite how this looks,
943 * this is treated as wikitext not html).
945 private static function langItem( $value, $lang, $default = false, $noHtml = false ) {
946 if ( $lang === false && $default === false) {
947 throw new MWException('$lang and $default cannot both '
952 $wrappedValue = $value;
954 $wrappedValue = '<span class="mw-metadata-lang-value">'
955 . $value . '</span>';
958 if ( $lang === false ) {
960 return wfMsg( 'metadata-langitem-default',
961 $wrappedValue ) . "\n\n";
963 return '<li class="mw-metadata-lang-default">'
964 . wfMsg( 'metadata-langitem-default',
969 $lowLang = strtolower( $lang );
970 $langName = Language
::fetchLanguageName( $lowLang );
971 if ( $langName === '' ) {
972 //try just the base language name. (aka en-US -> en ).
973 list( $langPrefix ) = explode( '-', $lowLang, 2 );
974 $langName = Language
::fetchLanguageName( $langPrefix );
975 if ( $langName === '' ) {
980 // else we have a language specified
983 return '*' . wfMsg( 'metadata-langitem',
984 $wrappedValue, $langName, $lang );
987 $item = '<li class="mw-metadata-lang-code-'
990 $item .= ' mw-metadata-lang-default';
992 $item .= '" lang="' . $lang . '">';
993 $item .= wfMsg( 'metadata-langitem',
994 $wrappedValue, $langName, $lang );
1000 * Convenience function for getFormattedData()
1004 * @param $tag String: the tag name to pass on
1005 * @param $val String: the value of the tag
1006 * @param $arg String: an argument to pass ($1)
1007 * @param $arg2 String: a 2nd argument to pass ($2)
1008 * @return string A wfMsg of "exif-$tag-$val" in lower case
1010 static function msg( $tag, $val, $arg = null, $arg2 = null ) {
1015 return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 );
1019 * Format a number, convert numbers from fractions into floating point
1020 * numbers, joins arrays of numbers with commas.
1024 * @param $num Mixed: the value to format
1025 * @param $round float|int digits to round to or false.
1026 * @return mixed A floating point number or whatever we were fed
1028 static function formatNum( $num, $round = false ) {
1031 if( is_array($num) ) {
1033 foreach( $num as $number ) {
1034 $out[] = self
::formatNum($number);
1036 return $wgLang->commaList( $out );
1038 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1040 $newNum = $m[1] / $m[2];
1041 if ( $round !== false ) {
1042 $newNum = round( $newNum, $round );
1048 return $wgLang->formatNum( $newNum );
1050 if ( is_numeric( $num ) && $round !== false ) {
1051 $num = round( $num, $round );
1053 return $wgLang->formatNum( $num );
1058 * Format a rational number, reducing fractions
1062 * @param $num Mixed: the value to format
1063 * @return mixed A floating point number or whatever we were fed
1065 static function formatFraction( $num ) {
1067 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1068 $numerator = intval( $m[1] );
1069 $denominator = intval( $m[2] );
1070 $gcd = self
::gcd( abs( $numerator ), $denominator );
1072 // 0 shouldn't happen! ;)
1073 return self
::formatNum( $numerator / $gcd ) . '/' . self
::formatNum( $denominator / $gcd );
1076 return self
::formatNum( $num );
1080 * Calculate the greatest common divisor of two integers.
1082 * @param $a Integer: Numerator
1083 * @param $b Integer: Denominator
1087 static function gcd( $a, $b ) {
1089 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1090 // Recursive form would be:
1094 return gcd( $b, $a % $b );
1097 $remainder = $a %
$b;
1099 // tail recursion...
1107 * Fetch the human readable version of a news code.
1108 * A news code is an 8 digit code. The first two
1109 * digits are a general classification, so we just
1112 * Note, leading 0's are significant, so this is
1113 * a string, not an int.
1115 * @param $val String: The 8 digit news code.
1116 * @return srting The human readable form
1118 static private function convertNewsCode( $val ) {
1119 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1120 // Not a valid news code.
1124 switch( substr( $val , 0, 2 ) ) {
1177 if ( $cat !== '' ) {
1178 $catMsg = self
::msg( 'iimcategory', $cat );
1179 $val = self
::msg( 'subjectnewscode', '', $val, $catMsg );
1185 * Format a coordinate value, convert numbers from floating point
1186 * into degree minute second representation.
1188 * @param $coord Array: degrees, minutes and seconds
1189 * @param $type String: latitude or longitude (for if its a NWS or E)
1190 * @return mixed A floating point number or whatever we were fed
1192 static function formatCoords( $coord, $type ) {
1196 if ( $type === 'latitude' ) {
1199 elseif ( $type === 'longitude' ) {
1205 if ( $type === 'latitude' ) {
1208 elseif ( $type === 'longitude' ) {
1213 $deg = floor( $nCoord );
1214 $min = floor( ( $nCoord - $deg ) * 60.0 );
1215 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1217 $deg = self
::formatNum( $deg );
1218 $min = self
::formatNum( $min );
1219 $sec = self
::formatNum( $sec );
1221 return wfMsg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord );
1225 * Format the contact info field into a single value.
1227 * @param $vals Array array with fields of the ContactInfo
1228 * struct defined in the IPTC4XMP spec. Or potentially
1229 * an array with one element that is a free form text
1230 * value from the older iptc iim 1:118 prop.
1232 * This function might be called from
1233 * JpegHandler::convertMetadataVersion which is why it is
1236 * @return String of html-ish looking wikitext
1238 public static function collapseContactInfo( $vals ) {
1239 if( ! ( isset( $vals['CiAdrExtadr'] )
1240 ||
isset( $vals['CiAdrCity'] )
1241 ||
isset( $vals['CiAdrCtry'] )
1242 ||
isset( $vals['CiEmailWork'] )
1243 ||
isset( $vals['CiTelWork'] )
1244 ||
isset( $vals['CiAdrPcode'] )
1245 ||
isset( $vals['CiAdrRegion'] )
1246 ||
isset( $vals['CiUrlWork'] )
1248 // We don't have any sub-properties
1249 // This could happen if its using old
1250 // iptc that just had this as a free-form
1252 // Note: We run this through htmlspecialchars
1253 // partially to be consistent, and partially
1254 // because people often insert >, etc into
1255 // the metadata which should not be interpreted
1256 // but we still want to auto-link urls.
1257 foreach( $vals as &$val ) {
1258 $val = htmlspecialchars( $val );
1260 return self
::flattenArray( $vals );
1262 // We have a real ContactInfo field.
1263 // Its unclear if all these fields have to be
1264 // set, so assume they do not.
1265 $url = $tel = $street = $city = $country = '';
1266 $email = $postal = $region = '';
1268 // Also note, some of the class names this uses
1269 // are similar to those used by hCard. This is
1270 // mostly because they're sensible names. This
1271 // does not (and does not attempt to) output
1272 // stuff in the hCard microformat. However it
1273 // might output in the adr microformat.
1275 if ( isset( $vals['CiAdrExtadr'] ) ) {
1276 // Todo: This can potentially be multi-line.
1277 // Need to check how that works in XMP.
1278 $street = '<span class="extended-address">'
1280 $vals['CiAdrExtadr'] )
1283 if ( isset( $vals['CiAdrCity'] ) ) {
1284 $city = '<span class="locality">'
1285 . htmlspecialchars( $vals['CiAdrCity'] )
1288 if ( isset( $vals['CiAdrCtry'] ) ) {
1289 $country = '<span class="country-name">'
1290 . htmlspecialchars( $vals['CiAdrCtry'] )
1293 if ( isset( $vals['CiEmailWork'] ) ) {
1295 // Have to split multiple emails at commas/new lines.
1296 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1297 foreach ( $splitEmails as $e1 ) {
1298 // Also split on comma
1299 foreach ( explode( ',', $e1 ) as $e2 ) {
1300 $finalEmail = trim( $e2 );
1301 if ( $finalEmail == ',' ||
$finalEmail == '' ) {
1304 if ( strpos( $finalEmail, '<' ) !== false ) {
1305 // Don't do fancy formatting to
1306 // "My name" <foo@bar.com> style stuff
1307 $emails[] = $finalEmail;
1309 $emails[] = '[mailto:'
1311 . ' <span class="email">'
1317 $email = implode( ', ', $emails );
1319 if ( isset( $vals['CiTelWork'] ) ) {
1320 $tel = '<span class="tel">'
1321 . htmlspecialchars( $vals['CiTelWork'] )
1324 if ( isset( $vals['CiAdrPcode'] ) ) {
1325 $postal = '<span class="postal-code">'
1327 $vals['CiAdrPcode'] )
1330 if ( isset( $vals['CiAdrRegion'] ) ) {
1331 // Note this is province/state.
1332 $region = '<span class="region">'
1334 $vals['CiAdrRegion'] )
1337 if ( isset( $vals['CiUrlWork'] ) ) {
1338 $url = '<span class="url">'
1339 . htmlspecialchars( $vals['CiUrlWork'] )
1342 return wfMsg( 'exif-contact-value', $email, $url,
1343 $street, $city, $region, $postal, $country,
1349 /** For compatability with old FormatExif class
1350 * which some extensions use.
1352 * @deprecated since 1.18
1357 function FormatExif ( $meta ) {
1358 wfDeprecated(__METHOD__
);
1359 $this->meta
= $meta;
1362 function getFormattedData ( ) {
1363 return FormatMetadata
::getFormattedData( $this->meta
);