Index: trunk/phase3/includes/conf/DatabaseConf.php |
— | — | @@ -24,10 +24,29 @@ |
25 | 25 | * @ingroup Config |
26 | 26 | */ |
27 | 27 | class DatabaseConf extends Conf { |
| 28 | + /** |
| 29 | + * @see Conf::initChangedSettings() |
| 30 | + */ |
28 | 31 | protected function initChangedSettings() { |
29 | 32 | $res = wfGetDB( DB_MASTER )->select( 'config', '*', array(), __METHOD__ ); |
30 | 33 | foreach( $res as $row ) { |
31 | | - $this->values[$row->cf_name] = $row->cf_value; |
| 34 | + $this->values[$row->cf_name] = unserialize( $row->cf_value ); |
32 | 35 | } |
33 | 36 | } |
| 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 | + } |
34 | 53 | } |
Index: trunk/phase3/includes/conf/Conf.php |
— | — | @@ -29,6 +29,15 @@ |
30 | 30 | */ |
31 | 31 | abstract class Conf { |
32 | 32 | /** |
| 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 | + /** |
33 | 42 | * The Wiki ID (usually $wgDBname) |
34 | 43 | * @var String |
35 | 44 | */ |
— | — | @@ -79,6 +88,13 @@ |
80 | 89 | abstract protected function initChangedSettings(); |
81 | 90 | |
82 | 91 | /** |
| 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 | + /** |
83 | 99 | * Initialize a new child class based on a configuration array |
84 | 100 | * @param $conf Array of configuration settings, see $wgConfiguration |
85 | 101 | * for details |
— | — | @@ -123,11 +139,12 @@ |
124 | 140 | |
125 | 141 | /** |
126 | 142 | * Actually get the setting, checking overrides, extensions, then core. |
127 | | - * On failure, |
| 143 | + * |
128 | 144 | * @param $name String Name of setting to get |
129 | 145 | * @return mixed |
130 | 146 | */ |
131 | 147 | public function retrieveSetting( $name ) { |
| 148 | + // isset() is ok here, because the default is to return null anyway. |
132 | 149 | if( isset( $this->values[$name] ) ) { |
133 | 150 | return $this->values[$name]; |
134 | 151 | } elseif( isset( $this->extensionDefaults[$name] ) ) { |
— | — | @@ -141,6 +158,39 @@ |
142 | 159 | } |
143 | 160 | |
144 | 161 | /** |
| 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 | + /** |
145 | 195 | * What is the wiki ID for this site? |
146 | 196 | * @return String |
147 | 197 | */ |