API:
[lhc/web/wiklou.git] / includes / Image.php
index c03466b..adcee14 100644 (file)
@@ -1,6 +1,5 @@
 <?php
 /**
- * @package MediaWiki
  */
 
 /**
 /**
  * Bump this number when serialized cache records may be incompatible.
  */
-define( 'MW_IMAGE_VERSION', 1 );
+define( 'MW_IMAGE_VERSION', 2 );
 
 /**
  * Class to represent an image
  *
  * Provides methods to retrieve paths (physical, logical, URL),
  * to generate thumbnails or for uploading.
- * @package MediaWiki
+ *
+ * @addtogroup Media
  */
 class Image
 {
+       const DELETED_FILE = 1;
+       const DELETED_COMMENT = 2;
+       const DELETED_USER = 4;
+       const DELETED_RESTRICTED = 8;
+       const RENDER_NOW = 1;
+    
        /**#@+
         * @private
         */
@@ -43,6 +49,7 @@ class Image
                $attr,          # /
                $type,          # MEDIATYPE_xxx (bitmap, drawing, audio...)
                $mime,          # MIME type, determined by MimeMagic::guessMimeType
+               $extension,     # The file extension (constructor)
                $size,          # Size in bytes (loadFromXxx)
                $metadata,      # Metadata
                $dataLoaded,    # Whether or not all this has been loaded from the database (loadFromXxx)
@@ -56,9 +63,10 @@ class Image
         * Create an Image object from an image name
         *
         * @param string $name name of the image, used to create a title object using Title::makeTitleSafe
+        * @return Image
         * @public
         */
-       function newFromName( $name ) {
+       public static function newFromName( $name ) {
                $title = Title::makeTitleSafe( NS_IMAGE, $name );
                if ( is_object( $title ) ) {
                        return new Image( $title );
@@ -69,35 +77,40 @@ class Image
 
        /**
         * Obsolete factory function, use constructor
+        * @param Title $title
+        * @return Image
         * @deprecated
         */
        function newFromTitle( $title ) {
                return new Image( $title );
        }
 
+       /**
+        * Constructor
+        * @param Title $title
+        * @return void
+        */
        function Image( $title ) {
                if( !is_object( $title ) ) {
                        throw new MWException( 'Image constructor given bogus title.' );
                }
                $this->title =& $title;
                $this->name = $title->getDBkey();
-               $this->metadata = serialize ( array() ) ;
+               $this->metadata = '';
 
                $n = strrpos( $this->name, '.' );
                $this->extension = Image::normalizeExtension( $n ?
                        substr( $this->name, $n + 1 ) : '' );
                $this->historyLine = 0;
-               $this->page = 1;
 
                $this->dataLoaded = false;
        }
 
-       
        /**
         * Normalize a file extension to the common form, and ensure it's clean.
         * Extensions with non-alphanumeric characters will be discarded.
         *
-        * @param $ext string (without the .)
+        * @param string $ext (without the .)
         * @return string
         */
        static function normalizeExtension( $ext ) {
@@ -115,10 +128,10 @@ class Image
                        return '';
                }
        }
-       
+
        /**
         * Get the memcached keys
-        * Returns an array, first element is the local cache key, second is the shared cache key, if there is one
+        * @return array[int]mixed Returns an array, first element is the local cache key, second is the shared cache key, if there is one
         */
        function getCacheKeys( ) {
                global $wgUseSharedUploads, $wgSharedUploadDBname, $wgCacheSharedUploads;
@@ -235,7 +248,7 @@ class Image
         * Load metadata from the file itself
         */
        function loadFromFile() {
-               global $wgUseSharedUploads, $wgSharedUploadDirectory, $wgContLang, $wgShowEXIF;
+               global $wgUseSharedUploads, $wgSharedUploadDirectory, $wgContLang;
                wfProfileIn( __METHOD__ );
                $this->imagePath = $this->getFullPath();
                $this->fileExists = file_exists( $this->imagePath );
@@ -264,32 +277,26 @@ class Image
 
                        $this->mime = $magic->guessMimeType($this->imagePath,true);
                        $this->type = $magic->getMediaType($this->imagePath,$this->mime);
+                       $handler = MediaHandler::getHandler( $this->mime );
 
                        # Get size in bytes
                        $this->size = filesize( $this->imagePath );
 
-                       $magic=& MimeMagic::singleton();
-
-                       # Height and width
-                       wfSuppressWarnings();
-                       if( $this->mime == 'image/svg' ) {
-                               $gis = wfGetSVGsize( $this->imagePath );
-                       } elseif( $this->mime == 'image/vnd.djvu' ) {
-                               $deja = new DjVuImage( $this->imagePath );
-                               $gis = $deja->getImageSize();
-                       } elseif ( !$magic->isPHPImageType( $this->mime ) ) {
-                               # Don't try to get the width and height of sound and video files, that's bad for performance
-                               $gis = false;
+                       # Height, width and metadata
+                       if ( $handler ) {
+                               $gis = $handler->getImageSize( $this, $this->imagePath );
+                               $this->metadata = $handler->getMetadata( $this, $this->imagePath );
                        } else {
-                               $gis = getimagesize( $this->imagePath );
+                               $gis = false;
+                               $this->metadata = '';
                        }
-                       wfRestoreWarnings();
 
                        wfDebug(__METHOD__.': '.$this->imagePath." loaded, ".$this->size." bytes, ".$this->mime.".\n");
                }
                else {
                        $this->mime = NULL;
                        $this->type = MEDIATYPE_UNKNOWN;
+                       $this->metadata = '';
                        wfDebug(__METHOD__.': '.$this->imagePath." NOT FOUND!\n");
                }
 
@@ -308,13 +315,6 @@ class Image
                # as ther's only one thread of execution, this should be safe anyway.
                $this->dataLoaded = true;
 
-
-               if ( $this->mime == 'image/vnd.djvu' ) {
-                       $this->metadata = $deja->retrieveMetaData();
-               } else {
-                       $this->metadata = serialize( $this->retrieveExifData( $this->imagePath ) );
-               }
-
                if ( isset( $gis['bits'] ) )  $this->bits = $gis['bits'];
                else $this->bits = 0;
 
@@ -328,8 +328,7 @@ class Image
                global $wgUseSharedUploads, $wgSharedUploadDBname, $wgSharedUploadDBprefix, $wgContLang;
                wfProfileIn( __METHOD__ );
 
-               $dbr =& wfGetDB( DB_SLAVE );
-               $this->checkDBSchema($dbr);
+               $dbr = wfGetDB( DB_SLAVE );
 
                $row = $dbr->selectRow( 'image',
                        array( 'img_size', 'img_width', 'img_height', 'img_bits',
@@ -341,15 +340,13 @@ class Image
                        $this->loadFromRow( $row );
                        $this->imagePath = $this->getFullPath();
                        // Check for rows from a previous schema, quietly upgrade them
-                       if ( is_null($this->type) ) {
-                               $this->upgradeRow();
-                       }
+                       $this->maybeUpgradeRow();
                } elseif ( $wgUseSharedUploads && $wgSharedUploadDBname ) {
                        # In case we're on a wgCapitalLinks=false wiki, we
                        # capitalize the first letter of the filename before
                        # looking it up in the shared repository.
                        $name = $wgContLang->ucfirst($this->name);
-                       $dbc =& wfGetDB( DB_SLAVE, 'commons' );
+                       $dbc = Image::getCommonsDB();
 
                        $row = $dbc->selectRow( "`$wgSharedUploadDBname`.{$wgSharedUploadDBprefix}image",
                                array(
@@ -364,9 +361,7 @@ class Image
                                $this->loadFromRow( $row );
 
                                // Check for rows from a previous schema, quietly upgrade them
-                               if ( is_null($this->type) ) {
-                                       $this->upgradeRow();
-                               }
+                               $this->maybeUpgradeRow();
                        }
                }
 
@@ -378,7 +373,7 @@ class Image
                        $this->type = 0;
                        $this->fileExists = false;
                        $this->fromSharedDirectory = false;
-                       $this->metadata = serialize ( array() ) ;
+                       $this->metadata = '';
                        $this->mime = false;
                }
 
@@ -405,9 +400,7 @@ class Image
                        if (!$minor) $minor= "unknown";
                        $this->mime = $major.'/'.$minor;
                }
-
                $this->metadata = $row->img_metadata;
-               if ( $this->metadata == "" ) $this->metadata = serialize ( array() ) ;
 
                $this->dataLoaded = true;
        }
@@ -434,8 +427,22 @@ class Image
        }
 
        /**
-        * Metadata was loaded from the database, but the row had a marker indicating it needs to be
-        * upgraded from the 1.4 schema, which had no width, height, bits or type. Upgrade the row.
+        * Upgrade a row if it needs it
+        * @return void
+        */
+       function maybeUpgradeRow() {
+               if ( is_null($this->type) || $this->mime == 'image/svg' ) {
+                       $this->upgradeRow();
+               } else {
+                       $handler = $this->getHandler();
+                       if ( $handler && !$handler->isMetadataValid( $this, $this->metadata ) ) {
+                               $this->upgradeRow();
+                       }
+               }
+       }
+
+       /**
+        * Fix assorted version-related problems with the image row by reloading it from the file
         */
        function upgradeRow() {
                global $wgDBname, $wgSharedUploadDBname;
@@ -451,17 +458,14 @@ class Image
 
                        // Write to the other DB using selectDB, not database selectors
                        // This avoids breaking replication in MySQL
-                       $dbw =& wfGetDB( DB_MASTER, 'commons' );
-                       $dbw->selectDB( $wgSharedUploadDBname );
+                       $dbw = Image::getCommonsDB();
                } else {
-                       $dbw =& wfGetDB( DB_MASTER );
+                       $dbw = wfGetDB( DB_MASTER );
                }
 
-               $this->checkDBSchema($dbw);
-
                list( $major, $minor ) = self::splitMime( $this->mime );
 
-               wfDebug(__METHOD__.': upgrading '.$this->name." to 1.5 schema\n");
+               wfDebug(__METHOD__.': upgrading '.$this->name." to the current schema\n");
 
                $dbw->update( 'image',
                        array(
@@ -479,12 +483,12 @@ class Image
                }
                wfProfileOut( __METHOD__ );
        }
-       
+
        /**
         * Split an internet media type into its two components; if not
         * a two-part name, set the minor type to 'unknown'.
         *
-        * @param $mime "text/html" etc
+        * @param string $mime "text/html" etc
         * @return array ("text", "html") etc
         */
        static function splitMime( $mime ) {
@@ -552,29 +556,53 @@ class Image
        }
 
        /**
-        * Return the width of the image
-        *
-        * Returns -1 if the file specified is not a known image type
+        * @return mixed Return the width of the image; returns false on error.
+        * @param int $page Page number to find the width of.
         * @public
         */
-       function getWidth() {
+       function getWidth( $page = 1 ) {
                $this->load();
-               return $this->width;
+               if ( $this->isMultipage() ) {
+                       $dim = $this->getHandler()->getPageDimensions( $this, $page );
+                       if ( $dim ) {
+                               return $dim['width'];
+                       } else {
+                               return false;
+                       }
+               } else {
+                       return $this->width;
+               }
        }
 
        /**
-        * Return the height of the image
-        *
-        * Returns -1 if the file specified is not a known image type
+        * @return mixed Return the height of the image; Returns false on error.
+        * @param int $page Page number to find the height of.
         * @public
         */
-       function getHeight() {
+       function getHeight( $page = 1 ) {
+               $this->load();
+               if ( $this->isMultipage() ) {
+                       $dim = $this->getHandler()->getPageDimensions( $this, $page );
+                       if ( $dim ) {
+                               return $dim['height'];
+                       } else {
+                               return false;
+                       }
+               } else {
+                       return $this->height;
+               }
+       }
+
+       /**
+        * Get handler-specific metadata
+        */
+       function getMetadata() {
                $this->load();
-               return $this->height;
+               return $this->metadata;
        }
 
        /**
-        * Return the size of the image file, in bytes
+        * @return int the size of the image file, in bytes
         * @public
         */
        function getSize() {
@@ -583,7 +611,7 @@ class Image
        }
 
        /**
-        * Returns the mime type of the file.
+        * @return string the mime type of the file.
         */
        function getMimeType() {
                $this->load();
@@ -608,60 +636,13 @@ class Image
         * or if it is an SVG image and SVG conversion is enabled.
         *
         * @todo remember the result of this check.
+        * @return boolean
         */
        function canRender() {
-               global $wgUseImageMagick, $wgDjvuRenderer;
-
-               if( $this->getWidth()<=0 || $this->getHeight()<=0 ) return false;
-
-               $mime= $this->getMimeType();
-
-               if (!$mime || $mime==='unknown' || $mime==='unknown/unknown') return false;
-
-               #if it's SVG, check if there's a converter enabled
-               if ($mime === 'image/svg') {
-                       global $wgSVGConverters, $wgSVGConverter;
-
-                       if ($wgSVGConverter && isset( $wgSVGConverters[$wgSVGConverter])) {
-                               wfDebug( "Image::canRender: SVG is ready!\n" );
-                               return true;
-                       } else {
-                               wfDebug( "Image::canRender: SVG renderer missing\n" );
-                       }
-               }
-
-               #image formats available on ALL browsers
-               if (  $mime === 'image/gif'
-                  || $mime === 'image/png'
-                  || $mime === 'image/jpeg' ) return true;
-
-               #image formats that can be converted to the above formats
-               if ($wgUseImageMagick) {
-                       #convertable by ImageMagick (there are more...)
-                       if ( $mime === 'image/vnd.wap.wbmp'
-                         || $mime === 'image/x-xbitmap'
-                         || $mime === 'image/x-xpixmap'
-                         #|| $mime === 'image/x-icon'   #file may be split into multiple parts
-                         || $mime === 'image/x-portable-anymap'
-                         || $mime === 'image/x-portable-bitmap'
-                         || $mime === 'image/x-portable-graymap'
-                         || $mime === 'image/x-portable-pixmap'
-                         #|| $mime === 'image/x-photoshop'  #this takes a lot of CPU and RAM!
-                         || $mime === 'image/x-rgb'
-                         || $mime === 'image/x-bmp'
-                         || $mime === 'image/tiff' ) return true;
-               }
-               else {
-                       #convertable by the PHP GD image lib
-                       if ( $mime === 'image/vnd.wap.wbmp'
-                         || $mime === 'image/x-xbitmap' ) return true;
-               }
-               if ( $mime === 'image/vnd.djvu' && isset( $wgDjvuRenderer ) && $wgDjvuRenderer ) return true;
-
-               return false;
+               $handler = $this->getHandler();
+               return $handler && $handler->canRender();
        }
 
-
        /**
         * Return true if the file is of a type that can't be directly
         * rendered by typical browsers and needs to be re-rasterized.
@@ -673,13 +654,8 @@ class Image
         * @return bool
         */
        function mustRender() {
-               $mime= $this->getMimeType();
-
-               if (  $mime === "image/gif"
-                  || $mime === "image/png"
-                  || $mime === "image/jpeg" ) return false;
-
-               return true;
+               $handler = $this->getHandler();
+               return $handler && $handler->mustRender();
        }
 
        /**
@@ -704,6 +680,8 @@ class Image
         *
         * Note that this function will always return true if allowInlineDisplay()
         * or isTrustedFile() is true for this file.
+        * 
+        * @return boolean
         */
        function isSafeFile() {
                if ($this->allowInlineDisplay()) return true;
@@ -724,36 +702,31 @@ class Image
                return false;
        }
 
-       /** Returns true if the file is flagged as trusted. Files flagged that way
-       * can be linked to directly, even if that is not allowed for this type of
-       * file normally.
-       *
-       * This is a dummy function right now and always returns false. It could be
-       * implemented to extract a flag from the database. The trusted flag could be
-       * set on upload, if the user has sufficient privileges, to bypass script-
-       * and html-filters. It may even be coupled with cryptographics signatures
-       * or such.
-       */
+       /**
+        * Returns true if the file is flagged as trusted. Files flagged that way
+        * can be linked to directly, even if that is not allowed for this type of
+        * file normally.
+        *
+        * This is a dummy function right now and always returns false. It could be
+        * implemented to extract a flag from the database. The trusted flag could be
+        * set on upload, if the user has sufficient privileges, to bypass script-
+        * and html-filters. It may even be coupled with cryptographics signatures
+        * or such.
+        * @return boolean
+        */
        function isTrustedFile() {
-               #this could be implemented to check a flag in the databas,
+               #this could be implemented to check a flag in the database,
                #look for signatures, etc
                return false;
        }
 
        /**
         * Return the escapeLocalURL of this image
+        * @param string $query URL query string
         * @public
         */
        function getEscapeLocalURL( $query=false) {
-               $this->getTitle();
-               if ( $query === false ) {
-                       if ( $this->page != 1 ) {
-                               $query = 'page=' . $this->page;
-                       } else {
-                               $query = '';
-                       }
-               }
-               return $this->title->escapeLocalURL( $query );
+               return $this->getTitle()->escapeLocalURL( $query );
        }
 
        /**
@@ -772,9 +745,8 @@ class Image
         * @param boolean $fromSharedDirectory  Should this be in $wgSharedUploadPath?
         * @return string URL of $name image
         * @public
-        * @static
         */
-       function imageUrl( $name, $fromSharedDirectory = false ) {
+       static function imageUrl( $name, $fromSharedDirectory = false ) {
                global $wgUploadPath,$wgUploadBaseUrl,$wgSharedUploadPath;
                if($fromSharedDirectory) {
                        $base = '';
@@ -799,76 +771,94 @@ class Image
 
        /**
         * @todo document
+        * @param string $thumbName
+        * @param string $subdir
+        * @return string
         * @private
         */
-       function thumbUrl( $width, $subdir='thumb') {
+       function thumbUrlFromName( $thumbName, $subdir = 'thumb' ) {
                global $wgUploadPath, $wgUploadBaseUrl, $wgSharedUploadPath;
-               global $wgSharedThumbnailScriptPath, $wgThumbnailScriptPath;
+               if($this->fromSharedDirectory) {
+                       $base = '';
+                       $path = $wgSharedUploadPath;
+               } else {
+                       $base = $wgUploadBaseUrl;
+                       $path = $wgUploadPath;
+               }
+               if ( Image::isHashed( $this->fromSharedDirectory ) ) {
+                       $hashdir = wfGetHashPath($this->name, $this->fromSharedDirectory) .
+                       wfUrlencode( $this->name );
+               } else {
+                       $hashdir = '';
+               }
+               $url = "{$base}{$path}/{$subdir}{$hashdir}/" . wfUrlencode( $thumbName );
+               return $url;
+       }
 
-               // Generate thumb.php URL if possible
-               $script = false;
-               $url = false;
+       /**
+        * @deprecated Use $image->transform()->getUrl() or thumbUrlFromName()
+        */
+       function thumbUrl( $width, $subdir = 'thumb' ) {
+               $name = $this->thumbName( array( 'width' => $width ) );
+               if ( strval( $name ) !== '' ) {
+                       return array( false, $this->thumbUrlFromName( $name, $subdir ) );
+               } else {
+                       return array( false, false );
+               }
+       }
 
+       /**
+        * @return mixed
+        */
+       function getTransformScript() {
+               global $wgSharedThumbnailScriptPath, $wgThumbnailScriptPath;
                if ( $this->fromSharedDirectory ) {
-                       if ( $wgSharedThumbnailScriptPath ) {
-                               $script = $wgSharedThumbnailScriptPath;
-                       }
+                       $script = $wgSharedThumbnailScriptPath;
                } else {
-                       if ( $wgThumbnailScriptPath ) {
-                               $script = $wgThumbnailScriptPath;
-                       }
+                       $script = $wgThumbnailScriptPath;
                }
                if ( $script ) {
-                       $url = $script . '?f=' . urlencode( $this->name ) . '&w=' . urlencode( $width );
-                       if( $this->mustRender() ) {
-                               $url.= '&r=1';
-                       }
+                       return "$script?f=" . urlencode( $this->name );
                } else {
-                       $name = $this->thumbName( $width );
-                       if($this->fromSharedDirectory) {
-                               $base = '';
-                               $path = $wgSharedUploadPath;
-                       } else {
-                               $base = $wgUploadBaseUrl;
-                               $path = $wgUploadPath;
-                       }
-                       if ( Image::isHashed( $this->fromSharedDirectory ) ) {
-                               $url = "{$base}{$path}/{$subdir}" .
-                               wfGetHashPath($this->name, $this->fromSharedDirectory)
-                               . $this->name.'/'.$name;
-                               $url = wfUrlencode( $url );
-                       } else {
-                               $url = "{$base}{$path}/{$subdir}/{$name}";
-                       }
+                       return false;
                }
-               return array( $script !== false, $url );
        }
 
        /**
-        * Return the file name of a thumbnail of the specified width
+        * Get a ThumbnailImage which is the same size as the source
+        * @param mixed $page
+        * @return MediaTransformOutput
+        */
+       function getUnscaledThumb( $page = false ) {
+               if ( $page ) {
+                       $params = array(
+                               'page' => $page,
+                               'width' => $this->getWidth( $page )
+                       );
+               } else {
+                       $params = array( 'width' => $this->getWidth() );
+               }
+               return $this->transform( $params );
+       }
+
+       /**
+        * Return the file name of a thumbnail with the specified parameters
         *
-        * @param integer $width        Width of the thumbnail image
-        * @param boolean $shared       Does the thumbnail come from the shared repository?
+        * @param array $params Handler-specific parameters
+        * @return string file name of a thumbnail with the specified parameters
         * @private
         */
-       function thumbName( $width ) {
-               $thumb = $width."px-".$this->name;
-               if ( $this->page != 1 ) {
-                       $thumb = "page{$this->page}-$thumb";
+       function thumbName( $params ) {
+               $handler = $this->getHandler();
+               if ( !$handler ) {
+                       return null;
                }
-
-               if( $this->mustRender() ) {
-                       if( $this->canRender() ) {
-                               # Rasterize to PNG (for SVG vector images, etc)
-                               $thumb .= '.png';
-                       }
-                       else {
-                               #should we use iconThumb here to get a symbolic thumbnail?
-                               #or should we fail with an internal error?
-                               return NULL; //can't make bitmap
-                       }
+               list( $thumbExt, /* $thumbMime */ ) = self::getThumbType( $this->extension, $this->mime );
+               $thumbName = $handler->makeParamString( $params ) . '-' . $this->name;
+               if ( $thumbExt != $this->extension ) {
+                       $thumbName .= ".$thumbExt";
                }
-               return $thumb;
+               return $thumbName;
        }
 
        /**
@@ -887,9 +877,13 @@ class Image
         * @param integer $height       maximum height of the image (optional)
         * @public
         */
-       function createThumb( $width, $height=-1 ) {
-               $thumb = $this->getThumbnail( $width, $height );
-               if( is_null( $thumb ) ) return '';
+       function createThumb( $width, $height = -1 ) {
+               $params = array( 'width' => $width );
+               if ( $height != -1 ) {
+                       $params['height'] = $height;
+               }
+               $thumb = $this->transform( $params );
+               if( is_null( $thumb ) || $thumb->isError() ) return '';
                return $thumb->getUrl();
        }
 
@@ -907,149 +901,93 @@ class Image
         *
         * @return ThumbnailImage or null on failure
         * @public
+        *
+        * @deprecated use transform()
         */
        function getThumbnail( $width, $height=-1, $render = true ) {
-               wfProfileIn( __METHOD__ );
-               if ($this->canRender()) {
-                       if ( $height > 0 ) {
-                               $this->load();
-                               if ( $width > $this->width * $height / $this->height ) {
-                                       $width = wfFitBoxWidth( $this->width, $this->height, $height );
-                               }
-                       }
-                       if ( $render ) {
-                               $thumb = $this->renderThumb( $width );
-                       } else {
-                               // Don't render, just return the URL
-                               if ( $this->validateThumbParams( $width, $height ) ) {
-                                       if ( !$this->mustRender() && $width == $this->width && $height == $this->height ) {
-                                               $url = $this->getURL();
-                                       } else {
-                                               list( $isScriptUrl, $url ) = $this->thumbUrl( $width );
-                                       }
-                                       $thumb = new ThumbnailImage( $url, $width, $height );
-                               } else {
-                                       $thumb = null;
-                               }
-                       }
-               } else {
-                       // not a bitmap or renderable image, don't try.
-                       $thumb = $this->iconThumb();
+               $params = array( 'width' => $width );
+               if ( $height != -1 ) {
+                       $params['height'] = $height;
                }
-               wfProfileOut( __METHOD__ );
-               return $thumb;
+               $flags = $render ? self::RENDER_NOW : 0;
+               return $this->transform( $params, $flags );
        }
-
+       
        /**
-        * @return ThumbnailImage
+        * Transform a media file
+        *
+        * @param array[string]mixed $params An associative array of handler-specific parameters.
+        *                  Typical keys are width, height and page.
+        * @param integer $flags A bitfield, may contain self::RENDER_NOW to force rendering
+        * @return MediaTransformOutput
         */
-       function iconThumb() {
-               global $wgStylePath, $wgStyleDirectory;
+       function transform( $params, $flags = 0 ) {
+               global $wgGenerateThumbnailOnParse, $wgUseSquid, $wgIgnoreImageErrors;
 
-               $try = array( 'fileicon-' . $this->extension . '.png', 'fileicon.png' );
-               foreach( $try as $icon ) {
-                       $path = '/common/images/icons/' . $icon;
-                       $filepath = $wgStyleDirectory . $path;
-                       if( file_exists( $filepath ) ) {
-                               return new ThumbnailImage( $wgStylePath . $path, 120, 120 );
+               wfProfileIn( __METHOD__ );
+               do {
+                       $handler = $this->getHandler();
+                       if ( !$handler || !$handler->canRender() ) {
+                               // not a bitmap or renderable image, don't try.
+                               $thumb = $this->iconThumb();
+                               break;
                        }
-               }
-               return null;
-       }
-
-       /**
-        * Validate thumbnail parameters and fill in the correct height
-        *
-        * @param integer &$width Specified width (input/output)
-        * @param integer &$height Height (output only)
-        * @return false to indicate that an error should be returned to the user. 
-        */
-       function validateThumbParams( &$width, &$height ) {
-               global $wgSVGMaxSize, $wgMaxImageArea;
-               
-               $this->load();
 
-               if ( ! $this->exists() )
-               {
-                       # If there is no image, there will be no thumbnail
-                       return false;
-               }
-               
-               $width = intval( $width );
-               
-               # Sanity check $width
-               if( $width <= 0 || $this->width <= 0) {
-                       # BZZZT
-                       return false;
-               }
+                       $script = $this->getTransformScript();
+                       if ( $script && !($flags & self::RENDER_NOW) ) {
+                               // Use a script to transform on client request
+                               $thumb = $handler->getScriptedTransform( $this, $script, $params );
+                               break;
+                       }
 
-               # Don't thumbnail an image so big that it will fill hard drives and send servers into swap
-               # JPEG has the handy property of allowing thumbnailing without full decompression, so we make
-               # an exception for it.
-               if ( $this->getMediaType() == MEDIATYPE_BITMAP &&
-                       $this->getMimeType() !== 'image/jpeg' &&
-                       $this->width * $this->height > $wgMaxImageArea )
-               {
-                       return false;
-               }
+                       $normalisedParams = $params;
+                       $handler->normaliseParams( $this, $normalisedParams );
+                       $thumbName = $this->thumbName( $normalisedParams );     
+                       $thumbPath = wfImageThumbDir( $this->name, $this->fromSharedDirectory ) .  "/$thumbName";
+                       $thumbUrl = $this->thumbUrlFromName( $thumbName );
 
-               # Don't make an image bigger than the source, or wgMaxSVGSize for SVGs
-               if ( $this->mustRender() ) {
-                       $width = min( $width, $wgSVGMaxSize );
-               } elseif ( $width > $this->width - 1 ) {
-                       $width = $this->width;
-                       $height = $this->height;
-                       return true;
-               }
 
-               $height = round( $this->height * $width / $this->width );
-               return true;
-       }
-       
-       /**
-        * Create a thumbnail of the image having the specified width.
-        * The thumbnail will not be created if the width is larger than the
-        * image's width. Let the browser do the scaling in this case.
-        * The thumbnail is stored on disk and is only computed if the thumbnail
-        * file does not exist OR if it is older than the image.
-        * Returns an object which can return the pathname, URL, and physical
-        * pixel size of the thumbnail -- or null on failure.
-        *
-        * @return ThumbnailImage or null on failure
-        * @private
-        */
-       function renderThumb( $width, $useScript = true ) {
-               global $wgUseSquid, $wgThumbnailEpoch;
+                       if ( !$wgGenerateThumbnailOnParse && !($flags & self::RENDER_NOW ) ) {
+                               $thumb = $handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
+                               break;
+                       }
+                       
+                       wfDebug( "Doing stat for $thumbPath\n" );
+                       $this->migrateThumbFile( $thumbName );
+                       if ( file_exists( $thumbPath ) ) {
+                               $thumb = $handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
+                               break;
+                       }
 
-               wfProfileIn( __METHOD__ );
+                       $thumb = $handler->doTransform( $this, $thumbPath, $thumbUrl, $params );
 
-               $this->load();
-               $height = -1;
-               if ( !$this->validateThumbParams( $width, $height ) ) {
-                       # Validation error
-                       wfProfileOut( __METHOD__ );
-                       return null;
-               }
+                       // Ignore errors if requested
+                       if ( !$thumb ) {
+                               $thumb = null;
+                       } elseif ( $thumb->isError() ) {
+                               $this->lastError = $thumb->toText();
+                               if ( $wgIgnoreImageErrors && !($flags & self::RENDER_NOW) ) {
+                                       $thumb = $handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
+                               }
+                       }
+                       
+                       if ( $wgUseSquid ) {
+                               wfPurgeSquidServers( array( $thumbUrl ) );
+                       }
+               } while (false);
 
-               if ( !$this->mustRender() && $width == $this->width && $height == $this->height ) {
-                       # validateThumbParams (or the user) wants us to return the unscaled image
-                       $thumb = new ThumbnailImage( $this->getURL(), $width, $height );
-                       wfProfileOut( __METHOD__ );
-                       return $thumb;
-               }
-               
-               list( $isScriptUrl, $url ) = $this->thumbUrl( $width );
-               if ( $isScriptUrl && $useScript ) {
-                       // Use thumb.php to render the image
-                       $thumb = new ThumbnailImage( $url, $width, $height );
-                       wfProfileOut( __METHOD__ );
-                       return $thumb;
-               }
+               wfProfileOut( __METHOD__ );
+               return $thumb;
+       }
 
-               $thumbName = $this->thumbName( $width, $this->fromSharedDirectory );
+       /**
+        * Fix thumbnail files from 1.4 or before, with extreme prejudice
+        * @param string $thumbName File name of thumbnail.
+        * @return void
+        */
+       function migrateThumbFile( $thumbName ) {
                $thumbDir = wfImageThumbDir( $this->name, $this->fromSharedDirectory );
-               $thumbPath = $thumbDir.'/'.$thumbName;
-
+               $thumbPath = "$thumbDir/$thumbName";
                if ( is_dir( $thumbPath ) ) {
                        // Directory where file should be
                        // This happened occasionally due to broken migration code in 1.5
@@ -1062,256 +1000,55 @@ class Image
                                        break;
                                }
                        }
-                       // Code below will ask if it exists, and the answer is now no
+                       // Doesn't exist anymore
                        clearstatcache();
                }
-
-               $done = true;
-               if ( !file_exists( $thumbPath ) ||
-                       filemtime( $thumbPath ) < wfTimestamp( TS_UNIX, $wgThumbnailEpoch ) ) 
-               {
-                       // Create the directory if it doesn't exist
-                       if ( is_file( $thumbDir ) ) {
-                               // File where thumb directory should be, destroy if possible
-                               @unlink( $thumbDir );
-                       }
-                       wfMkdirParents( $thumbDir );
-                       
-                       $oldThumbPath = wfDeprecatedThumbDir( $thumbName, 'thumb', $this->fromSharedDirectory ).
-                               '/'.$thumbName;
-                       $done = false;
-
-                       // Migration from old directory structure
-                       if ( is_file( $oldThumbPath ) ) {
-                               if ( filemtime($oldThumbPath) >= filemtime($this->imagePath) ) {
-                                       if ( file_exists( $thumbPath ) ) {
-                                               if ( !is_dir( $thumbPath ) ) {
-                                                       // Old image in the way of rename
-                                                       unlink( $thumbPath );
-                                               } else {
-                                                       // This should have been dealt with already
-                                                       throw new MWException( "Directory where image should be: $thumbPath" );
-                                               }
-                                       }
-                                       // Rename the old image into the new location
-                                       rename( $oldThumbPath, $thumbPath );
-                                       $done = true;
-                               } else {
-                                       unlink( $oldThumbPath );
-                               }
-                       }
-                       if ( !$done ) {
-                               $this->lastError = $this->reallyRenderThumb( $thumbPath, $width, $height );
-                               if ( $this->lastError === true ) {
-                                       $done = true;
-                               } elseif( $GLOBALS['wgIgnoreImageErrors'] ) {
-                                       // Log the error but output anyway.
-                                       // With luck it's a transitory error...
-                                       $done = true;
-                               }
-
-                               # Purge squid
-                               # This has to be done after the image is updated and present for all machines on NFS,
-                               # or else the old version might be stored into the squid again
-                               if ( $wgUseSquid ) {
-                                       $urlArr = array( $url );
-                                       wfPurgeSquidServers($urlArr);
-                               }
-                       }
-               }
-
-               if ( $done ) {
-                       $thumb = new ThumbnailImage( $url, $width, $height, $thumbPath );
-               } else {
-                       $thumb = null;
+               if ( is_file( $thumbDir ) ) {
+                       // File where directory should be
+                       unlink( $thumbDir );
+                       // Doesn't exist anymore
+                       clearstatcache();
                }
-               wfProfileOut( __METHOD__ );
-               return $thumb;
-       } // END OF function renderThumb
+       }
 
        /**
-        * Really render a thumbnail
-        * Call this only for images for which canRender() returns true.
-        *
-        * @param string $thumbPath Path to thumbnail
-        * @param int $width Desired width in pixels
-        * @param int $height Desired height in pixels
-        * @return bool True on error, false or error string on failure.
-        * @private
+        * Get a MediaHandler instance for this image
         */
-       function reallyRenderThumb( $thumbPath, $width, $height ) {
-               global $wgSVGConverters, $wgSVGConverter;
-               global $wgUseImageMagick, $wgImageMagickConvertCommand;
-               global $wgCustomConvertCommand;
-               global $wgDjvuRenderer, $wgDjvuPostProcessor;
-
-               $this->load();
-
-               $err = false;
-               $cmd = "";
-               $retval = 0;
-               
-               if( $this->mime === "image/svg" ) {
-                       #Right now we have only SVG
-
-                       global $wgSVGConverters, $wgSVGConverter;
-                       if( isset( $wgSVGConverters[$wgSVGConverter] ) ) {
-                               global $wgSVGConverterPath;
-                               $cmd = str_replace(
-                                       array( '$path/', '$width', '$height', '$input', '$output' ),
-                                       array( $wgSVGConverterPath ? "$wgSVGConverterPath/" : "",
-                                                  intval( $width ),
-                                                  intval( $height ),
-                                                  wfEscapeShellArg( $this->imagePath ),
-                                                  wfEscapeShellArg( $thumbPath ) ),
-                                       $wgSVGConverters[$wgSVGConverter] );
-                               wfProfileIn( 'rsvg' );
-                               wfDebug( "reallyRenderThumb SVG: $cmd\n" );
-                               $err = wfShellExec( $cmd, $retval );
-                               wfProfileOut( 'rsvg' );
-                       }
-               } else {
-                       if ( $this->mime === "image/vnd.djvu" && $wgDjvuRenderer ) {
-                               // DJVU image
-                               // The file contains several images. First, extract the
-                               // page in hi-res, if it doesn't yet exist. Then, thumbnail
-                               // it.
-
-                               $cmd = "{$wgDjvuRenderer} -page={$this->page} -size=${width}x${height} " .
-                                       wfEscapeShellArg( $this->imagePath ) . 
-                                       " | {$wgDjvuPostProcessor} > " . wfEscapeShellArg($thumbPath);
-                               wfProfileIn( 'ddjvu' );
-                               wfDebug( "reallyRenderThumb DJVU: $cmd\n" );
-                               $err = wfShellExec( $cmd, $retval );
-                               wfProfileOut( 'ddjvu' );
-
-                       } elseif ( $wgUseImageMagick ) {
-                               # use ImageMagick
-                       
-                               if ( $this->mime == 'image/jpeg' ) {
-                                       $quality = "-quality 80"; // 80%
-                               } elseif ( $this->mime == 'image/png' ) {
-                                       $quality = "-quality 95"; // zlib 9, adaptive filtering
-                               } else {
-                                       $quality = ''; // default
-                               }
-
-                               # Specify white background color, will be used for transparent images
-                               # in Internet Explorer/Windows instead of default black.
-       
-                               # Note, we specify "-size {$width}" and NOT "-size {$width}x{$height}".
-                               # It seems that ImageMagick has a bug wherein it produces thumbnails of
-                               # the wrong size in the second case.
-                               
-                               $cmd  =  wfEscapeShellArg($wgImageMagickConvertCommand) .
-                                       " {$quality} -background white -size {$width} ".
-                                       wfEscapeShellArg($this->imagePath) .
-                                       // Coalesce is needed to scale animated GIFs properly (bug 1017).
-                                       ' -coalesce ' .
-                                       // For the -resize option a "!" is needed to force exact size,
-                                       // or ImageMagick may decide your ratio is wrong and slice off
-                                       // a pixel.
-                                       " -thumbnail " . wfEscapeShellArg( "{$width}x{$height}!" ) .
-                                       " -depth 8 " .
-                                       wfEscapeShellArg($thumbPath) . " 2>&1";
-                               wfDebug("reallyRenderThumb: running ImageMagick: $cmd\n");
-                               wfProfileIn( 'convert' );
-                               $err = wfShellExec( $cmd, $retval );
-                               wfProfileOut( 'convert' );
-                       } elseif( $wgCustomConvertCommand ) {
-                               # Use a custom convert command
-                               # Variables: %s %d %w %h
-                               $src = wfEscapeShellArg( $this->imagePath );
-                               $dst = wfEscapeShellArg( $thumbPath );
-                               $cmd = $wgCustomConvertCommand;
-                               $cmd = str_replace( '%s', $src, str_replace( '%d', $dst, $cmd ) ); # Filenames
-                               $cmd = str_replace( '%h', $height, str_replace( '%w', $width, $cmd ) ); # Size
-                               wfDebug( "reallyRenderThumb: Running custom convert command $cmd\n" );
-                               wfProfileIn( 'convert' );
-                               $err = wfShellExec( $cmd, $retval );
-                               wfProfileOut( 'convert' );
-                       } else {
-                               # Use PHP's builtin GD library functions.
-                               #
-                               # First find out what kind of file this is, and select the correct
-                               # input routine for this.
-       
-                               $typemap = array(
-                                       'image/gif'          => array( 'imagecreatefromgif',  'palette',   'imagegif'  ),
-                                       'image/jpeg'         => array( 'imagecreatefromjpeg', 'truecolor', array( &$this, 'imageJpegWrapper' ) ),
-                                       'image/png'          => array( 'imagecreatefrompng',  'bits',      'imagepng'  ),
-                                       'image/vnd.wap.wmbp' => array( 'imagecreatefromwbmp', 'palette',   'imagewbmp'  ),
-                                       'image/xbm'          => array( 'imagecreatefromxbm',  'palette',   'imagexbm'  ),
-                               );
-                               if( !isset( $typemap[$this->mime] ) ) {
-                                       $err = 'Image type not supported';
-                                       wfDebug( "$err\n" );
-                                       return $err;
-                               }
-                               list( $loader, $colorStyle, $saveType ) = $typemap[$this->mime];
-
-                               if( !function_exists( $loader ) ) {
-                                       $err = "Incomplete GD library configuration: missing function $loader";
-                                       wfDebug( "$err\n" );
-                                       return $err;
-                               }
-                               if( $colorStyle == 'palette' ) {
-                                       $truecolor = false;
-                               } elseif( $colorStyle == 'truecolor' ) {
-                                       $truecolor = true;
-                               } elseif( $colorStyle == 'bits' ) {
-                                       $truecolor = ( $this->bits > 8 );
-                               }
+       function getHandler() {
+               return MediaHandler::getHandler( $this->getMimeType() );
+       }
 
-                               $src_image = call_user_func( $loader, $this->imagePath );
-                               if ( $truecolor ) {
-                                       $dst_image = imagecreatetruecolor( $width, $height );
-                               } else {
-                                       $dst_image = imagecreate( $width, $height );
-                               }
-                               imagecopyresampled( $dst_image, $src_image,
-                                                       0,0,0,0,
-                                                       $width, $height, $this->width, $this->height );
-                               call_user_func( $saveType, $dst_image, $thumbPath );
-                               imagedestroy( $dst_image );
-                               imagedestroy( $src_image );
-                       }
-               }
+       /**
+        * Get a ThumbnailImage representing a file type icon
+        * @return ThumbnailImage
+        */
+       function iconThumb() {
+               global $wgStylePath, $wgStyleDirectory;
 
-               #
-               # Check for zero-sized thumbnails. Those can be generated when
-               # no disk space is available or some other error occurs
-               #
-               if( file_exists( $thumbPath ) ) {
-                       $thumbstat = stat( $thumbPath );
-                       if( $thumbstat['size'] == 0 || $retval != 0 ) {
-                               wfDebugLog( 'thumbnail',
-                                       sprintf( 'Removing bad %d-byte thumbnail "%s"',
-                                               $thumbstat['size'], $thumbPath ) );
-                               unlink( $thumbPath );
+               $icons = array( 'fileicon-' . $this->extension . '.png', 'fileicon.png' );
+               foreach( $icons as $icon ) {
+                       $path = '/common/images/icons/' . $icon;
+                       $filepath = $wgStyleDirectory . $path;
+                       if( file_exists( $filepath ) ) {
+                               return new ThumbnailImage( $wgStylePath . $path, 120, 120 );
                        }
                }
-               if ( $retval != 0 ) {
-                       wfDebugLog( 'thumbnail',
-                               sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
-                                       wfHostname(), $retval, trim($err), $cmd ) );
-                       return wfMsg( 'thumbnail_error', $err );
-               } else {
-                       return true;
-               }
+               return null;
        }
 
+       /**
+        * Get last thumbnailing error.
+        * Largely obsolete.
+        * @return mixed
+        */
        function getLastError() {
                return $this->lastError;
        }
 
-       function imageJpegWrapper( $dst_image, $thumbPath ) {
-               imageinterlace( $dst_image );
-               imagejpeg( $dst_image, $thumbPath, 95 );
-       }
-
        /**
         * Get all thumbnail names previously generated for this image
+        * @param boolean $shared
+        * @return array[]string
         */
        function getThumbnails( $shared = false ) {
                if ( Image::isHashed( $shared ) ) {
@@ -1319,16 +1056,17 @@ class Image
                        $files = array();
                        $dir = wfImageThumbDir( $this->name, $shared );
 
-                       // This generates an error on failure, hence the @
-                       $handle = @opendir( $dir );
+                       if ( is_dir( $dir ) ) {
+                               $handle = opendir( $dir );
 
-                       if ( $handle ) {
-                               while ( false !== ( $file = readdir($handle) ) ) {
-                                       if ( $file{0} != '.' ) {
-                                               $files[] = $file;
+                               if ( $handle ) {
+                                       while ( false !== ( $file = readdir($handle) ) ) {
+                                               if ( $file[0] != '.' ) {
+                                                       $files[] = $file;
+                                               }
                                        }
+                                       closedir( $handle );
                                }
-                               closedir( $handle );
                        }
                } else {
                        $files = array();
@@ -1339,6 +1077,7 @@ class Image
 
        /**
         * Refresh metadata in memcached, but don't touch thumbnails or squid
+        * @return void
         */
        function purgeMetadataCache() {
                clearstatcache();
@@ -1348,6 +1087,9 @@ class Image
 
        /**
         * Delete all previously generated thumbnails, refresh metadata in memcached and purge the squid
+        * @param array $archiveFiles
+        * @param boolean $shared
+        * @return void
         */
        function purgeCache( $archiveFiles = array(), $shared = false ) {
                global $wgUseSquid;
@@ -1360,8 +1102,10 @@ class Image
                $dir = wfImageThumbDir( $this->name, $shared );
                $urls = array();
                foreach ( $files as $file ) {
-                       if ( preg_match( '/^(\d+)px/', $file, $m ) ) {
-                               list( $isScriptUrl, $url ) = $this->thumbUrl( $m[1] );
+                       # Check that the base image name is part of the thumb name
+                       # This is a basic sanity check to avoid erasing unrelated directories
+                       if ( strpos( $file, $this->name ) !== false ) {
+                               $url = $this->thumbUrlFromName( $file );
                                $urls[] = $url;
                                @unlink( "$dir/$file" );
                        }
@@ -1376,63 +1120,36 @@ class Image
                        wfPurgeSquidServers( $urls );
                }
        }
-       
+
        /**
         * Purge the image description page, but don't go after
         * pages using the image. Use when modifying file history
         * but not the current data.
+        * @return void
         */
        function purgeDescription() {
                $page = Title::makeTitle( NS_IMAGE, $this->name );
                $page->invalidateCache();
                $page->purgeSquid();
        }
-       
+
        /**
         * Purge metadata and all affected pages when the image is created,
-        * deleted, or majorly updated. A set of additional URLs may be
-        * passed to purge, such as specific image files which have changed.
-        * @param $urlArray array
+        * deleted, or majorly updated.
+        * @param array $urlArray A set of additional URLs may be passed to purge, 
+        *        such as specific image files which have changed (param not used?)
+        * @return void
         */
        function purgeEverything( $urlArr=array() ) {
                // Delete thumbnails and refresh image metadata cache
                $this->purgeCache();
                $this->purgeDescription();
-               
+
                // Purge cache of all pages using this image
                $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' );
                $update->doUpdate();
        }
 
-       function checkDBSchema(&$db) {
-               static $checkDone = false;
-               global $wgCheckDBSchema;
-               if (!$wgCheckDBSchema || $checkDone) {
-                       return;
-               }
-               # img_name must be unique
-               if ( !$db->indexUnique( 'image', 'img_name' ) && !$db->indexExists('image','PRIMARY') ) {
-                       throw new MWException( 'Database schema not up to date, please run maintenance/archives/patch-image_name_unique.sql' );
-               }
-               $checkDone = true;
-
-               # new fields must exist
-               # 
-               # Not really, there's hundreds of checks like this that we could do and they're all pointless, because 
-               # if the fields are missing, the database will loudly report a query error, the first time you try to do 
-               # something. The only reason I put the above schema check in was because the absence of that particular
-               # index would lead to an annoying subtle bug. No error message, just some very odd behaviour on duplicate
-               # uploads. -- TS
-               /*
-               if ( !$db->fieldExists( 'image', 'img_media_type' )
-                 || !$db->fieldExists( 'image', 'img_metadata' )
-                 || !$db->fieldExists( 'image', 'img_width' ) ) {
-
-                       throw new MWException( 'Database schema not up to date, please run maintenance/update.php' );
-                }
-                */
-       }
-
        /**
         * Return the image history of this image, line by line.
         * starts with current version, then old versions.
@@ -1442,11 +1159,10 @@ class Image
         *  2, ... return next old version from above query
         *
         * @public
+        * @return mixed false on no next history, object otherwise.
         */
        function nextHistoryLine() {
-               $dbr =& wfGetDB( DB_SLAVE );
-
-               $this->checkDBSchema($dbr);
+               $dbr = wfGetDB( DB_SLAVE );
 
                if ( $this->historyLine == 0 ) {// called for the first time, return line from cur
                        $this->historyRes = $dbr->select( 'image',
@@ -1490,6 +1206,7 @@ class Image
        /**
         * Reset the history pointer to the first element of the history
         * @public
+        * @return void
         */
        function resetHistory() {
                $this->historyLine = 0;
@@ -1507,7 +1224,7 @@ class Image
        * @param boolean $fromSharedDirectory Return the path to the file
        *   in a shared repository (see $wgUseSharedRepository and related
        *   options in DefaultSettings.php) instead of a local one.
-       *
+       * @return string Full filesystem path to the file.
        */
        function getFullPath( $fromSharedRepository = false ) {
                global $wgUploadDirectory, $wgSharedUploadDirectory;
@@ -1526,8 +1243,8 @@ class Image
        }
 
        /**
+        * @param boolean $shared
         * @return bool
-        * @static
         */
        public static function isHashed( $shared ) {
                global $wgHashedUploadDirectory, $wgHashedSharedUploadDirectory;
@@ -1536,13 +1253,18 @@ class Image
 
        /**
         * Record an image upload in the upload log and the image table
+        * @param string $oldver
+        * @param string $desc
+        * @param string $license
+        * @param string $copyStatus
+        * @param string $source
+        * @param boolean $watch
+        * @return boolean
         */
        function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false ) {
                global $wgUser, $wgUseCopyrightUpload;
 
-               $dbw =& wfGetDB( DB_MASTER );
-
-               $this->checkDBSchema($dbw);
+               $dbw = wfGetDB( DB_MASTER );
 
                // Delete thumbnails and refresh the metadata cache
                $this->purgeCache();
@@ -1669,6 +1391,9 @@ class Image
                        $article->insertNewArticle( $textdesc, $desc, $minor, $watch, $suppressRC );
                }
 
+               # Hooks, hooks, the magic of hooks...
+               wfRunHooks( 'FileUpload', array( $this ) );
+
                # Add the log entry
                $log = new LogPage( 'upload' );
                $log->addEntry( 'upload', $descTitle, $desc );
@@ -1691,18 +1416,20 @@ class Image
         * This is mostly copied from Title::getLinksTo()
         *
         * @deprecated Use HTMLCacheUpdate, this function uses too much memory
+        * @param string $options
+        * @return array[int]Title
         */
        function getLinksTo( $options = '' ) {
                wfProfileIn( __METHOD__ );
 
                if ( $options ) {
-                       $db =& wfGetDB( DB_MASTER );
+                       $db = wfGetDB( DB_MASTER );
                } else {
-                       $db =& wfGetDB( DB_SLAVE );
+                       $db = wfGetDB( DB_SLAVE );
                }
                $linkCache =& LinkCache::singleton();
 
-               extract( $db->tableNames( 'page', 'imagelinks' ) );
+               list( $page, $imagelinks ) = $db->tableNamesN( 'page', 'imagelinks' );
                $encName = $db->addQuotes( $this->name );
                $sql = "SELECT page_namespace,page_title,page_id FROM $page,$imagelinks WHERE page_id=il_from AND il_to=$encName $options";
                $res = $db->query( $sql, __METHOD__ );
@@ -1720,75 +1447,26 @@ class Image
                wfProfileOut( __METHOD__ );
                return $retVal;
        }
-       
+
        /**
-        * Retrive Exif data from the file and prune unrecognized tags
-        * and/or tags with invalid contents
-        *
-        * @param $filename
         * @return array
         */
-       private function retrieveExifData( $filename ) {
-               global $wgShowEXIF;
-               
-               /*
-               if ( $this->getMimeType() !== "image/jpeg" )
+       function getExifData() {
+               $handler = $this->getHandler();
+               if ( !$handler || $handler->getMetadataType( $this ) != 'exif' ) {
                        return array();
-               */
-
-               if( $wgShowEXIF && file_exists( $filename ) ) {
-                       $exif = new Exif( $filename );
-                       return $exif->getFilteredData();
                }
-               
-               return array();
-       }
-
-       function getExifData() {
-               global $wgRequest;
-               if ( $this->metadata === '0' || $this->mime == 'image/vnd.djvu' )
+               if ( !$this->metadata ) {
                        return array();
-
-               $purge = $wgRequest->getVal( 'action' ) == 'purge';
-               $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->purgeMetadataCache();
-                       $this->updateExifData( $newver );
                }
-               if ( isset( $ret['MEDIAWIKI_EXIF_VERSION'] ) )
-                       unset( $ret['MEDIAWIKI_EXIF_VERSION'] );
-               $format = new FormatExif( $ret );
-
-               return $format->getFormattedData();
-       }
-
-       function updateExifData( $version ) {
-               if ( $this->getImagePath() === false ) # Not a local image
-                       return;
-
-               # Get EXIF data from image
-               $exif = $this->retrieveExifData( $this->imagePath );
-               if ( count( $exif ) ) {
-                       $exif['MEDIAWIKI_EXIF_VERSION'] = $version;
-                       $this->metadata = serialize( $exif );
-               } else {
-                       $this->metadata = '0';
+               $exif = unserialize( $this->metadata );
+               if ( !$exif ) {
+                       return array();
                }
+               unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
+               $format = new FormatExif( $exif );
 
-               # Update EXIF data in database
-               $dbw =& wfGetDB( DB_MASTER );
-
-               $this->checkDBSchema($dbw);
-
-               $dbw->update( 'image',
-                       array( 'img_metadata' => $this->metadata ),
-                       array( 'img_name' => $this->name ),
-                       __METHOD__
-               );
+               return $format->getFormattedData();
        }
 
        /**
@@ -1800,7 +1478,7 @@ class Image
        function isLocal() {
                return !$this->fromSharedDirectory;
        }
-       
+
        /**
         * Was this image ever deleted from the wiki?
         *
@@ -1810,7 +1488,7 @@ class Image
                $title = Title::makeTitle( NS_IMAGE, $this->name );
                return ( $title->isDeleted() > 0 );
        }
-       
+
        /**
         * Delete all versions of the image.
         *
@@ -1819,40 +1497,41 @@ class Image
         *
         * Cache purging is done; logging is caller's responsibility.
         *
-        * @param $reason
-        * @return true on success, false on some kind of failure
+        * @param string $reason
+        * @param boolean $suppress
+        * @return boolean true on success, false on some kind of failure
         */
-       function delete( $reason ) {
+       function delete( $reason, $suppress=false ) {
                $transaction = new FSTransaction();
                $urlArr = array( $this->getURL() );
-               
+
                if( !FileStore::lock() ) {
                        wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" );
                        return false;
                }
-               
+
                try {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->begin();
-                       
+
                        // Delete old versions
                        $result = $dbw->select( 'oldimage',
                                array( 'oi_archive_name' ),
                                array( 'oi_name' => $this->name ) );
-                       
+
                        while( $row = $dbw->fetchObject( $result ) ) {
                                $oldName = $row->oi_archive_name;
-                               
-                               $transaction->add( $this->prepareDeleteOld( $oldName, $reason ) );
-                               
+
+                               $transaction->add( $this->prepareDeleteOld( $oldName, $reason, $suppress ) );
+
                                // We'll need to purge this URL from caches...
                                $urlArr[] = wfImageArchiveUrl( $oldName );
                        }
                        $dbw->freeResult( $result );
-                       
+
                        // And the current version...
-                       $transaction->add( $this->prepareDeleteCurrent( $reason ) );
-                       
+                       $transaction->add( $this->prepareDeleteCurrent( $reason, $suppress ) );
+
                        $dbw->immediateCommit();
                } catch( MWException $e ) {
                        wfDebug( __METHOD__.": db error, rolling back file transactions\n" );
@@ -1860,22 +1539,22 @@ class Image
                        FileStore::unlock();
                        throw $e;
                }
-               
+
                wfDebug( __METHOD__.": deleted db items, applying file transactions\n" );
                $transaction->commit();
                FileStore::unlock();
 
-               
+
                // Update site_stats
                $site_stats = $dbw->tableName( 'site_stats' );
                $dbw->query( "UPDATE $site_stats SET ss_images=ss_images-1", __METHOD__ );
-               
+
                $this->purgeEverything( $urlArr );
-               
+
                return true;
        }
-       
-       
+
+
        /**
         * Delete an old version of the image.
         *
@@ -1884,24 +1563,26 @@ class Image
         *
         * Cache purging is done; logging is caller's responsibility.
         *
-        * @param $reason
+        * @param string $archiveName
+        * @param string $reason
+        * @param boolean $suppress
         * @throws MWException or FSException on database or filestore failure
-        * @return true on success, false on some kind of failure
+        * @return boolean true on success, false on some kind of failure
         */
-       function deleteOld( $archiveName, $reason ) {
+       function deleteOld( $archiveName, $reason, $suppress=false ) {
                $transaction = new FSTransaction();
                $urlArr = array();
-               
+
                if( !FileStore::lock() ) {
                        wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" );
                        return false;
                }
-               
+
                $transaction = new FSTransaction();
                try {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->begin();
-                       $transaction->add( $this->prepareDeleteOld( $archiveName, $reason ) );
+                       $transaction->add( $this->prepareDeleteOld( $archiveName, $reason, $suppress ) );
                        $dbw->immediateCommit();
                } catch( MWException $e ) {
                        wfDebug( __METHOD__.": db error, rolling back file transaction\n" );
@@ -1909,11 +1590,11 @@ class Image
                        FileStore::unlock();
                        throw $e;
                }
-               
+
                wfDebug( __METHOD__.": deleted db items, applying file transaction\n" );
                $transaction->commit();
                FileStore::unlock();
-               
+
                $this->purgeDescription();
 
                // Squid purging
@@ -1926,13 +1607,15 @@ class Image
                }
                return true;
        }
-       
+
        /**
         * Delete the current version of a file.
         * May throw a database error.
-        * @return true on success, false on failure
+        * @param string $reason
+        * @param boolean $suppress
+        * @return boolean true on success, false on failure
         */
-       private function prepareDeleteCurrent( $reason ) {
+       private function prepareDeleteCurrent( $reason, $suppress=false ) {
                return $this->prepareDeleteVersion(
                        $this->getFullPath(),
                        $reason,
@@ -1953,15 +1636,19 @@ class Image
                                'fa_user_text'    => 'img_user_text',
                                'fa_timestamp'    => 'img_timestamp' ),
                        array( 'img_name' => $this->name ),
+                       $suppress,
                        __METHOD__ );
        }
 
        /**
         * Delete a given older version of a file.
         * May throw a database error.
-        * @return true on success, false on failure
+        * @param string $archiveName
+        * @param string $reason
+        * @param boolean $suppress
+        * @return boolean true on success, false on failure
         */
-       private function prepareDeleteOld( $archiveName, $reason ) {
+       private function prepareDeleteOld( $archiveName, $reason, $suppress=false ) {
                $oldpath = wfImageArchiveDir( $this->name ) .
                        DIRECTORY_SEPARATOR . $archiveName;
                return $this->prepareDeleteVersion(
@@ -1986,6 +1673,7 @@ class Image
                        array(
                                'oi_name' => $this->name,
                                'oi_archive_name' => $archiveName ),
+                       $suppress,
                        __METHOD__ );
        }
 
@@ -1998,14 +1686,14 @@ class Image
         *
         * @return FSTransaction
         */
-       private function prepareDeleteVersion( $path, $reason, $table, $fieldMap, $where, $fname ) {
+       private function prepareDeleteVersion( $path, $reason, $table, $fieldMap, $where, $suppress=false, $fname ) {
                global $wgUser, $wgSaveDeletedFiles;
-               
+
                // Dupe the file into the file store
                if( file_exists( $path ) ) {
                        if( $wgSaveDeletedFiles ) {
                                $group = 'deleted';
-                               
+
                                $store = FileStore::get( $group );
                                $key = FileStore::calculateKey( $path, $this->extension );
                                $transaction = $store->insert( $key, $path,
@@ -2021,7 +1709,7 @@ class Image
                        $key = null;
                        $transaction = new FSTransaction(); // empty
                }
-               
+
                if( $transaction === false ) {
                        // Fail to restore?
                        wfDebug( __METHOD__.": import to file store failed, aborting\n" );
@@ -2029,16 +1717,28 @@ class Image
                        return false;
                }
                
+               // Bitfields to further supress the image content
+               // Note that currently, live images are stored elsewhere
+               // and cannot be partially deleted
+               $bitfield = 0;
+               if ( $suppress ) {
+                       $bitfield |= self::DELETED_FILE;
+                       $bitfield |= self::DELETED_COMMENT;
+                       $bitfield |= self::DELETED_USER;
+                       $bitfield |= self::DELETED_RESTRICTED;
+               }
+
                $dbw = wfGetDB( DB_MASTER );
                $storageMap = array(
                        'fa_storage_group' => $dbw->addQuotes( $group ),
                        'fa_storage_key'   => $dbw->addQuotes( $key ),
-                       
+
                        'fa_deleted_user'      => $dbw->addQuotes( $wgUser->getId() ),
                        'fa_deleted_timestamp' => $dbw->timestamp(),
-                       'fa_deleted_reason'    => $dbw->addQuotes( $reason ) );
+                       'fa_deleted_reason'    => $dbw->addQuotes( $reason ),
+                       'fa_deleted'               => $bitfield);
                $allFields = array_merge( $storageMap, $fieldMap );
-               
+
                try {
                        if( $wgSaveDeletedFiles ) {
                                $dbw->insertSelect( 'filearchive', $table, $allFields, $where, $fname );
@@ -2051,10 +1751,10 @@ class Image
                        $transaction->rollback();
                        throw $e;
                }
-               
+
                return $transaction;
        }
-       
+
        /**
         * Restore all or specified deleted revisions to the given file.
         * Permissions and logging are left to the caller.
@@ -2066,36 +1766,38 @@ class Image
         * @return the number of file revisions restored if successful,
         *         or false on failure
         */
-       function restore( $versions=array() ) {
+       function restore( $versions=array(), $Unsuppress=false ) {
+               global $wgUser;
+       
                if( !FileStore::lock() ) {
                        wfDebug( __METHOD__." could not acquire filestore lock\n" );
                        return false;
                }
-               
+
                $transaction = new FSTransaction();
                try {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->begin();
-                       
+
                        // Re-confirm whether this image presently exists;
                        // if no we'll need to create an image record for the
                        // first item we restore.
                        $exists = $dbw->selectField( 'image', '1',
                                array( 'img_name' => $this->name ),
                                __METHOD__ );
-                       
+
                        // Fetch all or selected archived revisions for the file,
                        // sorted from the most recent to the oldest.
                        $conditions = array( 'fa_name' => $this->name );
                        if( $versions ) {
                                $conditions['fa_id'] = $versions;
                        }
-                       
+
                        $result = $dbw->select( 'filearchive', '*',
                                $conditions,
                                __METHOD__,
                                array( 'ORDER BY' => 'fa_timestamp DESC' ) );
-                       
+
                        if( $dbw->numRows( $result ) < count( $versions ) ) {
                                // There's some kind of conflict or confusion;
                                // we can't restore everything we were asked to.
@@ -2112,40 +1814,51 @@ class Image
                                FileStore::unlock();
                                return true;
                        }
-                       
+
                        $revisions = 0;
                        while( $row = $dbw->fetchObject( $result ) ) {
+                               if ( $Unsuppress ) {
+                               // Currently, fa_deleted flags fall off upon restore, lets be careful about this
+                               } else if ( ($row->fa_deleted & Revision::DELETED_RESTRICTED) && !$wgUser->isAllowed('hiderevision') ) {
+                               // Skip restoring file revisions that the user cannot restore
+                                       continue;
+                               }
                                $revisions++;
                                $store = FileStore::get( $row->fa_storage_group );
                                if( !$store ) {
                                        wfDebug( __METHOD__.": skipping row with no file.\n" );
                                        continue;
                                }
-                               
+
                                if( $revisions == 1 && !$exists ) {
                                        $destDir = wfImageDir( $row->fa_name );
                                        if ( !is_dir( $destDir ) ) {
                                                wfMkdirParents( $destDir );
                                        }
                                        $destPath = $destDir . DIRECTORY_SEPARATOR . $row->fa_name;
-                                       
+
                                        // We may have to fill in data if this was originally
                                        // an archived file revision.
                                        if( is_null( $row->fa_metadata ) ) {
                                                $tempFile = $store->filePath( $row->fa_storage_key );
-                                               $metadata = serialize( $this->retrieveExifData( $tempFile ) );
-                                               
+
                                                $magic = MimeMagic::singleton();
                                                $mime = $magic->guessMimeType( $tempFile, true );
                                                $media_type = $magic->getMediaType( $tempFile, $mime );
                                                list( $major_mime, $minor_mime ) = self::splitMime( $mime );
+                                               $handler = MediaHandler::getHandler( $mime );
+                                               if ( $handler ) {
+                                                       $metadata = $handler->getMetadata( $image, $tempFile );
+                                               } else {
+                                                       $metadata = '';
+                                               }
                                        } else {
                                                $metadata   = $row->fa_metadata;
                                                $major_mime = $row->fa_major_mime;
                                                $minor_mime = $row->fa_minor_mime;
                                                $media_type = $row->fa_media_type;
                                        }
-                                       
+
                                        $table = 'image';
                                        $fields = array(
                                                'img_name'        => $row->fa_name,
@@ -2176,7 +1889,7 @@ class Image
                                                wfMkdirParents( $destDir );
                                        }
                                        $destPath = $destDir . DIRECTORY_SEPARATOR . $archiveName;
-                                       
+
                                        $table = 'oldimage';
                                        $fields = array(
                                                'oi_name'         => $row->fa_name,
@@ -2190,13 +1903,13 @@ class Image
                                                'oi_user_text'    => $row->fa_user_text,
                                                'oi_timestamp'    => $row->fa_timestamp );
                                }
-                               
+
                                $dbw->insert( $table, $fields, __METHOD__ );
-                               /// @fixme this delete is not totally safe, potentially
+                               // @todo this delete is not totally safe, potentially
                                $dbw->delete( 'filearchive',
                                        array( 'fa_id' => $row->fa_id ),
                                        __METHOD__ );
-                               
+
                                // Check if any other stored revisions use this file;
                                // if so, we shouldn't remove the file from the deletion
                                // archives so they will still work.
@@ -2212,161 +1925,230 @@ class Image
                                } else {
                                        $flags = 0;
                                }
-                               
+
                                $transaction->add( $store->export( $row->fa_storage_key,
                                        $destPath, $flags ) );
                        }
-                       
+
                        $dbw->immediateCommit();
                } catch( MWException $e ) {
                        wfDebug( __METHOD__." caught error, aborting\n" );
                        $transaction->rollback();
                        throw $e;
                }
-               
+
                $transaction->commit();
                FileStore::unlock();
-               
+
                if( $revisions > 0 ) {
                        if( !$exists ) {
                                wfDebug( __METHOD__." restored $revisions items, creating a new current\n" );
-                               
+
                                // Update site_stats
                                $site_stats = $dbw->tableName( 'site_stats' );
                                $dbw->query( "UPDATE $site_stats SET ss_images=ss_images+1", __METHOD__ );
-                               
+
                                $this->purgeEverything();
                        } else {
                                wfDebug( __METHOD__." restored $revisions as archived versions\n" );
                                $this->purgeDescription();
                        }
                }
-               
+
                return $revisions;
        }
 
        /**
-        * Select a page from a multipage document. Determines the page used for
-        * rendering thumbnails.
+        * Returns 'true' if this image is a multipage document, e.g. a DJVU
+        * document.
         *
-        * @param $page Integer: page number, starting with 1
+        * @return Bool
         */
-       function selectPage( $page ) {
-               wfDebug( __METHOD__." selecting page $page \n" );
-               $this->page = $page;
-               if ( ! $this->dataLoaded ) {
-                       $this->load();
-               }
-               if ( ! isset( $this->multiPageXML ) ) {
-                       $this->initializeMultiPageXML();
+       function isMultipage() {
+               $handler = $this->getHandler();
+               return $handler && $handler->isMultiPage();
+       }
+
+       /**
+        * Returns the number of pages of a multipage document, or NULL for
+        * documents which aren't multipage documents
+        */
+       function pageCount() {
+               $handler = $this->getHandler();
+               if ( $handler && $handler->isMultiPage() ) {
+                       return $handler->pageCount( $this );
+               } else {
+                       return null;
                }
-               $o = $this->multiPageXML->BODY[0]->OBJECT[$page-1];
-               $this->height = intval( $o['height'] );
-               $this->width = intval( $o['width'] );
        }
 
-       function initializeMultiPageXML() {
-               #
-               # Check for files uploaded prior to DJVU support activation
-               # They have a '0' in their metadata field.
-               #
-               if ( $this->metadata == '0' ) {
-                       $deja = new DjVuImage( $this->imagePath );
-                       $this->metadata = $deja->retrieveMetaData();
-                       $this->purgeMetadataCache();
+       static function getCommonsDB() {
+               static $dbc;
+               global $wgLoadBalancer, $wgSharedUploadDBname;
+               if ( !isset( $dbc ) ) {
+                       $i = $wgLoadBalancer->getGroupIndex( 'commons' );
+                       $dbinfo = $wgLoadBalancer->mServers[$i];
+                       $dbc = new Database( $dbinfo['host'], $dbinfo['user'], 
+                               $dbinfo['password'], $wgSharedUploadDBname );
+               }
+               return $dbc;
+       }
 
-                       # Update metadata in the database
-                       $dbw =& wfGetDB( DB_MASTER );
-                       $dbw->update( 'image',
-                               array( 'img_metadata' => $this->metadata ),
-                               array( 'img_name' => $this->name ),
-                               __METHOD__
-                       );
+       /**
+        * Calculate the height of a thumbnail using the source and destination width
+        */
+       static function scaleHeight( $srcWidth, $srcHeight, $dstWidth ) {
+               // Exact integer multiply followed by division
+               if ( $srcWidth == 0 ) {
+                       return 0;
+               } else {
+                       return round( $srcHeight * $dstWidth / $srcWidth );
                }
-               wfSuppressWarnings();
-               $this->multiPageXML = new SimpleXMLElement( $this->metadata );
-               wfRestoreWarnings();
        }
 
        /**
-        * Returns 'true' if this image is a multipage document, e.g. a DJVU
-        * document.
+        * Get an image size array like that returned by getimagesize(), or false if it 
+        * can't be determined.
         *
-        * @return Bool
+        * @param string $fileName The filename
+        * @return array
         */
-       function isMultipage() {
-               return ( $this->mime == 'image/vnd.djvu' );
+       function getImageSize( $fileName ) {
+               $handler = $this->getHandler();
+               return $handler->getImageSize( $this, $fileName );
        }
 
        /**
-        * Returns the number of pages of a multipage document, or NULL for
-        * documents which aren't multipage documents
+        * Get the thumbnail extension and MIME type for a given source MIME type
+        * @return array thumbnail extension and MIME type
         */
-       function pageCount() {
-               if ( ! $this->isMultipage() ) {
-                       return null;
-               }
-               if ( ! isset( $this->multiPageXML ) ) {
-                       $this->initializeMultiPageXML();
+       static function getThumbType( $ext, $mime ) {
+               $handler = MediaHandler::getHandler( $mime );
+               if ( $handler ) {
+                       return $handler->getThumbType( $ext, $mime );
+               } else {
+                       return array( $ext, $mime );
                }
-               return count( $this->multiPageXML->xpath( '//OBJECT' ) );
        }
-       
+
 } //class
 
+
 /**
- * Wrapper class for thumbnail images
- * @package MediaWiki
+ * @addtogroup Media
  */
-class ThumbnailImage {
+class ArchivedFile
+{
        /**
-        * @param string $path Filesystem path to the thumb
-        * @param string $url URL path to the thumb
-        * @private
+        * Returns a file object from the filearchive table
+        * In the future, all current and old image storage
+        * may use FileStore. There will be a "old" storage 
+        * for current and previous file revisions as well as
+        * the "deleted" group for archived revisions
+        * @param $title, the corresponding image page title
+        * @param $id, the image id, a unique key
+        * @param $key, optional storage key
+        * @return ResultWrapper
         */
-       function ThumbnailImage( $url, $width, $height, $path = false ) {
-               $this->url = $url;
-               $this->width = round( $width );
-               $this->height = round( $height );
-                       # These should be integers when they get here.
-                       # If not, there's a bug somewhere.  But let's at
-                       # least produce valid HTML code regardless.
-               $this->path = $path;
+       function ArchivedFile( $title, $id=0, $key='' ) {
+               if( !is_object( $title ) ) {
+                       throw new MWException( 'Image constructor given bogus title.' );
+               }
+               $conds = ($id) ? "fa_id = $id" : "fa_storage_key = '$key'";
+               if( $title->getNamespace() == NS_IMAGE ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $res = $dbr->select( 'filearchive',
+                               array(
+                                       'fa_id',
+                                       'fa_name',
+                                       'fa_storage_key',
+                                       'fa_storage_group',
+                                       'fa_size',
+                                       'fa_bits',
+                                       'fa_width',
+                                       'fa_height',
+                                       'fa_metadata',
+                                       'fa_media_type',
+                                       'fa_major_mime',
+                                       'fa_minor_mime',
+                                       'fa_description',
+                                       'fa_user',
+                                       'fa_user_text',
+                                       'fa_timestamp',
+                                       'fa_deleted' ),
+                               array( 
+                                       'fa_name' => $title->getDbKey(),
+                                       $conds ),
+                               __METHOD__,
+                               array( 'ORDER BY' => 'fa_timestamp DESC' ) );
+                               
+                       if ( $dbr->numRows( $res ) == 0 ) {
+                       // this revision does not exist?
+                               return;
+                       }
+                       $ret = $dbr->resultObject( $res );
+                       $row = $ret->fetchObject();
+       
+                       // initialize fields for filestore image object
+                       $this->mId = intval($row->fa_id);
+                       $this->mName = $row->fa_name;
+                       $this->mGroup = $row->fa_storage_group;
+                       $this->mKey = $row->fa_storage_key;
+                       $this->mSize = $row->fa_size;
+                       $this->mBits = $row->fa_bits;
+                       $this->mWidth = $row->fa_width;
+                       $this->mHeight = $row->fa_height;
+                       $this->mMetaData = $row->fa_metadata;
+                       $this->mMime = "$row->fa_major_mime/$row->fa_minor_mime";
+                       $this->mType = $row->fa_media_type;
+                       $this->mDescription = $row->fa_description;
+                       $this->mUser = $row->fa_user;
+                       $this->mUserText = $row->fa_user_text;
+                       $this->mTimestamp = $row->fa_timestamp;
+                       $this->mDeleted = $row->fa_deleted;             
+               } else {
+                       throw new MWException( 'This title does not correspond to an image page.' );
+                       return;
+               }
+               return true;
        }
 
        /**
-        * @return string The thumbnail URL
+        * int $field one of DELETED_* bitfield constants
+        * for file or revision rows
+        * @return bool
         */
-       function getUrl() {
-               return $this->url;
+       function isDeleted( $field ) {
+               return ($this->mDeleted & $field) == $field;
        }
-
+       
        /**
-        * Return HTML <img ... /> tag for the thumbnail, will include
-        * width and height attributes and a blank alt text (as required).
-        *
-        * You can set or override additional attributes by passing an
-        * associative array of name => data pairs. The data will be escaped
-        * for HTML output, so should be in plaintext.
-        *
-        * @param array $attribs
-        * @return string
-        * @public
+        * Determine if the current user is allowed to view a particular
+        * field of this FileStore image file, if it's marked as deleted.
+        * @param int $field                                    
+        * @return bool
         */
-       function toHtml( $attribs = array() ) {
-               $attribs['src'] = $this->url;
-               $attribs['width'] = $this->width;
-               $attribs['height'] = $this->height;
-               if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
-
-               $html = '<img ';
-               foreach( $attribs as $name => $data ) {
-                       $html .= $name . '="' . htmlspecialchars( $data ) . '" ';
+       function userCan( $field ) {
+               if( isset($this->mDeleted) && ($this->mDeleted & $field) == $field ) {
+               // images
+                       global $wgUser;
+                       $permission = ( $this->mDeleted & Revision::DELETED_RESTRICTED ) == Revision::DELETED_RESTRICTED
+                               ? 'hiderevision'
+                               : 'deleterevision';
+                       wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
+                       return $wgUser->isAllowed( $permission );
+               } else {
+                       return true;
                }
-               $html .= '/>';
-               return $html;
        }
-
 }
 
+/**
+ * Aliases for backwards compatibility with 1.6
+ */
+define( 'MW_IMG_DELETED_FILE', Image::DELETED_FILE );
+define( 'MW_IMG_DELETED_COMMENT', Image::DELETED_COMMENT );
+define( 'MW_IMG_DELETED_USER', Image::DELETED_USER );
+define( 'MW_IMG_DELETED_RESTRICTED', Image::DELETED_RESTRICTED );
+
 ?>