From: Umherirrender Date: Wed, 5 Jun 2019 21:16:45 +0000 (+0200) Subject: Inject LoadBalancer into GenderCache X-Git-Tag: 1.34.0-rc.0~737^2 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=69c689380f5e1187a97c9a491bc91ac5a5d00610;p=lhc%2Fweb%2Fwiklou.git Inject LoadBalancer into GenderCache This avoids global function wfGetDB Let the GenderCache work without a database connection as it is used by the installer Change-Id: I8a203c50de5841bc33693dadb8439a23a8c60910 --- diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index c5764d2262..e22d3671dc 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -209,7 +209,12 @@ return [ }, 'GenderCache' => function ( MediaWikiServices $services ) : GenderCache { - return new GenderCache( $services->getNamespaceInfo() ); + $nsInfo = $services->getNamespaceInfo(); + // Database layer may be disabled, so processing without database connection + $dbLoadBalancer = $services->isServiceDisabled( 'DBLoadBalancer' ) + ? null + : $services->getDBLoadBalancer(); + return new GenderCache( $nsInfo, $dbLoadBalancer ); }, 'HttpRequestFactory' => diff --git a/includes/cache/GenderCache.php b/includes/cache/GenderCache.php index eedc3c6f04..a181507e4e 100644 --- a/includes/cache/GenderCache.php +++ b/includes/cache/GenderCache.php @@ -21,7 +21,9 @@ * @author Niklas Laxström * @ingroup Cache */ + use MediaWiki\MediaWikiServices; +use Wikimedia\Rdbms\ILoadBalancer; /** * Caches user genders when needed to use correct namespace aliases. @@ -37,8 +39,12 @@ class GenderCache { /** @var NamespaceInfo */ private $nsInfo; - public function __construct( NamespaceInfo $nsInfo = null ) { + /** @var ILoadBalancer|null */ + private $loadBalancer; + + public function __construct( NamespaceInfo $nsInfo = null, ILoadBalancer $loadBalancer = null ) { $this->nsInfo = $nsInfo ?? MediaWikiServices::getInstance()->getNamespaceInfo(); + $this->loadBalancer = $loadBalancer; } /** @@ -164,7 +170,13 @@ class GenderCache { return; } - $dbr = wfGetDB( DB_REPLICA ); + // Only query database, when load balancer is provided by service wiring + // This maybe not happen when running as part of the installer + if ( $this->loadBalancer === null ) { + return; + } + + $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA ); $table = [ 'user', 'user_properties' ]; $fields = [ 'user_name', 'up_value' ]; $conds = [ 'user_name' => $usersToCheck ];