* (bug 20131) PHP Notice: Undfined index: page_latest in includes/ChangesList.php...
[lhc/web/wiklou.git] / includes / filerepo / LocalFile.php
index fcd9d12..ce70ea2 100644 (file)
@@ -49,6 +49,7 @@ 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
+               $missing,          # True if file is not present in file system. Not to be cached in memcached
                $deleted;       # Bitfield akin to rev_deleted
 
        /**#@-*/
@@ -132,11 +133,12 @@ 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 );
+               return $this->repo->getSharedCacheKey( 'file', $hashedName );
        }
 
        /**
@@ -394,6 +396,14 @@ 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
         *
@@ -581,8 +591,10 @@ class LocalFile extends File
        function purgeHistory() {
                global $wgMemc;
                $hashedName = md5($this->getName());
-               $oldKey = wfMemcKey( 'oldfile', $hashedName );
-               $wgMemc->delete( $oldKey );
+               $oldKey = $this->repo->getSharedCacheKey( 'oldfile', $hashedName );
+               if ( $oldKey ) {
+                       $wgMemc->delete( $oldKey );
+               }
        }
 
        /**
@@ -633,7 +645,7 @@ class LocalFile extends File
                $fields = OldLocalFile::selectFields();
                $conds = $opts = $join_conds = array();
                $eq = $inc ? "=" : "";
-               $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBKey() );
+               $conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBkey() );
                if( $start ) {
                        $conds[] = "oi_timestamp <$eq " . $dbr->addQuotes( $dbr->timestamp( $start ) );
                }
@@ -1062,7 +1074,7 @@ class LocalFile extends File
         *
         * @param $reason
         * @param $suppress
-        * @throws MWException or FSException on database or filestore failure
+        * @throws MWException or FSException on database or file store failure
         * @return FileRepoStatus object.
         */
        function deleteOld( $archiveName, $reason, $suppress=false ) {
@@ -1389,7 +1401,7 @@ class LocalFileDeleteBatch {
         * Run the transaction
         */
        function execute() {
-               global $wgUser, $wgUseSquid;
+               global $wgUseSquid;
                wfProfileIn( __METHOD__ );
 
                $this->file->lock();
@@ -1402,7 +1414,7 @@ class LocalFileDeleteBatch {
                                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 ),
+                                       $dbw->bitAnd('oi_deleted', File::DELETED_FILE) => File::DELETED_FILE ),
                                __METHOD__ );
                        while( $row = $dbw->fetchObject( $res ) ) {
                                $privateFiles[$row->oi_archive_name] = 1;
@@ -1432,6 +1444,9 @@ 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() ) {
@@ -1465,6 +1480,22 @@ class LocalFileDeleteBatch {
                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;
+       }
 }
 
 #------------------------------------------------------------------------------
@@ -1512,7 +1543,7 @@ 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();
@@ -1657,6 +1688,9 @@ class LocalFileRestoreBatch {
                        $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 );
@@ -1687,7 +1721,7 @@ class LocalFileRestoreBatch {
                                __METHOD__ );
                }
 
-               if( $status->successCount > 0 ) {
+               if( $status->successCount > 0 || !$storeBatch ) {       // If store batch is empty (all files are missing), deletion is to be considered successful
                        if( !$exists ) {
                                wfDebug( __METHOD__." restored {$status->successCount} items, creating a new current\n" );
 
@@ -1706,6 +1740,38 @@ class LocalFileRestoreBatch {
                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.
@@ -1714,6 +1780,7 @@ 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;
        }
@@ -1732,7 +1799,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;
@@ -1793,11 +1860,7 @@ class LocalFileMoveBatch {
                $status = $repo->newGood();
                $triplets = $this->getMoveTriplets();
 
-               $statusPreCheck = $this->checkFileExistence( 0 );
-               if( !$statusPreCheck->isOk() ) {
-                       wfDebugLog( 'imagemove', "Move of {$this->file->name} aborted due to pre-move file existence check failure" );
-                       return $statusPreCheck;
-               }
+               $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 );
@@ -1805,12 +1868,6 @@ class LocalFileMoveBatch {
                if( !$statusMove->isOk() ) {
                        wfDebugLog( 'imagemove', "Error in moving files: " . $statusMove->getWikiText() );
                        $this->db->rollback();
-               } else {
-                       $statusPostCheck = $this->checkFileExistence( 1 );
-                       if( !$statusPostCheck->isOk() ) {
-                               // This clearly mustn't have happend. FSRepo::storeBatch should have given out an error in that case.
-                               wfDebugLog( 'imagemove', "ATTENTION! Move of {$this->file->name} has some files missing, while storeBatch() reported success" );
-                       }
                }
 
                $status->merge( $statusDb );
@@ -1874,21 +1931,20 @@ class LocalFileMoveBatch {
        }
 
        /*
-        * Checks file existence.
-        * Set $key = 0 for source files check
-        * and $key = 1 for destination files check.
+        * Removes non-existent files from move batch.
         */ 
-       function checkFileExistence( $key = 0 ) {
+       function removeNonexistentFiles( $triplets ) {
                $files = array();
-               foreach( array_merge( array( $this->cur ), $this->olds ) as $file )
-                       $files[$file[$key]] = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $file[$key] );
+               foreach( $triplets as $file )
+                       $files[$file[0]] = $file[0];
                $result = $this->file->repo->fileExistsBatch( $files, FSRepo::FILES_ONLY );
-               $status = $this->file->repo->newGood();
-               foreach( $result as $filename => $exists )
-                       if( !$exists ) {
-                               wfDebugLog( 'imagemove', "File {$filename} does not exist" );
-                               $status->fatal( 'filenotfound', $filename );
+               $filteredTriplets = array();
+               foreach( $triplets as $file )
+                       if( $result[$file[0]] ) {
+                               $filteredTriplets[] = $file;
+                       } else {
+                               wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
                        }
-               return $status;
+               return $filteredTriplets;
        }
 }