Consistently name the $this callback variable "$that"
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFile.php
index 3056ad8..fe42c2d 100644 (file)
@@ -124,14 +124,9 @@ class LocalFile extends File {
        /** @var bool True if file is not present in file system. Not to be cached in memcached */
        private $missing;
 
-       /** @var int UNIX timestamp of last markVolatile() call */
-       private $lastMarkedVolatile = 0;
-
        // @note: higher than IDBAccessObject constants
        const LOAD_ALL = 16; // integer; load all the lazy fields too (like metadata)
 
-       const VOLATILE_TTL = 300; // integer; seconds
-
        /**
         * Create a LocalFile from a title
         * Do not call this except from inside a repo class.
@@ -248,21 +243,19 @@ class LocalFile extends File {
         * @return bool
         */
        function loadFromCache() {
-               global $wgMemc;
-
                $this->dataLoaded = false;
                $this->extraDataLoaded = false;
                $key = $this->getCacheKey();
 
                if ( !$key ) {
-
                        return false;
                }
 
-               $cachedValues = $wgMemc->get( $key );
+               $cache = ObjectCache::getMainWANInstance();
+               $cachedValues = $cache->get( $key );
 
                // Check if the key existed and belongs to this version of MediaWiki
-               if ( isset( $cachedValues['version'] ) && $cachedValues['version'] == MW_FILE_VERSION ) {
+               if ( is_array( $cachedValues ) && $cachedValues['version'] == MW_FILE_VERSION ) {
                        wfDebug( "Pulling file metadata from cache key $key\n" );
                        $this->fileExists = $cachedValues['fileExists'];
                        if ( $this->fileExists ) {
@@ -288,22 +281,20 @@ class LocalFile extends File {
         * Save the file metadata to memcached
         */
        function saveToCache() {
-               global $wgMemc;
-
                $this->load();
-               $key = $this->getCacheKey();
 
+               $key = $this->getCacheKey();
                if ( !$key ) {
                        return;
                }
 
                $fields = $this->getCacheFields( '' );
-               $cache = array( 'version' => MW_FILE_VERSION );
-               $cache['fileExists'] = $this->fileExists;
+               $cacheVal = array( 'version' => MW_FILE_VERSION );
+               $cacheVal['fileExists'] = $this->fileExists;
 
                if ( $this->fileExists ) {
                        foreach ( $fields as $field ) {
-                               $cache[$field] = $this->$field;
+                               $cacheVal[$field] = $this->$field;
                        }
                }
 
@@ -311,13 +302,29 @@ class LocalFile extends File {
                // If the cache value gets to large it will not fit in memcached and nothing will
                // get cached at all, causing master queries for any file access.
                foreach ( $this->getLazyCacheFields( '' ) as $field ) {
-                       if ( isset( $cache[$field] ) && strlen( $cache[$field] ) > 100 * 1024 ) {
-                               unset( $cache[$field] ); // don't let the value get too big
+                       if ( isset( $cacheVal[$field] ) && strlen( $cacheVal[$field] ) > 100 * 1024 ) {
+                               unset( $cacheVal[$field] ); // don't let the value get too big
                        }
                }
 
                // Cache presence for 1 week and negatives for 1 day
-               $wgMemc->set( $key, $cache, $this->fileExists ? 86400 * 7 : 86400 );
+               $cache = ObjectCache::getMainWANInstance();
+               $cache->set( $key, $cacheVal, $this->fileExists ? 86400 * 7 : 86400 );
+       }
+
+       /**
+        * Purge the file object/metadata cache
+        */
+       function invalidateCache() {
+               $this->load();
+
+               $key = $this->getCacheKey();
+               if ( !$key ) {
+                       return;
+               }
+
+               $cache = ObjectCache::getMainWANInstance();
+               $cache->delete( $key );
        }
 
        /**
@@ -531,10 +538,6 @@ class LocalFile extends File {
        function load( $flags = 0 ) {
                if ( !$this->dataLoaded ) {
                        if ( ( $flags & self::READ_LATEST ) || !$this->loadFromCache() ) {
-                               // b/c for now for data consistency
-                               if ( $this->isVolatile() ) {
-                                       $flags |= self::READ_LATEST;
-                               }
                                $this->loadFromDB( $flags );
                                $this->saveToCache();
                        }
@@ -621,7 +624,7 @@ class LocalFile extends File {
                        __METHOD__
                );
 
-               $this->saveToCache();
+               $this->invalidateCache();
 
                $this->unlock(); // done
 
@@ -743,7 +746,7 @@ class LocalFile extends File {
                if ( $type == 'text' ) {
                        return $this->user_text;
                } elseif ( $type == 'id' ) {
-                       return $this->user;
+                       return (int)$this->user;
                }
        }
 
@@ -762,7 +765,7 @@ class LocalFile extends File {
        function getBitDepth() {
                $this->load();
 
-               return $this->bits;
+               return (int)$this->bits;
        }
 
        /**
@@ -851,25 +854,7 @@ class LocalFile extends File {
         * Refresh metadata in memcached, but don't touch thumbnails or squid
         */
        function purgeMetadataCache() {
-               $this->loadFromDB();
-               $this->saveToCache();
-               $this->purgeHistory();
-       }
-
-       /**
-        * Purge the shared history (OldLocalFile) cache.
-        *
-        * @note This used to purge old thumbnails as well.
-        */
-       function purgeHistory() {
-               global $wgMemc;
-
-               $hashedName = md5( $this->getName() );
-               $oldKey = $this->repo->getSharedCacheKey( 'oldfile', $hashedName );
-
-               if ( $oldKey ) {
-                       $wgMemc->delete( $oldKey );
-               }
+               $this->invalidateCache();
        }
 
        /**
@@ -1415,11 +1400,8 @@ class LocalFile extends File {
                #       to after $wikiPage->doEdit has been called.
                $dbw->commit( __METHOD__ );
 
-               # Save to memcache.
-               # 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();
+               # Update memcache after the commit
+               $this->invalidateCache();
 
                if ( $exists ) {
                        # Invalidate the cache for the description page
@@ -1621,21 +1603,21 @@ class LocalFile extends File {
 
                // Hack: the lock()/unlock() pair is nested in a transaction so the locking is not
                // tied to BEGIN/COMMIT. To avoid slow purges in the transaction, move them outside.
-               $file = $this;
+               $that = $this;
                $this->getRepo()->getMasterDB()->onTransactionIdle(
-                       function () use ( $file, $archiveNames ) {
+                       function () use ( $that, $archiveNames ) {
                                global $wgUseSquid;
 
-                               $file->purgeEverything();
+                               $that->purgeEverything();
                                foreach ( $archiveNames as $archiveName ) {
-                                       $file->purgeOldThumbnails( $archiveName );
+                                       $that->purgeOldThumbnails( $archiveName );
                                }
 
                                if ( $wgUseSquid ) {
                                        // Purge the squid
                                        $purgeUrls = array();
                                        foreach ( $archiveNames as $archiveName ) {
-                                               $purgeUrls[] = $file->getArchiveUrl( $archiveName );
+                                               $purgeUrls[] = $that->getArchiveUrl( $archiveName );
                                        }
                                        SquidUpdate::purge( $purgeUrls );
                                }
@@ -1676,7 +1658,6 @@ class LocalFile extends File {
                $this->purgeOldThumbnails( $archiveName );
                if ( $status->isOK() ) {
                        $this->purgeDescription();
-                       $this->purgeHistory();
                }
 
                if ( $wgUseSquid ) {
@@ -1793,7 +1774,10 @@ class LocalFile extends File {
                // itself gets it from elsewhere. To avoid repeating the DB lookups in such a case, we
                // need to differentiate between null (uninitialized) and false (failed to load).
                if ( $this->descriptionTouched === null ) {
-                       $cond = array( 'page_namespace' => $this->title->getNamespace(), 'page_title' => $this->title->getDBkey() );
+                       $cond = array(
+                               'page_namespace' => $this->title->getNamespace(),
+                               'page_title' => $this->title->getDBkey()
+                       );
                        $touched = $this->repo->getSlaveDB()->selectField( 'page', 'page_touched', $cond, __METHOD__ );
                        $this->descriptionTouched = $touched ? wfTimestamp( TS_MW, $touched ) : false;
                }
@@ -1817,7 +1801,7 @@ class LocalFile extends File {
                                        array( 'img_sha1' => $this->sha1 ),
                                        array( 'img_name' => $this->getName() ),
                                        __METHOD__ );
-                               $this->saveToCache();
+                               $this->invalidateCache();
                        }
 
                        $this->unlock(); // done
@@ -1866,8 +1850,6 @@ class LocalFile extends File {
                        } );
                }
 
-               $this->markVolatile(); // file may change soon
-
                return $this->lockedOwnTrx;
        }
 
@@ -1886,48 +1868,6 @@ class LocalFile extends File {
                }
        }
 
-       /**
-        * Mark a file as about to be changed
-        *
-        * This sets a cache key that alters master/slave DB loading behavior
-        *
-        * @return bool Success
-        */
-       protected function markVolatile() {
-               global $wgMemc;
-
-               $key = $this->repo->getSharedCacheKey( 'file-volatile', md5( $this->getName() ) );
-               if ( $key ) {
-                       $this->lastMarkedVolatile = time();
-                       return $wgMemc->set( $key, $this->lastMarkedVolatile, self::VOLATILE_TTL );
-               }
-
-               return true;
-       }
-
-       /**
-        * Check if a file is about to be changed or has been changed recently
-        *
-        * @see LocalFile::isVolatile()
-        * @return bool Whether the file is volatile
-        */
-       protected function isVolatile() {
-               global $wgMemc;
-
-               $key = $this->repo->getSharedCacheKey( 'file-volatile', md5( $this->getName() ) );
-               if ( !$key ) {
-                       // repo unavailable; bail.
-                       return false;
-               }
-
-               if ( $this->lastMarkedVolatile === 0 ) {
-                       $this->lastMarkedVolatile = $wgMemc->get( $key ) ?: 0;
-               }
-
-               $volatileDuration = time() - $this->lastMarkedVolatile;
-               return $volatileDuration <= self::VOLATILE_TTL;
-       }
-
        /**
         * Roll back the DB transaction and mark the image unlocked
         */
@@ -2615,7 +2555,6 @@ class LocalFileRestoreBatch {
                        } else {
                                wfDebug( __METHOD__ . " restored {$status->successCount} as archived versions\n" );
                                $this->file->purgeDescription();
-                               $this->file->purgeHistory();
                        }
                }