Don't initialize MediaWikiServices before extensions have been loaded
authorMax Semenik <maxsem.wiki@gmail.com>
Tue, 1 May 2018 00:35:08 +0000 (17:35 -0700)
committerMax Semenik <maxsem.wiki@gmail.com>
Fri, 4 May 2018 06:36:10 +0000 (23:36 -0700)
Bug: T153256
Bug: T190425
Change-Id: I749f66d13a8c8a8ae4a83661b83c56f0db74a187

includes/ServiceWiring.php
includes/objectcache/ObjectCache.php
includes/registration/ExtensionRegistry.php

index f97d60d..ee92cbf 100644 (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 ) {
index a6f55e6..c384032 100644 (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;
+       }
 }
index b000dc1..b34a123 100644 (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();
                }