r73697 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73696‎ | r73697 | r73698 >
Date:01:12, 25 September 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - fixed a few typo's and removed old way of handling parameter formats
Modified paths:
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/ItemParameterManipualtion.php (deleted) (history)
  • /trunk/extensions/Validator/includes/ItemParameterManipulation.php (added) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)
  • /trunk/extensions/Validator/includes/ValidationFormats.php (deleted) (history)
  • /trunk/extensions/Validator/includes/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/manipulations/ParamManipualtionBoolean.php (deleted) (history)
  • /trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolean.php (added) (history)
  • /trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolstr.php (added) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/ItemParameterManipualtion.php
@@ -1,64 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Item parameter manipulation base class. This is for manipulations
6 - * that apply to individial values, which can either be the whole value
7 - * of a non-list parameter, or a single item of a list parameter.
8 - *
9 - * @since 0.4
10 - *
11 - * @file ItemParameterManipulation.php
12 - * @ingroup Validator
13 - * @ingroup ParameterManipulations
14 - *
15 - * @author Jeroen De Dauw
16 - */
17 -abstract class ItemParameterManipulation extends ParameterManipulation {
18 -
19 - /**
20 - * Manipulate an actual value.
21 - *
22 - * @param string $value
23 - * @param array $parameters
24 - *
25 - * @since 0.4
26 - *
27 - * @return mixed
28 - */
29 - protected abstract function doManipulation( &$value, array &$parameters );
30 -
31 - /**
32 - * Constructor.
33 - *
34 - * @since 0.4
35 - */
36 - public function __construct() {
37 - parent::__construct();
38 - }
39 -
40 - /**
41 - * @see ParameterManipulation::isForLists
42 - */
43 - public function isForLists() {
44 - return false;
45 - }
46 - /**
47 - * Validate a parameter against the criterion.
48 - *
49 - * @param Parameter $parameter
50 - * @param array $parameters
51 - *
52 - * @since 0.4
53 - */
54 - public abstract function manipulate( Parameter &$parameter, array &$parameters ) {
55 - if ( is_array( $parameter->value ) ) {
56 - foreach ( $parameter->value as &$item ) {
57 - $this->doManipulation( $item, $parameters );
58 - }
59 - }
60 - else {
61 - $this->doManipulation( $parameter->value, $parameters );
62 - }
63 - }
64 -
65 -}
\ No newline at end of file
Index: trunk/extensions/Validator/includes/ValidationFormats.php
@@ -1,123 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Class holding variouse static methods for the appliance of output formats.
6 - *
7 - * @file ValidationFormats.php
8 - * @ingroup Validator
9 - *
10 - * @author Jeroen De Dauw
11 - */
12 -final class ValidationFormats {
13 -
14 - /**
15 - * Ensures the value is an array.
16 - *
17 - * @param $value
18 - * @param string name The name of the parameter.
19 - * @param array $parameters Array containing data about the so far handled parameters.
20 - */
21 - public static function format_array( &$value, $name, array $parameters ) {
22 - if ( ! is_array( $value ) ) $value = array( $value );
23 - }
24 -
25 - /**
26 - * Returns an array containing only the specified values.
27 - *
28 - * @param $value
29 - * @param string name The name of the parameter.
30 - * @param array $parameters Array containing data about the so far handled parameters.
31 - */
32 - public static function format_filtered_array( &$value, $name, array $parameters ) {
33 - // TODO: It's possible the way the allowed values are passed here is quite inneficient...
34 - $params = func_get_args();
35 - array_shift( $params ); // Ommit the value
36 -
37 - self::format_array( $value, $name, $parameters );
38 - $filtered = array();
39 - foreach ( $value as $item ) if ( in_array( $item, $params ) ) $filtered[] = $item;
40 -
41 - return $filtered;
42 - }
43 -
44 - /**
45 - * Changes the value to list notation, by separating items with a delimiter,
46 - * and/or adding wrappers before and after the items. Intended for lists, but
47 - * will also work for single values.
48 - *
49 - * @param $value
50 - * @param string name The name of the parameter.
51 - * @param array $parameters Array containing data about the so far handled parameters.
52 - * @param $delimiter
53 - * @param $wrapper
54 - */
55 - public static function format_list( &$value, $name, array $parameters, $delimiter = ',', $wrapper = '' ) {
56 - self::format_array( $value, $name, $parameters );
57 - $value = $wrapper . implode( $wrapper . $delimiter . $wrapper, $value ) . $wrapper;
58 - }
59 -
60 - /**
61 - * Changes every value into a boolean.
62 - *
63 - * TODO: work with a list of true-values.
64 - *
65 - * @param $value
66 - * @param string name The name of the parameter.
67 - * @param array $parameters Array containing data about the so far handled parameters.
68 - */
69 - public static function format_boolean( &$value, $name, array $parameters ) {
70 - if ( is_array( $value ) ) {
71 - $boolArray = array();
72 - foreach ( $value as $item ) $boolArray[] = in_array( $item, array( 'yes', 'on' ) );
73 - $value = $boolArray;
74 - }
75 - else {
76 - $value = in_array( $value, array( 'yes', 'on' ) );
77 - }
78 - }
79 -
80 - /**
81 - * Changes every value into a boolean, represented by a 'false' or 'true' string.
82 - *
83 - * @param $value
84 - * @param string name The name of the parameter.
85 - * @param array $parameters Array containing data about the so far handled parameters.
86 - */
87 - public static function format_boolean_string( &$value, $name, array $parameters ) {
88 - self::format_boolean( $value, $name, $parameters );
89 - if ( is_array( $value ) ) {
90 - $boolArray = array();
91 - foreach ( $value as $item ) $boolArray[] = $item ? 'true' : 'false';
92 - $value = $boolArray;
93 - }
94 - else {
95 - $value = $value ? 'true' : 'false';
96 - }
97 - }
98 -
99 - /**
100 - * Changes lists into strings, by enumerating the items using $wgLang->listToText.
101 - *
102 - * @param $value
103 - * @param string name The name of the parameter.
104 - * @param array $parameters Array containing data about the so far handled parameters.
105 - */
106 - public static function format_string( &$value, $name, array $parameters ) {
107 - if ( is_array( $value ) ) {
108 - global $wgLang;
109 - $value = $wgLang->listToText( $value );
110 - }
111 - }
112 -
113 - /**
114 - * Removes duplicate items from lists.
115 - *
116 - * @param $value
117 - * @param string name The name of the parameter.
118 - * @param array $parameters Array containing data about the so far handled parameters.
119 - */
120 - public static function format_unique_items( &$value, $name, array $parameters ) {
121 - if ( is_array( $value ) ) $value = array_unique( $value );
122 - }
123 -
124 -}
\ No newline at end of file
Index: trunk/extensions/Validator/includes/manipulations/ParamManipualtionBoolean.php
@@ -1,34 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Parameter manipulation converting the value to a boolean.
6 - *
7 - * @since 0.4
8 - *
9 - * @file ParamManipulationBoolean.php
10 - * @ingroup Validator
11 - * @ingroup ParameterManipulations
12 - *
13 - * @author Jeroen De Dauw
14 - */
15 -class ParamManipulationBoolean extends ItemParameterManipulation {
16 -
17 - /**
18 - * Constructor.
19 - *
20 - * @since 0.4
21 - */
22 - public function __construct() {
23 - parent::__construct();
24 - }
25 -
26 - /**
27 - * @see ItemParameterManipulation::doManipulation
28 - *
29 - * @since 0.4
30 - */
31 - public function doManipulation( &$value, array &$parameters ) {
32 - $value = in_array( $value, array( 'yes', 'on' ) );
33 - }
34 -
35 -}
\ No newline at end of file
Index: trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolean.php
@@ -0,0 +1,34 @@
 2+<?php
 3+
 4+/**
 5+ * Parameter manipulation converting the value to a boolean.
 6+ *
 7+ * @since 0.4
 8+ *
 9+ * @file ParamManipulationBoolean.php
 10+ * @ingroup Validator
 11+ * @ingroup ParameterManipulations
 12+ *
 13+ * @author Jeroen De Dauw
 14+ */
 15+class ParamManipulationBoolean extends ItemParameterManipulation {
 16+
 17+ /**
 18+ * Constructor.
 19+ *
 20+ * @since 0.4
 21+ */
 22+ public function __construct() {
 23+ parent::__construct();
 24+ }
 25+
 26+ /**
 27+ * @see ItemParameterManipulation::doManipulation
 28+ *
 29+ * @since 0.4
 30+ */
 31+ public function doManipulation( &$value, array &$parameters ) {
 32+ $value = in_array( $value, array( 'yes', 'on' ) );
 33+ }
 34+
 35+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolean.php
___________________________________________________________________
Added: svn:eol-style
136 + native
Index: trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolstr.php
@@ -0,0 +1,34 @@
 2+<?php
 3+
 4+/**
 5+ * Parameter manipulation converting the value to a boolean value as string.
 6+ *
 7+ * @since 0.4
 8+ *
 9+ * @file ParamManipulationBoolstr.php
 10+ * @ingroup Validator
 11+ * @ingroup ParameterManipulations
 12+ *
 13+ * @author Jeroen De Dauw
 14+ */
 15+class ParamManipulationBoolstr extends ItemParameterManipulation {
 16+
 17+ /**
 18+ * Constructor.
 19+ *
 20+ * @since 0.4
 21+ */
 22+ public function __construct() {
 23+ parent::__construct();
 24+ }
 25+
 26+ /**
 27+ * @see ItemParameterManipulation::doManipulation
 28+ *
 29+ * @since 0.4
 30+ */
 31+ public function doManipulation( &$value, array &$parameters ) {
 32+ $value = $value ? 'true' : 'false';
 33+ }
 34+
 35+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolstr.php
___________________________________________________________________
Added: svn:eol-style
136 + native
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -161,8 +161,6 @@
162162 $output = 'Demo: fatal error';
163163 }
164164 else {
165 - $this->validator->formatParameters();
166 -
167165 $output = $this->render( $this->validator->getParameterValues() );
168166 }
169167
Index: trunk/extensions/Validator/includes/ItemParameterManipulation.php
@@ -0,0 +1,64 @@
 2+<?php
 3+
 4+/**
 5+ * Item parameter manipulation base class. This is for manipulations
 6+ * that apply to individial values, which can either be the whole value
 7+ * of a non-list parameter, or a single item of a list parameter.
 8+ *
 9+ * @since 0.4
 10+ *
 11+ * @file ItemParameterManipulation.php
 12+ * @ingroup Validator
 13+ * @ingroup ParameterManipulations
 14+ *
 15+ * @author Jeroen De Dauw
 16+ */
 17+abstract class ItemParameterManipulation extends ParameterManipulation {
 18+
 19+ /**
 20+ * Manipulate an actual value.
 21+ *
 22+ * @param string $value
 23+ * @param array $parameters
 24+ *
 25+ * @since 0.4
 26+ *
 27+ * @return mixed
 28+ */
 29+ protected abstract function doManipulation( &$value, array &$parameters );
 30+
 31+ /**
 32+ * Constructor.
 33+ *
 34+ * @since 0.4
 35+ */
 36+ public function __construct() {
 37+ parent::__construct();
 38+ }
 39+
 40+ /**
 41+ * @see ParameterManipulation::isForLists
 42+ */
 43+ public function isForLists() {
 44+ return false;
 45+ }
 46+ /**
 47+ * Validate a parameter against the criterion.
 48+ *
 49+ * @param Parameter $parameter
 50+ * @param array $parameters
 51+ *
 52+ * @since 0.4
 53+ */
 54+ public function manipulate( Parameter &$parameter, array &$parameters ) {
 55+ if ( is_array( $parameter->value ) ) {
 56+ foreach ( $parameter->value as &$item ) {
 57+ $this->doManipulation( $item, $parameters );
 58+ }
 59+ }
 60+ else {
 61+ $this->doManipulation( $parameter->value, $parameters );
 62+ }
 63+ }
 64+
 65+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/includes/ItemParameterManipulation.php
___________________________________________________________________
Added: svn:eol-style
166 + native
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -67,15 +67,6 @@
6868 public $default;
6969
7070 /**
71 - * List of formatting functions to shape the final form of the parameter value.
72 - *
73 - * @since 0.4
74 - *
75 - * @var array
76 - */
77 - public $outputTypes = array();
78 -
79 - /**
8071 * The main name of the parameter.
8172 *
8273 * @since 0.4
@@ -109,9 +100,18 @@
110101 *
111102 * @var array of ParameterCriterion
112103 */
113 - protected $criteria;
 104+ protected $criteria = array();
114105
115106 /**
 107+ * List of manipulations the parameter value needs to undergo.
 108+ *
 109+ * @since 0.4
 110+ *
 111+ * @var array of ParameterManipulation
 112+ */
 113+ protected $manipulations = array();
 114+
 115+ /**
116116 * The original parameter name as provided by the user. This can be the
117117 * main name or an alias.
118118 *
@@ -297,10 +297,32 @@
298298 }
299299
300300 /**
 301+ * Adds one or more ParameterCriterion.
301302 *
 303+ * @since 0.4
302304 *
 305+ * @param mixed $criteria ParameterCriterion or array of ParameterCriterion
 306+ */
 307+ public function addCriteria( $criteria ) {
 308+ $this->criteria = array_merge( $this->criteria, (array)$criteria );
 309+ }
 310+
 311+ /**
 312+ * Adds one or more ParameterManipulation.
 313+ *
303314 * @since 0.4
304315 *
 316+ * @param mixed $criteria ParameterManipulation or array of ParameterManipulation
 317+ */
 318+ public function addManipulations( $manipulations ) {
 319+ $this->manipulations = array_merge( $this->manipulations, (array)$manipulations );
 320+ }
 321+
 322+ /**
 323+ *
 324+ *
 325+ * @since 0.4
 326+ *
305327 * @param string $paramName
306328 * @param string $paramValue
307329 *
@@ -367,6 +389,19 @@
368390 }
369391
370392 /**
 393+ * Applies the parameter manipulations.
 394+ *
 395+ * @since 0.4
 396+ *
 397+ * @param array $parameters
 398+ */
 399+ public function format( array &$parameters ) {
 400+ foreach ( $this->getManipulations() as $manipulation ) {
 401+ $manipulation->manipulate( $this, $parameters );
 402+ }
 403+ }
 404+
 405+ /**
371406 * Validates the parameter value.
372407 *
373408 * @since 0.4
@@ -526,6 +561,17 @@
527562 }
528563
529564 /**
 565+ * Returns the parameter manipulations.
 566+ *
 567+ * @since 0.4
 568+ *
 569+ * @return array of ParameterManipulation
 570+ */
 571+ public function getManipulations() {
 572+ return array_merge( $this->getManipulationsForType(), $this->manipulations );
 573+ }
 574+
 575+ /**
530576 * Gets the criteria for the type of the parameter.
531577 *
532578 * @since 0.4
@@ -535,8 +581,6 @@
536582 protected function getCriteriaForType() {
537583 $criteria = array();
538584
539 - // TODO: also have similar auto-adding of manipulations
540 -
541585 switch( $this->type ) {
542586 case self::TYPE_INTEGER:
543587 $criteria[] = new CriterionIsInteger();
@@ -544,7 +588,7 @@
545589 case self::TYPE_FLOAT:
546590 $criteria[] = new CriterionIsFloat();
547591 break;
548 - case self::TYPE_FLOAT: // Note: This accepts non-decimal notations!
 592+ case self::TYPE_NUMBER: // Note: This accepts non-decimal notations!
549593 $criteria[] = new CriterionIsNumeric();
550594 break;
551595 case self::TYPE_BOOLEAN:
@@ -563,6 +607,40 @@
564608 }
565609
566610 /**
 611+ * Gets the manipulation for the type of the parameter.
 612+ *
 613+ * @since 0.4
 614+ *
 615+ * @return array
 616+ */
 617+ protected function getManipulationsForType() {
 618+ $manipulations = array();
 619+
 620+ switch( $this->type ) {
 621+ case self::TYPE_INTEGER:
 622+ //$manipulations[] = new ();
 623+ break;
 624+ case self::TYPE_FLOAT:
 625+ //$manipulations[] = new ();
 626+ break;
 627+ case self::TYPE_NUMBER:
 628+ //$manipulations[] = new ();
 629+ break;
 630+ case self::TYPE_BOOLEAN:
 631+ $manipulations[] = new ParamManipulationBoolean();
 632+ break;
 633+ case self::TYPE_CHAR:
 634+ //$manipulations[] = new ();
 635+ break;
 636+ case self::TYPE_STRING: default:
 637+ // No extra criteria for strings.
 638+ break;
 639+ }
 640+
 641+ return $manipulations;
 642+ }
 643+
 644+ /**
567645 * Returns the criteria that apply to the list as a whole.
568646 *
569647 * @since 0.4
Index: trunk/extensions/Validator/includes/Validator.php
@@ -16,21 +16,6 @@
1717 * as it is not wished for all output formats in every case, and now a hacky approach is required there.
1818 */
1919 class Validator {
20 -
21 - /**
22 - * @deprecated TODO: remove
23 - *
24 - * @var array Holder for the formatting functions.
25 - */
26 - protected static $mOutputFormats = array(
27 - 'array' => array( 'ValidationFormats', 'format_array' ),
28 - 'list' => array( 'ValidationFormats', 'format_list' ),
29 - 'boolean' => array( 'ValidationFormats', 'format_boolean' ),
30 - 'boolstr' => array( 'ValidationFormats', 'format_boolean_string' ),
31 - 'string' => array( 'ValidationFormats', 'format_string' ),
32 - 'unique_items' => array( 'ValidationFormats', 'format_unique_items' ),
33 - 'filtered_array' => array( 'ValidationFormats', 'format_filtered_array' ),
34 - );
3520
3621 /**
3722 * Array containing the parameters.
@@ -71,20 +56,6 @@
7257 }
7358
7459 /**
75 - * @deprecated TODO: remove
76 - *
77 - * Adds a new output format and the formatting function that should validate values of this type.
78 - * You can use this function to override existing criteria type handlers.
79 - *
80 - * @param string $formatName The name of the format.
81 - * @param array $functionName The functions location. If it's a global function, only the name,
82 - * if it's in a class, first the class name, then the method name.
83 - */
84 - public static function addOutputFormat( $formatName, array $functionName ) {
85 - self::$mOutputFormats[strtolower( $formatName )] = $functionName;
86 - }
87 -
88 - /**
8960 * Determines the names and values of all parameters. Also takes care of default parameters.
9061 * After that the resulting parameter list is passed to Validator::setParameters
9162 *
@@ -145,6 +116,7 @@
146117 $defaultParams = $newDefaults;
147118 }
148119 }
 120+
149121 $nr++;
150122 }
151123
@@ -272,7 +244,7 @@
273245 }
274246
275247 /**
276 - * Validates all the parameters (but aborts when a fatal error occurs).
 248+ * Validates and formats all the parameters (but aborts when a fatal error occurs).
277249 *
278250 * @since 0.4
279251 */
@@ -289,37 +261,19 @@
290262 foreach ( $orderedParameters as $paramName ) {
291263 $parameter = $this->parameters[$paramName];
292264
293 - if ( !$parameter->validate() ) {
 265+ $validationSucceeded = $parameter->validate();
 266+
 267+ if ( !$validationSucceeded ) {
294268 foreach ( $parameter->getErrors() as $error ) {
295269 $this->registerError( $error );
296270 }
297271 }
 272+
 273+ $parameter->format( $this->parameters );
298274 }
299275 }
300276
301277 /**
302 - * Applies the output formats to all parameters.
303 - *
304 - * @param string $name
305 - */
306 - public function formatParameters() {
307 - foreach ( $this->parameters as $parameter ) {
308 - foreach ( $parameter->outputTypes as $outputType ) {
309 - $outputType[0] = strtolower( $outputType[0] );
310 - if ( array_key_exists( $outputType[0], self::$mOutputFormats ) ) {
311 - $parameters = array( &$parameter->value, $parameter->getName(), $this->parameters );
312 - $name = array_shift( $outputType );
313 - $parameters = array_merge( $parameters, $outputType );
314 - call_user_func_array( self::$mOutputFormats[$name], $parameters );
315 - }
316 - else {
317 - throw new Exception( 'There is no formatting function for output format ' . $outputType[0] );
318 - }
319 - }
320 - }
321 - }
322 -
323 - /**
324278 * Returns the parameters.
325279 *
326280 * @since 0.4
Index: trunk/extensions/Validator/Validator.php
@@ -65,7 +65,6 @@
6666 $wgAutoloadClasses['ParserHook'] = $incDir . 'ParserHook.php';
6767 $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php';
6868 $wgAutoloadClasses['TopologicalSort'] = $incDir . 'TopologicalSort.php';
69 -$wgAutoloadClasses['ValidationFormats'] = $incDir . 'ValidationFormats.php'; // TODO: remove
7069 $wgAutoloadClasses['ValidationError'] = $incDir . 'ValidationError.php';
7170 $wgAutoloadClasses['ValidationErrorHandler'] = $incDir . 'ValidationErrorHandler.php';
7271
@@ -81,6 +80,9 @@
8281 $wgAutoloadClasses['CriterionTrue'] = $incDir . 'criteria/CriterionTrue.php';
8382 $wgAutoloadClasses['CriterionUniqueItems'] = $incDir . 'criteria/CriterionUniqueItems.php';
8483
 84+$wgAutoloadClasses['ParamManipulationBoolean'] = $incDir . 'manipulations/ParamManipulationBoolean.php';
 85+$wgAutoloadClasses['ParamManipulationBoolstr'] = $incDir . 'manipulations/ParamManipulationBoolstr.php';
 86+
8587 $wgAutoloadClasses['ValidatorListErrors'] = $incDir . 'parserHooks/Validator_ListErrors.php';
8688 unset( $incDir );
8789

Follow-up revisions

RevisionCommit summaryAuthorDate
r73699Follow up to r73697jeroendedauw01:42, 25 September 2010

Status & tagging log