Jump to content

Requests for comment/Configuration interface

From mediawiki.org
Request for comment (RFC)
Configuration interface
Component Configuration
Creation date
Author(s) Kunal Mehta
Document status implemented

As part of the configuration overhaul, we want to move configuration out of the global scope ($GLOBALS), and instead have it be obtained via Config objects. This page documents the interface that was added to MediaWiki to handle accessing of configuration settings.

Interface

[edit]

This is the interface that was merged into core in 1.23 and refactored in 1.24:

/**
 * Interface for configuration instances
 *
 * @since 1.23
 */
interface Config {

	/**
	 * Get a configuration variable such as "Sitename" or "UploadMaintenance."
	 *
	 * @param string $name Name of configuration option
	 * @return mixed Value configured
	 * @throws ConfigException
	 */
	public function get( $name );

	/**
	 * Check whether a configuration option is set for the given name
	 *
	 * @param string $name Name of configuration option
	 * @return bool
	 * @since 1.24
	 */
	public function has( $name );
}


/**
 * Interface for mutable configuration instances
 *
 * @since 1.24
 */
interface MutableConfig {

	/**
	 * Set a configuration variable such a "Sitename" to something like "My Wiki"
	 *
	 * @param string $name Name of configuration option
	 * @param mixed $value Value to set
	 * @throws ConfigException
	 */
	public function set( $name, $value );
}

There are currently three Config implementations in core:

  • GlobalVarConfig - reads from $GLOBALS and only implements Config
  • HashConfig - reads from an internal array, implements both Config and MutableConfig
  • MultiConfig - takes an array of Config objects, and reads from them with fallback logic. It only implements Config

Implementation

[edit]
/**
 * Registry of factory functions to create config objects:
 * The 'main' key must be set, and the value should be a valid
 * callable.
 * @since 1.23
 */
$wgConfigRegistry = array(
	'main' => 'GlobalVarConfig::newInstance'
);

We have a $wgConfigRegistry (yes, to get rid of globals we must add one), which allows for core and extensions to register their config objects. The 'main' config object is used by MediaWiki core. Currently RequestContext::getConfig() initializes and provides core's Config object, but it should be moved to WebStart.php so it is available when initialization happens.

We are also removing the "wg" prefix from configuration settings during the process, as they were really only needed to namespace configuration settings from all other global variables.