MediaWiki REL1_33
SiteConfiguration.php
Go to the documentation of this file.
1<?php
24
125
129 public $suffixes = [];
130
134 public $wikis = [];
135
139 public $settings = [];
140
146 public $localVHosts = [];
147
152 public $fullLoadCallback = null;
153
155 public $fullLoadDone = false;
156
171 public $siteParamsCallback = null;
172
177 protected $cfgCache = [];
178
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
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
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
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] ) ) {
316 }
317 if ( !is_null( $value ) ) {
318 $localSettings[$var] = $value;
319 }
320 }
321 return $localSettings;
322 }
323
332 public function getBool( $setting, $wiki, $suffix = null, $wikiTags = [] ) {
333 return (bool)$this->get( $setting, $wiki, $suffix, [], $wikiTags );
334 }
335
341 function &getLocalDatabases() {
342 return $this->wikis;
343 }
344
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
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
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] ) ) {
390 } else {
392 }
393 } else {
395 }
396 }
397 }
398
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
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
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
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
526 public function getConfig( $wiki, $settings ) {
527 global $IP;
528
529 $multi = is_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 }
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 }
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
588 static function arrayMerge( $array1/* ... */ ) {
589 $out = $array1;
590 $argsCount = func_num_args();
591 for ( $i = 1; $i < $argsCount; $i++ ) {
592 foreach ( func_get_arg( $i ) 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}
unserialize( $serialized)
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$GLOBALS['IP']
MediaWiki exception.
Executes shell commands.
Definition Shell.php:44
This is a class for holding configuration settings, particularly for multi-wiki sites.
getConfig( $wiki, $settings)
Get the resolved (post-setup) configuration of a potentially foreign wiki.
$localVHosts
Array of domains that are local and can be handled by the same server.
extractAllGlobals( $wiki, $suffix=null, $params=[], $wikiTags=[])
Retrieves the values of all settings, and places them in their corresponding global variables.
$wikis
Array of wikis, should be the same as $wgLocalDatabases.
getSetting( $settingName, $wiki, array $params)
Really retrieves a configuration setting for a given wiki.
mergeParams( $wiki, $suffix, array $params, array $wikiTags)
Merge params between the ones passed to the function and the ones given by self::$siteParamsCallback ...
siteFromDB( $wiki)
Work out the site and language name from a database name.
static arrayMerge( $array1)
Merge multiple arrays together.
array $cfgCache
Configuration cache for getConfig()
string array $fullLoadCallback
Optional callback to load full configuration data.
extractGlobal( $setting, $wiki, $suffix=null, $params=[], $wikiTags=[])
Retrieves the value of a given setting, and places it in its corresponding global variable.
string array $siteParamsCallback
A callback function that returns an array with the following keys (all optional):
extractGlobalSetting( $setting, $wiki, $params)
getAll( $wiki, $suffix=null, $params=[], $wikiTags=[])
Gets all settings for a wiki.
extractVar( $setting, $wiki, $suffix, &$var, $params=[], $wikiTags=[])
Retrieves the value of a given setting, and places it in a variable passed by reference.
getWikiParams( $wiki)
Return specific settings for $wiki See the documentation of self::$siteParamsCallback for more in-dep...
getBool( $setting, $wiki, $suffix=null, $wikiTags=[])
Retrieves a configuration setting for a given wiki, forced to a boolean.
$settings
The whole array of settings.
& getLocalDatabases()
Retrieves an array of local databases.
$fullLoadDone
Whether or not all data has been loaded.
$suffixes
Array of suffixes, for self::siteFromDB()
doReplace( $from, $to, $in)
Type-safe string replace; won't do replacements on non-strings private?
$res
Definition database.txt:21
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:855
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
$IP
Definition update.php:3
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration settings
Definition globals.txt:37
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params
if(!isset( $args[0])) $lang