Index: trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php |
— | — | @@ -105,8 +105,7 @@ |
106 | 106 | $errorList = ValidatorErrorHandler::getErrorList( self::$severityMap[$parameters['minseverity']] ); |
107 | 107 | |
108 | 108 | if ( $errorList ) { |
109 | | - // TODO: render wikitext |
110 | | - return $errorList; |
| 109 | + return $this->parser->recursiveTagParse( $errorList ); |
111 | 110 | } |
112 | 111 | else { |
113 | 112 | return ''; |
Index: trunk/extensions/Validator/includes/ParserHook.php |
— | — | @@ -13,6 +13,10 @@ |
14 | 14 | */ |
15 | 15 | abstract class ParserHook { |
16 | 16 | |
| 17 | + protected $validator; |
| 18 | + |
| 19 | + protected $parser; |
| 20 | + |
17 | 21 | /** |
18 | 22 | * Gets the name of the parser hook. |
19 | 23 | * |
— | — | @@ -34,6 +38,15 @@ |
35 | 39 | protected abstract function render( array $parameters ); |
36 | 40 | |
37 | 41 | /** |
| 42 | + * Constructor. |
| 43 | + * |
| 44 | + * @since 0.4 |
| 45 | + */ |
| 46 | + public function __construct() { |
| 47 | + $this->validator = new Validator( $this->getName() ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
38 | 51 | * Function to hook up the coordinate rendering functions to the parser. |
39 | 52 | * |
40 | 53 | * @since 0.4 |
— | — | @@ -78,6 +91,8 @@ |
79 | 92 | * @return string |
80 | 93 | */ |
81 | 94 | public function renderTag( $input, array $args, Parser $parser, PPFrame $frame ) { |
| 95 | + $this->parser = $parser; |
| 96 | + |
82 | 97 | $defaultParam = array_shift( $this->getDefaultParameters() ); |
83 | 98 | |
84 | 99 | // If there is a first default parameter, set the tag contents as it's value. |
— | — | @@ -101,8 +116,7 @@ |
102 | 117 | public function renderFunction() { |
103 | 118 | $args = func_get_args(); |
104 | 119 | |
105 | | - // No need for the parser... |
106 | | - array_shift( $args ); |
| 120 | + $this->parser = array_shift( $args ); |
107 | 121 | |
108 | 122 | return array( $this->validateAndRender( $args, false ) ); |
109 | 123 | } |
— | — | @@ -119,28 +133,26 @@ |
120 | 134 | */ |
121 | 135 | public function validateAndRender( array $arguments, $parsed ) { |
122 | 136 | global $egValidatorErrorLevel; |
123 | | - |
124 | | - $validator = new Validator(); |
125 | 137 | |
126 | 138 | if ( $parsed ) { |
127 | | - $validator->setParameters( $arguments, $this->getParameterInfo() ); |
| 139 | + $this->validator->setParameters( $arguments, $this->getParameterInfo() ); |
128 | 140 | } |
129 | 141 | else { |
130 | | - $validator->parseAndSetParams( $arguments, $this->getParameterInfo(), $this->getDefaultParameters() ); |
| 142 | + $this->validator->parseAndSetParams( $arguments, $this->getParameterInfo(), $this->getDefaultParameters() ); |
131 | 143 | } |
132 | 144 | |
133 | | - $validator->validateAndFormatParameters(); |
| 145 | + $this->validator->validateAndFormatParameters(); |
134 | 146 | |
135 | | - if ( $validator->hasErrors() && $egValidatorErrorLevel < Validator_ERRORS_STRICT ) { |
136 | | - $validator->correctInvalidParams(); |
| 147 | + if ( $this->validator->hasErrors() && $egValidatorErrorLevel < Validator_ERRORS_STRICT ) { |
| 148 | + $this->validator->correctInvalidParams(); |
137 | 149 | } |
138 | 150 | |
139 | | - if ( $validator->hasFatalError() ) { |
| 151 | + if ( $this->validator->hasFatalError() ) { |
140 | 152 | // TODO |
141 | 153 | $output = 'Demo: fatal error'; |
142 | 154 | } |
143 | 155 | else { |
144 | | - $output = $this->render( $validator->getValidParams( false ) ); |
| 156 | + $output = $this->render( $this->validator->getValidParams( false ) ); |
145 | 157 | } |
146 | 158 | |
147 | 159 | return $output; |
Index: trunk/extensions/Validator/includes/ValidationManager.php |
— | — | @@ -3,6 +3,8 @@ |
4 | 4 | /** |
5 | 5 | * Class for parameter handling. |
6 | 6 | * |
| 7 | + * @deprecated |
| 8 | + * |
7 | 9 | * @file ValidationManager.php |
8 | 10 | * @ingroup Validator |
9 | 11 | * |
Index: trunk/extensions/Validator/includes/Validator.php |
— | — | @@ -129,27 +129,62 @@ |
130 | 130 | protected $errors = array(); |
131 | 131 | |
132 | 132 | /** |
133 | | - * Constructor. |
134 | 133 | * |
| 134 | + * |
135 | 135 | * @since 0.4 |
| 136 | + * |
| 137 | + * @var string |
136 | 138 | */ |
137 | | - public function __construct() { |
138 | | - // TODO |
139 | | - } |
| 139 | + protected $element; |
140 | 140 | |
141 | 141 | /** |
| 142 | + * Constructor. |
142 | 143 | * |
| 144 | + * @param srting $element |
143 | 145 | * |
144 | 146 | * @since 0.4 |
145 | | - * |
146 | | - * @return string |
147 | 147 | */ |
148 | | - protected function getElement() { |
149 | | - return ''; |
150 | | - // TODO |
151 | | - } |
| 148 | + public function __construct( $element = '' ) { |
| 149 | + $this->element = $element; |
| 150 | + } |
152 | 151 | |
153 | 152 | /** |
| 153 | + * Adds a new criteria type and the validation function that should validate values of this type. |
| 154 | + * You can use this function to override existing criteria type handlers. |
| 155 | + * |
| 156 | + * @param string $criteriaName The name of the cirteria. |
| 157 | + * @param array $functionName The functions location. If it's a global function, only the name, |
| 158 | + * if it's in a class, first the class name, then the method name. |
| 159 | + */ |
| 160 | + public static function addValidationFunction( $criteriaName, array $functionName ) { |
| 161 | + self::$mValidationFunctions[$criteriaName] = $functionName; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Adds a new list criteria type and the validation function that should validate values of this type. |
| 166 | + * You can use this function to override existing criteria type handlers. |
| 167 | + * |
| 168 | + * @param string $criteriaName The name of the list cirteria. |
| 169 | + * @param array $functionName The functions location. If it's a global function, only the name, |
| 170 | + * if it's in a class, first the class name, then the method name. |
| 171 | + */ |
| 172 | + public static function addListValidationFunction( $criteriaName, array $functionName ) { |
| 173 | + self::$mListValidationFunctions[strtolower( $criteriaName )] = $functionName; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Adds a new output format and the formatting function that should validate values of this type. |
| 178 | + * You can use this function to override existing criteria type handlers. |
| 179 | + * |
| 180 | + * @param string $formatName The name of the format. |
| 181 | + * @param array $functionName The functions location. If it's a global function, only the name, |
| 182 | + * if it's in a class, first the class name, then the method name. |
| 183 | + */ |
| 184 | + public static function addOutputFormat( $formatName, array $functionName ) { |
| 185 | + self::$mOutputFormats[strtolower( $formatName )] = $functionName; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
154 | 189 | * Registers an error. |
155 | 190 | * |
156 | 191 | * @param string $message |
— | — | @@ -160,7 +195,7 @@ |
161 | 196 | $error = new ValidatorError( |
162 | 197 | $message, |
163 | 198 | $severity, |
164 | | - $this->getElement(), |
| 199 | + $this->element, |
165 | 200 | (array)$tags |
166 | 201 | ); |
167 | 202 | |
— | — | @@ -392,7 +427,7 @@ |
393 | 428 | $paramName |
394 | 429 | ), |
395 | 430 | 'missing' |
396 | | - ); |
| 431 | + ); |
397 | 432 | } |
398 | 433 | else { |
399 | 434 | // Set the default value (or default 'default value' if none is provided), and ensure the type is correct. |
— | — | @@ -815,43 +850,117 @@ |
816 | 851 | * @return boolean |
817 | 852 | */ |
818 | 853 | public function hasFatalError() { |
819 | | - // TODO |
820 | | - return false; |
| 854 | + $has = false; |
| 855 | + |
| 856 | + foreach ( $this->errors as $error ) { |
| 857 | + if ( $error->severity >= ValidatorError::SEVERITY_CRITICAL ) { |
| 858 | + $has = true; |
| 859 | + break; |
| 860 | + } |
| 861 | + } |
| 862 | + |
| 863 | + return $has; |
821 | 864 | } |
822 | | - |
823 | | - /** |
824 | | - * Adds a new criteria type and the validation function that should validate values of this type. |
825 | | - * You can use this function to override existing criteria type handlers. |
826 | | - * |
827 | | - * @param string $criteriaName The name of the cirteria. |
828 | | - * @param array $functionName The functions location. If it's a global function, only the name, |
829 | | - * if it's in a class, first the class name, then the method name. |
830 | | - */ |
831 | | - public static function addValidationFunction( $criteriaName, array $functionName ) { |
832 | | - self::$mValidationFunctions[$criteriaName] = $functionName; |
833 | | - } |
834 | 865 | |
835 | 866 | /** |
836 | | - * Adds a new list criteria type and the validation function that should validate values of this type. |
837 | | - * You can use this function to override existing criteria type handlers. |
838 | | - * |
839 | | - * @param string $criteriaName The name of the list cirteria. |
840 | | - * @param array $functionName The functions location. If it's a global function, only the name, |
841 | | - * if it's in a class, first the class name, then the method name. |
| 867 | + * Returns an error message for a criteria validation that failed. |
| 868 | + * |
| 869 | + * @since 0.4 |
| 870 | + * |
| 871 | + * @param string $criteria |
| 872 | + * @param string $paramName |
| 873 | + * @param string $paramValue |
| 874 | + * @param array $args |
| 875 | + * @param boolean $isList |
| 876 | + * @param array $invalidItems |
| 877 | + * |
| 878 | + * @return string |
842 | 879 | */ |
843 | | - public static function addListValidationFunction( $criteriaName, array $functionName ) { |
844 | | - self::$mListValidationFunctions[strtolower( $criteriaName )] = $functionName; |
| 880 | + protected function getCriteriaErrorMessage( $criteria, $paramName, $paramValue, array $args = array(), $isList = false, array $invalidItems = array() ) { |
| 881 | + global $wgLang, $egValidatorErrorLevel; |
| 882 | + |
| 883 | + if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && $this->validator->hasErrors() ) { |
| 884 | + $rawErrors = $this->validator->getErrors(); |
| 885 | + |
| 886 | + $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', count( $rawErrors ) ) . '</b><br /><i>'; |
| 887 | + |
| 888 | + $errors = array(); |
| 889 | + |
| 890 | + foreach ( $rawErrors as $error ) { |
| 891 | + $error['name'] = '<b>' . Sanitizer::escapeId( $error['name'] ) . '</b>'; |
| 892 | + |
| 893 | + if ( $error['type'] == 'unknown' ) { |
| 894 | + $errors[] = wfMsgExt( 'validator_error_unknown_argument', array( 'parsemag' ), $error['name'] ); |
| 895 | + } |
| 896 | + elseif ( $error['type'] == 'missing' ) { |
| 897 | + $errors[] = wfMsgExt( 'validator_error_required_missing', array( 'parsemag' ), $error['name'] ); |
| 898 | + } |
| 899 | + elseif ( array_key_exists( 'list', $error ) && $error['list'] ) { |
| 900 | + switch( $error['type'] ) { |
| 901 | + case 'not_empty' : |
| 902 | + $msg = wfMsgExt( 'validator_list_error_empty_argument', array( 'parsemag' ), $error['name'] ); |
| 903 | + break; |
| 904 | + case 'in_range' : |
| 905 | + $msg = wfMsgExt( 'validator_list_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' ); |
| 906 | + break; |
| 907 | + case 'is_numeric' : |
| 908 | + $msg = wfMsgExt( 'validator_list_error_must_be_number', array( 'parsemag' ), $error['name'] ); |
| 909 | + break; |
| 910 | + case 'is_integer' : |
| 911 | + $msg = wfMsgExt( 'validator_list_error_must_be_integer', array( 'parsemag' ), $error['name'] ); |
| 912 | + break; |
| 913 | + case 'in_array' : |
| 914 | + $itemsText = $wgLang->listToText( $error['args'] ); |
| 915 | + $msg = wfMsgExt( 'validator_error_accepts_only', array( 'parsemag' ), $error['name'], $itemsText, count( $error['args'] ) ); |
| 916 | + break; |
| 917 | + case 'invalid' : default : |
| 918 | + $msg = wfMsgExt( 'validator_list_error_invalid_argument', array( 'parsemag' ), $error['name'] ); |
| 919 | + break; |
| 920 | + } |
| 921 | + |
| 922 | + if ( array_key_exists( 'invalid-items', $error ) ) { |
| 923 | + $omitted = array(); |
| 924 | + foreach ( $error['invalid-items'] as $item ) $omitted[] = Sanitizer::escapeId( $item ); |
| 925 | + $msg .= ' ' . wfMsgExt( 'validator_list_omitted', array( 'parsemag' ), |
| 926 | + $wgLang->listToText( $omitted ), count( $omitted ) ); |
| 927 | + } |
| 928 | + |
| 929 | + $errors[] = $msg; |
| 930 | + } |
| 931 | + else { |
| 932 | + switch( $error['type'] ) { |
| 933 | + case 'not_empty' : |
| 934 | + $errors[] = wfMsgExt( 'validator_error_empty_argument', array( 'parsemag' ), $error['name'] ); |
| 935 | + break; |
| 936 | + case 'in_range' : |
| 937 | + $errors[] = wfMsgExt( 'validator_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' ); |
| 938 | + break; |
| 939 | + case 'is_numeric' : |
| 940 | + $errors[] = wfMsgExt( 'validator_error_must_be_number', array( 'parsemag' ), $error['name'] ); |
| 941 | + break; |
| 942 | + case 'is_integer' : |
| 943 | + $errors[] = wfMsgExt( 'validator_error_must_be_integer', array( 'parsemag' ), $error['name'] ); |
| 944 | + break; |
| 945 | + case 'in_array' : |
| 946 | + $itemsText = $wgLang->listToText( $error['args'] ); |
| 947 | + $errors[] = wfMsgExt( 'validator_error_accepts_only', array( 'parsemag' ), $error['name'], $itemsText, count( $error['args'] ) ); |
| 948 | + break; |
| 949 | + case 'invalid' : default : |
| 950 | + $errors[] = wfMsgExt( 'validator_error_invalid_argument', array( 'parsemag' ), '<b>' . htmlspecialchars( $error['value'] ) . '</b>', $error['name'] ); |
| 951 | + break; |
| 952 | + } |
| 953 | + } |
| 954 | + } |
| 955 | + |
| 956 | + return $errorList . implode( $errors, '<br />' ) . '</i><br />'; |
| 957 | + } |
| 958 | + elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && $this->validator->hasErrors() ) { |
| 959 | + return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), count( $this->validator->getErrors() ) ) . '</b>'; |
| 960 | + } |
| 961 | + else { |
| 962 | + return ''; |
| 963 | + } |
| 964 | + |
845 | 965 | } |
846 | 966 | |
847 | | - /** |
848 | | - * Adds a new output format and the formatting function that should validate values of this type. |
849 | | - * You can use this function to override existing criteria type handlers. |
850 | | - * |
851 | | - * @param string $formatName The name of the format. |
852 | | - * @param array $functionName The functions location. If it's a global function, only the name, |
853 | | - * if it's in a class, first the class name, then the method name. |
854 | | - */ |
855 | | - public static function addOutputFormat( $formatName, array $functionName ) { |
856 | | - self::$mOutputFormats[strtolower( $formatName )] = $functionName; |
857 | | - } |
858 | 967 | } |
\ No newline at end of file |
Index: trunk/extensions/Validator/Validator.i18n.php |
— | — | @@ -23,7 +23,7 @@ |
24 | 24 | // General errors |
25 | 25 | 'validator_error_unknown_argument' => '$1 is not a valid parameter.', |
26 | 26 | 'validator_error_required_missing' => 'The required parameter $1 is not provided.', |
27 | | - 'validator-error-override-argument' => 'Tried to override parameter $1 ($2) with $3', |
| 27 | + 'validator-error-override-argument' => 'Tried to override parameter $1 (value: $2) with value "$3"', |
28 | 28 | |
29 | 29 | // Criteria errors for single values |
30 | 30 | 'validator_error_empty_argument' => 'Parameter $1 can not have an empty value.', |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -24,7 +24,7 @@ |
25 | 25 | die( 'Not an entry point.' ); |
26 | 26 | } |
27 | 27 | |
28 | | -define( 'Validator_VERSION', '0.4 alpha-2' ); |
| 28 | +define( 'Validator_VERSION', '0.4 alpha-3' ); |
29 | 29 | |
30 | 30 | // Constants indicating the strictness of the parameter validation. |
31 | 31 | define( 'Validator_ERRORS_NONE', 0 ); |
— | — | @@ -45,6 +45,12 @@ |
46 | 46 | 'descriptionmsg' => 'validator-desc', |
47 | 47 | ); |
48 | 48 | |
| 49 | +// This function has been deprecated in 1.16, but needed for earlier versions. |
| 50 | +// It's present in 1.16 as a stub, but lets check if it exists in case it gets removed at some point. |
| 51 | +if ( function_exists( 'wfLoadExtensionMessages' ) ) { |
| 52 | + wfLoadExtensionMessages( 'Validator' ); |
| 53 | +} |
| 54 | + |
49 | 55 | // Autoload the general classes. |
50 | 56 | $incDir = dirname( __FILE__ ) . '/includes/'; |
51 | 57 | $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php'; |