* (bug 2111) Collapsable exif metadata table, clean up display
[lhc/web/wiklou.git] / includes / Exif.php
1 <?php
2 if ( !defined( 'MEDIAWIKI' ) ) die();
3 /**
4 * @package MediaWiki
5 * @subpackage Metadata
6 *
7 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
8 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
9 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
27 * @bug 1555, 1947
28 */
29
30 /**#@+
31 * Exif tag type definition
32 */
33 define('MW_EXIF_BYTE', 1); # An 8-bit (1-byte) unsigned integer.
34 define('MW_EXIF_ASCII', 2); # An 8-bit byte containing one 7-bit ASCII code. The final byte is terminated with NULL.
35 define('MW_EXIF_SHORT', 3); # A 16-bit (2-byte) unsigned integer.
36 define('MW_EXIF_LONG', 4); # A 32-bit (4-byte) unsigned integer.
37 define('MW_EXIF_RATIONAL', 5); # Two LONGs. The first LONG is the numerator and the second LONG expresses the denominator
38 define('MW_EXIF_UNDEFINED', 7); # An 8-bit byte that can take any value depending on the field definition
39 define('MW_EXIF_SLONG', 9); # A 32-bit (4-byte) signed integer (2's complement notation),
40 define('MW_EXIF_SRATIONAL', 10); # Two SLONGs. The first SLONG is the numerator and the second SLONG is the denominator.
41 /**#@-*/
42
43
44 /**
45 * @package MediaWiki
46 * @subpackage Metadata
47 */
48 class Exif {
49 /**#@+
50 * @var array
51 * @access private
52 */
53
54 /**
55 * Exif tags grouped by category, the tagname itself is the key and the type
56 * is the value, in the case of more than one possible value type they are
57 * seperated by commas.
58 */
59 var $mExifTags;
60
61 /**
62 * A one dimentional array of all Exif tags
63 */
64 var $mFlatExifTags;
65
66 /**
67 * The raw Exif data returned by exif_read_data()
68 */
69 var $mRawExifData;
70
71 /**
72 * A Filtered version of $mRawExifData that has been pruned of invalid
73 * tags and tags that contain content they shouldn't contain according
74 * to the Exif specification
75 */
76 var $mFilteredExifData;
77
78 /**
79 * Filtered and formatted Exif data, see FormatExif::getFormattedData()
80 */
81 var $mFormattedExifData;
82
83 /**#@-*/
84
85 /**#@+
86 * @var string
87 * @access private
88 */
89
90 /**
91 * The file being processed
92 */
93 var $file;
94
95 /**
96 * The basename of the file being processed
97 */
98 var $basename;
99
100 /**
101 * The private log to log to
102 */
103 var $log = 'exif';
104
105 /**#@-*/
106
107 /**
108 * Constructor
109 *
110 * @param string $file
111 */
112 function Exif( $file ) {
113 /**
114 * Page numbers here refer to pages in the EXIF 2.2 standard
115 *
116 * @link http://exif.org/Exif2-2.PDF The Exif 2.2 specification
117 */
118 $this->mExifTags = array(
119 # TIFF Rev. 6.0 Attribute Information (p22)
120 'tiff' => array(
121 # Tags relating to image structure
122 'structure' => array(
123 'ImageWidth' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image width
124 'ImageLength' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image height
125 'BitsPerSample' => MW_EXIF_SHORT, # Number of bits per component
126 # "When a primary image is JPEG compressed, this designation is not"
127 # "necessary and is omitted." (p23)
128 'Compression' => MW_EXIF_SHORT, # Compression scheme #p23
129 'PhotometricInterpretation' => MW_EXIF_SHORT, # Pixel composition #p23
130 'Orientation' => MW_EXIF_SHORT, # Orientation of image #p24
131 'SamplesPerPixel' => MW_EXIF_SHORT, # Number of components
132 'PlanarConfiguration' => MW_EXIF_SHORT, # Image data arrangement #p24
133 'YCbCrSubSampling' => MW_EXIF_SHORT, # Subsampling ratio of Y to C #p24
134 'YCbCrPositioning' => MW_EXIF_SHORT, # Y and C positioning #p24-25
135 'XResolution' => MW_EXIF_RATIONAL, # Image resolution in width direction
136 'YResolution' => MW_EXIF_RATIONAL, # Image resolution in height direction
137 'ResolutionUnit' => MW_EXIF_SHORT, # Unit of X and Y resolution #(p26)
138 ),
139
140 # Tags relating to recording offset
141 'offset' => array(
142 'StripOffsets' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Image data location
143 'RowsPerStrip' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Number of rows per strip
144 'StripByteCounts' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Bytes per compressed strip
145 'JPEGInterchangeFormat' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Offset to JPEG SOI
146 'JPEGInterchangeFormatLength' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Bytes of JPEG data
147 ),
148
149 # Tags relating to image data characteristics
150 'characteristics' => array(
151 'TransferFunction' => MW_EXIF_SHORT, # Transfer function
152 'WhitePoint' => MW_EXIF_RATIONAL, # White point chromaticity
153 'PrimaryChromaticities' => MW_EXIF_RATIONAL, # Chromaticities of primarities
154 'YCbCrCoefficients' => MW_EXIF_RATIONAL, # Color space transformation matrix coefficients #p27
155 'ReferenceBlackWhite' => MW_EXIF_RATIONAL # Pair of black and white reference values
156 ),
157
158 # Other tags
159 'other' => array(
160 'DateTime' => MW_EXIF_ASCII, # File change date and time
161 'ImageDescription' => MW_EXIF_ASCII, # Image title
162 'Make' => MW_EXIF_ASCII, # Image input equipment manufacturer
163 'Model' => MW_EXIF_ASCII, # Image input equipment model
164 'Software' => MW_EXIF_ASCII, # Software used
165 'Artist' => MW_EXIF_ASCII, # Person who created the image
166 'Copyright' => MW_EXIF_ASCII, # Copyright holder
167 ),
168 ),
169
170 # Exif IFD Attribute Information (p30-31)
171 'exif' => array(
172 # Tags relating to version
173 'version' => array(
174 # TODO: NOTE: Nonexistence of this field is taken to mean nonconformance
175 # to the EXIF 2.1 AND 2.2 standards
176 'ExifVersion' => MW_EXIF_UNDEFINED, # Exif version
177 'FlashpixVersion' => MW_EXIF_UNDEFINED, # Supported Flashpix version #p32
178 ),
179
180 # Tags relating to Image Data Characteristics
181 'characteristics' => array(
182 'ColorSpace' => MW_EXIF_SHORT, # Color space information #p32
183 ),
184
185 # Tags relating to image configuration
186 'configuration' => array(
187 'ComponentsConfiguration' => MW_EXIF_UNDEFINED, # Meaning of each component #p33
188 'CompressedBitsPerPixel' => MW_EXIF_RATIONAL, # Image compression mode
189 'PixelYDimension' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Valid image width
190 'PixelXDimension' => MW_EXIF_SHORT.','.MW_EXIF_LONG, # Valind image height
191 ),
192
193 # Tags relating to related user information
194 'user' => array(
195 'MakerNote' => MW_EXIF_UNDEFINED, # Manufacturer notes
196 'UserComment' => MW_EXIF_UNDEFINED, # User comments #p34
197 ),
198
199 # Tags relating to related file information
200 'related' => array(
201 'RelatedSoundFile' => MW_EXIF_ASCII, # Related audio file
202 ),
203
204 # Tags relating to date and time
205 'dateandtime' => array(
206 'DateTimeOriginal' => MW_EXIF_ASCII, # Date and time of original data generation #p36
207 'DateTimeDigitized' => MW_EXIF_ASCII, # Date and time of original data generation
208 'SubSecTime' => MW_EXIF_ASCII, # DateTime subseconds
209 'SubSecTimeOriginal' => MW_EXIF_ASCII, # DateTimeOriginal subseconds
210 'SubSecTimeDigitized' => MW_EXIF_ASCII, # DateTimeDigitized subseconds
211 ),
212
213 # Tags relating to picture-taking conditions (p31)
214 'conditions' => array(
215 'ExposureTime' => MW_EXIF_RATIONAL, # Exposure time
216 'FNumber' => MW_EXIF_RATIONAL, # F Number
217 'ExposureProgram' => MW_EXIF_SHORT, # Exposure Program #p38
218 'SpectralSensitivity' => MW_EXIF_ASCII, # Spectral sensitivity
219 'ISOSpeedRatings' => MW_EXIF_SHORT, # ISO speed rating
220 'OECF' => MW_EXIF_UNDEFINED, # Optoelectronic conversion factor
221 'ShutterSpeedValue' => MW_EXIF_SRATIONAL, # Shutter speed
222 'ApertureValue' => MW_EXIF_RATIONAL, # Aperture
223 'BrightnessValue' => MW_EXIF_SRATIONAL, # Brightness
224 'ExposureBiasValue' => MW_EXIF_SRATIONAL, # Exposure bias
225 'MaxApertureValue' => MW_EXIF_RATIONAL, # Maximum land aperture
226 'SubjectDistance' => MW_EXIF_RATIONAL, # Subject distance
227 'MeteringMode' => MW_EXIF_SHORT, # Metering mode #p40
228 'LightSource' => MW_EXIF_SHORT, # Light source #p40-41
229 'Flash' => MW_EXIF_SHORT, # Flash #p41-42
230 'FocalLength' => MW_EXIF_RATIONAL, # Lens focal length
231 'SubjectArea' => MW_EXIF_SHORT, # Subject area
232 'FlashEnergy' => MW_EXIF_RATIONAL, # Flash energy
233 'SpatialFrequencyResponse' => MW_EXIF_UNDEFINED, # Spatial frequency response
234 'FocalPlaneXResolution' => MW_EXIF_RATIONAL, # Focal plane X resolution
235 'FocalPlaneYResolution' => MW_EXIF_RATIONAL, # Focal plane Y resolution
236 'FocalPlaneResolutionUnit' => MW_EXIF_SHORT, # Focal plane resolution unit #p46
237 'SubjectLocation' => MW_EXIF_SHORT, # Subject location
238 'ExposureIndex' => MW_EXIF_RATIONAL, # Exposure index
239 'SensingMethod' => MW_EXIF_SHORT, # Sensing method #p46
240 'FileSource' => MW_EXIF_UNDEFINED, # File source #p47
241 'SceneType' => MW_EXIF_UNDEFINED, # Scene type #p47
242 'CFAPattern' => MW_EXIF_UNDEFINED, # CFA pattern
243 'CustomRendered' => MW_EXIF_SHORT, # Custom image processing #p48
244 'ExposureMode' => MW_EXIF_SHORT, # Exposure mode #p48
245 'WhiteBalance' => MW_EXIF_SHORT, # White Balance #p49
246 'DigitalZoomRatio' => MW_EXIF_RATIONAL, # Digital zoom ration
247 'FocalLengthIn35mmFilm' => MW_EXIF_SHORT, # Focal length in 35 mm film
248 'SceneCaptureType' => MW_EXIF_SHORT, # Scene capture type #p49
249 'GainControl' => MW_EXIF_RATIONAL, # Scene control #p49-50
250 'Contrast' => MW_EXIF_SHORT, # Contrast #p50
251 'Saturation' => MW_EXIF_SHORT, # Saturation #p50
252 'Sharpness' => MW_EXIF_SHORT, # Sharpness #p50
253 'DeviceSettingDescription' => MW_EXIF_UNDEFINED, # Desice settings description
254 'SubjectDistanceRange' => MW_EXIF_SHORT, # Subject distance range #p51
255 ),
256
257 'other' => array(
258 'ImageUniqueID' => MW_EXIF_ASCII, # Unique image ID
259 ),
260 ),
261
262 # GPS Attribute Information (p52)
263 'gps' => array(
264 'GPSVersionID' => MW_EXIF_BYTE, # GPS tag version
265 'GPSLatitudeRef' => MW_EXIF_ASCII, # North or South Latitude #p52-53
266 'GPSLatitude' => MW_EXIF_RATIONAL, # Latitude
267 'GPSLongitudeRef' => MW_EXIF_ASCII, # East or West Longitude #p53
268 'GPSLongitude' => MW_EXIF_RATIONAL, # Longitude
269 'GPSAltitudeRef' => MW_EXIF_BYTE, # Altitude reference
270 'GPSAltitude' => MW_EXIF_RATIONAL, # Altitude
271 'GPSTimeStamp' => MW_EXIF_RATIONAL, # GPS time (atomic clock)
272 'GPSSatellites' => MW_EXIF_ASCII, # Satellites used for measurement
273 'GPSStatus' => MW_EXIF_ASCII, # Receiver status #p54
274 'GPSMeasureMode' => MW_EXIF_ASCII, # Measurement mode #p54-55
275 'GPSDOP' => MW_EXIF_RATIONAL, # Measurement precision
276 'GPSSpeedRef' => MW_EXIF_ASCII, # Speed unit #p55
277 'GPSSpeed' => MW_EXIF_RATIONAL, # Speed of GPS receiver
278 'GPSTrackRef' => MW_EXIF_ASCII, # Reference for direction of movement #p55
279 'GPSTrack' => MW_EXIF_RATIONAL, # Direction of movement
280 'GPSImgDirectionRef' => MW_EXIF_ASCII, # Reference for direction of image #p56
281 'GPSImgDirection' => MW_EXIF_RATIONAL, # Direction of image
282 'GPSMapDatum' => MW_EXIF_ASCII, # Geodetic survey data used
283 'GPSDestLatitudeRef' => MW_EXIF_ASCII, # Reference for latitude of destination #p56
284 'GPSDestLatitude' => MW_EXIF_RATIONAL, # Latitude destination
285 'GPSDestLongitudeRef' => MW_EXIF_ASCII, # Reference for longitude of destination #p57
286 'GPSDestLongitude' => MW_EXIF_RATIONAL, # Longitude of destination
287 'GPSDestBearingRef' => MW_EXIF_ASCII, # Reference for bearing of destination #p57
288 'GPSDestBearing' => MW_EXIF_RATIONAL, # Bearing of destination
289 'GPSDestDistanceRef' => MW_EXIF_ASCII, # Reference for distance to destination #p57-58
290 'GPSDestDistance' => MW_EXIF_RATIONAL, # Distance to destination
291 'GPSProcessingMethod' => MW_EXIF_UNDEFINED, # Name of GPS processing method
292 'GPSAreaInformation' => MW_EXIF_UNDEFINED, # Name of GPS area
293 'GPSDateStamp' => MW_EXIF_ASCII, # GPS date
294 'GPSDifferential' => MW_EXIF_SHORT, # GPS differential correction
295 ),
296 );
297
298 $this->file = $file;
299 $this->basename = basename( $this->file );
300
301 $this->makeFlatExifTags();
302
303 $this->debugFile( $this->basename, __FUNCTION__, true );
304 wfSuppressWarnings();
305 $data = exif_read_data( $this->file );
306 wfRestoreWarnings();
307 /**
308 * exif_read_data() will return false on invalid input, such as
309 * when somebody uploads a file called something.jpeg
310 * containing random gibberish.
311 */
312 $this->mRawExifData = $data ? $data : array();
313
314 $this->makeFilteredData();
315 $this->makeFormattedData();
316
317 $this->debugFile( __FUNCTION__, false );
318 }
319
320 /**#@+
321 * @access private
322 */
323 /**
324 * Generate a flat list of the exif tags
325 */
326 function makeFlatExifTags() {
327 $this->extractTags( $this->mExifTags );
328 }
329
330 /**
331 * A recursing extractor function used by makeFlatExifTags()
332 *
333 * Note: This used to use an array_walk function, but it made PHP5
334 * segfault, see `cvs diff -u -r 1.4 -r 1.5 Exif.php`
335 */
336 function extractTags( &$tagset ) {
337 foreach( $tagset as $key => $val ) {
338 if( is_array( $val ) ) {
339 $this->extractTags( $val );
340 } else {
341 $this->mFlatExifTags[$key] = $val;
342 }
343 }
344 }
345
346 /**
347 * Make $this->mFilteredExifData
348 */
349 function makeFilteredData() {
350 $this->mFilteredExifData = $this->mRawExifData;
351
352 foreach( $this->mFilteredExifData as $k => $v ) {
353 if ( !in_array( $k, array_keys( $this->mFlatExifTags ) ) ) {
354 $this->debug( $v, __FUNCTION__, "'$k' is not a valid Exif tag" );
355 unset( $this->mFilteredExifData[$k] );
356 }
357 }
358
359 foreach( $this->mFilteredExifData as $k => $v ) {
360 if ( !$this->validate($k, $v) ) {
361 $this->debug( $v, __FUNCTION__, "'$k' contained invalid data" );
362 unset( $this->mFilteredExifData[$k] );
363 }
364 }
365 }
366
367 function makeFormattedData( $data = null ) {
368 $format = new FormatExif( $this->getFilteredData() );
369 $this->mFormattedExifData = $format->getFormattedData();
370 }
371 /**#@-*/
372
373 /**#@+
374 * @return array
375 */
376 /**
377 * Get $this->mRawExifData
378 */
379 function getData() {
380 return $this->mRawExifData;
381 }
382
383 /**
384 * Get $this->mFilteredExifData
385 */
386 function getFilteredData() {
387 return $this->mFilteredExifData;
388 }
389
390 /**
391 * Get $this->mFormattedExifData
392 */
393 function getFormattedData() {
394 return $this->mFormattedExifData;
395 }
396 /**#@-*/
397
398 /**
399 * The version of the output format
400 *
401 * Before the actual metadata information is saved in the database we
402 * strip some of it since we don't want to save things like thumbnails
403 * which usually accompany Exif data. This value gets saved in the
404 * database along with the actual Exif data, and if the version in the
405 * database doesn't equal the value returned by this function the Exif
406 * data is regenerated.
407 *
408 * @return int
409 */
410 function version() {
411 return 1; // We don't need no bloddy constants!
412 }
413
414 /**#@+
415 * Validates if a tag value is of the type it should be according to the Exif spec
416 *
417 * @access private
418 *
419 * @param mixed $in The input value to check
420 * @return bool
421 */
422 function isByte( $in ) {
423 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 255 ) {
424 $this->debug( $in, __FUNCTION__, true );
425 return true;
426 } else {
427 $this->debug( $in, __FUNCTION__, false );
428 return false;
429 }
430 }
431
432 function isASCII( $in ) {
433 if ( preg_match( "/[^\x0a\x20-\x7e]/", $in ) ) {
434 $this->debug( $in, __FUNCTION__, 'found a character not in our whitelist' );
435 return false;
436 }
437
438 if ( preg_match( "/^\s*$/", $in ) ) {
439 $this->debug( $in, __FUNCTION__, 'input consisted solely of whitespace' );
440 return false;
441 }
442
443 return true;
444 }
445
446 function isShort( $in ) {
447 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 65536 ) {
448 $this->debug( $in, __FUNCTION__, true );
449 return true;
450 } else {
451 $this->debug( $in, __FUNCTION__, false );
452 return false;
453 }
454 }
455
456 function isLong( $in ) {
457 if ( sprintf('%d', $in) == $in && $in >= 0 && $in <= 4294967296 ) {
458 $this->debug( $in, __FUNCTION__, true );
459 return true;
460 } else {
461 $this->debug( $in, __FUNCTION__, false );
462 return false;
463 }
464 }
465
466 function isRational( $in ) {
467 if ( @preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
468 return $this->isLong( $m[1] ) && $this->isLong( $m[2] );
469 } else {
470 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
471 return false;
472 }
473 }
474
475 function isUndefined( $in ) {
476 if ( preg_match( "/^\d{4}$/", $in ) ) { // Allow ExifVersion and FlashpixVersion
477 $this->debug( $in, __FUNCTION__, true );
478 return true;
479 } else {
480 $this->debug( $in, __FUNCTION__, false );
481 return false;
482 }
483 }
484
485 function isSlong( $in ) {
486 if ( $this->isLong( abs( $in ) ) ) {
487 $this->debug( $in, __FUNCTION__, true );
488 return true;
489 } else {
490 $this->debug( $in, __FUNCTION__, false );
491 return false;
492 }
493 }
494
495 function isSrational( $in ) {
496 if ( preg_match( "/^(\d+)\/(\d+[1-9]|[1-9]\d*)$/", $in, $m ) ) { # Avoid division by zero
497 return $this->isSlong( $m[0] ) && $this->isSlong( $m[1] );
498 } else {
499 $this->debug( $in, __FUNCTION__, 'fed a non-fraction value' );
500 return false;
501 }
502 }
503 /**#@-*/
504
505 /**
506 * Validates if a tag has a legal value according to the Exif spec
507 *
508 * @access private
509 *
510 * @param string $tag The tag to check
511 * @param mixed $val The value of the tag
512 * @return bool
513 */
514 function validate( $tag, $val ) {
515 $debug = "tag is '$tag'";
516 // Fucks up if not typecast
517 switch( (string)$this->mFlatExifTags[$tag] ) {
518 case (string)MW_EXIF_BYTE:
519 $this->debug( $val, __FUNCTION__, $debug );
520 return $this->isByte( $val );
521 case (string)MW_EXIF_ASCII:
522 $this->debug( $val, __FUNCTION__, $debug );
523 return $this->isASCII( $val );
524 case (string)MW_EXIF_SHORT:
525 $this->debug( $val, __FUNCTION__, $debug );
526 return $this->isShort( $val );
527 case (string)MW_EXIF_LONG:
528 $this->debug( $val, __FUNCTION__, $debug );
529 return $this->isLong( $val );
530 case (string)MW_EXIF_RATIONAL:
531 $this->debug( $val, __FUNCTION__, $debug );
532 return $this->isRational( $val );
533 case (string)MW_EXIF_UNDEFINED:
534 $this->debug( $val, __FUNCTION__, $debug );
535 return $this->isUndefined( $val );
536 case (string)MW_EXIF_SLONG:
537 $this->debug( $val, __FUNCTION__, $debug );
538 return $this->isSlong( $val );
539 case (string)MW_EXIF_SRATIONAL:
540 $this->debug( $val, __FUNCTION__, $debug );
541 return $this->isSrational( $val );
542 case (string)MW_EXIF_SHORT.','.MW_EXIF_LONG:
543 $this->debug( $val, __FUNCTION__, $debug );
544 return $this->isShort( $val ) || $this->isLong( $val );
545 default:
546 $this->debug( $val, __FUNCTION__, "The tag '$tag' is unknown" );
547 return false;
548 }
549 }
550
551 /**
552 * Conviniance function for debugging output
553 *
554 * @access private
555 *
556 * @param mixed $in
557 * @param string $fname
558 * @param mixed $action
559 */
560 function debug( $in, $fname, $action = null ) {
561 $type = gettype( $in );
562 $class = ucfirst( __CLASS__ );
563 if ( $type === 'array' )
564 $in = print_r( $in, true );
565
566 if ( $action === true )
567 wfDebugLog( $this->log, "$class::$fname: accepted: '$in' (type: $type)\n");
568 elseif ( $action === false )
569 wfDebugLog( $this->log, "$class::$fname: rejected: '$in' (type: $type)\n");
570 elseif ( $action === null )
571 wfDebugLog( $this->log, "$class::$fname: input was: '$in' (type: $type)\n");
572 else
573 wfDebugLog( $this->log, "$class::$fname: $action (type: $type; content: '$in')\n");
574 }
575
576 /**
577 * Conviniance function for debugging output
578 *
579 * @access private
580 *
581 * @param string $basename The name of the file being processed
582 * @paran string $fname The name of the function calling this function
583 * @param bool $bool $io Specify whether we're beginning or ending
584 */
585 function debugFile( $fname, $io ) {
586 $class = ucfirst( __CLASS__ );
587 if ( $io )
588 wfDebugLog( $this->log, "$class::$fname: begin processing: '{$this->basename}'\n" );
589 else
590 wfDebugLog( $this->log, "$class::$fname: end processing: '{$this->basename}'\n" );
591 }
592
593 }
594
595 /**
596 * @package MediaWiki
597 * @subpackage Metadata
598 */
599 class FormatExif {
600 /**
601 * The Exif data to format
602 *
603 * @var array
604 * @access private
605 */
606 var $mExif;
607
608 /**
609 * Constructor
610 *
611 * @param array $exif The Exif data to format ( as returned by
612 * Exif::getFilteredData() )
613 */
614 function FormatExif( $exif ) {
615 $this->mExif = $exif;
616 }
617
618 /**
619 * Numbers given by Exif user agents are often magical, that is they
620 * should be replaced by a detailed explanation depending on their
621 * value which most of the time are plain integers. This function
622 * formats Exif values into human readable form.
623 *
624 * @return array
625 */
626 function getFormattedData() {
627 global $wgLang;
628
629 $tags =& $this->mExif;
630
631 $resolutionunit = !isset( $tags['ResolutionUnit'] ) || $tags['ResolutionUnit'] == 2 ? 2 : 3;
632 unset( $tags['ResolutionUnit'] );
633
634 foreach( $tags as $tag => $val ) {
635 switch( $tag ) {
636 case 'Compression':
637 switch( $val ) {
638 case 1: case 6:
639 $tags[$tag] = $this->msg( $tag, $val );
640 break;
641 default:
642 $tags[$tag] = $val;
643 break;
644 }
645 break;
646
647 case 'PhotometricInterpretation':
648 switch( $val ) {
649 case 2: case 6:
650 $tags[$tag] = $this->msg( $tag, $val );
651 break;
652 default:
653 $tags[$tag] = $val;
654 break;
655 }
656 break;
657
658 case 'Orientation':
659 switch( $val ) {
660 case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
661 $tags[$tag] = $this->msg( $tag, $val );
662 break;
663 default:
664 $tags[$tag] = $val;
665 break;
666 }
667 break;
668
669 case 'PlanarConfiguration':
670 switch( $val ) {
671 case 1: case 2:
672 $tags[$tag] = $this->msg( $tag, $val );
673 break;
674 default:
675 $tags[$tag] = $val;
676 break;
677 }
678 break;
679
680 // TODO: YCbCrSubSampling
681 // TODO: YCbCrPositioning
682
683 case 'XResolution':
684 case 'YResolution':
685 switch( $resolutionunit ) {
686 case 2:
687 $tags[$tag] = $this->msg( 'XYResolution', 'i', $this->formatNum( $val ) );
688 break;
689 case 3:
690 $this->msg( 'XYResolution', 'c', $this->formatNum( $val ) );
691 break;
692 default:
693 $tags[$tag] = $val;
694 break;
695 }
696 break;
697
698 // TODO: YCbCrCoefficients #p27 (see annex E)
699 case 'ExifVersion': case 'FlashpixVersion':
700 $tags[$tag] = "$val"/100;
701 break;
702
703 case 'ColorSpace':
704 switch( $val ) {
705 case 1: case 'FFFF.H':
706 $tags[$tag] = $this->msg( $tag, $val );
707 break;
708 default:
709 $tags[$tag] = $val;
710 break;
711 }
712 break;
713
714 case 'ComponentsConfiguration':
715 switch( $val ) {
716 case 0: case 1: case 2: case 3: case 4: case 5: case 6:
717 $tags[$tag] = $this->msg( $tag, $val );
718 break;
719 default:
720 $tags[$tag] = $val;
721 break;
722 }
723 break;
724
725 case 'DateTime':
726 case 'DateTimeOriginal':
727 case 'DateTimeDigitized':
728 $tags[$tag] = $wgLang->timeanddate( wfTimestamp(TS_MW, $val) );
729 break;
730
731 case 'ExposureProgram':
732 switch( $val ) {
733 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
734 $tags[$tag] = $this->msg( $tag, $val );
735 break;
736 default:
737 $tags[$tag] = $val;
738 break;
739 }
740 break;
741
742 case 'SubjectDistance':
743 $tags[$tag] = $this->msg( $tag, '', $this->formatNum( $val ) );
744 break;
745
746 case 'MeteringMode':
747 switch( $val ) {
748 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 255:
749 $tags[$tag] = $this->msg( $tag, $val );
750 break;
751 default:
752 $tags[$tag] = $val;
753 break;
754 }
755 break;
756
757 case 'LightSource':
758 switch( $val ) {
759 case 0: case 1: case 2: case 3: case 4: case 9: case 10: case 11:
760 case 12: case 13: case 14: case 15: case 17: case 18: case 19: case 20:
761 case 21: case 22: case 23: case 24: case 255:
762 $tags[$tag] = $this->msg( $tag, $val );
763 break;
764 default:
765 $tags[$tag] = $val;
766 break;
767 }
768 break;
769
770 // TODO: Flash
771 case 'FocalPlaneResolutionUnit':
772 switch( $val ) {
773 case 2:
774 $tags[$tag] = $this->msg( $tag, $val );
775 break;
776 default:
777 $tags[$tag] = $val;
778 break;
779 }
780 break;
781
782 case 'SensingMethod':
783 switch( $val ) {
784 case 1: case 2: case 3: case 4: case 5: case 7: case 8:
785 $tags[$tag] = $this->msg( $tag, $val );
786 break;
787 default:
788 $tags[$tag] = $val;
789 break;
790 }
791 break;
792
793 case 'FileSource':
794 switch( $val ) {
795 case 3:
796 $tags[$tag] = $this->msg( $tag, $val );
797 break;
798 default:
799 $tags[$tag] = $val;
800 break;
801 }
802 break;
803
804 case 'SceneType':
805 switch( $val ) {
806 case 1:
807 $tags[$tag] = $this->msg( $tag, $val );
808 break;
809 default:
810 $tags[$tag] = $val;
811 break;
812 }
813 break;
814
815 case 'CustomRendered':
816 switch( $val ) {
817 case 0: case 1:
818 $tags[$tag] = $this->msg( $tag, $val );
819 break;
820 default:
821 $tags[$tag] = $val;
822 break;
823 }
824 break;
825
826 case 'ExposureMode':
827 switch( $val ) {
828 case 0: case 1: case 2:
829 $tags[$tag] = $this->msg( $tag, $val );
830 break;
831 default:
832 $tags[$tag] = $val;
833 break;
834 }
835 break;
836
837 case 'WhiteBalance':
838 switch( $val ) {
839 case 0: case 1:
840 $tags[$tag] = $this->msg( $tag, $val );
841 break;
842 default:
843 $tags[$tag] = $val;
844 break;
845 }
846 break;
847
848 case 'SceneCaptureType':
849 switch( $val ) {
850 case 0: case 1: case 2: case 3:
851 $tags[$tag] = $this->msg( $tag, $val );
852 break;
853 default:
854 $tags[$tag] = $val;
855 break;
856 }
857 break;
858
859 case 'GainControl':
860 switch( $val ) {
861 case 0: case 1: case 2: case 3: case 4:
862 $tags[$tag] = $this->msg( $tag, $val );
863 break;
864 default:
865 $tags[$tag] = $val;
866 break;
867 }
868 break;
869
870 case 'Contrast':
871 switch( $val ) {
872 case 0: case 1: case 2:
873 $tags[$tag] = $this->msg( $tag, $val );
874 break;
875 default:
876 $tags[$tag] = $val;
877 break;
878 }
879 break;
880
881 case 'Saturation':
882 switch( $val ) {
883 case 0: case 1: case 2:
884 $tags[$tag] = $this->msg( $tag, $val );
885 break;
886 default:
887 $tags[$tag] = $val;
888 break;
889 }
890 break;
891
892 case 'Sharpness':
893 switch( $val ) {
894 case 0: case 1: case 2:
895 $tags[$tag] = $this->msg( $tag, $val );
896 break;
897 default:
898 $tags[$tag] = $val;
899 break;
900 }
901 break;
902
903 case 'SubjectDistanceRange':
904 switch( $val ) {
905 case 0: case 1: case 2: case 3:
906 $tags[$tag] = $this->msg( $tag, $val );
907 break;
908 default:
909 $tags[$tag] = $val;
910 break;
911 }
912 break;
913
914 case 'GPSLatitudeRef':
915 case 'GPSDestLatitudeRef':
916 switch( $val ) {
917 case 'N': case 'S':
918 $tags[$tag] = $this->msg( 'GPSLatitude', $val );
919 break;
920 default:
921 $tags[$tag] = $val;
922 break;
923 }
924 break;
925
926 case 'GPSLongitudeRef':
927 case 'GPSDestLongitudeRef':
928 switch( $val ) {
929 case 'E': case 'W':
930 $tags[$tag] = $this->msg( 'GPSLongitude', $val );
931 break;
932 default:
933 $tags[$tag] = $val;
934 break;
935 }
936 break;
937
938 case 'GPSStatus':
939 switch( $val ) {
940 case 'A': case 'V':
941 $tags[$tag] = $this->msg( $tag, $val );
942 break;
943 default:
944 $tags[$tag] = $val;
945 break;
946 }
947 break;
948
949 case 'GPSMeasureMode':
950 switch( $val ) {
951 case 2: case 3:
952 $tags[$tag] = $this->msg( $tag, $val );
953 break;
954 default:
955 $tags[$tag] = $val;
956 break;
957 }
958 break;
959
960 case 'GPSSpeedRef':
961 case 'GPSDestDistanceRef':
962 switch( $val ) {
963 case 'K': case 'M': case 'N':
964 $tags[$tag] = $this->msg( 'GPSSpeed', $val );
965 break;
966 default:
967 $tags[$tag] = $val;
968 break;
969 }
970 break;
971
972 case 'GPSTrackRef':
973 case 'GPSImgDirectionRef':
974 case 'GPSDestBearingRef':
975 switch( $val ) {
976 case 'T': case 'M':
977 $tags[$tag] = $this->msg( 'GPSDirection', $val );
978 break;
979 default:
980 $tags[$tag] = $val;
981 break;
982 }
983 break;
984
985 case 'GPSDateStamp':
986 $tags[$tag] = $wgLang->date( substr( $val, 0, 4 ) . substr( $val, 5, 2 ) . substr( $val, 8, 2 ) . '000000' );
987 break;
988
989 // This is not in the Exif standard, just a special
990 // case for our purposes which enables wikis to wikify
991 // the make, model and software name to link to their articles.
992 case 'Make':
993 case 'Model':
994 case 'Software':
995 $tags[$tag] = $this->msg( $tag, '', $val );
996 break;
997
998 case 'ExposureTime':
999 // Show the pretty fraction as well as decimal version
1000 $tags[$tag] = wfMsg( 'exif-exposuretime-format',
1001 $val, $this->formatNum( $val ) );
1002 break;
1003
1004 case 'FNumber':
1005 $tags[$tag] = wfMsg( 'exif-fnumber-format',
1006 $this->formatNum( $val ) );
1007 break;
1008
1009 case 'FocalLength':
1010 $tags[$tag] = wfMsg( 'exif-focallength-format',
1011 $this->formatNum( $val ) );
1012 break;
1013
1014 default:
1015 $tags[$tag] = $this->formatNum( $val );
1016 break;
1017 }
1018 }
1019
1020 return $tags;
1021 }
1022
1023 /**
1024 * Conviniance function for getFormattedData()
1025 *
1026 * @access private
1027 *
1028 * @param string $tag The tag name to pass on
1029 * @param string $val The value of the tag
1030 * @param string $arg An argument to pass ($1)
1031 * @return string A wfMsg of "exif-$tag-$val" in lower case
1032 */
1033 function msg( $tag, $val, $arg = null ) {
1034 global $wgContLang;
1035
1036 if ($val === '')
1037 $val = 'value';
1038 return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg );
1039 }
1040
1041 /**
1042 * Format a number, convert numbers from fractions into floating point
1043 * numbers
1044 *
1045 * @access private
1046 *
1047 * @param mixed $num The value to format
1048 * @return mixed A floating point number or whatever we were fed
1049 */
1050 function formatNum( $num ) {
1051 if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) )
1052 return $m[2] != 0 ? $m[1] / $m[2] : $num;
1053 else
1054 return $num;
1055 }
1056 }