Update docs for return and exception info
[lhc/web/wiklou.git] / includes / db / LBFactory_Multi.php
index b5fc1f6..9b468a7 100644 (file)
@@ -1,5 +1,22 @@
 <?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
  */
@@ -36,6 +53,8 @@
  *
  *     masterTemplateOverrides     An override array for all master servers.
  *
+ *     readOnlyBySection           A map of section name to read-only message. Missing or false for read/write.
+ *
  * @ingroup Database
  */
 class LBFactory_Multi extends LBFactory {
@@ -44,18 +63,23 @@ class LBFactory_Multi extends LBFactory {
        // Optional settings
        var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
        var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
-       var $templateOverridesByCluster, $masterTemplateOverrides;
+       var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
        // Other stuff
        var $conf, $mainLBs = array(), $extLBs = array();
-       var $localSection = null;
+       var $lastWiki, $lastSection;
 
+       /**
+        * @param $conf array
+        * @throws MWException
+        */
        function __construct( $conf ) {
                $this->chronProt = new ChronologyProtector;
                $this->conf = $conf;
                $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
                $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
                        'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
-                       'templateOverridesByCluster', 'masterTemplateOverrides' );
+                       'templateOverridesByCluster', 'masterTemplateOverrides',
+                       'readOnlyBySection' );
 
                foreach ( $required as $key ) {
                        if ( !isset( $conf[$key] ) ) {
@@ -69,75 +93,124 @@ class LBFactory_Multi extends LBFactory {
                                $this->$key = $conf[$key];
                        }
                }
+
+               // Check for read-only mode
+               $section = $this->getSectionForWiki();
+               if ( !empty( $this->readOnlyBySection[$section] ) ) {
+                       global $wgReadOnly;
+                       $wgReadOnly = $this->readOnlyBySection[$section];
+               }
        }
 
-       function getSectionForWiki( $wiki ) {
-               list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
+       /**
+        * @param $wiki bool|string
+        * @return string
+        */
+       function getSectionForWiki( $wiki = false ) {
+               if ( $this->lastWiki === $wiki ) {
+                       return $this->lastSection;
+               }
+               list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
                if ( isset( $this->sectionsByDB[$dbName] ) ) {
-                       return $this->sectionsByDB[$dbName];
+                       $section = $this->sectionsByDB[$dbName];
                } else {
-                       return 'DEFAULT';
+                       $section = 'DEFAULT';
                }
+               $this->lastSection = $section;
+               $this->lastWiki = $wiki;
+               return $section;
        }
 
-       function getMainLB( $wiki = false ) {
-               // Determine section
-               if ( $wiki === false ) {
-                       if ( $this->localSection === null ) {
-                               $this->localSection = $this->getSectionForWiki( $wiki );
-                       }
-                       $section = $this->localSection;
-               } else {
-                       $section = $this->getSectionForWiki( $wiki );
+       /**
+        * @param $wiki bool|string
+        * @return LoadBalancer
+        */
+       function newMainLB( $wiki = false ) {
+               list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
+               $section = $this->getSectionForWiki( $wiki );
+               $groupLoads = array();
+               if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
+                       $groupLoads = $this->groupLoadsByDB[$dbName];
+               }
+               if ( isset( $this->groupLoadsBySection[$section] ) ) {
+                       $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
                }
+               return $this->newLoadBalancer( $this->serverTemplate, $this->sectionLoads[$section], $groupLoads );
+       }
 
+       /**
+        * @param $wiki bool|string
+        * @return LoadBalancer
+        */
+       function getMainLB( $wiki = false ) {
+               $section = $this->getSectionForWiki( $wiki );
                if ( !isset( $this->mainLBs[$section] ) ) {
-                       list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $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] );
-                       }
-                       $this->mainLBs[$section] = $this->newLoadBalancer( $this->serverTemplate,
-                               $this->sectionLoads[$section], $groupLoads, "main-$section" );
-                       $this->chronProt->initLB( $this->mainLBs[$section] );
+                       $lb = $this->newMainLB( $wiki, $section );
+                       $this->chronProt->initLB( $lb );
+                       $lb->parentInfo( array( 'id' => "main-$section" ) );
+                       $this->mainLBs[$section] = $lb;
                }
                return $this->mainLBs[$section];
        }
 
+       /**
+        * @param String $cluster
+        * @param bool $wiki
+        * @throws MWException
+        * @return LoadBalancer
+        */
+       function newExternalLB( $cluster, $wiki = false ) {
+               if ( !isset( $this->externalLoads[$cluster] ) ) {
+                       throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
+               }
+               $template = $this->serverTemplate;
+               if ( isset( $this->externalTemplateOverrides ) ) {
+                       $template = $this->externalTemplateOverrides + $template;
+               }
+               if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
+                       $template = $this->templateOverridesByCluster[$cluster] + $template;
+               }
+               return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
+       }
+
+       /**
+        * @param $cluster
+        * @param $wiki
+        * @return LoadBalancer
+        */
        function &getExternalLB( $cluster, $wiki = false ) {
                if ( !isset( $this->extLBs[$cluster] ) ) {
-                       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;
-                       }
-                       $this->extLBs[$cluster] = $this->newLoadBalancer( $template,
-                               $this->externalLoads[$cluster], array(), "ext-$cluster" );
+                       $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
+                       $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
                }
                return $this->extLBs[$cluster];
        }
 
        /**
         * Make a new load balancer object based on template and load array
+        *
+        * @param $template
+        * @param $loads array
+        * @param $groupLoads
+        * @return LoadBalancer
         */
-       function newLoadBalancer( $template, $loads, $groupLoads, $id ) {
+       function newLoadBalancer( $template, $loads, $groupLoads ) {
                global $wgMasterWaitTimeout;
                $servers = $this->makeServerArray( $template, $loads, $groupLoads );
-               $lb = new LoadBalancer( $servers, false, $wgMasterWaitTimeout );
-               $lb->parentInfo( array( 'id' => $id ) );
+               $lb = new LoadBalancer( array(
+                       'servers' => $servers,
+                       'masterWaitTimeout' => $wgMasterWaitTimeout
+               ));
                return $lb;
        }
 
        /**
         * Make a server array as expected by LoadBalancer::__construct, using a template and load array
+        *
+        * @param $template
+        * @param $loads array
+        * @param $groupLoads
+        * @return array
         */
        function makeServerArray( $template, $loads, $groupLoads ) {
                $servers = array();
@@ -177,6 +250,8 @@ class LBFactory_Multi extends LBFactory {
 
        /**
         * Take a group load array indexed by group then server, and reindex it by server then group
+        * @param $groupLoads
+        * @return array
         */
        function reindexGroupLoads( $groupLoads ) {
                $reindexed = array();
@@ -190,6 +265,8 @@ class LBFactory_Multi extends LBFactory {
 
        /**
         * Get the database name and prefix based on the wiki ID
+        * @param $wiki bool
+        * @return array
         */
        function getDBNameAndPrefix( $wiki = false ) {
                if ( $wiki === false ) {
@@ -204,6 +281,8 @@ class LBFactory_Multi extends LBFactory {
         * Execute a function for each tracked load balancer
         * The callback is called with the load balancer as the first parameter,
         * and $params passed as the subsequent parameters.
+        * @param $callback
+        * @param $params array
         */
        function forEachLB( $callback, $params = array() ) {
                foreach ( $this->mainLBs as $lb ) {