From 210edff11b37ebb5b1439a2a1eb454cbac21565e Mon Sep 17 00:00:00 2001 From: Siebrand Mazeland Date: Wed, 27 Nov 2013 11:17:06 +0100 Subject: [PATCH] Remove underscore from classes LBFactory_*, LoadBalancer_*, LoadMonitor_* Part of program to remove underscores from class names. Checked core and 600+ extensions for occurrences. All uses are in core in core are updated in this patch. Patches for extensions: * https://gerrit.wikimedia.org/r/#/c/96463/ CentralAuth * https://gerrit.wikimedia.org/r/#/c/96460/ SimpleSecurity * https://gerrit.wikimedia.org/r/#/c/96459/ WikiBase * https://gerrit.wikimedia.org/r/#/c/96472/ Wikimedia configuration Antoine added tests to test the back compatibility. Change-Id: I507ba00a83bca8375a0215f00891b47e3c7afb9f --- RELEASE-NOTES-1.23 | 10 +++ includes/AutoLoader.php | 14 ++-- includes/DefaultSettings.php | 8 +- includes/dao/DBAccessBase.php | 2 +- includes/db/LBFactory.php | 42 ++++++++-- ...LBFactory_Multi.php => LBFactoryMulti.php} | 2 +- ...Factory_Single.php => LBFactorySingle.php} | 16 ++-- includes/db/LoadBalancer.php | 4 +- includes/db/LoadMonitor.php | 10 +-- includes/installer/DatabaseInstaller.php | 2 +- tests/phpunit/includes/db/LBFactoryTest.php | 82 +++++++++++++++++++ 11 files changed, 154 insertions(+), 38 deletions(-) rename includes/db/{LBFactory_Multi.php => LBFactoryMulti.php} (99%) rename includes/db/{LBFactory_Single.php => LBFactorySingle.php} (88%) create mode 100644 tests/phpunit/includes/db/LBFactoryTest.php diff --git a/RELEASE-NOTES-1.23 b/RELEASE-NOTES-1.23 index db80ad3c09..d9fbcbaae6 100644 --- a/RELEASE-NOTES-1.23 +++ b/RELEASE-NOTES-1.23 @@ -24,6 +24,8 @@ production. * Preferences 'watchcreations' and 'watchdefault' ("Add pages I create and files I upload to my watchlist", "Add pages and files I edit to my watchlist") are now enabled by default. +* $wgLBFactoryConf: Class names have had underscores removed. The configuration + should be updated if LBFactory_Simple or LBFactory_Multi is configured. === New features in 1.23 === * ResourceLoader can utilize the Web Storage API to cache modules client-side. @@ -90,6 +92,14 @@ production. properly for all parameters. * ApiQueryBase::titlePartToKey allows an extra parameter that indicates the namespace in order to properly capitalize the title part. +* Renamed classes: + - LBFactory_Fake to LBFactoryFake + - LBFactory_Multi to LBFactoryMulti + - LBFactory_Simple to LBFactorySimple + - LBFactory_Single to LBFactorySingle + - LoadBalancer_Single to LoadBalancerSingle + - LoadMonitor_MySQL to LoadMonitorMySQL + - LoadMonitor_Null to LoadMonitorNull === Languages updated in 1.23 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 9a9ce17e61..583f8896ea 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -461,16 +461,16 @@ $wgAutoloadLocalClasses = array( 'FakeResultWrapper' => 'includes/db/DatabaseUtility.php', 'Field' => 'includes/db/DatabaseUtility.php', 'LBFactory' => 'includes/db/LBFactory.php', - 'LBFactory_Fake' => 'includes/db/LBFactory.php', - 'LBFactory_Multi' => 'includes/db/LBFactory_Multi.php', - 'LBFactory_Simple' => 'includes/db/LBFactory.php', - 'LBFactory_Single' => 'includes/db/LBFactory_Single.php', + 'LBFactoryFake' => 'includes/db/LBFactory.php', + 'LBFactoryMulti' => 'includes/db/LBFactoryMulti.php', + 'LBFactorySimple' => 'includes/db/LBFactory.php', + 'LBFactorySingle' => 'includes/db/LBFactorySingle.php', 'LikeMatch' => 'includes/db/DatabaseUtility.php', 'LoadBalancer' => 'includes/db/LoadBalancer.php', - 'LoadBalancer_Single' => 'includes/db/LBFactory_Single.php', + 'LoadBalancerSingle' => 'includes/db/LBFactorySingle.php', 'LoadMonitor' => 'includes/db/LoadMonitor.php', - 'LoadMonitor_MySQL' => 'includes/db/LoadMonitor.php', - 'LoadMonitor_Null' => 'includes/db/LoadMonitor.php', + 'LoadMonitorMySQL' => 'includes/db/LoadMonitor.php', + 'LoadMonitorNull' => 'includes/db/LoadMonitor.php', 'MssqlField' => 'includes/db/DatabaseMssql.php', 'MssqlResult' => 'includes/db/DatabaseMssql.php', 'MySQLField' => 'includes/db/DatabaseMysqlBase.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 0f7707c380..f799452653 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1676,10 +1676,10 @@ $wgDBservers = false; * The class identified here is responsible for reading $wgDBservers, * $wgDBserver, etc., so overriding it may cause those globals to be ignored. * - * The LBFactory_Multi class is provided for this purpose, please see - * includes/db/LBFactory_Multi.php for configuration information. + * The LBFactoryMulti class is provided for this purpose, please see + * includes/db/LBFactoryMulti.php for configuration information. */ -$wgLBFactoryConf = array( 'class' => 'LBFactory_Simple' ); +$wgLBFactoryConf = array( 'class' => 'LBFactorySimple' ); /** * How long to wait for a slave to catch up to the master @@ -1828,7 +1828,7 @@ $wgExternalStores = array(); * ); * @endcode * - * Used by LBFactory_Simple, may be ignored if $wgLBFactoryConf is set to + * Used by LBFactorySimple, may be ignored if $wgLBFactoryConf is set to * another class. */ $wgExternalServers = array(); diff --git a/includes/dao/DBAccessBase.php b/includes/dao/DBAccessBase.php index a9f7b7f39e..2a0fde37ca 100644 --- a/includes/dao/DBAccessBase.php +++ b/includes/dao/DBAccessBase.php @@ -2,7 +2,7 @@ /** * Base class for objects that allow access to other wiki's databases using - * the foreign database access mechanism implemented by LBFactory_multi. + * the foreign database access mechanism implemented by LBFactoryMulti. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/includes/db/LBFactory.php b/includes/db/LBFactory.php index 6e377ff7be..c4e7976e04 100644 --- a/includes/db/LBFactory.php +++ b/includes/db/LBFactory.php @@ -37,7 +37,7 @@ abstract class LBFactory { */ public static function disableBackend() { global $wgLBFactoryConf; - self::$instance = new LBFactory_Fake( $wgLBFactoryConf ); + self::$instance = new LBFactoryFake( $wgLBFactoryConf ); } /** @@ -47,14 +47,44 @@ abstract class LBFactory { */ static function &singleton() { if ( is_null( self::$instance ) ) { - global $wgLBFactoryConf; - $class = $wgLBFactoryConf['class']; - self::$instance = new $class( $wgLBFactoryConf ); + $LBFactoryConf = self::getLBFactoryClass(); + + self::$instance = new $LBFactoryConf[0]( $LBFactoryConf[1] ); } return self::$instance; } + /** + * Returns the LBFactory class to use and the load balancer configuration. + * + * @return array ( factory class, $wgLBFactoryConf ) + */ + static function getLBFactoryClass() { + global $wgLBFactoryConf; + + // For configuration backward compatibility after removing + // underscores from class names in MediaWiki 1.23. + $bcClasses = array( + 'LBFactory_Simple' => 'LBFactorySimple', + 'LBFactory_Single' => 'LBFactorySingle', + 'LBFactory_Multi' => 'LBFactoryMulti', + 'LBFactory_Fake' => 'LBFactoryFake', + ); + + $class = $wgLBFactoryConf['class']; + + if ( in_array( $class, array_keys( $bcClasses ) ) ) { + $class = $bcClasses[$class]; + wfDeprecated( + '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details', + '1.23' + ); + } + + return array( $class, $wgLBFactoryConf ); + } + /** * Shut down, close connections and destroy the cached instance. */ @@ -167,7 +197,7 @@ abstract class LBFactory { /** * A simple single-master LBFactory that gets its configuration from the b/c globals */ -class LBFactory_Simple extends LBFactory { +class LBFactorySimple extends LBFactory { /** * @var LoadBalancer @@ -299,7 +329,7 @@ class LBFactory_Simple extends LBFactory { * Call LBFactory::disableBackend() to start using this, and * LBFactory::enableBackend() to return to normal behavior */ -class LBFactory_Fake extends LBFactory { +class LBFactoryFake extends LBFactory { function __construct( $conf ) { } diff --git a/includes/db/LBFactory_Multi.php b/includes/db/LBFactoryMulti.php similarity index 99% rename from includes/db/LBFactory_Multi.php rename to includes/db/LBFactoryMulti.php index a37a560505..2f4cb57bd9 100644 --- a/includes/db/LBFactory_Multi.php +++ b/includes/db/LBFactoryMulti.php @@ -73,7 +73,7 @@ * * @ingroup Database */ -class LBFactory_Multi extends LBFactory { +class LBFactoryMulti extends LBFactory { // Required settings var $sectionsByDB, $sectionLoads, $serverTemplate; // Optional settings diff --git a/includes/db/LBFactory_Single.php b/includes/db/LBFactorySingle.php similarity index 88% rename from includes/db/LBFactory_Single.php rename to includes/db/LBFactorySingle.php index 7dca06d7d7..fa9959f7bd 100644 --- a/includes/db/LBFactory_Single.php +++ b/includes/db/LBFactorySingle.php @@ -24,7 +24,7 @@ /** * An LBFactory class that always returns a single database object. */ -class LBFactory_Single extends LBFactory { +class LBFactorySingle extends LBFactory { protected $lb; /** @@ -32,13 +32,13 @@ class LBFactory_Single extends LBFactory { * - connection: The DatabaseBase connection object */ function __construct( $conf ) { - $this->lb = new LoadBalancer_Single( $conf ); + $this->lb = new LoadBalancerSingle( $conf ); } /** * @param $wiki bool|string * - * @return LoadBalancer_Single + * @return LoadBalancerSingle */ function newMainLB( $wiki = false ) { return $this->lb; @@ -47,7 +47,7 @@ class LBFactory_Single extends LBFactory { /** * @param $wiki bool|string * - * @return LoadBalancer_Single + * @return LoadBalancerSingle */ function getMainLB( $wiki = false ) { return $this->lb; @@ -57,7 +57,7 @@ class LBFactory_Single extends LBFactory { * @param $cluster * @param $wiki bool|string * - * @return LoadBalancer_Single + * @return LoadBalancerSingle */ function newExternalLB( $cluster, $wiki = false ) { return $this->lb; @@ -67,7 +67,7 @@ class LBFactory_Single extends LBFactory { * @param $cluster * @param $wiki bool|string * - * @return LoadBalancer_Single + * @return LoadBalancerSingle */ function &getExternalLB( $cluster, $wiki = false ) { return $this->lb; @@ -83,9 +83,9 @@ class LBFactory_Single extends LBFactory { } /** - * Helper class for LBFactory_Single. + * Helper class for LBFactorySingle. */ -class LoadBalancer_Single extends LoadBalancer { +class LoadBalancerSingle extends LoadBalancer { /** * @var DatabaseBase diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index 9967cc0f61..7dcb88f15e 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -72,9 +72,9 @@ class LoadBalancer { } else { $master = reset( $params['servers'] ); if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) { - $this->mLoadMonitorClass = 'LoadMonitor_MySQL'; + $this->mLoadMonitorClass = 'LoadMonitorMySQL'; } else { - $this->mLoadMonitorClass = 'LoadMonitor_Null'; + $this->mLoadMonitorClass = 'LoadMonitorNull'; } } diff --git a/includes/db/LoadMonitor.php b/includes/db/LoadMonitor.php index b2935316ed..58544565f3 100644 --- a/includes/db/LoadMonitor.php +++ b/includes/db/LoadMonitor.php @@ -71,11 +71,7 @@ interface LoadMonitor { function getLagTimes( $serverIndexes, $wiki ); } -/** - * @todo FIXME: Should be LoadMonitorNull per naming conventions. - * PHP CodeSniffer Squiz.Classes.ValidClassName.NotCamelCaps - */ -class LoadMonitor_Null implements LoadMonitor { +class LoadMonitorNull implements LoadMonitor { function __construct( $parent ) { } @@ -100,10 +96,8 @@ class LoadMonitor_Null implements LoadMonitor { * Uses memcached to cache the replication lag for a short time * * @ingroup Database - * @todo FIXME: Should be LoadMonitorMySQL per naming conventions. - * PHP CodeSniffer Squiz.Classes.ValidClassName.NotCamelCaps */ -class LoadMonitor_MySQL implements LoadMonitor { +class LoadMonitorMySQL implements LoadMonitor { /** * @var LoadBalancer */ diff --git a/includes/installer/DatabaseInstaller.php b/includes/installer/DatabaseInstaller.php index b4f21943ee..321884bd55 100644 --- a/includes/installer/DatabaseInstaller.php +++ b/includes/installer/DatabaseInstaller.php @@ -260,7 +260,7 @@ abstract class DatabaseInstaller { if ( !$status->isOK() ) { throw new MWException( __METHOD__ . ': unexpected DB connection error' ); } - LBFactory::setInstance( new LBFactory_Single( array( + LBFactory::setInstance( new LBFactorySingle( array( 'connection' => $status->value ) ) ); } diff --git a/tests/phpunit/includes/db/LBFactoryTest.php b/tests/phpunit/includes/db/LBFactoryTest.php new file mode 100644 index 0000000000..f64a784d4d --- /dev/null +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -0,0 +1,82 @@ +getMockBuilder( 'DatabaseMysql' ) + -> disableOriginalConstructor() + ->getMock(); + $this->setMwGlobals( 'wgLBFactoryConf', + array( + 'class' => $deprecated, + 'connection' => $mockDB, + # Various other parameters required: + 'sectionsByDB' => array(), + 'sectionLoads' => array(), + 'serverTemplate' => array(), + ) + ); + + global $wgLBFactoryConf; + $this->assertArrayHasKey( 'class', $wgLBFactoryConf ); + $this->assertEquals( $wgLBFactoryConf['class'], $deprecated ); + + # The point of this test is to call a deprecated interface and make + # sure it keeps back compatibility, so skip the deprecation warning. + $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' ); + $lbfactory = FakeLBFactory::singleton(); + $this->assertInstanceOf( $expected, $lbfactory, + "LBFactory passed $deprecated should yield the new class $expected" ); + } + + function provideDeprecatedLbfactoryClasses() { + return array( + # Format: new class, old class + array( 'LBFactorySimple', 'LBFactory_Simple' ), + array( 'LBFactorySingle', 'LBFactory_Single' ), + array( 'LBFactoryMulti', 'LBFactory_Multi' ), + array( 'LBFactoryFake', 'LBFactory_Fake' ), + ); + } +} -- 2.20.1