r88281 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88280‎ | r88281 | r88282 >
Date:00:04, 17 May 2011
Author:demon
Status:ok
Tags:
Comment:
More config stuff: method for applying settings, getting defaults, doc fixes, etc
Modified paths:
  • /trunk/phase3/includes/conf/Conf.php (modified) (history)
  • /trunk/phase3/includes/conf/DatabaseConf.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/conf/DatabaseConf.php
@@ -24,10 +24,29 @@
2525 * @ingroup Config
2626 */
2727 class DatabaseConf extends Conf {
 28+ /**
 29+ * @see Conf::initChangedSettings()
 30+ */
2831 protected function initChangedSettings() {
2932 $res = wfGetDB( DB_MASTER )->select( 'config', '*', array(), __METHOD__ );
3033 foreach( $res as $row ) {
31 - $this->values[$row->cf_name] = $row->cf_value;
 34+ $this->values[$row->cf_name] = unserialize( $row->cf_value );
3235 }
3336 }
 37+
 38+ /**
 39+ * @see Conf::writeSetting()
 40+ */
 41+ protected function writeSetting( $name, $value ) {
 42+ $dbw = wfGetDB( DB_MASTER );
 43+ $value = serialize( $value );
 44+ if( $dbw->selectRow( 'config', 'cf_name', array( 'cf_name' => $name ), __METHOD__ ) ) {
 45+ $dbw->update( 'config', array( 'cf_value' => $value ),
 46+ array( 'cf_name' => $name ), __METHOD__ );
 47+ } else {
 48+ $dbw->insert( 'config',
 49+ array( 'cf_name' => $name, 'cf_value' => $value ), __METHOD__ );
 50+ }
 51+ return (bool)$db->affectedRows();
 52+ }
3453 }
Index: trunk/phase3/includes/conf/Conf.php
@@ -29,6 +29,15 @@
3030 */
3131 abstract class Conf {
3232 /**
 33+ * A special value to return when default config items do not exist. Use
 34+ * this to differentiate from 'null' which may be a valid config value.
 35+ *
 36+ * Please don't ever make this a default (or accepted) value for your
 37+ * configuration. It's liable to Break Something.
 38+ */
 39+ const NO_SUCH_DEFAULT_CONFIG = 'mw-no-such-default-config';
 40+
 41+ /**
3342 * The Wiki ID (usually $wgDBname)
3443 * @var String
3544 */
@@ -79,6 +88,13 @@
8089 abstract protected function initChangedSettings();
8190
8291 /**
 92+ * Apply a setting to the backend store
 93+ * @param $name String Name of the setting
 94+ * @param $value mixed Value to store
 95+ */
 96+ abstract protected function writeSetting( $name, $value );
 97+
 98+ /**
8399 * Initialize a new child class based on a configuration array
84100 * @param $conf Array of configuration settings, see $wgConfiguration
85101 * for details
@@ -123,11 +139,12 @@
124140
125141 /**
126142 * Actually get the setting, checking overrides, extensions, then core.
127 - * On failure,
 143+ *
128144 * @param $name String Name of setting to get
129145 * @return mixed
130146 */
131147 public function retrieveSetting( $name ) {
 148+ // isset() is ok here, because the default is to return null anyway.
132149 if( isset( $this->values[$name] ) ) {
133150 return $this->values[$name];
134151 } elseif( isset( $this->extensionDefaults[$name] ) ) {
@@ -141,6 +158,39 @@
142159 }
143160
144161 /**
 162+ * Apply a setting to the configuration object.
 163+ * @param $name String Name of the config item
 164+ * @param $value mixed Any value to use for the key
 165+ * @param $write bool Whether to write to the static copy (db, file, etc)
 166+ */
 167+ public function applySetting( $name, $value, $write = false ) {
 168+ $this->values[$name] = $value;
 169+ if( $write && ( $value !== $this->getDefaultSetting( $name ) ) ) {
 170+ $this->writeSetting( $name, $value );
 171+ }
 172+ }
 173+
 174+ /**
 175+ * Get the default for a given setting name. Check core and then extensions.
 176+ * Will return NO_SUCH_DEFAULT_CONFIG if the config item does not exist.
 177+ *
 178+ * @param $name String Name of setting
 179+ * @return mixed
 180+ */
 181+ public function getDefaultSetting( $name ) {
 182+ // Use array_key_exists() here, to make sure we return a default
 183+ // that's really set to null.
 184+ if( array_key_exists( $name, $this->defaults ) ) {
 185+ return $this->defaults[$name];
 186+ } elseif( array_key_exists( $name, $this->extensionDefaults ) ) {
 187+ return $this->extensionDefaults[$name];
 188+ } else {
 189+ wfDebug( __METHOD__ . " called for unknown configuration item '$name'\n" );
 190+ return self::NO_SUCH_DEFAULT_CONFIG;
 191+ }
 192+ }
 193+
 194+ /**
145195 * What is the wiki ID for this site?
146196 * @return String
147197 */

Sign-offs

UserFlagDate
Nikerabbitinspected19:35, 29 June 2011

Status & tagging log