More config stuff: method for applying settings, getting defaults, doc fixes, etc

This commit is contained in:
Chad Horohoe 2011-05-17 00:04:49 +00:00
parent 83649ec5ce
commit 3b69e9fbac
2 changed files with 71 additions and 2 deletions

View file

@ -28,6 +28,15 @@
* @ingroup Config
*/
abstract class Conf {
/**
* A special value to return when default config items do not exist. Use
* this to differentiate from 'null' which may be a valid config value.
*
* Please don't ever make this a default (or accepted) value for your
* configuration. It's liable to Break Something.
*/
const NO_SUCH_DEFAULT_CONFIG = 'mw-no-such-default-config';
/**
* The Wiki ID (usually $wgDBname)
* @var String
@ -78,6 +87,13 @@ abstract class Conf {
*/
abstract protected function initChangedSettings();
/**
* Apply a setting to the backend store
* @param $name String Name of the setting
* @param $value mixed Value to store
*/
abstract protected function writeSetting( $name, $value );
/**
* Initialize a new child class based on a configuration array
* @param $conf Array of configuration settings, see $wgConfiguration
@ -123,11 +139,12 @@ abstract class Conf {
/**
* Actually get the setting, checking overrides, extensions, then core.
* On failure,
*
* @param $name String Name of setting to get
* @return mixed
*/
public function retrieveSetting( $name ) {
// isset() is ok here, because the default is to return null anyway.
if( isset( $this->values[$name] ) ) {
return $this->values[$name];
} elseif( isset( $this->extensionDefaults[$name] ) ) {
@ -140,6 +157,39 @@ abstract class Conf {
}
}
/**
* Apply a setting to the configuration object.
* @param $name String Name of the config item
* @param $value mixed Any value to use for the key
* @param $write bool Whether to write to the static copy (db, file, etc)
*/
public function applySetting( $name, $value, $write = false ) {
$this->values[$name] = $value;
if( $write && ( $value !== $this->getDefaultSetting( $name ) ) ) {
$this->writeSetting( $name, $value );
}
}
/**
* Get the default for a given setting name. Check core and then extensions.
* Will return NO_SUCH_DEFAULT_CONFIG if the config item does not exist.
*
* @param $name String Name of setting
* @return mixed
*/
public function getDefaultSetting( $name ) {
// Use array_key_exists() here, to make sure we return a default
// that's really set to null.
if( array_key_exists( $name, $this->defaults ) ) {
return $this->defaults[$name];
} elseif( array_key_exists( $name, $this->extensionDefaults ) ) {
return $this->extensionDefaults[$name];
} else {
wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
return self::NO_SUCH_DEFAULT_CONFIG;
}
}
/**
* What is the wiki ID for this site?
* @return String

View file

@ -24,10 +24,29 @@
* @ingroup Config
*/
class DatabaseConf extends Conf {
/**
* @see Conf::initChangedSettings()
*/
protected function initChangedSettings() {
$res = wfGetDB( DB_MASTER )->select( 'config', '*', array(), __METHOD__ );
foreach( $res as $row ) {
$this->values[$row->cf_name] = $row->cf_value;
$this->values[$row->cf_name] = unserialize( $row->cf_value );
}
}
/**
* @see Conf::writeSetting()
*/
protected function writeSetting( $name, $value ) {
$dbw = wfGetDB( DB_MASTER );
$value = serialize( $value );
if( $dbw->selectRow( 'config', 'cf_name', array( 'cf_name' => $name ), __METHOD__ ) ) {
$dbw->update( 'config', array( 'cf_value' => $value ),
array( 'cf_name' => $name ), __METHOD__ );
} else {
$dbw->insert( 'config',
array( 'cf_name' => $name, 'cf_value' => $value ), __METHOD__ );
}
return (bool)$db->affectedRows();
}
}