From: Chad Horohoe Date: Tue, 17 May 2011 00:04:49 +0000 (+0000) Subject: More config stuff: method for applying settings, getting defaults, doc fixes, etc X-Git-Tag: 1.31.0-rc.0~30113 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=3b69e9fbace6ee2862699815d7278a07ea32b578;p=lhc%2Fweb%2Fwiklou.git More config stuff: method for applying settings, getting defaults, doc fixes, etc --- diff --git a/includes/conf/Conf.php b/includes/conf/Conf.php index 3544f20092..a2309163e3 100755 --- a/includes/conf/Conf.php +++ b/includes/conf/Conf.php @@ -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 diff --git a/includes/conf/DatabaseConf.php b/includes/conf/DatabaseConf.php index 9b1f6efb0a..7df638534e 100644 --- a/includes/conf/DatabaseConf.php +++ b/includes/conf/DatabaseConf.php @@ -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(); + } }