[FileRepo] Purging/transaction fixes.
authorAaron <aschulz@wikimedia.org>
Wed, 9 May 2012 00:29:34 +0000 (17:29 -0700)
committerAaron <aschulz@wikimedia.org>
Wed, 9 May 2012 00:29:39 +0000 (17:29 -0700)
* Purge old version thumbnails on move
* Pushed purge operations outside of the DB transaction for move/delete

Change-Id: I260940149599eea814226d3c5b7092f4d6544a72

includes/filerepo/file/LocalFile.php

index 467bf1b..af93c53 100644 (file)
@@ -1250,13 +1250,16 @@ class LocalFile extends File {
 
                $this->lock(); // begin
                $batch->addCurrent();
-               $batch->addOlds();
+               $archiveNames = $batch->addOlds();
                $status = $batch->execute();
                $this->unlock(); // done
 
                wfDebugLog( 'imagemove', "Finished moving {$this->name}" );
 
                $this->purgeEverything();
+               foreach ( $archiveNames as $archiveName ) {
+                       $this->purgeOldThumbnails( $archiveName );
+               }
                if ( $status->isOK() ) {
                        // Now switch the object
                        $this->title = $target;
@@ -1287,23 +1290,16 @@ class LocalFile extends File {
                        return $this->readOnlyFatalStatus();
                }
 
-               $dbw = $this->repo->getMasterDB();
                $batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
-               $archiveNames = array();
 
                $this->lock(); // begin
                $batch->addCurrent();
                # Get old version relative paths
-               $result = $dbw->select( 'oldimage',
-                       array( 'oi_archive_name' ),
-                       array( 'oi_name' => $this->getName() ) );
-               foreach ( $result as $row ) {
-                       $batch->addOld( $row->oi_archive_name );
-                       $archiveNames[] = $row->oi_archive_name;
-               }
+               $archiveNames = $batch->addOlds();
                $status = $batch->execute();
                if ( $status->isOK() ) {
                        // Update site_stats
+                       $dbw = $this->repo->getMasterDB();
                        $site_stats = $dbw->tableName( 'site_stats' );
                        $dbw->query( "UPDATE $site_stats SET ss_images=ss_images-1", __METHOD__ );
                }
@@ -1336,15 +1332,14 @@ class LocalFile extends File {
                        return $this->readOnlyFatalStatus();
                }
 
-               $this->lock(); // begin
-
                $batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
+
+               $this->lock(); // begin
                $batch->addOld( $archiveName );
-               $this->purgeOldThumbnails( $archiveName );
                $status = $batch->execute();
-
                $this->unlock(); // done
 
+               $this->purgeOldThumbnails( $archiveName );
                if ( $status->isOK() ) {
                        $this->purgeDescription();
                        $this->purgeHistory();
@@ -1536,6 +1531,28 @@ class LocalFileDeleteBatch {
                $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName );
        }
 
+       /**
+        * Add the old versions of the image to the batch
+        * @return Array List of archive names from old versions
+        */
+       function addOlds() {
+               $archiveNames = array();
+
+               $dbw = $this->file->repo->getMasterDB();
+               $result = $dbw->select( 'oldimage',
+                       array( 'oi_archive_name' ),
+                       array( 'oi_name' => $this->file->getName() ),
+                       __METHOD__
+               );
+
+               foreach ( $result as $row ) {
+                       $this->addOld( $row->oi_archive_name );
+                       $archiveNames[] = $row->oi_archive_name;
+               }
+
+               return $archiveNames;
+       }
+
        function getOldRels() {
                if ( !isset( $this->srcRels['.'] ) ) {
                        $oldRels =& $this->srcRels;
@@ -1777,17 +1794,6 @@ class LocalFileDeleteBatch {
                        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 );
-               }
-
                // Delete image/oldimage rows
                $this->doDBDeletes();
 
@@ -2215,11 +2221,13 @@ class LocalFileMoveBatch {
 
        /**
         * Add the old versions of the image to the batch
+        * @return Array List of archive names from old versions
         */
        function addOlds() {
                $archiveBase = 'archive';
                $this->olds = array();
                $this->oldCount = 0;
+               $archiveNames = array();
 
                $result = $this->db->select( 'oldimage',
                        array( 'oi_archive_name', 'oi_deleted' ),
@@ -2228,6 +2236,7 @@ class LocalFileMoveBatch {
                );
 
                foreach ( $result as $row ) {
+                       $archiveNames[] = $row->oi_archive_name;
                        $oldName = $row->oi_archive_name;
                        $bits = explode( '!', $oldName, 2 );
 
@@ -2255,6 +2264,8 @@ class LocalFileMoveBatch {
                                "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
                        );
                }
+
+               return $archiveNames;
        }
 
        /**
@@ -2264,8 +2275,8 @@ class LocalFileMoveBatch {
        function execute() {
                $repo = $this->file->repo;
                $status = $repo->newGood();
-               $triplets = $this->getMoveTriplets();
 
+               $triplets = $this->getMoveTriplets();
                $triplets = $this->removeNonexistentFiles( $triplets );
 
                // Copy the files into their new location