]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/filerepo/LocalFile.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / includes / filerepo / LocalFile.php
index b997d75f3621e85cd847cec0583d4594c6a4cfc5..5489ecb207b22fa1a15e23d03d747a88845f21cb 100644 (file)
@@ -1,5 +1,9 @@
 <?php
 /**
+ * Local file in the wiki's own database
+ *
+ * @file
+ * @ingroup FileRepo
  */
 
 /**
@@ -24,14 +28,14 @@ define( 'MW_FILE_VERSION', 8 );
  *
  * @ingroup FileRepo
  */
-class LocalFile extends File
-{
+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)
+       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)
@@ -49,7 +53,8 @@ class LocalFile extends 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
+               $missing,          # True if file is not present in file system. Not to be cached in memcached
+               $deleted;          # Bitfield akin to rev_deleted
 
        /**#@-*/
 
@@ -71,29 +76,31 @@ class LocalFile extends File
                $title = Title::makeTitle( NS_FILE, $row->img_name );
                $file = new self( $title, $repo );
                $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 ) {
+
+               if ( $timestamp ) {
                        $conds['img_timestamp'] = $timestamp;
                }
-               $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ), $conds, $fname );
-               if( $row ) {
+
+               $dbr = $repo->getSlaveDB();
+               $row = $dbr->selectRow( 'image', self::selectFields(), $conds, __METHOD__ );
+
+               if ( $row ) {
                        return self::newFromRow( $row, $repo );
                } else {
                        return false;
                }
        }
-       
+
        /**
         * Fields in the image table
         */
@@ -121,10 +128,12 @@ class LocalFile extends File
         * Do not call this except from inside a repo class.
         */
        function __construct( $title, $repo ) {
-               if( !is_object( $title ) ) {
-                       throw new MWException( __CLASS__.' constructor given bogus title.' );
+               if ( !is_object( $title ) ) {
+                       throw new MWException( __CLASS__ . ' constructor given bogus title.' );
                }
+
                parent::__construct( $title, $repo );
+
                $this->metadata = '';
                $this->historyLine = 0;
                $this->historyRes = null;
@@ -132,11 +141,13 @@ class LocalFile extends File
        }
 
        /**
-        * Get the memcached key
+        * Get the memcached key for the main data for this file, or false if
+        * there is no access to the shared cache.
         */
        function getCacheKey() {
-               $hashedName = md5($this->getName());
-               return wfMemcKey( 'file', $hashedName );
+               $hashedName = md5( $this->getName() );
+
+               return $this->repo->getSharedCacheKey( 'file', $hashedName );
        }
 
        /**
@@ -144,16 +155,20 @@ class LocalFile extends File
         */
        function loadFromCache() {
                global $wgMemc;
+
                wfProfileIn( __METHOD__ );
                $this->dataLoaded = false;
                $key = $this->getCacheKey();
+
                if ( !$key ) {
+                       wfProfileOut( __METHOD__ );
                        return false;
                }
+
                $cachedValues = $wgMemc->get( $key );
 
                // Check if the key existed and belongs to this version of MediaWiki
-               if ( isset($cachedValues['version']) && ( $cachedValues['version'] == MW_FILE_VERSION ) ) {
+               if ( isset( $cachedValues['version'] ) && ( $cachedValues['version'] == MW_FILE_VERSION ) ) {
                        wfDebug( "Pulling file metadata from cache key $key\n" );
                        $this->fileExists = $cachedValues['fileExists'];
                        if ( $this->fileExists ) {
@@ -161,6 +176,7 @@ class LocalFile extends File
                        }
                        $this->dataLoaded = true;
                }
+
                if ( $this->dataLoaded ) {
                        wfIncrStats( 'image_cache_hit' );
                } else {
@@ -176,14 +192,18 @@ class LocalFile extends File
         */
        function saveToCache() {
                global $wgMemc;
+
                $this->load();
                $key = $this->getCacheKey();
+
                if ( !$key ) {
                        return;
                }
+
                $fields = $this->getCacheFields( '' );
                $cache = array( 'version' => MW_FILE_VERSION );
                $cache['fileExists'] = $this->fileExists;
+
                if ( $this->fileExists ) {
                        foreach ( $fields as $field ) {
                                $cache[$field] = $this->$field;
@@ -204,9 +224,11 @@ class LocalFile extends File
                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;
                }
+
                if ( !isset( $results[$prefix] ) ) {
                        $prefixedFields = array();
                        foreach ( $fields as $field ) {
@@ -214,6 +236,7 @@ class LocalFile extends File
                        }
                        $results[$prefix] = $prefixedFields;
                }
+
                return $results[$prefix];
        }
 
@@ -232,6 +255,7 @@ class LocalFile extends File
 
                $row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ),
                        array( 'img_name' => $this->getName() ), $fname );
+
                if ( $row ) {
                        $this->loadFromRow( $row );
                } else {
@@ -248,37 +272,46 @@ class LocalFile extends File
        function decodeRow( $row, $prefix = 'img_' ) {
                $array = (array)$row;
                $prefixLength = strlen( $prefix );
+
                // Sanity check prefix once
                if ( substr( key( $array ), 0, $prefixLength ) !== $prefix ) {
-                       throw new MWException( __METHOD__. ': incorrect $prefix parameter' );
+                       throw new MWException( __METHOD__ .  ': incorrect $prefix parameter' );
                }
+
                $decoded = array();
+
                foreach ( $array as $name => $value ) {
                        $decoded[substr( $name, $prefixLength )] = $value;
                }
+
                $decoded['timestamp'] = wfTimestamp( TS_MW, $decoded['timestamp'] );
+
                if ( empty( $decoded['major_mime'] ) ) {
-                       $decoded['mime'] = "unknown/unknown";
+                       $decoded['mime'] = 'unknown/unknown';
                } else {
-                       if (!$decoded['minor_mime']) {
-                               $decoded['minor_mime'] = "unknown";
+                       if ( !$decoded['minor_mime'] ) {
+                               $decoded['minor_mime'] = 'unknown';
                        }
-                       $decoded['mime'] = $decoded['major_mime'].'/'.$decoded['minor_mime'];
+                       $decoded['mime'] = $decoded['major_mime'] . '/' . $decoded['minor_mime'];
                }
+
                # Trim zero padding from char/binary field
                $decoded['sha1'] = rtrim( $decoded['sha1'], "\0" );
+
                return $decoded;
        }
 
-       /*
+       /**
         * 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;
                $this->maybeUpgradeRow();
        }
@@ -303,7 +336,8 @@ class LocalFile extends File
                if ( wfReadOnly() ) {
                        return;
                }
-               if ( is_null($this->media_type) ||
+
+               if ( is_null( $this->media_type ) ||
                        $this->mime == 'image/svg'
                ) {
                        $this->upgradeRow();
@@ -331,16 +365,19 @@ class LocalFile extends File
 
                # Don't destroy file info of missing files
                if ( !$this->fileExists ) {
-                       wfDebug( __METHOD__.": file does not exist, aborting\n" );
+                       wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
+                       wfProfileOut( __METHOD__ );
                        return;
                }
+
                $dbw = $this->repo->getMasterDB();
                list( $major, $minor ) = self::splitMime( $this->mime );
 
                if ( wfReadOnly() ) {
+                       wfProfileOut( __METHOD__ );
                        return;
                }
-               wfDebug(__METHOD__.': upgrading '.$this->getName()." to the current schema\n");
+               wfDebug( __METHOD__ . ': upgrading ' . $this->getName() . " to the current schema\n" );
 
                $dbw->update( 'image',
                        array(
@@ -355,6 +392,7 @@ class LocalFile extends File
                        ), array( 'img_name' => $this->getName() ),
                        __METHOD__
                );
+
                $this->saveToCache();
                wfProfileOut( __METHOD__ );
        }
@@ -370,15 +408,18 @@ class LocalFile extends File
                $this->dataLoaded = true;
                $fields = $this->getCacheFields( '' );
                $fields[] = 'fileExists';
+
                foreach ( $fields as $field ) {
                        if ( isset( $info[$field] ) ) {
                                $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'] ) ) {
+                       $this->mime = $info['mime'];
                        list( $this->major_mime, $this->minor_mime ) = self::splitMime( $this->mime );
                }
        }
@@ -391,14 +432,22 @@ class LocalFile extends File
        /** getPath inherited */
        /** isVisible inhereted */
 
+       function isMissing() {
+               if ( $this->missing === null ) {
+                       list( $fileExists ) = $this->repo->fileExistsBatch( array( $this->getVirtualUrl() ), FileRepo::FILES_ONLY );
+                       $this->missing = !$fileExists;
+               }
+               return $this->missing;
+       }
+
        /**
         * Return the width of the image
         *
         * Returns false on error
-        * @public
         */
-       function getWidth( $page = 1 ) {
+       public function getWidth( $page = 1 ) {
                $this->load();
+
                if ( $this->isMultipage() ) {
                        $dim = $this->getHandler()->getPageDimensions( $this, $page );
                        if ( $dim ) {
@@ -415,10 +464,10 @@ class LocalFile extends File
         * Return the height of the image
         *
         * Returns false on error
-        * @public
         */
-       function getHeight( $page = 1 ) {
+       public function getHeight( $page = 1 ) {
                $this->load();
+
                if ( $this->isMultipage() ) {
                        $dim = $this->getHandler()->getPageDimensions( $this, $page );
                        if ( $dim ) {
@@ -436,11 +485,12 @@ class LocalFile extends File
         *
         * @param $type string 'text' or 'id'
         */
-       function getUser($type='text') {
+       function getUser( $type = 'text' ) {
                $this->load();
-               if( $type == 'text' ) {
+
+               if ( $type == 'text' ) {
                        return $this->user_text;
-               } elseif( $type == 'id' ) {
+               } elseif ( $type == 'id' ) {
                        return $this->user;
                }
        }
@@ -460,9 +510,8 @@ class LocalFile extends File
 
        /**
         * Return the size of the image file, in bytes
-        * @public
         */
-       function getSize() {
+       public function getSize() {
                $this->load();
                return $this->size;
        }
@@ -493,9 +542,8 @@ class LocalFile extends File
        /**
         * Returns true if the file file exists on disk.
         * @return boolean Whether file file exist on disk.
-        * @public
         */
-       function exists() {
+       public function exists() {
                $this->load();
                return $this->fileExists;
        }
@@ -513,12 +561,13 @@ class LocalFile extends File
        function migrateThumbFile( $thumbName ) {
                $thumbDir = $this->getThumbPath();
                $thumbPath = "$thumbDir/$thumbName";
+
                if ( is_dir( $thumbPath ) ) {
                        // Directory where file should be
                        // This happened occasionally due to broken migration code in 1.5
                        // Rename to broken-*
                        for ( $i = 0; $i < 100 ; $i++ ) {
-                               $broken = $this->repo->getZonePath('public') . "/broken-$i-$thumbName";
+                               $broken = $this->repo->getZonePath( 'public' ) . "/broken-$i-$thumbName";
                                if ( !file_exists( $broken ) ) {
                                        rename( $thumbPath, $broken );
                                        break;
@@ -527,6 +576,7 @@ class LocalFile extends File
                        // Doesn't exist anymore
                        clearstatcache();
                }
+
                if ( is_file( $thumbDir ) ) {
                        // File where directory should be
                        unlink( $thumbDir );
@@ -544,6 +594,7 @@ class LocalFile extends File
         */
        function getThumbnails() {
                $this->load();
+
                $files = array();
                $dir = $this->getThumbPath();
 
@@ -551,11 +602,12 @@ class LocalFile extends File
                        $handle = opendir( $dir );
 
                        if ( $handle ) {
-                               while ( false !== ( $file = readdir($handle) ) ) {
-                                       if ( $file{0} != '.' ) {
+                               while ( false !== ( $file = readdir( $handle ) ) ) {
+                                       if ( $file { 0 } != '.' ) {
                                                $files[] = $file;
                                        }
                                }
+
                                closedir( $handle );
                        }
                }
@@ -577,9 +629,13 @@ class LocalFile extends File
         */
        function purgeHistory() {
                global $wgMemc;
-               $hashedName = md5($this->getName());
-               $oldKey = wfMemcKey( 'oldfile', $hashedName );
-               $wgMemc->delete( $oldKey );
+
+               $hashedName = md5( $this->getName() );
+               $oldKey = $this->repo->getSharedCacheKey( 'oldfile', $hashedName );
+
+               if ( $oldKey ) {
+                       $wgMemc->delete( $oldKey );
+               }
        }
 
        /**
@@ -601,10 +657,12 @@ class LocalFile extends File
         */
        function purgeThumbnails() {
                global $wgUseSquid;
+
                // Delete thumbnails
                $files = $this->getThumbnails();
                $dir = $this->getThumbPath();
                $urls = array();
+
                foreach ( $files as $file ) {
                        # Check that the base file name is part of the thumb name
                        # This is a basic sanity check to avoid erasing unrelated directories
@@ -624,38 +682,49 @@ class LocalFile extends File
        /** purgeDescription inherited */
        /** purgeEverything inherited */
 
-       function getHistory($limit = null, $start = null, $end = null, $inc = true) {
+       function getHistory( $limit = null, $start = null, $end = null, $inc = true ) {
                $dbr = $this->repo->getSlaveDB();
-               $tables = array('oldimage');
+               $tables = array( 'oldimage' );
                $fields = OldLocalFile::selectFields();
                $conds = $opts = $join_conds = array();
-               $eq = $inc ? "=" : "";
-               $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBKey() );
-               if( $start ) {
+               $eq = $inc ? '=' : '';
+               $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBkey() );
+
+               if ( $start ) {
                        $conds[] = "oi_timestamp <$eq " . $dbr->addQuotes( $dbr->timestamp( $start ) );
                }
-               if( $end ) {
+
+               if ( $end ) {
                        $conds[] = "oi_timestamp >$eq " . $dbr->addQuotes( $dbr->timestamp( $end ) );
                }
-               if( $limit ) {
+
+               if ( $limit ) {
                        $opts['LIMIT'] = $limit;
                }
+
                // Search backwards for time > x queries
-               $order = (!$start && $end !== null) ? "ASC" : "DESC";
+               $order = ( !$start && $end !== null ) ? 'ASC' : 'DESC';
                $opts['ORDER BY'] = "oi_timestamp $order";
-               $opts['USE INDEX'] = array('oldimage' => 'oi_name_timestamp');
-               
-               wfRunHooks( 'LocalFile::getHistory', array( &$this, &$tables, &$fields, 
+               $opts['USE INDEX'] = array( 'oldimage' => 'oi_name_timestamp' );
+
+               wfRunHooks( 'LocalFile::getHistory', array( &$this, &$tables, &$fields,
                        &$conds, &$opts, &$join_conds ) );
-               
+
                $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $opts, $join_conds );
                $r = array();
-               while( $row = $dbr->fetchObject($res) ) {
-                       $r[] = OldLocalFile::newFromRow($row, $this->repo);
+
+               foreach ( $res as $row ) {
+                       if ( $this->repo->oldFileFromRowFactory ) {
+                               $r[] = call_user_func( $this->repo->oldFileFromRowFactory, $row, $this->repo );
+                       } else {
+                               $r[] = OldLocalFile::newFromRow( $row, $this->repo );
+                       }
                }
-               if( $order == "ASC" ) {
+
+               if ( $order == 'ASC' ) {
                        $r = array_reverse( $r ); // make sure it ends up descending
                }
+
                return $r;
        }
 
@@ -666,10 +735,8 @@ class LocalFile extends File
         *  0      return line for current version
         *  1      query for old versions, return first one
         *  2, ... return next old version from above query
-        *
-        * @public
         */
-       function nextHistoryLine() {
+       public function nextHistoryLine() {
                # Polymorphic function name to distinguish foreign and local fetches
                $fname = get_class( $this ) . '::' . __FUNCTION__;
 
@@ -686,13 +753,12 @@ class LocalFile extends File
                                array( 'img_name' => $this->title->getDBkey() ),
                                $fname
                        );
+
                        if ( 0 == $dbr->numRows( $this->historyRes ) ) {
-                               $dbr->freeResult($this->historyRes);
                                $this->historyRes = null;
-                               return FALSE;
+                               return false;
                        }
-               } else if ( $this->historyLine == 1 ) {
-                       $dbr->freeResult($this->historyRes);
+               } elseif ( $this->historyLine == 1 ) {
                        $this->historyRes = $dbr->select( 'oldimage', '*',
                                array( 'oi_name' => $this->title->getDBkey() ),
                                $fname,
@@ -706,12 +772,11 @@ class LocalFile extends File
 
        /**
         * Reset the history pointer to the first element of the history
-        * @public
         */
-       function resetHistory() {
+       public function resetHistory() {
                $this->historyLine = 0;
-               if (!is_null($this->historyRes)) {
-                       $this->repo->getSlaveDB()->freeResult($this->historyRes);
+
+               if ( !is_null( $this->historyRes ) ) {
                        $this->historyRes = null;
                }
        }
@@ -732,14 +797,16 @@ class LocalFile extends File
 
        /**
         * Upload a file and record it in the DB
-        * @param string $srcPath Source path or virtual URL
-        * @param string $comment Upload description
-        * @param string $pageText Text to use for the new description page, if a new description page is created
-        * @param integer $flags Flags for publish()
-        * @param array $props File properties, if known. This can be used to reduce the
-        *                         upload time when uploading virtual URLs for which the file info
-        *                         is already known
-        * @param string $timestamp Timestamp for img_timestamp, or false to use the current time
+        * @param $srcPath String: source path or virtual URL
+        * @param $comment String: upload description
+        * @param $pageText String: text to use for the new description page,
+        *                  if a new description page is created
+        * @param $flags Integer: flags for publish()
+        * @param $props Array: File properties, if known. This can be used to reduce the
+        *               upload time when uploading virtual URLs for which the file info
+        *               is already known
+        * @param $timestamp String: timestamp for img_timestamp, or false to use the current time
+        * @param $user Mixed: User object or null to use $wgUser
         *
         * @return FileRepoStatus object. On success, the value member contains the
         *     archive name, or an empty string if it was a new file.
@@ -747,12 +814,15 @@ class LocalFile extends File
        function upload( $srcPath, $comment, $pageText, $flags = 0, $props = false, $timestamp = false, $user = null ) {
                $this->lock();
                $status = $this->publish( $srcPath, $flags );
+
                if ( $status->ok ) {
                        if ( !$this->recordUpload2( $status->value, $comment, $pageText, $props, $timestamp, $user ) ) {
                                $status->fatal( 'filenotfound', $srcPath );
                        }
                }
+
                $this->unlock();
+
                return $status;
        }
 
@@ -763,10 +833,12 @@ class LocalFile extends File
        function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '',
                $watch = false, $timestamp = false )
        {
-               $pageText = UploadForm::getInitialPageText( $desc, $license, $copyStatus, $source );
+               $pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source );
+
                if ( !$this->recordUpload2( $oldver, $desc, $pageText ) ) {
                        return false;
                }
+
                if ( $watch ) {
                        global $wgUser;
                        $wgUser->addWatch( $this->getTitle() );
@@ -778,11 +850,12 @@ 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, $user = null )
-       {
-               if( is_null( $user ) ) {
+       function recordUpload2(
+               $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null
+       ) {
+               if ( is_null( $user ) ) {
                        global $wgUser;
-                       $user = $wgUser; 
+                       $user = $wgUser;
                }
 
                $dbw = $this->repo->getMasterDB();
@@ -791,27 +864,30 @@ class LocalFile extends File
                if ( !$props ) {
                        $props = $this->repo->getFileProps( $this->getVirtualUrl() );
                }
+
+               if ( $timestamp === false ) {
+                       $timestamp = $dbw->timestamp();
+               }
+
                $props['description'] = $comment;
                $props['user'] = $user->getId();
                $props['user_text'] = $user->getName();
-               $props['timestamp'] = wfTimestamp( TS_MW );
+               $props['timestamp'] = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW
                $this->setProps( $props );
 
-               // Delete thumbnails and refresh the metadata cache
+               # Delete thumbnails
                $this->purgeThumbnails();
-               $this->saveToCache();
+
+               # The file is already on its final location, remove it from the squid cache
                SquidUpdate::purge( array( $this->getURL() ) );
 
-               // Fail now if the file isn't there
+               # Fail now if the file isn't there
                if ( !$this->fileExists ) {
-                       wfDebug( __METHOD__.": File ".$this->getPath()." went missing!\n" );
+                       wfDebug( __METHOD__ . ": File " . $this->getPath() . " went missing!\n" );
                        return false;
                }
 
                $reupload = false;
-               if ( $timestamp === false ) {
-                       $timestamp = $dbw->timestamp();
-               }
 
                # Test to see if the row exists using INSERT IGNORE
                # This avoids race conditions by locking the row until the commit, and also
@@ -819,7 +895,7 @@ class LocalFile extends File
                $dbw->insert( 'image',
                        array(
                                'img_name' => $this->getName(),
-                               'img_size'=> $this->size,
+                               'img_size' => $this->size,
                                'img_width' => intval( $this->width ),
                                'img_height' => intval( $this->height ),
                                'img_bits' => $this->bits,
@@ -837,7 +913,7 @@ class LocalFile extends File
                        'IGNORE'
                );
 
-               if( $dbw->affectedRows() == 0 ) {
+               if ( $dbw->affectedRows() == 0 ) {
                        $reupload = true;
 
                        # Collision, this is an update of a file
@@ -898,38 +974,51 @@ class LocalFile extends File
                $action = $reupload ? 'overwrite' : 'upload';
                $log->addEntry( $action, $descTitle, $comment, array(), $user );
 
-               if( $descTitle->exists() ) {
+               if ( $descTitle->exists() ) {
                        # Create a null revision
                        $latest = $descTitle->getLatestRevID();
-                       $nullRevision = Revision::newNullRevision( $dbw, $descTitle->getArticleId(),
-                               $log->getRcComment(), false );
+                       $nullRevision = Revision::newNullRevision(
+                               $dbw,
+                               $descTitle->getArticleId(),
+                               $log->getRcComment(),
+                               false
+                       );
                        $nullRevision->insertOn( $dbw );
-                       
-                       wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, $latest, $user) );
+
+                       wfRunHooks( 'NewRevisionFromEditComplete', array( $article, $nullRevision, $latest, $user ) );
                        $article->updateRevisionOn( $dbw, $nullRevision );
 
                        # Invalidate the cache for the description page
                        $descTitle->invalidateCache();
                        $descTitle->purgeSquid();
                } else {
-                       // New file; create the description page.
-                       // There's already a log entry, so don't make a second RC entry
+                       # New file; create the description page.
+                       # There's already a log entry, so don't make a second RC entry
+                       # Squid and file cache for the description page are purged by doEdit.
                        $article->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC );
                }
 
-               # Hooks, hooks, the magic of hooks...
-               wfRunHooks( 'FileUpload', array( $this ) );
-
                # Commit the transaction now, in case something goes wrong later
                # The most important thing is that files don't get lost, especially archives
-               $dbw->immediateCommit();
+               $dbw->commit();
+
+               # Save to cache and purge the squid
+               # We shall not saveToCache before the commit since otherwise
+               # in case of a rollback there is an usable file from memcached
+               # which in fact doesn't really exist (bug 24978)
+               $this->saveToCache();
+
+               # Hooks, hooks, the magic of hooks...
+               wfRunHooks( 'FileUpload', array( $this, $reupload, $descTitle->exists() ) );
 
                # 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 ) {
+
+               foreach ( $redirs as $redir ) {
                        $update = new HTMLCacheUpdate( $redir, 'imagelinks' );
                        $update->doUpdate();
                }
@@ -939,15 +1028,14 @@ class LocalFile extends File
 
        /**
         * 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.
+        * destination, move it to an archive. Returns a FileRepoStatus object with
+        * the archive name in the "value" member on success.
         *
         * 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:
+        * @param $srcPath String: local filesystem path to the source image
+        * @param $flags Integer: a bitwise combination of:
         *     File::DELETE_SOURCE    Delete the source file, i.e. move
         *         rather than copy
         * @return FileRepoStatus object. On success, the value member contains the
@@ -955,17 +1043,21 @@ class LocalFile extends File
         */
        function publish( $srcPath, $flags = 0 ) {
                $this->lock();
+
                $dstRel = $this->getRel();
-               $archiveName = gmdate( 'YmdHis' ) . '!'. $this->getName();
+               $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 ( $status->value == 'new' ) {
                        $status->value = '';
                } else {
                        $status->value = $archiveName;
                }
+
                $this->unlock();
+
                return $status;
        }
 
@@ -989,12 +1081,14 @@ class LocalFile extends File
        function move( $target ) {
                wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() );
                $this->lock();
+
                $batch = new LocalFileMoveBatch( $this, $target );
                $batch->addCurrent();
                $batch->addOlds();
 
                $status = $batch->execute();
                wfDebugLog( 'imagemove', "Finished moving {$this->name}" );
+
                $this->purgeEverything();
                $this->unlock();
 
@@ -1007,7 +1101,7 @@ class LocalFile extends File
                        // Purge the new image
                        $this->purgeEverything();
                }
-               
+
                return $status;
        }
 
@@ -1025,6 +1119,7 @@ class LocalFile extends File
         */
        function delete( $reason, $suppress = false ) {
                $this->lock();
+
                $batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
                $batch->addCurrent();
 
@@ -1033,7 +1128,7 @@ class LocalFile extends File
                $result = $dbw->select( 'oldimage',
                        array( 'oi_archive_name' ),
                        array( 'oi_name' => $this->getName() ) );
-               while ( $row = $dbw->fetchObject( $result ) ) {
+               foreach ( $result as $row ) {
                        $batch->addOld( $row->oi_archive_name );
                }
                $status = $batch->execute();
@@ -1046,6 +1141,7 @@ class LocalFile extends File
                }
 
                $this->unlock();
+
                return $status;
        }
 
@@ -1057,21 +1153,26 @@ 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
+        * @param $archiveName String
+        * @param $reason String
+        * @param $suppress Boolean
+        * @throws MWException or FSException on database or file store failure
         * @return FileRepoStatus object.
         */
-       function deleteOld( $archiveName, $reason, $suppress=false ) {
+       function deleteOld( $archiveName, $reason, $suppress = 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;
        }
 
@@ -1083,17 +1184,20 @@ class LocalFile extends File
         *
         * @param $versions set of record ids of deleted items to restore,
         *                    or empty to restore all revisions.
-        * @param $unuppress
+        * @param $unsuppress Boolean
         * @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;
                }
@@ -1102,6 +1206,7 @@ class LocalFile extends File
                $cleanupStatus->successCount = 0;
                $cleanupStatus->failCount = 0;
                $status->merge( $cleanupStatus );
+
                return $status;
        }
 
@@ -1167,10 +1272,12 @@ class LocalFile extends File
         */
        function lock() {
                $dbw = $this->repo->getMasterDB();
+
                if ( !$this->locked ) {
                        $dbw->begin();
                        $this->locked++;
                }
+
                return $dbw->selectField( 'image', '1', array( 'img_name' => $this->getName() ), __METHOD__ );
        }
 
@@ -1198,7 +1305,7 @@ class LocalFile extends File
        }
 } // LocalFile class
 
-#------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
 
 /**
  * Helper class for file deletion
@@ -1233,25 +1340,33 @@ class LocalFileDeleteBatch {
                        unset( $oldRels['.'] );
                        $deleteCurrent = true;
                }
+
                return array( $oldRels, $deleteCurrent );
        }
 
        /*protected*/ function getHashes() {
                $hashes = array();
                list( $oldRels, $deleteCurrent ) = $this->getOldRels();
+
                if ( $deleteCurrent ) {
                        $hashes['.'] = $this->file->getSha1();
                }
+
                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 ) ) {
+                       $res = $dbw->select(
+                               'oldimage',
+                               array( 'oi_archive_name', 'oi_sha1' ),
+                               'oi_archive_name IN (' . $dbw->makeList( array_keys( $oldRels ) ) . ')',
+                               __METHOD__
+                       );
+
+                       foreach ( $res as $row ) {
                                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',
@@ -1267,10 +1382,13 @@ class LocalFileDeleteBatch {
                                }
                        }
                }
+
                $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] );
@@ -1283,6 +1401,7 @@ class LocalFileDeleteBatch {
 
        function doDBInserts() {
                global $wgUser;
+
                $dbw = $this->file->repo->getMasterDB();
                $encTimestamp = $dbw->addQuotes( $dbw->timestamp() );
                $encUserId = $dbw->addQuotes( $wgUser->getId() );
@@ -1370,6 +1489,7 @@ class LocalFileDeleteBatch {
        function doDBDeletes() {
                $dbw = $this->file->repo->getMasterDB();
                list( $oldRels, $deleteCurrent ) = $this->getOldRels();
+
                if ( count( $oldRels ) ) {
                        $dbw->delete( 'oldimage',
                                array(
@@ -1377,6 +1497,7 @@ class LocalFileDeleteBatch {
                                        'oi_archive_name' => array_keys( $oldRels )
                                ), __METHOD__ );
                }
+
                if ( $deleteCurrent ) {
                        $dbw->delete( 'image', array( 'img_name' => $this->file->getName() ), __METHOD__ );
                }
@@ -1386,7 +1507,7 @@ class LocalFileDeleteBatch {
         * Run the transaction
         */
        function execute() {
-               global $wgUser, $wgUseSquid;
+               global $wgUseSquid;
                wfProfileIn( __METHOD__ );
 
                $this->file->lock();
@@ -1394,14 +1515,16 @@ class LocalFileDeleteBatch {
                $privateFiles = array();
                list( $oldRels, $deleteCurrent ) = $this->getOldRels();
                $dbw = $this->file->repo->getMasterDB();
-               if( !empty( $oldRels ) ) {
+
+               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 ),
+                                       'oi_archive_name IN (' . $dbw->makeList( array_keys( $oldRels ) ) . ')',
+                                       $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE ),
                                __METHOD__ );
-                       while( $row = $dbw->fetchObject( $res ) ) {
+
+                       foreach ( $res as $row ) {
                                $privateFiles[$row->oi_archive_name] = 1;
                        }
                }
@@ -1410,10 +1533,11 @@ class LocalFileDeleteBatch {
                $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) ) {
+                       if ( isset( $hashes[$name] ) && !array_key_exists( $name, $privateFiles ) ) {
                                $hash = $hashes[$name];
                                $key = $hash . $dotExt;
                                $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
@@ -1429,8 +1553,12 @@ class LocalFileDeleteBatch {
                // them in a separate transaction, then run the file ops, then update the fa_name fields.
                $this->doDBInserts();
 
+               // Removes non-existent file from the batch, so we don't get errors.
+               $this->deletionBatch = $this->removeNonexistentFiles( $this->deletionBatch );
+
                // Execute the file deletion batch
                $status = $this->file->repo->deleteBatch( $this->deletionBatch );
+
                if ( !$status->isGood() ) {
                        $this->status->merge( $status );
                }
@@ -1440,12 +1568,14 @@ class LocalFileDeleteBatch {
                        // Roll back inserts, release lock and abort
                        // TODO: delete the defunct filearchive rows if we are using a non-transactional DB
                        $this->file->unlockAndRollback();
+                       wfProfileOut( __METHOD__ );
                        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;
@@ -1459,11 +1589,34 @@ class LocalFileDeleteBatch {
                // Commit and return
                $this->file->unlock();
                wfProfileOut( __METHOD__ );
+
                return $this->status;
        }
+
+       /**
+        * Removes non-existent files from a deletion batch.
+        */
+       function removeNonexistentFiles( $batch ) {
+               $files = $newBatch = array();
+
+               foreach ( $batch as $batchItem ) {
+                       list( $src, $dest ) = $batchItem;
+                       $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
+               }
+
+               $result = $this->file->repo->fileExistsBatch( $files, FSRepo::FILES_ONLY );
+
+               foreach ( $batch as $batchItem ) {
+                       if ( $result[$batchItem[0]] ) {
+                               $newBatch[] = $batchItem;
+                       }
+               }
+
+               return $newBatch;
+       }
 }
 
-#------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
 
 /**
  * Helper class for file undeletion
@@ -1508,7 +1661,8 @@ class LocalFileRestoreBatch {
         * So we save the batch and let the caller call cleanup()
         */
        function execute() {
-               global $wgUser, $wgLang;
+               global $wgLang;
+
                if ( !$this->all && !$this->ids ) {
                        // Do nothing
                        return $this->file->repo->newGood();
@@ -1521,7 +1675,8 @@ class LocalFileRestoreBatch {
                // 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 ) {
+
+               if ( !$this->all ) {
                        $conditions[] = 'fa_id IN (' . $dbw->makeList( $this->ids ) . ')';
                }
 
@@ -1538,7 +1693,8 @@ class LocalFileRestoreBatch {
                $deleteIds = array();
                $first = true;
                $archiveNames = array();
-               while( $row = $dbw->fetchObject( $result ) ) {
+
+               foreach ( $result as $row ) {
                        $idsPresent[] = $row->fa_id;
 
                        if ( $row->fa_name != $this->file->getName() ) {
@@ -1546,6 +1702,7 @@ class LocalFileRestoreBatch {
                                $status->failCount++;
                                continue;
                        }
+
                        if ( $row->fa_storage_key == '' ) {
                                // Revision was missing pre-deletion
                                $status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp ) );
@@ -1557,12 +1714,13 @@ class LocalFileRestoreBatch {
                        $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'
+                       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 ) ) {
@@ -1597,23 +1755,27 @@ class LocalFileRestoreBatch {
                                        'img_timestamp'   => $row->fa_timestamp,
                                        'img_sha1'        => $sha1
                                );
+
                                // The live (current) version cannot be hidden!
-                               if( !$this->unsuppress && $row->fa_deleted ) {
+                               if ( !$this->unsuppress && $row->fa_deleted ) {
                                        $storeBatch[] = array( $deletedUrl, 'public', $destRel );
                                        $this->cleanupBatch[] = $row->fa_storage_key;
                                }
                        } else {
                                $archiveName = $row->fa_archive_name;
-                               if( $archiveName == '' ) {
+
+                               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(
@@ -1636,23 +1798,30 @@ class LocalFileRestoreBatch {
                        }
 
                        $deleteIds[] = $row->fa_id;
-                       if( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
+
+                       if ( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
                                // private files can stay where they are
                                $status->successCount++;
                        } else {
                                $storeBatch[] = array( $deletedUrl, 'public', $destRel );
                                $this->cleanupBatch[] = $row->fa_storage_key;
                        }
+
                        $first = false;
                }
+
                unset( $result );
 
                // 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 );
                }
 
+               // Remove missing files from batch, so we don't get errors when undeleting them
+               $storeBatch = $this->removeNonexistentFiles( $storeBatch );
+
                // Run the store batch
                // Use the OVERWRITE_SAME flag to smooth over a common error
                $storeStatus = $this->file->repo->storeBatch( $storeBatch, FileRepo::OVERWRITE_SAME );
@@ -1662,6 +1831,7 @@ class LocalFileRestoreBatch {
                        // Store batch returned a critical error -- this usually means nothing was stored
                        // Stop now and return an error
                        $this->file->unlock();
+
                        return $status;
                }
 
@@ -1674,18 +1844,21 @@ class LocalFileRestoreBatch {
                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 {$status->successCount} items, creating a new current\n" );
+               // If store batch is empty (all files are missing), deletion is to be considered successful
+               if ( $status->successCount > 0 || !$storeBatch ) {
+                       if ( !$exists ) {
+                               wfDebug( __METHOD__ . " restored {$status->successCount} items, creating a new current\n" );
 
                                // Update site_stats
                                $site_stats = $dbw->tableName( 'site_stats' );
@@ -1693,15 +1866,59 @@ class LocalFileRestoreBatch {
 
                                $this->file->purgeEverything();
                        } else {
-                               wfDebug( __METHOD__." restored {$status->successCount} as archived versions\n" );
+                               wfDebug( __METHOD__ . " restored {$status->successCount} as archived versions\n" );
                                $this->file->purgeDescription();
                                $this->file->purgeHistory();
                        }
                }
+
                $this->file->unlock();
+
                return $status;
        }
 
+       /**
+        * Removes non-existent files from a store batch.
+        */
+       function removeNonexistentFiles( $triplets ) {
+               $files = $filteredTriplets = array();
+               foreach ( $triplets as $file )
+                       $files[$file[0]] = $file[0];
+
+               $result = $this->file->repo->fileExistsBatch( $files, FSRepo::FILES_ONLY );
+
+               foreach ( $triplets as $file ) {
+                       if ( $result[$file[0]] ) {
+                               $filteredTriplets[] = $file;
+                       }
+               }
+
+               return $filteredTriplets;
+       }
+
+       /**
+        * Removes non-existent files from a cleanup batch.
+        */
+       function removeNonexistentFromCleanup( $batch ) {
+               $files = $newBatch = array();
+               $repo = $this->file->repo;
+
+               foreach ( $batch as $file ) {
+                       $files[$file] = $repo->getVirtualUrl( 'deleted' ) . '/' .
+                               rawurlencode( $repo->getDeletedHashPath( $file ) . $file );
+               }
+
+               $result = $repo->fileExistsBatch( $files, FSRepo::FILES_ONLY );
+
+               foreach ( $batch as $file ) {
+                       if ( $result[$file] ) {
+                               $newBatch[] = $file;
+                       }
+               }
+
+               return $newBatch;
+       }
+
        /**
         * Delete unused files in the deleted zone.
         * This should be called from outside the transaction in which execute() was called.
@@ -1710,12 +1927,16 @@ class LocalFileRestoreBatch {
                if ( !$this->cleanupBatch ) {
                        return $this->file->repo->newGood();
                }
+
+               $this->cleanupBatch = $this->removeNonexistentFromCleanup( $this->cleanupBatch );
+
                $status = $this->file->repo->cleanupDeletedBatch( $this->cleanupBatch );
+
                return $status;
        }
 }
 
-#------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
 
 /**
  * Helper class for file movement
@@ -1728,7 +1949,7 @@ class LocalFileMoveBatch {
                $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->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;
@@ -1736,14 +1957,14 @@ class LocalFileMoveBatch {
                $this->db = $file->repo->getMasterDb();
        }
 
-       /*
+       /**
         * Add the current image to the batch
         */
        function addCurrent() {
                $this->cur = array( $this->oldRel, $this->newRel );
        }
 
-       /*
+       /**
         * Add the old versions of the image to the batch
         */
        function addOlds() {
@@ -1756,32 +1977,38 @@ class LocalFileMoveBatch {
                        array( 'oi_name' => $this->oldName ),
                        __METHOD__
                );
-               while( $row = $this->db->fetchObject( $result ) ) {
+
+               foreach ( $result as $row ) {
                        $oldName = $row->oi_archive_name;
                        $bits = explode( '!', $oldName, 2 );
-                       if( count( $bits ) != 2 ) {
+
+                       if ( count( $bits ) != 2 ) {
                                wfDebug( "Invalid old file name: $oldName \n" );
                                continue;
                        }
+
                        list( $timestamp, $filename ) = $bits;
-                       if( $this->oldName != $filename ) {
+
+                       if ( $this->oldName != $filename ) {
                                wfDebug( "Invalid old file name: $oldName \n" );
                                continue;
                        }
+
                        $this->oldCount++;
+
                        // Do we want to add those to oldCount?
-                       if( $row->oi_deleted & File::DELETED_FILE ) {
+                       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 );
        }
 
-       /*
+       /**
         * Perform the move.
         */
        function execute() {
@@ -1789,22 +2016,28 @@ class LocalFileMoveBatch {
                $status = $repo->newGood();
                $triplets = $this->getMoveTriplets();
 
+               $triplets = $this->removeNonexistentFiles( $triplets );
                $statusDb = $this->doDBUpdates();
                wfDebugLog( 'imagemove', "Renamed {$this->file->name} in database: {$statusDb->successCount} successes, {$statusDb->failCount} failures" );
                $statusMove = $repo->storeBatch( $triplets, FSRepo::DELETE_SOURCE );
                wfDebugLog( 'imagemove', "Moved files for {$this->file->name}: {$statusMove->successCount} successes, {$statusMove->failCount} failures" );
-               if( !$statusMove->isOk() ) {
+
+               if ( !$statusMove->isOk() ) {
                        wfDebugLog( 'imagemove', "Error in moving files: " . $statusMove->getWikiText() );
                        $this->db->rollback();
                }
+
                $status->merge( $statusDb );
                $status->merge( $statusMove );
+
                return $status;
        }
 
-       /*
-        * Do the database updates and return a new WikiError indicating how many
-        * rows where updated.
+       /**
+        * Do the database updates and return a new FileRepoStatus indicating how
+        * many rows where updated.
+        *
+        * @return FileRepoStatus
         */
        function doDBUpdates() {
                $repo = $this->file->repo;
@@ -1812,13 +2045,14 @@ class LocalFileMoveBatch {
                $dbw = $this->db;
 
                // Update current image
-               $dbw->update( 
+               $dbw->update(
                        'image',
                        array( 'img_name' => $this->newName ),
                        array( 'img_name' => $this->oldName ),
                        __METHOD__
                );
-               if( $dbw->affectedRows() ) {
+
+               if ( $dbw->affectedRows() ) {
                        $status->successCount++;
                } else {
                        $status->failCount++;
@@ -1829,11 +2063,12 @@ class LocalFileMoveBatch {
                        'oldimage',
                        array(
                                'oi_name' => $this->newName,
-                               'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name', $dbw->addQuotes($this->oldName), $dbw->addQuotes($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;
@@ -1842,18 +2077,44 @@ class LocalFileMoveBatch {
                return $status;
        }
 
-       /*
+       /**
         * Generate triplets for FSRepo::storeBatch().
-        */ 
+        */
        function getMoveTriplets() {
                $moves = array_merge( array( $this->cur ), $this->olds );
                $triplets = array();    // The format is: (srcUrl, destZone, destUrl)
-               foreach( $moves as $move ) {
+
+               foreach ( $moves as $move ) {
                        // $move: (oldRelativePath, newRelativePath)
                        $srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
                        $triplets[] = array( $srcUrl, 'public', $move[1] );
                        wfDebugLog( 'imagemove', "Generated move triplet for {$this->file->name}: {$srcUrl} :: public :: {$move[1]}" );
                }
+
                return $triplets;
        }
+
+       /**
+        * Removes non-existent files from move batch.
+        */
+       function removeNonexistentFiles( $triplets ) {
+               $files = array();
+
+               foreach ( $triplets as $file ) {
+                       $files[$file[0]] = $file[0];
+               }
+
+               $result = $this->file->repo->fileExistsBatch( $files, FSRepo::FILES_ONLY );
+               $filteredTriplets = array();
+
+               foreach ( $triplets as $file ) {
+                       if ( $result[$file[0]] ) {
+                               $filteredTriplets[] = $file;
+                       } else {
+                               wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
+                       }
+               }
+
+               return $filteredTriplets;
+       }
 }