r94403 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94402‎ | r94403 | r94404 >
Date:10:34, 13 August 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on new settings stuff
Modified paths:
  • /trunk/extensions/Maps/Maps.php (modified) (history)
  • /trunk/extensions/Maps/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/Maps/includes/Maps_Settings.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/Maps.php
@@ -83,7 +83,7 @@
8484 $wgAutoloadClasses['iMappingService'] = $incDir . 'iMappingService.php';
8585 $wgAutoloadClasses['MapsMappingServices'] = $incDir . 'Maps_MappingServices.php';
8686 $wgAutoloadClasses['MapsMappingService'] = $incDir . 'Maps_MappingService.php';
87 -$wgAutoloadClasses['MapsSettings'] = $incDir . 'Maps_Settings.php';
 87+//$wgAutoloadClasses['MapsSettings'] = $incDir . 'Maps_Settings.php';
8888
8989 // Autoload the "includes/criteria/" classes.
9090 $criDir = $incDir . 'criteria/';
@@ -229,6 +229,8 @@
230230
231231 # WMF OSM
232232 include_once $egMapsDir . 'includes/services/OSM/OSM.php';
 233+
 234+$egMapsSettings = array();
233235
234236 // Include the settings file.
235237 require_once $egMapsDir . 'Maps_Settings.php';
Index: trunk/extensions/Maps/RELEASE-NOTES
@@ -8,7 +8,7 @@
99
1010
1111 === Maps 1.0.1 ===
12 -(2011-0x-xx)
 12+(2011-08-xx)
1313
1414 * Added language parameter to the mapsdoc hook.
1515
Index: trunk/extensions/Maps/includes/Maps_Settings.php
@@ -41,7 +41,11 @@
4242 static $defaultSettings = false;
4343
4444 if ( $defaultSettings === false ) {
45 - $defaultSettings = include 'Maps.settings.php';
 45+ $defaultSettings = array();
 46+
 47+ foreach( ( include dirname( __FILE__ ) . '/../Maps.settings.php' ) as $setting ) {
 48+ $defaultSettings[$setting->getName()] = $setting->getValue();
 49+ }
4650 }
4751
4852 return $defaultSettings;
@@ -69,7 +73,7 @@
7074 */
7175 protected static function getPhpCompatSettings() {
7276 $mappings = array(
73 - 'egMapsFoobar' => 'foobar' // TODO
 77+ 'egMapsAvailableServices' => 'services',
7478 );
7579
7680 $settings = array();
@@ -135,7 +139,7 @@
136140 *
137141 * @return array
138142 */
139 - protected abstract static function getDefaultSettings();
 143+ protected static function getDefaultSettings() { return array(); }
140144
141145 /**
142146 * Initiate the settings list if not done already.
@@ -145,35 +149,48 @@
146150 * @return boolean True if the settings where initiates in this call.
147151 */
148152 protected static function initIfNeeded() {
149 - $init = self::$settings === false;
 153+ $init = static::$settings === false;
150154
151155 if ( $init ) {
152 - self::$settings = array(
153 - 'default' => self::getDefaultSettings(),
154 - 'php' => self::getPhpSettings()
 156+ static::$settings = array(
 157+ 'default' => static::getDefaultSettings(),
 158+ 'php' => static::getPhpSettings()
155159 );
156160 }
157161
158162 return $init;
159163 }
160164
 165+ /**
 166+ * Returns the settings of the specified groups, merged.
 167+ * If available in the cache, it'll be used. If not,
 168+ * and $cache is true, it'll be cached.
 169+ *
 170+ * @since 1.1
 171+ *
 172+ * @param array $groups
 173+ * @param boolean $cache
 174+ *
 175+ * @return array
 176+ */
161177 protected static function getMergedSettings( array $groups, $cache = true ) {
162178 $names = implode( '|', $groups );
163179
164 - if ( array_key_exists( $names, self::$mergedCaches ) ) {
165 - return self::$mergedCaches[$names];
 180+ if ( array_key_exists( $names, static::$mergedCaches ) ) {
 181+ return static::$mergedCaches[$names];
166182 }
167183 else {
168184 $settings = array();
169185
170186 foreach ( $groups as $group ) {
171 - if ( array_key_exists( $group, self::$settings ) ) {
172 - $settings = array_merge_recursive( $settings, self::$settings[$group] );
 187+ if ( array_key_exists( $group, static::$settings ) ) {
 188+ // TODO: recursive merge, that does not append for arrays w/o named keys.
 189+ $settings = array_merge( $settings, static::$settings[$group] );
173190 }
174191 }
175192
176193 if ( $cache ) {
177 - self::$mergedCaches[$names] = $settings;
 194+ static::$mergedCaches[$names] = $settings;
178195 }
179196
180197 return $settings;
@@ -203,16 +220,16 @@
204221 * @return array
205222 */
206223 public static function getSettings( $groups = true, $cache = true ) {
207 - self::initIfNeeded();
 224+ static::initIfNeeded();
208225
209226 if ( $groups === false ) {
210 - return self::getDefaultSettings();
 227+ return static::getDefaultSettings();
211228 }
212229 else {
213230 if ( $groups === true ) {
214 - $groups = array_keys( self::$settings );
 231+ $groups = array_keys( static::$settings );
215232 }
216 - return self::getMergedSettings( $groups, $cache );
 233+ return static::getMergedSettings( $groups, $cache );
217234 }
218235 }
219236
@@ -228,7 +245,7 @@
229246 * @return mixed
230247 */
231248 public static function get( $settingName, $groups = true, $cache = true ) {
232 - $settings = self::getSettings( $groups, $cache );
 249+ $settings = static::getSettings( $groups, $cache );
233250
234251 if ( !array_key_exists( $settingName, $settings ) ) {
235252 throw new MWException(); // TODO
@@ -249,7 +266,7 @@
250267 * @return boolean
251268 */
252269 public static function has( $settingName, $groups = true, $cache = true ) {
253 - $settings = self::getSettings( $groups, $cache );
 270+ $settings = static::getSettings( $groups, $cache );
254271 return array_key_exists( $settingName, $settings );
255272 }
256273
@@ -266,16 +283,16 @@
267284 * @return boolean
268285 */
269286 public static function set( $settingName, $settingValue, $groupName, $invalidateCache = true ) {
270 - if ( !array_key_exists( $groupName, self::$settings ) ) {
271 - self::$settings[$groupName] = array();
 287+ if ( !array_key_exists( $groupName, static::$settings ) ) {
 288+ static::$settings[$groupName] = array();
272289 }
273290 elseif ( $invalidateCache
274 - && ( !array_key_exists( $settingName, self::$settings[$groupName] )
275 - || self::$settings[$groupName][$settingName] !== $settingValue ) ) {
276 - self::ivalidateCachesForGroup( $groupName );
 291+ && ( !array_key_exists( $settingName, static::$settings[$groupName] )
 292+ || static::$settings[$groupName][$settingName] !== $settingValue ) ) {
 293+ static::ivalidateCachesForGroup( $groupName );
277294 }
278295
279 - self::$settings[$groupName][$settingName] = $settingValue;
 296+ static::$settings[$groupName][$settingName] = $settingValue;
280297 }
281298
282299 /**
@@ -286,11 +303,37 @@
287304 * @param name $group
288305 */
289306 protected static function ivalidateCachesForGroup( $group ) {
290 - foreach ( array_keys( self::$mergedCaches ) as $cacheName ) {
 307+ foreach ( array_keys( static::$mergedCaches ) as $cacheName ) {
291308 if ( in_array( $groupName, explode( '|', $cacheName ) ) ) {
292 - unset( self::$mergedCaches[$cacheName] );
 309+ unset( static::$mergedCaches[$cacheName] );
293310 }
294311 }
295312 }
296313
297314 }
 315+
 316+class Setting {
 317+
 318+ public $name;
 319+ public $value;
 320+ public $message = false;
 321+
 322+ public static function newFromValue( $name, $value ) {
 323+ return new Setting( $name, $value );
 324+ }
 325+
 326+ public function __construct( $name, $value, $message = false ) {
 327+ $this->name = $name;
 328+ $this->value = $value;
 329+ $this->message = $message;
 330+ }
 331+
 332+ public function getValue() {
 333+ return $this->value;
 334+ }
 335+
 336+ public function getName() {
 337+ return $this->name;
 338+ }
 339+
 340+}