Don't initialize MediaWikiServices before extensions have been loaded

Bug: T153256
Bug: T190425
Change-Id: I749f66d13a8c8a8ae4a83661b83c56f0db74a187
This commit is contained in:
Max Semenik 2018-04-30 17:35:08 -07:00
parent 4f4a620f8e
commit 69aecc2eea
3 changed files with 23 additions and 21 deletions

View file

@ -404,24 +404,8 @@ return [
},
'LocalServerObjectCache' => function ( MediaWikiServices $services ) {
$mainConfig = $services->getMainConfig();
if ( function_exists( 'apc_fetch' ) ) {
$id = 'apc';
} elseif ( function_exists( 'apcu_fetch' ) ) {
$id = 'apcu';
} elseif ( function_exists( 'wincache_ucache_get' ) ) {
$id = 'wincache';
} else {
$id = CACHE_NONE;
}
if ( !isset( $mainConfig->get( 'ObjectCaches' )[$id] ) ) {
throw new UnexpectedValueException(
"Cache type \"$id\" is not present in \$wgObjectCaches." );
}
return \ObjectCache::newFromParams( $mainConfig->get( 'ObjectCaches' )[$id] );
$cacheId = \ObjectCache::detectLocalServerCache();
return \ObjectCache::newFromId( $cacheId );
},
'VirtualRESTServiceClient' => function ( MediaWikiServices $services ) {

View file

@ -413,4 +413,21 @@ class ObjectCache {
self::$instances = [];
self::$wanInstances = [];
}
/**
* Detects which local server cache library is present and returns a configuration for it
* @since 1.32
*
* @return int|string Index to cache in $wgObjectCaches
*/
public static function detectLocalServerCache() {
if ( function_exists( 'apc_fetch' ) ) {
return 'apc';
} elseif ( function_exists( 'apcu_fetch' ) ) {
return 'apcu';
} elseif ( function_exists( 'wincache_ucache_get' ) ) {
return 'wincache';
}
return CACHE_NONE;
}
}

View file

@ -1,7 +1,5 @@
<?php
use MediaWiki\MediaWikiServices;
/**
* ExtensionRegistry class
*
@ -142,7 +140,10 @@ class ExtensionRegistry {
// We use a try/catch because we don't want to fail here
// if $wgObjectCaches is not configured properly for APC setup
try {
$cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
// Don't use MediaWikiServices here to prevent instantiating it before extensions have
// been loaded
$cacheId = ObjectCache::detectLocalServerCache();
$cache = ObjectCache::newFromId( $cacheId );
} catch ( MWException $e ) {
$cache = new EmptyBagOStuff();
}