r101575 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101574‎ | r101575 | r101576 >
Date:02:21, 2 November 2011
Author:danwe
Status:deferred
Tags:
Comment:
option for pure unnamed default parameter handling without named fall-back added.
Modified paths:
  • /trunk/extensions/Validator/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)
  • /trunk/extensions/Validator/includes/Validator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/RELEASE-NOTES
@@ -18,6 +18,10 @@
1919 registered Hooks. First option can be set via ParserHook::FH_NO_HASH to define that the function
2020 hook should be callable without leading hash ("{{plural:...}}"-like style).
2121
 22+* Option for unnamed parameter handling to work without named fallback. This allows to ignore '='
 23+ within parameter values entirely, these parameters bust be set before any named parameter then.
 24+ See Validator::setFunctionParams() and ParserHook::getParameterInfo() for details.
 25+
2226 * ParserHook Validation messages will now output text in global content language instead of users interface language.
2327
2428 === Validator 0.4.12 ===
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -470,7 +470,16 @@
471471 }
472472
473473 /**
474 - * Returns the list of default parameters.
 474+ * Returns the list of default parameters. These parameters can be used as
 475+ * unnamed parameters where it is not necessary to use the name and the '=' as
 476+ * long as there is no '=' within the value.
 477+ * It is possible to define that a parameter should not have a named fallback.
 478+ * Therefore the information has to be returnd as sub-array with the parameter
 479+ * name as first and Validator::PARAM_UNNAMED as second value. Parameter using
 480+ * this option must be set first, before any unnamed parameter in the same order
 481+ * as set here. All parameters defined before the last parameter making use of
 482+ * Validator::PARAM_UNNAMED will automatically be populated with this option.
 483+ *
475484 * Override in deriving classes to add default parameters.
476485 *
477486 * @since 0.4
Index: trunk/extensions/Validator/includes/Validator.php
@@ -10,10 +10,18 @@
1111 *
1212 * @licence GNU GPL v3 or later
1313 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ * @author Daniel Werner
1415 */
1516 class Validator {
1617
1718 /**
 19+ * Flag for default parameters used in Validator::setFunctionParams().
 20+ *
 21+ * @since 0.4.13
 22+ */
 23+ const PARAM_UNNAMED = 1;
 24+
 25+ /**
1826 * Array containing the parameters.
1927 *
2028 * @since 0.4
@@ -96,26 +104,52 @@
97105 *
98106 * @param array $rawParams
99107 * @param array $parameterInfo
100 - * @param array $defaultParams
 108+ * @param array $defaultParams array of strings or array of arrays to define which parameters can be used unnamed.
 109+ * The second value in array-form is reserved for flags. Currently, Validator::PARAM_UNNAMED determines that
 110+ * the parameter has no name which can be used to set it. Therefore all these parameters must be set before
 111+ * any named parameter. The effecdt is, that '=' within the string won't confuse the parameter anymore like
 112+ * it would happen with default parameters that still have a name as well.
101113 * @param boolean $toLower Indicates if the parameter values should be put to lower case. Defaults to true.
 114+ *
 115+ * @todo: $toLower takes no effect yet.
102116 */
103117 public function setFunctionParams( array $rawParams, array $parameterInfo, array $defaultParams = array(), $toLower = true ) {
104118 $parameters = array();
105119
106120 $nr = 0;
107121 $defaultNr = 0;
 122+ $lastUnnamedDefaultNr = -1;
108123
 124+ /*
 125+ * Find last parameter with self::PARAM_UNNAMED set. Tread all parameters in front as
 126+ * the flag were set for them as well to ensure that there can't be any unnamed params
 127+ * after the first named param. Wouldn't be possible to determine which unnamed value
 128+ * belongs to which parameter otherwise.
 129+ */
 130+ for( $i = count( $defaultParams ) - 1; $i >= 0 ; $i-- ) {
 131+ $dflt = $defaultParams[$i];
 132+ if( is_array( $dflt ) && !empty( $dflt[1] ) && ( $dflt[1] | self::PARAM_UNNAMED ) ) {
 133+ $lastUnnamedDefaultNr = $i;
 134+ break;
 135+ }
 136+ }
 137+
109138 foreach ( $rawParams as $arg ) {
110139 // Only take into account strings. If the value is not a string,
111140 // it is not a raw parameter, and can not be parsed correctly in all cases.
112 - if ( is_string( $arg ) ) {
113 - $parts = explode( '=', $arg, 2 );
 141+ if ( is_string( $arg ) ) {
 142+ $parts = explode( '=', $arg, ( $nr <= $lastUnnamedDefaultNr ? 1 : 2 ) );
114143
115144 // If there is only one part, no parameter name is provided, so try default parameter assignment.
 145+ // Default parameters having self::PARAM_UNNAMED set for having no name alias go here in any case.
116146 if ( count( $parts ) == 1 ) {
117147 // Default parameter assignment is only possible when there are default parameters!
118148 if ( count( $defaultParams ) > 0 ) {
119 - $defaultParam = strtolower( array_shift( $defaultParams ) );
 149+ $defaultParam = array_shift( $defaultParams );
 150+ if( is_array( $defaultParam ) ) {
 151+ $defaultParam = $defaultParam[0];
 152+ }
 153+ $defaultParam = strtolower( $defaultParam );
120154
121155 $parameters[$defaultParam] = array(
122156 'original-value' => trim( $parts[0] ),
@@ -161,6 +195,8 @@
162196 * @param array $parameters Parameter name as key, parameter value as value
163197 * @param array $parameterInfo List of Parameter objects
164198 * @param boolean $toLower Indicates if the parameter values should be put to lower case. Defaults to true.
 199+ *
 200+ * @todo: $toLower takes no effect yet.
165201 */
166202 public function setParameters( array $parameters, array $parameterInfo, $toLower = true ) {
167203 $this->parameters = $parameterInfo;