r91839 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91838‎ | r91839 | r91840 >
Date:17:54, 10 July 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
Refactoring of preference description specifications, putting all things together
Modified paths:
  • /branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php
@@ -10,128 +10,142 @@
1111
1212 class GadgetPrefs {
1313
14 - //Syntax specifications of preference description language
 14+ //Syntax specifications of preference description language.
 15+ //Each element describes a type, and it has a 'description' and may have a 'checker'.
 16+ // - 'description' is an array that describes the fields of that option.
 17+ // - 'checker' is an optional function that does validation of the entire preference description,
 18+ // when more complex semantics are needed.
1519 private static $prefsDescriptionSpecifications = array(
16 - 'boolean' => array(
17 - 'default' => array(
18 - 'isMandatory' => true,
19 - 'checker' => 'is_bool'
20 - ),
21 - 'label' => array(
22 - 'isMandatory' => true,
23 - 'checker' => 'is_string'
 20+ 'boolean' => array(
 21+ 'description' => array(
 22+ 'default' => array(
 23+ 'isMandatory' => true,
 24+ 'checker' => 'is_bool'
 25+ ),
 26+ 'label' => array(
 27+ 'isMandatory' => true,
 28+ 'checker' => 'is_string'
 29+ )
2430 )
2531 ),
2632 'string' => array(
27 - 'default' => array(
28 - 'isMandatory' => true,
29 - 'checker' => 'is_string'
 33+ 'description' => array(
 34+ 'default' => array(
 35+ 'isMandatory' => true,
 36+ 'checker' => 'is_string'
 37+ ),
 38+ 'label' => array(
 39+ 'isMandatory' => true,
 40+ 'checker' => 'is_string'
 41+ ),
 42+ 'required' => array(
 43+ 'isMandatory' => false,
 44+ 'checker' => 'is_bool'
 45+ ),
 46+ 'minlength' => array(
 47+ 'isMandatory' => false,
 48+ 'checker' => 'is_integer'
 49+ ),
 50+ 'maxlength' => array(
 51+ 'isMandatory' => false,
 52+ 'checker' => 'is_integer'
 53+ )
3054 ),
31 - 'label' => array(
32 - 'isMandatory' => true,
33 - 'checker' => 'is_string'
34 - ),
35 - 'required' => array(
36 - 'isMandatory' => false,
37 - 'checker' => 'is_bool'
38 - ),
39 - 'minlength' => array(
40 - 'isMandatory' => false,
41 - 'checker' => 'is_integer'
42 - ),
43 - 'maxlength' => array(
44 - 'isMandatory' => false,
45 - 'checker' => 'is_integer'
46 - )
 55+ 'checker' => 'GadgetPrefs::checkStringOptionDefinition'
4756 ),
4857 'number' => array(
49 - 'default' => array(
50 - 'isMandatory' => true,
51 - 'checker' => 'GadgetPrefs::isFloatOrIntOrNull'
 58+ 'description' => array(
 59+ 'default' => array(
 60+ 'isMandatory' => true,
 61+ 'checker' => 'GadgetPrefs::isFloatOrIntOrNull'
 62+ ),
 63+ 'label' => array(
 64+ 'isMandatory' => true,
 65+ 'checker' => 'is_string'
 66+ ),
 67+ 'required' => array(
 68+ 'isMandatory' => false,
 69+ 'checker' => 'is_bool'
 70+ ),
 71+ 'integer' => array(
 72+ 'isMandatory' => false,
 73+ 'checker' => 'is_bool'
 74+ ),
 75+ 'min' => array(
 76+ 'isMandatory' => false,
 77+ 'checker' => 'GadgetPrefs::isFloatOrInt'
 78+ ),
 79+ 'max' => array(
 80+ 'isMandatory' => false,
 81+ 'checker' => 'GadgetPrefs::isFloatOrInt'
 82+ )
5283 ),
53 - 'label' => array(
54 - 'isMandatory' => true,
55 - 'checker' => 'is_string'
56 - ),
57 - 'required' => array(
58 - 'isMandatory' => false,
59 - 'checker' => 'is_bool'
60 - ),
61 - 'integer' => array(
62 - 'isMandatory' => false,
63 - 'checker' => 'is_bool'
64 - ),
65 - 'min' => array(
66 - 'isMandatory' => false,
67 - 'checker' => 'GadgetPrefs::isFloatOrInt'
68 - ),
69 - 'max' => array(
70 - 'isMandatory' => false,
71 - 'checker' => 'GadgetPrefs::isFloatOrInt'
72 - )
 84+ 'checker' => 'GadgetPrefs::checkNumberOptionDefinition'
7385 ),
7486 'select' => array(
75 - 'default' => array(
76 - 'isMandatory' => true
 87+ 'description' => array(
 88+ 'default' => array(
 89+ 'isMandatory' => true
 90+ ),
 91+ 'label' => array(
 92+ 'isMandatory' => true,
 93+ 'checker' => 'is_string'
 94+ ),
 95+ 'options' => array(
 96+ 'isMandatory' => true,
 97+ 'checker' => 'is_array'
 98+ )
7799 ),
78 - 'label' => array(
79 - 'isMandatory' => true,
80 - 'checker' => 'is_string'
81 - ),
82 - 'options' => array(
83 - 'isMandatory' => true,
84 - 'checker' => 'is_array'
85 - )
 100+ 'checker' => 'GadgetPrefs::checkSelectOptionDefinition'
86101 ),
87102 'range' => array(
88 - 'default' => array(
89 - 'isMandatory' => true,
90 - 'checker' => 'GadgetPrefs::isFloatOrIntOrNull'
 103+ 'description' => array(
 104+ 'default' => array(
 105+ 'isMandatory' => true,
 106+ 'checker' => 'GadgetPrefs::isFloatOrIntOrNull'
 107+ ),
 108+ 'label' => array(
 109+ 'isMandatory' => true,
 110+ 'checker' => 'is_string'
 111+ ),
 112+ 'min' => array(
 113+ 'isMandatory' => true,
 114+ 'checker' => 'GadgetPrefs::isFloatOrInt'
 115+ ),
 116+ 'max' => array(
 117+ 'isMandatory' => true,
 118+ 'checker' => 'GadgetPrefs::isFloatOrInt'
 119+ ),
 120+ 'step' => array(
 121+ 'isMandatory' => false,
 122+ 'checker' => 'GadgetPrefs::isFloatOrInt'
 123+ )
91124 ),
92 - 'label' => array(
93 - 'isMandatory' => true,
94 - 'checker' => 'is_string'
95 - ),
96 - 'min' => array(
97 - 'isMandatory' => true,
98 - 'checker' => 'GadgetPrefs::isFloatOrInt'
99 - ),
100 - 'max' => array(
101 - 'isMandatory' => true,
102 - 'checker' => 'GadgetPrefs::isFloatOrInt'
103 - ),
104 - 'step' => array(
105 - 'isMandatory' => false,
106 - 'checker' => 'GadgetPrefs::isFloatOrInt'
107 - )
 125+ 'checker' => 'GadgetPrefs::checkRangeOptionDefinition'
108126 ),
109127 'date' => array(
110 - 'default' => array(
111 - 'isMandatory' => true
112 - ),
113 - 'label' => array(
114 - 'isMandatory' => true,
115 - 'checker' => 'is_string'
 128+ 'description' => array(
 129+ 'default' => array(
 130+ 'isMandatory' => true
 131+ ),
 132+ 'label' => array(
 133+ 'isMandatory' => true,
 134+ 'checker' => 'is_string'
 135+ )
116136 )
117137 ),
118138 'color' => array(
119 - 'default' => array(
120 - 'isMandatory' => true
121 - ),
122 - 'label' => array(
123 - 'isMandatory' => true,
124 - 'checker' => 'is_string'
 139+ 'description' => array(
 140+ 'default' => array(
 141+ 'isMandatory' => true
 142+ ),
 143+ 'label' => array(
 144+ 'isMandatory' => true,
 145+ 'checker' => 'is_string'
 146+ )
125147 )
126148 )
127149 );
128 -
129 - //Type-specific checkers for finer validation
130 - private static $typeCheckers = array(
131 - 'string' => 'GadgetPrefs::checkStringOptionDefinition',
132 - 'number' => 'GadgetPrefs::checkNumberOptionDefinition',
133 - 'select' => 'GadgetPrefs::checkSelectOptionDefinition',
134 - 'range' => 'GadgetPrefs::checkRangeOptionDefinition'
135 - );
136150
137151 //Further checks for 'string' options
138152 private static function checkStringOptionDefinition( $option ) {
@@ -234,7 +248,7 @@
235249 $mandatoryCount = array();
236250 foreach ( self::$prefsDescriptionSpecifications as $type => $typeSpec ) {
237251 $mandatoryCount[$type] = 0;
238 - foreach ( $typeSpec as $fieldName => $fieldSpec ) {
 252+ foreach ( $typeSpec['description'] as $fieldName => $fieldSpec ) {
239253 if ( $fieldSpec['isMandatory'] === true ) {
240254 ++$mandatoryCount[$type];
241255 }
@@ -265,6 +279,7 @@
266280
267281 //Check if all fields satisfy specification
268282 $typeSpec = self::$prefsDescriptionSpecifications[$type];
 283+ $typeDescription = $typeSpec['description'];
269284 $count = 0; //count of present mandatory members
270285 foreach ( $optionDefinition as $fieldName => $fieldValue ) {
271286
@@ -272,16 +287,16 @@
273288 continue; //'type' must not be checked
274289 }
275290
276 - if ( !isset( $typeSpec[$fieldName] ) ) {
 291+ if ( !isset( $typeDescription[$fieldName] ) ) {
277292 return false;
278293 }
279294
280 - if ( $typeSpec[$fieldName]['isMandatory'] ) {
 295+ if ( $typeDescription[$fieldName]['isMandatory'] ) {
281296 ++$count;
282297 }
283298
284 - if ( isset( $typeSpec[$fieldName]['checker'] ) ) {
285 - $checker = $typeSpec[$fieldName]['checker'];
 299+ if ( isset( $typeDescription[$fieldName]['checker'] ) ) {
 300+ $checker = $typeDescription[$fieldName]['checker'];
286301 if ( !call_user_func( $checker, $fieldValue ) ) {
287302 return false;
288303 }
@@ -292,9 +307,9 @@
293308 return false; //not all mandatory members are given
294309 }
295310
296 - if ( isset( self::$typeCheckers[$type] ) ) {
 311+ if ( isset( $typeSpec['checker'] ) ) {
297312 //Call type-specific checker for finer validation
298 - if ( !call_user_func( self::$typeCheckers[$type], $optionDefinition ) ) {
 313+ if ( !call_user_func( $typeSpec['checker'], $optionDefinition ) ) {
299314 return false;
300315 }
301316 }

Status & tagging log