Avoid undefined variable error on null edits
[lhc/web/wiklou.git] / includes / Exif.php
index b2b43c1..9c3965a 100644 (file)
@@ -81,14 +81,28 @@ class Exif {
        var $mFormattedExifData;
        
        /**#@-*/
+       
+       /**#@+
+        * @var string
+        * @access private
+        */
+       
+       /**
+        * The file being processed
+        */
+       var $file;
 
+       /**
+        * The basename of the file being processed
+        */
+       var $basename;
+       
        /**
         * The private log to log to
-        *
-        * @var string
-        * @access private
         */
        var $log = 'exif';
+       
+       /**#@-*/
 
        /**
         * Constructor
@@ -281,13 +295,14 @@ class Exif {
                        ),
                );
 
-               $basename = basename( $file );
+               $this->file = $file;
+               $this->basename = basename( $this->file );
                
                $this->makeFlatExifTags();
                
-               $this->debugFile( $basename, __FUNCTION__, true );
+               $this->debugFile( $this->basename, __FUNCTION__, true );
                wfSuppressWarnings();
-               $data = exif_read_data( $file );
+               $data = exif_read_data( $this->file );
                wfRestoreWarnings();
                /**
                 * exif_read_data() will return false on invalid input, such as
@@ -299,7 +314,7 @@ class Exif {
                $this->makeFilteredData();
                $this->makeFormattedData();
                
-               $this->debugFile( $basename, __FUNCTION__, false );
+               $this->debugFile( __FUNCTION__, false );
        }
        
        /**#@+
@@ -567,12 +582,12 @@ class Exif {
         * @paran string $fname The name of the function calling this function
         * @param bool $bool $io Specify whether we're beginning or ending
         */
-       function debugFile( $basename, $fname, $io ) {
+       function debugFile( $fname, $io ) {
                $class = ucfirst( __CLASS__ );
                if ( $io )
-                       wfDebugLog( $this->log, "$class::$fname: begin processing: '$basename'\n" );
+                       wfDebugLog( $this->log, "$class::$fname: begin processing: '{$this->basename}'\n" );
                else
-                       wfDebugLog( $this->log, "$class::$fname: end processing: '$basename'\n" );
+                       wfDebugLog( $this->log, "$class::$fname: end processing: '{$this->basename}'\n" );
        }
 
 }
@@ -979,6 +994,23 @@ class FormatExif {
                        case 'Software':
                                $tags[$tag] = $this->msg( $tag, '', $val );
                                break;
+                       
+                       case 'ExposureTime':
+                               // Show the pretty fraction as well as decimal version
+                               $tags[$tag] = wfMsg( 'exif-exposuretime-format',
+                                       $this->formatFraction( $val ), $this->formatNum( $val ) );
+                               break;
+                       
+                       case 'FNumber':
+                               $tags[$tag] = wfMsg( 'exif-fnumber-format',
+                                       $this->formatNum( $val ) );
+                               break;
+                       
+                       case 'FocalLength':
+                               $tags[$tag] = wfMsg( 'exif-focallength-format',
+                                       $this->formatNum( $val ) );
+                               break;
+                       
                        default:
                                $tags[$tag] = $this->formatNum( $val );
                                break;
@@ -999,9 +1031,11 @@ class FormatExif {
         * @return string A wfMsg of "exif-$tag-$val" in lower case
         */
        function msg( $tag, $val, $arg = null ) {
+               global $wgContLang;
+               
                if ($val === '')
                        $val = 'value';
-               return wfMsg( strtolower( "exif-$tag-$val" ), $arg );
+               return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg );
        }
 
        /**
@@ -1019,4 +1053,52 @@ class FormatExif {
                else
                        return $num;
        }
+       
+       /**
+        * Format a rational number, reducing fractions
+        *
+        * @access private
+        *
+        * @param mixed $num The value to format
+        * @return mixed A floating point number or whatever we were fed
+        */
+       function formatFraction( $num ) {
+               if ( preg_match( '/^(\d+)\/(\d+)$/', $num, $m ) ) {
+                       $numerator = intval( $m[1] );
+                       $denominator = intval( $m[2] );
+                       $gcd = $this->gcd( $numerator, $denominator );
+                       if( $gcd != 0 ) {
+                               // 0 shouldn't happen! ;)
+                               return $numerator / $gcd . '/' . $denominator / $gcd;
+                       }
+               }
+               return $this->formatNum( $num );
+       }
+
+       /**
+        * Calculate the greatest common divisor of two integers.
+        *
+        * @param int $a
+        * @param int $b
+        * @return int
+        * @access private
+        */
+       function gcd( $a, $b ) {
+               /*
+                       // http://en.wikipedia.org/wiki/Euclidean_algorithm
+                       // Recursive form would be:
+                       if( $b == 0 )
+                               return $a;
+                       else
+                               return gcd( $b, $a % $b );
+               */
+               while( $b != 0 ) {
+                       $remainder = $a % $b;
+                       
+                       // tail recursion...
+                       $a = $b;
+                       $b = $remainder;
+               }
+               return $a;
+       }
 }