4 * The include paths change after this file is included from commandLine.inc,
5 * meaning that require_once() fails to detect that it is including the same
6 * file again. We use DIY C-style protection as a workaround.
9 // Hide this pattern from Doxygen, which spazzes out at it
11 if (!defined('SITE_CONFIGURATION')) {
12 define('SITE_CONFIGURATION', 1);
16 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
19 class SiteConfiguration
{
20 var $suffixes = array();
22 var $settings = array();
23 var $localVHosts = array();
26 * Retrieves a configuration setting for a given wiki.
27 * @param $settingName String ID of the setting name to retrieve
28 * @param $wiki String Wiki ID of the wiki in question.
29 * @param $suffix String The suffix of the wiki in question.
30 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
31 * @param $wikiTags Array The tags assigned to the wiki.
32 * @return Mixed the value of the setting requested.
34 function get( $settingName, $wiki, $suffix, $params = array(), $wikiTags = array() ) {
35 if ( array_key_exists( $settingName, $this->settings
) ) {
36 $thisSetting =& $this->settings
[$settingName];
38 // Do individual wiki settings
39 if ( array_key_exists( $wiki, $thisSetting ) ) {
40 $retval = $thisSetting[$wiki];
42 } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array($thisSetting["+$wiki"]) ) {
43 $retval = $thisSetting["+$wiki"];
47 foreach ( $wikiTags as $tag ) {
48 if ( array_key_exists( $tag, $thisSetting ) ) {
49 if ( is_array($retval) && is_array($thisSetting[$tag]) ) {
50 $retval = array_merge( $retval, $thisSetting[$tag] );
52 $retval = $thisSetting[$tag];
55 } elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
58 $retval = array_merge( $retval, $thisSetting["+$tag"] );
63 if ( array_key_exists( $suffix, $thisSetting ) ) {
64 if ( is_array($retval) && is_array($thisSetting[$suffix]) ) {
65 $retval = array_merge( $retval, $thisSetting[$suffix] );
67 $retval = $thisSetting[$suffix];
70 } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
73 $retval = array_merge( $retval, $thisSetting["+$suffix"] );
76 // Fall back to default.
77 if ( array_key_exists( 'default', $thisSetting ) ) {
78 if ( is_array($retval) && is_array($thisSetting['default']) ) {
79 $retval = array_merge( $retval, $thisSetting['default'] );
81 $retval = $thisSetting['default'];
91 if ( !is_null( $retval ) && count( $params ) ) {
92 foreach ( $params as $key => $value ) {
93 $retval = $this->doReplace( '$' . $key, $value, $retval );
99 /** Type-safe string replace; won't do replacements on non-strings */
100 function doReplace( $from, $to, $in ) {
101 if( is_string( $in ) ) {
102 return str_replace( $from, $to, $in );
103 } elseif( is_array( $in ) ) {
104 foreach( $in as $key => $val ) {
105 $in[$key] = $this->doReplace( $from, $to, $val );
114 * Gets all settings for a wiki
115 * @param $wiki String Wiki ID of the wiki in question.
116 * @param $suffix String The suffix of the wiki in question.
117 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
118 * @param $wikiTags Array The tags assigned to the wiki.
119 * @return Array Array of settings requested.
121 function getAll( $wiki, $suffix, $params, $wikiTags = array() ) {
122 $localSettings = array();
123 foreach ( $this->settings
as $varname => $stuff ) {
124 $value = $this->get( $varname, $wiki, $suffix, $params, $wikiTags );
125 if ( !is_null( $value ) ) {
126 $localSettings[$varname] = $value;
129 return $localSettings;
133 * Retrieves a configuration setting for a given wiki, forced to a boolean.
134 * @param $settingName String ID of the setting name to retrieve
135 * @param $wiki String Wiki ID of the wiki in question.
136 * @param $suffix String The suffix of the wiki in question.
137 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
138 * @param $wikiTags Array The tags assigned to the wiki.
139 * @return bool The value of the setting requested.
141 function getBool( $setting, $wiki, $suffix, $wikiTags = array() ) {
142 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
145 /** Retrieves an array of local databases */
146 function &getLocalDatabases() {
151 function initialise() {
155 * Retrieves the value of a given setting, and places it in a variable passed by reference.
156 * @param $settingName String ID of the setting name to retrieve
157 * @param $wiki String Wiki ID of the wiki in question.
158 * @param $suffix String The suffix of the wiki in question.
159 * @param $var Reference The variable to insert the value into.
160 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
161 * @param $wikiTags Array The tags assigned to the wiki.
163 function extractVar( $setting, $wiki, $suffix, &$var, $params, $wikiTags = array() ) {
164 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
165 if ( !is_null( $value ) ) {
171 * Retrieves the value of a given setting, and places it in its corresponding global variable.
172 * @param $settingName String ID of the setting name to retrieve
173 * @param $wiki String Wiki ID of the wiki in question.
174 * @param $suffix String The suffix of the wiki in question.
175 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
176 * @param $wikiTags Array The tags assigned to the wiki.
178 function extractGlobal( $setting, $wiki, $suffix, $params, $wikiTags = array() ) {
179 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
180 if ( !is_null( $value ) ) {
181 if (substr($setting,0,1) == '+' && is_array($value)) {
182 $setting = substr($setting,1);
183 if ( is_array($GLOBALS[$setting]) ) {
184 $GLOBALS[$setting] = array_merge( $GLOBALS[$setting], $value );
186 $GLOBALS[$setting] = $value;
189 $GLOBALS[$setting] = $value;
195 * Retrieves the values of all settings, and places them in their corresponding global variables.
196 * @param $wiki String Wiki ID of the wiki in question.
197 * @param $suffix String The suffix of the wiki in question.
198 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
199 * @param $wikiTags Array The tags assigned to the wiki.
201 function extractAllGlobals( $wiki, $suffix, $params, $wikiTags = array() ) {
202 foreach ( $this->settings
as $varName => $setting ) {
203 $this->extractGlobal( $varName, $wiki, $suffix, $params, $wikiTags );
208 * Work out the site and language name from a database name
211 function siteFromDB( $db ) {
214 foreach ( $this->suffixes
as $suffix ) {
215 if ( $suffix === '' ) {
219 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
220 $site = $suffix == 'wiki' ?
'wikipedia' : $suffix;
221 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
225 $lang = str_replace( '_', '-', $lang );
226 return array( $site, $lang );
229 /** Returns true if the given vhost is handled locally. */
230 function isLocalVHost( $vhost ) {
231 return in_array( $vhost, $this->localVHosts
);