From 5a3d42dbd86939a80d96f5033afdffbd520d729b Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 21 Sep 2016 09:38:34 -0700 Subject: [PATCH] Use standard exceptions in ObjectCache and always handle CACHE_NONE/"hash" * If cache CACHE_NONE or "hash" is not set yet or was unset, return the proper new object anyway for sanity. * Also fixed $params typo in newFromParams(). This should not have any effect since SqlBagOStuff ignores the key in this case. * Removed excess "use" statement. Change-Id: I36108a865273b9c209145828e5e1216b545ef431 --- includes/objectcache/ObjectCache.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/includes/objectcache/ObjectCache.php b/includes/objectcache/ObjectCache.php index ea237aa735..14809a87e2 100644 --- a/includes/objectcache/ObjectCache.php +++ b/includes/objectcache/ObjectCache.php @@ -23,7 +23,6 @@ use MediaWiki\Logger\LoggerFactory; use MediaWiki\MediaWikiServices; -use MediaWiki\Services\ServiceDisabledException; /** * Functions to get cache objects @@ -118,13 +117,20 @@ class ObjectCache { * * @param string $id A key in $wgObjectCaches. * @return BagOStuff - * @throws MWException + * @throws InvalidArgumentException */ public static function newFromId( $id ) { global $wgObjectCaches; if ( !isset( $wgObjectCaches[$id] ) ) { - throw new MWException( "Invalid object cache type \"$id\" requested. " . + // Always recognize these ones + if ( $id === CACHE_NONE ) { + return new EmptyBagOStuff(); + } elseif ( $id === 'hash' ) { + return new HashBagOStuff(); + } + + throw new InvalidArgumentException( "Invalid object cache type \"$id\" requested. " . "It is not present in \$wgObjectCaches." ); } @@ -160,7 +166,7 @@ class ObjectCache { * - loggroup: Alias to set 'logger' key with LoggerFactory group. * - .. Other parameters passed to factory or class. * @return BagOStuff - * @throws MWException + * @throws InvalidArgumentException */ public static function newFromParams( $params ) { if ( isset( $params['loggroup'] ) ) { @@ -187,7 +193,7 @@ class ObjectCache { if ( is_subclass_of( $class, SqlBagOStuff::class ) ) { if ( isset( $params['server'] ) && !isset( $params['servers'] ) ) { $params['servers'] = [ $params['server'] ]; - unset( $param['server'] ); + unset( $params['server'] ); } // In the past it was not required to set 'dbDirectory' in $wgObjectCaches if ( isset( $params['servers'] ) ) { @@ -217,7 +223,7 @@ class ObjectCache { } return new $class( $params ); } else { - throw new MWException( "The definition of cache type \"" + throw new InvalidArgumentException( "The definition of cache type \"" . print_r( $params, true ) . "\" lacks both " . "factory and class parameters." ); } @@ -270,7 +276,7 @@ class ObjectCache { * * @param int|string|array $fallback Fallback cache or parameter map with 'fallback' * @return BagOStuff - * @throws MWException + * @throws InvalidArgumentException * @since 1.27 */ public static function getLocalServerInstance( $fallback = CACHE_NONE ) { @@ -315,13 +321,13 @@ class ObjectCache { * @since 1.26 * @param string $id A key in $wgWANObjectCaches. * @return WANObjectCache - * @throws MWException + * @throws InvalidArgumentException */ public static function newWANCacheFromId( $id ) { global $wgWANObjectCaches; if ( !isset( $wgWANObjectCaches[$id] ) ) { - throw new MWException( "Invalid object cache type \"$id\" requested. " . + throw new InvalidArgumentException( "Invalid object cache type \"$id\" requested. " . "It is not present in \$wgWANObjectCaches." ); } -- 2.20.1