r94736 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94735‎ | r94736 | r94737 >
Date:08:58, 17 August 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
Moved functionality to remove preferences that equal default from GadgetHooks to GadgetPrefs; fixed bug that made it fail with complex preferences description and issued PHP warnings, added unit test.
Modified paths:
  • /branches/salvatoreingala/Gadgets/Gadgets_tests.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/backend/GadgetHooks.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/Gadgets_tests.php
@@ -891,6 +891,125 @@
892892
893893
894894 /**
 895+ * Tests GadgetPrefs::simplifyPrefs.
 896+ */
 897+ function testSimplifyPrefs() {
 898+ $prefsDescription = array(
 899+ 'fields' => array(
 900+ array(
 901+ 'type' => 'boolean',
 902+ 'name' => 'foo',
 903+ 'label' => 'some label',
 904+ 'default' => true
 905+ ),
 906+ array(
 907+ 'type' => 'bundle',
 908+ 'sections' => array(
 909+ array(
 910+ 'name' => 'Section 1',
 911+ 'fields' => array(
 912+ array(
 913+ 'type' => 'boolean',
 914+ 'name' => 'bar',
 915+ 'label' => 'dummy label',
 916+ 'default' => false
 917+ ),
 918+ )
 919+ ),
 920+ array(
 921+ 'name' => 'Section 2',
 922+ 'fields' => array(
 923+ array(
 924+ 'type' => 'string',
 925+ 'name' => 'baz',
 926+ 'label' => 'A string',
 927+ 'default' => 'qwerty'
 928+ )
 929+ )
 930+ )
 931+ )
 932+ ),
 933+ array(
 934+ 'type' => 'composite',
 935+ 'name' => 'cmp',
 936+ 'fields' => array(
 937+ array(
 938+ 'type' => 'number',
 939+ 'name' => 'aNumber',
 940+ 'label' => 'A number',
 941+ 'default' => 3.14
 942+ ),
 943+ array(
 944+ 'type' => 'color',
 945+ 'name' => 'aColor',
 946+ 'label' => 'A color',
 947+ 'default' => '#a023e2'
 948+ )
 949+ )
 950+ ),
 951+ array(
 952+ 'type' => 'list',
 953+ 'name' => 'aList',
 954+ 'default' => array( 2, 3, 5, 7 ),
 955+ 'field' => array(
 956+ 'type' => 'range',
 957+ 'label' => 'A range',
 958+ 'min' => 0,
 959+ 'max' => 256,
 960+ 'default' => 128
 961+ )
 962+ )
 963+ )
 964+ );
 965+
 966+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $prefsDescription ) );
 967+
 968+ $prefs = array(
 969+ 'foo' => true, //=default
 970+ 'bar' => true,
 971+ 'baz' => 'asdfgh',
 972+ 'cmp' => array(
 973+ 'aNumber' => 2.81,
 974+ 'aColor' => '#a023e2' //=default
 975+ ),
 976+ 'aList' => array( 2, 3, 5, 9 )
 977+ );
 978+
 979+ GadgetPrefs::simplifyPrefs( $prefsDescription, $prefs );
 980+ $this->assertEquals(
 981+ $prefs,
 982+ array(
 983+ 'bar' => true,
 984+ 'baz' => 'asdfgh',
 985+ 'cmp' => array(
 986+ 'aNumber' => 2.81,
 987+ ),
 988+ 'aList' => array( 2, 3, 5, 9 )
 989+ )
 990+ );
 991+
 992+
 993+ $prefs = array(
 994+ 'foo' => false,
 995+ 'bar' => false, //=default
 996+ 'baz' => 'asdfgh',
 997+ 'cmp' => array(
 998+ 'aNumber' => 3.14, //=default
 999+ 'aColor' => '#a023e2' //=default
 1000+ ),
 1001+ 'aList' => array( 2, 3, 5, 7 ) //=default
 1002+ );
 1003+ GadgetPrefs::simplifyPrefs( $prefsDescription, $prefs );
 1004+ $this->assertEquals(
 1005+ $prefs,
 1006+ array(
 1007+ 'foo' => false,
 1008+ 'baz' => 'asdfgh'
 1009+ )
 1010+ );
 1011+ }
 1012+
 1013+ /**
8951014 * Tests GadgetPrefs::getMessages.
8961015 *
8971016 * @dataProvider prefsDescProvider
Index: branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php
@@ -30,6 +30,10 @@
3131 * an array of preference values $prefs and the name of a preference $preferenceName and returns an array where
3232 * $prefs[$prefName] is changed in a way that passes validation. If omitted, the default action is to set $prefs[$prefName]
3333 * to $prefDescription['default'].
 34+ * - 'simplifier', only for "simple" fields, is an optional function that takes two arguments, a valid description
 35+ * of a field $prefDescription, and an array of preference values $prefs; it returns an array where the preference
 36+ * encoded by $prefDescription is removed if it is equal to default. If omitted, the preference is omitted if it
 37+ * equals $prefDescription['default'].
3438 * - 'getDefault', only for "simple" fields, if a function che takes one argument, the description of the field, and
3539 * returns its default value; if omitted, the value of the 'default' field is returned.
3640 * - 'getMessages', if specified, is the name of a function that takes a valid description of a field and returns
@@ -235,7 +239,8 @@
236240 'getMessages' => 'GadgetPrefs::getCompositeMessages',
237241 'getDefault' => 'GadgetPrefs::getCompositeDefault',
238242 'checker' => 'GadgetPrefs::checkCompositePref',
239 - 'matcher' => 'GadgetPrefs::matchCompositePref'
 243+ 'matcher' => 'GadgetPrefs::matchCompositePref',
 244+ 'simplifier' => 'GadgetPrefs::simplifyCompositePref'
240245 ),
241246 'list' => array(
242247 'description' => array(
@@ -916,6 +921,44 @@
917922 }
918923
919924 /**
 925+ * Removes from $prefs all preferences that don't need to be saved, because
 926+ * they are equal to their default value.
 927+ * It is assumed that $prefsDescription is a valid description of preferences.
 928+ *
 929+ * @param $prefsDescription Array: the preferences description to use.
 930+ * @param &$prefs Array: reference of the array of preferences to simplify.
 931+ */
 932+ public static function simplifyPrefs( $prefsDescription, &$prefs ) {
 933+ $flattenedPrefs = self::flattenPrefsDescription( $prefsDescription );
 934+
 935+ foreach( $flattenedPrefs as $prefName => $prefDescription ) {
 936+ $type = $prefDescription['type'];
 937+
 938+ if ( isset( self::$prefsDescriptionSpecifications[$type]['simplifier'] ) ) {
 939+ $simplify = self::$prefsDescriptionSpecifications[$type]['simplifier'];
 940+ $prefs = call_user_func( $simplify, $prefDescription, $prefs );
 941+ } else {
 942+ $prefDefault = $prefDescription['default'];
 943+ if ( $prefs[$prefName] === $prefDefault ) {
 944+ unset( $prefs[$prefName] );
 945+ }
 946+ }
 947+ }
 948+ }
 949+
 950+ //Simplifier for 'composite' type fields
 951+ private static function simplifyCompositePref( $prefDescription, $prefs ) {
 952+ $name = $prefDescription['name'];
 953+ if ( array_key_exists( $name, $prefs ) ) {
 954+ self::simplifyPrefs( $prefDescription, $prefs[$name] );
 955+ if ( count( $prefs[$name] ) == 0 ) {
 956+ unset( $prefs[$name] );
 957+ }
 958+ }
 959+ return $prefs;
 960+ }
 961+
 962+ /**
920963 * Returns true if $str should be interpreted as a message, false otherwise.
921964 *
922965 * @param $str String
Index: branches/salvatoreingala/Gadgets/backend/GadgetHooks.php
@@ -301,16 +301,8 @@
302302 foreach ( $gadgets as $gadget ) {
303303 $prefs = $gadget->getPrefs();
304304 if ( $prefs !== null ) {
305 - $prefsDescription = $gadget->getPrefsDescription();
306 -
307305 //Remove preferences that equal their default
308 - foreach ( $prefsDescription['fields'] as $prefDescription ) {
309 - $prefName = $prefDescription['name'];
310 - $prefDefault = $prefDescription['default'];
311 - if ( $prefs[$prefName] === $prefDefault ) {
312 - unset( $prefs[$prefName] );
313 - }
314 - }
 306+ GadgetPrefs::simplifyPrefs( $gadget->getPrefsDescription(), $prefs );
315307
316308 //Save back preferences
317309 $options["gadget-{$gadget->getName()}-config"] = serialize( array(

Status & tagging log