r65888 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65887‎ | r65888 | r65889 >
Date:22:58, 3 May 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.3 - partially implemented new meta data structure
Modified paths:
  • /trunk/extensions/Validator/Validator.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/Validator.class.php
@@ -24,6 +24,8 @@
2525 * this will allow for the special behaviour of the default parameter of display_points in Maps
2626 * where the actual alias influences the handling
2727 * TODO: break on fatal errors, such as missing required parameters that are dependencies
 28+ *
 29+ * FIXME: lists are broken
2830 */
2931 final class Validator {
3032
@@ -98,15 +100,42 @@
99101 'filtered_array' => array( 'ValidatorFormats', 'format_filtered_array' ),
100102 );
101103
 104+ /**
 105+ * An array containing the parameter definitions. The keys are main parameter names,
 106+ * and the values are associative arrays themselves, consisting out of elements that
 107+ * can be seen as properties of the parameter as they would be in the case of objects.
 108+ *
 109+ * @var associative array
 110+ */
102111 private $mParameterInfo;
103112
104 - private $mRawParameters = array();
105 -
 113+ /**
 114+ * An array initially containing the user provided values. Adittional data about
 115+ * the validation and formatting processes gets added later on, and so stays
 116+ * available for validation and formatting of other parameters.
 117+ *
 118+ * original-value
 119+ * default
 120+ * position
 121+ * original-name
 122+ *
 123+ *
 124+ * @var associative array
 125+ */
106126 private $mParameters = array();
 127+
 128+ /**
 129+ * Arrays for holding the (main) names of valid, invalid and unknown parameters.
 130+ */
107131 private $mValidParams = array();
108132 private $mInvalidParams = array();
109133 private $mUnknownParams = array();
110134
 135+ /**
 136+ * Holds all errors and their meta data.
 137+ *
 138+ * @var associative array
 139+ */
111140 private $mErrors = array();
112141
113142 /**
@@ -125,8 +154,6 @@
126155 *
127156 * @param array $rawParams
128157 * @param array $defaultParams
129 - *
130 - * TODO: retain info about the changing of defaults
131158 */
132159 public function parseAndSetParams( array $rawParams, array $defaultParams = array() ) {
133160 $parameters = array();
@@ -143,7 +170,7 @@
144171 if ( count( $defaultParams ) > 0 ) {
145172 $defaultParam = array_shift( $defaultParams );
146173 $parameters[$defaultParam] = array(
147 - 'value' => trim( $parts[0] ),
 174+ 'original-value' => trim( $parts[0] ),
148175 'default' => $defaultNr,
149176 'position' => $nr
150177 );
@@ -152,7 +179,7 @@
153180 } else {
154181 $name = strtolower( trim( array_shift( $parts ) ) );
155182 $parameters[$name] = array(
156 - 'value' => trim( implode( '=', $parts ) ),
 183+ 'original-value' => trim( implode( '=', $parts ) ),
157184 'default' => false,
158185 'position' => $nr
159186 );
@@ -161,47 +188,67 @@
162189 $nr++;
163190 }
164191
165 - $this->mRawParameters = $parameters;
166 - }
167 -
168 - /**
169 - * @new
170 - *
171 - * TODO: merge meta data and value arrays
172 - */
173 - public function validateAndFormatParameters() {
174192 // Loop through all the user provided parameters, and destinguise between those that are allowed and those that are not.
175 - foreach ( $this->mRawParameters as $paramName => $paramData ) {
176 - $paramValue = $paramData['value'];
 193+ foreach ( $parameters as $paramName => $paramData ) {
177194 // Attempt to get the main parameter name (takes care of aliases).
178195 $mainName = self::getMainParamName( $paramName );
 196+
179197 // If the parameter is found in the list of allowed ones, add it to the $mParameters array.
180198 if ( $mainName ) {
181199 // Check for parameter overriding. In most cases, this has already largely been taken care off,
182200 // in the form of later parameters overriding earlier ones. This is not true for different aliases though.
183201 if ( !array_key_exists( $mainName, $this->mParameters ) || self::$acceptOverriding ) {
184 - $this->mParameters[$mainName] = array(
185 - 'value' => $paramValue,
186 - 'original-name' => $paramName,
187 - 'default' => $paramData['default'],
188 - 'position' => $paramData['position']
189 - );
 202+ $paramData['original-name'] = $paramName;
 203+ $this->mParameters[$mainName] = $paramData;
190204 }
191205 else {
192206 $this->errors[] = array( 'type' => 'override', 'name' => $mainName );
193207 }
194208 }
195209 else { // If the parameter is not found in the list of allowed ones, add an item to the $this->mErrors array.
196 - if ( self::$storeUnknownParameters ) $this->mUnknownParams[$paramName] = $paramValue;
 210+ if ( self::$storeUnknownParameters ) $this->mUnknownParams[$paramName] = $paramData['original-value'];
197211 $this->mErrors[] = array( 'type' => 'unknown', 'name' => $paramName );
198212 }
 213+ }
 214+ }
 215+
 216+ /**
 217+ * Returns the main parameter name for a given parameter or alias, or false
 218+ * when it is not recognized as main parameter or alias.
 219+ *
 220+ * @param string $paramName
 221+ *
 222+ * @return string or false
 223+ */
 224+ private function getMainParamName( $paramName ) {
 225+ $result = false;
 226+
 227+ if ( array_key_exists( $paramName, $this->mParameterInfo ) ) {
 228+ $result = $paramName;
199229 }
200 -
 230+ else {
 231+ foreach ( $this->mParameterInfo as $name => $data ) {
 232+ if ( array_key_exists( 'aliases', $data ) && in_array( $paramName, $data['aliases'] ) ) {
 233+ $result = $name;
 234+ break;
 235+ }
 236+ }
 237+ }
 238+
 239+ return $result;
 240+ }
 241+
 242+ /**
 243+ * @new
 244+ *
 245+ * TODO: further impelement new meta data structure from this point on
 246+ */
 247+ public function validateAndFormatParameters() {
201248 $dependencyList = array();
202249
203250 foreach ( $this->mParameterInfo as $paramName => $paramInfo ) {
204 - $dependencyList[$paramName] = array_key_exists( 'dependencies', $paramInfo ) ?
205 - (array)$paramInfo['dependencies'] : array();
 251+ $dependencyList[$paramName] =
 252+ array_key_exists( 'dependencies', $paramInfo ) ? (array)$paramInfo['dependencies'] : array();
206253 }
207254
208255 $sorter = new TopologicalSort( $dependencyList, true );
@@ -213,7 +260,7 @@
214261 // If the user provided a value for this parameter, validate and handle it.
215262 if ( array_key_exists( $paramName, $this->mParameters ) ) {
216263
217 - $paramValue = $this->mParameters[$paramName]['value'];
 264+ $paramValue = $this->mParameters[$paramName]['original-value'];
218265 $this->cleanParameter( $paramName, $paramValue );
219266
220267 if ( $this->validateParameter( $paramName ) ) {
@@ -241,32 +288,6 @@
242289 }
243290
244291 /**
245 - * Returns the main parameter name for a given parameter or alias, or false
246 - * when it is not recognized as main parameter or alias.
247 - *
248 - * @param string $paramName
249 - *
250 - * @return string
251 - */
252 - private function getMainParamName( $paramName ) {
253 - $result = false;
254 -
255 - if ( array_key_exists( $paramName, $this->mParameterInfo ) ) {
256 - $result = $paramName;
257 - }
258 - else {
259 - foreach ( $this->mParameterInfo as $name => $data ) {
260 - if ( array_key_exists( 'aliases', $data ) && in_array( $paramName, $data['aliases'] ) ) {
261 - $result = $name;
262 - break;
263 - }
264 - }
265 - }
266 -
267 - return $result;
268 - }
269 -
270 - /**
271292 * Ensures the parameter info is valid, and splits lists.
272293 *
273294 * @param string $name
@@ -341,7 +362,7 @@
342363 $hasNoErrors = true;
343364 $checkItemCriteria = true;
344365
345 - $value = $this->mParameters[$name]['value'];
 366+ $value = $this->mParameters[$name]['original-value'];
346367
347368 if ( array_key_exists( 'list-criteria', $this->mParameterInfo[$name] ) ) {
348369 foreach ( $this->mParameterInfo[$name]['list-criteria'] as $criteriaName => $criteriaArgs ) {
@@ -435,7 +456,7 @@
436457 // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated.
437458 if ( !$isValid ) {
438459 $isList = is_array( $value );
439 - if ( $isList ) $value = $this->mRawParameters[$name];
 460+ if ( $isList ) $value = $this->mParameters[$name]['original-value'];
440461 $this->mErrors[] = array( 'type' => $criteriaName, 'args' => $criteriaArgs, 'name' => $name, 'list' => $isList, 'value' => $value );
441462 $hasNoErrors = false;
442463 if ( !self::$accumulateParameterErrors ) break;

Status & tagging log