Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
index 25c289f..8a977fb 100644 (file)
@@ -1,17 +1,4 @@
 <?php
-
-/**
- * The include paths change after this file is included from commandLine.inc,
- * meaning that require_once() fails to detect that it is including the same
- * file again. We use DIY C-style protection as a workaround.
- */
-
-// Hide this pattern from Doxygen, which spazzes out at it
-/// @cond
-if( !defined( 'SITE_CONFIGURATION' ) ){
-define( 'SITE_CONFIGURATION', 1 );
-/// @endcond
-
 /**
  * This is a class used to hold configuration settings, particularly for multi-wiki sites.
  */
@@ -26,7 +13,7 @@ class SiteConfiguration {
         * Array of wikis, should be the same as $wgLocalDatabases
         */
        public $wikis = array();
-       
+
        /**
         * The whole array of settings
         */
@@ -37,6 +24,14 @@ class SiteConfiguration {
         */
        public $localVHosts = array();
 
+       /**
+        * Optional callback to load full configuration data.
+        */
+       public $fullLoadCallback = null;
+
+       /** Whether or not all data has been loaded */
+       public $fullLoadDone = false;
+
        /**
         * A callback function that returns an array with the following keys (all
         * optional):
@@ -64,7 +59,7 @@ class SiteConfiguration {
                $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
                return $this->getSetting( $settingName, $wiki, $params );
        }
-       
+
        /**
         * Really retrieves a configuration setting for a given wiki.
         *
@@ -85,7 +80,7 @@ class SiteConfiguration {
                                } elseif( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
                                        $retval = $thisSetting["+$wiki"];
                                }
-                               
+
                                // Do tag settings
                                foreach( $params['tags'] as $tag ) {
                                        if( array_key_exists( $tag, $thisSetting ) ) {
@@ -117,7 +112,7 @@ class SiteConfiguration {
                                                $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
                                        }
                                }
-                               
+
                                // Fall back to default.
                                if( array_key_exists( 'default', $thisSetting ) ) {
                                        if( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
@@ -141,6 +136,11 @@ class SiteConfiguration {
        /**
         * Type-safe string replace; won't do replacements on non-strings
         * private?
+        *
+        * @param $from
+        * @param $to
+        * @param $in
+        * @return string
         */
        function doReplace( $from, $to, $in ) {
                if( is_string( $in ) ) {
@@ -186,10 +186,9 @@ class SiteConfiguration {
 
        /**
         * Retrieves a configuration setting for a given wiki, forced to a boolean.
-        * @param $settingName String ID of the setting name to retrieve
+        * @param $setting String ID of the setting name to retrieve
         * @param $wiki String Wiki ID of the wiki in question.
         * @param $suffix String The suffix of the wiki in question.
-        * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
         * @param $wikiTags Array The tags assigned to the wiki.
         * @return bool The value of the setting requested.
         */
@@ -197,18 +196,18 @@ class SiteConfiguration {
                return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
        }
 
-       /** Retrieves an array of local databases */
+       /**
+        * Retrieves an array of local databases
+        *
+        * @return array
+        */
        function &getLocalDatabases() {
                return $this->wikis;
        }
 
-       /** A no-op */
-       function initialise() {
-       }
-
        /**
         * Retrieves the value of a given setting, and places it in a variable passed by reference.
-        * @param $settingName String ID of the setting name to retrieve
+        * @param $setting String ID of the setting name to retrieve
         * @param $wiki String Wiki ID of the wiki in question.
         * @param $suffix String The suffix of the wiki in question.
         * @param $var Reference The variable to insert the value into.
@@ -224,7 +223,7 @@ class SiteConfiguration {
 
        /**
         * Retrieves the value of a given setting, and places it in its corresponding global variable.
-        * @param $settingName String ID of the setting name to retrieve
+        * @param $setting String ID of the setting name to retrieve
         * @param $wiki String Wiki ID of the wiki in question.
         * @param $suffix String The suffix of the wiki in question.
         * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
@@ -235,6 +234,11 @@ class SiteConfiguration {
                $this->extractGlobalSetting( $setting, $wiki, $params );
        }
 
+       /**
+        * @param $setting string
+        * @param $wiki string
+        * @param $params array
+        */
        public function extractGlobalSetting( $setting, $wiki, $params ) {
                $value = $this->getSetting( $setting, $wiki, $params );
                if ( !is_null( $value ) ) {
@@ -281,24 +285,26 @@ class SiteConfiguration {
                        'params' => array(),
                );
 
-               if( !is_callable( $this->siteParamsCallback ) )
+               if( !is_callable( $this->siteParamsCallback ) ) {
                        return $default;
+               }
 
                $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
                # Validate the returned value
-               if( !is_array( $ret ) )
+               if( !is_array( $ret ) ) {
                        return $default;
+               }
 
                foreach( $default as $name => $def ){
                        if( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) )
                                $ret[$name] = $default[$name];
                }
-               
+
                return $ret;
        }
 
        /**
-        * Merge params beetween the ones passed to the function and the ones given
+        * Merge params between the ones passed to the function and the ones given
         * by self::$siteParamsCallback for backward compatibility
         * Values returned by self::getWikiParams() have the priority.
         *
@@ -317,23 +323,26 @@ class SiteConfiguration {
 
                $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
 
+               $ret['params'] += $params;
+
                // Automatically fill that ones if needed
                if( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) )
                        $ret['params']['lang'] = $ret['lang'];
                if( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) )
                        $ret['params']['site'] = $ret['suffix'];
-       
-               $ret['params'] += $params;
+
                return $ret;
        }
 
        /**
         * Work out the site and language name from a database name
         * @param $db
+        *
+        * @return array
         */
        public function siteFromDB( $db ) {
                // Allow override
-               $def = $this->getWikiParams( $db );             
+               $def = $this->getWikiParams( $db );
                if( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) )
                        return array( $def['suffix'], $def['lang'] );
 
@@ -362,16 +371,20 @@ class SiteConfiguration {
        public function isLocalVHost( $vhost ) {
                return in_array( $vhost, $this->localVHosts );
        }
-       
+
        /**
         * Merge multiple arrays together.
         * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
         * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
         * which is not fun
+        *
+        * @param $array1 array
+        *
+        * @return array
         */
        static function arrayMerge( $array1/* ... */ ) {
                $out = $array1;
-               for( $i=1; $i < func_num_args(); $i++ ) {
+               for( $i = 1; $i < func_num_args(); $i++ ) {
                        foreach( func_get_arg( $i ) as $key => $value ) {
                                if ( isset($out[$key]) && is_array($out[$key]) && is_array($value) ) {
                                        $out[$key] = self::arrayMerge( $out[$key], $value );
@@ -383,8 +396,14 @@ class SiteConfiguration {
                                }
                        }
                }
-       
+
                return $out;
        }
-}
+
+       public function loadFullData() {
+               if ($this->fullLoadCallback && !$this->fullLoadDone) {
+                       call_user_func( $this->fullLoadCallback, $this );
+                       $this->fullLoadDone = true;
+               }
+       }
 }