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.
107 if ( $time && intval( $time ) > 0 ) {
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 ) {
126 case 1: case 2: case 3: case 4:
127 case 5: case 6: case 7: case 8:
128 case 32773: case 32946: case 34712:
129 $val = self
::msg( $tag, $val );
132 /* If not recognized, display as is. */
137 case 'PhotometricInterpretation':
140 $val = self
::msg( $tag, $val );
143 /* If not recognized, display as is. */
150 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
151 $val = self
::msg( $tag, $val );
154 /* If not recognized, display as is. */
159 case 'PlanarConfiguration':
162 $val = self
::msg( $tag, $val );
165 /* If not recognized, display as is. */
170 // TODO: YCbCrSubSampling
171 case 'YCbCrPositioning':
175 $val = self
::msg( $tag, $val );
178 /* If not recognized, display as is. */
185 switch( $resolutionunit ) {
187 $val = self
::msg( 'XYResolution', 'i', self
::formatNum( $val ) );
190 $val = self
::msg( 'XYResolution', 'c', self
::formatNum( $val ) );
193 /* If not recognized, display as is. */
198 // TODO: YCbCrCoefficients #p27 (see annex E)
199 case 'ExifVersion': case 'FlashpixVersion':
206 $val = self
::msg( $tag, $val );
209 /* If not recognized, display as is. */
214 case 'ComponentsConfiguration':
216 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
217 $val = self
::msg( $tag, $val );
220 /* If not recognized, display as is. */
226 case 'DateTimeOriginal':
227 case 'DateTimeDigitized':
228 case 'DateTimeReleased':
229 case 'DateTimeExpires':
232 case 'DateTimeMetadata':
233 if ( $val == '0000:00:00 00:00:00' ||
$val == ' : : : : ' ) {
234 $val = wfMsg( 'exif-unknowndate' );
235 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
237 $time = wfTimestamp( TS_MW
, $val );
238 if ( $time && intval( $time ) > 0 ) {
239 $val = $wgLang->timeanddate( $time );
241 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) {
242 // No second field. Still format the same
243 // since timeanddate doesn't include seconds anyways,
244 // but second still available in api
245 $time = wfTimestamp( TS_MW
, $val . ':00' );
246 if ( $time && intval( $time ) > 0 ) {
247 $val = $wgLang->timeanddate( $time );
249 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
250 // If only the date but not the time is filled in.
251 $time = wfTimestamp( TS_MW
, substr( $val, 0, 4 )
252 . substr( $val, 5, 2 )
253 . substr( $val, 8, 2 )
255 if ( $time && intval( $time ) > 0 ) {
256 $val = $wgLang->date( $time );
259 // else it will just output $val without formatting it.
262 case 'ExposureProgram':
264 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
265 $val = self
::msg( $tag, $val );
268 /* If not recognized, display as is. */
273 case 'SubjectDistance':
274 $val = self
::msg( $tag, '', self
::formatNum( $val ) );
279 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
280 $val = self
::msg( $tag, $val );
283 /* If not recognized, display as is. */
290 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
291 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
292 case 21: case 22: case 23: case 24: case 255:
293 $val = self
::msg( $tag, $val );
296 /* If not recognized, display as is. */
302 $flashDecode = array(
303 'fired' => $val & bindec( '00000001' ),
304 'return' => ( $val & bindec( '00000110' ) ) >> 1,
305 'mode' => ( $val & bindec( '00011000' ) ) >> 3,
306 'function' => ( $val & bindec( '00100000' ) ) >> 5,
307 'redeye' => ( $val & bindec( '01000000' ) ) >> 6,
308 // 'reserved' => ($val & bindec( '10000000' )) >> 7,
311 # We do not need to handle unknown values since all are used.
312 foreach ( $flashDecode as $subTag => $subValue ) {
313 # We do not need any message for zeroed values.
314 if ( $subTag != 'fired' && $subValue == 0 ) {
317 $fullTag = $tag . '-' . $subTag ;
318 $flashMsgs[] = self
::msg( $fullTag, $subValue );
320 $val = $wgLang->commaList( $flashMsgs );
323 case 'FocalPlaneResolutionUnit':
326 $val = self
::msg( $tag, $val );
329 /* If not recognized, display as is. */
334 case 'SensingMethod':
336 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
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. */
359 $val = self
::msg( $tag, $val );
362 /* If not recognized, display as is. */
367 case 'CustomRendered':
370 $val = self
::msg( $tag, $val );
373 /* If not recognized, display as is. */
380 case 0: case 1: case 2:
381 $val = self
::msg( $tag, $val );
384 /* If not recognized, display as is. */
392 $val = self
::msg( $tag, $val );
395 /* If not recognized, display as is. */
400 case 'SceneCaptureType':
402 case 0: case 1: case 2: case 3:
403 $val = self
::msg( $tag, $val );
406 /* If not recognized, display as is. */
413 case 0: case 1: case 2: case 3: case 4:
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. */
446 case 0: case 1: case 2:
447 $val = self
::msg( $tag, $val );
450 /* If not recognized, display as is. */
455 case 'SubjectDistanceRange':
457 case 0: case 1: case 2: case 3:
458 $val = self
::msg( $tag, $val );
461 /* If not recognized, display as is. */
466 //The GPS...Ref values are kept for compatibility, probably won't be reached.
467 case 'GPSLatitudeRef':
468 case 'GPSDestLatitudeRef':
471 $val = self
::msg( 'GPSLatitude', $val );
474 /* If not recognized, display as is. */
479 case 'GPSLongitudeRef':
480 case 'GPSDestLongitudeRef':
483 $val = self
::msg( 'GPSLongitude', $val );
486 /* If not recognized, display as is. */
493 $val = self
::msg( 'GPSAltitude', 'below-sealevel', self
::formatNum( -$val, 3 ) );
495 $val = self
::msg( 'GPSAltitude', 'above-sealevel', self
::formatNum( $val, 3 ) );
502 $val = self
::msg( $tag, $val );
505 /* If not recognized, display as is. */
510 case 'GPSMeasureMode':
513 $val = self
::msg( $tag, $val );
516 /* If not recognized, display as is. */
523 case 'GPSImgDirectionRef':
524 case 'GPSDestBearingRef':
527 $val = self
::msg( 'GPSDirection', $val );
530 /* If not recognized, display as is. */
536 case 'GPSDestLatitude':
537 $val = self
::formatCoords( $val, 'latitude' );
540 case 'GPSDestLongitude':
541 $val = self
::formatCoords( $val, 'longitude' );
546 case 'K': case 'M': case 'N':
547 $val = self
::msg( 'GPSSpeed', $val );
550 /* If not recognized, display as is. */
555 case 'GPSDestDistanceRef':
557 case 'K': case 'M': case 'N':
558 $val = self
::msg( 'GPSDestDistance', $val );
561 /* If not recognized, display as is. */
567 // See http://en.wikipedia.org/wiki/Dilution_of_precision_(GPS)
569 $val = self
::msg( $tag, 'excellent', self
::formatNum( $val ) );
570 } elseif ( $val <= 5 ) {
571 $val = self
::msg( $tag, 'good', self
::formatNum( $val ) );
572 } elseif ( $val <= 10 ) {
573 $val = self
::msg( $tag, 'moderate', self
::formatNum( $val ) );
574 } elseif ( $val <= 20 ) {
575 $val = self
::msg( $tag, 'fair', self
::formatNum( $val ) );
577 $val = self
::msg( $tag, 'poor', self
::formatNum( $val ) );
581 // This is not in the Exif standard, just a special
582 // case for our purposes which enables wikis to wikify
583 // the make, model and software name to link to their articles.
586 $val = self
::msg( $tag, '', $val );
590 if ( is_array( $val ) ) {
591 //if its a software, version array.
592 $val = wfMsg( 'exif-software-version-value', $val[0], $val[1] );
594 $val = self
::msg( $tag, '', $val );
599 // Show the pretty fraction as well as decimal version
600 $val = wfMsg( 'exif-exposuretime-format',
601 self
::formatFraction( $val ), self
::formatNum( $val ) );
603 case 'ISOSpeedRatings':
604 // If its = 65535 that means its at the
605 // limit of the size of Exif::short and
607 if ( $val == '65535' ) {
608 $val = self
::msg( $tag, 'overflow' );
610 $val = self
::formatNum( $val );
614 $val = wfMsg( 'exif-fnumber-format',
615 self
::formatNum( $val ) );
618 case 'FocalLength': case 'FocalLengthIn35mmFilm':
619 $val = wfMsg( 'exif-focallength-format',
620 self
::formatNum( $val ) );
623 case 'MaxApertureValue':
624 if ( strpos( $val, '/' ) !== false ) {
625 // need to expand this earlier to calculate fNumber
626 list($n, $d) = explode('/', $val);
627 if ( is_numeric( $n ) && is_numeric( $d ) ) {
631 if ( is_numeric( $val ) ) {
632 $fNumber = pow( 2, $val / 2 );
633 if ( $fNumber !== false ) {
634 $val = wfMsg( 'exif-maxaperturevalue-value',
635 self
::formatNum( $val ),
636 self
::formatNum( $fNumber, 2 )
643 switch( strtolower( $val ) ) {
644 // See pg 29 of IPTC photo
645 // metadata standard.
646 case 'ace': case 'clj':
647 case 'dis': case 'fin':
648 case 'edu': case 'evn':
649 case 'hth': case 'hum':
650 case 'lab': case 'lif':
651 case 'pol': case 'rel':
652 case 'sci': case 'soi':
653 case 'spo': case 'war':
661 case 'SubjectNewsCode':
662 // Essentially like iimCategory.
663 // 8 (numeric) digit hierarchical
664 // classification. We decode the
665 // first 2 digits, which provide
667 $val = self
::convertNewsCode( $val );
670 // 1-8 with 1 being highest, 5 normal
671 // 0 is reserved, and 9 is 'user-defined'.
673 if ( $val == 0 ||
$val == 9 ) {
675 } elseif ( $val < 5 && $val > 1 ) {
677 } elseif ( $val == 5 ) {
679 } elseif ( $val <= 8 && $val > 5) {
683 if ( $urgency !== '' ) {
684 $val = self
::msg( 'urgency',
690 // Things that have a unit of pixels.
691 case 'OriginalImageHeight':
692 case 'OriginalImageWidth':
693 case 'PixelXDimension':
694 case 'PixelYDimension':
697 $val = self
::formatNum( $val ) . ' ' . wfMsg( 'unit-pixel' );
700 // Do not transform fields with pure text.
701 // For some languages the formatNum()
702 // conversion results to wrong output like
703 // foo,bar@example,com or foo٫bar@example٫com.
704 // Also some 'numeric' things like Scene codes
705 // are included here as we really don't want
707 case 'ImageDescription':
710 case 'RelatedSoundFile':
711 case 'ImageUniqueID':
712 case 'SpectralSensitivity':
713 case 'GPSSatellites':
717 case 'WorldRegionDest':
719 case 'CountryCodeDest':
720 case 'ProvinceOrStateDest':
722 case 'SublocationDest':
723 case 'WorldRegionCreated':
724 case 'CountryCreated':
725 case 'CountryCodeCreated':
726 case 'ProvinceOrStateCreated':
728 case 'SublocationCreated':
730 case 'SpecialInstructions':
735 case 'FixtureIdentifier':
737 case 'LocationDestCode':
739 case 'JPEGFileComment':
740 case 'iimSupplementalCategory':
741 case 'OriginalTransmissionRef':
743 case 'dc-contributor':
752 case 'CameraOwnerName':
755 case 'RightsCertificate':
756 case 'CopyrightOwner':
759 case 'OriginalDocumentID':
761 case 'MorePermissionsUrl':
762 case 'AttributionUrl':
763 case 'PreferredAttributionName':
764 case 'PNGFileComment':
766 case 'ContentWarning':
767 case 'GIFFileComment':
769 case 'IntellectualGenre':
771 case 'OrginisationInImage':
772 case 'PersonInImage':
774 $val = htmlspecialchars( $val );
779 case 'a': case 'p': case 'b':
780 $val = self
::msg( $tag, $val );
783 $val = htmlspecialchars( $val );
789 case 'True': case 'False':
790 $val = self
::msg( $tag, $val );
795 if ( $val == '-1' ) {
796 $val = self
::msg( $tag, 'rejected' );
798 $val = self
::formatNum( $val );
803 $lang = Language
::fetchLanguageName( strtolower( $val ), $wgLang );
805 $val = htmlspecialchars( $lang );
807 $val = htmlspecialchars( $val );
812 $val = self
::formatNum( $val );
816 // End formatting values, start flattening arrays.
817 $vals = self
::flattenArray( $vals, $type );
824 * A function to collapse multivalued tags into a single value.
825 * This turns an array of (for example) authors into a bulleted list.
827 * This is public on the basis it might be useful outside of this class.
829 * @param $vals Array array of values
830 * @param $type String Type of array (either lang, ul, ol).
831 * lang = language assoc array with keys being the lang code
832 * ul = unordered list, ol = ordered list
833 * type can also come from the '_type' member of $vals.
834 * @param $noHtml Boolean If to avoid returning anything resembling
835 * html. (Ugly hack for backwards compatibility with old mediawiki).
836 * @return String single value (in wiki-syntax).
838 public static function flattenArray( $vals, $type = 'ul', $noHtml = false ) {
839 if ( isset( $vals['_type'] ) ) {
840 $type = $vals['_type'];
841 unset( $vals['_type'] );
844 if ( !is_array( $vals ) ) {
845 return $vals; // do nothing if not an array;
847 elseif ( count( $vals ) === 1 && $type !== 'lang' ) {
850 elseif ( count( $vals ) === 0 ) {
851 wfDebug( __METHOD__
. ' metadata array with 0 elements!' );
852 return ""; // paranoia. This should never happen
854 /* @todo FIXME: This should hide some of the list entries if there are
855 * say more than four. Especially if a field is translated into 20
856 * languages, we don't want to show them all by default
862 // Display default, followed by ContLang,
863 // followed by the rest in no particular
866 // Todo: hide some items if really long list.
870 $cLang = $wgContLang->getCode();
871 $defaultItem = false;
872 $defaultLang = false;
874 // If default is set, save it for later,
875 // as we don't know if it's equal to
876 // one of the lang codes. (In xmp
877 // you specify the language for a
878 // default property by having both
879 // a default prop, and one in the language
880 // that are identical)
881 if ( isset( $vals['x-default'] ) ) {
882 $defaultItem = $vals['x-default'];
883 unset( $vals['x-default'] );
885 // Do contentLanguage.
886 if ( isset( $vals[$cLang] ) ) {
888 if ( $vals[$cLang] === $defaultItem ) {
889 $defaultItem = false;
892 $content .= self
::langItem(
893 $vals[$cLang], $cLang,
894 $isDefault, $noHtml );
896 unset( $vals[$cLang] );
900 foreach ( $vals as $lang => $item ) {
901 if ( $item === $defaultItem ) {
902 $defaultLang = $lang;
905 $content .= self
::langItem( $item,
906 $lang, false, $noHtml );
908 if ( $defaultItem !== false ) {
909 $content = self
::langItem( $defaultItem,
910 $defaultLang, true, $noHtml )
916 return '<ul class="metadata-langlist">' .
921 return "\n#" . implode( "\n#", $vals );
923 return "<ol><li>" . implode( "</li>\n<li>", $vals ) . '</li></ol>';
927 return "\n*" . implode( "\n*", $vals );
929 return "<ul><li>" . implode( "</li>\n<li>", $vals ) . '</li></ul>';
934 /** Helper function for creating lists of translations.
936 * @param $value String value (this is not escaped)
937 * @param $lang String lang code of item or false
938 * @param $default Boolean if it is default value.
939 * @param $noHtml Boolean If to avoid html (for back-compat)
940 * @return language item (Note: despite how this looks,
941 * this is treated as wikitext not html).
943 private static function langItem( $value, $lang, $default = false, $noHtml = false ) {
944 if ( $lang === false && $default === false) {
945 throw new MWException('$lang and $default cannot both '
950 $wrappedValue = $value;
952 $wrappedValue = '<span class="mw-metadata-lang-value">'
953 . $value . '</span>';
956 if ( $lang === false ) {
958 return wfMsg( 'metadata-langitem-default',
959 $wrappedValue ) . "\n\n";
961 return '<li class="mw-metadata-lang-default">'
962 . wfMsg( 'metadata-langitem-default',
967 $lowLang = strtolower( $lang );
968 $langName = Language
::fetchLanguageName( $lowLang );
969 if ( $langName === '' ) {
970 //try just the base language name. (aka en-US -> en ).
971 list( $langPrefix ) = explode( '-', $lowLang, 2 );
972 $langName = Language
::fetchLanguageName( $langPrefix );
973 if ( $langName === '' ) {
978 // else we have a language specified
981 return '*' . wfMsg( 'metadata-langitem',
982 $wrappedValue, $langName, $lang );
985 $item = '<li class="mw-metadata-lang-code-'
988 $item .= ' mw-metadata-lang-default';
990 $item .= '" lang="' . $lang . '">';
991 $item .= wfMsg( 'metadata-langitem',
992 $wrappedValue, $langName, $lang );
998 * Convenience function for getFormattedData()
1002 * @param $tag String: the tag name to pass on
1003 * @param $val String: the value of the tag
1004 * @param $arg String: an argument to pass ($1)
1005 * @param $arg2 String: a 2nd argument to pass ($2)
1006 * @return string A wfMsg of "exif-$tag-$val" in lower case
1008 static function msg( $tag, $val, $arg = null, $arg2 = null ) {
1013 return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 );
1017 * Format a number, convert numbers from fractions into floating point
1018 * numbers, joins arrays of numbers with commas.
1022 * @param $num Mixed: the value to format
1023 * @param $round float|int digits to round to or false.
1024 * @return mixed A floating point number or whatever we were fed
1026 static function formatNum( $num, $round = false ) {
1029 if( is_array($num) ) {
1031 foreach( $num as $number ) {
1032 $out[] = self
::formatNum($number);
1034 return $wgLang->commaList( $out );
1036 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1038 $newNum = $m[1] / $m[2];
1039 if ( $round !== false ) {
1040 $newNum = round( $newNum, $round );
1046 return $wgLang->formatNum( $newNum );
1048 if ( is_numeric( $num ) && $round !== false ) {
1049 $num = round( $num, $round );
1051 return $wgLang->formatNum( $num );
1056 * Format a rational number, reducing fractions
1060 * @param $num Mixed: the value to format
1061 * @return mixed A floating point number or whatever we were fed
1063 static function formatFraction( $num ) {
1065 if ( preg_match( '/^(-?\d+)\/(\d+)$/', $num, $m ) ) {
1066 $numerator = intval( $m[1] );
1067 $denominator = intval( $m[2] );
1068 $gcd = self
::gcd( abs( $numerator ), $denominator );
1070 // 0 shouldn't happen! ;)
1071 return self
::formatNum( $numerator / $gcd ) . '/' . self
::formatNum( $denominator / $gcd );
1074 return self
::formatNum( $num );
1078 * Calculate the greatest common divisor of two integers.
1080 * @param $a Integer: Numerator
1081 * @param $b Integer: Denominator
1085 static function gcd( $a, $b ) {
1087 // http://en.wikipedia.org/wiki/Euclidean_algorithm
1088 // Recursive form would be:
1092 return gcd( $b, $a % $b );
1095 $remainder = $a %
$b;
1097 // tail recursion...
1105 * Fetch the human readable version of a news code.
1106 * A news code is an 8 digit code. The first two
1107 * digits are a general classification, so we just
1110 * Note, leading 0's are significant, so this is
1111 * a string, not an int.
1113 * @param $val String: The 8 digit news code.
1114 * @return srting The human readable form
1116 static private function convertNewsCode( $val ) {
1117 if ( !preg_match( '/^\d{8}$/D', $val ) ) {
1118 // Not a valid news code.
1122 switch( substr( $val , 0, 2 ) ) {
1175 if ( $cat !== '' ) {
1176 $catMsg = self
::msg( 'iimcategory', $cat );
1177 $val = self
::msg( 'subjectnewscode', '', $val, $catMsg );
1183 * Format a coordinate value, convert numbers from floating point
1184 * into degree minute second representation.
1186 * @param $coord Array: degrees, minutes and seconds
1187 * @param $type String: latitude or longitude (for if its a NWS or E)
1188 * @return mixed A floating point number or whatever we were fed
1190 static function formatCoords( $coord, $type ) {
1194 if ( $type === 'latitude' ) {
1197 elseif ( $type === 'longitude' ) {
1203 if ( $type === 'latitude' ) {
1206 elseif ( $type === 'longitude' ) {
1211 $deg = floor( $nCoord );
1212 $min = floor( ( $nCoord - $deg ) * 60.0 );
1213 $sec = round( ( ( $nCoord - $deg ) - $min / 60 ) * 3600, 2 );
1215 $deg = self
::formatNum( $deg );
1216 $min = self
::formatNum( $min );
1217 $sec = self
::formatNum( $sec );
1219 return wfMsg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord );
1223 * Format the contact info field into a single value.
1225 * @param $vals Array array with fields of the ContactInfo
1226 * struct defined in the IPTC4XMP spec. Or potentially
1227 * an array with one element that is a free form text
1228 * value from the older iptc iim 1:118 prop.
1230 * This function might be called from
1231 * JpegHandler::convertMetadataVersion which is why it is
1234 * @return String of html-ish looking wikitext
1236 public static function collapseContactInfo( $vals ) {
1237 if( ! ( isset( $vals['CiAdrExtadr'] )
1238 ||
isset( $vals['CiAdrCity'] )
1239 ||
isset( $vals['CiAdrCtry'] )
1240 ||
isset( $vals['CiEmailWork'] )
1241 ||
isset( $vals['CiTelWork'] )
1242 ||
isset( $vals['CiAdrPcode'] )
1243 ||
isset( $vals['CiAdrRegion'] )
1244 ||
isset( $vals['CiUrlWork'] )
1246 // We don't have any sub-properties
1247 // This could happen if its using old
1248 // iptc that just had this as a free-form
1250 // Note: We run this through htmlspecialchars
1251 // partially to be consistent, and partially
1252 // because people often insert >, etc into
1253 // the metadata which should not be interpreted
1254 // but we still want to auto-link urls.
1255 foreach( $vals as &$val ) {
1256 $val = htmlspecialchars( $val );
1258 return self
::flattenArray( $vals );
1260 // We have a real ContactInfo field.
1261 // Its unclear if all these fields have to be
1262 // set, so assume they do not.
1263 $url = $tel = $street = $city = $country = '';
1264 $email = $postal = $region = '';
1266 // Also note, some of the class names this uses
1267 // are similar to those used by hCard. This is
1268 // mostly because they're sensible names. This
1269 // does not (and does not attempt to) output
1270 // stuff in the hCard microformat. However it
1271 // might output in the adr microformat.
1273 if ( isset( $vals['CiAdrExtadr'] ) ) {
1274 // Todo: This can potentially be multi-line.
1275 // Need to check how that works in XMP.
1276 $street = '<span class="extended-address">'
1278 $vals['CiAdrExtadr'] )
1281 if ( isset( $vals['CiAdrCity'] ) ) {
1282 $city = '<span class="locality">'
1283 . htmlspecialchars( $vals['CiAdrCity'] )
1286 if ( isset( $vals['CiAdrCtry'] ) ) {
1287 $country = '<span class="country-name">'
1288 . htmlspecialchars( $vals['CiAdrCtry'] )
1291 if ( isset( $vals['CiEmailWork'] ) ) {
1293 // Have to split multiple emails at commas/new lines.
1294 $splitEmails = explode( "\n", $vals['CiEmailWork'] );
1295 foreach ( $splitEmails as $e1 ) {
1296 // Also split on comma
1297 foreach ( explode( ',', $e1 ) as $e2 ) {
1298 $finalEmail = trim( $e2 );
1299 if ( $finalEmail == ',' ||
$finalEmail == '' ) {
1302 if ( strpos( $finalEmail, '<' ) !== false ) {
1303 // Don't do fancy formatting to
1304 // "My name" <foo@bar.com> style stuff
1305 $emails[] = $finalEmail;
1307 $emails[] = '[mailto:'
1309 . ' <span class="email">'
1315 $email = implode( ', ', $emails );
1317 if ( isset( $vals['CiTelWork'] ) ) {
1318 $tel = '<span class="tel">'
1319 . htmlspecialchars( $vals['CiTelWork'] )
1322 if ( isset( $vals['CiAdrPcode'] ) ) {
1323 $postal = '<span class="postal-code">'
1325 $vals['CiAdrPcode'] )
1328 if ( isset( $vals['CiAdrRegion'] ) ) {
1329 // Note this is province/state.
1330 $region = '<span class="region">'
1332 $vals['CiAdrRegion'] )
1335 if ( isset( $vals['CiUrlWork'] ) ) {
1336 $url = '<span class="url">'
1337 . htmlspecialchars( $vals['CiUrlWork'] )
1340 return wfMsg( 'exif-contact-value', $email, $url,
1341 $street, $city, $region, $postal, $country,
1347 /** For compatability with old FormatExif class
1348 * which some extensions use.
1350 * @deprecated since 1.18
1355 function FormatExif ( $meta ) {
1356 wfDeprecated(__METHOD__
);
1357 $this->meta
= $meta;
1360 function getFormattedData ( ) {
1361 return FormatMetadata
::getFormattedData( $this->meta
);