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