Merge "registration: Improve docs for return value of Processor::getExtractedInfo()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 14 May 2015 07:00:32 +0000 (07:00 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 14 May 2015 07:00:32 +0000 (07:00 +0000)
includes/filebackend/SwiftFileBackend.php
includes/filerepo/LocalRepo.php
includes/filerepo/file/LocalFile.php
includes/libs/objectcache/WANObjectCache.php
includes/specials/SpecialContributions.php
includes/specials/SpecialDeletedContributions.php
tests/phpunit/includes/libs/ArrayUtilsTest.php
tests/phpunit/includes/objectcache/WANObjectCacheTest.php
tests/phpunit/includes/site/CachingSiteStoreTest.php
tests/phpunit/includes/site/SiteImporterTest.php

index 5f406c9..d6d7e9e 100644 (file)
@@ -235,16 +235,16 @@ class SwiftFileBackend extends FileBackendStore {
                        'body' => $params['content']
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 201 ) {
                                // good
                        } elseif ( $rcode === 412 ) {
                                $status->fatal( 'backend-fail-contenttype', $params['dst'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -298,16 +298,16 @@ class SwiftFileBackend extends FileBackendStore {
                        'body' => $handle // resource
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 201 ) {
                                // good
                        } elseif ( $rcode === 412 ) {
                                $status->fatal( 'backend-fail-contenttype', $params['dst'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -347,16 +347,16 @@ class SwiftFileBackend extends FileBackendStore {
                        ) + $this->sanitizeHdrs( $params ), // extra headers merged into object
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 201 ) {
                                // good
                        } elseif ( $rcode === 404 ) {
                                $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -405,9 +405,9 @@ class SwiftFileBackend extends FileBackendStore {
                        );
                }
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $request['method'] === 'PUT' && $rcode === 201 ) {
                                // good
@@ -416,7 +416,7 @@ class SwiftFileBackend extends FileBackendStore {
                        } elseif ( $rcode === 404 ) {
                                $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -446,9 +446,9 @@ class SwiftFileBackend extends FileBackendStore {
                        'headers' => array()
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 204 ) {
                                // good
@@ -457,7 +457,7 @@ class SwiftFileBackend extends FileBackendStore {
                                        $status->fatal( 'backend-fail-delete', $params['src'] );
                                }
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
@@ -505,16 +505,16 @@ class SwiftFileBackend extends FileBackendStore {
                        'headers' => $metaHdrs + $customHdrs
                ) );
 
-               $be = $this;
+               $that = $this;
                $method = __METHOD__;
-               $handler = function ( array $request, Status $status ) use ( $be, $method, $params ) {
+               $handler = function ( array $request, Status $status ) use ( $that, $method, $params ) {
                        list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $request['response'];
                        if ( $rcode === 202 ) {
                                // good
                        } elseif ( $rcode === 404 ) {
                                $status->fatal( 'backend-fail-describe', $params['src'] );
                        } else {
-                               $be->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
+                               $that->onError( $status, $method, $params, $rerr, $rcode, $rdesc );
                        }
                };
 
index 591d684..ef402ea 100644 (file)
@@ -275,14 +275,14 @@ class LocalRepo extends FileRepo {
                        );
                };
 
-               $repo = $this;
+               $that = $this;
                $applyMatchingFiles = function ( ResultWrapper $res, &$searchSet, &$finalFiles )
-                       use ( $repo, $fileMatchesSearch, $flags )
+                       use ( $that, $fileMatchesSearch, $flags )
                {
                        global $wgContLang;
-                       $info = $repo->getInfo();
+                       $info = $that->getInfo();
                        foreach ( $res as $row ) {
-                               $file = $repo->newFileFromRow( $row );
+                               $file = $that->newFileFromRow( $row );
                                // There must have been a search for this DB key, but this has to handle the
                                // cases were title capitalization is different on the client and repo wikis.
                                $dbKeysLook = array( str_replace( ' ', '_', $file->getName() ) );
index e26f739..fe42c2d 100644 (file)
@@ -1603,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 );
                                }
index e1f64ba..8d202c7 100755 (executable)
@@ -76,6 +76,8 @@ class WANObjectCache {
 
        /** Idiom for set()/getWithSetCallback() TTL */
        const TTL_NONE = 0;
+       /** Idiom for getWithSetCallback() callbacks to avoid calling set() */
+       const TTL_UNCACHEABLE = -1;
 
        /** Cache format version number */
        const VERSION = 1;
@@ -307,10 +309,13 @@ class WANObjectCache {
        /**
         * Method to fetch/regenerate cache keys
         *
-        * On cache miss, the key will be set to the callback result.
+        * On cache miss, the key will be set to the callback result,
+        * unless the callback returns false. The arguments supplied are:
+        *     (current value or false, &$ttl)
         * The callback function returns the new value given the current
-        * value (false if not present). If false is returned, then nothing
-        * will be saved to cache.
+        * value (false if not present). Preemptive re-caching and $checkKeys
+        * can result in a non-false current value. The TTL of the new value
+        * can be set dynamically by altering $ttl in the callback (by reference).
         *
         * Usually, callbacks ignore the current value, but it can be used
         * to maintain "most recent X" values that come from time or sequence
@@ -333,7 +338,7 @@ class WANObjectCache {
         * @code
         *     $key = wfMemcKey( 'cat-recent-actions', $catId );
         *     // Function that derives the new key value given the old value
-        *     $callback = function( $cValue ) { ... };
+        *     $callback = function( $cValue, &$ttl ) { ... };
         *     // Get the key value from cache or from source on cache miss;
         *     // try to only let one cluster thread manage doing cache updates
         *     $opts = array( 'lockTSE' => 5, 'lowTTL' => 10 );
@@ -362,7 +367,9 @@ class WANObjectCache {
         *
         * @param string $key Cache key
         * @param callable $callback Value generation function
-        * @param integer $ttl Seconds to live when the key is updated [0=forever]
+        * @param integer $ttl Seconds to live for key updates. Special values are:
+        *   - WANObjectCache::TTL_NONE        : cache forever
+        *   - WANObjectCache::TTL_UNCACHEABLE : do not cache at all
         * @param array $checkKeys List of "check" keys
         * @param array $opts Options map:
         *   - lowTTL  : consider pre-emptive updates when the current TTL (sec)
@@ -426,10 +433,10 @@ class WANObjectCache {
                }
 
                // Generate the new value from the callback...
-               $value = call_user_func( $callback, $cValue );
+               $value = call_user_func_array( $callback, array( $cValue, &$ttl ) );
                // When delete() is called, writes are write-holed by the tombstone,
                // so use a special stash key to pass the new value around threads.
-               if ( $value !== false && ( $isHot || $isTombstone ) ) {
+               if ( $value !== false && ( $isHot || $isTombstone ) && $ttl >= 0 ) {
                        $this->cache->set( self::STASH_KEY_PREFIX . $key, $value, $tempTTL );
                }
 
@@ -437,7 +444,7 @@ class WANObjectCache {
                        $this->cache->unlock( $key );
                }
 
-               if ( $value !== false ) {
+               if ( $value !== false && $ttl >= 0 ) {
                        // Update the cache; this will fail if the key is tombstoned
                        $this->set( $key, $value, $ttl );
                }
index c2cd812..6b918e9 100644 (file)
@@ -724,7 +724,6 @@ class ContribsPager extends ReverseChronologicalPager {
                        $limit,
                        $descending
                );
-               $pager = $this;
 
                /*
                 * This hook will allow extensions to add in additional queries, so they can get their data
@@ -749,7 +748,7 @@ class ContribsPager extends ReverseChronologicalPager {
                ) );
                Hooks::run(
                        'ContribsPager::reallyDoQuery',
-                       array( &$data, $pager, $offset, $limit, $descending )
+                       array( &$data, $this, $offset, $limit, $descending )
                );
 
                $result = array();
index 9e4bbbe..387c174 100644 (file)
@@ -88,15 +88,13 @@ class DeletedContribsPager extends IndexPager {
         * @return ResultWrapper
         */
        function reallyDoQuery( $offset, $limit, $descending ) {
-               $pager = $this;
-
                $data = array( parent::reallyDoQuery( $offset, $limit, $descending ) );
 
                // This hook will allow extensions to add in additional queries, nearly
                // identical to ContribsPager::reallyDoQuery.
                Hooks::run(
                        'DeletedContribsPager::reallyDoQuery',
-                       array( &$data, $pager, $offset, $limit, $descending )
+                       array( &$data, $this, $offset, $limit, $descending )
                );
 
                $result = array();
index b5ea7b7..a91cc95 100644 (file)
@@ -23,11 +23,11 @@ class ArrayUtilsTest extends PHPUnit_Framework_TestCase {
        }
 
        function provideFindLowerBound() {
-               $self = $this;
-               $indexValueCallback = function ( $size ) use ( $self ) {
-                       return function ( $val ) use ( $self, $size ) {
-                               $self->assertTrue( $val >= 0 );
-                               $self->assertTrue( $val < $size );
+               $that = $this;
+               $indexValueCallback = function ( $size ) use ( $that ) {
+                       return function ( $val ) use ( $that, $size ) {
+                               $that->assertTrue( $val >= 0 );
+                               $that->assertTrue( $val < $size );
                                return $val;
                        };
                };
index 3161d18..3e284c8 100644 (file)
@@ -86,13 +86,22 @@ class WANObjectCacheTest extends MediaWikiTestCase {
                $cKey2 = wfRandomString();
 
                $wasSet = 0;
-               $func = function() use ( &$wasSet, $value ) { ++$wasSet; return $value; };
+               $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
+                       ++$wasSet;
+                       $ttl = 20; // override with another value
+                       return $value;
+               };
 
                $wasSet = 0;
                $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
                $this->assertEquals( $v, $value );
                $this->assertEquals( 1, $wasSet, "Value regenerated" );
 
+               $curTTL = null;
+               $v = $cache->get( $key, $curTTL );
+               $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
+               $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
+
                $wasSet = 0;
                $v = $cache->getWithSetCallback( $key, $func, 30, array(), array( 'lockTSE' => 5 ) );
                $this->assertEquals( $v, $value );
index d0a7980..4305ceb 100644 (file)
@@ -96,17 +96,17 @@ class CachingSiteStoreTest extends MediaWikiTestCase {
                        ->getMock();
 
                // php 5.3 compatibility!
-               $self = $this;
+               $that = $this;
 
                $dbSiteStore->expects( $this->any() )
                        ->method( 'getSite' )
-                       ->will( $this->returnValue( $self->getTestSite() ) );
+                       ->will( $this->returnValue( $that->getTestSite() ) );
 
                $dbSiteStore->expects( $this->any() )
                        ->method( 'getSites' )
-                       ->will( $this->returnCallback( function() use( $self ) {
+                       ->will( $this->returnCallback( function() use ( $that ) {
                                $siteList = new SiteList();
-                               $siteList->setSite( $self->getTestSite() );
+                               $siteList->setSite( $that->getTestSite() );
 
                                return $siteList;
                        } ) );
index cb0316a..64b195d 100644 (file)
@@ -34,11 +34,11 @@ class SiteImporterTest extends PHPUnit_Framework_TestCase {
        private function newSiteImporter( array $expectedSites, $errorCount ) {
                $store = $this->getMock( 'SiteStore' );
 
-               $self = $this;
+               $that = $this;
                $store->expects( $this->once() )
                        ->method( 'saveSites' )
-                       ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites, $self ) {
-                               $self->assertSitesEqual( $expectedSites, $sites );
+                       ->will( $this->returnCallback( function ( $sites ) use ( $expectedSites, $that ) {
+                               $that->assertSitesEqual( $expectedSites, $sites );
                        } ) );
 
                $store->expects( $this->any() )