Stop calling FileRepo->streamFile()
[lhc/web/wiklou.git] / includes / SiteConfiguration.php
1 <?php
2 /**
3 * Configuration holder, particularly for multi-wiki sites.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\Shell\Shell;
24
25 /**
26 * This is a class for holding configuration settings, particularly for
27 * multi-wiki sites.
28 *
29 * A basic synopsis:
30 *
31 * Consider a wikifarm having three sites: two production sites, one in English
32 * and one in German, and one testing site. You can assign them easy-to-remember
33 * identifiers - ISO 639 codes 'en' and 'de' for language wikis, and 'beta' for
34 * the testing wiki.
35 *
36 * You would thus initialize the site configuration by specifying the wiki
37 * identifiers:
38 *
39 * @code
40 * $conf = new SiteConfiguration;
41 * $conf->wikis = [ 'de', 'en', 'beta' ];
42 * @endcode
43 *
44 * When configuring the MediaWiki global settings (the $wg variables),
45 * the identifiers will be available to specify settings on a per wiki basis.
46 *
47 * @code
48 * $conf->settings = [
49 * 'wgSomeSetting' => [
50 *
51 * # production:
52 * 'de' => false,
53 * 'en' => false,
54 *
55 * # test:
56 * 'beta => true,
57 * ],
58 * ];
59 * @endcode
60 *
61 * With three wikis, that is easy to manage. But what about a farm with
62 * hundreds of wikis? Site configuration provides a special keyword named
63 * 'default' which is the value used when a wiki is not found. Hence
64 * the above code could be written:
65 *
66 * @code
67 * $conf->settings = [
68 * 'wgSomeSetting' => [
69 *
70 * 'default' => false,
71 *
72 * # Enable feature on test
73 * 'beta' => true,
74 * ],
75 * ];
76 * @endcode
77 *
78 *
79 * Since settings can contain arrays, site configuration provides a way
80 * to merge an array with the default. This is very useful to avoid
81 * repeating settings again and again while still maintaining specific changes
82 * on a per wiki basis.
83 *
84 * @code
85 * $conf->settings = [
86 * 'wgMergeSetting' = [
87 * # Value that will be shared among all wikis:
88 * 'default' => [ NS_USER => true ],
89 *
90 * # Leading '+' means merging the array of value with the defaults
91 * '+beta' => [ NS_HELP => true ],
92 * ],
93 * ];
94 *
95 * # Get configuration for the German site:
96 * $conf->get( 'wgMergeSetting', 'de' );
97 * // --> [ NS_USER => true ];
98 *
99 * # Get configuration for the testing site:
100 * $conf->get( 'wgMergeSetting', 'beta' );
101 * // --> [ NS_USER => true, NS_HELP => true ];
102 * @endcode
103 *
104 * Finally, to load all configuration settings, extract them in global context:
105 *
106 * @code
107 * # Name / identifier of the wiki as set in $conf->wikis
108 * $wikiID = 'beta';
109 * $globals = $conf->getAll( $wikiID );
110 * extract( $globals );
111 * @endcode
112 *
113 * @note For WikiMap to function, the configuration must define string values for
114 * $wgServer (or $wgCanonicalServer) and $wgArticlePath, even if these are the
115 * same for all wikis or can be correctly determined by the logic in
116 * Setup.php.
117 *
118 * @todo Give examples for,
119 * suffixes:
120 * $conf->suffixes = [ 'wiki' ];
121 * localVHosts
122 * callbacks!
123 */
124 class SiteConfiguration {
125
126 /**
127 * Array of suffixes, for self::siteFromDB()
128 */
129 public $suffixes = [];
130
131 /**
132 * Array of wikis, should be the same as $wgLocalDatabases
133 */
134 public $wikis = [];
135
136 /**
137 * The whole array of settings
138 */
139 public $settings = [];
140
141 /**
142 * Array of domains that are local and can be handled by the same server
143 *
144 * @deprecated since 1.25; use $wgLocalVirtualHosts instead.
145 */
146 public $localVHosts = [];
147
148 /**
149 * Optional callback to load full configuration data.
150 * @var string|array
151 */
152 public $fullLoadCallback = null;
153
154 /** Whether or not all data has been loaded */
155 public $fullLoadDone = false;
156
157 /**
158 * A callback function that returns an array with the following keys (all
159 * optional):
160 * - suffix: site's suffix
161 * - lang: site's lang
162 * - tags: array of wiki tags
163 * - params: array of parameters to be replaced
164 * The function will receive the SiteConfiguration instance in the first
165 * argument and the wiki in the second one.
166 * if suffix and lang are passed they will be used for the return value of
167 * self::siteFromDB() and self::$suffixes will be ignored
168 *
169 * @var string|array
170 */
171 public $siteParamsCallback = null;
172
173 /**
174 * Configuration cache for getConfig()
175 * @var array
176 */
177 protected $cfgCache = [];
178
179 /**
180 * Retrieves a configuration setting for a given wiki.
181 * @param string $settingName ID of the setting name to retrieve
182 * @param string $wiki Wiki ID of the wiki in question.
183 * @param string|null $suffix The suffix of the wiki in question.
184 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
185 * @param array $wikiTags The tags assigned to the wiki.
186 * @return mixed The value of the setting requested.
187 */
188 public function get( $settingName, $wiki, $suffix = null, $params = [],
189 $wikiTags = []
190 ) {
191 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
192 return $this->getSetting( $settingName, $wiki, $params );
193 }
194
195 /**
196 * Really retrieves a configuration setting for a given wiki.
197 *
198 * @param string $settingName ID of the setting name to retrieve.
199 * @param string $wiki Wiki ID of the wiki in question.
200 * @param array $params Array of parameters.
201 * @return mixed The value of the setting requested.
202 */
203 protected function getSetting( $settingName, $wiki, array $params ) {
204 $retval = null;
205 if ( array_key_exists( $settingName, $this->settings ) ) {
206 $thisSetting =& $this->settings[$settingName];
207 do {
208 // Do individual wiki settings
209 if ( array_key_exists( $wiki, $thisSetting ) ) {
210 $retval = $thisSetting[$wiki];
211 break;
212 } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
213 $retval = $thisSetting["+$wiki"];
214 }
215
216 // Do tag settings
217 foreach ( $params['tags'] as $tag ) {
218 if ( array_key_exists( $tag, $thisSetting ) ) {
219 if ( is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
220 $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
221 } else {
222 $retval = $thisSetting[$tag];
223 }
224 break 2;
225 } elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array( $thisSetting["+$tag"] ) ) {
226 if ( $retval === null ) {
227 $retval = [];
228 }
229 $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
230 }
231 }
232 // Do suffix settings
233 $suffix = $params['suffix'];
234 if ( !is_null( $suffix ) ) {
235 if ( array_key_exists( $suffix, $thisSetting ) ) {
236 if ( is_array( $retval ) && is_array( $thisSetting[$suffix] ) ) {
237 $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
238 } else {
239 $retval = $thisSetting[$suffix];
240 }
241 break;
242 } elseif ( array_key_exists( "+$suffix", $thisSetting )
243 && is_array( $thisSetting["+$suffix"] )
244 ) {
245 if ( $retval === null ) {
246 $retval = [];
247 }
248 $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
249 }
250 }
251
252 // Fall back to default.
253 if ( array_key_exists( 'default', $thisSetting ) ) {
254 if ( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
255 $retval = self::arrayMerge( $retval, $thisSetting['default'] );
256 } else {
257 $retval = $thisSetting['default'];
258 }
259 break;
260 }
261 } while ( false );
262 }
263
264 if ( !is_null( $retval ) && count( $params['params'] ) ) {
265 foreach ( $params['params'] as $key => $value ) {
266 $retval = $this->doReplace( '$' . $key, $value, $retval );
267 }
268 }
269 return $retval;
270 }
271
272 /**
273 * Type-safe string replace; won't do replacements on non-strings
274 * private?
275 *
276 * @param string $from
277 * @param string $to
278 * @param string|array $in
279 * @return string|array
280 */
281 function doReplace( $from, $to, $in ) {
282 if ( is_string( $in ) ) {
283 return str_replace( $from, $to, $in );
284 } elseif ( is_array( $in ) ) {
285 foreach ( $in as $key => $val ) {
286 $in[$key] = $this->doReplace( $from, $to, $val );
287 }
288 return $in;
289 } else {
290 return $in;
291 }
292 }
293
294 /**
295 * Gets all settings for a wiki
296 * @param string $wiki Wiki ID of the wiki in question.
297 * @param string|null $suffix The suffix of the wiki in question.
298 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
299 * @param array $wikiTags The tags assigned to the wiki.
300 * @return array Array of settings requested.
301 */
302 public function getAll( $wiki, $suffix = null, $params = [], $wikiTags = [] ) {
303 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
304 $localSettings = [];
305 foreach ( $this->settings as $varname => $stuff ) {
306 $append = false;
307 $var = $varname;
308 if ( substr( $varname, 0, 1 ) == '+' ) {
309 $append = true;
310 $var = substr( $varname, 1 );
311 }
312
313 $value = $this->getSetting( $varname, $wiki, $params );
314 if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) {
315 $value = self::arrayMerge( $value, $GLOBALS[$var] );
316 }
317 if ( !is_null( $value ) ) {
318 $localSettings[$var] = $value;
319 }
320 }
321 return $localSettings;
322 }
323
324 /**
325 * Retrieves a configuration setting for a given wiki, forced to a boolean.
326 * @param string $setting ID of the setting name to retrieve
327 * @param string $wiki Wiki ID of the wiki in question.
328 * @param string|null $suffix The suffix of the wiki in question.
329 * @param array $wikiTags The tags assigned to the wiki.
330 * @return bool The value of the setting requested.
331 */
332 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = [] ) {
333 return (bool)$this->get( $setting, $wiki, $suffix, [], $wikiTags );
334 }
335
336 /**
337 * Retrieves an array of local databases
338 *
339 * @return array
340 */
341 function &getLocalDatabases() {
342 return $this->wikis;
343 }
344
345 /**
346 * Retrieves the value of a given setting, and places it in a variable passed by reference.
347 * @param string $setting ID of the setting name to retrieve
348 * @param string $wiki Wiki ID of the wiki in question.
349 * @param string $suffix The suffix of the wiki in question.
350 * @param array &$var Reference The variable to insert the value into.
351 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
352 * @param array $wikiTags The tags assigned to the wiki.
353 */
354 public function extractVar( $setting, $wiki, $suffix, &$var,
355 $params = [], $wikiTags = []
356 ) {
357 $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
358 if ( !is_null( $value ) ) {
359 $var = $value;
360 }
361 }
362
363 /**
364 * Retrieves the value of a given setting, and places it in its corresponding global variable.
365 * @param string $setting ID of the setting name to retrieve
366 * @param string $wiki Wiki ID of the wiki in question.
367 * @param string|null $suffix The suffix of the wiki in question.
368 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
369 * @param array $wikiTags The tags assigned to the wiki.
370 */
371 public function extractGlobal( $setting, $wiki, $suffix = null,
372 $params = [], $wikiTags = []
373 ) {
374 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
375 $this->extractGlobalSetting( $setting, $wiki, $params );
376 }
377
378 /**
379 * @param string $setting
380 * @param string $wiki
381 * @param array $params
382 */
383 public function extractGlobalSetting( $setting, $wiki, $params ) {
384 $value = $this->getSetting( $setting, $wiki, $params );
385 if ( !is_null( $value ) ) {
386 if ( substr( $setting, 0, 1 ) == '+' && is_array( $value ) ) {
387 $setting = substr( $setting, 1 );
388 if ( is_array( $GLOBALS[$setting] ) ) {
389 $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
390 } else {
391 $GLOBALS[$setting] = $value;
392 }
393 } else {
394 $GLOBALS[$setting] = $value;
395 }
396 }
397 }
398
399 /**
400 * Retrieves the values of all settings, and places them in their corresponding global variables.
401 * @param string $wiki Wiki ID of the wiki in question.
402 * @param string|null $suffix The suffix of the wiki in question.
403 * @param array $params List of parameters. $.'key' is replaced by $value in all returned data.
404 * @param array $wikiTags The tags assigned to the wiki.
405 */
406 public function extractAllGlobals( $wiki, $suffix = null, $params = [],
407 $wikiTags = []
408 ) {
409 $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
410 foreach ( $this->settings as $varName => $setting ) {
411 $this->extractGlobalSetting( $varName, $wiki, $params );
412 }
413 }
414
415 /**
416 * Return specific settings for $wiki
417 * See the documentation of self::$siteParamsCallback for more in-depth
418 * documentation about this function
419 *
420 * @param string $wiki
421 * @return array
422 */
423 protected function getWikiParams( $wiki ) {
424 static $default = [
425 'suffix' => null,
426 'lang' => null,
427 'tags' => [],
428 'params' => [],
429 ];
430
431 if ( !is_callable( $this->siteParamsCallback ) ) {
432 return $default;
433 }
434
435 $ret = ( $this->siteParamsCallback )( $this, $wiki );
436 # Validate the returned value
437 if ( !is_array( $ret ) ) {
438 return $default;
439 }
440
441 foreach ( $default as $name => $def ) {
442 if ( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) {
443 $ret[$name] = $default[$name];
444 }
445 }
446
447 return $ret;
448 }
449
450 /**
451 * Merge params between the ones passed to the function and the ones given
452 * by self::$siteParamsCallback for backward compatibility
453 * Values returned by self::getWikiParams() have the priority.
454 *
455 * @param string $wiki Wiki ID of the wiki in question.
456 * @param string $suffix The suffix of the wiki in question.
457 * @param array $params List of parameters. $.'key' is replaced by $value in
458 * all returned data.
459 * @param array $wikiTags The tags assigned to the wiki.
460 * @return array
461 */
462 protected function mergeParams( $wiki, $suffix, array $params, array $wikiTags ) {
463 $ret = $this->getWikiParams( $wiki );
464
465 if ( is_null( $ret['suffix'] ) ) {
466 $ret['suffix'] = $suffix;
467 }
468
469 $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
470
471 $ret['params'] += $params;
472
473 // Automatically fill that ones if needed
474 if ( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ) {
475 $ret['params']['lang'] = $ret['lang'];
476 }
477 if ( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) {
478 $ret['params']['site'] = $ret['suffix'];
479 }
480
481 return $ret;
482 }
483
484 /**
485 * Work out the site and language name from a database name
486 * @param string $wiki Wiki ID
487 *
488 * @return array
489 */
490 public function siteFromDB( $wiki ) {
491 // Allow override
492 $def = $this->getWikiParams( $wiki );
493 if ( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) {
494 return [ $def['suffix'], $def['lang'] ];
495 }
496
497 $site = null;
498 $lang = null;
499 foreach ( $this->suffixes as $altSite => $suffix ) {
500 if ( $suffix === '' ) {
501 $site = '';
502 $lang = $wiki;
503 break;
504 } elseif ( substr( $wiki, -strlen( $suffix ) ) == $suffix ) {
505 $site = is_numeric( $altSite ) ? $suffix : $altSite;
506 $lang = substr( $wiki, 0, strlen( $wiki ) - strlen( $suffix ) );
507 break;
508 }
509 }
510 $lang = str_replace( '_', '-', $lang );
511
512 return [ $site, $lang ];
513 }
514
515 /**
516 * Get the resolved (post-setup) configuration of a potentially foreign wiki.
517 * For foreign wikis, this is expensive, and only works if maintenance
518 * scripts are setup to handle the --wiki parameter such as in wiki farms.
519 *
520 * @param string $wiki
521 * @param array|string $settings A setting name or array of setting names
522 * @return mixed|mixed[] Array if $settings is an array, otherwise the value
523 * @throws MWException
524 * @since 1.21
525 */
526 public function getConfig( $wiki, $settings ) {
527 global $IP;
528
529 $multi = is_array( $settings );
530 $settings = (array)$settings;
531 if ( WikiMap::isCurrentWikiId( $wiki ) ) { // $wiki is this wiki
532 $res = [];
533 foreach ( $settings as $name ) {
534 if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
535 throw new MWException( "Variable '$name' does start with 'wg'." );
536 } elseif ( !isset( $GLOBALS[$name] ) ) {
537 throw new MWException( "Variable '$name' is not set." );
538 }
539 $res[$name] = $GLOBALS[$name];
540 }
541 } else { // $wiki is a foreign wiki
542 if ( isset( $this->cfgCache[$wiki] ) ) {
543 $res = array_intersect_key( $this->cfgCache[$wiki], array_flip( $settings ) );
544 if ( count( $res ) == count( $settings ) ) {
545 return $multi ? $res : current( $res ); // cache hit
546 }
547 } elseif ( !in_array( $wiki, $this->wikis ) ) {
548 throw new MWException( "No such wiki '$wiki'." );
549 } else {
550 $this->cfgCache[$wiki] = [];
551 }
552 $result = Shell::makeScriptCommand(
553 "$IP/maintenance/getConfiguration.php",
554 [
555 '--wiki', $wiki,
556 '--settings', implode( ' ', $settings ),
557 '--format', 'PHP',
558 ]
559 )
560 // limit.sh breaks this call
561 ->limits( [ 'memory' => 0, 'filesize' => 0 ] )
562 ->execute();
563
564 $data = trim( $result->getStdout() );
565 if ( $result->getExitCode() || $data === '' ) {
566 throw new MWException( "Failed to run getConfiguration.php: {$result->getStdout()}" );
567 }
568 $res = unserialize( $data );
569 if ( !is_array( $res ) ) {
570 throw new MWException( "Failed to unserialize configuration array." );
571 }
572 $this->cfgCache[$wiki] = $this->cfgCache[$wiki] + $res;
573 }
574
575 return $multi ? $res : current( $res );
576 }
577
578 /**
579 * Merge multiple arrays together.
580 * On encountering duplicate keys, merge the two, but ONLY if they're arrays.
581 * PHP's array_merge_recursive() merges ANY duplicate values into arrays,
582 * which is not fun
583 *
584 * @param array $array1
585 * @param array ...$arrays
586 *
587 * @return array
588 */
589 static function arrayMerge( array $array1, ...$arrays ) {
590 $out = $array1;
591 foreach ( $arrays as $array ) {
592 foreach ( $array as $key => $value ) {
593 if ( isset( $out[$key] ) && is_array( $out[$key] ) && is_array( $value ) ) {
594 $out[$key] = self::arrayMerge( $out[$key], $value );
595 } elseif ( !isset( $out[$key] ) || !$out[$key] && !is_numeric( $key ) ) {
596 // Values that evaluate to true given precedence, for the
597 // primary purpose of merging permissions arrays.
598 $out[$key] = $value;
599 } elseif ( is_numeric( $key ) ) {
600 $out[] = $value;
601 }
602 }
603 }
604
605 return $out;
606 }
607
608 public function loadFullData() {
609 if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
610 ( $this->fullLoadCallback )( $this );
611 $this->fullLoadDone = true;
612 }
613 }
614 }