Merge "Replacing old references from Bugzilla to Phabricator"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 25 Sep 2015 07:08:53 +0000 (07:08 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 25 Sep 2015 07:08:53 +0000 (07:08 +0000)
38 files changed:
RELEASE-NOTES-1.26
autoload.php
composer.json
includes/FileContentsHasher.php [deleted file]
includes/HttpFunctions.php
includes/cache/LocalisationCache.php
includes/db/LBFactory.php [deleted file]
includes/db/LBFactoryMulti.php [deleted file]
includes/db/LBFactorySingle.php [deleted file]
includes/db/LoadBalancer.php [deleted file]
includes/db/LoadMonitor.php [deleted file]
includes/db/LoadMonitorMySQL.php [deleted file]
includes/db/loadbalancer/LBFactory.php [new file with mode: 0644]
includes/db/loadbalancer/LBFactoryFake.php [new file with mode: 0644]
includes/db/loadbalancer/LBFactoryMulti.php [new file with mode: 0644]
includes/db/loadbalancer/LBFactorySimple.php [new file with mode: 0644]
includes/db/loadbalancer/LBFactorySingle.php [new file with mode: 0644]
includes/db/loadbalancer/LoadBalancer.php [new file with mode: 0644]
includes/db/loadbalancer/LoadMonitor.php [new file with mode: 0644]
includes/db/loadbalancer/LoadMonitorMySQL.php [new file with mode: 0644]
includes/utils/FileContentsHasher.php [new file with mode: 0644]
languages/Language.php
languages/utils/CLDRPluralRuleConverter.php [deleted file]
languages/utils/CLDRPluralRuleConverterExpression.php [deleted file]
languages/utils/CLDRPluralRuleConverterFragment.php [deleted file]
languages/utils/CLDRPluralRuleConverterOperator.php [deleted file]
languages/utils/CLDRPluralRuleError.php [deleted file]
languages/utils/CLDRPluralRuleEvaluator.php [deleted file]
languages/utils/CLDRPluralRuleEvaluatorRange.php [deleted file]
resources/src/jquery/jquery.tablesorter.js
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/data/filecontentshasher/hash.svg [new file with mode: 0644]
tests/phpunit/data/filecontentshasher/primes.txt [new file with mode: 0644]
tests/phpunit/includes/utils/FileContentsHasherTest.php [new file with mode: 0644]
tests/phpunit/languages/utils/CLDRPluralRuleEvaluatorTest.php [deleted file]
tests/phpunit/phpunit.php
tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js
tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js

index d4150eb..a13fc2e 100644 (file)
@@ -44,6 +44,7 @@ production.
 * $wgBlockAllowsUTEdit is now set to true by default. This allows
   blocked users to edit their talk pages unless explicitly disabled
   when they are being blocked.
+* CLDRPluralRule* classes have been replaced with wikimedia/cldr-plural-rule-parser.
 
 === New features in 1.26 ===
 * (T51506) Now action=info gives estimates of actual watchers for a page.
@@ -98,6 +99,7 @@ production.
 * Upgrade jQuery Client from v1.0.0 to v2.0.0.
 * Added mediawiki/at-ease 1.0.0.
 * Update QUnit from v1.17.1 to v1.18.0.
+* Added wikimedia/cldr-plural-rule-parser 1.0.0
 
 === Bug fixes in 1.26 ===
 * (T53283) load.php sometimes sends 304 response without full headers
index e5e7cda..f1b0a6c 100644 (file)
@@ -184,13 +184,6 @@ $wgAutoloadLocalClasses = array(
        'BmpHandler' => __DIR__ . '/includes/media/BMP.php',
        'BrokenRedirectsPage' => __DIR__ . '/includes/specials/SpecialBrokenRedirects.php',
        'BufferingStatsdDataFactory' => __DIR__ . '/includes/libs/BufferingStatsdDataFactory.php',
-       'CLDRPluralRuleConverter' => __DIR__ . '/languages/utils/CLDRPluralRuleConverter.php',
-       'CLDRPluralRuleConverterExpression' => __DIR__ . '/languages/utils/CLDRPluralRuleConverterExpression.php',
-       'CLDRPluralRuleConverterFragment' => __DIR__ . '/languages/utils/CLDRPluralRuleConverterFragment.php',
-       'CLDRPluralRuleConverterOperator' => __DIR__ . '/languages/utils/CLDRPluralRuleConverterOperator.php',
-       'CLDRPluralRuleError' => __DIR__ . '/languages/utils/CLDRPluralRuleError.php',
-       'CLDRPluralRuleEvaluator' => __DIR__ . '/languages/utils/CLDRPluralRuleEvaluator.php',
-       'CLDRPluralRuleEvaluatorRange' => __DIR__ . '/languages/utils/CLDRPluralRuleEvaluatorRange.php',
        'CLIParser' => __DIR__ . '/maintenance/parse.php',
        'CSSMin' => __DIR__ . '/includes/libs/CSSMin.php',
        'CacheDependency' => __DIR__ . '/includes/cache/CacheDependency.php',
@@ -283,7 +276,7 @@ $wgAutoloadLocalClasses = array(
        'CsvStatsOutput' => __DIR__ . '/maintenance/language/StatOutputs.php',
        'CurlHttpRequest' => __DIR__ . '/includes/HttpFunctions.php',
        'DBAccessBase' => __DIR__ . '/includes/dao/DBAccessBase.php',
-       'DBAccessError' => __DIR__ . '/includes/db/LBFactory.php',
+       'DBAccessError' => __DIR__ . '/includes/db/loadbalancer/LBFactory.php',
        'DBAccessObjectUtils' => __DIR__ . '/includes/dao/DBAccessObjectUtils.php',
        'DBConnRef' => __DIR__ . '/includes/db/DBConnRef.php',
        'DBConnectionError' => __DIR__ . '/includes/db/DatabaseError.php',
@@ -437,7 +430,7 @@ $wgAutoloadLocalClasses = array(
        'FileBackendStoreShardListIterator' => __DIR__ . '/includes/filebackend/FileBackendStore.php',
        'FileBasedSiteLookup' => __DIR__ . '/includes/site/FileBasedSiteLookup.php',
        'FileCacheBase' => __DIR__ . '/includes/cache/FileCacheBase.php',
-       'FileContentsHasher' => __DIR__ . '/includes/FileContentsHasher.php',
+       'FileContentsHasher' => __DIR__ . '/includes/utils/FileContentsHasher.php',
        'FileDeleteForm' => __DIR__ . '/includes/FileDeleteForm.php',
        'FileDependency' => __DIR__ . '/includes/cache/CacheDependency.php',
        'FileDuplicateSearchPage' => __DIR__ . '/includes/specials/SpecialFileDuplicateSearch.php',
@@ -611,11 +604,11 @@ $wgAutoloadLocalClasses = array(
        'JsonContentHandler' => __DIR__ . '/includes/content/JsonContentHandler.php',
        'KkConverter' => __DIR__ . '/languages/classes/LanguageKk.php',
        'KuConverter' => __DIR__ . '/languages/classes/LanguageKu.php',
-       'LBFactory' => __DIR__ . '/includes/db/LBFactory.php',
-       'LBFactoryFake' => __DIR__ . '/includes/db/LBFactory.php',
-       'LBFactoryMulti' => __DIR__ . '/includes/db/LBFactoryMulti.php',
-       'LBFactorySimple' => __DIR__ . '/includes/db/LBFactory.php',
-       'LBFactorySingle' => __DIR__ . '/includes/db/LBFactorySingle.php',
+       'LBFactory' => __DIR__ . '/includes/db/loadbalancer/LBFactory.php',
+       'LBFactoryFake' => __DIR__ . '/includes/db/loadbalancer/LBFactoryFake.php',
+       'LBFactoryMulti' => __DIR__ . '/includes/db/loadbalancer/LBFactoryMulti.php',
+       'LBFactorySimple' => __DIR__ . '/includes/db/loadbalancer/LBFactorySimple.php',
+       'LBFactorySingle' => __DIR__ . '/includes/db/loadbalancer/LBFactorySingle.php',
        'LCStore' => __DIR__ . '/includes/cache/LocalisationCache.php',
        'LCStoreCDB' => __DIR__ . '/includes/cache/LocalisationCache.php',
        'LCStoreDB' => __DIR__ . '/includes/cache/LocalisationCache.php',
@@ -686,11 +679,11 @@ $wgAutoloadLocalClasses = array(
        'ListDuplicatedFilesPage' => __DIR__ . '/includes/specials/SpecialListDuplicatedFiles.php',
        'ListVariants' => __DIR__ . '/maintenance/language/listVariants.php',
        'ListredirectsPage' => __DIR__ . '/includes/specials/SpecialListredirects.php',
-       'LoadBalancer' => __DIR__ . '/includes/db/LoadBalancer.php',
-       'LoadBalancerSingle' => __DIR__ . '/includes/db/LBFactorySingle.php',
-       'LoadMonitor' => __DIR__ . '/includes/db/LoadMonitor.php',
-       'LoadMonitorMySQL' => __DIR__ . '/includes/db/LoadMonitorMySQL.php',
-       'LoadMonitorNull' => __DIR__ . '/includes/db/LoadMonitor.php',
+       'LoadBalancer' => __DIR__ . '/includes/db/loadbalancer/LoadBalancer.php',
+       'LoadBalancerSingle' => __DIR__ . '/includes/db/loadbalancer/LBFactorySingle.php',
+       'LoadMonitor' => __DIR__ . '/includes/db/loadbalancer/LoadMonitor.php',
+       'LoadMonitorMySQL' => __DIR__ . '/includes/db/loadbalancer/LoadMonitorMySQL.php',
+       'LoadMonitorNull' => __DIR__ . '/includes/db/loadbalancer/LoadMonitor.php',
        'LocalFile' => __DIR__ . '/includes/filerepo/file/LocalFile.php',
        'LocalFileDeleteBatch' => __DIR__ . '/includes/filerepo/file/LocalFile.php',
        'LocalFileMoveBatch' => __DIR__ . '/includes/filerepo/file/LocalFile.php',
index 0f0d93c..6153045 100644 (file)
@@ -27,6 +27,7 @@
                "psr/log": "1.0.0",
                "wikimedia/assert": "0.2.2",
                "wikimedia/cdb": "1.3.0",
+               "wikimedia/cldr-plural-rule-parser": "1.0.0",
                "wikimedia/composer-merge-plugin": "1.2.1",
                "wikimedia/ip-set": "1.0.1",
                "wikimedia/utfnormal": "1.0.3",
diff --git a/includes/FileContentsHasher.php b/includes/FileContentsHasher.php
deleted file mode 100644 (file)
index 67eb9d2..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-<?php
-/**
- * Generate hash digests of file contents to help with cache invalidation.
- *
- * 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
- */
-class FileContentsHasher {
-
-       /** @var BagOStuff */
-       protected $cache;
-
-       /** @var FileContentsHasher */
-       private static $instance;
-
-       /**
-        * Constructor.
-        */
-       public function __construct() {
-               $this->cache = ObjectCache::newAccelerator( 'hash' );
-       }
-
-       /**
-        * Get the singleton instance of this class.
-        *
-        * @return FileContentsHasher
-        */
-       public static function singleton() {
-               if ( !self::$instance ) {
-                       self::$instance = new self;
-               }
-
-               return self::$instance;
-       }
-
-       /**
-        * Get a hash of a file's contents, either by retrieving a previously-
-        * computed hash from the cache, or by computing a hash from the file.
-        *
-        * @private
-        * @param string $filePath Full path to the file.
-        * @param string $algo Name of selected hashing algorithm.
-        * @return string|bool Hash of file contents, or false if the file could not be read.
-        */
-       public function getFileContentsHashInternal( $filePath, $algo = 'md4' ) {
-               $mtime = MediaWiki\quietCall( 'filemtime', $filePath );
-               if ( $mtime === false ) {
-                       return false;
-               }
-
-               $cacheKey = wfGlobalCacheKey( __CLASS__, $filePath, $mtime, $algo );
-               $hash = $this->cache->get( $cacheKey );
-
-               if ( $hash ) {
-                       return $hash;
-               }
-
-               $contents = MediaWiki\quietCall( 'file_get_contents', $filePath );
-               if ( $contents === false ) {
-                       return false;
-               }
-
-               $hash = hash( $algo, $contents );
-               $this->cache->set( $cacheKey, $hash, 60 * 60 * 24 );  // 24h
-
-               return $hash;
-       }
-
-       /**
-        * Get a hash of the combined contents of one or more files, either by
-        * retrieving a previously-computed hash from the cache, or by computing
-        * a hash from the files.
-        *
-        * @param string|string[] $filePaths One or more file paths.
-        * @param string $algo Name of selected hashing algorithm.
-        * @return string|bool Hash of files' contents, or false if no file could not be read.
-        */
-       public static function getFileContentsHash( $filePaths, $algo = 'md4' ) {
-               $instance = self::singleton();
-
-               if ( !is_array( $filePaths ) ) {
-                       $filePaths = (array) $filePaths;
-               }
-
-               if ( count( $filePaths ) === 1 ) {
-                       return $instance->getFileContentsHashInternal( $filePaths[0], $algo );
-               }
-
-               sort( $filePaths );
-               $hashes = array_map( function ( $filePath ) use ( $instance, $algo ) {
-                       return $instance->getFileContentsHashInternal( $filePath, $algo ) ?: '';
-               }, $filePaths );
-
-               $hashes = implode( '', $hashes );
-               return $hashes ? hash( $algo, $hashes ) : false;
-       }
-}
index bc5a957..5d46b5e 100644 (file)
@@ -838,17 +838,19 @@ class CurlHttpRequest extends MWHttpRequest {
         * @return bool
         */
        public function canFollowRedirects() {
-               if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
-                       wfDebug( "Cannot follow redirects in safe mode\n" );
-                       return false;
-               }
-
                $curlVersionInfo = curl_version();
                if ( $curlVersionInfo['version_number'] < 0x071304 ) {
                        wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
                        return false;
                }
 
+               if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
+                       if ( strval( ini_get( 'open_basedir' ) ) !== '' || wfIniGetBool( 'safe_mode' ) ) {
+                               wfDebug( "Cannot follow redirects in safe mode\n" );
+                               return false;
+                       }
+               }
+
                return true;
        }
 }
index 276e84a..f5b2350 100644 (file)
@@ -23,6 +23,7 @@
 use Cdb\Exception as CdbException;
 use Cdb\Reader as CdbReader;
 use Cdb\Writer as CdbWriter;
+use CLDRPluralRuleParser\Evaluator;
 
 /**
  * Class for caching the contents of localisation files, Messages*.php
@@ -576,7 +577,7 @@ class LocalisationCache {
                        return null;
                }
                try {
-                       $compiledRules = CLDRPluralRuleEvaluator::compile( $rules );
+                       $compiledRules = Evaluator::compile( $rules );
                } catch ( CLDRPluralRuleError $e ) {
                        wfDebugLog( 'l10n', $e->getMessage() );
 
diff --git a/includes/db/LBFactory.php b/includes/db/LBFactory.php
deleted file mode 100644 (file)
index cf522b2..0000000
+++ /dev/null
@@ -1,387 +0,0 @@
-<?php
-/**
- * Generator of database load balancing objects.
- *
- * 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 interface for generating database load balancers
- * @ingroup Database
- */
-abstract class LBFactory {
-       /** @var LBFactory */
-       private static $instance;
-
-       /**
-        * Disables all access to the load balancer, will cause all database access
-        * to throw a DBAccessError
-        */
-       public static function disableBackend() {
-               global $wgLBFactoryConf;
-               self::$instance = new LBFactoryFake( $wgLBFactoryConf );
-       }
-
-       /**
-        * Get an LBFactory instance
-        *
-        * @return LBFactory
-        */
-       public static function singleton() {
-               global $wgLBFactoryConf;
-
-               if ( is_null( self::$instance ) ) {
-                       $class = self::getLBFactoryClass( $wgLBFactoryConf );
-
-                       self::$instance = new $class( $wgLBFactoryConf );
-               }
-
-               return self::$instance;
-       }
-
-       /**
-        * Returns the LBFactory class to use and the load balancer configuration.
-        *
-        * @param array $config (e.g. $wgLBFactoryConf)
-        * @return string Class name
-        */
-       public static function getLBFactoryClass( array $config ) {
-               // 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 = $config['class'];
-
-               if ( isset( $bcClasses[$class] ) ) {
-                       $class = $bcClasses[$class];
-                       wfDeprecated(
-                               '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
-                               '1.23'
-                       );
-               }
-
-               return $class;
-       }
-
-       /**
-        * Shut down, close connections and destroy the cached instance.
-        */
-       public static function destroyInstance() {
-               if ( self::$instance ) {
-                       self::$instance->shutdown();
-                       self::$instance->forEachLBCallMethod( 'closeAll' );
-                       self::$instance = null;
-               }
-       }
-
-       /**
-        * Set the instance to be the given object
-        *
-        * @param LBFactory $instance
-        */
-       public static function setInstance( $instance ) {
-               self::destroyInstance();
-               self::$instance = $instance;
-       }
-
-       /**
-        * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
-        * @param array $conf
-        */
-       abstract public function __construct( array $conf );
-
-       /**
-        * Create a new load balancer object. The resulting object will be untracked,
-        * not chronology-protected, and the caller is responsible for cleaning it up.
-        *
-        * @param bool|string $wiki Wiki ID, or false for the current wiki
-        * @return LoadBalancer
-        */
-       abstract public function newMainLB( $wiki = false );
-
-       /**
-        * Get a cached (tracked) load balancer object.
-        *
-        * @param bool|string $wiki Wiki ID, or false for the current wiki
-        * @return LoadBalancer
-        */
-       abstract public function getMainLB( $wiki = false );
-
-       /**
-        * Create a new load balancer for external storage. The resulting object will be
-        * untracked, not chronology-protected, and the caller is responsible for
-        * cleaning it up.
-        *
-        * @param string $cluster External storage cluster, or false for core
-        * @param bool|string $wiki Wiki ID, or false for the current wiki
-        * @return LoadBalancer
-        */
-       abstract protected function newExternalLB( $cluster, $wiki = false );
-
-       /**
-        * Get a cached (tracked) load balancer for external storage
-        *
-        * @param string $cluster External storage cluster, or false for core
-        * @param bool|string $wiki Wiki ID, or false for the current wiki
-        * @return LoadBalancer
-        */
-       abstract public function &getExternalLB( $cluster, $wiki = false );
-
-       /**
-        * 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 callable $callback
-        * @param array $params
-        */
-       abstract public function forEachLB( $callback, array $params = array() );
-
-       /**
-        * Prepare all tracked load balancers for shutdown
-        * STUB
-        */
-       public function shutdown() {
-       }
-
-       /**
-        * Call a method of each tracked load balancer
-        *
-        * @param string $methodName
-        * @param array $args
-        */
-       private function forEachLBCallMethod( $methodName, array $args = array() ) {
-               $this->forEachLB( function ( LoadBalancer $loadBalancer, $methodName, array $args ) {
-                       call_user_func_array( array( $loadBalancer, $methodName ), $args );
-               }, array( $methodName, $args ) );
-       }
-
-       /**
-        * Commit on all connections. Done for two reasons:
-        * 1. To commit changes to the masters.
-        * 2. To release the snapshot on all connections, master and slave.
-        */
-       public function commitAll() {
-               $this->forEachLBCallMethod( 'commitAll' );
-       }
-
-       /**
-        * Commit changes on all master connections
-        */
-       public function commitMasterChanges() {
-               $this->forEachLBCallMethod( 'commitMasterChanges' );
-       }
-
-       /**
-        * Rollback changes on all master connections
-        * @since 1.23
-        */
-       public function rollbackMasterChanges() {
-               $this->forEachLBCallMethod( 'rollbackMasterChanges' );
-       }
-
-       /**
-        * Detemine if any master connection has pending changes.
-        * @since 1.23
-        * @return bool
-        */
-       public function hasMasterChanges() {
-               $ret = false;
-               $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) {
-                       $ret = $ret || $lb->hasMasterChanges();
-               } );
-               return $ret;
-       }
-}
-
-/**
- * A simple single-master LBFactory that gets its configuration from the b/c globals
- */
-class LBFactorySimple extends LBFactory {
-       /** @var LoadBalancer */
-       private $mainLB;
-
-       /** @var LoadBalancer[] */
-       private $extLBs = array();
-
-       /** @var ChronologyProtector */
-       private $chronProt;
-
-       public function __construct( array $conf ) {
-               $this->chronProt = new ChronologyProtector;
-       }
-
-       /**
-        * @param bool|string $wiki
-        * @return LoadBalancer
-        */
-       public function newMainLB( $wiki = false ) {
-               global $wgDBservers;
-               if ( $wgDBservers ) {
-                       $servers = $wgDBservers;
-               } else {
-                       global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
-                       global $wgDBssl, $wgDBcompress;
-
-                       $flags = DBO_DEFAULT;
-                       if ( $wgDebugDumpSql ) {
-                               $flags |= DBO_DEBUG;
-                       }
-                       if ( $wgDBssl ) {
-                               $flags |= DBO_SSL;
-                       }
-                       if ( $wgDBcompress ) {
-                               $flags |= DBO_COMPRESS;
-                       }
-
-                       $servers = array( array(
-                               'host' => $wgDBserver,
-                               'user' => $wgDBuser,
-                               'password' => $wgDBpassword,
-                               'dbname' => $wgDBname,
-                               'type' => $wgDBtype,
-                               'load' => 1,
-                               'flags' => $flags
-                       ) );
-               }
-
-               return new LoadBalancer( array(
-                       'servers' => $servers,
-               ) );
-       }
-
-       /**
-        * @param bool|string $wiki
-        * @return LoadBalancer
-        */
-       public function getMainLB( $wiki = false ) {
-               if ( !isset( $this->mainLB ) ) {
-                       $this->mainLB = $this->newMainLB( $wiki );
-                       $this->mainLB->parentInfo( array( 'id' => 'main' ) );
-                       $this->chronProt->initLB( $this->mainLB );
-               }
-
-               return $this->mainLB;
-       }
-
-       /**
-        * @throws MWException
-        * @param string $cluster
-        * @param bool|string $wiki
-        * @return LoadBalancer
-        */
-       protected function newExternalLB( $cluster, $wiki = false ) {
-               global $wgExternalServers;
-               if ( !isset( $wgExternalServers[$cluster] ) ) {
-                       throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
-               }
-
-               return new LoadBalancer( array(
-                       'servers' => $wgExternalServers[$cluster]
-               ) );
-       }
-
-       /**
-        * @param string $cluster
-        * @param bool|string $wiki
-        * @return array
-        */
-       public 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];
-       }
-
-       /**
-        * 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 callable $callback
-        * @param array $params
-        */
-       public function forEachLB( $callback, array $params = array() ) {
-               if ( isset( $this->mainLB ) ) {
-                       call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
-               }
-               foreach ( $this->extLBs as $lb ) {
-                       call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
-               }
-       }
-
-       public function shutdown() {
-               if ( $this->mainLB ) {
-                       $this->chronProt->shutdownLB( $this->mainLB );
-               }
-               foreach ( $this->extLBs as $extLB ) {
-                       $this->chronProt->shutdownLB( $extLB );
-               }
-               $this->chronProt->shutdown();
-               $this->commitMasterChanges();
-       }
-}
-
-/**
- * LBFactory class that throws an error on any attempt to use it.
- * This will typically be done via wfGetDB().
- * Call LBFactory::disableBackend() to start using this, and
- * LBFactory::enableBackend() to return to normal behavior
- */
-class LBFactoryFake extends LBFactory {
-       public function __construct( array $conf ) {
-       }
-
-       public function newMainLB( $wiki = false ) {
-               throw new DBAccessError;
-       }
-
-       public function getMainLB( $wiki = false ) {
-               throw new DBAccessError;
-       }
-
-       protected function newExternalLB( $cluster, $wiki = false ) {
-               throw new DBAccessError;
-       }
-
-       public function &getExternalLB( $cluster, $wiki = false ) {
-               throw new DBAccessError;
-       }
-
-       public function forEachLB( $callback, array $params = array() ) {
-       }
-}
-
-/**
- * Exception class for attempted DB access
- */
-class DBAccessError extends MWException {
-       public function __construct() {
-               parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
-                       "This is not allowed." );
-       }
-}
diff --git a/includes/db/LBFactoryMulti.php b/includes/db/LBFactoryMulti.php
deleted file mode 100644 (file)
index 92fbccd..0000000
+++ /dev/null
@@ -1,399 +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 LBFactoryMulti extends LBFactory {
-       // Required settings
-
-       /** @var array A map of database names to section names */
-       private $sectionsByDB;
-
-       /**
-        * @var array A 2-d map. For each section, gives a map of server names to
-        * load ratios
-        */
-       private $sectionLoads;
-
-       /**
-        * @var array A server info associative array as documented for
-        * $wgDBservers. The host, hostName and load entries will be
-        * overridden
-        */
-       private $serverTemplate;
-
-       // Optional settings
-
-       /** @var array A 3-d map giving server load ratios for each section and group */
-       private $groupLoadsBySection = array();
-
-       /** @var array A 3-d map giving server load ratios by DB name */
-       private $groupLoadsByDB = array();
-
-       /** @var array A map of hostname to IP address */
-       private $hostsByName = array();
-
-       /** @var array A map of external storage cluster name to server load map */
-       private $externalLoads = array();
-
-       /**
-        * @var array A set of server info keys overriding serverTemplate for
-        * external storage
-        */
-       private $externalTemplateOverrides;
-
-       /**
-        * @var array A 2-d map overriding serverTemplate and
-        * externalTemplateOverrides on a server-by-server basis. Applies to both
-        * core and external storage
-        */
-       private $templateOverridesByServer;
-
-       /** @var array A 2-d map overriding the server info by external storage cluster */
-       private $templateOverridesByCluster;
-
-       /** @var array An override array for all master servers */
-       private $masterTemplateOverrides;
-
-       /**
-        * @var array|bool A map of section name to read-only message. Missing or
-        * false for read/write
-        */
-       private $readOnlyBySection = array();
-
-       // Other stuff
-
-       /** @var array Load balancer factory configuration */
-       private $conf;
-
-       /** @var LoadBalancer[] */
-       private $mainLBs = array();
-
-       /** @var LoadBalancer[] */
-       private $extLBs = array();
-
-       /** @var string */
-       private $lastWiki;
-
-       /** @var string */
-       private $lastSection;
-
-       /**
-        * @param array $conf
-        * @throws MWException
-        */
-       public function __construct( array $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 bool|string $wiki
-        * @return string
-        */
-       private 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 bool|string $wiki
-        * @return LoadBalancer
-        */
-       public 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 bool|string $wiki
-        * @return LoadBalancer
-        */
-       public function getMainLB( $wiki = false ) {
-               $section = $this->getSectionForWiki( $wiki );
-               if ( !isset( $this->mainLBs[$section] ) ) {
-                       $lb = $this->newMainLB( $wiki );
-                       $lb->parentInfo( array( 'id' => "main-$section" ) );
-                       $this->chronProt->initLB( $lb );
-                       $this->mainLBs[$section] = $lb;
-               }
-
-               return $this->mainLBs[$section];
-       }
-
-       /**
-        * @param string $cluster
-        * @param bool|string $wiki
-        * @throws MWException
-        * @return LoadBalancer
-        */
-       protected 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 string $cluster External storage cluster, or false for core
-        * @param bool|string $wiki Wiki ID, or false for the current wiki
-        * @return LoadBalancer
-        */
-       public 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 array $template
-        * @param array $loads
-        * @param array $groupLoads
-        * @return LoadBalancer
-        */
-       private function newLoadBalancer( $template, $loads, $groupLoads ) {
-               $servers = $this->makeServerArray( $template, $loads, $groupLoads );
-               $lb = new LoadBalancer( array(
-                       'servers' => $servers,
-               ) );
-
-               return $lb;
-       }
-
-       /**
-        * Make a server array as expected by LoadBalancer::__construct, using a template and load array
-        *
-        * @param array $template
-        * @param array $loads
-        * @param array $groupLoads
-        * @return array
-        */
-       private 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 array $groupLoads
-        * @return array
-        */
-       private 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 bool|string $wiki
-        * @return array
-        */
-       private 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 callable $callback
-        * @param array $params
-        */
-       public function forEachLB( $callback, array $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 ) );
-               }
-       }
-
-       public 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
deleted file mode 100644 (file)
index a41dadf..0000000
+++ /dev/null
@@ -1,112 +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 LBFactorySingle extends LBFactory {
-       /** @var LoadBalancerSingle */
-       private $lb;
-
-       /**
-        * @param array $conf An associative array with one member:
-        *  - connection: The DatabaseBase connection object
-        */
-       public function __construct( array $conf ) {
-               $this->lb = new LoadBalancerSingle( $conf );
-       }
-
-       /**
-        * @param bool|string $wiki
-        * @return LoadBalancerSingle
-        */
-       public function newMainLB( $wiki = false ) {
-               return $this->lb;
-       }
-
-       /**
-        * @param bool|string $wiki
-        * @return LoadBalancerSingle
-        */
-       public function getMainLB( $wiki = false ) {
-               return $this->lb;
-       }
-
-       /**
-        * @param string $cluster External storage cluster, or false for core
-        * @param bool|string $wiki Wiki ID, or false for the current wiki
-        * @return LoadBalancerSingle
-        */
-       protected function newExternalLB( $cluster, $wiki = false ) {
-               return $this->lb;
-       }
-
-       /**
-        * @param string $cluster External storage cluster, or false for core
-        * @param bool|string $wiki Wiki ID, or false for the current wiki
-        * @return LoadBalancerSingle
-        */
-       public function &getExternalLB( $cluster, $wiki = false ) {
-               return $this->lb;
-       }
-
-       /**
-        * @param string|callable $callback
-        * @param array $params
-        */
-       public function forEachLB( $callback, array $params = array() ) {
-               call_user_func_array( $callback, array_merge( array( $this->lb ), $params ) );
-       }
-}
-
-/**
- * Helper class for LBFactorySingle.
- */
-class LoadBalancerSingle extends LoadBalancer {
-       /** @var DatabaseBase */
-       private $db;
-
-       /**
-        * @param array $params
-        */
-       public function __construct( array $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 string $server
-        * @param bool $dbNameOverride
-        *
-        * @return DatabaseBase
-        */
-       protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
-               return $this->db;
-       }
-}
diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php
deleted file mode 100644 (file)
index 52dca08..0000000
+++ /dev/null
@@ -1,1273 +0,0 @@
-<?php
-/**
- * Database load balancing.
- *
- * 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
- */
-
-/**
- * Database load balancing object
- *
- * @todo document
- * @ingroup Database
- */
-class LoadBalancer {
-       /** @var array[] Map of (server index => server config array) */
-       private $mServers;
-       /** @var array[] Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) */
-       private $mConns;
-       /** @var array Map of (server index => weight) */
-       private $mLoads;
-       /** @var array[] Map of (group => server index => weight) */
-       private $mGroupLoads;
-       /** @var bool Whether to disregard slave lag as a factor in slave selection */
-       private $mAllowLagged;
-       /** @var integer Seconds to spend waiting on slave lag to resolve */
-       private $mWaitTimeout;
-
-       /** @var array LBFactory information */
-       private $mParentInfo;
-       /** @var string The LoadMonitor subclass name */
-       private $mLoadMonitorClass;
-       /** @var LoadMonitor */
-       private $mLoadMonitor;
-
-       /** @var bool|DatabaseBase Database connection that caused a problem */
-       private $mErrorConnection;
-       /** @var integer The generic (not query grouped) slave index (of $mServers) */
-       private $mReadIndex;
-       /** @var bool|DBMasterPos False if not set */
-       private $mWaitForPos;
-       /** @var bool Whether the generic reader fell back to a lagged slave */
-       private $mLaggedSlaveMode;
-       /** @var string The last DB selection or connection error */
-       private $mLastError = 'Unknown error';
-       /** @var integer Total connections opened */
-       private $connsOpened = 0;
-
-       /** @var integer Warn when this many connection are held */
-       const CONN_HELD_WARN_THRESHOLD = 10;
-
-       /**
-        * @param array $params Array with keys:
-        *   servers           Required. Array of server info structures.
-        *   loadMonitor       Name of a class used to fetch server lag and load.
-        * @throws MWException
-        */
-       public function __construct( array $params ) {
-               if ( !isset( $params['servers'] ) ) {
-                       throw new MWException( __CLASS__ . ': missing servers parameter' );
-               }
-               $this->mServers = $params['servers'];
-               $this->mWaitTimeout = 10;
-
-               $this->mReadIndex = -1;
-               $this->mWriteIndex = -1;
-               $this->mConns = array(
-                       'local' => array(),
-                       'foreignUsed' => array(),
-                       'foreignFree' => array() );
-               $this->mLoads = array();
-               $this->mWaitForPos = false;
-               $this->mLaggedSlaveMode = false;
-               $this->mErrorConnection = false;
-               $this->mAllowLagged = false;
-
-               if ( isset( $params['loadMonitor'] ) ) {
-                       $this->mLoadMonitorClass = $params['loadMonitor'];
-               } else {
-                       $master = reset( $params['servers'] );
-                       if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
-                               $this->mLoadMonitorClass = 'LoadMonitorMySQL';
-                       } else {
-                               $this->mLoadMonitorClass = 'LoadMonitorNull';
-                       }
-               }
-
-               foreach ( $params['servers'] as $i => $server ) {
-                       $this->mLoads[$i] = $server['load'];
-                       if ( isset( $server['groupLoads'] ) ) {
-                               foreach ( $server['groupLoads'] as $group => $ratio ) {
-                                       if ( !isset( $this->mGroupLoads[$group] ) ) {
-                                               $this->mGroupLoads[$group] = array();
-                                       }
-                                       $this->mGroupLoads[$group][$i] = $ratio;
-                               }
-                       }
-               }
-       }
-
-       /**
-        * Get a LoadMonitor instance
-        *
-        * @return LoadMonitor
-        */
-       private function getLoadMonitor() {
-               if ( !isset( $this->mLoadMonitor ) ) {
-                       $class = $this->mLoadMonitorClass;
-                       $this->mLoadMonitor = new $class( $this );
-               }
-
-               return $this->mLoadMonitor;
-       }
-
-       /**
-        * Get or set arbitrary data used by the parent object, usually an LBFactory
-        * @param mixed $x
-        * @return mixed
-        */
-       public function parentInfo( $x = null ) {
-               return wfSetVar( $this->mParentInfo, $x );
-       }
-
-       /**
-        * Given an array of non-normalised probabilities, this function will select
-        * an element and return the appropriate key
-        *
-        * @deprecated since 1.21, use ArrayUtils::pickRandom()
-        *
-        * @param array $weights
-        * @return bool|int|string
-        */
-       public function pickRandom( array $weights ) {
-               return ArrayUtils::pickRandom( $weights );
-       }
-
-       /**
-        * @param array $loads
-        * @param bool|string $wiki Wiki to get non-lagged for
-        * @param float $maxLag Restrict the maximum allowed lag to this many seconds
-        * @return bool|int|string
-        */
-       private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = INF ) {
-               $lags = $this->getLagTimes( $wiki );
-
-               # Unset excessively lagged servers
-               foreach ( $lags as $i => $lag ) {
-                       if ( $i != 0 ) {
-                               $maxServerLag = $maxLag;
-                               if ( isset( $this->mServers[$i]['max lag'] ) ) {
-                                       $maxServerLag = min( $maxServerLag, $this->mServers[$i]['max lag'] );
-                               }
-                               if ( $lag === false ) {
-                                       wfDebugLog( 'replication', "Server #$i is not replicating" );
-                                       unset( $loads[$i] );
-                               } elseif ( $lag > $maxServerLag ) {
-                                       wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)" );
-                                       unset( $loads[$i] );
-                               }
-                       }
-               }
-
-               # Find out if all the slaves with non-zero load are lagged
-               $sum = 0;
-               foreach ( $loads as $load ) {
-                       $sum += $load;
-               }
-               if ( $sum == 0 ) {
-                       # No appropriate DB servers except maybe the master and some slaves with zero load
-                       # Do NOT use the master
-                       # Instead, this function will return false, triggering read-only mode,
-                       # and a lagged slave will be used instead.
-                       return false;
-               }
-
-               if ( count( $loads ) == 0 ) {
-                       return false;
-               }
-
-               #wfDebugLog( 'connect', var_export( $loads, true ) );
-
-               # Return a random representative of the remainder
-               return ArrayUtils::pickRandom( $loads );
-       }
-
-       /**
-        * Get the index of the reader connection, which may be a slave
-        * This takes into account load ratios and lag times. It should
-        * always return a consistent index during a given invocation
-        *
-        * Side effect: opens connections to databases
-        * @param string|bool $group Query group, or false for the generic reader
-        * @param string|bool $wiki Wiki ID, or false for the current wiki
-        * @throws MWException
-        * @return bool|int|string
-        */
-       public function getReaderIndex( $group = false, $wiki = false ) {
-               global $wgDBtype;
-
-               # @todo FIXME: For now, only go through all this for mysql databases
-               if ( $wgDBtype != 'mysql' ) {
-                       return $this->getWriterIndex();
-               }
-
-               if ( count( $this->mServers ) == 1 ) {
-                       # Skip the load balancing if there's only one server
-                       return 0;
-               } elseif ( $group === false && $this->mReadIndex >= 0 ) {
-                       # Shortcut if generic reader exists already
-                       return $this->mReadIndex;
-               }
-
-               # Find the relevant load array
-               if ( $group !== false ) {
-                       if ( isset( $this->mGroupLoads[$group] ) ) {
-                               $nonErrorLoads = $this->mGroupLoads[$group];
-                       } else {
-                               # No loads for this group, return false and the caller can use some other group
-                               wfDebug( __METHOD__ . ": no loads for group $group\n" );
-
-                               return false;
-                       }
-               } else {
-                       $nonErrorLoads = $this->mLoads;
-               }
-
-               if ( !count( $nonErrorLoads ) ) {
-                       throw new MWException( "Empty server array given to LoadBalancer" );
-               }
-
-               # Scale the configured load ratios according to the dynamic load (if the load monitor supports it)
-               $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $wiki );
-
-               $laggedSlaveMode = false;
-
-               # No server found yet
-               $i = false;
-               # First try quickly looking through the available servers for a server that
-               # meets our criteria
-               $currentLoads = $nonErrorLoads;
-               while ( count( $currentLoads ) ) {
-                       if ( $this->mAllowLagged || $laggedSlaveMode ) {
-                               $i = ArrayUtils::pickRandom( $currentLoads );
-                       } else {
-                               $i = false;
-                               if ( $this->mWaitForPos && $this->mWaitForPos->asOfTime() ) {
-                                       # ChronologyProtecter causes mWaitForPos to be set via sessions.
-                                       # This triggers doWait() after connect, so it's especially good to
-                                       # avoid lagged servers so as to avoid just blocking in that method.
-                                       $ago = microtime( true ) - $this->mWaitForPos->asOfTime();
-                                       # Aim for <= 1 second of waiting (being too picky can backfire)
-                                       $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago + 1 );
-                               }
-                               if ( $i === false ) {
-                                       # Any server with less lag than it's 'max lag' param is preferable
-                                       $i = $this->getRandomNonLagged( $currentLoads, $wiki );
-                               }
-                               if ( $i === false && count( $currentLoads ) != 0 ) {
-                                       # All slaves lagged. Switch to read-only mode
-                                       wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" );
-                                       $i = ArrayUtils::pickRandom( $currentLoads );
-                                       $laggedSlaveMode = true;
-                               }
-                       }
-
-                       if ( $i === false ) {
-                               # pickRandom() returned false
-                               # This is permanent and means the configuration or the load monitor
-                               # wants us to return false.
-                               wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false" );
-
-                               return false;
-                       }
-
-                       $serverName = $this->getServerName( $i );
-                       wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: $serverName..." );
-
-                       $conn = $this->openConnection( $i, $wiki );
-                       if ( !$conn ) {
-                               wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki" );
-                               unset( $nonErrorLoads[$i] );
-                               unset( $currentLoads[$i] );
-                               $i = false;
-                               continue;
-                       }
-
-                       // Decrement reference counter, we are finished with this connection.
-                       // It will be incremented for the caller later.
-                       if ( $wiki !== false ) {
-                               $this->reuseConnection( $conn );
-                       }
-
-                       # Return this server
-                       break;
-               }
-
-               # If all servers were down, quit now
-               if ( !count( $nonErrorLoads ) ) {
-                       wfDebugLog( 'connect', "All servers down" );
-               }
-
-               if ( $i !== false ) {
-                       # Slave connection successful
-                       # Wait for the session master pos for a short time
-                       if ( $this->mWaitForPos && $i > 0 ) {
-                               if ( !$this->doWait( $i ) ) {
-                                       $this->mServers[$i]['slave pos'] = $conn->getSlavePos();
-                               }
-                       }
-                       if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) {
-                               $this->mReadIndex = $i;
-                               # Record if the generic reader index is in "lagged slave" mode
-                               if ( $laggedSlaveMode ) {
-                                       $this->mLaggedSlaveMode = true;
-                               }
-                       }
-                       $serverName = $this->getServerName( $i );
-                       wfDebug( __METHOD__ . ": using server $serverName for group '$group'\n" );
-               }
-
-               return $i;
-       }
-
-       /**
-        * Set the master wait position
-        * If a DB_SLAVE connection has been opened already, waits
-        * Otherwise sets a variable telling it to wait if such a connection is opened
-        * @param DBMasterPos $pos
-        */
-       public function waitFor( $pos ) {
-               $this->mWaitForPos = $pos;
-               $i = $this->mReadIndex;
-
-               if ( $i > 0 ) {
-                       if ( !$this->doWait( $i ) ) {
-                               $this->mServers[$i]['slave pos'] = $this->getAnyOpenConnection( $i )->getSlavePos();
-                               $this->mLaggedSlaveMode = true;
-                       }
-               }
-       }
-
-       /**
-        * Set the master wait position and wait for a "generic" slave to catch up to it
-        *
-        * This can be used a faster proxy for waitForAll()
-        *
-        * @param DBMasterPos $pos
-        * @param int $timeout Max seconds to wait; default is mWaitTimeout
-        * @return bool Success (able to connect and no timeouts reached)
-        * @since 1.26
-        */
-       public function waitForOne( $pos, $timeout = null ) {
-               $this->mWaitForPos = $pos;
-
-               $i = $this->mReadIndex;
-               if ( $i <= 0 ) {
-                       // Pick a generic slave if there isn't one yet
-                       $readLoads = $this->mLoads;
-                       unset( $readLoads[$this->getWriterIndex()] ); // slaves only
-                       $readLoads = array_filter( $readLoads ); // with non-zero load
-                       $i = ArrayUtils::pickRandom( $readLoads );
-               }
-
-               if ( $i > 0 ) {
-                       $ok = $this->doWait( $i, true, $timeout );
-               } else {
-                       $ok = true; // no applicable loads
-               }
-
-               return $ok;
-       }
-
-       /**
-        * Set the master wait position and wait for ALL slaves to catch up to it
-        * @param DBMasterPos $pos
-        * @param int $timeout Max seconds to wait; default is mWaitTimeout
-        * @return bool Success (able to connect and no timeouts reached)
-        */
-       public function waitForAll( $pos, $timeout = null ) {
-               $this->mWaitForPos = $pos;
-               $serverCount = count( $this->mServers );
-
-               $ok = true;
-               for ( $i = 1; $i < $serverCount; $i++ ) {
-                       if ( $this->mLoads[$i] > 0 ) {
-                               $ok = $this->doWait( $i, true, $timeout ) && $ok;
-                       }
-               }
-
-               return $ok;
-       }
-
-       /**
-        * Get any open connection to a given server index, local or foreign
-        * Returns false if there is no connection open
-        *
-        * @param int $i
-        * @return DatabaseBase|bool False on failure
-        */
-       public function getAnyOpenConnection( $i ) {
-               foreach ( $this->mConns as $conns ) {
-                       if ( !empty( $conns[$i] ) ) {
-                               return reset( $conns[$i] );
-                       }
-               }
-
-               return false;
-       }
-
-       /**
-        * Wait for a given slave to catch up to the master pos stored in $this
-        * @param int $index Server index
-        * @param bool $open Check the server even if a new connection has to be made
-        * @param int $timeout Max seconds to wait; default is mWaitTimeout
-        * @return bool
-        */
-       protected function doWait( $index, $open = false, $timeout = null ) {
-               $close = false; // close the connection afterwards
-
-               # Find a connection to wait on, creating one if needed and allowed
-               $conn = $this->getAnyOpenConnection( $index );
-               if ( !$conn ) {
-                       if ( !$open ) {
-                               wfDebug( __METHOD__ . ": no connection open\n" );
-
-                               return false;
-                       } else {
-                               $conn = $this->openConnection( $index, '' );
-                               if ( !$conn ) {
-                                       wfDebug( __METHOD__ . ": failed to open connection\n" );
-
-                                       return false;
-                               }
-                               // Avoid connection spam in waitForAll() when connections
-                               // are made just for the sake of doing this lag check.
-                               $close = true;
-                       }
-               }
-
-               wfDebug( __METHOD__ . ": Waiting for slave #$index to catch up...\n" );
-               $timeout = $timeout ?: $this->mWaitTimeout;
-               $result = $conn->masterPosWait( $this->mWaitForPos, $timeout );
-
-               if ( $result == -1 || is_null( $result ) ) {
-                       # Timed out waiting for slave, use master instead
-                       $server = $server = $this->getServerName( $index );
-                       $msg = __METHOD__ . ": Timed out waiting on $server pos {$this->mWaitForPos}";
-                       wfDebug( "$msg\n" );
-                       wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
-                       $ok = false;
-               } else {
-                       wfDebug( __METHOD__ . ": Done\n" );
-                       $ok = true;
-               }
-
-               if ( $close ) {
-                       $this->closeConnection( $conn );
-               }
-
-               return $ok;
-       }
-
-       /**
-        * Get a connection by index
-        * This is the main entry point for this class.
-        *
-        * @param int $i Server index
-        * @param array|string|bool $groups Query group(s), or false for the generic reader
-        * @param string|bool $wiki Wiki ID, or false for the current wiki
-        *
-        * @throws MWException
-        * @return DatabaseBase
-        */
-       public function getConnection( $i, $groups = array(), $wiki = false ) {
-               if ( $i === null || $i === false ) {
-                       throw new MWException( 'Attempt to call ' . __METHOD__ .
-                               ' with invalid server index' );
-               }
-
-               if ( $wiki === wfWikiID() ) {
-                       $wiki = false;
-               }
-
-               $groups = ( $groups === false || $groups === array() )
-                       ? array( false ) // check one "group": the generic pool
-                       : (array)$groups;
-
-               $masterOnly = ( $i == DB_MASTER || $i == $this->getWriterIndex() );
-               $oldConnsOpened = $this->connsOpened; // connections open now
-
-               if ( $i == DB_MASTER ) {
-                       $i = $this->getWriterIndex();
-               } else {
-                       # Try to find an available server in any the query groups (in order)
-                       foreach ( $groups as $group ) {
-                               $groupIndex = $this->getReaderIndex( $group, $wiki );
-                               if ( $groupIndex !== false ) {
-                                       $i = $groupIndex;
-                                       break;
-                               }
-                       }
-               }
-
-               # Operation-based index
-               if ( $i == DB_SLAVE ) {
-                       $this->mLastError = 'Unknown error'; // reset error string
-                       # Try the general server pool if $groups are unavailable.
-                       $i = in_array( false, $groups, true )
-                               ? false // don't bother with this if that is what was tried above
-                               : $this->getReaderIndex( false, $wiki );
-                       # Couldn't find a working server in getReaderIndex()?
-                       if ( $i === false ) {
-                               $this->mLastError = 'No working slave server: ' . $this->mLastError;
-
-                               return $this->reportConnectionError();
-                       }
-               }
-
-               # Now we have an explicit index into the servers array
-               $conn = $this->openConnection( $i, $wiki );
-               if ( !$conn ) {
-                       return $this->reportConnectionError();
-               }
-
-               # Profile any new connections that happen
-               if ( $this->connsOpened > $oldConnsOpened ) {
-                       $host = $conn->getServer();
-                       $dbname = $conn->getDBname();
-                       $trxProf = Profiler::instance()->getTransactionProfiler();
-                       $trxProf->recordConnection( $host, $dbname, $masterOnly );
-               }
-
-               return $conn;
-       }
-
-       /**
-        * Mark a foreign connection as being available for reuse under a different
-        * DB name or prefix. This mechanism is reference-counted, and must be called
-        * the same number of times as getConnection() to work.
-        *
-        * @param DatabaseBase $conn
-        * @throws MWException
-        */
-       public function reuseConnection( $conn ) {
-               $serverIndex = $conn->getLBInfo( 'serverIndex' );
-               $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
-               if ( $serverIndex === null || $refCount === null ) {
-                       wfDebug( __METHOD__ . ": this connection was not opened as a foreign connection\n" );
-
-                       /**
-                        * This can happen in code like:
-                        *   foreach ( $dbs as $db ) {
-                        *     $conn = $lb->getConnection( DB_SLAVE, array(), $db );
-                        *     ...
-                        *     $lb->reuseConnection( $conn );
-                        *   }
-                        * When a connection to the local DB is opened in this way, reuseConnection()
-                        * should be ignored
-                        */
-
-                       return;
-               }
-
-               $dbName = $conn->getDBname();
-               $prefix = $conn->tablePrefix();
-               if ( strval( $prefix ) !== '' ) {
-                       $wiki = "$dbName-$prefix";
-               } else {
-                       $wiki = $dbName;
-               }
-               if ( $this->mConns['foreignUsed'][$serverIndex][$wiki] !== $conn ) {
-                       throw new MWException( __METHOD__ . ": connection not found, has " .
-                               "the connection been freed already?" );
-               }
-               $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
-               if ( $refCount <= 0 ) {
-                       $this->mConns['foreignFree'][$serverIndex][$wiki] = $conn;
-                       unset( $this->mConns['foreignUsed'][$serverIndex][$wiki] );
-                       wfDebug( __METHOD__ . ": freed connection $serverIndex/$wiki\n" );
-               } else {
-                       wfDebug( __METHOD__ . ": reference count for $serverIndex/$wiki reduced to $refCount\n" );
-               }
-       }
-
-       /**
-        * Get a database connection handle reference
-        *
-        * The handle's methods wrap simply wrap those of a DatabaseBase handle
-        *
-        * @see LoadBalancer::getConnection() for parameter information
-        *
-        * @param int $db
-        * @param array|string|bool $groups Query group(s), or false for the generic reader
-        * @param string|bool $wiki Wiki ID, or false for the current wiki
-        * @return DBConnRef
-        */
-       public function getConnectionRef( $db, $groups = array(), $wiki = false ) {
-               return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
-       }
-
-       /**
-        * Get a database connection handle reference without connecting yet
-        *
-        * The handle's methods wrap simply wrap those of a DatabaseBase handle
-        *
-        * @see LoadBalancer::getConnection() for parameter information
-        *
-        * @param int $db
-        * @param array|string|bool $groups Query group(s), or false for the generic reader
-        * @param string|bool $wiki Wiki ID, or false for the current wiki
-        * @return DBConnRef
-        */
-       public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) {
-               return new DBConnRef( $this, array( $db, $groups, $wiki ) );
-       }
-
-       /**
-        * Open a connection to the server given by the specified index
-        * Index must be an actual index into the array.
-        * If the server is already open, returns it.
-        *
-        * On error, returns false, and the connection which caused the
-        * error will be available via $this->mErrorConnection.
-        *
-        * @param int $i Server index
-        * @param string|bool $wiki Wiki ID, or false for the current wiki
-        * @return DatabaseBase
-        *
-        * @access private
-        */
-       public function openConnection( $i, $wiki = false ) {
-               if ( $wiki !== false ) {
-                       $conn = $this->openForeignConnection( $i, $wiki );
-               } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
-                       $conn = $this->mConns['local'][$i][0];
-               } else {
-                       $server = $this->mServers[$i];
-                       $server['serverIndex'] = $i;
-                       $conn = $this->reallyOpenConnection( $server, false );
-                       $serverName = $this->getServerName( $i );
-                       if ( $conn->isOpen() ) {
-                               wfDebug( "Connected to database $i at $serverName\n" );
-                               $this->mConns['local'][$i][0] = $conn;
-                       } else {
-                               wfDebug( "Failed to connect to database $i at $serverName\n" );
-                               $this->mErrorConnection = $conn;
-                               $conn = false;
-                       }
-               }
-
-               if ( $conn && !$conn->isOpen() ) {
-                       // Connection was made but later unrecoverably lost for some reason.
-                       // Do not return a handle that will just throw exceptions on use,
-                       // but let the calling code (e.g. getReaderIndex) try another server.
-                       // See DatabaseMyslBase::ping() for how this can happen.
-                       $this->mErrorConnection = $conn;
-                       $conn = false;
-               }
-
-               return $conn;
-       }
-
-       /**
-        * Open a connection to a foreign DB, or return one if it is already open.
-        *
-        * Increments a reference count on the returned connection which locks the
-        * connection to the requested wiki. This reference count can be
-        * decremented by calling reuseConnection().
-        *
-        * If a connection is open to the appropriate server already, but with the wrong
-        * database, it will be switched to the right database and returned, as long as
-        * it has been freed first with reuseConnection().
-        *
-        * On error, returns false, and the connection which caused the
-        * error will be available via $this->mErrorConnection.
-        *
-        * @param int $i Server index
-        * @param string $wiki Wiki ID to open
-        * @return DatabaseBase
-        */
-       private function openForeignConnection( $i, $wiki ) {
-               list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
-               if ( isset( $this->mConns['foreignUsed'][$i][$wiki] ) ) {
-                       // Reuse an already-used connection
-                       $conn = $this->mConns['foreignUsed'][$i][$wiki];
-                       wfDebug( __METHOD__ . ": reusing connection $i/$wiki\n" );
-               } elseif ( isset( $this->mConns['foreignFree'][$i][$wiki] ) ) {
-                       // Reuse a free connection for the same wiki
-                       $conn = $this->mConns['foreignFree'][$i][$wiki];
-                       unset( $this->mConns['foreignFree'][$i][$wiki] );
-                       $this->mConns['foreignUsed'][$i][$wiki] = $conn;
-                       wfDebug( __METHOD__ . ": reusing free connection $i/$wiki\n" );
-               } elseif ( !empty( $this->mConns['foreignFree'][$i] ) ) {
-                       // Reuse a connection from another wiki
-                       $conn = reset( $this->mConns['foreignFree'][$i] );
-                       $oldWiki = key( $this->mConns['foreignFree'][$i] );
-
-                       // The empty string as a DB name means "don't care".
-                       // DatabaseMysqlBase::open() already handle this on connection.
-                       if ( $dbName !== '' && !$conn->selectDB( $dbName ) ) {
-                               $this->mLastError = "Error selecting database $dbName on server " .
-                                       $conn->getServer() . " from client host " . wfHostname() . "\n";
-                               $this->mErrorConnection = $conn;
-                               $conn = false;
-                       } else {
-                               $conn->tablePrefix( $prefix );
-                               unset( $this->mConns['foreignFree'][$i][$oldWiki] );
-                               $this->mConns['foreignUsed'][$i][$wiki] = $conn;
-                               wfDebug( __METHOD__ . ": reusing free connection from $oldWiki for $wiki\n" );
-                       }
-               } else {
-                       // Open a new connection
-                       $server = $this->mServers[$i];
-                       $server['serverIndex'] = $i;
-                       $server['foreignPoolRefCount'] = 0;
-                       $server['foreign'] = true;
-                       $conn = $this->reallyOpenConnection( $server, $dbName );
-                       if ( !$conn->isOpen() ) {
-                               wfDebug( __METHOD__ . ": error opening connection for $i/$wiki\n" );
-                               $this->mErrorConnection = $conn;
-                               $conn = false;
-                       } else {
-                               $conn->tablePrefix( $prefix );
-                               $this->mConns['foreignUsed'][$i][$wiki] = $conn;
-                               wfDebug( __METHOD__ . ": opened new connection for $i/$wiki\n" );
-                       }
-               }
-
-               // Increment reference count
-               if ( $conn ) {
-                       $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
-                       $conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
-               }
-
-               return $conn;
-       }
-
-       /**
-        * Test if the specified index represents an open connection
-        *
-        * @param int $index Server index
-        * @access private
-        * @return bool
-        */
-       private function isOpen( $index ) {
-               if ( !is_integer( $index ) ) {
-                       return false;
-               }
-
-               return (bool)$this->getAnyOpenConnection( $index );
-       }
-
-       /**
-        * Really opens a connection. Uncached.
-        * Returns a Database object whether or not the connection was successful.
-        * @access private
-        *
-        * @param array $server
-        * @param bool $dbNameOverride
-        * @throws MWException
-        * @return DatabaseBase
-        */
-       protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
-               if ( !is_array( $server ) ) {
-                       throw new MWException( 'You must update your load-balancing configuration. ' .
-                               'See DefaultSettings.php entry for $wgDBservers.' );
-               }
-
-               if ( $dbNameOverride !== false ) {
-                       $server['dbname'] = $dbNameOverride;
-               }
-
-               // Log when many connection are made on requests
-               if ( ++$this->connsOpened >= self::CONN_HELD_WARN_THRESHOLD ) {
-                       $masterAddr = $this->getServerName( 0 );
-                       wfDebugLog( 'DBPerformance', __METHOD__ . ": " .
-                               "{$this->connsOpened}+ connections made (master=$masterAddr)\n" .
-                               wfBacktrace( true ) );
-               }
-
-               # Create object
-               try {
-                       $db = DatabaseBase::factory( $server['type'], $server );
-               } catch ( DBConnectionError $e ) {
-                       // FIXME: This is probably the ugliest thing I have ever done to
-                       // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
-                       $db = $e->db;
-               }
-
-               $db->setLBInfo( $server );
-               if ( isset( $server['fakeSlaveLag'] ) ) {
-                       $db->setFakeSlaveLag( $server['fakeSlaveLag'] );
-               }
-               if ( isset( $server['fakeMaster'] ) ) {
-                       $db->setFakeMaster( true );
-               }
-
-               return $db;
-       }
-
-       /**
-        * @throws DBConnectionError
-        * @return bool
-        */
-       private function reportConnectionError() {
-               $conn = $this->mErrorConnection; // The connection which caused the error
-               $context = array(
-                       'method' => __METHOD__,
-                       'last_error' => $this->mLastError,
-               );
-
-               if ( !is_object( $conn ) ) {
-                       // No last connection, probably due to all servers being too busy
-                       wfLogDBError(
-                               "LB failure with no last connection. Connection error: {last_error}",
-                               $context
-                       );
-
-                       // If all servers were busy, mLastError will contain something sensible
-                       throw new DBConnectionError( null, $this->mLastError );
-               } else {
-                       $context['db_server'] = $conn->getProperty( 'mServer' );
-                       wfLogDBError(
-                               "Connection error: {last_error} ({db_server})",
-                               $context
-                       );
-                       $conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" ); // throws DBConnectionError
-               }
-
-               return false; /* not reached */
-       }
-
-       /**
-        * @return int
-        * @since 1.26
-        */
-       public function getWriterIndex() {
-               return 0;
-       }
-
-       /**
-        * Returns true if the specified index is a valid server index
-        *
-        * @param string $i
-        * @return bool
-        */
-       public function haveIndex( $i ) {
-               return array_key_exists( $i, $this->mServers );
-       }
-
-       /**
-        * Returns true if the specified index is valid and has non-zero load
-        *
-        * @param string $i
-        * @return bool
-        */
-       public function isNonZeroLoad( $i ) {
-               return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
-       }
-
-       /**
-        * Get the number of defined servers (not the number of open connections)
-        *
-        * @return int
-        */
-       public function getServerCount() {
-               return count( $this->mServers );
-       }
-
-       /**
-        * Get the host name or IP address of the server with the specified index
-        * Prefer a readable name if available.
-        * @param string $i
-        * @return string
-        */
-       public function getServerName( $i ) {
-               if ( isset( $this->mServers[$i]['hostName'] ) ) {
-                       $name = $this->mServers[$i]['hostName'];
-               } elseif ( isset( $this->mServers[$i]['host'] ) ) {
-                       $name = $this->mServers[$i]['host'];
-               } else {
-                       $name = '';
-               }
-
-               return ( $name != '' ) ? $name : 'localhost';
-       }
-
-       /**
-        * Return the server info structure for a given index, or false if the index is invalid.
-        * @param int $i
-        * @return array|bool
-        */
-       public function getServerInfo( $i ) {
-               if ( isset( $this->mServers[$i] ) ) {
-                       return $this->mServers[$i];
-               } else {
-                       return false;
-               }
-       }
-
-       /**
-        * Sets the server info structure for the given index. Entry at index $i
-        * is created if it doesn't exist
-        * @param int $i
-        * @param array $serverInfo
-        */
-       public function setServerInfo( $i, array $serverInfo ) {
-               $this->mServers[$i] = $serverInfo;
-       }
-
-       /**
-        * Get the current master position for chronology control purposes
-        * @return mixed
-        */
-       public function getMasterPos() {
-               # If this entire request was served from a slave without opening a connection to the
-               # master (however unlikely that may be), then we can fetch the position from the slave.
-               $masterConn = $this->getAnyOpenConnection( 0 );
-               if ( !$masterConn ) {
-                       $serverCount = count( $this->mServers );
-                       for ( $i = 1; $i < $serverCount; $i++ ) {
-                               $conn = $this->getAnyOpenConnection( $i );
-                               if ( $conn ) {
-                                       wfDebug( "Master pos fetched from slave\n" );
-
-                                       return $conn->getSlavePos();
-                               }
-                       }
-               } else {
-                       wfDebug( "Master pos fetched from master\n" );
-
-                       return $masterConn->getMasterPos();
-               }
-
-               return false;
-       }
-
-       /**
-        * Close all open connections
-        */
-       public function closeAll() {
-               foreach ( $this->mConns as $conns2 ) {
-                       foreach ( $conns2 as $conns3 ) {
-                               /** @var DatabaseBase $conn */
-                               foreach ( $conns3 as $conn ) {
-                                       $conn->close();
-                               }
-                       }
-               }
-               $this->mConns = array(
-                       'local' => array(),
-                       'foreignFree' => array(),
-                       'foreignUsed' => array(),
-               );
-               $this->connsOpened = 0;
-       }
-
-       /**
-        * Close a connection
-        * Using this function makes sure the LoadBalancer knows the connection is closed.
-        * If you use $conn->close() directly, the load balancer won't update its state.
-        * @param DatabaseBase $conn
-        */
-       public function closeConnection( $conn ) {
-               $done = false;
-               foreach ( $this->mConns as $i1 => $conns2 ) {
-                       foreach ( $conns2 as $i2 => $conns3 ) {
-                               foreach ( $conns3 as $i3 => $candidateConn ) {
-                                       if ( $conn === $candidateConn ) {
-                                               $conn->close();
-                                               unset( $this->mConns[$i1][$i2][$i3] );
-                                               --$this->connsOpened;
-                                               $done = true;
-                                               break;
-                                       }
-                               }
-                       }
-               }
-               if ( !$done ) {
-                       $conn->close();
-               }
-       }
-
-       /**
-        * Commit transactions on all open connections
-        */
-       public function commitAll() {
-               foreach ( $this->mConns as $conns2 ) {
-                       foreach ( $conns2 as $conns3 ) {
-                               /** @var DatabaseBase[] $conns3 */
-                               foreach ( $conns3 as $conn ) {
-                                       if ( $conn->trxLevel() ) {
-                                               $conn->commit( __METHOD__, 'flush' );
-                                       }
-                               }
-                       }
-               }
-       }
-
-       /**
-        *  Issue COMMIT only on master, only if queries were done on connection
-        */
-       public function commitMasterChanges() {
-               $masterIndex = $this->getWriterIndex();
-               foreach ( $this->mConns as $conns2 ) {
-                       if ( empty( $conns2[$masterIndex] ) ) {
-                               continue;
-                       }
-                       /** @var DatabaseBase $conn */
-                       foreach ( $conns2[$masterIndex] as $conn ) {
-                               if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
-                                       $conn->commit( __METHOD__, 'flush' );
-                               }
-                       }
-               }
-       }
-
-       /**
-        * Issue ROLLBACK only on master, only if queries were done on connection
-        * @since 1.23
-        */
-       public function rollbackMasterChanges() {
-               $failedServers = array();
-
-               $masterIndex = $this->getWriterIndex();
-               foreach ( $this->mConns as $conns2 ) {
-                       if ( empty( $conns2[$masterIndex] ) ) {
-                               continue;
-                       }
-                       /** @var DatabaseBase $conn */
-                       foreach ( $conns2[$masterIndex] as $conn ) {
-                               if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
-                                       try {
-                                               $conn->rollback( __METHOD__, 'flush' );
-                                       } catch ( DBError $e ) {
-                                               MWExceptionHandler::logException( $e );
-                                               $failedServers[] = $conn->getServer();
-                                       }
-                               }
-                       }
-               }
-
-               if ( $failedServers ) {
-                       throw new DBExpectedError( null, "Rollback failed on server(s) " .
-                               implode( ', ', array_unique( $failedServers ) ) );
-               }
-       }
-
-       /**
-        * @return bool Whether a master connection is already open
-        * @since 1.24
-        */
-       public function hasMasterConnection() {
-               return $this->isOpen( $this->getWriterIndex() );
-       }
-
-       /**
-        * Determine if there are pending changes in a transaction by this thread
-        * @since 1.23
-        * @return bool
-        */
-       public function hasMasterChanges() {
-               $masterIndex = $this->getWriterIndex();
-               foreach ( $this->mConns as $conns2 ) {
-                       if ( empty( $conns2[$masterIndex] ) ) {
-                               continue;
-                       }
-                       /** @var DatabaseBase $conn */
-                       foreach ( $conns2[$masterIndex] as $conn ) {
-                               if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
-                                       return true;
-                               }
-                       }
-               }
-               return false;
-       }
-
-       /**
-        * Get the timestamp of the latest write query done by this thread
-        * @since 1.25
-        * @return float|bool UNIX timestamp or false
-        */
-       public function lastMasterChangeTimestamp() {
-               $lastTime = false;
-               $masterIndex = $this->getWriterIndex();
-               foreach ( $this->mConns as $conns2 ) {
-                       if ( empty( $conns2[$masterIndex] ) ) {
-                               continue;
-                       }
-                       /** @var DatabaseBase $conn */
-                       foreach ( $conns2[$masterIndex] as $conn ) {
-                               $lastTime = max( $lastTime, $conn->lastDoneWrites() );
-                       }
-               }
-               return $lastTime;
-       }
-
-       /**
-        * Check if this load balancer object had any recent or still
-        * pending writes issued against it by this PHP thread
-        *
-        * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
-        * @return bool
-        * @since 1.25
-        */
-       public function hasOrMadeRecentMasterChanges( $age = null ) {
-               $age = ( $age === null ) ? $this->mWaitTimeout : $age;
-
-               return ( $this->hasMasterChanges()
-                       || $this->lastMasterChangeTimestamp() > microtime( true ) - $age );
-       }
-
-       /**
-        * @param mixed $value
-        * @return mixed
-        */
-       public function waitTimeout( $value = null ) {
-               return wfSetVar( $this->mWaitTimeout, $value );
-       }
-
-       /**
-        * @return bool Whether the generic connection for reads is highly "lagged"
-        */
-       public function getLaggedSlaveMode() {
-               # Get a generic reader connection
-               $this->getConnection( DB_SLAVE );
-
-               return $this->mLaggedSlaveMode;
-       }
-
-       /**
-        * Disables/enables lag checks
-        * @param null|bool $mode
-        * @return bool
-        */
-       public function allowLagged( $mode = null ) {
-               if ( $mode === null ) {
-                       return $this->mAllowLagged;
-               }
-               $this->mAllowLagged = $mode;
-
-               return $this->mAllowLagged;
-       }
-
-       /**
-        * @return bool
-        */
-       public function pingAll() {
-               $success = true;
-               foreach ( $this->mConns as $conns2 ) {
-                       foreach ( $conns2 as $conns3 ) {
-                               /** @var DatabaseBase[] $conns3 */
-                               foreach ( $conns3 as $conn ) {
-                                       if ( !$conn->ping() ) {
-                                               $success = false;
-                                       }
-                               }
-                       }
-               }
-
-               return $success;
-       }
-
-       /**
-        * Call a function with each open connection object
-        * @param callable $callback
-        * @param array $params
-        */
-       public function forEachOpenConnection( $callback, array $params = array() ) {
-               foreach ( $this->mConns as $conns2 ) {
-                       foreach ( $conns2 as $conns3 ) {
-                               foreach ( $conns3 as $conn ) {
-                                       $mergedParams = array_merge( array( $conn ), $params );
-                                       call_user_func_array( $callback, $mergedParams );
-                               }
-                       }
-               }
-       }
-
-       /**
-        * Get the hostname and lag time of the most-lagged slave
-        *
-        * This is useful for maintenance scripts that need to throttle their updates.
-        * May attempt to open connections to slaves on the default DB. If there is
-        * no lag, the maximum lag will be reported as -1.
-        *
-        * @param bool|string $wiki Wiki ID, or false for the default database
-        * @return array ( host, max lag, index of max lagged host )
-        */
-       public function getMaxLag( $wiki = false ) {
-               $maxLag = -1;
-               $host = '';
-               $maxIndex = 0;
-
-               if ( $this->getServerCount() <= 1 ) {
-                       return array( $host, $maxLag, $maxIndex ); // no replication = no lag
-               }
-
-               $lagTimes = $this->getLagTimes( $wiki );
-               foreach ( $lagTimes as $i => $lag ) {
-                       if ( $lag > $maxLag ) {
-                               $maxLag = $lag;
-                               $host = $this->mServers[$i]['host'];
-                               $maxIndex = $i;
-                       }
-               }
-
-               return array( $host, $maxLag, $maxIndex );
-       }
-
-       /**
-        * Get lag time for each server
-        *
-        * Results are cached for a short time in memcached/process cache
-        *
-        * @param string|bool $wiki
-        * @return int[] Map of (server index => seconds)
-        */
-       public function getLagTimes( $wiki = false ) {
-               if ( $this->getServerCount() <= 1 ) {
-                       return array( 0 => 0 ); // no replication = no lag
-               }
-
-               # Send the request to the load monitor
-               return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki );
-       }
-
-       /**
-        * Get the lag in seconds for a given connection, or zero if this load
-        * balancer does not have replication enabled.
-        *
-        * This should be used in preference to Database::getLag() in cases where
-        * replication may not be in use, since there is no way to determine if
-        * replication is in use at the connection level without running
-        * potentially restricted queries such as SHOW SLAVE STATUS. Using this
-        * function instead of Database::getLag() avoids a fatal error in this
-        * case on many installations.
-        *
-        * @param DatabaseBase $conn
-        * @return int
-        */
-       public function safeGetLag( $conn ) {
-               if ( $this->getServerCount() == 1 ) {
-                       return 0;
-               } else {
-                       return $conn->getLag();
-               }
-       }
-
-       /**
-        * Clear the cache for slag lag delay times
-        *
-        * This is only used for testing
-        */
-       public function clearLagTimeCache() {
-               $this->getLoadMonitor()->clearCaches();
-       }
-}
diff --git a/includes/db/LoadMonitor.php b/includes/db/LoadMonitor.php
deleted file mode 100644 (file)
index 4975ea1..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-<?php
-/**
- * Database load monitoring.
- *
- * 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 interface for database load monitoring
- *
- * @ingroup Database
- */
-interface LoadMonitor {
-       /**
-        * Construct a new LoadMonitor with a given LoadBalancer parent
-        *
-        * @param LoadBalancer $parent
-        */
-       public function __construct( $parent );
-
-       /**
-        * Perform pre-connection load ratio adjustment.
-        * @param array $loads
-        * @param string|bool $group The selected query group. Default: false
-        * @param string|bool $wiki Default: false
-        */
-       public function scaleLoads( &$loads, $group = false, $wiki = false );
-
-       /**
-        * Return an estimate of replication lag for each server
-        *
-        * @param array $serverIndexes
-        * @param string $wiki
-        *
-        * @return array Map of (server index => seconds)
-        */
-       public function getLagTimes( $serverIndexes, $wiki );
-}
-
-class LoadMonitorNull implements LoadMonitor {
-       public function __construct( $parent ) {
-       }
-
-       public function scaleLoads( &$loads, $group = false, $wiki = false ) {
-       }
-
-       public function getLagTimes( $serverIndexes, $wiki ) {
-               return array_fill_keys( $serverIndexes, 0 );
-       }
-}
diff --git a/includes/db/LoadMonitorMySQL.php b/includes/db/LoadMonitorMySQL.php
deleted file mode 100644 (file)
index 3008419..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-<?php
-/**
- * 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
- */
-
-/**
- * Basic MySQL load monitor with no external dependencies
- * Uses memcached to cache the replication lag for a short time
- *
- * @ingroup Database
- */
-class LoadMonitorMySQL implements LoadMonitor {
-       /** @var LoadBalancer */
-       public $parent;
-       /** @var BagOStuff */
-       protected $srvCache;
-       /** @var BagOStuff */
-       protected $mainCache;
-
-       public function __construct( $parent ) {
-               $this->parent = $parent;
-
-               $this->srvCache = ObjectCache::newAccelerator( 'hash' );
-               $this->mainCache = wfGetMainCache();
-       }
-
-       public function scaleLoads( &$loads, $group = false, $wiki = false ) {
-       }
-
-       public function getLagTimes( $serverIndexes, $wiki ) {
-               if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
-                       # Single server only, just return zero without caching
-                       return array( 0 => 0 );
-               }
-
-               $key = $this->getLagTimeCacheKey();
-               # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
-               $ttl = mt_rand( 4e6, 5e6 ) / 1e6;
-               # Keep keys around longer as fallbacks
-               $staleTTL = 60;
-
-               # (a) Check the local APC cache
-               $value = $this->srvCache->get( $key );
-               if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
-                       wfDebugLog( 'replication',  __FUNCTION__ . ": got lag times ($key) from local cache" );
-                       return $value['lagTimes']; // cache hit
-               }
-               $staleValue = $value ?: false;
-
-               # (b) Check the shared cache and backfill APC
-               $value = $this->mainCache->get( $key );
-               if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
-                       $this->srvCache->set( $key, $value, $staleTTL );
-                       wfDebugLog( 'replication',  __FUNCTION__ . ": got lag times ($key) from main cache" );
-
-                       return $value['lagTimes']; // cache hit
-               }
-               $staleValue = $value ?: $staleValue;
-
-               # (c) Cache key missing or expired; regenerate and backfill
-               if ( $this->mainCache->lock( $key, 0, 10 ) ) {
-                       # Let this process alone update the cache value
-                       $cache = $this->mainCache;
-                       /** @noinspection PhpUnusedLocalVariableInspection */
-                       $unlocker = new ScopedCallback( function () use ( $cache, $key ) {
-                               $cache->unlock( $key );
-                       } );
-               } elseif ( $staleValue ) {
-                       # Could not acquire lock but an old cache exists, so use it
-                       return $staleValue['lagTimes'];
-               }
-
-               $lagTimes = array();
-               foreach ( $serverIndexes as $i ) {
-                       if ( $i == 0 ) { # Master
-                               $lagTimes[$i] = 0;
-                       } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
-                               $lagTimes[$i] = $conn->getLag();
-                       } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
-                               $lagTimes[$i] = $conn->getLag();
-                               # Close the connection to avoid sleeper connections piling up.
-                               # Note that the caller will pick one of these DBs and reconnect,
-                               # which is slightly inefficient, but this only matters for the lag
-                               # time cache miss cache, which is far less common that cache hits.
-                               $this->parent->closeConnection( $conn );
-                       }
-               }
-
-               # Add a timestamp key so we know when it was cached
-               $value = array( 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) );
-               $this->mainCache->set( $key, $value, $staleTTL );
-               $this->srvCache->set( $key, $value, $staleTTL );
-               wfDebugLog( 'replication',  __FUNCTION__ . ": re-calculated lag times ($key)" );
-
-               return $value['lagTimes'];
-       }
-
-       public function clearCaches() {
-               $key = $this->getLagTimeCacheKey();
-               $this->srvCache->delete( $key );
-               $this->mainCache->delete( $key );
-       }
-
-       private function getLagTimeCacheKey() {
-               # Lag is per-server, not per-DB, so key on the master DB name
-               return wfGlobalCacheKey( 'lag-times', $this->parent->getServerName( 0 ) );
-       }
-}
diff --git a/includes/db/loadbalancer/LBFactory.php b/includes/db/loadbalancer/LBFactory.php
new file mode 100644 (file)
index 0000000..da0fe44
--- /dev/null
@@ -0,0 +1,226 @@
+<?php
+/**
+ * Generator of database load balancing objects.
+ *
+ * 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 interface for generating database load balancers
+ * @ingroup Database
+ */
+abstract class LBFactory {
+       /** @var LBFactory */
+       private static $instance;
+
+       /**
+        * Disables all access to the load balancer, will cause all database access
+        * to throw a DBAccessError
+        */
+       public static function disableBackend() {
+               global $wgLBFactoryConf;
+               self::$instance = new LBFactoryFake( $wgLBFactoryConf );
+       }
+
+       /**
+        * Get an LBFactory instance
+        *
+        * @return LBFactory
+        */
+       public static function singleton() {
+               global $wgLBFactoryConf;
+
+               if ( is_null( self::$instance ) ) {
+                       $class = self::getLBFactoryClass( $wgLBFactoryConf );
+
+                       self::$instance = new $class( $wgLBFactoryConf );
+               }
+
+               return self::$instance;
+       }
+
+       /**
+        * Returns the LBFactory class to use and the load balancer configuration.
+        *
+        * @param array $config (e.g. $wgLBFactoryConf)
+        * @return string Class name
+        */
+       public static function getLBFactoryClass( array $config ) {
+               // 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 = $config['class'];
+
+               if ( isset( $bcClasses[$class] ) ) {
+                       $class = $bcClasses[$class];
+                       wfDeprecated(
+                               '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
+                               '1.23'
+                       );
+               }
+
+               return $class;
+       }
+
+       /**
+        * Shut down, close connections and destroy the cached instance.
+        */
+       public static function destroyInstance() {
+               if ( self::$instance ) {
+                       self::$instance->shutdown();
+                       self::$instance->forEachLBCallMethod( 'closeAll' );
+                       self::$instance = null;
+               }
+       }
+
+       /**
+        * Set the instance to be the given object
+        *
+        * @param LBFactory $instance
+        */
+       public static function setInstance( $instance ) {
+               self::destroyInstance();
+               self::$instance = $instance;
+       }
+
+       /**
+        * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
+        * @param array $conf
+        */
+       abstract public function __construct( array $conf );
+
+       /**
+        * Create a new load balancer object. The resulting object will be untracked,
+        * not chronology-protected, and the caller is responsible for cleaning it up.
+        *
+        * @param bool|string $wiki Wiki ID, or false for the current wiki
+        * @return LoadBalancer
+        */
+       abstract public function newMainLB( $wiki = false );
+
+       /**
+        * Get a cached (tracked) load balancer object.
+        *
+        * @param bool|string $wiki Wiki ID, or false for the current wiki
+        * @return LoadBalancer
+        */
+       abstract public function getMainLB( $wiki = false );
+
+       /**
+        * Create a new load balancer for external storage. The resulting object will be
+        * untracked, not chronology-protected, and the caller is responsible for
+        * cleaning it up.
+        *
+        * @param string $cluster External storage cluster, or false for core
+        * @param bool|string $wiki Wiki ID, or false for the current wiki
+        * @return LoadBalancer
+        */
+       abstract protected function newExternalLB( $cluster, $wiki = false );
+
+       /**
+        * Get a cached (tracked) load balancer for external storage
+        *
+        * @param string $cluster External storage cluster, or false for core
+        * @param bool|string $wiki Wiki ID, or false for the current wiki
+        * @return LoadBalancer
+        */
+       abstract public function &getExternalLB( $cluster, $wiki = false );
+
+       /**
+        * 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 callable $callback
+        * @param array $params
+        */
+       abstract public function forEachLB( $callback, array $params = array() );
+
+       /**
+        * Prepare all tracked load balancers for shutdown
+        * STUB
+        */
+       public function shutdown() {
+       }
+
+       /**
+        * Call a method of each tracked load balancer
+        *
+        * @param string $methodName
+        * @param array $args
+        */
+       private function forEachLBCallMethod( $methodName, array $args = array() ) {
+               $this->forEachLB( function ( LoadBalancer $loadBalancer, $methodName, array $args ) {
+                       call_user_func_array( array( $loadBalancer, $methodName ), $args );
+               }, array( $methodName, $args ) );
+       }
+
+       /**
+        * Commit on all connections. Done for two reasons:
+        * 1. To commit changes to the masters.
+        * 2. To release the snapshot on all connections, master and slave.
+        */
+       public function commitAll() {
+               $this->forEachLBCallMethod( 'commitAll' );
+       }
+
+       /**
+        * Commit changes on all master connections
+        */
+       public function commitMasterChanges() {
+               $this->forEachLBCallMethod( 'commitMasterChanges' );
+       }
+
+       /**
+        * Rollback changes on all master connections
+        * @since 1.23
+        */
+       public function rollbackMasterChanges() {
+               $this->forEachLBCallMethod( 'rollbackMasterChanges' );
+       }
+
+       /**
+        * Detemine if any master connection has pending changes.
+        * @since 1.23
+        * @return bool
+        */
+       public function hasMasterChanges() {
+               $ret = false;
+               $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) {
+                       $ret = $ret || $lb->hasMasterChanges();
+               } );
+               return $ret;
+       }
+}
+
+/**
+ * Exception class for attempted DB access
+ */
+class DBAccessError extends MWException {
+       public function __construct() {
+               parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
+                       "This is not allowed." );
+       }
+}
diff --git a/includes/db/loadbalancer/LBFactoryFake.php b/includes/db/loadbalancer/LBFactoryFake.php
new file mode 100644 (file)
index 0000000..d8becf5
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Generator of database load balancing objects.
+ *
+ * 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
+ */
+
+/**
+ * LBFactory class that throws an error on any attempt to use it.
+ * This will typically be done via wfGetDB().
+ * Call LBFactory::disableBackend() to start using this, and
+ * LBFactory::enableBackend() to return to normal behavior
+ */
+class LBFactoryFake extends LBFactory {
+       public function __construct( array $conf ) {
+       }
+
+       public function newMainLB( $wiki = false ) {
+               throw new DBAccessError;
+       }
+
+       public function getMainLB( $wiki = false ) {
+               throw new DBAccessError;
+       }
+
+       protected function newExternalLB( $cluster, $wiki = false ) {
+               throw new DBAccessError;
+       }
+
+       public function &getExternalLB( $cluster, $wiki = false ) {
+               throw new DBAccessError;
+       }
+
+       public function forEachLB( $callback, array $params = array() ) {
+       }
+}
diff --git a/includes/db/loadbalancer/LBFactoryMulti.php b/includes/db/loadbalancer/LBFactoryMulti.php
new file mode 100644 (file)
index 0000000..92fbccd
--- /dev/null
@@ -0,0 +1,399 @@
+<?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 array A map of database names to section names */
+       private $sectionsByDB;
+
+       /**
+        * @var array A 2-d map. For each section, gives a map of server names to
+        * load ratios
+        */
+       private $sectionLoads;
+
+       /**
+        * @var array A server info associative array as documented for
+        * $wgDBservers. The host, hostName and load entries will be
+        * overridden
+        */
+       private $serverTemplate;
+
+       // Optional settings
+
+       /** @var array A 3-d map giving server load ratios for each section and group */
+       private $groupLoadsBySection = array();
+
+       /** @var array A 3-d map giving server load ratios by DB name */
+       private $groupLoadsByDB = array();
+
+       /** @var array A map of hostname to IP address */
+       private $hostsByName = array();
+
+       /** @var array A map of external storage cluster name to server load map */
+       private $externalLoads = array();
+
+       /**
+        * @var array A set of server info keys overriding serverTemplate for
+        * external storage
+        */
+       private $externalTemplateOverrides;
+
+       /**
+        * @var array A 2-d map overriding serverTemplate and
+        * externalTemplateOverrides on a server-by-server basis. Applies to both
+        * core and external storage
+        */
+       private $templateOverridesByServer;
+
+       /** @var array A 2-d map overriding the server info by external storage cluster */
+       private $templateOverridesByCluster;
+
+       /** @var array An override array for all master servers */
+       private $masterTemplateOverrides;
+
+       /**
+        * @var array|bool A map of section name to read-only message. Missing or
+        * false for read/write
+        */
+       private $readOnlyBySection = array();
+
+       // Other stuff
+
+       /** @var array Load balancer factory configuration */
+       private $conf;
+
+       /** @var LoadBalancer[] */
+       private $mainLBs = array();
+
+       /** @var LoadBalancer[] */
+       private $extLBs = array();
+
+       /** @var string */
+       private $lastWiki;
+
+       /** @var string */
+       private $lastSection;
+
+       /**
+        * @param array $conf
+        * @throws MWException
+        */
+       public function __construct( array $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 bool|string $wiki
+        * @return string
+        */
+       private 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 bool|string $wiki
+        * @return LoadBalancer
+        */
+       public 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 bool|string $wiki
+        * @return LoadBalancer
+        */
+       public function getMainLB( $wiki = false ) {
+               $section = $this->getSectionForWiki( $wiki );
+               if ( !isset( $this->mainLBs[$section] ) ) {
+                       $lb = $this->newMainLB( $wiki );
+                       $lb->parentInfo( array( 'id' => "main-$section" ) );
+                       $this->chronProt->initLB( $lb );
+                       $this->mainLBs[$section] = $lb;
+               }
+
+               return $this->mainLBs[$section];
+       }
+
+       /**
+        * @param string $cluster
+        * @param bool|string $wiki
+        * @throws MWException
+        * @return LoadBalancer
+        */
+       protected 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 string $cluster External storage cluster, or false for core
+        * @param bool|string $wiki Wiki ID, or false for the current wiki
+        * @return LoadBalancer
+        */
+       public 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 array $template
+        * @param array $loads
+        * @param array $groupLoads
+        * @return LoadBalancer
+        */
+       private function newLoadBalancer( $template, $loads, $groupLoads ) {
+               $servers = $this->makeServerArray( $template, $loads, $groupLoads );
+               $lb = new LoadBalancer( array(
+                       'servers' => $servers,
+               ) );
+
+               return $lb;
+       }
+
+       /**
+        * Make a server array as expected by LoadBalancer::__construct, using a template and load array
+        *
+        * @param array $template
+        * @param array $loads
+        * @param array $groupLoads
+        * @return array
+        */
+       private 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 array $groupLoads
+        * @return array
+        */
+       private 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 bool|string $wiki
+        * @return array
+        */
+       private 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 callable $callback
+        * @param array $params
+        */
+       public function forEachLB( $callback, array $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 ) );
+               }
+       }
+
+       public 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/loadbalancer/LBFactorySimple.php b/includes/db/loadbalancer/LBFactorySimple.php
new file mode 100644 (file)
index 0000000..23cdbc6
--- /dev/null
@@ -0,0 +1,153 @@
+<?php
+/**
+ * Generator of database load balancing objects.
+ *
+ * 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 simple single-master LBFactory that gets its configuration from the b/c globals
+ */
+class LBFactorySimple extends LBFactory {
+       /** @var LoadBalancer */
+       private $mainLB;
+
+       /** @var LoadBalancer[] */
+       private $extLBs = array();
+
+       /** @var ChronologyProtector */
+       private $chronProt;
+
+       public function __construct( array $conf ) {
+               $this->chronProt = new ChronologyProtector;
+       }
+
+       /**
+        * @param bool|string $wiki
+        * @return LoadBalancer
+        */
+       public function newMainLB( $wiki = false ) {
+               global $wgDBservers;
+               if ( $wgDBservers ) {
+                       $servers = $wgDBservers;
+               } else {
+                       global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
+                       global $wgDBssl, $wgDBcompress;
+
+                       $flags = DBO_DEFAULT;
+                       if ( $wgDebugDumpSql ) {
+                               $flags |= DBO_DEBUG;
+                       }
+                       if ( $wgDBssl ) {
+                               $flags |= DBO_SSL;
+                       }
+                       if ( $wgDBcompress ) {
+                               $flags |= DBO_COMPRESS;
+                       }
+
+                       $servers = array( array(
+                               'host' => $wgDBserver,
+                               'user' => $wgDBuser,
+                               'password' => $wgDBpassword,
+                               'dbname' => $wgDBname,
+                               'type' => $wgDBtype,
+                               'load' => 1,
+                               'flags' => $flags
+                       ) );
+               }
+
+               return new LoadBalancer( array(
+                       'servers' => $servers,
+               ) );
+       }
+
+       /**
+        * @param bool|string $wiki
+        * @return LoadBalancer
+        */
+       public function getMainLB( $wiki = false ) {
+               if ( !isset( $this->mainLB ) ) {
+                       $this->mainLB = $this->newMainLB( $wiki );
+                       $this->mainLB->parentInfo( array( 'id' => 'main' ) );
+                       $this->chronProt->initLB( $this->mainLB );
+               }
+
+               return $this->mainLB;
+       }
+
+       /**
+        * @throws MWException
+        * @param string $cluster
+        * @param bool|string $wiki
+        * @return LoadBalancer
+        */
+       protected function newExternalLB( $cluster, $wiki = false ) {
+               global $wgExternalServers;
+               if ( !isset( $wgExternalServers[$cluster] ) ) {
+                       throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
+               }
+
+               return new LoadBalancer( array(
+                       'servers' => $wgExternalServers[$cluster]
+               ) );
+       }
+
+       /**
+        * @param string $cluster
+        * @param bool|string $wiki
+        * @return array
+        */
+       public 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];
+       }
+
+       /**
+        * 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 callable $callback
+        * @param array $params
+        */
+       public function forEachLB( $callback, array $params = array() ) {
+               if ( isset( $this->mainLB ) ) {
+                       call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
+               }
+               foreach ( $this->extLBs as $lb ) {
+                       call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
+               }
+       }
+
+       public function shutdown() {
+               if ( $this->mainLB ) {
+                       $this->chronProt->shutdownLB( $this->mainLB );
+               }
+               foreach ( $this->extLBs as $extLB ) {
+                       $this->chronProt->shutdownLB( $extLB );
+               }
+               $this->chronProt->shutdown();
+               $this->commitMasterChanges();
+       }
+}
diff --git a/includes/db/loadbalancer/LBFactorySingle.php b/includes/db/loadbalancer/LBFactorySingle.php
new file mode 100644 (file)
index 0000000..a41dadf
--- /dev/null
@@ -0,0 +1,112 @@
+<?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 {
+       /** @var LoadBalancerSingle */
+       private $lb;
+
+       /**
+        * @param array $conf An associative array with one member:
+        *  - connection: The DatabaseBase connection object
+        */
+       public function __construct( array $conf ) {
+               $this->lb = new LoadBalancerSingle( $conf );
+       }
+
+       /**
+        * @param bool|string $wiki
+        * @return LoadBalancerSingle
+        */
+       public function newMainLB( $wiki = false ) {
+               return $this->lb;
+       }
+
+       /**
+        * @param bool|string $wiki
+        * @return LoadBalancerSingle
+        */
+       public function getMainLB( $wiki = false ) {
+               return $this->lb;
+       }
+
+       /**
+        * @param string $cluster External storage cluster, or false for core
+        * @param bool|string $wiki Wiki ID, or false for the current wiki
+        * @return LoadBalancerSingle
+        */
+       protected function newExternalLB( $cluster, $wiki = false ) {
+               return $this->lb;
+       }
+
+       /**
+        * @param string $cluster External storage cluster, or false for core
+        * @param bool|string $wiki Wiki ID, or false for the current wiki
+        * @return LoadBalancerSingle
+        */
+       public function &getExternalLB( $cluster, $wiki = false ) {
+               return $this->lb;
+       }
+
+       /**
+        * @param string|callable $callback
+        * @param array $params
+        */
+       public function forEachLB( $callback, array $params = array() ) {
+               call_user_func_array( $callback, array_merge( array( $this->lb ), $params ) );
+       }
+}
+
+/**
+ * Helper class for LBFactorySingle.
+ */
+class LoadBalancerSingle extends LoadBalancer {
+       /** @var DatabaseBase */
+       private $db;
+
+       /**
+        * @param array $params
+        */
+       public function __construct( array $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 string $server
+        * @param bool $dbNameOverride
+        *
+        * @return DatabaseBase
+        */
+       protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
+               return $this->db;
+       }
+}
diff --git a/includes/db/loadbalancer/LoadBalancer.php b/includes/db/loadbalancer/LoadBalancer.php
new file mode 100644 (file)
index 0000000..52dca08
--- /dev/null
@@ -0,0 +1,1273 @@
+<?php
+/**
+ * Database load balancing.
+ *
+ * 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
+ */
+
+/**
+ * Database load balancing object
+ *
+ * @todo document
+ * @ingroup Database
+ */
+class LoadBalancer {
+       /** @var array[] Map of (server index => server config array) */
+       private $mServers;
+       /** @var array[] Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) */
+       private $mConns;
+       /** @var array Map of (server index => weight) */
+       private $mLoads;
+       /** @var array[] Map of (group => server index => weight) */
+       private $mGroupLoads;
+       /** @var bool Whether to disregard slave lag as a factor in slave selection */
+       private $mAllowLagged;
+       /** @var integer Seconds to spend waiting on slave lag to resolve */
+       private $mWaitTimeout;
+
+       /** @var array LBFactory information */
+       private $mParentInfo;
+       /** @var string The LoadMonitor subclass name */
+       private $mLoadMonitorClass;
+       /** @var LoadMonitor */
+       private $mLoadMonitor;
+
+       /** @var bool|DatabaseBase Database connection that caused a problem */
+       private $mErrorConnection;
+       /** @var integer The generic (not query grouped) slave index (of $mServers) */
+       private $mReadIndex;
+       /** @var bool|DBMasterPos False if not set */
+       private $mWaitForPos;
+       /** @var bool Whether the generic reader fell back to a lagged slave */
+       private $mLaggedSlaveMode;
+       /** @var string The last DB selection or connection error */
+       private $mLastError = 'Unknown error';
+       /** @var integer Total connections opened */
+       private $connsOpened = 0;
+
+       /** @var integer Warn when this many connection are held */
+       const CONN_HELD_WARN_THRESHOLD = 10;
+
+       /**
+        * @param array $params Array with keys:
+        *   servers           Required. Array of server info structures.
+        *   loadMonitor       Name of a class used to fetch server lag and load.
+        * @throws MWException
+        */
+       public function __construct( array $params ) {
+               if ( !isset( $params['servers'] ) ) {
+                       throw new MWException( __CLASS__ . ': missing servers parameter' );
+               }
+               $this->mServers = $params['servers'];
+               $this->mWaitTimeout = 10;
+
+               $this->mReadIndex = -1;
+               $this->mWriteIndex = -1;
+               $this->mConns = array(
+                       'local' => array(),
+                       'foreignUsed' => array(),
+                       'foreignFree' => array() );
+               $this->mLoads = array();
+               $this->mWaitForPos = false;
+               $this->mLaggedSlaveMode = false;
+               $this->mErrorConnection = false;
+               $this->mAllowLagged = false;
+
+               if ( isset( $params['loadMonitor'] ) ) {
+                       $this->mLoadMonitorClass = $params['loadMonitor'];
+               } else {
+                       $master = reset( $params['servers'] );
+                       if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
+                               $this->mLoadMonitorClass = 'LoadMonitorMySQL';
+                       } else {
+                               $this->mLoadMonitorClass = 'LoadMonitorNull';
+                       }
+               }
+
+               foreach ( $params['servers'] as $i => $server ) {
+                       $this->mLoads[$i] = $server['load'];
+                       if ( isset( $server['groupLoads'] ) ) {
+                               foreach ( $server['groupLoads'] as $group => $ratio ) {
+                                       if ( !isset( $this->mGroupLoads[$group] ) ) {
+                                               $this->mGroupLoads[$group] = array();
+                                       }
+                                       $this->mGroupLoads[$group][$i] = $ratio;
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Get a LoadMonitor instance
+        *
+        * @return LoadMonitor
+        */
+       private function getLoadMonitor() {
+               if ( !isset( $this->mLoadMonitor ) ) {
+                       $class = $this->mLoadMonitorClass;
+                       $this->mLoadMonitor = new $class( $this );
+               }
+
+               return $this->mLoadMonitor;
+       }
+
+       /**
+        * Get or set arbitrary data used by the parent object, usually an LBFactory
+        * @param mixed $x
+        * @return mixed
+        */
+       public function parentInfo( $x = null ) {
+               return wfSetVar( $this->mParentInfo, $x );
+       }
+
+       /**
+        * Given an array of non-normalised probabilities, this function will select
+        * an element and return the appropriate key
+        *
+        * @deprecated since 1.21, use ArrayUtils::pickRandom()
+        *
+        * @param array $weights
+        * @return bool|int|string
+        */
+       public function pickRandom( array $weights ) {
+               return ArrayUtils::pickRandom( $weights );
+       }
+
+       /**
+        * @param array $loads
+        * @param bool|string $wiki Wiki to get non-lagged for
+        * @param float $maxLag Restrict the maximum allowed lag to this many seconds
+        * @return bool|int|string
+        */
+       private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = INF ) {
+               $lags = $this->getLagTimes( $wiki );
+
+               # Unset excessively lagged servers
+               foreach ( $lags as $i => $lag ) {
+                       if ( $i != 0 ) {
+                               $maxServerLag = $maxLag;
+                               if ( isset( $this->mServers[$i]['max lag'] ) ) {
+                                       $maxServerLag = min( $maxServerLag, $this->mServers[$i]['max lag'] );
+                               }
+                               if ( $lag === false ) {
+                                       wfDebugLog( 'replication', "Server #$i is not replicating" );
+                                       unset( $loads[$i] );
+                               } elseif ( $lag > $maxServerLag ) {
+                                       wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)" );
+                                       unset( $loads[$i] );
+                               }
+                       }
+               }
+
+               # Find out if all the slaves with non-zero load are lagged
+               $sum = 0;
+               foreach ( $loads as $load ) {
+                       $sum += $load;
+               }
+               if ( $sum == 0 ) {
+                       # No appropriate DB servers except maybe the master and some slaves with zero load
+                       # Do NOT use the master
+                       # Instead, this function will return false, triggering read-only mode,
+                       # and a lagged slave will be used instead.
+                       return false;
+               }
+
+               if ( count( $loads ) == 0 ) {
+                       return false;
+               }
+
+               #wfDebugLog( 'connect', var_export( $loads, true ) );
+
+               # Return a random representative of the remainder
+               return ArrayUtils::pickRandom( $loads );
+       }
+
+       /**
+        * Get the index of the reader connection, which may be a slave
+        * This takes into account load ratios and lag times. It should
+        * always return a consistent index during a given invocation
+        *
+        * Side effect: opens connections to databases
+        * @param string|bool $group Query group, or false for the generic reader
+        * @param string|bool $wiki Wiki ID, or false for the current wiki
+        * @throws MWException
+        * @return bool|int|string
+        */
+       public function getReaderIndex( $group = false, $wiki = false ) {
+               global $wgDBtype;
+
+               # @todo FIXME: For now, only go through all this for mysql databases
+               if ( $wgDBtype != 'mysql' ) {
+                       return $this->getWriterIndex();
+               }
+
+               if ( count( $this->mServers ) == 1 ) {
+                       # Skip the load balancing if there's only one server
+                       return 0;
+               } elseif ( $group === false && $this->mReadIndex >= 0 ) {
+                       # Shortcut if generic reader exists already
+                       return $this->mReadIndex;
+               }
+
+               # Find the relevant load array
+               if ( $group !== false ) {
+                       if ( isset( $this->mGroupLoads[$group] ) ) {
+                               $nonErrorLoads = $this->mGroupLoads[$group];
+                       } else {
+                               # No loads for this group, return false and the caller can use some other group
+                               wfDebug( __METHOD__ . ": no loads for group $group\n" );
+
+                               return false;
+                       }
+               } else {
+                       $nonErrorLoads = $this->mLoads;
+               }
+
+               if ( !count( $nonErrorLoads ) ) {
+                       throw new MWException( "Empty server array given to LoadBalancer" );
+               }
+
+               # Scale the configured load ratios according to the dynamic load (if the load monitor supports it)
+               $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $wiki );
+
+               $laggedSlaveMode = false;
+
+               # No server found yet
+               $i = false;
+               # First try quickly looking through the available servers for a server that
+               # meets our criteria
+               $currentLoads = $nonErrorLoads;
+               while ( count( $currentLoads ) ) {
+                       if ( $this->mAllowLagged || $laggedSlaveMode ) {
+                               $i = ArrayUtils::pickRandom( $currentLoads );
+                       } else {
+                               $i = false;
+                               if ( $this->mWaitForPos && $this->mWaitForPos->asOfTime() ) {
+                                       # ChronologyProtecter causes mWaitForPos to be set via sessions.
+                                       # This triggers doWait() after connect, so it's especially good to
+                                       # avoid lagged servers so as to avoid just blocking in that method.
+                                       $ago = microtime( true ) - $this->mWaitForPos->asOfTime();
+                                       # Aim for <= 1 second of waiting (being too picky can backfire)
+                                       $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago + 1 );
+                               }
+                               if ( $i === false ) {
+                                       # Any server with less lag than it's 'max lag' param is preferable
+                                       $i = $this->getRandomNonLagged( $currentLoads, $wiki );
+                               }
+                               if ( $i === false && count( $currentLoads ) != 0 ) {
+                                       # All slaves lagged. Switch to read-only mode
+                                       wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" );
+                                       $i = ArrayUtils::pickRandom( $currentLoads );
+                                       $laggedSlaveMode = true;
+                               }
+                       }
+
+                       if ( $i === false ) {
+                               # pickRandom() returned false
+                               # This is permanent and means the configuration or the load monitor
+                               # wants us to return false.
+                               wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false" );
+
+                               return false;
+                       }
+
+                       $serverName = $this->getServerName( $i );
+                       wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: $serverName..." );
+
+                       $conn = $this->openConnection( $i, $wiki );
+                       if ( !$conn ) {
+                               wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki" );
+                               unset( $nonErrorLoads[$i] );
+                               unset( $currentLoads[$i] );
+                               $i = false;
+                               continue;
+                       }
+
+                       // Decrement reference counter, we are finished with this connection.
+                       // It will be incremented for the caller later.
+                       if ( $wiki !== false ) {
+                               $this->reuseConnection( $conn );
+                       }
+
+                       # Return this server
+                       break;
+               }
+
+               # If all servers were down, quit now
+               if ( !count( $nonErrorLoads ) ) {
+                       wfDebugLog( 'connect', "All servers down" );
+               }
+
+               if ( $i !== false ) {
+                       # Slave connection successful
+                       # Wait for the session master pos for a short time
+                       if ( $this->mWaitForPos && $i > 0 ) {
+                               if ( !$this->doWait( $i ) ) {
+                                       $this->mServers[$i]['slave pos'] = $conn->getSlavePos();
+                               }
+                       }
+                       if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) {
+                               $this->mReadIndex = $i;
+                               # Record if the generic reader index is in "lagged slave" mode
+                               if ( $laggedSlaveMode ) {
+                                       $this->mLaggedSlaveMode = true;
+                               }
+                       }
+                       $serverName = $this->getServerName( $i );
+                       wfDebug( __METHOD__ . ": using server $serverName for group '$group'\n" );
+               }
+
+               return $i;
+       }
+
+       /**
+        * Set the master wait position
+        * If a DB_SLAVE connection has been opened already, waits
+        * Otherwise sets a variable telling it to wait if such a connection is opened
+        * @param DBMasterPos $pos
+        */
+       public function waitFor( $pos ) {
+               $this->mWaitForPos = $pos;
+               $i = $this->mReadIndex;
+
+               if ( $i > 0 ) {
+                       if ( !$this->doWait( $i ) ) {
+                               $this->mServers[$i]['slave pos'] = $this->getAnyOpenConnection( $i )->getSlavePos();
+                               $this->mLaggedSlaveMode = true;
+                       }
+               }
+       }
+
+       /**
+        * Set the master wait position and wait for a "generic" slave to catch up to it
+        *
+        * This can be used a faster proxy for waitForAll()
+        *
+        * @param DBMasterPos $pos
+        * @param int $timeout Max seconds to wait; default is mWaitTimeout
+        * @return bool Success (able to connect and no timeouts reached)
+        * @since 1.26
+        */
+       public function waitForOne( $pos, $timeout = null ) {
+               $this->mWaitForPos = $pos;
+
+               $i = $this->mReadIndex;
+               if ( $i <= 0 ) {
+                       // Pick a generic slave if there isn't one yet
+                       $readLoads = $this->mLoads;
+                       unset( $readLoads[$this->getWriterIndex()] ); // slaves only
+                       $readLoads = array_filter( $readLoads ); // with non-zero load
+                       $i = ArrayUtils::pickRandom( $readLoads );
+               }
+
+               if ( $i > 0 ) {
+                       $ok = $this->doWait( $i, true, $timeout );
+               } else {
+                       $ok = true; // no applicable loads
+               }
+
+               return $ok;
+       }
+
+       /**
+        * Set the master wait position and wait for ALL slaves to catch up to it
+        * @param DBMasterPos $pos
+        * @param int $timeout Max seconds to wait; default is mWaitTimeout
+        * @return bool Success (able to connect and no timeouts reached)
+        */
+       public function waitForAll( $pos, $timeout = null ) {
+               $this->mWaitForPos = $pos;
+               $serverCount = count( $this->mServers );
+
+               $ok = true;
+               for ( $i = 1; $i < $serverCount; $i++ ) {
+                       if ( $this->mLoads[$i] > 0 ) {
+                               $ok = $this->doWait( $i, true, $timeout ) && $ok;
+                       }
+               }
+
+               return $ok;
+       }
+
+       /**
+        * Get any open connection to a given server index, local or foreign
+        * Returns false if there is no connection open
+        *
+        * @param int $i
+        * @return DatabaseBase|bool False on failure
+        */
+       public function getAnyOpenConnection( $i ) {
+               foreach ( $this->mConns as $conns ) {
+                       if ( !empty( $conns[$i] ) ) {
+                               return reset( $conns[$i] );
+                       }
+               }
+
+               return false;
+       }
+
+       /**
+        * Wait for a given slave to catch up to the master pos stored in $this
+        * @param int $index Server index
+        * @param bool $open Check the server even if a new connection has to be made
+        * @param int $timeout Max seconds to wait; default is mWaitTimeout
+        * @return bool
+        */
+       protected function doWait( $index, $open = false, $timeout = null ) {
+               $close = false; // close the connection afterwards
+
+               # Find a connection to wait on, creating one if needed and allowed
+               $conn = $this->getAnyOpenConnection( $index );
+               if ( !$conn ) {
+                       if ( !$open ) {
+                               wfDebug( __METHOD__ . ": no connection open\n" );
+
+                               return false;
+                       } else {
+                               $conn = $this->openConnection( $index, '' );
+                               if ( !$conn ) {
+                                       wfDebug( __METHOD__ . ": failed to open connection\n" );
+
+                                       return false;
+                               }
+                               // Avoid connection spam in waitForAll() when connections
+                               // are made just for the sake of doing this lag check.
+                               $close = true;
+                       }
+               }
+
+               wfDebug( __METHOD__ . ": Waiting for slave #$index to catch up...\n" );
+               $timeout = $timeout ?: $this->mWaitTimeout;
+               $result = $conn->masterPosWait( $this->mWaitForPos, $timeout );
+
+               if ( $result == -1 || is_null( $result ) ) {
+                       # Timed out waiting for slave, use master instead
+                       $server = $server = $this->getServerName( $index );
+                       $msg = __METHOD__ . ": Timed out waiting on $server pos {$this->mWaitForPos}";
+                       wfDebug( "$msg\n" );
+                       wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
+                       $ok = false;
+               } else {
+                       wfDebug( __METHOD__ . ": Done\n" );
+                       $ok = true;
+               }
+
+               if ( $close ) {
+                       $this->closeConnection( $conn );
+               }
+
+               return $ok;
+       }
+
+       /**
+        * Get a connection by index
+        * This is the main entry point for this class.
+        *
+        * @param int $i Server index
+        * @param array|string|bool $groups Query group(s), or false for the generic reader
+        * @param string|bool $wiki Wiki ID, or false for the current wiki
+        *
+        * @throws MWException
+        * @return DatabaseBase
+        */
+       public function getConnection( $i, $groups = array(), $wiki = false ) {
+               if ( $i === null || $i === false ) {
+                       throw new MWException( 'Attempt to call ' . __METHOD__ .
+                               ' with invalid server index' );
+               }
+
+               if ( $wiki === wfWikiID() ) {
+                       $wiki = false;
+               }
+
+               $groups = ( $groups === false || $groups === array() )
+                       ? array( false ) // check one "group": the generic pool
+                       : (array)$groups;
+
+               $masterOnly = ( $i == DB_MASTER || $i == $this->getWriterIndex() );
+               $oldConnsOpened = $this->connsOpened; // connections open now
+
+               if ( $i == DB_MASTER ) {
+                       $i = $this->getWriterIndex();
+               } else {
+                       # Try to find an available server in any the query groups (in order)
+                       foreach ( $groups as $group ) {
+                               $groupIndex = $this->getReaderIndex( $group, $wiki );
+                               if ( $groupIndex !== false ) {
+                                       $i = $groupIndex;
+                                       break;
+                               }
+                       }
+               }
+
+               # Operation-based index
+               if ( $i == DB_SLAVE ) {
+                       $this->mLastError = 'Unknown error'; // reset error string
+                       # Try the general server pool if $groups are unavailable.
+                       $i = in_array( false, $groups, true )
+                               ? false // don't bother with this if that is what was tried above
+                               : $this->getReaderIndex( false, $wiki );
+                       # Couldn't find a working server in getReaderIndex()?
+                       if ( $i === false ) {
+                               $this->mLastError = 'No working slave server: ' . $this->mLastError;
+
+                               return $this->reportConnectionError();
+                       }
+               }
+
+               # Now we have an explicit index into the servers array
+               $conn = $this->openConnection( $i, $wiki );
+               if ( !$conn ) {
+                       return $this->reportConnectionError();
+               }
+
+               # Profile any new connections that happen
+               if ( $this->connsOpened > $oldConnsOpened ) {
+                       $host = $conn->getServer();
+                       $dbname = $conn->getDBname();
+                       $trxProf = Profiler::instance()->getTransactionProfiler();
+                       $trxProf->recordConnection( $host, $dbname, $masterOnly );
+               }
+
+               return $conn;
+       }
+
+       /**
+        * Mark a foreign connection as being available for reuse under a different
+        * DB name or prefix. This mechanism is reference-counted, and must be called
+        * the same number of times as getConnection() to work.
+        *
+        * @param DatabaseBase $conn
+        * @throws MWException
+        */
+       public function reuseConnection( $conn ) {
+               $serverIndex = $conn->getLBInfo( 'serverIndex' );
+               $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
+               if ( $serverIndex === null || $refCount === null ) {
+                       wfDebug( __METHOD__ . ": this connection was not opened as a foreign connection\n" );
+
+                       /**
+                        * This can happen in code like:
+                        *   foreach ( $dbs as $db ) {
+                        *     $conn = $lb->getConnection( DB_SLAVE, array(), $db );
+                        *     ...
+                        *     $lb->reuseConnection( $conn );
+                        *   }
+                        * When a connection to the local DB is opened in this way, reuseConnection()
+                        * should be ignored
+                        */
+
+                       return;
+               }
+
+               $dbName = $conn->getDBname();
+               $prefix = $conn->tablePrefix();
+               if ( strval( $prefix ) !== '' ) {
+                       $wiki = "$dbName-$prefix";
+               } else {
+                       $wiki = $dbName;
+               }
+               if ( $this->mConns['foreignUsed'][$serverIndex][$wiki] !== $conn ) {
+                       throw new MWException( __METHOD__ . ": connection not found, has " .
+                               "the connection been freed already?" );
+               }
+               $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
+               if ( $refCount <= 0 ) {
+                       $this->mConns['foreignFree'][$serverIndex][$wiki] = $conn;
+                       unset( $this->mConns['foreignUsed'][$serverIndex][$wiki] );
+                       wfDebug( __METHOD__ . ": freed connection $serverIndex/$wiki\n" );
+               } else {
+                       wfDebug( __METHOD__ . ": reference count for $serverIndex/$wiki reduced to $refCount\n" );
+               }
+       }
+
+       /**
+        * Get a database connection handle reference
+        *
+        * The handle's methods wrap simply wrap those of a DatabaseBase handle
+        *
+        * @see LoadBalancer::getConnection() for parameter information
+        *
+        * @param int $db
+        * @param array|string|bool $groups Query group(s), or false for the generic reader
+        * @param string|bool $wiki Wiki ID, or false for the current wiki
+        * @return DBConnRef
+        */
+       public function getConnectionRef( $db, $groups = array(), $wiki = false ) {
+               return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
+       }
+
+       /**
+        * Get a database connection handle reference without connecting yet
+        *
+        * The handle's methods wrap simply wrap those of a DatabaseBase handle
+        *
+        * @see LoadBalancer::getConnection() for parameter information
+        *
+        * @param int $db
+        * @param array|string|bool $groups Query group(s), or false for the generic reader
+        * @param string|bool $wiki Wiki ID, or false for the current wiki
+        * @return DBConnRef
+        */
+       public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) {
+               return new DBConnRef( $this, array( $db, $groups, $wiki ) );
+       }
+
+       /**
+        * Open a connection to the server given by the specified index
+        * Index must be an actual index into the array.
+        * If the server is already open, returns it.
+        *
+        * On error, returns false, and the connection which caused the
+        * error will be available via $this->mErrorConnection.
+        *
+        * @param int $i Server index
+        * @param string|bool $wiki Wiki ID, or false for the current wiki
+        * @return DatabaseBase
+        *
+        * @access private
+        */
+       public function openConnection( $i, $wiki = false ) {
+               if ( $wiki !== false ) {
+                       $conn = $this->openForeignConnection( $i, $wiki );
+               } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
+                       $conn = $this->mConns['local'][$i][0];
+               } else {
+                       $server = $this->mServers[$i];
+                       $server['serverIndex'] = $i;
+                       $conn = $this->reallyOpenConnection( $server, false );
+                       $serverName = $this->getServerName( $i );
+                       if ( $conn->isOpen() ) {
+                               wfDebug( "Connected to database $i at $serverName\n" );
+                               $this->mConns['local'][$i][0] = $conn;
+                       } else {
+                               wfDebug( "Failed to connect to database $i at $serverName\n" );
+                               $this->mErrorConnection = $conn;
+                               $conn = false;
+                       }
+               }
+
+               if ( $conn && !$conn->isOpen() ) {
+                       // Connection was made but later unrecoverably lost for some reason.
+                       // Do not return a handle that will just throw exceptions on use,
+                       // but let the calling code (e.g. getReaderIndex) try another server.
+                       // See DatabaseMyslBase::ping() for how this can happen.
+                       $this->mErrorConnection = $conn;
+                       $conn = false;
+               }
+
+               return $conn;
+       }
+
+       /**
+        * Open a connection to a foreign DB, or return one if it is already open.
+        *
+        * Increments a reference count on the returned connection which locks the
+        * connection to the requested wiki. This reference count can be
+        * decremented by calling reuseConnection().
+        *
+        * If a connection is open to the appropriate server already, but with the wrong
+        * database, it will be switched to the right database and returned, as long as
+        * it has been freed first with reuseConnection().
+        *
+        * On error, returns false, and the connection which caused the
+        * error will be available via $this->mErrorConnection.
+        *
+        * @param int $i Server index
+        * @param string $wiki Wiki ID to open
+        * @return DatabaseBase
+        */
+       private function openForeignConnection( $i, $wiki ) {
+               list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
+               if ( isset( $this->mConns['foreignUsed'][$i][$wiki] ) ) {
+                       // Reuse an already-used connection
+                       $conn = $this->mConns['foreignUsed'][$i][$wiki];
+                       wfDebug( __METHOD__ . ": reusing connection $i/$wiki\n" );
+               } elseif ( isset( $this->mConns['foreignFree'][$i][$wiki] ) ) {
+                       // Reuse a free connection for the same wiki
+                       $conn = $this->mConns['foreignFree'][$i][$wiki];
+                       unset( $this->mConns['foreignFree'][$i][$wiki] );
+                       $this->mConns['foreignUsed'][$i][$wiki] = $conn;
+                       wfDebug( __METHOD__ . ": reusing free connection $i/$wiki\n" );
+               } elseif ( !empty( $this->mConns['foreignFree'][$i] ) ) {
+                       // Reuse a connection from another wiki
+                       $conn = reset( $this->mConns['foreignFree'][$i] );
+                       $oldWiki = key( $this->mConns['foreignFree'][$i] );
+
+                       // The empty string as a DB name means "don't care".
+                       // DatabaseMysqlBase::open() already handle this on connection.
+                       if ( $dbName !== '' && !$conn->selectDB( $dbName ) ) {
+                               $this->mLastError = "Error selecting database $dbName on server " .
+                                       $conn->getServer() . " from client host " . wfHostname() . "\n";
+                               $this->mErrorConnection = $conn;
+                               $conn = false;
+                       } else {
+                               $conn->tablePrefix( $prefix );
+                               unset( $this->mConns['foreignFree'][$i][$oldWiki] );
+                               $this->mConns['foreignUsed'][$i][$wiki] = $conn;
+                               wfDebug( __METHOD__ . ": reusing free connection from $oldWiki for $wiki\n" );
+                       }
+               } else {
+                       // Open a new connection
+                       $server = $this->mServers[$i];
+                       $server['serverIndex'] = $i;
+                       $server['foreignPoolRefCount'] = 0;
+                       $server['foreign'] = true;
+                       $conn = $this->reallyOpenConnection( $server, $dbName );
+                       if ( !$conn->isOpen() ) {
+                               wfDebug( __METHOD__ . ": error opening connection for $i/$wiki\n" );
+                               $this->mErrorConnection = $conn;
+                               $conn = false;
+                       } else {
+                               $conn->tablePrefix( $prefix );
+                               $this->mConns['foreignUsed'][$i][$wiki] = $conn;
+                               wfDebug( __METHOD__ . ": opened new connection for $i/$wiki\n" );
+                       }
+               }
+
+               // Increment reference count
+               if ( $conn ) {
+                       $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
+                       $conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
+               }
+
+               return $conn;
+       }
+
+       /**
+        * Test if the specified index represents an open connection
+        *
+        * @param int $index Server index
+        * @access private
+        * @return bool
+        */
+       private function isOpen( $index ) {
+               if ( !is_integer( $index ) ) {
+                       return false;
+               }
+
+               return (bool)$this->getAnyOpenConnection( $index );
+       }
+
+       /**
+        * Really opens a connection. Uncached.
+        * Returns a Database object whether or not the connection was successful.
+        * @access private
+        *
+        * @param array $server
+        * @param bool $dbNameOverride
+        * @throws MWException
+        * @return DatabaseBase
+        */
+       protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
+               if ( !is_array( $server ) ) {
+                       throw new MWException( 'You must update your load-balancing configuration. ' .
+                               'See DefaultSettings.php entry for $wgDBservers.' );
+               }
+
+               if ( $dbNameOverride !== false ) {
+                       $server['dbname'] = $dbNameOverride;
+               }
+
+               // Log when many connection are made on requests
+               if ( ++$this->connsOpened >= self::CONN_HELD_WARN_THRESHOLD ) {
+                       $masterAddr = $this->getServerName( 0 );
+                       wfDebugLog( 'DBPerformance', __METHOD__ . ": " .
+                               "{$this->connsOpened}+ connections made (master=$masterAddr)\n" .
+                               wfBacktrace( true ) );
+               }
+
+               # Create object
+               try {
+                       $db = DatabaseBase::factory( $server['type'], $server );
+               } catch ( DBConnectionError $e ) {
+                       // FIXME: This is probably the ugliest thing I have ever done to
+                       // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
+                       $db = $e->db;
+               }
+
+               $db->setLBInfo( $server );
+               if ( isset( $server['fakeSlaveLag'] ) ) {
+                       $db->setFakeSlaveLag( $server['fakeSlaveLag'] );
+               }
+               if ( isset( $server['fakeMaster'] ) ) {
+                       $db->setFakeMaster( true );
+               }
+
+               return $db;
+       }
+
+       /**
+        * @throws DBConnectionError
+        * @return bool
+        */
+       private function reportConnectionError() {
+               $conn = $this->mErrorConnection; // The connection which caused the error
+               $context = array(
+                       'method' => __METHOD__,
+                       'last_error' => $this->mLastError,
+               );
+
+               if ( !is_object( $conn ) ) {
+                       // No last connection, probably due to all servers being too busy
+                       wfLogDBError(
+                               "LB failure with no last connection. Connection error: {last_error}",
+                               $context
+                       );
+
+                       // If all servers were busy, mLastError will contain something sensible
+                       throw new DBConnectionError( null, $this->mLastError );
+               } else {
+                       $context['db_server'] = $conn->getProperty( 'mServer' );
+                       wfLogDBError(
+                               "Connection error: {last_error} ({db_server})",
+                               $context
+                       );
+                       $conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" ); // throws DBConnectionError
+               }
+
+               return false; /* not reached */
+       }
+
+       /**
+        * @return int
+        * @since 1.26
+        */
+       public function getWriterIndex() {
+               return 0;
+       }
+
+       /**
+        * Returns true if the specified index is a valid server index
+        *
+        * @param string $i
+        * @return bool
+        */
+       public function haveIndex( $i ) {
+               return array_key_exists( $i, $this->mServers );
+       }
+
+       /**
+        * Returns true if the specified index is valid and has non-zero load
+        *
+        * @param string $i
+        * @return bool
+        */
+       public function isNonZeroLoad( $i ) {
+               return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
+       }
+
+       /**
+        * Get the number of defined servers (not the number of open connections)
+        *
+        * @return int
+        */
+       public function getServerCount() {
+               return count( $this->mServers );
+       }
+
+       /**
+        * Get the host name or IP address of the server with the specified index
+        * Prefer a readable name if available.
+        * @param string $i
+        * @return string
+        */
+       public function getServerName( $i ) {
+               if ( isset( $this->mServers[$i]['hostName'] ) ) {
+                       $name = $this->mServers[$i]['hostName'];
+               } elseif ( isset( $this->mServers[$i]['host'] ) ) {
+                       $name = $this->mServers[$i]['host'];
+               } else {
+                       $name = '';
+               }
+
+               return ( $name != '' ) ? $name : 'localhost';
+       }
+
+       /**
+        * Return the server info structure for a given index, or false if the index is invalid.
+        * @param int $i
+        * @return array|bool
+        */
+       public function getServerInfo( $i ) {
+               if ( isset( $this->mServers[$i] ) ) {
+                       return $this->mServers[$i];
+               } else {
+                       return false;
+               }
+       }
+
+       /**
+        * Sets the server info structure for the given index. Entry at index $i
+        * is created if it doesn't exist
+        * @param int $i
+        * @param array $serverInfo
+        */
+       public function setServerInfo( $i, array $serverInfo ) {
+               $this->mServers[$i] = $serverInfo;
+       }
+
+       /**
+        * Get the current master position for chronology control purposes
+        * @return mixed
+        */
+       public function getMasterPos() {
+               # If this entire request was served from a slave without opening a connection to the
+               # master (however unlikely that may be), then we can fetch the position from the slave.
+               $masterConn = $this->getAnyOpenConnection( 0 );
+               if ( !$masterConn ) {
+                       $serverCount = count( $this->mServers );
+                       for ( $i = 1; $i < $serverCount; $i++ ) {
+                               $conn = $this->getAnyOpenConnection( $i );
+                               if ( $conn ) {
+                                       wfDebug( "Master pos fetched from slave\n" );
+
+                                       return $conn->getSlavePos();
+                               }
+                       }
+               } else {
+                       wfDebug( "Master pos fetched from master\n" );
+
+                       return $masterConn->getMasterPos();
+               }
+
+               return false;
+       }
+
+       /**
+        * Close all open connections
+        */
+       public function closeAll() {
+               foreach ( $this->mConns as $conns2 ) {
+                       foreach ( $conns2 as $conns3 ) {
+                               /** @var DatabaseBase $conn */
+                               foreach ( $conns3 as $conn ) {
+                                       $conn->close();
+                               }
+                       }
+               }
+               $this->mConns = array(
+                       'local' => array(),
+                       'foreignFree' => array(),
+                       'foreignUsed' => array(),
+               );
+               $this->connsOpened = 0;
+       }
+
+       /**
+        * Close a connection
+        * Using this function makes sure the LoadBalancer knows the connection is closed.
+        * If you use $conn->close() directly, the load balancer won't update its state.
+        * @param DatabaseBase $conn
+        */
+       public function closeConnection( $conn ) {
+               $done = false;
+               foreach ( $this->mConns as $i1 => $conns2 ) {
+                       foreach ( $conns2 as $i2 => $conns3 ) {
+                               foreach ( $conns3 as $i3 => $candidateConn ) {
+                                       if ( $conn === $candidateConn ) {
+                                               $conn->close();
+                                               unset( $this->mConns[$i1][$i2][$i3] );
+                                               --$this->connsOpened;
+                                               $done = true;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+               if ( !$done ) {
+                       $conn->close();
+               }
+       }
+
+       /**
+        * Commit transactions on all open connections
+        */
+       public function commitAll() {
+               foreach ( $this->mConns as $conns2 ) {
+                       foreach ( $conns2 as $conns3 ) {
+                               /** @var DatabaseBase[] $conns3 */
+                               foreach ( $conns3 as $conn ) {
+                                       if ( $conn->trxLevel() ) {
+                                               $conn->commit( __METHOD__, 'flush' );
+                                       }
+                               }
+                       }
+               }
+       }
+
+       /**
+        *  Issue COMMIT only on master, only if queries were done on connection
+        */
+       public function commitMasterChanges() {
+               $masterIndex = $this->getWriterIndex();
+               foreach ( $this->mConns as $conns2 ) {
+                       if ( empty( $conns2[$masterIndex] ) ) {
+                               continue;
+                       }
+                       /** @var DatabaseBase $conn */
+                       foreach ( $conns2[$masterIndex] as $conn ) {
+                               if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
+                                       $conn->commit( __METHOD__, 'flush' );
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Issue ROLLBACK only on master, only if queries were done on connection
+        * @since 1.23
+        */
+       public function rollbackMasterChanges() {
+               $failedServers = array();
+
+               $masterIndex = $this->getWriterIndex();
+               foreach ( $this->mConns as $conns2 ) {
+                       if ( empty( $conns2[$masterIndex] ) ) {
+                               continue;
+                       }
+                       /** @var DatabaseBase $conn */
+                       foreach ( $conns2[$masterIndex] as $conn ) {
+                               if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
+                                       try {
+                                               $conn->rollback( __METHOD__, 'flush' );
+                                       } catch ( DBError $e ) {
+                                               MWExceptionHandler::logException( $e );
+                                               $failedServers[] = $conn->getServer();
+                                       }
+                               }
+                       }
+               }
+
+               if ( $failedServers ) {
+                       throw new DBExpectedError( null, "Rollback failed on server(s) " .
+                               implode( ', ', array_unique( $failedServers ) ) );
+               }
+       }
+
+       /**
+        * @return bool Whether a master connection is already open
+        * @since 1.24
+        */
+       public function hasMasterConnection() {
+               return $this->isOpen( $this->getWriterIndex() );
+       }
+
+       /**
+        * Determine if there are pending changes in a transaction by this thread
+        * @since 1.23
+        * @return bool
+        */
+       public function hasMasterChanges() {
+               $masterIndex = $this->getWriterIndex();
+               foreach ( $this->mConns as $conns2 ) {
+                       if ( empty( $conns2[$masterIndex] ) ) {
+                               continue;
+                       }
+                       /** @var DatabaseBase $conn */
+                       foreach ( $conns2[$masterIndex] as $conn ) {
+                               if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
+                                       return true;
+                               }
+                       }
+               }
+               return false;
+       }
+
+       /**
+        * Get the timestamp of the latest write query done by this thread
+        * @since 1.25
+        * @return float|bool UNIX timestamp or false
+        */
+       public function lastMasterChangeTimestamp() {
+               $lastTime = false;
+               $masterIndex = $this->getWriterIndex();
+               foreach ( $this->mConns as $conns2 ) {
+                       if ( empty( $conns2[$masterIndex] ) ) {
+                               continue;
+                       }
+                       /** @var DatabaseBase $conn */
+                       foreach ( $conns2[$masterIndex] as $conn ) {
+                               $lastTime = max( $lastTime, $conn->lastDoneWrites() );
+                       }
+               }
+               return $lastTime;
+       }
+
+       /**
+        * Check if this load balancer object had any recent or still
+        * pending writes issued against it by this PHP thread
+        *
+        * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
+        * @return bool
+        * @since 1.25
+        */
+       public function hasOrMadeRecentMasterChanges( $age = null ) {
+               $age = ( $age === null ) ? $this->mWaitTimeout : $age;
+
+               return ( $this->hasMasterChanges()
+                       || $this->lastMasterChangeTimestamp() > microtime( true ) - $age );
+       }
+
+       /**
+        * @param mixed $value
+        * @return mixed
+        */
+       public function waitTimeout( $value = null ) {
+               return wfSetVar( $this->mWaitTimeout, $value );
+       }
+
+       /**
+        * @return bool Whether the generic connection for reads is highly "lagged"
+        */
+       public function getLaggedSlaveMode() {
+               # Get a generic reader connection
+               $this->getConnection( DB_SLAVE );
+
+               return $this->mLaggedSlaveMode;
+       }
+
+       /**
+        * Disables/enables lag checks
+        * @param null|bool $mode
+        * @return bool
+        */
+       public function allowLagged( $mode = null ) {
+               if ( $mode === null ) {
+                       return $this->mAllowLagged;
+               }
+               $this->mAllowLagged = $mode;
+
+               return $this->mAllowLagged;
+       }
+
+       /**
+        * @return bool
+        */
+       public function pingAll() {
+               $success = true;
+               foreach ( $this->mConns as $conns2 ) {
+                       foreach ( $conns2 as $conns3 ) {
+                               /** @var DatabaseBase[] $conns3 */
+                               foreach ( $conns3 as $conn ) {
+                                       if ( !$conn->ping() ) {
+                                               $success = false;
+                                       }
+                               }
+                       }
+               }
+
+               return $success;
+       }
+
+       /**
+        * Call a function with each open connection object
+        * @param callable $callback
+        * @param array $params
+        */
+       public function forEachOpenConnection( $callback, array $params = array() ) {
+               foreach ( $this->mConns as $conns2 ) {
+                       foreach ( $conns2 as $conns3 ) {
+                               foreach ( $conns3 as $conn ) {
+                                       $mergedParams = array_merge( array( $conn ), $params );
+                                       call_user_func_array( $callback, $mergedParams );
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Get the hostname and lag time of the most-lagged slave
+        *
+        * This is useful for maintenance scripts that need to throttle their updates.
+        * May attempt to open connections to slaves on the default DB. If there is
+        * no lag, the maximum lag will be reported as -1.
+        *
+        * @param bool|string $wiki Wiki ID, or false for the default database
+        * @return array ( host, max lag, index of max lagged host )
+        */
+       public function getMaxLag( $wiki = false ) {
+               $maxLag = -1;
+               $host = '';
+               $maxIndex = 0;
+
+               if ( $this->getServerCount() <= 1 ) {
+                       return array( $host, $maxLag, $maxIndex ); // no replication = no lag
+               }
+
+               $lagTimes = $this->getLagTimes( $wiki );
+               foreach ( $lagTimes as $i => $lag ) {
+                       if ( $lag > $maxLag ) {
+                               $maxLag = $lag;
+                               $host = $this->mServers[$i]['host'];
+                               $maxIndex = $i;
+                       }
+               }
+
+               return array( $host, $maxLag, $maxIndex );
+       }
+
+       /**
+        * Get lag time for each server
+        *
+        * Results are cached for a short time in memcached/process cache
+        *
+        * @param string|bool $wiki
+        * @return int[] Map of (server index => seconds)
+        */
+       public function getLagTimes( $wiki = false ) {
+               if ( $this->getServerCount() <= 1 ) {
+                       return array( 0 => 0 ); // no replication = no lag
+               }
+
+               # Send the request to the load monitor
+               return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki );
+       }
+
+       /**
+        * Get the lag in seconds for a given connection, or zero if this load
+        * balancer does not have replication enabled.
+        *
+        * This should be used in preference to Database::getLag() in cases where
+        * replication may not be in use, since there is no way to determine if
+        * replication is in use at the connection level without running
+        * potentially restricted queries such as SHOW SLAVE STATUS. Using this
+        * function instead of Database::getLag() avoids a fatal error in this
+        * case on many installations.
+        *
+        * @param DatabaseBase $conn
+        * @return int
+        */
+       public function safeGetLag( $conn ) {
+               if ( $this->getServerCount() == 1 ) {
+                       return 0;
+               } else {
+                       return $conn->getLag();
+               }
+       }
+
+       /**
+        * Clear the cache for slag lag delay times
+        *
+        * This is only used for testing
+        */
+       public function clearLagTimeCache() {
+               $this->getLoadMonitor()->clearCaches();
+       }
+}
diff --git a/includes/db/loadbalancer/LoadMonitor.php b/includes/db/loadbalancer/LoadMonitor.php
new file mode 100644 (file)
index 0000000..4975ea1
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+/**
+ * Database load monitoring.
+ *
+ * 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 interface for database load monitoring
+ *
+ * @ingroup Database
+ */
+interface LoadMonitor {
+       /**
+        * Construct a new LoadMonitor with a given LoadBalancer parent
+        *
+        * @param LoadBalancer $parent
+        */
+       public function __construct( $parent );
+
+       /**
+        * Perform pre-connection load ratio adjustment.
+        * @param array $loads
+        * @param string|bool $group The selected query group. Default: false
+        * @param string|bool $wiki Default: false
+        */
+       public function scaleLoads( &$loads, $group = false, $wiki = false );
+
+       /**
+        * Return an estimate of replication lag for each server
+        *
+        * @param array $serverIndexes
+        * @param string $wiki
+        *
+        * @return array Map of (server index => seconds)
+        */
+       public function getLagTimes( $serverIndexes, $wiki );
+}
+
+class LoadMonitorNull implements LoadMonitor {
+       public function __construct( $parent ) {
+       }
+
+       public function scaleLoads( &$loads, $group = false, $wiki = false ) {
+       }
+
+       public function getLagTimes( $serverIndexes, $wiki ) {
+               return array_fill_keys( $serverIndexes, 0 );
+       }
+}
diff --git a/includes/db/loadbalancer/LoadMonitorMySQL.php b/includes/db/loadbalancer/LoadMonitorMySQL.php
new file mode 100644 (file)
index 0000000..3008419
--- /dev/null
@@ -0,0 +1,124 @@
+<?php
+/**
+ * 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
+ */
+
+/**
+ * Basic MySQL load monitor with no external dependencies
+ * Uses memcached to cache the replication lag for a short time
+ *
+ * @ingroup Database
+ */
+class LoadMonitorMySQL implements LoadMonitor {
+       /** @var LoadBalancer */
+       public $parent;
+       /** @var BagOStuff */
+       protected $srvCache;
+       /** @var BagOStuff */
+       protected $mainCache;
+
+       public function __construct( $parent ) {
+               $this->parent = $parent;
+
+               $this->srvCache = ObjectCache::newAccelerator( 'hash' );
+               $this->mainCache = wfGetMainCache();
+       }
+
+       public function scaleLoads( &$loads, $group = false, $wiki = false ) {
+       }
+
+       public function getLagTimes( $serverIndexes, $wiki ) {
+               if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
+                       # Single server only, just return zero without caching
+                       return array( 0 => 0 );
+               }
+
+               $key = $this->getLagTimeCacheKey();
+               # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
+               $ttl = mt_rand( 4e6, 5e6 ) / 1e6;
+               # Keep keys around longer as fallbacks
+               $staleTTL = 60;
+
+               # (a) Check the local APC cache
+               $value = $this->srvCache->get( $key );
+               if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
+                       wfDebugLog( 'replication',  __FUNCTION__ . ": got lag times ($key) from local cache" );
+                       return $value['lagTimes']; // cache hit
+               }
+               $staleValue = $value ?: false;
+
+               # (b) Check the shared cache and backfill APC
+               $value = $this->mainCache->get( $key );
+               if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
+                       $this->srvCache->set( $key, $value, $staleTTL );
+                       wfDebugLog( 'replication',  __FUNCTION__ . ": got lag times ($key) from main cache" );
+
+                       return $value['lagTimes']; // cache hit
+               }
+               $staleValue = $value ?: $staleValue;
+
+               # (c) Cache key missing or expired; regenerate and backfill
+               if ( $this->mainCache->lock( $key, 0, 10 ) ) {
+                       # Let this process alone update the cache value
+                       $cache = $this->mainCache;
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $unlocker = new ScopedCallback( function () use ( $cache, $key ) {
+                               $cache->unlock( $key );
+                       } );
+               } elseif ( $staleValue ) {
+                       # Could not acquire lock but an old cache exists, so use it
+                       return $staleValue['lagTimes'];
+               }
+
+               $lagTimes = array();
+               foreach ( $serverIndexes as $i ) {
+                       if ( $i == 0 ) { # Master
+                               $lagTimes[$i] = 0;
+                       } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
+                               $lagTimes[$i] = $conn->getLag();
+                       } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
+                               $lagTimes[$i] = $conn->getLag();
+                               # Close the connection to avoid sleeper connections piling up.
+                               # Note that the caller will pick one of these DBs and reconnect,
+                               # which is slightly inefficient, but this only matters for the lag
+                               # time cache miss cache, which is far less common that cache hits.
+                               $this->parent->closeConnection( $conn );
+                       }
+               }
+
+               # Add a timestamp key so we know when it was cached
+               $value = array( 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) );
+               $this->mainCache->set( $key, $value, $staleTTL );
+               $this->srvCache->set( $key, $value, $staleTTL );
+               wfDebugLog( 'replication',  __FUNCTION__ . ": re-calculated lag times ($key)" );
+
+               return $value['lagTimes'];
+       }
+
+       public function clearCaches() {
+               $key = $this->getLagTimeCacheKey();
+               $this->srvCache->delete( $key );
+               $this->mainCache->delete( $key );
+       }
+
+       private function getLagTimeCacheKey() {
+               # Lag is per-server, not per-DB, so key on the master DB name
+               return wfGlobalCacheKey( 'lag-times', $this->parent->getServerName( 0 ) );
+       }
+}
diff --git a/includes/utils/FileContentsHasher.php b/includes/utils/FileContentsHasher.php
new file mode 100644 (file)
index 0000000..67eb9d2
--- /dev/null
@@ -0,0 +1,111 @@
+<?php
+/**
+ * Generate hash digests of file contents to help with cache invalidation.
+ *
+ * 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
+ */
+class FileContentsHasher {
+
+       /** @var BagOStuff */
+       protected $cache;
+
+       /** @var FileContentsHasher */
+       private static $instance;
+
+       /**
+        * Constructor.
+        */
+       public function __construct() {
+               $this->cache = ObjectCache::newAccelerator( 'hash' );
+       }
+
+       /**
+        * Get the singleton instance of this class.
+        *
+        * @return FileContentsHasher
+        */
+       public static function singleton() {
+               if ( !self::$instance ) {
+                       self::$instance = new self;
+               }
+
+               return self::$instance;
+       }
+
+       /**
+        * Get a hash of a file's contents, either by retrieving a previously-
+        * computed hash from the cache, or by computing a hash from the file.
+        *
+        * @private
+        * @param string $filePath Full path to the file.
+        * @param string $algo Name of selected hashing algorithm.
+        * @return string|bool Hash of file contents, or false if the file could not be read.
+        */
+       public function getFileContentsHashInternal( $filePath, $algo = 'md4' ) {
+               $mtime = MediaWiki\quietCall( 'filemtime', $filePath );
+               if ( $mtime === false ) {
+                       return false;
+               }
+
+               $cacheKey = wfGlobalCacheKey( __CLASS__, $filePath, $mtime, $algo );
+               $hash = $this->cache->get( $cacheKey );
+
+               if ( $hash ) {
+                       return $hash;
+               }
+
+               $contents = MediaWiki\quietCall( 'file_get_contents', $filePath );
+               if ( $contents === false ) {
+                       return false;
+               }
+
+               $hash = hash( $algo, $contents );
+               $this->cache->set( $cacheKey, $hash, 60 * 60 * 24 );  // 24h
+
+               return $hash;
+       }
+
+       /**
+        * Get a hash of the combined contents of one or more files, either by
+        * retrieving a previously-computed hash from the cache, or by computing
+        * a hash from the files.
+        *
+        * @param string|string[] $filePaths One or more file paths.
+        * @param string $algo Name of selected hashing algorithm.
+        * @return string|bool Hash of files' contents, or false if no file could not be read.
+        */
+       public static function getFileContentsHash( $filePaths, $algo = 'md4' ) {
+               $instance = self::singleton();
+
+               if ( !is_array( $filePaths ) ) {
+                       $filePaths = (array) $filePaths;
+               }
+
+               if ( count( $filePaths ) === 1 ) {
+                       return $instance->getFileContentsHashInternal( $filePaths[0], $algo );
+               }
+
+               sort( $filePaths );
+               $hashes = array_map( function ( $filePath ) use ( $instance, $algo ) {
+                       return $instance->getFileContentsHashInternal( $filePath, $algo ) ?: '';
+               }, $filePaths );
+
+               $hashes = implode( '', $hashes );
+               return $hashes ? hash( $algo, $hashes ) : false;
+       }
+}
index 8265d69..be97d83 100644 (file)
@@ -34,6 +34,8 @@ if ( function_exists( 'mb_strtoupper' ) ) {
        mb_internal_encoding( 'UTF-8' );
 }
 
+use CLDRPluralRuleParser\Evaluator;
+
 /**
  * Internationalisation code
  * @ingroup Language
@@ -4968,7 +4970,7 @@ class Language {
         */
        public function getPluralRuleIndexNumber( $number ) {
                $pluralRules = $this->getCompiledPluralRules();
-               $form = CLDRPluralRuleEvaluator::evaluateCompiled( $number, $pluralRules );
+               $form = Evaluator::evaluateCompiled( $number, $pluralRules );
                return $form;
        }
 
diff --git a/languages/utils/CLDRPluralRuleConverter.php b/languages/utils/CLDRPluralRuleConverter.php
deleted file mode 100644 (file)
index 2eabcab..0000000
+++ /dev/null
@@ -1,322 +0,0 @@
-<?php
-/**
- * @author Niklas Laxström, Tim Starling
- *
- * @copyright Copyright © 2010-2012, Niklas Laxström
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
- *
- * @file
- * @since 1.20
- */
-
-/**
- * Helper class for converting rules to reverse polish notation (RPN).
- */
-class CLDRPluralRuleConverter {
-       /**
-        * The input string
-        *
-        * @var string
-        */
-       public $rule;
-
-       /**
-        * The current position
-        *
-        * @var int
-        */
-       public $pos;
-
-       /**
-        * The past-the-end position
-        *
-        * @var int
-        */
-       public $end;
-
-       /**
-        * The operator stack
-        *
-        * @var array
-        */
-       public $operators = array();
-
-       /**
-        * The operand stack
-        *
-        * @var array
-        */
-       public $operands = array();
-
-       /**
-        * Precedence levels. Note that there's no need to worry about associativity
-        * for the level 4 operators, since they return boolean and don't accept
-        * boolean inputs.
-        */
-       private static $precedence = array(
-               'or' => 2,
-               'and' => 3,
-               'is' => 4,
-               'is-not' => 4,
-               'in' => 4,
-               'not-in' => 4,
-               'within' => 4,
-               'not-within' => 4,
-               'mod' => 5,
-               ',' => 6,
-               '..' => 7,
-       );
-
-       /**
-        * A character list defining whitespace, for use in strspn() etc.
-        */
-       const WHITESPACE_CLASS = " \t\r\n";
-
-       /**
-        * Same for digits. Note that the grammar given in UTS #35 doesn't allow
-        * negative numbers or decimal separators.
-        */
-       const NUMBER_CLASS = '0123456789';
-
-       /**
-        * A character list of symbolic operands.
-        */
-       const OPERAND_SYMBOLS = 'nivwft';
-
-       /**
-        * An anchored regular expression which matches a word at the current offset.
-        */
-       const WORD_REGEX = '/[a-zA-Z@]+/A';
-
-       /**
-        * Convert a rule to RPN. This is the only public entry point.
-        *
-        * @param string $rule The rule to convert
-        * @return string The RPN representation of the rule
-        */
-       public static function convert( $rule ) {
-               $parser = new self( $rule );
-
-               return $parser->doConvert();
-       }
-
-       /**
-        * Private constructor.
-        * @param string $rule
-        */
-       protected function __construct( $rule ) {
-               $this->rule = $rule;
-               $this->pos = 0;
-               $this->end = strlen( $rule );
-       }
-
-       /**
-        * Do the operation.
-        *
-        * @return string The RPN representation of the rule (e.g. "5 3 mod n is")
-        */
-       protected function doConvert() {
-               $expectOperator = true;
-
-               // Iterate through all tokens, saving the operators and operands to a
-               // stack per Dijkstra's shunting yard algorithm.
-               /** @var CLDRPluralRuleConverterOperator $token */
-               while ( false !== ( $token = $this->nextToken() ) ) {
-                       // In this grammar, there are only binary operators, so every valid
-                       // rule string will alternate between operator and operand tokens.
-                       $expectOperator = !$expectOperator;
-
-                       if ( $token instanceof CLDRPluralRuleConverterExpression ) {
-                               // Operand
-                               if ( $expectOperator ) {
-                                       $token->error( 'unexpected operand' );
-                               }
-                               $this->operands[] = $token;
-                               continue;
-                       } else {
-                               // Operator
-                               if ( !$expectOperator ) {
-                                       $token->error( 'unexpected operator' );
-                               }
-                               // Resolve higher precedence levels
-                               $lastOp = end( $this->operators );
-                               while ( $lastOp && self::$precedence[$token->name] <= self::$precedence[$lastOp->name] ) {
-                                       $this->doOperation( $lastOp, $this->operands );
-                                       array_pop( $this->operators );
-                                       $lastOp = end( $this->operators );
-                               }
-                               $this->operators[] = $token;
-                       }
-               }
-
-               // Finish off the stack
-               while ( $op = array_pop( $this->operators ) ) {
-                       $this->doOperation( $op, $this->operands );
-               }
-
-               // Make sure the result is sane. The first case is possible for an empty
-               // string input, the second should be unreachable.
-               if ( !count( $this->operands ) ) {
-                       $this->error( 'condition expected' );
-               } elseif ( count( $this->operands ) > 1 ) {
-                       $this->error( 'missing operator or too many operands' );
-               }
-
-               $value = $this->operands[0];
-               if ( $value->type !== 'boolean' ) {
-                       $this->error( 'the result must have a boolean type' );
-               }
-
-               return $this->operands[0]->rpn;
-       }
-
-       /**
-        * Fetch the next token from the input string.
-        *
-        * @return CLDRPluralRuleConverterFragment The next token
-        */
-       protected function nextToken() {
-               if ( $this->pos >= $this->end ) {
-                       return false;
-               }
-
-               // Whitespace
-               $length = strspn( $this->rule, self::WHITESPACE_CLASS, $this->pos );
-               $this->pos += $length;
-
-               if ( $this->pos >= $this->end ) {
-                       return false;
-               }
-
-               // Number
-               $length = strspn( $this->rule, self::NUMBER_CLASS, $this->pos );
-               if ( $length !== 0 ) {
-                       $token = $this->newNumber( substr( $this->rule, $this->pos, $length ), $this->pos );
-                       $this->pos += $length;
-
-                       return $token;
-               }
-
-               // Two-character operators
-               $op2 = substr( $this->rule, $this->pos, 2 );
-               if ( $op2 === '..' || $op2 === '!=' ) {
-                       $token = $this->newOperator( $op2, $this->pos, 2 );
-                       $this->pos += 2;
-
-                       return $token;
-               }
-
-               // Single-character operators
-               $op1 = $this->rule[$this->pos];
-               if ( $op1 === ',' || $op1 === '=' || $op1 === '%' ) {
-                       $token = $this->newOperator( $op1, $this->pos, 1 );
-                       $this->pos++;
-
-                       return $token;
-               }
-
-               // Word
-               if ( !preg_match( self::WORD_REGEX, $this->rule, $m, 0, $this->pos ) ) {
-                       $this->error( 'unexpected character "' . $this->rule[$this->pos] . '"' );
-               }
-               $word1 = strtolower( $m[0] );
-               $word2 = '';
-               $nextTokenPos = $this->pos + strlen( $word1 );
-               if ( $word1 === 'not' || $word1 === 'is' ) {
-                       // Look ahead one word
-                       $nextTokenPos += strspn( $this->rule, self::WHITESPACE_CLASS, $nextTokenPos );
-                       if ( $nextTokenPos < $this->end
-                               && preg_match( self::WORD_REGEX, $this->rule, $m, 0, $nextTokenPos )
-                       ) {
-                               $word2 = strtolower( $m[0] );
-                               $nextTokenPos += strlen( $word2 );
-                       }
-               }
-
-               // Two-word operators like "is not" take precedence over single-word operators like "is"
-               if ( $word2 !== '' ) {
-                       $bothWords = "{$word1}-{$word2}";
-                       if ( isset( self::$precedence[$bothWords] ) ) {
-                               $token = $this->newOperator( $bothWords, $this->pos, $nextTokenPos - $this->pos );
-                               $this->pos = $nextTokenPos;
-
-                               return $token;
-                       }
-               }
-
-               // Single-word operators
-               if ( isset( self::$precedence[$word1] ) ) {
-                       $token = $this->newOperator( $word1, $this->pos, strlen( $word1 ) );
-                       $this->pos += strlen( $word1 );
-
-                       return $token;
-               }
-
-               // The single-character operand symbols
-               if ( strpos( self::OPERAND_SYMBOLS, $word1 ) !== false ) {
-                       $token = $this->newNumber( $word1, $this->pos );
-                       $this->pos++;
-
-                       return $token;
-               }
-
-               // Samples
-               if ( $word1 === '@integer' || $word1 === '@decimal' ) {
-                       // Samples are like comments, they have no effect on rule evaluation.
-                       // They run from the first sample indicator to the end of the string.
-                       $this->pos = $this->end;
-
-                       return false;
-               }
-
-               $this->error( 'unrecognised word' );
-       }
-
-       /**
-        * For the binary operator $op, pop its operands off the stack and push
-        * a fragment with rpn and type members describing the result of that
-        * operation.
-        *
-        * @param CLDRPluralRuleConverterOperator $op
-        */
-       protected function doOperation( $op ) {
-               if ( count( $this->operands ) < 2 ) {
-                       $op->error( 'missing operand' );
-               }
-               $right = array_pop( $this->operands );
-               $left = array_pop( $this->operands );
-               $result = $op->operate( $left, $right );
-               $this->operands[] = $result;
-       }
-
-       /**
-        * Create a numerical expression object
-        *
-        * @param string $text
-        * @param int $pos
-        * @return CLDRPluralRuleConverterExpression The numerical expression
-        */
-       protected function newNumber( $text, $pos ) {
-               return new CLDRPluralRuleConverterExpression( $this, 'number', $text, $pos, strlen( $text ) );
-       }
-
-       /**
-        * Create a binary operator
-        *
-        * @param string $type
-        * @param int $pos
-        * @param int $length
-        * @return CLDRPluralRuleConverterOperator The operator
-        */
-       protected function newOperator( $type, $pos, $length ) {
-               return new CLDRPluralRuleConverterOperator( $this, $type, $pos, $length );
-       }
-
-       /**
-        * Throw an error
-        * @param string $message
-        */
-       protected function error( $message ) {
-               throw new CLDRPluralRuleError( $message );
-       }
-}
diff --git a/languages/utils/CLDRPluralRuleConverterExpression.php b/languages/utils/CLDRPluralRuleConverterExpression.php
deleted file mode 100644 (file)
index 1ee6b4c..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-/**
- * @author Niklas Laxström, Tim Starling
- *
- * @copyright Copyright © 2010-2012, Niklas Laxström
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
- *
- * @file
- * @since 1.20
- */
-
-/**
- * Helper for CLDRPluralRuleConverter.
- * An expression object, representing a region of the input string (for error
- * messages), the RPN notation used to evaluate it, and the result type for
- * validation.
- */
-class CLDRPluralRuleConverterExpression extends CLDRPluralRuleConverterFragment {
-       /** @var string */
-       public $type;
-
-       /** @var string */
-       public $rpn;
-
-       function __construct( $parser, $type, $rpn, $pos, $length ) {
-               parent::__construct( $parser, $pos, $length );
-               $this->type = $type;
-               $this->rpn = $rpn;
-       }
-
-       public function isType( $type ) {
-               if ( $type === 'range' && ( $this->type === 'range' || $this->type === 'number' ) ) {
-                       return true;
-               }
-               if ( $type === $this->type ) {
-                       return true;
-               }
-
-               return false;
-       }
-}
diff --git a/languages/utils/CLDRPluralRuleConverterFragment.php b/languages/utils/CLDRPluralRuleConverterFragment.php
deleted file mode 100644 (file)
index df299cb..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-<?php
-/**
- * @author Niklas Laxström, Tim Starling
- *
- * @copyright Copyright © 2010-2012, Niklas Laxström
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
- *
- * @file
- * @since 1.20
- */
-
-/**
- * Helper for CLDRPluralRuleConverter.
- * The base class for operators and expressions, describing a region of the input string.
- */
-class CLDRPluralRuleConverterFragment {
-       public $parser, $pos, $length, $end;
-
-       function __construct( $parser, $pos, $length ) {
-               $this->parser = $parser;
-               $this->pos = $pos;
-               $this->length = $length;
-               $this->end = $pos + $length;
-       }
-
-       public function error( $message ) {
-               $text = $this->getText();
-               throw new CLDRPluralRuleError( "$message at position " . ( $this->pos + 1 ) . ": \"$text\"" );
-       }
-
-       public function getText() {
-               return substr( $this->parser->rule, $this->pos, $this->length );
-       }
-}
diff --git a/languages/utils/CLDRPluralRuleConverterOperator.php b/languages/utils/CLDRPluralRuleConverterOperator.php
deleted file mode 100644 (file)
index de17f29..0000000
+++ /dev/null
@@ -1,114 +0,0 @@
-<?php
-/**
- * @author Niklas Laxström, Tim Starling
- *
- * @copyright Copyright © 2010-2012, Niklas Laxström
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
- *
- * @file
- * @since 1.20
- */
-
-/**
- * Helper for CLDRPluralRuleConverter.
- * An operator object, representing a region of the input string (for error
- * messages), and the binary operator at that location.
- */
-class CLDRPluralRuleConverterOperator extends CLDRPluralRuleConverterFragment {
-       /** @var string The name */
-       public $name;
-
-       /**
-        * Each op type has three characters: left operand type, right operand type and result type
-        *
-        *   b = boolean
-        *   n = number
-        *   r = range
-        *
-        * A number is a kind of range.
-        *
-        * @var array
-        */
-       private static $opTypes = array(
-               'or' => 'bbb',
-               'and' => 'bbb',
-               'is' => 'nnb',
-               'is-not' => 'nnb',
-               'in' => 'nrb',
-               'not-in' => 'nrb',
-               'within' => 'nrb',
-               'not-within' => 'nrb',
-               'mod' => 'nnn',
-               ',' => 'rrr',
-               '..' => 'nnr',
-       );
-
-       /**
-        * Map converting from the abbrevation to the full form.
-        *
-        * @var array
-        */
-       private static $typeSpecMap = array(
-               'b' => 'boolean',
-               'n' => 'number',
-               'r' => 'range',
-       );
-
-       /**
-        * Map for converting the new operators introduced in Rev 33 to the old forms
-        */
-       private static $aliasMap = array(
-               '%' => 'mod',
-               '!=' => 'not-in',
-               '=' => 'in'
-       );
-
-       /**
-        * Initialize a new instance of a CLDRPluralRuleConverterOperator object
-        *
-        * @param CLDRPluralRuleConverter $parser The parser
-        * @param string $name The operator name
-        * @param int $pos The length
-        * @param int $length
-        */
-       function __construct( $parser, $name, $pos, $length ) {
-               parent::__construct( $parser, $pos, $length );
-               if ( isset( self::$aliasMap[$name] ) ) {
-                       $name = self::$aliasMap[$name];
-               }
-               $this->name = $name;
-       }
-
-       /**
-        * Compute the operation
-        *
-        * @param CLDRPluralRuleConverterExpression $left The left part of the expression
-        * @param CLDRPluralRuleConverterExpression $right The right part of the expression
-        * @return CLDRPluralRuleConverterExpression The result of the operation
-        */
-       public function operate( $left, $right ) {
-               $typeSpec = self::$opTypes[$this->name];
-
-               $leftType = self::$typeSpecMap[$typeSpec[0]];
-               $rightType = self::$typeSpecMap[$typeSpec[1]];
-               $resultType = self::$typeSpecMap[$typeSpec[2]];
-
-               $start = min( $this->pos, $left->pos, $right->pos );
-               $end = max( $this->end, $left->end, $right->end );
-               $length = $end - $start;
-
-               $newExpr = new CLDRPluralRuleConverterExpression( $this->parser, $resultType,
-                       "{$left->rpn} {$right->rpn} {$this->name}",
-                       $start, $length );
-
-               if ( !$left->isType( $leftType ) ) {
-                       $newExpr->error( "invalid type for left operand: expected $leftType, got {$left->type}" );
-               }
-
-               if ( !$right->isType( $rightType ) ) {
-                       $newExpr->error( "invalid type for right operand: expected $rightType, got {$right->type}" );
-               }
-
-               return $newExpr;
-       }
-}
diff --git a/languages/utils/CLDRPluralRuleError.php b/languages/utils/CLDRPluralRuleError.php
deleted file mode 100644 (file)
index cc0b5d2..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-/**
- * @author Niklas Laxström, Tim Starling
- *
- * @copyright Copyright © 2010-2012, Niklas Laxström
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
- *
- * @file
- * @since 1.20
- */
-
-/**
- * The exception class for all the classes in this file. This will be thrown
- * back to the caller if there is any validation error.
- */
-class CLDRPluralRuleError extends MWException {
-       function __construct( $message ) {
-               parent::__construct( 'CLDR plural rule error: ' . $message );
-       }
-}
diff --git a/languages/utils/CLDRPluralRuleEvaluator.php b/languages/utils/CLDRPluralRuleEvaluator.php
deleted file mode 100644 (file)
index 7e7208a..0000000
+++ /dev/null
@@ -1,187 +0,0 @@
-<?php
-
-/**
- * Parse and evaluate a plural rule.
- *
- * UTS #35 Revision 33
- * http://www.unicode.org/reports/tr35/tr35-33/tr35-numbers.html#Language_Plural_Rules
- *
- * @author Niklas Laxström, Tim Starling
- *
- * @copyright Copyright © 2010-2012, Niklas Laxström
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0
- * or later
- *
- * 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
- * @since 1.20
- */
-class CLDRPluralRuleEvaluator {
-       /**
-        * Evaluate a number against a set of plural rules. If a rule passes,
-        * return the index of plural rule.
-        *
-        * @param int $number The number to be evaluated against the rules
-        * @param array $rules The associative array of plural rules in pluralform => rule format.
-        * @return int The index of the plural form which passed the evaluation
-        */
-       public static function evaluate( $number, array $rules ) {
-               $rules = self::compile( $rules );
-
-               return self::evaluateCompiled( $number, $rules );
-       }
-
-       /**
-        * Convert a set of rules to a compiled form which is optimised for
-        * fast evaluation. The result will be an array of strings, and may be cached.
-        *
-        * @param array $rules The rules to compile
-        * @return array An array of compile rules.
-        */
-       public static function compile( array $rules ) {
-               // We can't use array_map() for this because it generates a warning if
-               // there is an exception.
-               foreach ( $rules as &$rule ) {
-                       $rule = CLDRPluralRuleConverter::convert( $rule );
-               }
-
-               return $rules;
-       }
-
-       /**
-        * Evaluate a compiled set of rules returned by compile(). Do not allow
-        * the user to edit the compiled form, or else PHP errors may result.
-        *
-        * @param string $number The number to be evaluated against the rules, in English, or it
-        *   may be a type convertible to string.
-        * @param array $rules The associative array of plural rules in pluralform => rule format.
-        * @return int The index of the plural form which passed the evaluation
-        */
-       public static function evaluateCompiled( $number, array $rules ) {
-               // Calculate the values of the operand symbols
-               $number = strval( $number );
-               if ( !preg_match( '/^ -? ( ([0-9]+) (?: \. ([0-9]+) )? )$/x', $number, $m ) ) {
-                       wfDebug( __METHOD__ . ": invalid number input, returning 'other'\n" );
-
-                       return count( $rules );
-               }
-               if ( !isset( $m[3] ) ) {
-                       $operandSymbols = array(
-                               'n' => intval( $m[1] ),
-                               'i' => intval( $m[1] ),
-                               'v' => 0,
-                               'w' => 0,
-                               'f' => 0,
-                               't' => 0
-                       );
-               } else {
-                       $absValStr = $m[1];
-                       $intStr = $m[2];
-                       $fracStr = $m[3];
-                       $operandSymbols = array(
-                               'n' => floatval( $absValStr ),
-                               'i' => intval( $intStr ),
-                               'v' => strlen( $fracStr ),
-                               'w' => strlen( rtrim( $fracStr, '0' ) ),
-                               'f' => intval( $fracStr ),
-                               't' => intval( rtrim( $fracStr, '0' ) ),
-                       );
-               }
-
-               // The compiled form is RPN, with tokens strictly delimited by
-               // spaces, so this is a simple RPN evaluator.
-               foreach ( $rules as $i => $rule ) {
-                       $stack = array();
-                       $zero = ord( '0' );
-                       $nine = ord( '9' );
-                       foreach ( StringUtils::explode( ' ', $rule ) as $token ) {
-                               $ord = ord( $token );
-                               if ( isset( $operandSymbols[$token] ) ) {
-                                       $stack[] = $operandSymbols[$token];
-                               } elseif ( $ord >= $zero && $ord <= $nine ) {
-                                       $stack[] = intval( $token );
-                               } else {
-                                       $right = array_pop( $stack );
-                                       $left = array_pop( $stack );
-                                       $result = self::doOperation( $token, $left, $right );
-                                       $stack[] = $result;
-                               }
-                       }
-                       if ( $stack[0] ) {
-                               return $i;
-                       }
-               }
-               // None of the provided rules match. The number belongs to category
-               // 'other', which comes last.
-               return count( $rules );
-       }
-
-       /**
-        * Do a single operation
-        *
-        * @param string $token The token string
-        * @param mixed $left The left operand. If it is an object, its state may be destroyed.
-        * @param mixed $right The right operand
-        * @throws CLDRPluralRuleError
-        * @return mixed The operation result
-        */
-       private static function doOperation( $token, $left, $right ) {
-               if ( in_array( $token, array( 'in', 'not-in', 'within', 'not-within' ) ) ) {
-                       if ( !( $right instanceof CLDRPluralRuleEvaluatorRange ) ) {
-                               $right = new CLDRPluralRuleEvaluatorRange( $right );
-                       }
-               }
-               switch ( $token ) {
-                       case 'or':
-                               return $left || $right;
-                       case 'and':
-                               return $left && $right;
-                       case 'is':
-                               return $left == $right;
-                       case 'is-not':
-                               return $left != $right;
-                       case 'in':
-                               return $right->isNumberIn( $left );
-                       case 'not-in':
-                               return !$right->isNumberIn( $left );
-                       case 'within':
-                               return $right->isNumberWithin( $left );
-                       case 'not-within':
-                               return !$right->isNumberWithin( $left );
-                       case 'mod':
-                               if ( is_int( $left ) ) {
-                                       return (int)fmod( $left, $right );
-                               }
-
-                               return fmod( $left, $right );
-                       case ',':
-                               if ( $left instanceof CLDRPluralRuleEvaluatorRange ) {
-                                       $range = $left;
-                               } else {
-                                       $range = new CLDRPluralRuleEvaluatorRange( $left );
-                               }
-                               $range->add( $right );
-
-                               return $range;
-                       case '..':
-                               return new CLDRPluralRuleEvaluatorRange( $left, $right );
-                       default:
-                               throw new CLDRPluralRuleError( "Invalid RPN token" );
-               }
-       }
-}
diff --git a/languages/utils/CLDRPluralRuleEvaluatorRange.php b/languages/utils/CLDRPluralRuleEvaluatorRange.php
deleted file mode 100644 (file)
index 996c22e..0000000
+++ /dev/null
@@ -1,110 +0,0 @@
-<?php
-/**
- * @author Niklas Laxström, Tim Starling
- *
- * @copyright Copyright © 2010-2012, Niklas Laxström
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
- *
- * @file
- * @since 1.20
- */
-
-/**
- * Evaluator helper class representing a range list.
- */
-class CLDRPluralRuleEvaluatorRange {
-       /**
-        * The parts
-        *
-        * @var array
-        */
-       public $parts = array();
-
-       /**
-        * Initialize a new instance of CLDRPluralRuleEvaluatorRange
-        *
-        * @param int $start The start of the range
-        * @param int|bool $end The end of the range, or false if the range is not bounded.
-        */
-       function __construct( $start, $end = false ) {
-               if ( $end === false ) {
-                       $this->parts[] = $start;
-               } else {
-                       $this->parts[] = array( $start, $end );
-               }
-       }
-
-       /**
-        * Determine if the given number is inside the range.
-        *
-        * @param int $number The number to check
-        * @param bool $integerConstraint If true, also asserts the number is an integer;
-        *   otherwise, number simply has to be inside the range.
-        * @return bool True if the number is inside the range; otherwise, false.
-        */
-       function isNumberIn( $number, $integerConstraint = true ) {
-               foreach ( $this->parts as $part ) {
-                       if ( is_array( $part ) ) {
-                               if ( ( !$integerConstraint || floor( $number ) === (float)$number )
-                                       && $number >= $part[0] && $number <= $part[1]
-                               ) {
-                                       return true;
-                               }
-                       } else {
-                               if ( $number == $part ) {
-                                       return true;
-                               }
-                       }
-               }
-
-               return false;
-       }
-
-       /**
-        * Readable alias for isNumberIn( $number, false ), and the implementation
-        * of the "within" operator.
-        *
-        * @param int $number The number to check
-        * @return bool True if the number is inside the range; otherwise, false.
-        */
-       function isNumberWithin( $number ) {
-               return $this->isNumberIn( $number, false );
-       }
-
-       /**
-        * Add another part to this range.
-        *
-        * @param CLDRPluralRuleEvaluatorRange|int $other The part to add, either
-        *   a range object itself or a single number.
-        */
-       function add( $other ) {
-               if ( $other instanceof self ) {
-                       $this->parts = array_merge( $this->parts, $other->parts );
-               } else {
-                       $this->parts[] = $other;
-               }
-       }
-
-       /**
-        * Returns the string representation of the rule evaluator range.
-        * The purpose of this method is to help debugging.
-        *
-        * @return string The string representation of the rule evaluator range
-        */
-       function __toString() {
-               $s = 'Range(';
-               foreach ( $this->parts as $i => $part ) {
-                       if ( $i ) {
-                               $s .= ', ';
-                       }
-                       if ( is_array( $part ) ) {
-                               $s .= $part[0] . '..' . $part[1];
-                       } else {
-                               $s .= $part;
-                       }
-               }
-               $s .= ')';
-
-               return $s;
-       }
-}
index eaa138b..f6857e8 100644 (file)
                                new RegExp( /(https?|ftp|file):\/\// )
                        ],
                        isoDate: [
-                               new RegExp( /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/ )
+                               new RegExp( /^([-+]?\d{1,4})-([01]\d)-([0-3]\d)([T\s]((([01]\d|2[0-3])(:?[0-5]\d)?|24:?00)?(:?([0-5]\d))?([.,]\d+)?)([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?/ ),
+                               new RegExp( /^([-+]?\d{1,4})-([01]\d)-([0-3]\d)/ )
                        ],
                        usLongDate: [
                                new RegExp( /^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/ )
                        return ts.rgx.isoDate[ 0 ].test( s );
                },
                format: function ( s ) {
-                       return $.tablesorter.formatFloat( ( s !== '' ) ? new Date( s.replace(
-                       new RegExp( /-/g ), '/' ) ).getTime() : '0' );
+                       var isodate,
+                               matches;
+                       if ( !Date.prototype.toISOString ) {
+                               // Old browsers don't understand iso, Fallback to US date parsing and ignore the time part.
+                               matches = $.trim( s ).match( ts.rgx.isoDate[ 1 ] );
+                               if ( matches ) {
+                                       isodate = new Date( matches[ 2 ]  + '/' + matches[ 3 ] + '/' + matches[ 1 ] );
+                               } else {
+                                       return $.tablesorter.formatFloat( 0 );
+                               }
+                       } else {
+                               isodate = new Date( $.trim( s ) );
+                       }
+                       return $.tablesorter.formatFloat( ( isodate !== undefined ) ? isodate.getTime() : 0 );
                },
                type: 'numeric'
        } );
index ec95665..bd30397 100644 (file)
@@ -103,6 +103,12 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                 */
                ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
 
+               // Sandbox APC by replacing with in-process hash instead.
+               // Ensures values are removed between tests.
+               ObjectCache::$instances['apc'] =
+               ObjectCache::$instances['xcache'] =
+               ObjectCache::$instances['wincache'] = new HashBagOStuff;
+
                $needsResetDB = false;
 
                if ( $this->needsDB() ) {
diff --git a/tests/phpunit/data/filecontentshasher/hash.svg b/tests/phpunit/data/filecontentshasher/hash.svg
new file mode 100644 (file)
index 0000000..44068ba
--- /dev/null
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/"
+   xmlns:cc="http://creativecommons.org/ns#"
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+   xmlns:svg="http://www.w3.org/2000/svg"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   width="200"
+   height="200"
+   id="svg2"
+   version="1.1"
+   inkscape:version="0.48.2 r9819"
+   sodipodi:docname="New document 1">
+  <defs
+     id="defs4" />
+  <sodipodi:namedview
+     id="base"
+     pagecolor="#ffffff"
+     bordercolor="#666666"
+     borderopacity="1.0"
+     inkscape:pageopacity="0.0"
+     inkscape:pageshadow="2"
+     inkscape:zoom="1.765"
+     inkscape:cx="101.66909"
+     inkscape:cy="64.929256"
+     inkscape:document-units="px"
+     inkscape:current-layer="layer1"
+     showgrid="false"
+     fit-margin-top="0"
+     fit-margin-left="0"
+     fit-margin-right="0"
+     fit-margin-bottom="0"
+     showborder="false"
+     inkscape:showpageshadow="false"
+     inkscape:window-width="1132"
+     inkscape:window-height="961"
+     inkscape:window-x="0"
+     inkscape:window-y="0"
+     inkscape:window-maximized="0" />
+  <metadata
+     id="metadata7">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
+        <dc:title></dc:title>
+      </cc:Work>
+    </rdf:RDF>
+  </metadata>
+  <g
+     inkscape:label="Layer 1"
+     inkscape:groupmode="layer"
+     id="layer1"
+     transform="translate(-193.20113,-609.30267)">
+    <g
+       id="g3829">
+      <g
+         transform="translate(-61.473095,237.81998)"
+         id="g3821">
+        <rect
+           style="fill:#000000;fill-opacity:1;stroke:none"
+           id="rect2998"
+           width="120.67989"
+           height="19.546741"
+           x="298.017"
+           y="434.23184"
+           ry="0" />
+        <rect
+           style="fill:#000000;fill-opacity:1;stroke:none"
+           id="rect2998-1"
+           width="120.67989"
+           height="19.546741"
+           x="290.65155"
+           y="488.76447"
+           ry="0" />
+        <rect
+           style="fill:#000000;fill-opacity:1;stroke:none"
+           id="rect2998-7"
+           width="183.60896"
+           height="19.546741"
+           x="384.33142"
+           y="-455.46609"
+           ry="0"
+           transform="matrix(-0.13731609,0.99052728,-1,0,0,0)" />
+        <rect
+           style="fill:#000000;fill-opacity:1;stroke:none"
+           id="rect2998-7-4"
+           width="183.60896"
+           height="19.546741"
+           x="384.04288"
+           y="-406.21848"
+           ry="0"
+           transform="matrix(-0.13731609,0.99052728,-1,0,0,0)" />
+      </g>
+      <rect
+         y="609.30267"
+         x="193.20113"
+         height="200"
+         width="200"
+         id="rect3827"
+         style="fill:none;stroke:none" />
+    </g>
+  </g>
+</svg>
diff --git a/tests/phpunit/data/filecontentshasher/primes.txt b/tests/phpunit/data/filecontentshasher/primes.txt
new file mode 100644 (file)
index 0000000..a2fe1fb
--- /dev/null
@@ -0,0 +1,105 @@
+                         The First 1,000 Primes
+                          (the 1,000th is 7919)
+         For more information on primes see http://primes.utm.edu/
+
+      2      3      5      7     11     13     17     19     23     29
+     31     37     41     43     47     53     59     61     67     71
+     73     79     83     89     97    101    103    107    109    113
+    127    131    137    139    149    151    157    163    167    173
+    179    181    191    193    197    199    211    223    227    229
+    233    239    241    251    257    263    269    271    277    281
+    283    293    307    311    313    317    331    337    347    349
+    353    359    367    373    379    383    389    397    401    409
+    419    421    431    433    439    443    449    457    461    463
+    467    479    487    491    499    503    509    521    523    541
+    547    557    563    569    571    577    587    593    599    601
+    607    613    617    619    631    641    643    647    653    659
+    661    673    677    683    691    701    709    719    727    733
+    739    743    751    757    761    769    773    787    797    809
+    811    821    823    827    829    839    853    857    859    863
+    877    881    883    887    907    911    919    929    937    941
+    947    953    967    971    977    983    991    997   1009   1013
+   1019   1021   1031   1033   1039   1049   1051   1061   1063   1069
+   1087   1091   1093   1097   1103   1109   1117   1123   1129   1151
+   1153   1163   1171   1181   1187   1193   1201   1213   1217   1223
+   1229   1231   1237   1249   1259   1277   1279   1283   1289   1291
+   1297   1301   1303   1307   1319   1321   1327   1361   1367   1373
+   1381   1399   1409   1423   1427   1429   1433   1439   1447   1451
+   1453   1459   1471   1481   1483   1487   1489   1493   1499   1511
+   1523   1531   1543   1549   1553   1559   1567   1571   1579   1583
+   1597   1601   1607   1609   1613   1619   1621   1627   1637   1657
+   1663   1667   1669   1693   1697   1699   1709   1721   1723   1733
+   1741   1747   1753   1759   1777   1783   1787   1789   1801   1811
+   1823   1831   1847   1861   1867   1871   1873   1877   1879   1889
+   1901   1907   1913   1931   1933   1949   1951   1973   1979   1987
+   1993   1997   1999   2003   2011   2017   2027   2029   2039   2053
+   2063   2069   2081   2083   2087   2089   2099   2111   2113   2129
+   2131   2137   2141   2143   2153   2161   2179   2203   2207   2213
+   2221   2237   2239   2243   2251   2267   2269   2273   2281   2287
+   2293   2297   2309   2311   2333   2339   2341   2347   2351   2357
+   2371   2377   2381   2383   2389   2393   2399   2411   2417   2423
+   2437   2441   2447   2459   2467   2473   2477   2503   2521   2531
+   2539   2543   2549   2551   2557   2579   2591   2593   2609   2617
+   2621   2633   2647   2657   2659   2663   2671   2677   2683   2687
+   2689   2693   2699   2707   2711   2713   2719   2729   2731   2741
+   2749   2753   2767   2777   2789   2791   2797   2801   2803   2819
+   2833   2837   2843   2851   2857   2861   2879   2887   2897   2903
+   2909   2917   2927   2939   2953   2957   2963   2969   2971   2999
+   3001   3011   3019   3023   3037   3041   3049   3061   3067   3079
+   3083   3089   3109   3119   3121   3137   3163   3167   3169   3181
+   3187   3191   3203   3209   3217   3221   3229   3251   3253   3257
+   3259   3271   3299   3301   3307   3313   3319   3323   3329   3331
+   3343   3347   3359   3361   3371   3373   3389   3391   3407   3413
+   3433   3449   3457   3461   3463   3467   3469   3491   3499   3511
+   3517   3527   3529   3533   3539   3541   3547   3557   3559   3571
+   3581   3583   3593   3607   3613   3617   3623   3631   3637   3643
+   3659   3671   3673   3677   3691   3697   3701   3709   3719   3727
+   3733   3739   3761   3767   3769   3779   3793   3797   3803   3821
+   3823   3833   3847   3851   3853   3863   3877   3881   3889   3907
+   3911   3917   3919   3923   3929   3931   3943   3947   3967   3989
+   4001   4003   4007   4013   4019   4021   4027   4049   4051   4057
+   4073   4079   4091   4093   4099   4111   4127   4129   4133   4139
+   4153   4157   4159   4177   4201   4211   4217   4219   4229   4231
+   4241   4243   4253   4259   4261   4271   4273   4283   4289   4297
+   4327   4337   4339   4349   4357   4363   4373   4391   4397   4409
+   4421   4423   4441   4447   4451   4457   4463   4481   4483   4493
+   4507   4513   4517   4519   4523   4547   4549   4561   4567   4583
+   4591   4597   4603   4621   4637   4639   4643   4649   4651   4657
+   4663   4673   4679   4691   4703   4721   4723   4729   4733   4751
+   4759   4783   4787   4789   4793   4799   4801   4813   4817   4831
+   4861   4871   4877   4889   4903   4909   4919   4931   4933   4937
+   4943   4951   4957   4967   4969   4973   4987   4993   4999   5003
+   5009   5011   5021   5023   5039   5051   5059   5077   5081   5087
+   5099   5101   5107   5113   5119   5147   5153   5167   5171   5179
+   5189   5197   5209   5227   5231   5233   5237   5261   5273   5279
+   5281   5297   5303   5309   5323   5333   5347   5351   5381   5387
+   5393   5399   5407   5413   5417   5419   5431   5437   5441   5443
+   5449   5471   5477   5479   5483   5501   5503   5507   5519   5521
+   5527   5531   5557   5563   5569   5573   5581   5591   5623   5639
+   5641   5647   5651   5653   5657   5659   5669   5683   5689   5693
+   5701   5711   5717   5737   5741   5743   5749   5779   5783   5791
+   5801   5807   5813   5821   5827   5839   5843   5849   5851   5857
+   5861   5867   5869   5879   5881   5897   5903   5923   5927   5939
+   5953   5981   5987   6007   6011   6029   6037   6043   6047   6053
+   6067   6073   6079   6089   6091   6101   6113   6121   6131   6133
+   6143   6151   6163   6173   6197   6199   6203   6211   6217   6221
+   6229   6247   6257   6263   6269   6271   6277   6287   6299   6301
+   6311   6317   6323   6329   6337   6343   6353   6359   6361   6367
+   6373   6379   6389   6397   6421   6427   6449   6451   6469   6473
+   6481   6491   6521   6529   6547   6551   6553   6563   6569   6571
+   6577   6581   6599   6607   6619   6637   6653   6659   6661   6673
+   6679   6689   6691   6701   6703   6709   6719   6733   6737   6761
+   6763   6779   6781   6791   6793   6803   6823   6827   6829   6833
+   6841   6857   6863   6869   6871   6883   6899   6907   6911   6917
+   6947   6949   6959   6961   6967   6971   6977   6983   6991   6997
+   7001   7013   7019   7027   7039   7043   7057   7069   7079   7103
+   7109   7121   7127   7129   7151   7159   7177   7187   7193   7207
+   7211   7213   7219   7229   7237   7243   7247   7253   7283   7297
+   7307   7309   7321   7331   7333   7349   7351   7369   7393   7411
+   7417   7433   7451   7457   7459   7477   7481   7487   7489   7499
+   7507   7517   7523   7529   7537   7541   7547   7549   7559   7561
+   7573   7577   7583   7589   7591   7603   7607   7621   7639   7643
+   7649   7669   7673   7681   7687   7691   7699   7703   7717   7723
+   7727   7741   7753   7757   7759   7789   7793   7817   7823   7829
+   7841   7853   7867   7873   7877   7879   7883   7901   7907   7919
+end.
diff --git a/tests/phpunit/includes/utils/FileContentsHasherTest.php b/tests/phpunit/includes/utils/FileContentsHasherTest.php
new file mode 100644 (file)
index 0000000..a03e1fc
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+/**
+ * @covers FileContentsHasherTest
+ */
+class FileContentsHasherTest extends MediaWikiTestCase {
+
+       public function provideSingleFile() {
+               return array_map( function ( $file ) {
+                       return array( $file, file_get_contents( $file ) );
+               }, glob( __DIR__ . '/../../data/filecontentshasher/*.*' ) );
+       }
+
+       public function provideMultipleFiles() {
+               return array(
+                       array( $this->provideSingleFile() )
+               );
+       }
+
+       /**
+        * @covers FileContentsHasher::getFileContentHash
+        * @covers FileContentsHasher::getFileContentsHashInternal
+        * @dataProvider provideSingleFile
+        */
+       public function testSingleFileHash( $fileName, $contents ) {
+               foreach ( array( 'md4', 'md5' ) as $algo ) {
+                       $expectedHash = hash( $algo, $contents );
+                       $actualHash = FileContentsHasher::getFileContentsHash( $fileName, $algo );
+                       $this->assertEquals( $expectedHash, $actualHash );
+                       $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileName, $algo );
+                       $this->assertEquals( $expectedHash, $actualHashRepeat );
+               }
+       }
+
+       /**
+        * @covers FileContentsHasher::getFileContentHash
+        * @covers FileContentsHasher::getFileContentsHashInternal
+        * @dataProvider provideMultipleFiles
+        */
+       public function testMultipleFileHash( $files ) {
+               $fileNames = array();
+               $hashes = array();
+               foreach ( $files as $fileInfo ) {
+                       list( $fileName, $contents ) = $fileInfo;
+                       $fileNames[] = $fileName;
+                       $hashes[] = md5( $contents );
+               }
+
+               $expectedHash = md5( implode( '', $hashes ) );
+               $actualHash = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' );
+               $this->assertEquals( $expectedHash, $actualHash );
+               $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' );
+               $this->assertEquals( $expectedHash, $actualHashRepeat );
+       }
+}
diff --git a/tests/phpunit/languages/utils/CLDRPluralRuleEvaluatorTest.php b/tests/phpunit/languages/utils/CLDRPluralRuleEvaluatorTest.php
deleted file mode 100644 (file)
index 8e3b114..0000000
+++ /dev/null
@@ -1,151 +0,0 @@
-<?php
-/**
- * @author Niklas Laxström
- * @file
- */
-
-/**
- * @covers CLDRPluralRuleEvaluator
- */
-class CLDRPluralRuleEvaluatorTest extends MediaWikiTestCase {
-       /**
-        * @dataProvider validTestCases
-        */
-       function testValidRules( $expected, $rules, $number, $comment ) {
-               $result = CLDRPluralRuleEvaluator::evaluate( $number, (array)$rules );
-               $this->assertEquals( $expected, $result, $comment );
-       }
-
-       /**
-        * @dataProvider invalidTestCases
-        * @expectedException CLDRPluralRuleError
-        */
-       function testInvalidRules( $rules, $comment ) {
-               CLDRPluralRuleEvaluator::evaluate( 1, (array)$rules );
-       }
-
-       function validTestCases() {
-               $tests = array(
-                       # expected, rule, number, comment
-                       array( 0, 'n is 1', 1, 'integer number and is' ),
-                       array( 0, 'n is 1', "1", 'string integer number and is' ),
-                       array( 0, 'n is 1', 1.0, 'float number and is' ),
-                       array( 0, 'n is 1', "1.0", 'string float number and is' ),
-                       array( 1, 'n is 1', 1.1, 'float number and is' ),
-                       array( 1, 'n is 1', 2, 'float number and is' ),
-
-                       array( 0, 'n in 1,3,5', 3, '' ),
-                       array( 1, 'n not in 1,3,5', 5, '' ),
-
-                       array( 1, 'n in 1,3,5', 2, '' ),
-                       array( 0, 'n not in 1,3,5', 4, '' ),
-
-                       array( 0, 'n in 1..3', 2, '' ),
-                       array( 0, 'n in 1..3', 3, 'in is inclusive' ),
-                       array( 1, 'n in 1..3', 0, '' ),
-
-                       array( 1, 'n not in 1..3', 2, '' ),
-                       array( 1, 'n not in 1..3', 3, 'in is inclusive' ),
-                       array( 0, 'n not in 1..3', 0, '' ),
-
-                       array( 1, 'n is not 1 and n is not 2 and n is not 3', 1, 'and relation' ),
-                       array( 0, 'n is not 1 and n is not 2 and n is not 4', 3, 'and relation' ),
-
-                       array( 0, 'n is not 1 or n is 1', 1, 'or relation' ),
-                       array( 1, 'n is 1 or n is 2', 3, 'or relation' ),
-
-                       array( 0, 'n              is      1', 1, 'extra whitespace' ),
-
-                       array( 0, 'n mod 3 is 1', 7, 'mod' ),
-                       array( 0, 'n mod 3 is not 1', 4.3, 'mod with floats' ),
-
-                       array( 0, 'n within 1..3', 2, 'within with integer' ),
-                       array( 0, 'n within 1..3', 2.5, 'within with float' ),
-                       array( 0, 'n in 1..3', 2, 'in with integer' ),
-                       array( 1, 'n in 1..3', 2.5, 'in with float' ),
-
-                       array( 0, 'n in 3 or n is 4 and n is 5', 3, 'and binds more tightly than or' ),
-                       array( 1, 'n is 3 or n is 4 and n is 5', 4, 'and binds more tightly than or' ),
-
-                       array( 0, 'n mod 10 in 3..4,9 and n mod 100 not in 10..19,70..79,90..99', 24, 'breton rule' ),
-                       array( 1, 'n mod 10 in 3..4,9 and n mod 100 not in 10..19,70..79,90..99', 25, 'breton rule' ),
-
-                       array( 0, 'n within 0..2 and n is not 2', 0, 'french rule' ),
-                       array( 0, 'n within 0..2 and n is not 2', 1, 'french rule' ),
-                       array( 0, 'n within 0..2 and n is not 2', 1.2, 'french rule' ),
-                       array( 1, 'n within 0..2 and n is not 2', 2, 'french rule' ),
-
-                       array( 1, 'n in 3..10,13..19', 2, 'scottish rule - ranges with comma' ),
-                       array( 0, 'n in 3..10,13..19', 4, 'scottish rule - ranges with comma' ),
-                       array( 1, 'n in 3..10,13..19', 12.999, 'scottish rule - ranges with comma' ),
-                       array( 0, 'n in 3..10,13..19', 13, 'scottish rule - ranges with comma' ),
-
-                       array( 0, '5 mod 3 is n', 2, 'n as result of mod - no need to pass' ),
-
-                       # Revision 33 new operand examples
-                       # expected, rule, number, comment
-                       array( 0, 'i is 1', '1.00', 'new operand i' ),
-                       array( 0, 'v is 2', '1.00', 'new operand v' ),
-                       array( 0, 'w is 0', '1.00', 'new operand w' ),
-                       array( 0, 'f is 0', '1.00', 'new operand f' ),
-                       array( 0, 't is 0', '1.00', 'new operand t' ),
-
-                       array( 0, 'i is 1', '1.30', 'new operand i' ),
-                       array( 0, 'v is 2', '1.30', 'new operand v' ),
-                       array( 0, 'w is 1', '1.30', 'new operand w' ),
-                       array( 0, 'f is 30', '1.30', 'new operand f' ),
-                       array( 0, 't is 3', '1.30', 'new operand t' ),
-
-                       array( 0, 'i is 1', '1.03', 'new operand i' ),
-                       array( 0, 'v is 2', '1.03', 'new operand v' ),
-                       array( 0, 'w is 2', '1.03', 'new operand w' ),
-                       array( 0, 'f is 3', '1.03', 'new operand f' ),
-                       array( 0, 't is 3', '1.03', 'new operand t' ),
-
-                       # Revision 33 new operator aliases
-                       # expected, rule, number, comment
-                       array( 0, 'n % 3 is 1', 7, 'new % operator' ),
-                       array( 0, 'n = 1,3,5', 3, 'new = operator' ),
-                       array( 1, 'n != 1,3,5', 5, 'new != operator' ),
-
-                       # Revision 33 samples
-                       # expected, rule, number, comment
-                       // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
-                       array( 0, 'n in 1,3,5@integer 3~10, 103~110, 1003, … @decimal 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 103.0, 1003.0, …', 3, 'samples' ),
-                       // @codingStandardsIgnoreEnd
-
-                       # Revision 33 some test cases from CLDR
-                       array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.1', 'pt one' ),
-                       array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.01', 'pt one' ),
-                       array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.10', 'pt one' ),
-                       array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.010', 'pt one' ),
-                       array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.100', 'pt one' ),
-                       array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '0.0', 'pt other' ),
-                       array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '0.2', 'pt other' ),
-                       array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '10.0', 'pt other' ),
-                       array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '100.0', 'pt other' ),
-                       // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
-                       array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '2', 'bs few' ),
-                       array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '4', 'bs few' ),
-                       array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '22', 'bs few' ),
-                       array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '102', 'bs few' ),
-                       array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '0.2', 'bs few' ),
-                       array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '0.4', 'bs few' ),
-                       array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '10.2', 'bs few' ),
-                       array( 1, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '10.0', 'bs other' ),
-                       // @codingStandardsIgnoreEnd
-               );
-
-               return $tests;
-       }
-
-       function invalidTestCases() {
-               $tests = array(
-                       array( 'n mod mod 5 is 1', 'mod mod' ),
-                       array( 'n', 'just n' ),
-                       array( 'n is in 5', 'is in' ),
-               );
-
-               return $tests;
-       }
-}
index 587d6d0..af924a0 100755 (executable)
@@ -66,11 +66,21 @@ class PHPUnitMaintClass extends Maintenance {
                // wfWarn should cause tests to fail
                $wgDevelopmentWarnings = true;
 
+               // Make sure all caches and stashes are either disabled or use
+               // in-process cache only to prevent tests from using any preconfigured
+               // cache meant for the local wiki from outside the test run.
+               // See also MediaWikiTestCase::run() which mocks CACHE_DB and APC.
+
+               // Disabled in DefaultSettings, override local settings
+               $wgMainWANCache =
                $wgMainCacheType = CACHE_NONE;
-               $wgMainWANCache = CACHE_NONE;
-               $wgMessageCacheType = CACHE_NONE;
-               $wgParserCacheType = CACHE_NONE;
-               $wgLanguageConverterCacheType = CACHE_NONE;
+               // Uses CACHE_ANYTHING in DefaultSettings, use hash instead of db
+               $wgMessageCacheType =
+               $wgParserCacheType =
+               $wgSessionCacheType =
+               $wgLanguageConverterCacheType = 'hash';
+               // Uses db-replicated in DefaultSettings
+               $wgMainStash = 'hash';
 
                $wgUseDatabaseMessages = false; # Set for future resets
 
index 032551d..1726a48 100644 (file)
@@ -5,7 +5,7 @@
         */
 
        var text, ipv4,
-               simpleMDYDatesInMDY, simpleMDYDatesInDMY, oldMDYDates, complexMDYDates, clobberedDates, MYDates, YDates,
+               simpleMDYDatesInMDY, simpleMDYDatesInDMY, oldMDYDates, complexMDYDates, clobberedDates, MYDates, YDates, ISODates,
                currencyData, transformedCurrencyData;
 
        QUnit.module( 'jquery.tablesorter.parsers', QUnit.newMwEnvironment( {
        ];
        parserTest( 'Y Dates', 'date', YDates );
 
+       ISODates = [
+               [ '2000',               false, 946684800000, 'Plain 4-digit year' ],
+               [ '2000-01',            false, 946684800000, 'Year with month' ],
+               [ '2000-01-01', true, 946684800000, 'Year with month and day' ],
+               [ '2000-13-01', true, 0, 'Non existant month' ],
+               [ '2000-01-32', true, 0, 'Non existant day' ],
+               [ '2000-01-01T12:30:30',                true, 946729830000, 'Date with a time' ],
+               [ '2000-01-01T12:30:30Z',       true, 946729830000, 'Date with a UTC+0 time' ],
+               [ '2000-01-01T24:30:30Z',       true, 0, 'Date with invalid hours' ],
+               [ '2000-01-01T12:60:30Z',       true, 0, 'Date with invalid minutes' ],
+               [ '2000-01-01T12:30:61Z',       true, 0, 'Date with invalid amount of seconds' ],
+               [ '2000-01-01T23:59:59Z',       true, 946771199000, 'Edges of time' ],
+               [ '2000-01-01T12:30:30.111Z',   true, 946729830111, 'Date with milliseconds' ],
+               [ '2000-01-01T12:30:30.11111Z', true, 946729830111, 'Date with too high precision' ],
+               [ '2000-01-01T12:30:30,111Z',   true, 0, 'Date with milliseconds and , separator' ],
+               [ '2000-01-01T12:30:30+01:00',  true, 946726230000, 'Date time in UTC+1' ],
+               [ '2000-01-01T12:30:30+01:30',  true, 946724430000, 'Date time in UTC+1:30' ],
+               [ '2000-01-01T12:30:30-01:00',  true, 946733430000, 'Date time in UTC-1' ],
+               [ '2000-01-01T12:30:30-01:30',  true, 946735230000, 'Date time in UTC-1:30' ],
+               [ '2000-01-01T12:30:30.111+01:00', true, 946726230111, 'Date time and milliseconds in UTC+1 ' ]
+               /* Disable testcases, because behavior is browser dependant */
+               /*
+               [ '2000-11-31', true, 0, '31 days in 30 day month' ],
+               [ '50-01-01',   false, -60589296000000, 'Year with just two digits' ],
+               [ '-1000-01-01',        true, -93724128000000, 'Year BC' ],
+               [ '+1000-01-01',        true, -30610224000000, 'Date with +sign' ],
+               [ '2000-01-01 12:30:30Z',       true, 0, 'Date and time with no T marker' ],
+               [ '2000-01-01T12:30:60Z',       true, 946729860000, 'Date with leap second' ],
+               [ '2000-01-01T12:30:30-24:00',  true, 946816230000, 'Date time in UTC-24' ],
+               [ '2000-01-01T12:30:30+24:00',  true, 946643430000, 'Date time in UTC+24' ],
+               [ '2000-01-01T12:30:30+0100',   true, 946726230000, 'Time without separator in timezone offset' ]
+               */
+       ];
+       parserTest( 'ISO Dates', 'isoDate', ISODates );
+
        currencyData = [
                [ '1.02 $',     true, 1.02, '' ],
                [ '$ 3.00',     true, 3, '' ],
index d4d0ce1..8026cc3 100644 (file)
                        [ 'January 01 2010' ],
                        [ 'January 16 2010' ],
                        [ 'February 05 2010' ]
+               ],
+               isoDateSorting = [
+                       [ '2010-02-01' ],
+                       [ '2009-12-25T12:30:45.001Z' ],
+                       [ '2010-01-31' ],
+                       [ '2009' ],
+                       [ '2009-12-25T12:30:45' ],
+                       [ '2009-12-25T12:30:45.111' ],
+                       [ '2009-12-25T12:30:45+01:00' ]
+               ],
+               isoDateSortingSorted = [
+                       [ '2009' ],
+                       [ '2009-12-25T12:30:45' ],
+                       [ '2009-12-25T12:30:45+01:00' ],
+                       [ '2009-12-25T12:30:45.001Z' ],
+                       [ '2009-12-25T12:30:45.111' ],
+                       [ '2010-01-31' ],
+                       [ '2010-02-01' ]
                ];
 
        QUnit.module( 'jquery.tablesorter', QUnit.newMwEnvironment( {
                }
        );
 
+       tableTest(
+               'ISO date sorting',
+               [ 'isoDate' ],
+               isoDateSorting,
+               isoDateSortingSorted,
+               function ( $table ) {
+                       mw.config.set( 'wgDefaultDateFormat', 'dmy' );
+
+                       $table.tablesorter();
+                       $table.find( '.headerSort:eq(0)' ).click();
+               }
+       );
+
        QUnit.test( 'Sorting images using alt text', 1, function ( assert ) {
                var $table = $(
                        '<table class="sortable">' +