Revert r39623 "Use array_merge_recursive instead of array_merge for merging settings."
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
1 <?php
2
3 /**
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.
7 */
8
9 // Hide this pattern from Doxygen, which spazzes out at it
10 /// @cond
11 if (!defined('SITE_CONFIGURATION')) {
12 define('SITE_CONFIGURATION', 1);
13 /// @endcond
14
15 /**
16 * This is a class used to hold configuration settings, particularly for multi-wiki sites.
17 *
18 */
19 class SiteConfiguration {
20 var $suffixes = array();
21 var $wikis = array();
22 var $settings = array();
23 var $localVHosts = array();
24
25 /**
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.
33 */
34 function get( $settingName, $wiki, $suffix, $params = array(), $wikiTags = array() ) {
35 if ( array_key_exists( $settingName, $this->settings ) ) {
36 $thisSetting =& $this->settings[$settingName];
37 do {
38 // Do individual wiki settings
39 if ( array_key_exists( $wiki, $thisSetting ) ) {
40 $retval = $thisSetting[$wiki];
41 break;
42 } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array($thisSetting["+$wiki"]) ) {
43 $retval = $thisSetting["+$wiki"];
44 }
45
46 // Do tag settings
47 foreach ( $wikiTags as $tag ) {
48 if ( array_key_exists( $tag, $thisSetting ) ) {
49 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$tag]) ) {
50 $retval = array_merge( $retval, $thisSetting[$tag] );
51 } else {
52 $retval = $thisSetting[$tag];
53 }
54 break 2;
55 } elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array($thisSetting["+$tag"]) ) {
56 if (!isset($retval))
57 $retval = array();
58 $retval = array_merge( $retval, $thisSetting["+$tag"] );
59 }
60 }
61
62 // Do suffix settings
63 if ( array_key_exists( $suffix, $thisSetting ) ) {
64 if ( isset($retval) && is_array($retval) && is_array($thisSetting[$suffix]) ) {
65 $retval = array_merge( $retval, $thisSetting[$suffix] );
66 } else {
67 $retval = $thisSetting[$suffix];
68 }
69 break;
70 } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array($thisSetting["+$suffix"]) ) {
71 if (!isset($retval))
72 $retval = array();
73 $retval = array_merge( $retval, $thisSetting["+$suffix"] );
74 }
75
76 // Fall back to default.
77 if ( array_key_exists( 'default', $thisSetting ) ) {
78 if ( isset($retval) && is_array($retval) && is_array($thisSetting['default']) ) {
79 $retval = array_merge( $retval, $thisSetting['default'] );
80 } else {
81 $retval = $thisSetting['default'];
82 }
83 break;
84 }
85 $retval = null;
86 } while ( false );
87 } else {
88 $retval = NULL;
89 }
90
91 if ( !is_null( $retval ) && count( $params ) ) {
92 foreach ( $params as $key => $value ) {
93 $retval = $this->doReplace( '$' . $key, $value, $retval );
94 }
95 }
96 return $retval;
97 }
98
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 );
106 }
107 return $in;
108 } else {
109 return $in;
110 }
111 }
112
113 /**
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.
120 */
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;
127 }
128 }
129 return $localSettings;
130 }
131
132 /**
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.
140 */
141 function getBool( $setting, $wiki, $suffix, $wikiTags = array() ) {
142 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
143 }
144
145 /** Retrieves an array of local databases */
146 function &getLocalDatabases() {
147 return $this->wikis;
148 }
149
150 /** A no-op */
151 function initialise() {
152 }
153
154 /**
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.
162 */
163 function extractVar( $setting, $wiki, $suffix, &$var, $params, $wikiTags = array() ) {
164 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
165 if ( !is_null( $value ) ) {
166 $var = $value;
167 }
168 }
169
170 /**
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.
177 */
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 );
185 } else {
186 $GLOBALS[$setting] = $value;
187 }
188 } else {
189 $GLOBALS[$setting] = $value;
190 }
191 }
192 }
193
194 /**
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.
200 */
201 function extractAllGlobals( $wiki, $suffix, $params, $wikiTags = array() ) {
202 foreach ( $this->settings as $varName => $setting ) {
203 $this->extractGlobal( $varName, $wiki, $suffix, $params, $wikiTags );
204 }
205 }
206
207 /**
208 * Work out the site and language name from a database name
209 * @param $db
210 */
211 function siteFromDB( $db ) {
212 $site = NULL;
213 $lang = NULL;
214 foreach ( $this->suffixes as $suffix ) {
215 if ( $suffix === '' ) {
216 $site = '';
217 $lang = $db;
218 break;
219 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
220 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
221 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
222 break;
223 }
224 }
225 $lang = str_replace( '_', '-', $lang );
226 return array( $site, $lang );
227 }
228
229 /** Returns true if the given vhost is handled locally. */
230 function isLocalVHost( $vhost ) {
231 return in_array( $vhost, $this->localVHosts );
232 }
233 }
234 }