Cleanup
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
index abdcec0..beeeaf1 100644 (file)
 <?php
 
-# This file is used to configure the live Wikimedia wikis. The file that includes
-# it contains passwords and other sensitive data, and there's currently no public
-# equivalent. 
+/**
+ * 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, $wikis, $settings;
-       var $localDatabases;
-       
-       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'];
+       var $suffixes = array();
+       var $wikis = array();
+       var $settings = array();
+       var $localVHosts = array();
+
+       /** */
+       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;
        }
+       
+       /** 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 ) {
-               return (bool)($this->get( $setting, $wiki, $suffix ));
+       /** */
+       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 );
                }
        }
 
-       # Work out the site and language name from a database name
+       /**
+        * Work out the site and language name from a database name
+        * @param $db
+        */
        function siteFromDB( $db ) {
                $site = NULL;
                $lang = NULL;
@@ -71,9 +132,15 @@ class SiteConfiguration {
                                break;
                        }
                }
+               $lang = str_replace( '_', '-', $lang );
                return array( $site, $lang );
        }
+
+       /** */
+       function isLocalVHost( $vhost ) {
+               return in_array( $vhost, $this->localVHosts );
+       }
+}
 }
 
-       
-?>
+