Localisation updates Cantonese, Chinese and Old/Late Time Chinese
[lhc/web/wiklou.git] / includes / filerepo / LocalFile.php
index 3f25d91..228659c 100644 (file)
@@ -5,7 +5,7 @@
 /**
  * Bump this number when serialized cache records may be incompatible.
  */
-define( 'MW_FILE_VERSION', 4 );
+define( 'MW_FILE_VERSION', 8 );
 
 /**
  * Class to represent a local file in the wiki's own database
@@ -14,7 +14,7 @@ define( 'MW_FILE_VERSION', 4 );
  * to generate image thumbnails or for uploading.
  *
  * Note that only the repo object knows what its file class is called. You should
- * never name a file class explictly outside of the repo class. Instead use the 
+ * never name a file class explictly outside of the repo class. Instead use the
  * repo's factory functions to generate file objects, for example:
  *
  * RepoGroup::singleton()->getLocalRepo()->newFile($title);
@@ -22,37 +22,44 @@ define( 'MW_FILE_VERSION', 4 );
  * The convenience functions wfLocalFile() and wfFindFile() should be sufficient
  * in most cases.
  *
- * @addtogroup FileRepo
+ * @ingroup FileRepo
  */
 class LocalFile extends File
 {
        /**#@+
         * @private
         */
-       var     $fileExists,    # does the file file exist on disk? (loadFromXxx)
-               $historyLine,   # Number of line to return by nextHistoryLine() (constructor)
-               $historyRes,    # result of the query for the file's history (nextHistoryLine)
-               $width,         # \
-               $height,        #  |
-               $bits,          #   --- returned by getimagesize (loadFromXxx)
-               $attr,          # /
-               $media_type,    # MEDIATYPE_xxx (bitmap, drawing, audio...)
-               $mime,          # MIME type, determined by MimeMagic::guessMimeType
-               $major_mime,    # Major mime type
-               $minor_mine,    # Minor mime type
-               $size,          # Size in bytes (loadFromXxx)
-               $metadata,      # Metadata
-               $timestamp,     # Upload timestamp
-               $dataLoaded,    # Whether or not all this has been loaded from the database (loadFromXxx)
-               $upgraded;      # Whether the row was upgraded on load
+       var     $fileExists,       # does the file file exist on disk? (loadFromXxx)
+               $historyLine,      # Number of line to return by nextHistoryLine() (constructor)
+               $historyRes,       # result of the query for the file's history (nextHistoryLine)
+               $width,            # \
+               $height,           #  |
+               $bits,             #   --- returned by getimagesize (loadFromXxx)
+               $attr,             # /
+               $media_type,       # MEDIATYPE_xxx (bitmap, drawing, audio...)
+               $mime,             # MIME type, determined by MimeMagic::guessMimeType
+               $major_mime,       # Major mime type
+               $minor_mime,       # Minor mime type
+               $size,             # Size in bytes (loadFromXxx)
+               $metadata,         # Handler-specific metadata
+               $timestamp,        # Upload timestamp
+               $sha1,             # SHA-1 base 36 content hash
+               $user, $user_text, # User, who uploaded the file
+               $description,      # Description of current revision of the file
+               $dataLoaded,       # Whether or not all this has been loaded from the database (loadFromXxx)
+               $upgraded,         # Whether the row was upgraded on load
+               $locked,           # True if the image row is locked
+               $deleted;       # Bitfield akin to rev_deleted
 
        /**#@-*/
 
        /**
         * Create a LocalFile from a title
         * Do not call this except from inside a repo class.
+        *
+        * Note: $unused param is only here to avoid an E_STRICT
         */
-       static function newFromTitle( $title, $repo ) {
+       static function newFromTitle( $title, $repo, $unused = null ) {
                return new self( $title, $repo );
        }
 
@@ -66,6 +73,48 @@ class LocalFile extends File
                $file->loadFromRow( $row );
                return $file;
        }
+       
+       /**
+        * Create a LocalFile from a SHA-1 key
+        * Do not call this except from inside a repo class.
+        */
+       static function newFromKey( $sha1, $repo, $timestamp = false ) {
+               # Polymorphic function name to distinguish foreign and local fetches
+               $fname = get_class( $this ) . '::' . __FUNCTION__;
+
+               $conds = array( 'img_sha1' => $sha1 );
+               if( $timestamp ) {
+                       $conds['img_timestamp'] = $timestamp;
+               }
+               $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ), $conds, $fname );
+               if( $row ) {
+                       return self::newFromRow( $row, $repo );
+               } else {
+                       return false;
+               }
+       }
+       
+       /**
+        * Fields in the image table
+        */
+       static function selectFields() {
+               return array(
+                       'img_name',
+                       'img_size',
+                       'img_width',
+                       'img_height',
+                       'img_metadata',
+                       'img_bits',
+                       'img_media_type',
+                       'img_major_mime',
+                       'img_minor_mime',
+                       'img_description',
+                       'img_user',
+                       'img_user_text',
+                       'img_timestamp',
+                       'img_sha1',
+               );
+       }
 
        /**
         * Constructor.
@@ -108,12 +157,9 @@ class LocalFile extends File
                        wfDebug( "Pulling file metadata from cache key $key\n" );
                        $this->fileExists = $cachedValues['fileExists'];
                        if ( $this->fileExists ) {
-                               unset( $cachedValues['version'] );
-                               unset( $cachedValues['fileExists'] );
-                               foreach ( $cachedValues as $name => $value ) {
-                                       $this->$name = $value;
-                               }
+                               $this->setProps( $cachedValues );
                        }
+                       $this->dataLoaded = true;
                }
                if ( $this->dataLoaded ) {
                        wfIncrStats( 'image_cache_hit' );
@@ -155,8 +201,8 @@ class LocalFile extends File
        }
 
        function getCacheFields( $prefix = 'img_' ) {
-               static $fields = array( 'size', 'width', 'height', 'bits', 'media_type', 
-                       'major_mime', 'minor_mime', 'metadata', 'timestamp' );
+               static $fields = array( 'size', 'width', 'height', 'bits', 'media_type',
+                       'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'user', 'user_text', 'description' );
                static $results = array();
                if ( $prefix == '' ) {
                        return $fields;
@@ -175,26 +221,28 @@ class LocalFile extends File
         * Load file metadata from the DB
         */
        function loadFromDB() {
-               wfProfileIn( __METHOD__ );
+               # Polymorphic function name to distinguish foreign and local fetches
+               $fname = get_class( $this ) . '::' . __FUNCTION__;
+               wfProfileIn( $fname );
 
                # Unconditionally set loaded=true, we don't want the accessors constantly rechecking
                $this->dataLoaded = true;
 
-               $dbr = $this->repo->getSlaveDB();
+               $dbr = $this->repo->getMasterDB();
 
                $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ),
-                       array( 'img_name' => $this->getName() ), __METHOD__ );
+                       array( 'img_name' => $this->getName() ), $fname );
                if ( $row ) {
                        $this->loadFromRow( $row );
                } else {
                        $this->fileExists = false;
                }
 
-               wfProfileOut( __METHOD__ );
+               wfProfileOut( $fname );
        }
 
        /**
-        * Decode a row from the database (either object or array) to an array 
+        * Decode a row from the database (either object or array) to an array
         * with timestamps and MIME types decoded, and the field prefix removed.
         */
        function decodeRow( $row, $prefix = 'img_' ) {
@@ -206,7 +254,6 @@ class LocalFile extends File
                }
                $decoded = array();
                foreach ( $array as $name => $value ) {
-                       $deprefixedName = substr( $name, $prefixLength );
                        $decoded[substr( $name, $prefixLength )] = $value;
                }
                $decoded['timestamp'] = wfTimestamp( TS_MW, $decoded['timestamp'] );
@@ -218,6 +265,8 @@ class LocalFile extends File
                        }
                        $decoded['mime'] = $decoded['major_mime'].'/'.$decoded['minor_mime'];
                }
+               # Trim zero padding from char/binary field
+               $decoded['sha1'] = rtrim( $decoded['sha1'], "\0" );
                return $decoded;
        }
 
@@ -225,12 +274,12 @@ class LocalFile extends File
         * Load file metadata from a DB result row
         */
        function loadFromRow( $row, $prefix = 'img_' ) {
+               $this->dataLoaded = true;
                $array = $this->decodeRow( $row, $prefix );
                foreach ( $array as $name => $value ) {
                        $this->$name = $value;
                }
                $this->fileExists = true;
-               // Check for rows from a previous schema, quietly upgrade them
                $this->maybeUpgradeRow();
        }
 
@@ -254,7 +303,9 @@ class LocalFile extends File
                if ( wfReadOnly() ) {
                        return;
                }
-               if ( is_null($this->media_type) || $this->mime == 'image/svg' ) {
+               if ( is_null($this->media_type) ||
+                       $this->mime == 'image/svg'
+               ) {
                        $this->upgradeRow();
                        $this->upgraded = true;
                } else {
@@ -278,9 +329,17 @@ class LocalFile extends File
 
                $this->loadFromFile();
 
+               # Don't destroy file info of missing files
+               if ( !$this->fileExists ) {
+                       wfDebug( __METHOD__.": file does not exist, aborting\n" );
+                       return;
+               }
                $dbw = $this->repo->getMasterDB();
                list( $major, $minor ) = self::splitMime( $this->mime );
 
+               if ( wfReadOnly() ) {
+                       return;
+               }
                wfDebug(__METHOD__.': upgrading '.$this->getName()." to the current schema\n");
 
                $dbw->update( 'image',
@@ -292,6 +351,7 @@ class LocalFile extends File
                                'img_major_mime' => $major,
                                'img_minor_mime' => $minor,
                                'img_metadata' => $this->metadata,
+                               'img_sha1' => $this->sha1,
                        ), array( 'img_name' => $this->getName() ),
                        __METHOD__
                );
@@ -299,6 +359,13 @@ class LocalFile extends File
                wfProfileOut( __METHOD__ );
        }
 
+       /**
+        * Set properties in this object to be equal to those given in the
+        * associative array $info. Only cacheable fields can be set.
+        *
+        * If 'mime' is given, it will be split into major_mime/minor_mime.
+        * If major_mime/minor_mime are given, $this->mime will also be set.
+        */
        function setProps( $info ) {
                $this->dataLoaded = true;
                $fields = $this->getCacheFields( '' );
@@ -308,6 +375,12 @@ class LocalFile extends File
                                $this->$field = $info[$field];
                        }
                }
+               // Fix up mime fields
+               if ( isset( $info['major_mime'] ) ) {
+                       $this->mime = "{$info['major_mime']}/{$info['minor_mime']}";
+               } elseif ( isset( $info['mime'] ) ) {
+                       list( $this->major_mime, $this->minor_mime ) = self::splitMime( $this->mime );
+               }
        }
 
        /** splitMime inherited */
@@ -316,6 +389,7 @@ class LocalFile extends File
        /** getURL inherited */
        /** getViewURL inherited */
        /** getPath inherited */
+       /** isVisible inhereted */
 
        /**
         * Return the width of the image
@@ -357,6 +431,20 @@ class LocalFile extends File
                }
        }
 
+       /**
+        * Returns ID or name of user who uploaded the file
+        *
+        * @param $type string 'text' or 'id'
+        */
+       function getUser($type='text') {
+               $this->load();
+               if( $type == 'text' ) {
+                       return $this->user_text;
+               } elseif( $type == 'id' ) {
+                       return $this->user;
+               }
+       }
+
        /**
         * Get handler-specific metadata
         */
@@ -413,7 +501,7 @@ class LocalFile extends File
        /** createThumb inherited */
        /** getThumbnail inherited */
        /** transform inherited */
-       
+
        /**
         * Fix thumbnail files from 1.4 or before, with extreme prejudice
         */
@@ -450,25 +538,21 @@ class LocalFile extends File
         * Get all thumbnail names previously generated for this file
         */
        function getThumbnails() {
-               if ( $this->isHashed() ) {
-                       $this->load();
-                       $files = array();
-                       $dir = $this->getThumbPath();
-
-                       if ( is_dir( $dir ) ) {
-                               $handle = opendir( $dir );
-
-                               if ( $handle ) {
-                                       while ( false !== ( $file = readdir($handle) ) ) {
-                                               if ( $file{0} != '.' ) {
-                                                       $files[] = $file;
-                                               }
+               $this->load();
+               $files = array();
+               $dir = $this->getThumbPath();
+
+               if ( is_dir( $dir ) ) {
+                       $handle = opendir( $dir );
+
+                       if ( $handle ) {
+                               while ( false !== ( $file = readdir($handle) ) ) {
+                                       if ( $file{0} != '.' ) {
+                                               $files[] = $file;
                                        }
-                                       closedir( $handle );
                                }
+                               closedir( $handle );
                        }
-               } else {
-                       $files = array();
                }
 
                return $files;
@@ -480,12 +564,23 @@ class LocalFile extends File
        function purgeMetadataCache() {
                $this->loadFromDB();
                $this->saveToCache();
+               $this->purgeHistory();
+       }
+
+       /**
+        * Purge the shared history (OldLocalFile) cache
+        */
+       function purgeHistory() {
+               global $wgMemc;
+               $hashedName = md5($this->getName());
+               $oldKey = wfMemcKey( 'oldfile', $hashedName );
+               $wgMemc->delete( $oldKey );
        }
 
        /**
         * Delete all previously generated thumbnails, refresh metadata in memcached and purge the squid
         */
-       function purgeCache( $archiveFiles = array() ) {
+       function purgeCache() {
                // Refresh metadata cache
                $this->purgeMetadataCache();
 
@@ -493,7 +588,7 @@ class LocalFile extends File
                $this->purgeThumbnails();
 
                // Purge squid cache for this file
-               wfPurgeSquidServers( array( $this->getURL() ) );
+               SquidUpdate::purge( array( $this->getURL() ) );
        }
 
        /**
@@ -506,7 +601,6 @@ class LocalFile extends File
                $dir = $this->getThumbPath();
                $urls = array();
                foreach ( $files as $file ) {
-                       $m = array();
                        # Check that the base file name is part of the thumb name
                        # This is a basic sanity check to avoid erasing unrelated directories
                        if ( strpos( $file, $this->getName() ) !== false ) {
@@ -518,13 +612,35 @@ class LocalFile extends File
 
                // Purge the squid
                if ( $wgUseSquid ) {
-                       wfPurgeSquidServers( $urls );
+                       SquidUpdate::purge( $urls );
                }
        }
 
        /** purgeDescription inherited */
        /** purgeEverything inherited */
 
+       function getHistory($limit = null, $start = null, $end = null) {
+               $dbr = $this->repo->getSlaveDB();
+               $conds = $opts = array();
+               $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBKey() );
+               if( $start !== null ) {
+                       $conds[] = "oi_timestamp <= " . $dbr->addQuotes( $dbr->timestamp( $start ) );
+               }
+               if( $end !== null ) {
+                       $conds[] = "oi_timestamp >= " . $dbr->addQuotes( $dbr->timestamp( $end ) );
+               }
+               if( $limit ) {
+                       $opts['LIMIT'] = $limit;
+               }
+               $opts['ORDER BY'] = 'oi_timestamp DESC';
+               $res = $dbr->select('oldimage', '*', $conds, __METHOD__, $opts);
+               $r = array();
+               while( $row = $dbr->fetchObject($res) ) {
+                       $r[] = OldLocalFile::newFromRow($row, $this->repo);
+               }
+               return $r;
+       }
+
        /**
         * Return the history of this file, line by line.
         * starts with current version, then old versions.
@@ -536,21 +652,21 @@ class LocalFile extends File
         * @public
         */
        function nextHistoryLine() {
+               # Polymorphic function name to distinguish foreign and local fetches
+               $fname = get_class( $this ) . '::' . __FUNCTION__;
+
                $dbr = $this->repo->getSlaveDB();
 
                if ( $this->historyLine == 0 ) {// called for the first time, return line from cur
                        $this->historyRes = $dbr->select( 'image',
                                array(
-                                       'img_size',
-                                       'img_description',
-                                       'img_user','img_user_text',
-                                       'img_timestamp',
-                                       'img_width',
-                                       'img_height',
-                                       "'' AS oi_archive_name"
+                                       '*',
+                                       "'' AS oi_archive_name",
+                                       '0 as oi_deleted',
+                                       'img_sha1'
                                ),
                                array( 'img_name' => $this->title->getDBkey() ),
-                               __METHOD__
+                               $fname
                        );
                        if ( 0 == $dbr->numRows( $this->historyRes ) ) {
                                $dbr->freeResult($this->historyRes);
@@ -559,19 +675,9 @@ class LocalFile extends File
                        }
                } else if ( $this->historyLine == 1 ) {
                        $dbr->freeResult($this->historyRes);
-                       $this->historyRes = $dbr->select( 'oldimage',
-                               array(
-                                       'oi_size AS img_size',
-                                       'oi_description AS img_description',
-                                       'oi_user AS img_user',
-                                       'oi_user_text AS img_user_text',
-                                       'oi_timestamp AS img_timestamp',
-                                       'oi_width as img_width',
-                                       'oi_height as img_height',
-                                       'oi_archive_name'
-                               ),
+                       $this->historyRes = $dbr->select( 'oldimage', '*',
                                array( 'oi_name' => $this->title->getDBkey() ),
-                               __METHOD__,
+                               $fname,
                                array( 'ORDER BY' => 'oi_timestamp DESC' )
                        );
                }
@@ -596,6 +702,8 @@ class LocalFile extends File
        /** getHashPath inherited */
        /** getRel inherited */
        /** getUrlRel inherited */
+       /** getArchiveRel inherited */
+       /** getThumbRel inherited */
        /** getArchivePath inherited */
        /** getThumbPath inherited */
        /** getArchiveUrl inherited */
@@ -615,26 +723,27 @@ class LocalFile extends File
         *                         is already known
         * @param string $timestamp Timestamp for img_timestamp, or false to use the current time
         *
-        * @return Returns the archive name on success or an empty string if it was a new upload. 
-        *      Returns a wikitext-formatted WikiError on failure. 
+        * @return FileRepoStatus object. On success, the value member contains the
+        *     archive name, or an empty string if it was a new file.
         */
        function upload( $srcPath, $comment, $pageText, $flags = 0, $props = false, $timestamp = false ) {
-               $archive = $this->publish( $srcPath, $flags );
-               if ( WikiError::isError( $archive ) ){ 
-                       return $archive;
-               }
-               if ( !$this->recordUpload2( $archive, $comment, $pageText, $props, $timestamp ) ) {
-                       return new WikiErrorMsg( 'filenotfound', wfEscapeWikiText( $srcPath ) );
+               $this->lock();
+               $status = $this->publish( $srcPath, $flags );
+               if ( $status->ok ) {
+                       if ( !$this->recordUpload2( $status->value, $comment, $pageText, $props, $timestamp ) ) {
+                               $status->fatal( 'filenotfound', $srcPath );
+                       }
                }
-               return $archive;
+               $this->unlock();
+               return $status;
        }
 
        /**
         * Record a file upload in the upload log and the image table
         * @deprecated use upload()
         */
-       function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', 
-               $watch = false, $timestamp = false ) 
+       function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
+               $watch = false, $timestamp = false )
        {
                $pageText = UploadForm::getInitialPageText( $desc, $license, $copyStatus, $source );
                if ( !$this->recordUpload2( $oldver, $desc, $pageText ) ) {
@@ -651,7 +760,7 @@ class LocalFile extends File
        /**
         * Record a file upload in the upload log and the image table
         */
-       function recordUpload2( $oldver, $comment, $pageText, $props = false, $timestamp = false ) 
+       function recordUpload2( $oldver, $comment, $pageText, $props = false, $timestamp = false )
        {
                global $wgUser;
 
@@ -660,12 +769,16 @@ class LocalFile extends File
                if ( !$props ) {
                        $props = $this->repo->getFileProps( $this->getVirtualUrl() );
                }
+               $props['description'] = $comment;
+               $props['user'] = $wgUser->getId();
+               $props['user_text'] = $wgUser->getName();
+               $props['timestamp'] = wfTimestamp( TS_MW );
                $this->setProps( $props );
 
                // Delete thumbnails and refresh the metadata cache
                $this->purgeThumbnails();
                $this->saveToCache();
-               wfPurgeSquidServers( array( $this->getURL() ) );
+               SquidUpdate::purge( array( $this->getURL() ) );
 
                // Fail now if the file isn't there
                if ( !$this->fileExists ) {
@@ -673,6 +786,7 @@ class LocalFile extends File
                        return false;
                }
 
+               $reupload = false;
                if ( $timestamp === false ) {
                        $timestamp = $dbw->timestamp();
                }
@@ -692,15 +806,18 @@ class LocalFile extends File
                                'img_minor_mime' => $this->minor_mime,
                                'img_timestamp' => $timestamp,
                                'img_description' => $comment,
-                               'img_user' => $wgUser->getID(),
+                               'img_user' => $wgUser->getId(),
                                'img_user_text' => $wgUser->getName(),
                                'img_metadata' => $this->metadata,
+                               'img_sha1' => $this->sha1
                        ),
                        __METHOD__,
                        'IGNORE'
                );
 
                if( $dbw->affectedRows() == 0 ) {
+                       $reupload = true;
+
                        # Collision, this is an update of a file
                        # Insert previous contents into oldimage
                        $dbw->insertSelect( 'oldimage', 'image',
@@ -715,6 +832,11 @@ class LocalFile extends File
                                        'oi_description' => 'img_description',
                                        'oi_user' => 'img_user',
                                        'oi_user_text' => 'img_user_text',
+                                       'oi_metadata' => 'img_metadata',
+                                       'oi_media_type' => 'img_media_type',
+                                       'oi_major_mime' => 'img_major_mime',
+                                       'oi_minor_mime' => 'img_minor_mime',
+                                       'oi_sha1' => 'img_sha1'
                                ), array( 'img_name' => $this->getName() ), __METHOD__
                        );
 
@@ -730,9 +852,10 @@ class LocalFile extends File
                                        'img_minor_mime' => $this->minor_mime,
                                        'img_timestamp' => $timestamp,
                                        'img_description' => $comment,
-                                       'img_user' => $wgUser->getID(),
+                                       'img_user' => $wgUser->getId(),
                                        'img_user_text' => $wgUser->getName(),
                                        'img_metadata' => $this->metadata,
+                                       'img_sha1' => $this->sha1
                                ), array( /* WHERE */
                                        'img_name' => $this->getName()
                                ), __METHOD__
@@ -749,12 +872,16 @@ class LocalFile extends File
 
                # Add the log entry
                $log = new LogPage( 'upload' );
-               $log->addEntry( 'upload', $descTitle, $comment );
+               $action = $reupload ? 'overwrite' : 'upload';
+               $log->addEntry( $action, $descTitle, $comment );
 
                if( $descTitle->exists() ) {
                        # Create a null revision
                        $nullRevision = Revision::newNullRevision( $dbw, $descTitle->getArticleId(), $log->getRcComment(), false );
                        $nullRevision->insertOn( $dbw );
+                       
+                       wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, false) );
+                       $article->updateRevisionOn( $dbw, $nullRevision );
 
                        # Invalidate the cache for the description page
                        $descTitle->invalidateCache();
@@ -775,46 +902,92 @@ class LocalFile extends File
                # Invalidate cache for all pages using this file
                $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' );
                $update->doUpdate();
+               # Invalidate cache for all pages that redirects on this page
+               $redirs = $this->getTitle()->getRedirectsHere();
+               foreach( $redirs as $redir ) {
+                       $update = new HTMLCacheUpdate( $redir, 'imagelinks' );
+                       $update->doUpdate();
+               }
 
                return true;
        }
 
        /**
-        * Move or copy a file to its public location. If a file exists at the  
-        * destination, move it to an archive. Returns the archive name on success 
-        * or an empty string if it was a new file, and a wikitext-formatted 
-        * WikiError object on failure. 
+        * Move or copy a file to its public location. If a file exists at the
+        * destination, move it to an archive. Returns the archive name on success
+        * or an empty string if it was a new file, and a wikitext-formatted
+        * WikiError object on failure.
         *
         * The archive name should be passed through to recordUpload for database
         * registration.
         *
         * @param string $sourcePath Local filesystem path to the source image
         * @param integer $flags A bitwise combination of:
-        *     File::DELETE_SOURCE    Delete the source file, i.e. move 
+        *     File::DELETE_SOURCE    Delete the source file, i.e. move
         *         rather than copy
-        * @return The archive name on success or an empty string if it was a new 
-        *     file, and a wikitext-formatted WikiError object on failure. 
+        * @return FileRepoStatus object. On success, the value member contains the
+        *     archive name, or an empty string if it was a new file.
         */
        function publish( $srcPath, $flags = 0 ) {
+               $this->lock();
                $dstRel = $this->getRel();
                $archiveName = gmdate( 'YmdHis' ) . '!'. $this->getName();
                $archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
                $flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0;
                $status = $this->repo->publish( $srcPath, $dstRel, $archiveRel, $flags );
-               if ( WikiError::isError( $status ) ) {
-                       return $status;
-               } elseif ( $status == 'new' ) {
-                       return '';
+               if ( $status->value == 'new' ) {
+                       $status->value = '';
                } else {
-                       return $archiveName;
+                       $status->value = $archiveName;
                }
+               $this->unlock();
+               return $status;
        }
 
        /** getLinksTo inherited */
        /** getExifData inherited */
        /** isLocal inherited */
        /** wasDeleted inherited */
-       
+
+       /**
+        * Move file to the new title
+        *
+        * Move current, old version and all thumbnails
+        * to the new filename. Old file is deleted.
+        *
+        * Cache purging is done; checks for validity
+        * and logging are caller's responsibility
+        *
+        * @param $target Title New file name
+        * @return FileRepoStatus object.
+        */
+       function move( $target ) {
+               $this->lock();
+               $dbw = $this->repo->getMasterDB();
+               $batch = new LocalFileMoveBatch( $this, $target, $dbw );
+               $batch->addCurrent();
+               $batch->addOlds();
+               if( !$this->repo->canTransformVia404() ) {
+                       $batch->addThumbs();
+               }
+
+               $status = $batch->execute();
+               $this->purgeEverything();
+               $this->unlock();
+
+               if ( $status->isOk() ) {
+                       // Now switch the object
+                       $this->title = $target;
+                       // Force regeneration of the name and hashpath
+                       unset( $this->name );
+                       unset( $this->hashPath );
+                       // Purge the new image
+                       $this->purgeEverything();
+               }
+               
+               return $status;
+       }
+
        /**
         * Delete all versions of the file.
         *
@@ -824,62 +997,35 @@ class LocalFile extends File
         * Cache purging is done; logging is caller's responsibility.
         *
         * @param $reason
-        * @return true on success, false on some kind of failure
+        * @param $suppress
+        * @return FileRepoStatus object.
         */
-       function delete( $reason, $suppress=false ) {
-               $transaction = new FSTransaction();
-               $urlArr = array( $this->getURL() );
+       function delete( $reason, $suppress = false ) {
+               $this->lock();
+               $batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
+               $batch->addCurrent();
 
-               if( !FileStore::lock() ) {
-                       wfDebug( __METHOD__.": failed to acquire file store lock, aborting\n" );
-                       return false;
+               # Get old version relative paths
+               $dbw = $this->repo->getMasterDB();
+               $result = $dbw->select( 'oldimage',
+                       array( 'oi_archive_name' ),
+                       array( 'oi_name' => $this->getName() ) );
+               while ( $row = $dbw->fetchObject( $result ) ) {
+                       $batch->addOld( $row->oi_archive_name );
                }
+               $status = $batch->execute();
 
-               try {
-                       $dbw = $this->repo->getMasterDB();
-                       $dbw->begin();
-
-                       // Delete old versions
-                       $result = $dbw->select( 'oldimage',
-                               array( 'oi_archive_name' ),
-                               array( 'oi_name' => $this->getName() ) );
-
-                       while( $row = $dbw->fetchObject( $result ) ) {
-                               $oldName = $row->oi_archive_name;
-
-                               $transaction->add( $this->prepareDeleteOld( $oldName, $reason, $suppress ) );
-
-                               // We'll need to purge this URL from caches...
-                               $urlArr[] = $this->getArchiveUrl( $oldName );
-                       }
-                       $dbw->freeResult( $result );
-
-                       // And the current version...
-                       $transaction->add( $this->prepareDeleteCurrent( $reason, $suppress ) );
-
-                       $dbw->immediateCommit();
-               } catch( MWException $e ) {
-                       wfDebug( __METHOD__.": db error, rolling back file transactions\n" );
-                       $transaction->rollback();
-                       FileStore::unlock();
-                       throw $e;
+               if ( $status->ok ) {
+                       // Update site_stats
+                       $site_stats = $dbw->tableName( 'site_stats' );
+                       $dbw->query( "UPDATE $site_stats SET ss_images=ss_images-1", __METHOD__ );
+                       $this->purgeEverything();
                }
 
-               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;
+               $this->unlock();
+               return $status;
        }
 
-
        /**
         * Delete an old version of the file.
         *
@@ -889,502 +1035,784 @@ class LocalFile extends File
         * Cache purging is done; logging is caller's responsibility.
         *
         * @param $reason
+        * @param $suppress
         * @throws MWException or FSException on database or filestore failure
-        * @return true on success, false on some kind of failure
+        * @return FileRepoStatus object.
         */
        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;
+               $this->lock();
+               $batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
+               $batch->addOld( $archiveName );
+               $status = $batch->execute();
+               $this->unlock();
+               if ( $status->ok ) {
+                       $this->purgeDescription();
+                       $this->purgeHistory();
                }
+               return $status;
+       }
 
-               $transaction = new FSTransaction();
-               try {
-                       $dbw = $this->repo->getMasterDB();
-                       $dbw->begin();
-                       $transaction->add( $this->prepareDeleteOld( $archiveName, $reason, $suppress ) );
-                       $dbw->immediateCommit();
-               } catch( MWException $e ) {
-                       wfDebug( __METHOD__.": db error, rolling back file transaction\n" );
-                       $transaction->rollback();
-                       FileStore::unlock();
-                       throw $e;
+       /**
+        * Restore all or specified deleted revisions to the given file.
+        * Permissions and logging are left to the caller.
+        *
+        * May throw database exceptions on error.
+        *
+        * @param $versions set of record ids of deleted items to restore,
+        *                    or empty to restore all revisions.
+        * @param $unuppress
+        * @return FileRepoStatus
+        */
+       function restore( $versions = array(), $unsuppress = false ) {
+               $batch = new LocalFileRestoreBatch( $this, $unsuppress );
+               if ( !$versions ) {
+                       $batch->addAll();
+               } else {
+                       $batch->addIds( $versions );
+               }
+               $status = $batch->execute();
+               if ( !$status->ok ) {
+                       return $status;
                }
 
-               wfDebug( __METHOD__.": deleted db items, applying file transaction\n" );
-               $transaction->commit();
-               FileStore::unlock();
+               $cleanupStatus = $batch->cleanup();
+               $cleanupStatus->successCount = 0;
+               $cleanupStatus->failCount = 0;
+               $status->merge( $cleanupStatus );
+               return $status;
+       }
 
-               $this->purgeDescription();
+       /** isMultipage inherited */
+       /** pageCount inherited */
+       /** scaleHeight inherited */
+       /** getImageSize inherited */
 
-               // Squid purging
-               global $wgUseSquid;
-               if ( $wgUseSquid ) {
-                       $urlArr = array(
-                               $this->getArchiveUrl( $archiveName ),
-                       );
-                       wfPurgeSquidServers( $urlArr );
-               }
-               return true;
+       /**
+        * Get the URL of the file description page.
+        */
+       function getDescriptionUrl() {
+               return $this->title->getLocalUrl();
        }
 
        /**
-        * Delete the current version of a file.
-        * May throw a database error.
-        * @return true on success, false on failure
+        * Get the HTML text of the description page
+        * This is not used by ImagePage for local files, since (among other things)
+        * it skips the parser cache.
         */
-       private function prepareDeleteCurrent( $reason, $suppress=false ) {
-               return $this->prepareDeleteVersion(
-                       $this->getFullPath(),
-                       $reason,
-                       'image',
-                       array(
-                               'fa_name'         => 'img_name',
-                               'fa_archive_name' => 'NULL',
-                               'fa_size'         => 'img_size',
-                               'fa_width'        => 'img_width',
-                               'fa_height'       => 'img_height',
-                               'fa_metadata'     => 'img_metadata',
-                               'fa_bits'         => 'img_bits',
-                               'fa_media_type'   => 'img_media_type',
-                               'fa_major_mime'   => 'img_major_mime',
-                               'fa_minor_mime'   => 'img_minor_mime',
-                               'fa_description'  => 'img_description',
-                               'fa_user'         => 'img_user',
-                               'fa_user_text'    => 'img_user_text',
-                               'fa_timestamp'    => 'img_timestamp' ),
-                       array( 'img_name' => $this->getName() ),
-                       $suppress,
-                       __METHOD__ );
+       function getDescriptionText() {
+               global $wgParser;
+               $revision = Revision::newFromTitle( $this->title );
+               if ( !$revision ) return false;
+               $text = $revision->getText();
+               if ( !$text ) return false;
+               $html = $wgParser->parse( $text, new ParserOptions );
+               return $html;
+       }
+
+       function getDescription() {
+               $this->load();
+               return $this->description;
+       }
+
+       function getTimestamp() {
+               $this->load();
+               return $this->timestamp;
+       }
+
+       function getSha1() {
+               $this->load();
+               // Initialise now if necessary
+               if ( $this->sha1 == '' && $this->fileExists ) {
+                       $this->sha1 = File::sha1Base36( $this->getPath() );
+                       if ( strval( $this->sha1 ) != '' ) {
+                               $dbw = $this->repo->getMasterDB();
+                               $dbw->update( 'image',
+                                       array( 'img_sha1' => $this->sha1 ),
+                                       array( 'img_name' => $this->getName() ),
+                                       __METHOD__ );
+                               $this->saveToCache();
+                       }
+               }
+
+               return $this->sha1;
        }
 
        /**
-        * Delete a given older version of a file.
-        * May throw a database error.
-        * @return true on success, false on failure
+        * Start a transaction and lock the image for update
+        * Increments a reference counter if the lock is already held
+        * @return boolean True if the image exists, false otherwise
         */
-       private function prepareDeleteOld( $archiveName, $reason, $suppress=false ) {
-               $oldpath = $this->getArchivePath() .
-                       DIRECTORY_SEPARATOR . $archiveName;
-               return $this->prepareDeleteVersion(
-                       $oldpath,
-                       $reason,
-                       'oldimage',
-                       array(
-                               'fa_name'         => 'oi_name',
-                               'fa_archive_name' => 'oi_archive_name',
-                               'fa_size'         => 'oi_size',
-                               'fa_width'        => 'oi_width',
-                               'fa_height'       => 'oi_height',
-                               'fa_metadata'     => 'NULL',
-                               'fa_bits'         => 'oi_bits',
-                               'fa_media_type'   => 'NULL',
-                               'fa_major_mime'   => 'NULL',
-                               'fa_minor_mime'   => 'NULL',
-                               'fa_description'  => 'oi_description',
-                               'fa_user'         => 'oi_user',
-                               'fa_user_text'    => 'oi_user_text',
-                               'fa_timestamp'    => 'oi_timestamp' ),
-                       array(
-                               'oi_name' => $this->getName(),
-                               'oi_archive_name' => $archiveName ),
-                       $suppress,
-                       __METHOD__ );
+       function lock() {
+               $dbw = $this->repo->getMasterDB();
+               if ( !$this->locked ) {
+                       $dbw->begin();
+                       $this->locked++;
+               }
+               return $dbw->selectField( 'image', '1', array( 'img_name' => $this->getName() ), __METHOD__ );
        }
 
        /**
-        * Do the dirty work of backing up an image row and its file
-        * (if $wgSaveDeletedFiles is on) and removing the originals.
-        *
-        * Must be run while the file store is locked and a database
-        * transaction is open to avoid race conditions.
-        *
-        * @return FSTransaction
+        * Decrement the lock reference count. If the reference count is reduced to zero, commits
+        * the transaction and thereby releases the image lock.
         */
-       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->getExtension() );
-                               $transaction = $store->insert( $key, $path,
-                                       FileStore::DELETE_ORIGINAL );
-                       } else {
-                               $group = null;
-                               $key = null;
-                               $transaction = FileStore::deleteFile( $path );
+       function unlock() {
+               if ( $this->locked ) {
+                       --$this->locked;
+                       if ( !$this->locked ) {
+                               $dbw = $this->repo->getMasterDB();
+                               $dbw->commit();
                        }
+               }
+       }
+
+       /**
+        * Roll back the DB transaction and mark the image unlocked
+        */
+       function unlockAndRollback() {
+               $this->locked = false;
+               $dbw = $this->repo->getMasterDB();
+               $dbw->rollback();
+       }
+} // LocalFile class
+
+#------------------------------------------------------------------------------
+
+/**
+ * Helper class for file deletion
+ * @ingroup FileRepo
+ */
+class LocalFileDeleteBatch {
+       var $file, $reason, $srcRels = array(), $archiveUrls = array(), $deletionBatch, $suppress;
+       var $status;
+
+       function __construct( File $file, $reason = '', $suppress = false ) {
+               $this->file = $file;
+               $this->reason = $reason;
+               $this->suppress = $suppress;
+               $this->status = $file->repo->newGood();
+       }
+
+       function addCurrent() {
+               $this->srcRels['.'] = $this->file->getRel();
+       }
+
+       function addOld( $oldName ) {
+               $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName );
+               $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName );
+       }
+
+       function getOldRels() {
+               if ( !isset( $this->srcRels['.'] ) ) {
+                       $oldRels =& $this->srcRels;
+                       $deleteCurrent = false;
                } else {
-                       wfDebug( __METHOD__." deleting already-missing '$path'; moving on to database\n" );
-                       $group = null;
-                       $key = null;
-                       $transaction = new FSTransaction(); // empty
+                       $oldRels = $this->srcRels;
+                       unset( $oldRels['.'] );
+                       $deleteCurrent = true;
                }
+               return array( $oldRels, $deleteCurrent );
+       }
 
-               if( $transaction === false ) {
-                       // Fail to restore?
-                       wfDebug( __METHOD__.": import to file store failed, aborting\n" );
-                       throw new MWException( "Could not archive and delete file $path" );
-                       return false;
+       /*protected*/ function getHashes() {
+               $hashes = array();
+               list( $oldRels, $deleteCurrent ) = $this->getOldRels();
+               if ( $deleteCurrent ) {
+                       $hashes['.'] = $this->file->getSha1();
                }
-               
-               // Bitfields to further supress the file content
-               // Note that currently, live files 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;
+               if ( count( $oldRels ) ) {
+                       $dbw = $this->file->repo->getMasterDB();
+                       $res = $dbw->select( 'oldimage', array( 'oi_archive_name', 'oi_sha1' ),
+                               'oi_archive_name IN(' . $dbw->makeList( array_keys( $oldRels ) ) . ')',
+                               __METHOD__ );
+                       while ( $row = $dbw->fetchObject( $res ) ) {
+                               if ( rtrim( $row->oi_sha1, "\0" ) === '' ) {
+                                       // Get the hash from the file
+                                       $oldUrl = $this->file->getArchiveVirtualUrl( $row->oi_archive_name );
+                                       $props = $this->file->repo->getFileProps( $oldUrl );
+                                       if ( $props['fileExists'] ) {
+                                               // Upgrade the oldimage row
+                                               $dbw->update( 'oldimage',
+                                                       array( 'oi_sha1' => $props['sha1'] ),
+                                                       array( 'oi_name' => $this->file->getName(), 'oi_archive_name' => $row->oi_archive_name ),
+                                                       __METHOD__ );
+                                               $hashes[$row->oi_archive_name] = $props['sha1'];
+                                       } else {
+                                               $hashes[$row->oi_archive_name] = false;
+                                       }
+                               } else {
+                                       $hashes[$row->oi_archive_name] = $row->oi_sha1;
+                               }
+                       }
                }
-
-               $dbw = $this->repo->getMasterDB();
-               $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'               => $bitfield);
-               $allFields = array_merge( $storageMap, $fieldMap );
-
-               try {
-                       if( $wgSaveDeletedFiles ) {
-                               $dbw->insertSelect( 'filearchive', $table, $allFields, $where, $fname );
+               $missing = array_diff_key( $this->srcRels, $hashes );
+               foreach ( $missing as $name => $rel ) {
+                       $this->status->error( 'filedelete-old-unregistered', $name );
+               }
+               foreach ( $hashes as $name => $hash ) {
+                       if ( !$hash ) {
+                               $this->status->error( 'filedelete-missing', $this->srcRels[$name] );
+                               unset( $hashes[$name] );
                        }
-                       $dbw->delete( $table, $where, $fname );
-               } catch( DBQueryError $e ) {
-                       // Something went horribly wrong!
-                       // Leave the file as it was...
-                       wfDebug( __METHOD__.": database error, rolling back file transaction\n" );
-                       $transaction->rollback();
-                       throw $e;
                }
 
-               return $transaction;
+               return $hashes;
        }
 
-       /**
-        * Restore all or specified deleted revisions to the given file.
-        * Permissions and logging are left to the caller.
-        *
-        * May throw database exceptions on error.
-        *
-        * @param $versions set of record ids of deleted items to restore,
-        *                    or empty to restore all revisions.
-        * @return the number of file revisions restored if successful,
-        *         or false on failure
-        */
-       function restore( $versions=array(), $Unsuppress=false ) {
+       function doDBInserts() {
                global $wgUser;
-       
-               if( !FileStore::lock() ) {
-                       wfDebug( __METHOD__." could not acquire filestore lock\n" );
-                       return false;
+               $dbw = $this->file->repo->getMasterDB();
+               $encTimestamp = $dbw->addQuotes( $dbw->timestamp() );
+               $encUserId = $dbw->addQuotes( $wgUser->getId() );
+               $encReason = $dbw->addQuotes( $this->reason );
+               $encGroup = $dbw->addQuotes( 'deleted' );
+               $ext = $this->file->getExtension();
+               $dotExt = $ext === '' ? '' : ".$ext";
+               $encExt = $dbw->addQuotes( $dotExt );
+               list( $oldRels, $deleteCurrent ) = $this->getOldRels();
+
+               // Bitfields to further suppress the content
+               if ( $this->suppress ) {
+                       $bitfield = 0;
+                       // This should be 15...
+                       $bitfield |= Revision::DELETED_TEXT;
+                       $bitfield |= Revision::DELETED_COMMENT;
+                       $bitfield |= Revision::DELETED_USER;
+                       $bitfield |= Revision::DELETED_RESTRICTED;
+               } else {
+                       $bitfield = 'oi_deleted';
                }
 
-               $transaction = new FSTransaction();
-               try {
-                       $dbw = $this->repo->getMasterDB();
-                       $dbw->begin();
+               if ( $deleteCurrent ) {
+                       $concat = $dbw->buildConcat( array( "img_sha1", $encExt ) );
+                       $where = array( 'img_name' => $this->file->getName() );
+                       $dbw->insertSelect( 'filearchive', 'image',
+                               array(
+                                       'fa_storage_group' => $encGroup,
+                                       'fa_storage_key'   => "CASE WHEN img_sha1='' THEN '' ELSE $concat END",
+                                       'fa_deleted_user'      => $encUserId,
+                                       'fa_deleted_timestamp' => $encTimestamp,
+                                       'fa_deleted_reason'    => $encReason,
+                                       'fa_deleted'               => $this->suppress ? $bitfield : 0,
+
+                                       'fa_name'         => 'img_name',
+                                       'fa_archive_name' => 'NULL',
+                                       'fa_size'         => 'img_size',
+                                       'fa_width'        => 'img_width',
+                                       'fa_height'       => 'img_height',
+                                       'fa_metadata'     => 'img_metadata',
+                                       'fa_bits'         => 'img_bits',
+                                       'fa_media_type'   => 'img_media_type',
+                                       'fa_major_mime'   => 'img_major_mime',
+                                       'fa_minor_mime'   => 'img_minor_mime',
+                                       'fa_description'  => 'img_description',
+                                       'fa_user'         => 'img_user',
+                                       'fa_user_text'    => 'img_user_text',
+                                       'fa_timestamp'    => 'img_timestamp'
+                               ), $where, __METHOD__ );
+               }
 
-                       // Re-confirm whether this file presently exists;
-                       // if no we'll need to create an file record for the
-                       // first item we restore.
-                       $exists = $dbw->selectField( 'image', '1',
-                               array( 'img_name' => $this->getName() ),
-                               __METHOD__ );
+               if ( count( $oldRels ) ) {
+                       $concat = $dbw->buildConcat( array( "oi_sha1", $encExt ) );
+                       $where = array(
+                               'oi_name' => $this->file->getName(),
+                               'oi_archive_name IN (' . $dbw->makeList( array_keys( $oldRels ) ) . ')' );
+                       $dbw->insertSelect( 'filearchive', 'oldimage',
+                               array(
+                                       'fa_storage_group' => $encGroup,
+                                       'fa_storage_key'   => "CASE WHEN oi_sha1='' THEN '' ELSE $concat END",
+                                       'fa_deleted_user'      => $encUserId,
+                                       'fa_deleted_timestamp' => $encTimestamp,
+                                       'fa_deleted_reason'    => $encReason,
+                                       'fa_deleted'               => $this->suppress ? $bitfield : 'oi_deleted',
+
+                                       'fa_name'         => 'oi_name',
+                                       'fa_archive_name' => 'oi_archive_name',
+                                       'fa_size'         => 'oi_size',
+                                       'fa_width'        => 'oi_width',
+                                       'fa_height'       => 'oi_height',
+                                       'fa_metadata'     => 'oi_metadata',
+                                       'fa_bits'         => 'oi_bits',
+                                       'fa_media_type'   => 'oi_media_type',
+                                       'fa_major_mime'   => 'oi_major_mime',
+                                       'fa_minor_mime'   => 'oi_minor_mime',
+                                       'fa_description'  => 'oi_description',
+                                       'fa_user'         => 'oi_user',
+                                       'fa_user_text'    => 'oi_user_text',
+                                       'fa_timestamp'    => 'oi_timestamp',
+                                       'fa_deleted'      => $bitfield
+                               ), $where, __METHOD__ );
+               }
+       }
 
-                       // Fetch all or selected archived revisions for the file,
-                       // sorted from the most recent to the oldest.
-                       $conditions = array( 'fa_name' => $this->getName() );
-                       if( $versions ) {
-                               $conditions['fa_id'] = $versions;
-                       }
+       function doDBDeletes() {
+               $dbw = $this->file->repo->getMasterDB();
+               list( $oldRels, $deleteCurrent ) = $this->getOldRels();
+               if ( count( $oldRels ) ) {
+                       $dbw->delete( 'oldimage',
+                               array(
+                                       'oi_name' => $this->file->getName(),
+                                       'oi_archive_name IN (' . $dbw->makeList( array_keys( $oldRels ) ) . ')'
+                               ), __METHOD__ );
+               }
+               if ( $deleteCurrent ) {
+                       $dbw->delete( 'image', array( 'img_name' => $this->file->getName() ), __METHOD__ );
+               }
+       }
 
-                       $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.
-                               wfDebug( __METHOD__.": couldn't find requested items\n" );
-                               $dbw->rollback();
-                               FileStore::unlock();
-                               return false;
+       /**
+        * Run the transaction
+        */
+       function execute() {
+               global $wgUser, $wgUseSquid;
+               wfProfileIn( __METHOD__ );
+
+               $this->file->lock();
+               // Leave private files alone
+               $privateFiles = array();
+               list( $oldRels, $deleteCurrent ) = $this->getOldRels();
+               $dbw = $this->file->repo->getMasterDB();
+               if( !empty( $oldRels ) ) {
+                       $res = $dbw->select( 'oldimage',
+                               array( 'oi_archive_name' ),
+                               array( 'oi_name' => $this->file->getName(),
+                                       'oi_archive_name IN (' . $dbw->makeList( array_keys($oldRels) ) . ')',
+                                       'oi_deleted & ' . File::DELETED_FILE => File::DELETED_FILE ),
+                               __METHOD__ );
+                       while( $row = $dbw->fetchObject( $res ) ) {
+                               $privateFiles[$row->oi_archive_name] = 1;
+                       }
+               }
+               // Prepare deletion batch
+               $hashes = $this->getHashes();
+               $this->deletionBatch = array();
+               $ext = $this->file->getExtension();
+               $dotExt = $ext === '' ? '' : ".$ext";
+               foreach ( $this->srcRels as $name => $srcRel ) {
+                       // Skip files that have no hash (missing source).
+                       // Keep private files where they are.
+                       if ( isset($hashes[$name]) && !array_key_exists($name,$privateFiles) ) {
+                               $hash = $hashes[$name];
+                               $key = $hash . $dotExt;
+                               $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
+                               $this->deletionBatch[$name] = array( $srcRel, $dstRel );
                        }
+               }
 
-                       if( $dbw->numRows( $result ) == 0 ) {
-                               // Nothing to do.
-                               wfDebug( __METHOD__.": nothing to do\n" );
-                               $dbw->rollback();
-                               FileStore::unlock();
-                               return true;
+               // Lock the filearchive rows so that the files don't get deleted by a cleanup operation
+               // We acquire this lock by running the inserts now, before the file operations.
+               //
+               // This potentially has poor lock contention characteristics -- an alternative
+               // scheme would be to insert stub filearchive entries with no fa_name and commit
+               // them in a separate transaction, then run the file ops, then update the fa_name fields.
+               $this->doDBInserts();
+
+               // Execute the file deletion batch
+               $status = $this->file->repo->deleteBatch( $this->deletionBatch );
+               if ( !$status->isGood() ) {
+                       $this->status->merge( $status );
+               }
+
+               if ( !$this->status->ok ) {
+                       // Critical file deletion error
+                       // Roll back inserts, release lock and abort
+                       // TODO: delete the defunct filearchive rows if we are using a non-transactional DB
+                       $this->file->unlockAndRollback();
+                       return $this->status;
+               }
+
+               // Purge squid
+               if ( $wgUseSquid ) {
+                       $urls = array();
+                       foreach ( $this->srcRels as $srcRel ) {
+                               $urlRel = str_replace( '%2F', '/', rawurlencode( $srcRel ) );
+                               $urls[] = $this->file->repo->getZoneUrl( 'public' ) . '/' . $urlRel;
                        }
+                       SquidUpdate::purge( $urls );
+               }
 
-                       $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;
-                               }
+               // Delete image/oldimage rows
+               $this->doDBDeletes();
 
-                               $restoredImage = new self( Title::makeTitle( NS_IMAGE, $row->fa_name ), $this->repo );
+               // Commit and return
+               $this->file->unlock();
+               wfProfileOut( __METHOD__ );
+               return $this->status;
+       }
+}
 
-                               if( $revisions == 1 && !$exists ) {
-                                       $destPath = $restoredImage->getFullPath();
-                                       $destDir = dirname( $destPath );
-                                       if ( !is_dir( $destDir ) ) {
-                                               wfMkdirParents( $destDir );
-                                       }
+#------------------------------------------------------------------------------
 
-                                       // 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 );
-
-                                               $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( false, $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;
-                                       }
+/**
+ * Helper class for file undeletion
+ * @ingroup FileRepo
+ */
+class LocalFileRestoreBatch {
+       var $file, $cleanupBatch, $ids, $all, $unsuppress = false;
+
+       function __construct( File $file, $unsuppress = false ) {
+               $this->file = $file;
+               $this->cleanupBatch = $this->ids = array();
+               $this->ids = array();
+               $this->unsuppress = $unsuppress;
+       }
 
-                                       $table = 'image';
-                                       $fields = array(
-                                               'img_name'        => $row->fa_name,
-                                               'img_size'        => $row->fa_size,
-                                               'img_width'       => $row->fa_width,
-                                               'img_height'      => $row->fa_height,
-                                               'img_metadata'    => $metadata,
-                                               'img_bits'        => $row->fa_bits,
-                                               'img_media_type'  => $media_type,
-                                               'img_major_mime'  => $major_mime,
-                                               'img_minor_mime'  => $minor_mime,
-                                               'img_description' => $row->fa_description,
-                                               'img_user'        => $row->fa_user,
-                                               'img_user_text'   => $row->fa_user_text,
-                                               'img_timestamp'   => $row->fa_timestamp );
-                               } else {
-                                       $archiveName = $row->fa_archive_name;
-                                       if( $archiveName == '' ) {
-                                               // This was originally a current version; we
-                                               // have to devise a new archive name for it.
-                                               // Format is <timestamp of archiving>!<name>
-                                               $archiveName =
-                                                       wfTimestamp( TS_MW, $row->fa_deleted_timestamp ) .
-                                                       '!' . $row->fa_name;
-                                       }
-                                       $destDir = $restoredImage->getArchivePath();
-                                       if ( !is_dir( $destDir ) ) {
-                                               wfMkdirParents( $destDir );
-                                       }
-                                       $destPath = $destDir . DIRECTORY_SEPARATOR . $archiveName;
-
-                                       $table = 'oldimage';
-                                       $fields = array(
-                                               'oi_name'         => $row->fa_name,
-                                               'oi_archive_name' => $archiveName,
-                                               'oi_size'         => $row->fa_size,
-                                               'oi_width'        => $row->fa_width,
-                                               'oi_height'       => $row->fa_height,
-                                               'oi_bits'         => $row->fa_bits,
-                                               'oi_description'  => $row->fa_description,
-                                               'oi_user'         => $row->fa_user,
-                                               'oi_user_text'    => $row->fa_user_text,
-                                               'oi_timestamp'    => $row->fa_timestamp );
-                               }
+       /**
+        * Add a file by ID
+        */
+       function addId( $fa_id ) {
+               $this->ids[] = $fa_id;
+       }
 
-                               $dbw->insert( $table, $fields, __METHOD__ );
-                               // @todo this delete is not totally safe, potentially
-                               $dbw->delete( 'filearchive',
-                                       array( 'fa_id' => $row->fa_id ),
-                                       __METHOD__ );
+       /**
+        * Add a whole lot of files by ID
+        */
+       function addIds( $ids ) {
+               $this->ids = array_merge( $this->ids, $ids );
+       }
 
-                               // 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.
-                               $useCount = $dbw->selectField( 'filearchive',
-                                       'COUNT(*)',
-                                       array(
-                                               'fa_storage_group' => $row->fa_storage_group,
-                                               'fa_storage_key'   => $row->fa_storage_key ),
-                                       __METHOD__ );
-                               if( $useCount == 0 ) {
-                                       wfDebug( __METHOD__.": nothing else using {$row->fa_storage_key}, will deleting after\n" );
-                                       $flags = FileStore::DELETE_ORIGINAL;
-                               } else {
-                                       $flags = 0;
+       /**
+        * Add all revisions of the file
+        */
+       function addAll() {
+               $this->all = true;
+       }
+
+       /**
+        * Run the transaction, except the cleanup batch.
+        * The cleanup batch should be run in a separate transaction, because it locks different
+        * rows and there's no need to keep the image row locked while it's acquiring those locks
+        * The caller may have its own transaction open.
+        * So we save the batch and let the caller call cleanup()
+        */
+       function execute() {
+               global $wgUser, $wgLang;
+               if ( !$this->all && !$this->ids ) {
+                       // Do nothing
+                       return $this->file->repo->newGood();
+               }
+
+               $exists = $this->file->lock();
+               $dbw = $this->file->repo->getMasterDB();
+               $status = $this->file->repo->newGood();
+
+               // Fetch all or selected archived revisions for the file,
+               // sorted from the most recent to the oldest.
+               $conditions = array( 'fa_name' => $this->file->getName() );
+               if( !$this->all ) {
+                       $conditions[] = 'fa_id IN (' . $dbw->makeList( $this->ids ) . ')';
+               }
+
+               $result = $dbw->select( 'filearchive', '*',
+                       $conditions,
+                       __METHOD__,
+                       array( 'ORDER BY' => 'fa_timestamp DESC' ) );
+
+               $idsPresent = array();
+               $storeBatch = array();
+               $insertBatch = array();
+               $insertCurrent = false;
+               $deleteIds = array();
+               $first = true;
+               $archiveNames = array();
+               while( $row = $dbw->fetchObject( $result ) ) {
+                       $idsPresent[] = $row->fa_id;
+
+                       if ( $row->fa_name != $this->file->getName() ) {
+                               $status->error( 'undelete-filename-mismatch', $wgLang->timeanddate( $row->fa_timestamp ) );
+                               $status->failCount++;
+                               continue;
+                       }
+                       if ( $row->fa_storage_key == '' ) {
+                               // Revision was missing pre-deletion
+                               $status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp ) );
+                               $status->failCount++;
+                               continue;
+                       }
+
+                       $deletedRel = $this->file->repo->getDeletedHashPath( $row->fa_storage_key ) . $row->fa_storage_key;
+                       $deletedUrl = $this->file->repo->getVirtualUrl() . '/deleted/' . $deletedRel;
+
+                       $sha1 = substr( $row->fa_storage_key, 0, strcspn( $row->fa_storage_key, '.' ) );
+                       # Fix leading zero
+                       if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) {
+                               $sha1 = substr( $sha1, 1 );
+                       }
+
+                       if( is_null( $row->fa_major_mime ) || $row->fa_major_mime == 'unknown'
+                               || is_null( $row->fa_minor_mime ) || $row->fa_minor_mime == 'unknown'
+                               || is_null( $row->fa_media_type ) || $row->fa_media_type == 'UNKNOWN'
+                               || is_null( $row->fa_metadata ) ) {
+                               // Refresh our metadata
+                               // Required for a new current revision; nice for older ones too. :)
+                               $props = RepoGroup::singleton()->getFileProps( $deletedUrl );
+                       } else {
+                               $props = array(
+                                       'minor_mime' => $row->fa_minor_mime,
+                                       'major_mime' => $row->fa_major_mime,
+                                       'media_type' => $row->fa_media_type,
+                                       'metadata' => $row->fa_metadata );
+                       }
+
+                       if ( $first && !$exists ) {
+                               // The live (current) version cannot be hidden!
+                               if( !$this->unsuppress && $row->fa_deleted ) {
+                                       $this->file->unlock();
+                                       return $status;
+                               }
+                               // This revision will be published as the new current version
+                               $destRel = $this->file->getRel();
+                               $insertCurrent = array(
+                                       'img_name'        => $row->fa_name,
+                                       'img_size'        => $row->fa_size,
+                                       'img_width'       => $row->fa_width,
+                                       'img_height'      => $row->fa_height,
+                                       'img_metadata'    => $props['metadata'],
+                                       'img_bits'        => $row->fa_bits,
+                                       'img_media_type'  => $props['media_type'],
+                                       'img_major_mime'  => $props['major_mime'],
+                                       'img_minor_mime'  => $props['minor_mime'],
+                                       'img_description' => $row->fa_description,
+                                       'img_user'        => $row->fa_user,
+                                       'img_user_text'   => $row->fa_user_text,
+                                       'img_timestamp'   => $row->fa_timestamp,
+                                       'img_sha1'        => $sha1);
+                       } else {
+                               $archiveName = $row->fa_archive_name;
+                               if( $archiveName == '' ) {
+                                       // This was originally a current version; we
+                                       // have to devise a new archive name for it.
+                                       // Format is <timestamp of archiving>!<name>
+                                       $timestamp = wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
+                                       do {
+                                               $archiveName = wfTimestamp( TS_MW, $timestamp ) . '!' . $row->fa_name;
+                                               $timestamp++;
+                                       } while ( isset( $archiveNames[$archiveName] ) );
                                }
+                               $archiveNames[$archiveName] = true;
+                               $destRel = $this->file->getArchiveRel( $archiveName );
+                               $insertBatch[] = array(
+                                       'oi_name'         => $row->fa_name,
+                                       'oi_archive_name' => $archiveName,
+                                       'oi_size'         => $row->fa_size,
+                                       'oi_width'        => $row->fa_width,
+                                       'oi_height'       => $row->fa_height,
+                                       'oi_bits'         => $row->fa_bits,
+                                       'oi_description'  => $row->fa_description,
+                                       'oi_user'         => $row->fa_user,
+                                       'oi_user_text'    => $row->fa_user_text,
+                                       'oi_timestamp'    => $row->fa_timestamp,
+                                       'oi_metadata'     => $props['metadata'],
+                                       'oi_media_type'   => $props['media_type'],
+                                       'oi_major_mime'   => $props['major_mime'],
+                                       'oi_minor_mime'   => $props['minor_mime'],
+                                       'oi_deleted'      => $this->unsuppress ? 0 : $row->fa_deleted,
+                                       'oi_sha1'         => $sha1 );
+                       }
 
-                               $transaction->add( $store->export( $row->fa_storage_key,
-                                       $destPath, $flags ) );
+                       $deleteIds[] = $row->fa_id;
+                       if( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
+                               // private files can stay where they are
+                       } else {
+                               $storeBatch[] = array( $deletedUrl, 'public', $destRel );
+                               $this->cleanupBatch[] = $row->fa_storage_key;
                        }
+                       $first = false;
+               }
+               unset( $result );
 
-                       $dbw->immediateCommit();
-               } catch( MWException $e ) {
-                       wfDebug( __METHOD__." caught error, aborting\n" );
-                       $transaction->rollback();
-                       $dbw->rollback();
-                       throw $e;
+               // Add a warning to the status object for missing IDs
+               $missingIds = array_diff( $this->ids, $idsPresent );
+               foreach ( $missingIds as $id ) {
+                       $status->error( 'undelete-missing-filearchive', $id );
                }
 
-               $transaction->commit();
-               FileStore::unlock();
+               // Run the store batch
+               // Use the OVERWRITE_SAME flag to smooth over a common error
+               $storeStatus = $this->file->repo->storeBatch( $storeBatch, FileRepo::OVERWRITE_SAME );
+               $status->merge( $storeStatus );
+
+               if ( !$status->ok ) {
+                       // Store batch returned a critical error -- this usually means nothing was stored
+                       // Stop now and return an error
+                       $this->file->unlock();
+                       return $status;
+               }
 
-               if( $revisions > 0 ) {
+               // Run the DB updates
+               // Because we have locked the image row, key conflicts should be rare.
+               // If they do occur, we can roll back the transaction at this time with
+               // no data loss, but leaving unregistered files scattered throughout the
+               // public zone.
+               // This is not ideal, which is why it's important to lock the image row.
+               if ( $insertCurrent ) {
+                       $dbw->insert( 'image', $insertCurrent, __METHOD__ );
+               }
+               if ( $insertBatch ) {
+                       $dbw->insert( 'oldimage', $insertBatch, __METHOD__ );
+               }
+               if ( $deleteIds ) {
+                       $dbw->delete( 'filearchive',
+                               array( 'fa_id IN (' . $dbw->makeList( $deleteIds ) . ')' ),
+                               __METHOD__ );
+               }
+
+               if( $status->successCount > 0 ) {
                        if( !$exists ) {
-                               wfDebug( __METHOD__." restored $revisions items, creating a new current\n" );
+                               wfDebug( __METHOD__." restored {$status->successCount} 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();
+                               $this->file->purgeEverything();
                        } else {
-                               wfDebug( __METHOD__." restored $revisions as archived versions\n" );
-                               $this->purgeDescription();
+                               wfDebug( __METHOD__." restored {$status->successCount} as archived versions\n" );
+                               $this->file->purgeDescription();
+                               $this->file->purgeHistory();
                        }
                }
-
-               return $revisions;
-       }
-
-       /** isMultipage inherited */
-       /** pageCount inherited */
-       /** scaleHeight inherited */
-       /** getImageSize inherited */
-       
-       /**
-        * Get the URL of the file description page. 
-        */
-       function getDescriptionUrl() {
-               return $this->title->getLocalUrl();
+               $this->file->unlock();
+               return $status;
        }
 
        /**
-        * Get the HTML text of the description page
-        * This is not used by ImagePage for local files, since (among other things)
-        * it skips the parser cache.
+        * Delete unused files in the deleted zone.
+        * This should be called from outside the transaction in which execute() was called.
         */
-       function getDescriptionText() {
-               global $wgParser;
-               $revision = Revision::newFromTitle( $this->title );
-               if ( !$revision ) return false;
-               $text = $revision->getText();
-               if ( !$text ) return false;
-               $html = $wgParser->parse( $text, new ParserOptions );
-               return $html;
+       function cleanup() {
+               if ( !$this->cleanupBatch ) {
+                       return $this->file->repo->newGood();
+               }
+               $status = $this->file->repo->cleanupDeletedBatch( $this->cleanupBatch );
+               return $status;
        }
+}
 
-       function getTimestamp() {
-               $this->load();
-               return $this->timestamp;
-       }
-} // LocalFile class
+#------------------------------------------------------------------------------
 
 /**
- * Backwards compatibility class
+ * Helper class for file movement
+ * @ingroup FileRepo
  */
-class Image extends LocalFile {
-       function __construct( $title ) {
-               $repo = RepoGroup::singleton()->getLocalRepo();
-               parent::__construct( $title, $repo );
+class LocalFileMoveBatch {
+       var $file, $cur, $olds, $oldcount, $archive, $thumbs, $target, $db;
+
+       function __construct( File $file, Title $target, Database $db ) {
+               $this->file = $file;
+               $this->target = $target;
+               $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
+               $this->newHash = $this->file->repo->getHashPath( $this->target->getDbKey() );
+               $this->oldName = $this->file->getName();
+               $this->newName = $this->file->repo->getNameFromTitle( $this->target );
+               $this->oldRel = $this->oldHash . $this->oldName;
+               $this->newRel = $this->newHash . $this->newName;
+               $this->db = $db;
        }
 
-       /**
-        * Wrapper for wfFindFile(), for backwards-compatibility only
-        * Do not use in core code.
-        * @deprecated
-        */
-       static function newFromTitle( $title, $time = false ) {
-               $img = wfFindFile( $title, $time );
-               if ( !$img ) {
-                       $img = wfLocalFile( $title );
-               }
-               return $img;
+       function addCurrent() {
+               $this->cur = array( $this->oldRel, $this->newRel );
        }
-       
-       /**
-        * Wrapper for wfFindFile(), for backwards-compatibility only.
-        * Do not use in core code.
-        *
-        * @param string $name name of the image, used to create a title object using Title::makeTitleSafe
-        * @return image object or null if invalid title
-        * @deprecated
-        */
-       static function newFromName( $name ) {
-               $title = Title::makeTitleSafe( NS_IMAGE, $name );
-               if ( is_object( $title ) ) {
-                       $img = wfFindFile( $title );
-                       if ( !$img ) {
-                               $img = wfLocalFile( $title );
+
+       function addThumbs() {
+               // Thumbnails are purged, so no need to move them
+               $this->thumbs = array();
+       }
+
+       function addOlds() {
+               $archiveBase = 'archive';
+               $this->olds = array();
+               $this->oldcount = 0;
+
+               $result = $this->db->select( 'oldimage',
+                       array( 'oi_archive_name', 'oi_deleted' ),
+                       array( 'oi_name' => $this->oldName ),
+                       __METHOD__
+               );
+               while( $row = $this->db->fetchObject( $result ) ) {
+                       $oldname = $row->oi_archive_name;
+                       $bits = explode( '!', $oldname, 2 );
+                       if( count( $bits ) != 2 ) {
+                               wfDebug( 'Invalid old file name: ' . $oldname );
+                               continue;
                        }
-                       return $img;
-               } else {
-                       return NULL;
+                       list( $timestamp, $filename ) = $bits;
+                       if( $this->oldName != $filename ) {
+                               wfDebug( 'Invalid old file name:' . $oldName );
+                               continue;
+                       }
+                       $this->oldcount++;
+                       // Do we want to add those to oldcount?
+                       if( $row->oi_deleted & File::DELETED_FILE ) {
+                               continue;
+                       }
+                       $this->olds[] = array(
+                               "{$archiveBase}/{$this->oldHash}{$oldname}",
+                               "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
+                       );
                }
+               $this->db->freeResult( $result );
        }
-       
-       /**
-        * Return the URL of an image, provided its name.
-        *
-        * Backwards-compatibility for extensions.
-        * Note that fromSharedDirectory will only use the shared path for files
-        * that actually exist there now, and will return local paths otherwise.
-        *
-        * @param string $name  Name of the image, without the leading "Image:"
-        * @param boolean $fromSharedDirectory  Should this be in $wgSharedUploadPath?
-        * @return string URL of $name image
-        * @deprecated
-        */
-       static function imageUrl( $name, $fromSharedDirectory = false ) {
-               $image = null;
-               if( $fromSharedDirectory ) {
-                       $image = wfFindFile( $name );
-               }
-               if( !$image ) {
-                       $image = wfLocalFile( $name );
+
+       function execute() {
+               $repo = $this->file->repo;
+               $status = $repo->newGood();
+               $triplets = $this->getMoveTriplets();
+
+               $statusDb = $this->doDBUpdates();
+               $statusMove = $repo->storeBatch( $triplets, FSRepo::DELETE_SOURCE );
+               if( !$statusMove->isOk() ) {
+                       $this->db->rollback();
                }
-               return $image->getUrl();
+               $status->merge( $statusDb );
+               $status->merge( $statusMove );
+               return $status;
        }
-}
 
-/**
- * Aliases for backwards compatibility with 1.6
- */
-define( 'MW_IMG_DELETED_FILE', File::DELETED_FILE );
-define( 'MW_IMG_DELETED_COMMENT', File::DELETED_COMMENT );
-define( 'MW_IMG_DELETED_USER', File::DELETED_USER );
-define( 'MW_IMG_DELETED_RESTRICTED', File::DELETED_RESTRICTED );
+       function doDBUpdates() {
+               $repo = $this->file->repo;
+               $status = $repo->newGood();
+               $dbw = $this->db;
 
+               // Update current image
+               $dbw->update( 
+                       'image',
+                       array( 'img_name' => $this->newName ),
+                       array( 'img_name' => $this->oldName ),
+                       __METHOD__
+               );
+               if( $dbw->affectedRows() ) {
+                       $status->successCount++;
+               } else {
+                       $status->failCount++;
+               }
 
+               // Update old images
+               $dbw->update(
+                       'oldimage',
+                       array(
+                               'oi_name' => $this->newName,
+                               'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name', $dbw->addQuotes($this->oldName), $dbw->addQuotes($this->newName) ),
+                       ),
+                       array( 'oi_name' => $this->oldName ),
+                       __METHOD__
+               );
+               $affected = $dbw->affectedRows();
+               $total = $this->oldcount;
+               $status->successCount += $affected;
+               $status->failCount += $total - $affected;
+
+               return $status;
+       }
+
+       // Generates triplets for FSRepo::storeBatch()
+       function getMoveTriplets() {
+               $moves = array_merge( array( $this->cur ), $this->olds, $this->thumbs );
+               $triplets = array();    // The format is: (srcUrl, destZone, destUrl)
+               foreach( $moves as $move ) {
+                       // $move: (oldRelativePath, newRelativePath)
+                       $srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
+                       $triplets[] = array( $srcUrl, 'public', $move[1] );
+               }
+               return $triplets;
+       }
+}