Only run hooks if Hooks.php has been loaded (i.e. we CAN run them)
[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 $append = false;
125 $var = $varname;
126 if ( substr( $varname, 0, 1 ) == '+' ) {
127 $append = true;
128 $var = substr( $varname, 1 );
129 }
130
131 $value = $this->get( $varname, $wiki, $suffix, $params, $wikiTags );
132 if ( $append && is_array($value) && is_array( $GLOBALS[$var] ) )
133 $value = array_merge( $value, $GLOBALS[$var] );
134 if ( !is_null( $value ) ) {
135 $localSettings[$var] = $value;
136 }
137 }
138 return $localSettings;
139 }
140
141 /**
142 * Retrieves a configuration setting for a given wiki, forced to a boolean.
143 * @param $settingName String ID of the setting name to retrieve
144 * @param $wiki String Wiki ID of the wiki in question.
145 * @param $suffix String The suffix of the wiki in question.
146 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
147 * @param $wikiTags Array The tags assigned to the wiki.
148 * @return bool The value of the setting requested.
149 */
150 function getBool( $setting, $wiki, $suffix, $wikiTags = array() ) {
151 return (bool)($this->get( $setting, $wiki, $suffix, array(), $wikiTags ) );
152 }
153
154 /** Retrieves an array of local databases */
155 function &getLocalDatabases() {
156 return $this->wikis;
157 }
158
159 /** A no-op */
160 function initialise() {
161 }
162
163 /**
164 * Retrieves the value of a given setting, and places it in a variable passed by reference.
165 * @param $settingName String ID of the setting name to retrieve
166 * @param $wiki String Wiki ID of the wiki in question.
167 * @param $suffix String The suffix of the wiki in question.
168 * @param $var Reference The variable to insert the value into.
169 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
170 * @param $wikiTags Array The tags assigned to the wiki.
171 */
172 function extractVar( $setting, $wiki, $suffix, &$var, $params, $wikiTags = array() ) {
173 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
174 if ( !is_null( $value ) ) {
175 $var = $value;
176 }
177 }
178
179 /**
180 * Retrieves the value of a given setting, and places it in its corresponding global variable.
181 * @param $settingName String ID of the setting name to retrieve
182 * @param $wiki String Wiki ID of the wiki in question.
183 * @param $suffix String The suffix of the wiki in question.
184 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
185 * @param $wikiTags Array The tags assigned to the wiki.
186 */
187 function extractGlobal( $setting, $wiki, $suffix, $params, $wikiTags = array() ) {
188 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
189 if ( !is_null( $value ) ) {
190 if (substr($setting,0,1) == '+' && is_array($value)) {
191 $setting = substr($setting,1);
192 if ( is_array($GLOBALS[$setting]) ) {
193 $GLOBALS[$setting] = array_merge( $GLOBALS[$setting], $value );
194 } else {
195 $GLOBALS[$setting] = $value;
196 }
197 } else {
198 $GLOBALS[$setting] = $value;
199 }
200 }
201 }
202
203 /**
204 * Retrieves the values of all settings, and places them in their corresponding global variables.
205 * @param $wiki String Wiki ID of the wiki in question.
206 * @param $suffix String The suffix of the wiki in question.
207 * @param $params Array List of parameters. $.'key' is replaced by $value in all returned data.
208 * @param $wikiTags Array The tags assigned to the wiki.
209 */
210 function extractAllGlobals( $wiki, $suffix, $params, $wikiTags = array() ) {
211 foreach ( $this->settings as $varName => $setting ) {
212 $this->extractGlobal( $varName, $wiki, $suffix, $params, $wikiTags );
213 }
214 }
215
216 /**
217 * Work out the site and language name from a database name
218 * @param $db
219 */
220 function siteFromDB( $db ) {
221 $site = NULL;
222 $lang = NULL;
223
224 // Only run hooks if they *can* be run.
225 if (function_exists( 'wfRunHooks' ) && !wfRunHooks( 'SiteFromDB', array( $db, &$site, &$lang ) ) )
226 return array( $site, $lang );
227 foreach ( $this->suffixes as $suffix ) {
228 if ( $suffix === '' ) {
229 $site = '';
230 $lang = $db;
231 break;
232 } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
233 $site = $suffix == 'wiki' ? 'wikipedia' : $suffix;
234 $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
235 break;
236 }
237 }
238 $lang = str_replace( '_', '-', $lang );
239 return array( $site, $lang );
240 }
241
242 /** Returns true if the given vhost is handled locally. */
243 function isLocalVHost( $vhost ) {
244 return in_array( $vhost, $this->localVHosts );
245 }
246 }
247 }