* using htmlspecialchars() for safe XHTML output
[lhc/web/wiklou.git] / includes / Image.php
index 1314b4a..6644084 100644 (file)
@@ -72,8 +72,6 @@ class Image
        }
        
        function Image( $title ) {
-               global $wgShowEXIF;
-               
                if( !is_object( $title ) ) {
                        wfDebugDieBacktrace( 'Image constructor given bogus title.' );
                }
@@ -197,7 +195,7 @@ class Image
                                'metadata'   => $this->metadata,
                                'size'       => $this->size );
 
-                       $wgMemc->set( $keys[0], $cachedValues );
+                       $wgMemc->set( $keys[0], $cachedValues, 60 * 60 * 24 * 7 ); // A week
                } else {
                        // However we should clear them, so they aren't leftover
                        // if we've deleted the file.
@@ -582,7 +580,7 @@ class Image
                
                if (!$mime || $mime==='unknown' || $mime==='unknown/unknown') return false;
                
-               #if it's SVG, check if ther's a converter enabled    
+               #if it's SVG, check if there's a converter enabled    
                if ($mime === 'image/svg') {
                        global $wgSVGConverters, $wgSVGConverter;
                        
@@ -915,25 +913,36 @@ class Image
        function renderThumb( $width, $useScript = true ) {
                global $wgUseSquid, $wgInternalServer;
                global $wgThumbnailScriptPath, $wgSharedThumbnailScriptPath;
+
+               $fname = 'Image::renderThumb';
+               wfProfileIn( $fname );
                
-               $width = IntVal( $width );
+               $width = intval( $width );
 
                $this->load();
                if ( ! $this->exists() )
                {
                        # If there is no image, there will be no thumbnail
+                       wfProfileOut( $fname );
                        return null;
                }
                
                # Sanity check $width
                if( $width <= 0 || $this->width <= 0) {
                        # BZZZT
+                       wfProfileOut( $fname );
                        return null;
                }
 
-               if( $width >= $this->width && !$this->mustRender() ) {
+               global $wgSVGMaxSize;
+               $maxsize = $this->mustRender()
+                       ? max( $this->width, $wgSVGMaxSize )
+                       : $this->width - 1;
+               if( $width > $maxsize ) {
                        # Don't make an image bigger than the source
-                       return new ThumbnailImage( $this->getViewURL(), $this->getWidth(), $this->getHeight() );
+                       $thumb = new ThumbnailImage( $this->getViewURL(), $this->getWidth(), $this->getHeight() );
+                       wfProfileOut( $fname );
+                       return $thumb;
                }
                
                $height = floor( $this->height * ( $width/$this->width ) );
@@ -941,7 +950,9 @@ class Image
                list( $isScriptUrl, $url ) = $this->thumbUrl( $width );
                if ( $isScriptUrl && $useScript ) {
                        // Use thumb.php to render the image
-                       return new ThumbnailImage( $url, $width, $height );
+                       $thumb = new ThumbnailImage( $url, $width, $height );
+                       wfProfileOut( $fname );
+                       return $thumb;
                }
 
                $thumbName = $this->thumbName( $width, $this->fromSharedDirectory );
@@ -976,7 +987,9 @@ class Image
                        }
                }
                
-               return new ThumbnailImage( $url, $width, $height, $thumbPath );
+               $thumb = new ThumbnailImage( $url, $width, $height, $thumbPath );
+               wfProfileOut( $fname );
+               return $thumb;
        } // END OF function renderThumb
 
        /**
@@ -998,12 +1011,14 @@ class Image
                        if( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
                                global $wgSVGConverterPath;
                                $cmd = str_replace(
-                                       array( '$path/', '$width', '$input', '$output' ),
-                                       array( $wgSVGConverterPath,
-                                                  $width,
+                                       array( '$path/', '$width', '$height', '$input', '$output' ),
+                                       array( $wgSVGConverterPath ? "$wgSVGConverterPath/" : "",
+                                                  intval( $width ),
+                                                  intval( $height ),
                                                   wfEscapeShellArg( $this->imagePath ),
                                                   wfEscapeShellArg( $thumbPath ) ),
                                        $wgSVGConverters[$wgSVGConverter] );
+                               wfDebug( "reallyRenderThumb SVG: $cmd\n" );
                                $conv = shell_exec( $cmd );
                        } else {
                                $conv = false;
@@ -1013,10 +1028,10 @@ class Image
                        # Specify white background color, will be used for transparent images
                        # in Internet Explorer/Windows instead of default black.
                        $cmd  =  $wgImageMagickConvertCommand .
-                               " -quality 85 -background white -geometry {$width} ".
-                               wfEscapeShellArg($this->imagePath) . " " .
+                               " -quality 85 -background white -size {$width}x{$height} ".
+                               wfEscapeShellArg($this->imagePath) . " -resize {$width}x{$height} " .
                                wfEscapeShellArg($thumbPath);                           
-                       wfDebug("reallyRenderThumb: running ImageMagick: $cmd");
+                       wfDebug("reallyRenderThumb: running ImageMagick: $cmd\n");
                        $conv = shell_exec( $cmd );
                } else {
                        # Use PHP's builtin GD library functions.
@@ -1244,7 +1259,7 @@ class Image
        /**
         * Record an image upload in the upload log and the image table
         */
-       function recordUpload( $oldver, $desc, $copyStatus = '', $source = '' ) {
+       function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '' ) {
                global $wgUser, $wgLang, $wgTitle, $wgDeferredUpdateList;
                global $wgUseCopyrightUpload, $wgUseSquid, $wgPostCommitUpdateList;
 
@@ -1263,11 +1278,21 @@ class Image
                }
 
                if ( $wgUseCopyrightUpload ) {
+                       if ( $license != '' ) {
+                               $licensetxt = '== ' . wfMsg( 'license' ) . " ==\n" . '{{' . $license . '}}' . "\n";
+                       }
                        $textdesc = '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n" .
                          '== ' . wfMsg ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
+                         "$licensetxt" .
                          '== ' . wfMsg ( 'filesource' ) . " ==\n" . $source ;
                } else {
-                       $textdesc = $desc;
+                       if ( $license != '' ) {
+                               $filedesc = $desc == '' ? '' : '== ' . wfMsg ( 'filedesc' ) . " ==\n" . $desc . "\n";
+                                $textdesc = $filedesc . 
+                                        '== ' . wfMsg ( 'license' ) . " ==\n" . '{{' . $license . '}}' . "\n"; 
+                       } else {
+                               $textdesc = $desc;
+                       }
                }
 
                $now = $dbw->timestamp();
@@ -1288,8 +1313,8 @@ class Image
                        array(
                                'img_name' => $this->name,
                                'img_size'=> $this->size,
-                               'img_width' => IntVal( $this->width ),
-                               'img_height' => IntVal( $this->height ),
+                               'img_width' => intval( $this->width ),
+                               'img_height' => intval( $this->height ),
                                'img_bits' => $this->bits,
                                'img_media_type' => $this->type,
                                'img_major_mime' => $major,
@@ -1402,6 +1427,7 @@ class Image
                        }
                }
                $db->freeResult( $res );
+               wfProfileOut( $fname );
                return $retVal;
        }
        /**
@@ -1412,30 +1438,33 @@ class Image
         *
         * @return array
         */
-       function retrieveExifData () {
-               if ( $this->getMimeType() !== "image/jpeg" ) return array ();
+       function retrieveExifData() {
+               if ( $this->getMimeType() !== "image/jpeg" ) 
+                       return array();
 
                $exif = new Exif( $this->imagePath );
                return $exif->getFilteredData();
        }
                
-       function getExifData () {
+       function getExifData() {
                global $wgRequest;
                if ( $this->metadata === '0' )
                        return array();
                
                $purge = $wgRequest->getVal( 'action' ) == 'purge';
-               $ret = unserialize ( $this->metadata );
+               $ret = unserialize( $this->metadata );
 
                $oldver = isset( $ret['MEDIAWIKI_EXIF_VERSION'] ) ? $ret['MEDIAWIKI_EXIF_VERSION'] : 0;
                $newver = Exif::version();
                
                if ( !count( $ret ) || $purge || $oldver != $newver ) {
+                       $this->purgeCache();
                        $this->updateExifData( $newver );
                }
                if ( isset( $ret['MEDIAWIKI_EXIF_VERSION'] ) )
                        unset( $ret['MEDIAWIKI_EXIF_VERSION'] );
                $format = new FormatExif( $ret );
+               
                return $format->getFormattedData();
        }
 
@@ -1465,6 +1494,16 @@ class Image
                        $fname
                );
        }
+       
+       /**
+        * Returns true if the image does not come from the shared
+        * image repository.
+        *
+        * @return bool
+        */
+       function isLocal() {
+               return !$this->fromSharedDirectory;
+       }
 
 } //class
 
@@ -1633,13 +1672,13 @@ function wfScaleSVGUnit( $length ) {
                ''   => 1.0, // "User units" pixels by default
                '%'  => 2.0, // Fake it!
                );
-       if( preg_match( '/^(\d+)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
-               $length = FloatVal( $matches[1] );
+       if( preg_match( '/^(\d+(?:\.\d+)?)(em|ex|px|pt|pc|cm|mm|in|%|)$/', $length, $matches ) ) {
+               $length = floatval( $matches[1] );
                $unit = $matches[2];
                return round( $length * $unitLength[$unit] );
        } else {
                // Assume pixels
-               return round( FloatVal( $length ) );
+               return round( floatval( $length ) );
        }
 }