Add bug and comment for r35609: * (bug 13434) Show a warning when hash identical...
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
index c7c78a6..6cdd508 100644 (file)
 <?php
-/**
- * This is a class used to hold configuration settings, particularly for multi-wiki sites. 
- *
- * @package MediaWiki
- */
-
-/**
- *
- * @package MediaWiki
- */
 
 /**
- * The include paths change after this file is included from commandLine.inc, 
+ * 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.
  */
 if (!defined('SITE_CONFIGURATION')) {
 define('SITE_CONFIGURATION', 1);
 
+/**
+ * This is a class used to hold configuration settings, particularly for multi-wiki sites.
+ *
+ */
 class SiteConfiguration {
        var $suffixes = array();
        var $wikis = array();
        var $settings = array();
-       var $localDatabases = array();
        var $localVHosts = array();
-       
-       function get( $setting, $wiki, $suffix, $params = array() ) {
-               if ( array_key_exists( $wiki, $this->settings[$setting] ) ) {
-                       $retval = $this->settings[$setting][$wiki];
-               } elseif ( array_key_exists( $suffix, $this->settings[$setting] ) ) {
-                       $retval = $this->settings[$setting][$suffix];
-               } elseif ( array_key_exists( 'default', $this->settings[$setting] ) ) {
-                       $retval = $this->settings[$setting]['default'];
+
+       /** */
+       function get( $settingName, $wiki, $suffix, $params = array(), $wikiTags = array() ) {
+               if ( array_key_exists( $settingName, $this->settings ) ) {
+                       $thisSetting =& $this->settings[$settingName];
+                       do {
+                               if ( array_key_exists( $wiki, $thisSetting ) ) {
+                                       $retval = $thisSetting[$wiki];
+                                       break;
+                               }
+                               foreach ( $wikiTags as $tag ) {
+                                       if ( array_key_exists( $tag, $thisSetting ) ) {
+                                               $retval = $thisSetting[$tag];
+                                               break 2;
+                                       }
+                               }
+                               if ( array_key_exists( $suffix, $thisSetting ) ) {
+                                       $retval = $thisSetting[$suffix];
+                                       break;
+                               }
+                               if ( array_key_exists( 'default', $thisSetting ) ) {
+                                       $retval = $thisSetting['default'];
+                                       break;
+                               }
+                               $retval = null;
+                       } while ( false );
                } else {
                        $retval = NULL;
                }
+
                if ( !is_null( $retval ) && count( $params ) ) {
                        foreach ( $params as $key => $value ) {
-                               $retval = str_replace( '$' . $key, $value, $retval );
+                               $retval = $this->doReplace( '$' . $key, $value, $retval );
                        }
                }
                return $retval;
        }
 
-       function getBool( $setting, $wiki, $suffix ) {
-               return (bool)($this->get( $setting, $wiki, $suffix ));
+       /** Type-safe string replace; won't do replacements on non-strings */
+       function doReplace( $from, $to, $in ) {
+               if( is_string( $in ) ) {
+                       return str_replace( $from, $to, $in );
+               } elseif( is_array( $in ) ) {
+                       foreach( $in as $key => $val ) {
+                               $in[$key] = $this->doReplace( $from, $to, $val );
+                       }
+                       return $in;
+               } else {
+                       return $in;
+               }
+       }
+
+       /** */
+       function getAll( $wiki, $suffix, $params, $wikiTags = array() ) {
+               $localSettings = array();
+               foreach ( $this->settings as $varname => $stuff ) {
+                       $value = $this->get( $varname, $wiki, $suffix, $params, $wikiTags );
+                       if ( !is_null( $value ) ) {
+                               $localSettings[$varname] = $value;
+                       }
+               }
+               return $localSettings;
        }
 
+       /** */
+       function getBool( $setting, $wiki, $suffix, $wikiTags = array() ) {
+               return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
+       }
+
+       /** */
        function &getLocalDatabases() {
-               return $this->localDatabases;
+               return $this->wikis;
        }
-       
+
+       /** */
        function initialise() {
-               foreach ( $this->wikis as $db ) {
-                       $this->localDatabases[$db] = $db;
-               }
        }
 
-       function extractVar( $setting, $wiki, $suffix, &$var, $params ) {
-               $value = $this->get( $setting, $wiki, $suffix, $params );
+       /** */
+       function extractVar( $setting, $wiki, $suffix, &$var, $params, $wikiTags = array() ) {
+               $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
                if ( !is_null( $value ) ) {
                        $var = $value;
                }
        }
-       
-       function extractGlobal( $setting, $wiki, $suffix, $params ) {
-               $value = $this->get( $setting, $wiki, $suffix, $params );
+
+       /** */
+       function extractGlobal( $setting, $wiki, $suffix, $params, $wikiTags = array() ) {
+               $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
                if ( !is_null( $value ) ) {
                        $GLOBALS[$setting] = $value;
                }
        }
 
-       function extractAllGlobals( $wiki, $suffix, $params ) {
+       /** */
+       function extractAllGlobals( $wiki, $suffix, $params, $wikiTags = array() ) {
                foreach ( $this->settings as $varName => $setting ) {
-                       $this->extractGlobal( $varName, $wiki, $suffix, $params );
+                       $this->extractGlobal( $varName, $wiki, $suffix, $params, $wikiTags );
                }
        }
 
@@ -85,19 +126,23 @@ class SiteConfiguration {
                $site = NULL;
                $lang = NULL;
                foreach ( $this->suffixes as $suffix ) {
-                       if ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
+                       if ( $suffix === '' ) {
+                               $site = '';
+                               $lang = $db;
+                               break;
+                       } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
                                $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
                                $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
                                break;
                        }
                }
+               $lang = str_replace( '_', '-', $lang );
                return array( $site, $lang );
        }
 
+       /** */
        function isLocalVHost( $vhost ) {
                return in_array( $vhost, $this->localVHosts );
        }
 }
 }
-       
-?>