User objectCacheFactory methods not deprecated ObjectCache methods

Bug: T363770
Change-Id: I2335b315bec6a540409492df4891c518640966d5
This commit is contained in:
Wandji69 2024-06-05 19:44:20 +01:00
parent 8af8ebf0a1
commit 1665ea876f
14 changed files with 85 additions and 49 deletions

View file

@ -49,7 +49,6 @@ use MediaWiki\WikiMap\WikiMap;
use MWExceptionHandler;
use MWExceptionRenderer;
use Net_URL2;
use ObjectCache;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
@ -521,7 +520,8 @@ class ResourceLoader implements LoggerAwareInterface {
DeferredUpdates::addCallableUpdate( function () {
$updatesByEntity = $this->depStoreUpdateBuffer;
$this->depStoreUpdateBuffer = [];
$cache = ObjectCache::getLocalClusterInstance();
$cache = MediaWikiServices::getInstance()
->getObjectCacheFactory()->getLocalClusterInstance();
$scopeLocks = [];
$depsByEntity = [];

View file

@ -493,12 +493,13 @@ return [
$mainConfig->get( MainConfigNames::MainCacheType )
);
$objectCacheFactory = $services->getObjectCacheFactory();
if ( is_string( $cpStashType ) ) {
$cpStash = $services->getObjectCacheFactory()->getInstance( $cpStashType );
$cpStash = $objectCacheFactory->getInstance( $cpStashType );
} elseif ( $isMainCacheBad ) {
$cpStash = new EmptyBagOStuff();
} else {
$cpStash = ObjectCache::getLocalClusterInstance();
$cpStash = $objectCacheFactory->getLocalClusterInstance();
}
$chronologyProtector = new ChronologyProtector(
@ -655,7 +656,7 @@ return [
// Setup salt cache. Use APC, or fallback to the main cache if it isn't setup
$cache = $services->getLocalServerObjectCache();
if ( $cache instanceof EmptyBagOStuff ) {
$cache = ObjectCache::getLocalClusterInstance();
$cache = $services->getObjectCacheFactory()->getLocalClusterInstance();
}
return new CryptHKDF( $secret, $config->get( MainConfigNames::HKDFAlgorithm ), $cache, $context );
@ -1417,7 +1418,7 @@ return [
'PageEditStash' => static function ( MediaWikiServices $services ): PageEditStash {
return new PageEditStash(
ObjectCache::getLocalClusterInstance(),
$services->getObjectCacheFactory()->getLocalClusterInstance(),
$services->getConnectionProvider(),
LoggerFactory::getInstance( 'StashEdit' ),
$services->getStatsdDataFactory(),
@ -1745,7 +1746,7 @@ return [
return new Pingback(
$services->getMainConfig(),
$services->getConnectionProvider(),
ObjectCache::getLocalClusterInstance(),
$services->getObjectCacheFactory()->getLocalClusterInstance(),
$services->getHttpRequestFactory(),
LoggerFactory::getInstance( 'Pingback' )
);
@ -2082,7 +2083,7 @@ return [
$cache = $services->getLocalServerObjectCache();
if ( $cache instanceof EmptyBagOStuff ) {
$cache = ObjectCache::getLocalClusterInstance();
$cache = $services->getObjectCacheFactory()->getLocalClusterInstance();
}
return new CachingSiteStore( $rawSiteStore, $cache );
@ -2266,14 +2267,14 @@ return [
$services->getMainConfig()->get( MainConfigNames::TempAccountCreationThrottle ),
[
'type' => 'tempacctcreate',
'cache' => ObjectCache::getLocalClusterInstance(),
'cache' => $services->getObjectCacheFactory()->getLocalClusterInstance(),
]
),
new Throttler(
$services->getMainConfig()->get( MainConfigNames::TempAccountNameAcquisitionThrottle ),
[
'type' => 'tempacctnameacquisition',
'cache' => ObjectCache::getLocalClusterInstance(),
'cache' => $services->getObjectCacheFactory()->getLocalClusterInstance(),
]
)
);

View file

@ -35,6 +35,7 @@ use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\Languages\LanguageConverterFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Permissions\Authority;
use MediaWiki\Permissions\PermissionStatus;
@ -258,6 +259,7 @@ class AuthManager implements LoggerAwareInterface {
* @param UserFactory $userFactory
* @param UserIdentityLookup $userIdentityLookup
* @param UserOptionsManager $userOptionsManager
*
*/
public function __construct(
WebRequest $request,
@ -1406,7 +1408,7 @@ class AuthManager implements LoggerAwareInterface {
}
// Avoid account creation races on double submissions
$cache = \ObjectCache::getLocalClusterInstance();
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance();
$lock = $cache->getScopedLock( $cache->makeGlobalKey( 'account', md5( $user->getName() ) ) );
if ( !$lock ) {
// Don't clear AuthManager::accountCreationState for this code
@ -1907,7 +1909,7 @@ class AuthManager implements LoggerAwareInterface {
}
// Avoid account creation races on double submissions
$cache = \ObjectCache::getLocalClusterInstance();
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance();
$lock = $cache->getScopedLock( $cache->makeGlobalKey( 'account', md5( $username ) ) );
if ( !$lock ) {
$this->logger->debug( __METHOD__ . ': Could not acquire account creation lock', [

View file

@ -23,6 +23,7 @@ namespace MediaWiki\Auth;
use BagOStuff;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\User;
/**
@ -59,7 +60,9 @@ class ThrottlePreAuthenticationProvider extends AbstractPreAuthenticationProvide
public function __construct( $params = [] ) {
$this->throttleSettings = array_intersect_key( $params,
[ 'accountCreationThrottle' => true, 'passwordAttemptThrottle' => true ] );
$this->cache = $params['cache'] ?? \ObjectCache::getLocalClusterInstance();
$services = MediaWikiServices::getInstance();
$this->cache = $params['cache'] ?? $services->getObjectCacheFactory()
->getLocalClusterInstance();
}
protected function postInitSetup() {

View file

@ -70,18 +70,21 @@ class Throttler implements LoggerAwareInterface {
. implode( ', ', array_keys( $invalidParams ) ) );
}
$services = MediaWikiServices::getInstance();
$objectCacheFactory = $services->getObjectCacheFactory();
if ( $conditions === null ) {
$config = MediaWikiServices::getInstance()->getMainConfig();
$config = $services->getMainConfig();
$conditions = $config->get( MainConfigNames::PasswordAttemptThrottle );
$params += [
'type' => 'password',
'cache' => \ObjectCache::getLocalClusterInstance(),
'cache' => $objectCacheFactory->getLocalClusterInstance(),
'warningLimit' => 50,
];
} else {
$params += [
'type' => 'custom',
'cache' => \ObjectCache::getLocalClusterInstance(),
'cache' => $objectCacheFactory->getLocalClusterInstance(),
'warningLimit' => INF,
];
}

View file

@ -28,7 +28,6 @@ use MediaWiki\Config\ServiceOptions;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Request\WebRequest;
use ObjectCache;
use Wikimedia\AtEase\AtEase;
use Wikimedia\IPUtils;
@ -262,7 +261,8 @@ abstract class FileCacheBase {
: IPUtils::sanitizeRange( "$ip/16" );
# Bail out if a request already came from this range...
$cache = ObjectCache::getLocalClusterInstance();
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()
->getLocalClusterInstance();
$key = $cache->makeKey( static::class, 'attempt', $this->mType, $this->mKey, $ip );
if ( !$cache->add( $key, 1, self::MISS_TTL_SEC ) ) {
return; // possibly the same user
@ -278,7 +278,8 @@ abstract class FileCacheBase {
* @return int
*/
public function getMissesRecent() {
$cache = ObjectCache::getLocalClusterInstance();
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()
->getLocalClusterInstance();
return self::MISS_FACTOR * $cache->get( $this->cacheMissKey( $cache ) );
}

View file

@ -48,7 +48,6 @@ use MediaWiki\Status\Status;
use MediaWiki\Title\Title;
use Message;
use MessageSpecifier;
use ObjectCache;
use RepoGroup;
use UnregisteredLocalFile;
use Wikimedia\AtEase\AtEase;
@ -366,7 +365,9 @@ class ThumbnailEntryPoint extends MediaWikiEntryPoint {
protected function generateThumbnail( File $file, array $params, $thumbName, $thumbPath ) {
$attemptFailureEpoch = $this->getConfig( MainConfigNames::AttemptFailureEpoch );
$cache = ObjectCache::getLocalClusterInstance();
$services = MediaWikiServices::getInstance()->getObjectCacheFactory();
$cache = $services->getLocalClusterInstance();
$key = $cache->makeKey(
'attempt-failures',
$attemptFailureEpoch,

View file

@ -20,6 +20,7 @@
use MediaWiki\Deferred\DeferredUpdates;
use MediaWiki\Deferred\JobQueueEnqueueUpdate;
use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\ReadOnlyMode;
use Wikimedia\UUID\GlobalIdGenerator;
@ -75,6 +76,7 @@ class JobQueueGroup {
* @param IBufferingStatsdDataFactory $statsdDataFactory
* @param WANObjectCache $wanCache
* @param GlobalIdGenerator $globalIdGenerator
*
*/
public function __construct(
$domain,
@ -162,7 +164,7 @@ class JobQueueGroup {
}
}
$cache = ObjectCache::getLocalClusterInstance();
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance();
$cache->set(
$cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_ANY ),
'true',
@ -333,7 +335,7 @@ class JobQueueGroup {
* @return bool
*/
public function queuesHaveJobs( $type = self::TYPE_ANY ) {
$cache = ObjectCache::getLocalClusterInstance();
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance();
$key = $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', $type );
$value = $cache->get( $key );

View file

@ -3024,7 +3024,8 @@ class WikiPage implements Page, PageRecord {
if ( $this->getLinksTimestamp() > $this->getTouched() ) {
// If a page is uncacheable, do not keep spamming a job for it.
// Although it would be de-duplicated, it would still waste I/O.
$cache = ObjectCache::getLocalClusterInstance();
$services = MediaWikiServices::getInstance()->getObjectCacheFactory();
$cache = $services->getLocalClusterInstance();
$key = $cache->makeKey( 'dynamic-linksupdate', 'last', $this->getId() );
$ttl = max( $parserOutput->getCacheExpiry(), 3600 );
if ( $cache->add( $key, time(), $ttl ) ) {

View file

@ -37,7 +37,6 @@ use MediaWiki\Session\BotPasswordSessionProvider;
use MediaWiki\Session\SessionManager;
use MediaWiki\Status\Status;
use MWRestrictions;
use ObjectCache;
use stdClass;
use UnexpectedValueException;
use Wikimedia\Rdbms\IDatabase;
@ -407,7 +406,8 @@ class BotPassword {
if ( $passwordAttemptThrottle ) {
$throttle = new Throttler( $passwordAttemptThrottle, [
'type' => 'botpassword',
'cache' => ObjectCache::getLocalClusterInstance(),
'cache' => MediaWikiServices::getInstance()->getObjectCacheFactory()
->getLocalClusterInstance(),
] );
$result = $throttle->increase( $user->getName(), $request->getIP(), __METHOD__ );
if ( $result ) {

View file

@ -24,6 +24,7 @@
use MediaWiki\Auth\Throttler;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use Wikimedia\IPUtils;
require_once __DIR__ . '/Maintenance.php';
@ -113,9 +114,11 @@ class ResetAuthenticationThrottle extends Maintenance {
return;
}
$objectCacheFactory = $this->getServiceContainer()->getInstance()->getObjectCacheFactory();
$throttler = new Throttler( $passwordAttemptThrottle, [
'type' => 'password',
'cache' => ObjectCache::getLocalClusterInstance(),
'cache' => $objectCacheFactory->getLocalClusterInstance(),
] );
if ( $rawUsername !== null ) {
$usernames = $this->getServiceContainer()->getAuthManager()
@ -132,7 +135,7 @@ class ResetAuthenticationThrottle extends Maintenance {
$botPasswordThrottler = new Throttler( $passwordAttemptThrottle, [
'type' => 'botpassword',
'cache' => ObjectCache::getLocalClusterInstance(),
'cache' => $objectCacheFactory->getLocalClusterInstance(),
] );
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable T240141
$botPasswordThrottler->clear( $username, $ip );
@ -159,7 +162,8 @@ class ResetAuthenticationThrottle extends Maintenance {
}
$throttler = new Throttler( $accountCreationThrottle, [
'type' => 'acctcreate',
'cache' => ObjectCache::getLocalClusterInstance(),
'cache' => MediaWikiServices::getInstance()->getObjectCacheFactory()
->getLocalClusterInstance(),
] );
$throttler->clear( null, $ip );
@ -177,7 +181,8 @@ class ResetAuthenticationThrottle extends Maintenance {
}
$throttler = new Throttler( $tempAccountCreationThrottle, [
'type' => 'tempacctcreate',
'cache' => ObjectCache::getLocalClusterInstance(),
'cache' => MediaWikiServices::getInstance()->getObjectCacheFactory()
->getLocalClusterInstance(),
] );
$throttler->clear( null, $ip );
@ -197,7 +202,8 @@ class ResetAuthenticationThrottle extends Maintenance {
}
$throttler = new Throttler( $tempAccountNameAcquisitionThrottle, [
'type' => 'tempacctnameacquisition',
'cache' => ObjectCache::getLocalClusterInstance(),
'cache' => MediaWikiServices::getInstance()->getObjectCacheFactory()
->getLocalClusterInstance(),
] );
$throttler->clear( null, $ip );

View file

@ -61,7 +61,7 @@ use MediaWiki\Watchlist\WatchlistManager;
use MediaWikiIntegrationTestCase;
use Message;
use MessageSpecifier;
use ObjectCache;
use ObjectCacheFactory;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
@ -141,6 +141,9 @@ class AuthManagerTest extends MediaWikiIntegrationTestCase {
/** @var UserOptionsManager */
private $userOptionsManager;
/** @var ObjectCacheFactory */
private $objectCacheFactory;
/**
* Sets a mock on a hook
* @param string $hook
@ -272,7 +275,7 @@ class AuthManagerTest extends MediaWikiIntegrationTestCase {
$this->getServiceContainer()->getDatabaseBlockStore(),
$this->getServiceContainer()->getProxyLookup()
) extends BlockManager
{
{
protected function checkHost( $hostname ) {
return '127.0.0.1';
}
@ -312,6 +315,9 @@ class AuthManagerTest extends MediaWikiIntegrationTestCase {
if ( $regen || !$this->userOptionsManager ) {
$this->userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
}
if ( $regen || !$this->objectCacheFactory ) {
$this->objectCacheFactory = $this->getServiceContainer()->getObjectCacheFactory();
}
if ( !$this->logger ) {
$this->logger = new TestLogger();
}
@ -334,7 +340,8 @@ class AuthManagerTest extends MediaWikiIntegrationTestCase {
$this->botPasswordStore,
$this->userFactory,
$this->userIdentityLookup,
$this->userOptionsManager
$this->userOptionsManager,
$this->objectCacheFactory
);
$this->manager->setLogger( $this->logger );
$this->managerPriv = TestingAccessWrapper::newFromObject( $this->manager );
@ -2052,7 +2059,7 @@ class AuthManagerTest extends MediaWikiIntegrationTestCase {
$this->request->getSession()->setSecret( AuthManager::ACCOUNT_CREATION_STATE, $session );
$this->hook( 'LocalUserCreated', LocalUserCreatedHook::class, $this->never() );
$cache = ObjectCache::getLocalClusterInstance();
$cache = $this->objectCacheFactory->getLocalClusterInstance();
$lock = $cache->getScopedLock( $cache->makeGlobalKey( 'account', md5( $username ) ) );
$ret = $this->manager->continueAccountCreation( [] );
unset( $lock );
@ -2882,7 +2889,7 @@ class AuthManagerTest extends MediaWikiIntegrationTestCase {
$session->clear();
$user = User::newFromName( $username );
$this->hook( 'LocalUserCreated', LocalUserCreatedHook::class, $this->never() );
$cache = ObjectCache::getLocalClusterInstance();
$cache = $this->objectCacheFactory->getLocalClusterInstance();
$lock = $cache->getScopedLock( $cache->makeGlobalKey( 'account', md5( $username ) ) );
$ret = $this->manager->autoCreateUser( $user, AuthManager::AUTOCREATE_SOURCE_SESSION, true, true );
unset( $lock );
@ -2949,7 +2956,7 @@ class AuthManagerTest extends MediaWikiIntegrationTestCase {
);
// Test backoff
$cache = ObjectCache::getLocalClusterInstance();
$cache = $this->objectCacheFactory->getLocalClusterInstance();
$backoffKey = $cache->makeKey( 'AuthManager', 'autocreate-failed', md5( $username ) );
$cache->set( $backoffKey, true );
$session->clear();
@ -2988,7 +2995,7 @@ class AuthManagerTest extends MediaWikiIntegrationTestCase {
$this->assertSame( null, $session->get( AuthManager::AUTOCREATE_BLOCKLIST ) );
// Test addToDatabase throws an exception
$cache = ObjectCache::getLocalClusterInstance();
$cache = $this->objectCacheFactory->getLocalClusterInstance();
$backoffKey = $cache->makeKey( 'AuthManager', 'autocreate-failed', md5( $username ) );
$this->assertFalse( $cache->get( $backoffKey ) );
$session->clear();

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Tests\Site;
use MediaWiki\MediaWikiServices;
use MediaWiki\Site\CachingSiteStore;
use MediaWiki\Site\HashSiteStore;
use MediaWiki\Site\MediaWikiSite;
@ -9,7 +10,6 @@ use MediaWiki\Site\Site;
use MediaWiki\Site\SiteList;
use MediaWiki\Site\SiteStore;
use MediaWikiIntegrationTestCase;
use ObjectCache;
/**
* @covers \MediaWiki\Site\CachingSiteStore
@ -21,10 +21,12 @@ class CachingSiteStoreTest extends MediaWikiIntegrationTestCase {
public function testGetSites() {
$testSites = TestSites::getSites();
$services = MediaWikiServices::getInstance();
$store = new CachingSiteStore(
$this->getHashSiteStore( $testSites ),
ObjectCache::getLocalClusterInstance()
$services->getObjectCacheFActory()
->getLocalClusterInstance()
);
$sites = $store->getSites();
@ -44,8 +46,10 @@ class CachingSiteStoreTest extends MediaWikiIntegrationTestCase {
}
public function testSaveSites() {
$services = MediaWikiServices::getInstance();
$store = new CachingSiteStore(
new HashSiteStore(), ObjectCache::getLocalClusterInstance()
new HashSiteStore(),
$services->getObjectCacheFActory()->getLocalClusterInstance()
);
$sites = [];
@ -77,6 +81,8 @@ class CachingSiteStoreTest extends MediaWikiIntegrationTestCase {
$dbSiteStore->method( 'getSite' )
->willReturn( $this->getTestSite() );
$services = MediaWikiServices::getInstance()->getObjectCacheFactory();
$dbSiteStore->method( 'getSites' )
->willReturnCallback( function () {
$siteList = new SiteList();
@ -85,7 +91,7 @@ class CachingSiteStoreTest extends MediaWikiIntegrationTestCase {
return $siteList;
} );
$store = new CachingSiteStore( $dbSiteStore, ObjectCache::getLocalClusterInstance() );
$store = new CachingSiteStore( $dbSiteStore, $services->getLocalClusterInstance() );
// initialize internal cache
$this->assertGreaterThan( 0, $store->getSites()->count(), 'count sites' );
@ -111,8 +117,9 @@ class CachingSiteStoreTest extends MediaWikiIntegrationTestCase {
}
public function testClear() {
$services = MediaWikiServices::getInstance()->getObjectCacheFactory();
$store = new CachingSiteStore(
new HashSiteStore(), ObjectCache::getLocalClusterInstance()
new HashSiteStore(), $services->getLocalClusterInstance()
);
$this->assertTrue( $store->clear() );

View file

@ -85,7 +85,7 @@ class MediaWikiIntegrationTestCaseTest extends MediaWikiIntegrationTestCase {
public function testObjectCache() {
$this->assertSame( 'hash', $this->getServiceContainer()->getMainConfig()->get( MainConfigNames::MainCacheType ) );
$this->assertInstanceOf( HashBagOStuff::class, ObjectCache::getLocalClusterInstance() );
$this->assertInstanceOf( HashBagOStuff::class, $this->getServiceContainer()->getObjectCacheFactory()->getLocalClusterInstance() );
$this->assertInstanceOf( HashBagOStuff::class, $this->getServiceContainer()->getObjectCacheFactory()->getLocalServerInstance() );
$this->assertInstanceOf( HashBagOStuff::class, $this->getServiceContainer()->getObjectCacheFactory()->getInstance( CACHE_ANYTHING ) );
$this->assertInstanceOf( HashBagOStuff::class, $this->getServiceContainer()->getObjectCacheFactory()->getInstance( CACHE_ACCEL ) );
@ -213,27 +213,29 @@ class MediaWikiIntegrationTestCaseTest extends MediaWikiIntegrationTestCase {
public function testSetMainCache() {
// Cache should be set to a hash by default.
$this->assertInstanceOf( HashBagOStuff::class, ObjectCache::getLocalClusterInstance() );
$this->assertInstanceOf( HashBagOStuff::class, $this->getServiceContainer()
->getObjectCacheFactory()->getLocalClusterInstance() );
// Use HashBagOStuff.
$this->setMainCache( CACHE_HASH );
$cache = ObjectCache::getLocalClusterInstance();
$cache = $this->getServiceContainer()->getObjectCacheFactory()->getLocalClusterInstance();
$this->assertInstanceOf( HashBagOStuff::class, $cache );
// Install different HashBagOStuff
$cache = new HashBagOStuff();
$name = $this->setMainCache( $cache );
$this->assertSame( $cache, ObjectCache::getLocalClusterInstance() );
$this->assertSame( $cache, $this->getServiceContainer()->getObjectCacheFactory()->getLocalClusterInstance() );
$this->assertSame( $cache, $this->getServiceContainer()->getObjectCacheFactory()->getInstance( $name ) );
// Our custom cache object should not replace an existing entry.
$this->assertNotSame( $cache, $this->getServiceContainer()->getObjectCacheFactory()->getInstance( CACHE_HASH ) );
$this->setMainCache( CACHE_HASH );
$this->assertNotSame( $cache, ObjectCache::getLocalClusterInstance() );
$this->assertNotSame( $cache, $this->getServiceContainer()->getObjectCacheFactory()->getLocalClusterInstance() );
// We should be able to disable the cache.
$this->assertSame( CACHE_NONE, $this->setMainCache( CACHE_NONE ) );
$this->assertInstanceOf( EmptyBagOStuff::class, ObjectCache::getLocalClusterInstance() );
$this->assertInstanceOf( EmptyBagOStuff::class, $this->getServiceContainer()
->getObjectCacheFactory()->getLocalClusterInstance() );
}
public function testOverrideMwServices() {