MediaWiki  1.23.0
SiteConfiguration.php
Go to the documentation of this file.
1 <?php
118 
122  public $suffixes = array();
123 
127  public $wikis = array();
128 
132  public $settings = array();
133 
137  public $localVHosts = array();
138 
143  public $fullLoadCallback = null;
144 
146  public $fullLoadDone = false;
147 
162  public $siteParamsCallback = null;
163 
168  protected $cfgCache = array();
169 
179  public function get( $settingName, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
180  $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
181  return $this->getSetting( $settingName, $wiki, $params );
182  }
183 
192  protected function getSetting( $settingName, $wiki, /*array*/ $params ) {
193  $retval = null;
194  if ( array_key_exists( $settingName, $this->settings ) ) {
195  $thisSetting =& $this->settings[$settingName];
196  do {
197  // Do individual wiki settings
198  if ( array_key_exists( $wiki, $thisSetting ) ) {
199  $retval = $thisSetting[$wiki];
200  break;
201  } elseif ( array_key_exists( "+$wiki", $thisSetting ) && is_array( $thisSetting["+$wiki"] ) ) {
202  $retval = $thisSetting["+$wiki"];
203  }
204 
205  // Do tag settings
206  foreach ( $params['tags'] as $tag ) {
207  if ( array_key_exists( $tag, $thisSetting ) ) {
208  if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$tag] ) ) {
209  $retval = self::arrayMerge( $retval, $thisSetting[$tag] );
210  } else {
211  $retval = $thisSetting[$tag];
212  }
213  break 2;
214  } elseif ( array_key_exists( "+$tag", $thisSetting ) && is_array( $thisSetting["+$tag"] ) ) {
215  if ( !isset( $retval ) ) {
216  $retval = array();
217  }
218  $retval = self::arrayMerge( $retval, $thisSetting["+$tag"] );
219  }
220  }
221  // Do suffix settings
222  $suffix = $params['suffix'];
223  if ( !is_null( $suffix ) ) {
224  if ( array_key_exists( $suffix, $thisSetting ) ) {
225  if ( isset( $retval ) && is_array( $retval ) && is_array( $thisSetting[$suffix] ) ) {
226  $retval = self::arrayMerge( $retval, $thisSetting[$suffix] );
227  } else {
228  $retval = $thisSetting[$suffix];
229  }
230  break;
231  } elseif ( array_key_exists( "+$suffix", $thisSetting ) && is_array( $thisSetting["+$suffix"] ) ) {
232  if ( !isset( $retval ) ) {
233  $retval = array();
234  }
235  $retval = self::arrayMerge( $retval, $thisSetting["+$suffix"] );
236  }
237  }
238 
239  // Fall back to default.
240  if ( array_key_exists( 'default', $thisSetting ) ) {
241  if ( is_array( $retval ) && is_array( $thisSetting['default'] ) ) {
242  $retval = self::arrayMerge( $retval, $thisSetting['default'] );
243  } else {
244  $retval = $thisSetting['default'];
245  }
246  break;
247  }
248  } while ( false );
249  }
250 
251  if ( !is_null( $retval ) && count( $params['params'] ) ) {
252  foreach ( $params['params'] as $key => $value ) {
253  $retval = $this->doReplace( '$' . $key, $value, $retval );
254  }
255  }
256  return $retval;
257  }
258 
268  function doReplace( $from, $to, $in ) {
269  if ( is_string( $in ) ) {
270  return str_replace( $from, $to, $in );
271  } elseif ( is_array( $in ) ) {
272  foreach ( $in as $key => $val ) {
273  $in[$key] = $this->doReplace( $from, $to, $val );
274  }
275  return $in;
276  } else {
277  return $in;
278  }
279  }
280 
289  public function getAll( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
290  $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
291  $localSettings = array();
292  foreach ( $this->settings as $varname => $stuff ) {
293  $append = false;
294  $var = $varname;
295  if ( substr( $varname, 0, 1 ) == '+' ) {
296  $append = true;
297  $var = substr( $varname, 1 );
298  }
299 
300  $value = $this->getSetting( $varname, $wiki, $params );
301  if ( $append && is_array( $value ) && is_array( $GLOBALS[$var] ) ) {
303  }
304  if ( !is_null( $value ) ) {
305  $localSettings[$var] = $value;
306  }
307  }
308  return $localSettings;
309  }
310 
319  public function getBool( $setting, $wiki, $suffix = null, $wikiTags = array() ) {
320  return (bool)$this->get( $setting, $wiki, $suffix, array(), $wikiTags );
321  }
322 
328  function &getLocalDatabases() {
329  return $this->wikis;
330  }
331 
341  public function extractVar( $setting, $wiki, $suffix, &$var, $params = array(), $wikiTags = array() ) {
342  $value = $this->get( $setting, $wiki, $suffix, $params, $wikiTags );
343  if ( !is_null( $value ) ) {
344  $var = $value;
345  }
346  }
347 
356  public function extractGlobal( $setting, $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
357  $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
358  $this->extractGlobalSetting( $setting, $wiki, $params );
359  }
360 
366  public function extractGlobalSetting( $setting, $wiki, $params ) {
367  $value = $this->getSetting( $setting, $wiki, $params );
368  if ( !is_null( $value ) ) {
369  if ( substr( $setting, 0, 1 ) == '+' && is_array( $value ) ) {
370  $setting = substr( $setting, 1 );
371  if ( is_array( $GLOBALS[$setting] ) ) {
372  $GLOBALS[$setting] = self::arrayMerge( $GLOBALS[$setting], $value );
373  } else {
374  $GLOBALS[$setting] = $value;
375  }
376  } else {
377  $GLOBALS[$setting] = $value;
378  }
379  }
380  }
381 
389  public function extractAllGlobals( $wiki, $suffix = null, $params = array(), $wikiTags = array() ) {
390  $params = $this->mergeParams( $wiki, $suffix, $params, $wikiTags );
391  foreach ( $this->settings as $varName => $setting ) {
392  $this->extractGlobalSetting( $varName, $wiki, $params );
393  }
394  }
395 
404  protected function getWikiParams( $wiki ) {
405  static $default = array(
406  'suffix' => null,
407  'lang' => null,
408  'tags' => array(),
409  'params' => array(),
410  );
411 
412  if ( !is_callable( $this->siteParamsCallback ) ) {
413  return $default;
414  }
415 
416  $ret = call_user_func_array( $this->siteParamsCallback, array( $this, $wiki ) );
417  # Validate the returned value
418  if ( !is_array( $ret ) ) {
419  return $default;
420  }
421 
422  foreach ( $default as $name => $def ) {
423  if ( !isset( $ret[$name] ) || ( is_array( $default[$name] ) && !is_array( $ret[$name] ) ) ) {
424  $ret[$name] = $default[$name];
425  }
426  }
427 
428  return $ret;
429  }
430 
443  protected function mergeParams( $wiki, $suffix, /*array*/ $params, /*array*/ $wikiTags ) {
444  $ret = $this->getWikiParams( $wiki );
445 
446  if ( is_null( $ret['suffix'] ) ) {
447  $ret['suffix'] = $suffix;
448  }
449 
450  $ret['tags'] = array_unique( array_merge( $ret['tags'], $wikiTags ) );
451 
452  $ret['params'] += $params;
453 
454  // Automatically fill that ones if needed
455  if ( !isset( $ret['params']['lang'] ) && !is_null( $ret['lang'] ) ) {
456  $ret['params']['lang'] = $ret['lang'];
457  }
458  if ( !isset( $ret['params']['site'] ) && !is_null( $ret['suffix'] ) ) {
459  $ret['params']['site'] = $ret['suffix'];
460  }
461 
462  return $ret;
463  }
464 
471  public function siteFromDB( $db ) {
472  // Allow override
473  $def = $this->getWikiParams( $db );
474  if ( !is_null( $def['suffix'] ) && !is_null( $def['lang'] ) ) {
475  return array( $def['suffix'], $def['lang'] );
476  }
477 
478  $site = null;
479  $lang = null;
480  foreach ( $this->suffixes as $altSite => $suffix ) {
481  if ( $suffix === '' ) {
482  $site = '';
483  $lang = $db;
484  break;
485  } elseif ( substr( $db, -strlen( $suffix ) ) == $suffix ) {
486  $site = is_numeric( $altSite ) ? $suffix : $altSite;
487  $lang = substr( $db, 0, strlen( $db ) - strlen( $suffix ) );
488  break;
489  }
490  }
491  $lang = str_replace( '_', '-', $lang );
492  return array( $site, $lang );
493  }
494 
506  public function getConfig( $wiki, $settings ) {
507  global $IP;
508 
509  $multi = is_array( $settings );
511  if ( $wiki === wfWikiID() ) { // $wiki is this wiki
512  $res = array();
513  foreach ( $settings as $name ) {
514  if ( !preg_match( '/^wg[A-Z]/', $name ) ) {
515  throw new MWException( "Variable '$name' does start with 'wg'." );
516  } elseif ( !isset( $GLOBALS[$name] ) ) {
517  throw new MWException( "Variable '$name' is not set." );
518  }
519  $res[$name] = $GLOBALS[$name];
520  }
521  } else { // $wiki is a foreign wiki
522  if ( isset( $this->cfgCache[$wiki] ) ) {
523  $res = array_intersect_key( $this->cfgCache[$wiki], array_flip( $settings ) );
524  if ( count( $res ) == count( $settings ) ) {
525  return $res; // cache hit
526  }
527  } elseif ( !in_array( $wiki, $this->wikis ) ) {
528  throw new MWException( "No such wiki '$wiki'." );
529  } else {
530  $this->cfgCache[$wiki] = array();
531  }
532  $retVal = 1;
533  $cmd = wfShellWikiCmd(
534  "$IP/maintenance/getConfiguration.php",
535  array(
536  '--wiki', $wiki,
537  '--settings', implode( ' ', $settings ),
538  '--format', 'PHP'
539  )
540  );
541  // ulimit5.sh breaks this call
542  $data = trim( wfShellExec( $cmd, $retVal, array(), array( 'memory' => 0 ) ) );
543  if ( $retVal != 0 || !strlen( $data ) ) {
544  throw new MWException( "Failed to run getConfiguration.php." );
545  }
546  $res = unserialize( $data );
547  if ( !is_array( $res ) ) {
548  throw new MWException( "Failed to unserialize configuration array." );
549  }
550  $this->cfgCache[$wiki] = $this->cfgCache[$wiki] + $res;
551  }
552 
553  return $multi ? $res : current( $res );
554  }
555 
561  public function isLocalVHost( $vhost ) {
562  return in_array( $vhost, $this->localVHosts );
563  }
564 
575  static function arrayMerge( $array1/* ... */ ) {
576  $out = $array1;
577  for ( $i = 1; $i < func_num_args(); $i++ ) {
578  foreach ( func_get_arg( $i ) as $key => $value ) {
579  if ( isset( $out[$key] ) && is_array( $out[$key] ) && is_array( $value ) ) {
580  $out[$key] = self::arrayMerge( $out[$key], $value );
581  } elseif ( !isset( $out[$key] ) || !$out[$key] && !is_numeric( $key ) ) {
582  // Values that evaluate to true given precedence, for the primary purpose of merging permissions arrays.
583  $out[$key] = $value;
584  } elseif ( is_numeric( $key ) ) {
585  $out[] = $value;
586  }
587  }
588  }
589 
590  return $out;
591  }
592 
593  public function loadFullData() {
594  if ( $this->fullLoadCallback && !$this->fullLoadDone ) {
595  call_user_func( $this->fullLoadCallback, $this );
596  $this->fullLoadDone = true;
597  }
598  }
599 }
SiteConfiguration\mergeParams
mergeParams( $wiki, $suffix, $params, $wikiTags)
Merge params between the ones passed to the function and the ones given by self::$siteParamsCallback ...
Definition: SiteConfiguration.php:440
wfShellExec
wfShellExec( $cmd, &$retval=null, $environ=array(), $limits=array(), $options=array())
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
Definition: GlobalFunctions.php:2804
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
SiteConfiguration\$fullLoadDone
$fullLoadDone
Whether or not all data has been loaded.
Definition: SiteConfiguration.php:145
SiteConfiguration
This is a class for holding configuration settings, particularly for multi-wiki sites.
Definition: SiteConfiguration.php:117
SiteConfiguration\getConfig
getConfig( $wiki, $settings)
Get the resolved (post-setup) configuration of a potentially foreign wiki.
Definition: SiteConfiguration.php:503
$ret
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:1530
$from
$from
Definition: importImages.php:90
$params
$params
Definition: styleTest.css.php:40
$in
$in
Definition: Utf8Test.php:42
SiteConfiguration\isLocalVHost
isLocalVHost( $vhost)
Returns true if the given vhost is handled locally.
Definition: SiteConfiguration.php:558
SiteConfiguration\doReplace
doReplace( $from, $to, $in)
Type-safe string replace; won't do replacements on non-strings private?
Definition: SiteConfiguration.php:265
SiteConfiguration\siteFromDB
siteFromDB( $db)
Work out the site and language name from a database name.
Definition: SiteConfiguration.php:468
MWException
MediaWiki exception.
Definition: MWException.php:26
$out
$out
Definition: UtfNormalGenerate.php:167
SiteConfiguration\extractVar
extractVar( $setting, $wiki, $suffix, &$var, $params=array(), $wikiTags=array())
Retrieves the value of a given setting, and places it in a variable passed by reference.
Definition: SiteConfiguration.php:338
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SiteConfiguration\$cfgCache
array $cfgCache
Configuration cache for getConfig()
Definition: SiteConfiguration.php:165
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
settings
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:25
SiteConfiguration\$localVHosts
$localVHosts
Array of domains that are local and can be handled by the same server.
Definition: SiteConfiguration.php:137
SiteConfiguration\$siteParamsCallback
string array $siteParamsCallback
A callback function that returns an array with the following keys (all optional):
Definition: SiteConfiguration.php:160
SiteConfiguration\extractGlobal
extractGlobal( $setting, $wiki, $suffix=null, $params=array(), $wikiTags=array())
Retrieves the value of a given setting, and places it in its corresponding global variable.
Definition: SiteConfiguration.php:353
wfShellWikiCmd
wfShellWikiCmd( $script, array $parameters=array(), array $options=array())
Generate a shell-escaped command line string to run a MediaWiki cli script.
Definition: GlobalFunctions.php:3062
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:3604
SiteConfiguration\extractAllGlobals
extractAllGlobals( $wiki, $suffix=null, $params=array(), $wikiTags=array())
Retrieves the values of all settings, and places them in their corresponding global variables.
Definition: SiteConfiguration.php:386
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
SiteConfiguration\arrayMerge
static arrayMerge( $array1)
Merge multiple arrays together.
Definition: SiteConfiguration.php:572
SiteConfiguration\getLocalDatabases
& getLocalDatabases()
Retrieves an array of local databases.
Definition: SiteConfiguration.php:325
SiteConfiguration\$wikis
$wikis
Array of wikis, should be the same as $wgLocalDatabases.
Definition: SiteConfiguration.php:127
SiteConfiguration\$fullLoadCallback
string array $fullLoadCallback
Optional callback to load full configuration data.
Definition: SiteConfiguration.php:142
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
SiteConfiguration\getBool
getBool( $setting, $wiki, $suffix=null, $wikiTags=array())
Retrieves a configuration setting for a given wiki, forced to a boolean.
Definition: SiteConfiguration.php:316
SiteConfiguration\$settings
$settings
The whole array of settings.
Definition: SiteConfiguration.php:132
SiteConfiguration\getSetting
getSetting( $settingName, $wiki, $params)
Really retrieves a configuration setting for a given wiki.
Definition: SiteConfiguration.php:189
SiteConfiguration\loadFullData
loadFullData()
Definition: SiteConfiguration.php:590
$IP
$IP
Definition: WebStart.php:88
$res
$res
Definition: database.txt:21
SiteConfiguration\extractGlobalSetting
extractGlobalSetting( $setting, $wiki, $params)
Definition: SiteConfiguration.php:363
$retval
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account incomplete not yet checked for validity & $retval
Definition: hooks.txt:237
SiteConfiguration\$suffixes
$suffixes
Array of suffixes, for self::siteFromDB()
Definition: SiteConfiguration.php:122
$GLOBALS
$GLOBALS['IP']
Definition: ComposerHookHandler.php:6
SiteConfiguration\getAll
getAll( $wiki, $suffix=null, $params=array(), $wikiTags=array())
Gets all settings for a wiki.
Definition: SiteConfiguration.php:286
SiteConfiguration\getWikiParams
getWikiParams( $wiki)
Return specific settings for $wiki See the documentation of self::$siteParamsCallback for more in-dep...
Definition: SiteConfiguration.php:401