Remove underscore from classes LBFactory_*, LoadBalancer_*, LoadMonitor_*
authorSiebrand Mazeland <s.mazeland@xs4all.nl>
Wed, 27 Nov 2013 10:17:06 +0000 (11:17 +0100)
committerChad Horohoe <chadh@wikimedia.org>
Thu, 26 Dec 2013 21:30:15 +0000 (16:30 -0500)
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

13 files changed:
RELEASE-NOTES-1.23
includes/AutoLoader.php
includes/DefaultSettings.php
includes/dao/DBAccessBase.php
includes/db/LBFactory.php
includes/db/LBFactoryMulti.php [new file with mode: 0644]
includes/db/LBFactorySingle.php [new file with mode: 0644]
includes/db/LBFactory_Multi.php [deleted file]
includes/db/LBFactory_Single.php [deleted file]
includes/db/LoadBalancer.php
includes/db/LoadMonitor.php
includes/installer/DatabaseInstaller.php
tests/phpunit/includes/db/LBFactoryTest.php [new file with mode: 0644]

index db80ad3..d9fbcba 100644 (file)
@@ -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 ===
 
index 9a9ce17..583f889 100644 (file)
@@ -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',
index 0f7707c..f799452 100644 (file)
@@ -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();
index a9f7b7f..2a0fde3 100644 (file)
@@ -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
index 6e377ff..c4e7976 100644 (file)
@@ -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/LBFactoryMulti.php b/includes/db/LBFactoryMulti.php
new file mode 100644 (file)
index 0000000..2f4cb57
--- /dev/null
@@ -0,0 +1,337 @@
+<?php
+/**
+ * Advanced generator of database load balancing objects for wiki farms.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Database
+ */
+
+/**
+ * A multi-wiki, multi-master factory for Wikimedia and similar installations.
+ * Ignores the old configuration globals
+ *
+ * Configuration:
+ *     sectionsByDB                A map of database names to section names
+ *
+ *     sectionLoads                A 2-d map. For each section, gives a map of server names to
+ *                                 load ratios. For example:
+ *                                 array(
+ *                                     'section1' => array(
+ *                                         'db1' => 100,
+ *                                         'db2' => 100
+ *                                     )
+ *                                 )
+ *
+ *     serverTemplate              A server info associative array as documented for $wgDBservers.
+ *                                 The host, hostName and load entries will be overridden.
+ *
+ *     groupLoadsBySection         A 3-d map giving server load ratios for each section and group.
+ *                                 For example:
+ *                                 array(
+ *                                     'section1' => array(
+ *                                         'group1' => array(
+ *                                             'db1' => 100,
+ *                                             'db2' => 100
+ *                                         )
+ *                                     )
+ *                                 )
+ *
+ *     groupLoadsByDB              A 3-d map giving server load ratios by DB name.
+ *
+ *     hostsByName                 A map of hostname to IP address.
+ *
+ *     externalLoads               A map of external storage cluster name to server load map
+ *
+ *     externalTemplateOverrides   A set of server info keys overriding serverTemplate for external
+ *                                 storage
+ *
+ *     templateOverridesByServer   A 2-d map overriding serverTemplate and
+ *                                 externalTemplateOverrides on a server-by-server basis. Applies
+ *                                 to both core and external storage.
+ *
+ *     templateOverridesByCluster  A 2-d map overriding the server info by external storage cluster
+ *
+ *     masterTemplateOverrides     An override array for all master servers.
+ *
+ *     readOnlyBySection           A map of section name to read-only message.
+ *                                 Missing or false for read/write.
+ *
+ * @ingroup Database
+ */
+class LBFactoryMulti extends LBFactory {
+       // Required settings
+       var $sectionsByDB, $sectionLoads, $serverTemplate;
+       // Optional settings
+       var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
+       var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
+       var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
+       // Other stuff
+       var $conf, $mainLBs = array(), $extLBs = array();
+       var $lastWiki, $lastSection;
+
+       /**
+        * @param $conf array
+        * @throws MWException
+        */
+       function __construct( $conf ) {
+               $this->chronProt = new ChronologyProtector;
+               $this->conf = $conf;
+               $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
+               $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
+                       'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
+                       'templateOverridesByCluster', 'masterTemplateOverrides',
+                       'readOnlyBySection' );
+
+               foreach ( $required as $key ) {
+                       if ( !isset( $conf[$key] ) ) {
+                               throw new MWException( __CLASS__ . ": $key is required in configuration" );
+                       }
+                       $this->$key = $conf[$key];
+               }
+
+               foreach ( $optional as $key ) {
+                       if ( isset( $conf[$key] ) ) {
+                               $this->$key = $conf[$key];
+                       }
+               }
+
+               // Check for read-only mode
+               $section = $this->getSectionForWiki();
+               if ( !empty( $this->readOnlyBySection[$section] ) ) {
+                       global $wgReadOnly;
+                       $wgReadOnly = $this->readOnlyBySection[$section];
+               }
+       }
+
+       /**
+        * @param $wiki bool|string
+        * @return string
+        */
+       function getSectionForWiki( $wiki = false ) {
+               if ( $this->lastWiki === $wiki ) {
+                       return $this->lastSection;
+               }
+               list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
+               if ( isset( $this->sectionsByDB[$dbName] ) ) {
+                       $section = $this->sectionsByDB[$dbName];
+               } else {
+                       $section = 'DEFAULT';
+               }
+               $this->lastSection = $section;
+               $this->lastWiki = $wiki;
+
+               return $section;
+       }
+
+       /**
+        * @param $wiki bool|string
+        * @return LoadBalancer
+        */
+       function newMainLB( $wiki = false ) {
+               list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
+               $section = $this->getSectionForWiki( $wiki );
+               $groupLoads = array();
+               if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
+                       $groupLoads = $this->groupLoadsByDB[$dbName];
+               }
+
+               if ( isset( $this->groupLoadsBySection[$section] ) ) {
+                       $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
+               }
+
+               return $this->newLoadBalancer(
+                       $this->serverTemplate,
+                       $this->sectionLoads[$section],
+                       $groupLoads
+               );
+       }
+
+       /**
+        * @param $wiki bool|string
+        * @return LoadBalancer
+        */
+       function getMainLB( $wiki = false ) {
+               $section = $this->getSectionForWiki( $wiki );
+               if ( !isset( $this->mainLBs[$section] ) ) {
+                       $lb = $this->newMainLB( $wiki, $section );
+                       $lb->parentInfo( array( 'id' => "main-$section" ) );
+                       $this->chronProt->initLB( $lb );
+                       $this->mainLBs[$section] = $lb;
+               }
+
+               return $this->mainLBs[$section];
+       }
+
+       /**
+        * @param string $cluster
+        * @param bool $wiki
+        * @throws MWException
+        * @return LoadBalancer
+        */
+       function newExternalLB( $cluster, $wiki = false ) {
+               if ( !isset( $this->externalLoads[$cluster] ) ) {
+                       throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
+               }
+               $template = $this->serverTemplate;
+               if ( isset( $this->externalTemplateOverrides ) ) {
+                       $template = $this->externalTemplateOverrides + $template;
+               }
+               if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
+                       $template = $this->templateOverridesByCluster[$cluster] + $template;
+               }
+
+               return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
+       }
+
+       /**
+        * @param $cluster
+        * @param $wiki
+        * @return LoadBalancer
+        */
+       function &getExternalLB( $cluster, $wiki = false ) {
+               if ( !isset( $this->extLBs[$cluster] ) ) {
+                       $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
+                       $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
+                       $this->chronProt->initLB( $this->extLBs[$cluster] );
+               }
+
+               return $this->extLBs[$cluster];
+       }
+
+       /**
+        * Make a new load balancer object based on template and load array
+        *
+        * @param $template
+        * @param $loads array
+        * @param $groupLoads
+        * @return LoadBalancer
+        */
+       function newLoadBalancer( $template, $loads, $groupLoads ) {
+               global $wgMasterWaitTimeout;
+               $servers = $this->makeServerArray( $template, $loads, $groupLoads );
+               $lb = new LoadBalancer( array(
+                       'servers' => $servers,
+                       'masterWaitTimeout' => $wgMasterWaitTimeout
+               ) );
+
+               return $lb;
+       }
+
+       /**
+        * Make a server array as expected by LoadBalancer::__construct, using a template and load array
+        *
+        * @param $template
+        * @param $loads array
+        * @param $groupLoads
+        * @return array
+        */
+       function makeServerArray( $template, $loads, $groupLoads ) {
+               $servers = array();
+               $master = true;
+               $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
+               foreach ( $groupLoadsByServer as $server => $stuff ) {
+                       if ( !isset( $loads[$server] ) ) {
+                               $loads[$server] = 0;
+                       }
+               }
+               foreach ( $loads as $serverName => $load ) {
+                       $serverInfo = $template;
+                       if ( $master ) {
+                               $serverInfo['master'] = true;
+                               if ( isset( $this->masterTemplateOverrides ) ) {
+                                       $serverInfo = $this->masterTemplateOverrides + $serverInfo;
+                               }
+                               $master = false;
+                       }
+                       if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
+                               $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
+                       }
+                       if ( isset( $groupLoadsByServer[$serverName] ) ) {
+                               $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
+                       }
+                       if ( isset( $this->hostsByName[$serverName] ) ) {
+                               $serverInfo['host'] = $this->hostsByName[$serverName];
+                       } else {
+                               $serverInfo['host'] = $serverName;
+                       }
+                       $serverInfo['hostName'] = $serverName;
+                       $serverInfo['load'] = $load;
+                       $servers[] = $serverInfo;
+               }
+
+               return $servers;
+       }
+
+       /**
+        * Take a group load array indexed by group then server, and reindex it by server then group
+        * @param $groupLoads
+        * @return array
+        */
+       function reindexGroupLoads( $groupLoads ) {
+               $reindexed = array();
+               foreach ( $groupLoads as $group => $loads ) {
+                       foreach ( $loads as $server => $load ) {
+                               $reindexed[$server][$group] = $load;
+                       }
+               }
+
+               return $reindexed;
+       }
+
+       /**
+        * Get the database name and prefix based on the wiki ID
+        * @param $wiki bool
+        * @return array
+        */
+       function getDBNameAndPrefix( $wiki = false ) {
+               if ( $wiki === false ) {
+                       global $wgDBname, $wgDBprefix;
+
+                       return array( $wgDBname, $wgDBprefix );
+               } else {
+                       return wfSplitWikiID( $wiki );
+               }
+       }
+
+       /**
+        * Execute a function for each tracked load balancer
+        * The callback is called with the load balancer as the first parameter,
+        * and $params passed as the subsequent parameters.
+        * @param $callback
+        * @param $params array
+        */
+       function forEachLB( $callback, $params = array() ) {
+               foreach ( $this->mainLBs as $lb ) {
+                       call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
+               }
+               foreach ( $this->extLBs as $lb ) {
+                       call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
+               }
+       }
+
+       function shutdown() {
+               foreach ( $this->mainLBs as $lb ) {
+                       $this->chronProt->shutdownLB( $lb );
+               }
+               foreach ( $this->extLBs as $extLB ) {
+                       $this->chronProt->shutdownLB( $extLB );
+               }
+               $this->chronProt->shutdown();
+               $this->commitMasterChanges();
+       }
+}
diff --git a/includes/db/LBFactorySingle.php b/includes/db/LBFactorySingle.php
new file mode 100644 (file)
index 0000000..fa9959f
--- /dev/null
@@ -0,0 +1,118 @@
+<?php
+/**
+ * Simple generator of database connections that always returns the same object.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Database
+ */
+
+/**
+ * An LBFactory class that always returns a single database object.
+ */
+class LBFactorySingle extends LBFactory {
+       protected $lb;
+
+       /**
+        * @param array $conf An associative array with one member:
+        *  - connection: The DatabaseBase connection object
+        */
+       function __construct( $conf ) {
+               $this->lb = new LoadBalancerSingle( $conf );
+       }
+
+       /**
+        * @param $wiki bool|string
+        *
+        * @return LoadBalancerSingle
+        */
+       function newMainLB( $wiki = false ) {
+               return $this->lb;
+       }
+
+       /**
+        * @param $wiki bool|string
+        *
+        * @return LoadBalancerSingle
+        */
+       function getMainLB( $wiki = false ) {
+               return $this->lb;
+       }
+
+       /**
+        * @param $cluster
+        * @param $wiki bool|string
+        *
+        * @return LoadBalancerSingle
+        */
+       function newExternalLB( $cluster, $wiki = false ) {
+               return $this->lb;
+       }
+
+       /**
+        * @param $cluster
+        * @param $wiki bool|string
+        *
+        * @return LoadBalancerSingle
+        */
+       function &getExternalLB( $cluster, $wiki = false ) {
+               return $this->lb;
+       }
+
+       /**
+        * @param $callback string|array
+        * @param $params array
+        */
+       function forEachLB( $callback, $params = array() ) {
+               call_user_func_array( $callback, array_merge( array( $this->lb ), $params ) );
+       }
+}
+
+/**
+ * Helper class for LBFactorySingle.
+ */
+class LoadBalancerSingle extends LoadBalancer {
+
+       /**
+        * @var DatabaseBase
+        */
+       var $db;
+
+       /**
+        * @param $params array
+        */
+       function __construct( $params ) {
+               $this->db = $params['connection'];
+               parent::__construct( array( 'servers' => array( array(
+                       'type' => $this->db->getType(),
+                       'host' => $this->db->getServer(),
+                       'dbname' => $this->db->getDBname(),
+                       'load' => 1,
+               ) ) ) );
+       }
+
+       /**
+        *
+        * @param $server string
+        * @param $dbNameOverride bool
+        *
+        * @return DatabaseBase
+        */
+       function reallyOpenConnection( $server, $dbNameOverride = false ) {
+               return $this->db;
+       }
+}
diff --git a/includes/db/LBFactory_Multi.php b/includes/db/LBFactory_Multi.php
deleted file mode 100644 (file)
index a37a560..0000000
+++ /dev/null
@@ -1,337 +0,0 @@
-<?php
-/**
- * Advanced generator of database load balancing objects for wiki farms.
- *
- * 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
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @ingroup Database
- */
-
-/**
- * A multi-wiki, multi-master factory for Wikimedia and similar installations.
- * Ignores the old configuration globals
- *
- * Configuration:
- *     sectionsByDB                A map of database names to section names
- *
- *     sectionLoads                A 2-d map. For each section, gives a map of server names to
- *                                 load ratios. For example:
- *                                 array(
- *                                     'section1' => array(
- *                                         'db1' => 100,
- *                                         'db2' => 100
- *                                     )
- *                                 )
- *
- *     serverTemplate              A server info associative array as documented for $wgDBservers.
- *                                 The host, hostName and load entries will be overridden.
- *
- *     groupLoadsBySection         A 3-d map giving server load ratios for each section and group.
- *                                 For example:
- *                                 array(
- *                                     'section1' => array(
- *                                         'group1' => array(
- *                                             'db1' => 100,
- *                                             'db2' => 100
- *                                         )
- *                                     )
- *                                 )
- *
- *     groupLoadsByDB              A 3-d map giving server load ratios by DB name.
- *
- *     hostsByName                 A map of hostname to IP address.
- *
- *     externalLoads               A map of external storage cluster name to server load map
- *
- *     externalTemplateOverrides   A set of server info keys overriding serverTemplate for external
- *                                 storage
- *
- *     templateOverridesByServer   A 2-d map overriding serverTemplate and
- *                                 externalTemplateOverrides on a server-by-server basis. Applies
- *                                 to both core and external storage.
- *
- *     templateOverridesByCluster  A 2-d map overriding the server info by external storage cluster
- *
- *     masterTemplateOverrides     An override array for all master servers.
- *
- *     readOnlyBySection           A map of section name to read-only message.
- *                                 Missing or false for read/write.
- *
- * @ingroup Database
- */
-class LBFactory_Multi extends LBFactory {
-       // Required settings
-       var $sectionsByDB, $sectionLoads, $serverTemplate;
-       // Optional settings
-       var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
-       var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
-       var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
-       // Other stuff
-       var $conf, $mainLBs = array(), $extLBs = array();
-       var $lastWiki, $lastSection;
-
-       /**
-        * @param $conf array
-        * @throws MWException
-        */
-       function __construct( $conf ) {
-               $this->chronProt = new ChronologyProtector;
-               $this->conf = $conf;
-               $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
-               $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
-                       'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
-                       'templateOverridesByCluster', 'masterTemplateOverrides',
-                       'readOnlyBySection' );
-
-               foreach ( $required as $key ) {
-                       if ( !isset( $conf[$key] ) ) {
-                               throw new MWException( __CLASS__ . ": $key is required in configuration" );
-                       }
-                       $this->$key = $conf[$key];
-               }
-
-               foreach ( $optional as $key ) {
-                       if ( isset( $conf[$key] ) ) {
-                               $this->$key = $conf[$key];
-                       }
-               }
-
-               // Check for read-only mode
-               $section = $this->getSectionForWiki();
-               if ( !empty( $this->readOnlyBySection[$section] ) ) {
-                       global $wgReadOnly;
-                       $wgReadOnly = $this->readOnlyBySection[$section];
-               }
-       }
-
-       /**
-        * @param $wiki bool|string
-        * @return string
-        */
-       function getSectionForWiki( $wiki = false ) {
-               if ( $this->lastWiki === $wiki ) {
-                       return $this->lastSection;
-               }
-               list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
-               if ( isset( $this->sectionsByDB[$dbName] ) ) {
-                       $section = $this->sectionsByDB[$dbName];
-               } else {
-                       $section = 'DEFAULT';
-               }
-               $this->lastSection = $section;
-               $this->lastWiki = $wiki;
-
-               return $section;
-       }
-
-       /**
-        * @param $wiki bool|string
-        * @return LoadBalancer
-        */
-       function newMainLB( $wiki = false ) {
-               list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
-               $section = $this->getSectionForWiki( $wiki );
-               $groupLoads = array();
-               if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
-                       $groupLoads = $this->groupLoadsByDB[$dbName];
-               }
-
-               if ( isset( $this->groupLoadsBySection[$section] ) ) {
-                       $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
-               }
-
-               return $this->newLoadBalancer(
-                       $this->serverTemplate,
-                       $this->sectionLoads[$section],
-                       $groupLoads
-               );
-       }
-
-       /**
-        * @param $wiki bool|string
-        * @return LoadBalancer
-        */
-       function getMainLB( $wiki = false ) {
-               $section = $this->getSectionForWiki( $wiki );
-               if ( !isset( $this->mainLBs[$section] ) ) {
-                       $lb = $this->newMainLB( $wiki, $section );
-                       $lb->parentInfo( array( 'id' => "main-$section" ) );
-                       $this->chronProt->initLB( $lb );
-                       $this->mainLBs[$section] = $lb;
-               }
-
-               return $this->mainLBs[$section];
-       }
-
-       /**
-        * @param string $cluster
-        * @param bool $wiki
-        * @throws MWException
-        * @return LoadBalancer
-        */
-       function newExternalLB( $cluster, $wiki = false ) {
-               if ( !isset( $this->externalLoads[$cluster] ) ) {
-                       throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
-               }
-               $template = $this->serverTemplate;
-               if ( isset( $this->externalTemplateOverrides ) ) {
-                       $template = $this->externalTemplateOverrides + $template;
-               }
-               if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
-                       $template = $this->templateOverridesByCluster[$cluster] + $template;
-               }
-
-               return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
-       }
-
-       /**
-        * @param $cluster
-        * @param $wiki
-        * @return LoadBalancer
-        */
-       function &getExternalLB( $cluster, $wiki = false ) {
-               if ( !isset( $this->extLBs[$cluster] ) ) {
-                       $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
-                       $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
-                       $this->chronProt->initLB( $this->extLBs[$cluster] );
-               }
-
-               return $this->extLBs[$cluster];
-       }
-
-       /**
-        * Make a new load balancer object based on template and load array
-        *
-        * @param $template
-        * @param $loads array
-        * @param $groupLoads
-        * @return LoadBalancer
-        */
-       function newLoadBalancer( $template, $loads, $groupLoads ) {
-               global $wgMasterWaitTimeout;
-               $servers = $this->makeServerArray( $template, $loads, $groupLoads );
-               $lb = new LoadBalancer( array(
-                       'servers' => $servers,
-                       'masterWaitTimeout' => $wgMasterWaitTimeout
-               ) );
-
-               return $lb;
-       }
-
-       /**
-        * Make a server array as expected by LoadBalancer::__construct, using a template and load array
-        *
-        * @param $template
-        * @param $loads array
-        * @param $groupLoads
-        * @return array
-        */
-       function makeServerArray( $template, $loads, $groupLoads ) {
-               $servers = array();
-               $master = true;
-               $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
-               foreach ( $groupLoadsByServer as $server => $stuff ) {
-                       if ( !isset( $loads[$server] ) ) {
-                               $loads[$server] = 0;
-                       }
-               }
-               foreach ( $loads as $serverName => $load ) {
-                       $serverInfo = $template;
-                       if ( $master ) {
-                               $serverInfo['master'] = true;
-                               if ( isset( $this->masterTemplateOverrides ) ) {
-                                       $serverInfo = $this->masterTemplateOverrides + $serverInfo;
-                               }
-                               $master = false;
-                       }
-                       if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
-                               $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
-                       }
-                       if ( isset( $groupLoadsByServer[$serverName] ) ) {
-                               $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
-                       }
-                       if ( isset( $this->hostsByName[$serverName] ) ) {
-                               $serverInfo['host'] = $this->hostsByName[$serverName];
-                       } else {
-                               $serverInfo['host'] = $serverName;
-                       }
-                       $serverInfo['hostName'] = $serverName;
-                       $serverInfo['load'] = $load;
-                       $servers[] = $serverInfo;
-               }
-
-               return $servers;
-       }
-
-       /**
-        * Take a group load array indexed by group then server, and reindex it by server then group
-        * @param $groupLoads
-        * @return array
-        */
-       function reindexGroupLoads( $groupLoads ) {
-               $reindexed = array();
-               foreach ( $groupLoads as $group => $loads ) {
-                       foreach ( $loads as $server => $load ) {
-                               $reindexed[$server][$group] = $load;
-                       }
-               }
-
-               return $reindexed;
-       }
-
-       /**
-        * Get the database name and prefix based on the wiki ID
-        * @param $wiki bool
-        * @return array
-        */
-       function getDBNameAndPrefix( $wiki = false ) {
-               if ( $wiki === false ) {
-                       global $wgDBname, $wgDBprefix;
-
-                       return array( $wgDBname, $wgDBprefix );
-               } else {
-                       return wfSplitWikiID( $wiki );
-               }
-       }
-
-       /**
-        * Execute a function for each tracked load balancer
-        * The callback is called with the load balancer as the first parameter,
-        * and $params passed as the subsequent parameters.
-        * @param $callback
-        * @param $params array
-        */
-       function forEachLB( $callback, $params = array() ) {
-               foreach ( $this->mainLBs as $lb ) {
-                       call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
-               }
-               foreach ( $this->extLBs as $lb ) {
-                       call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
-               }
-       }
-
-       function shutdown() {
-               foreach ( $this->mainLBs as $lb ) {
-                       $this->chronProt->shutdownLB( $lb );
-               }
-               foreach ( $this->extLBs as $extLB ) {
-                       $this->chronProt->shutdownLB( $extLB );
-               }
-               $this->chronProt->shutdown();
-               $this->commitMasterChanges();
-       }
-}
diff --git a/includes/db/LBFactory_Single.php b/includes/db/LBFactory_Single.php
deleted file mode 100644 (file)
index 7dca06d..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-<?php
-/**
- * Simple generator of database connections that always returns the same object.
- *
- * 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
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @ingroup Database
- */
-
-/**
- * An LBFactory class that always returns a single database object.
- */
-class LBFactory_Single extends LBFactory {
-       protected $lb;
-
-       /**
-        * @param array $conf An associative array with one member:
-        *  - connection: The DatabaseBase connection object
-        */
-       function __construct( $conf ) {
-               $this->lb = new LoadBalancer_Single( $conf );
-       }
-
-       /**
-        * @param $wiki bool|string
-        *
-        * @return LoadBalancer_Single
-        */
-       function newMainLB( $wiki = false ) {
-               return $this->lb;
-       }
-
-       /**
-        * @param $wiki bool|string
-        *
-        * @return LoadBalancer_Single
-        */
-       function getMainLB( $wiki = false ) {
-               return $this->lb;
-       }
-
-       /**
-        * @param $cluster
-        * @param $wiki bool|string
-        *
-        * @return LoadBalancer_Single
-        */
-       function newExternalLB( $cluster, $wiki = false ) {
-               return $this->lb;
-       }
-
-       /**
-        * @param $cluster
-        * @param $wiki bool|string
-        *
-        * @return LoadBalancer_Single
-        */
-       function &getExternalLB( $cluster, $wiki = false ) {
-               return $this->lb;
-       }
-
-       /**
-        * @param $callback string|array
-        * @param $params array
-        */
-       function forEachLB( $callback, $params = array() ) {
-               call_user_func_array( $callback, array_merge( array( $this->lb ), $params ) );
-       }
-}
-
-/**
- * Helper class for LBFactory_Single.
- */
-class LoadBalancer_Single extends LoadBalancer {
-
-       /**
-        * @var DatabaseBase
-        */
-       var $db;
-
-       /**
-        * @param $params array
-        */
-       function __construct( $params ) {
-               $this->db = $params['connection'];
-               parent::__construct( array( 'servers' => array( array(
-                       'type' => $this->db->getType(),
-                       'host' => $this->db->getServer(),
-                       'dbname' => $this->db->getDBname(),
-                       'load' => 1,
-               ) ) ) );
-       }
-
-       /**
-        *
-        * @param $server string
-        * @param $dbNameOverride bool
-        *
-        * @return DatabaseBase
-        */
-       function reallyOpenConnection( $server, $dbNameOverride = false ) {
-               return $this->db;
-       }
-}
index 9967cc0..7dcb88f 100644 (file)
@@ -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';
                        }
                }
 
index b293531..5854456 100644 (file)
@@ -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
         */
index b4f2194..321884b 100644 (file)
@@ -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 (file)
index 0000000..f64a784
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+/**
+ * Holds tests for LBFactory abstract MediaWiki class.
+ *
+ * @section LICENSE
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Antoine Musso
+ * @copyright © 2013 Antoine Musso
+ * @copyright © 2013 Wikimedia Foundation Inc.
+ */
+
+class FakeLBFactory extends LBFactory {
+       function __construct( $conf ) {}
+       function newMainLB( $wiki = false ) {}
+       function getMainLB( $wiki = false ) {}
+       function newExternalLB( $cluster, $wiki = false ) {}
+       function &getExternalLB( $cluster, $wiki = false ) {}
+       function forEachLB( $callback, $params = array() ) {}
+}
+
+class LBFactoryTest extends MediaWikiTestCase {
+
+       function setup() {
+               parent::setup();
+               FakeLBFactory::destroyInstance();
+       }
+
+       /**
+        * @dataProvider provideDeprecatedLbfactoryClasses
+        */
+       function testLbfactoryClassBackcompatibility( $expected, $deprecated ) {
+               $mockDB = $this->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' ),
+               );
+       }
+}