http://www.mediawiki.org/wiki/Special:Code/MediaWiki/72191
Revision: 72191
Author: jeroendedauw
Date: 2010-09-02 10:18:20 +0000 (Thu, 02 Sep 2010)
Log Message:
-----------
Changes for 0.4 - renamed files and classes to make more sense
Modified Paths:
--------------
trunk/extensions/Validator/Validator.php
trunk/extensions/Validator/includes/ParserHook.php
trunk/extensions/Validator/includes/Validator.php
Added Paths:
-----------
trunk/extensions/Validator/includes/ValidationErrors.php
trunk/extensions/Validator/includes/ValidationFormats.php
trunk/extensions/Validator/includes/ValidationFunctions.php
trunk/extensions/Validator/includes/ValidationManager.php
Removed Paths:
-------------
trunk/extensions/Validator/includes/Validator_Formats.php
trunk/extensions/Validator/includes/Validator_Functions.php
trunk/extensions/Validator/includes/Validator_Manager.php
Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php 2010-09-02 09:53:32 UTC (rev
72190)
+++ trunk/extensions/Validator/Validator.php 2010-09-02 10:18:20 UTC (rev
72191)
@@ -24,7 +24,7 @@
die( 'Not an entry point.' );
}
-define( 'Validator_VERSION', '0.4 alpha-1' );
+define( 'Validator_VERSION', '0.4 alpha-2' );
// Constants indicating the strictness of the parameter validation.
define( 'Validator_ERRORS_NONE', 0 );
@@ -52,8 +52,8 @@
$incDir = dirname( __FILE__ ) . '/includes/';
$wgAutoloadClasses['Validator'] = $incDir .
'Validator.php';
$wgAutoloadClasses['ParserHook'] = $incDir .
'ParserHook.php';
-$wgAutoloadClasses['ValidatorFunctions'] = $incDir .
'Validator_Functions.php';
-$wgAutoloadClasses['ValidatorFormats'] = $incDir .
'Validator_Formats.php';
-$wgAutoloadClasses['ValidatorManager'] = $incDir .
'Validator_Manager.php';
+$wgAutoloadClasses['ValidationFunctions'] = $incDir .
'ValidationFunctions.php';
+$wgAutoloadClasses['ValidationFormats'] = $incDir .
'ValidationFormats.php';
+$wgAutoloadClasses['ValidationManager'] = $incDir .
'ValidationManager.php';
$wgAutoloadClasses['TopologicalSort'] = $incDir .
'TopologicalSort.php';
unset( $incDir );
\ No newline at end of file
Modified: trunk/extensions/Validator/includes/ParserHook.php
===================================================================
--- trunk/extensions/Validator/includes/ParserHook.php 2010-09-02 09:53:32 UTC
(rev 72190)
+++ trunk/extensions/Validator/includes/ParserHook.php 2010-09-02 10:18:20 UTC
(rev 72191)
@@ -113,14 +113,11 @@
* @return string
*/
public function validateAndRender( array $arguments, $parsed ) {
- $manager = new ValidatorManager();
+ $manager = new ValidationManager();
if ( $parsed ) {
- $doRender = $manager->manageParsedParameters(
- $arguments,
- $this->getParameterInfo(),
- $this->getDefaultParameters()
- );
+ $validator->setParameters( $arguments,
$this->getParameterInfo() );
+ $validator->validateAndFormatParameters();
}
else {
$doRender = $manager->manageParameters(
Added: trunk/extensions/Validator/includes/ValidationErrors.php
===================================================================
--- trunk/extensions/Validator/includes/ValidationErrors.php
(rev 0)
+++ trunk/extensions/Validator/includes/ValidationErrors.php 2010-09-02
10:18:20 UTC (rev 72191)
@@ -0,0 +1 @@
+<?php
Property changes on: trunk/extensions/Validator/includes/ValidationErrors.php
___________________________________________________________________
Added: svn:eol-style
+ native
Copied: trunk/extensions/Validator/includes/ValidationFormats.php (from rev
72188, trunk/extensions/Validator/includes/Validator_Formats.php)
===================================================================
--- trunk/extensions/Validator/includes/ValidationFormats.php
(rev 0)
+++ trunk/extensions/Validator/includes/ValidationFormats.php 2010-09-02
10:18:20 UTC (rev 72191)
@@ -0,0 +1,123 @@
+<?php
+
+/**
+ * Class holding variouse static methods for the appliance of output formats.
+ *
+ * @file ValidationFormats.php
+ * @ingroup Validator
+ *
+ * @author Jeroen De Dauw
+ */
+final class ValidationFormats {
+
+ /**
+ * Ensures the value is an array.
+ *
+ * @param $value
+ * @param string name The name of the parameter.
+ * @param array $parameters Array containing data about the so far
handled parameters.
+ */
+ public static function format_array( &$value, $name, array $parameters
) {
+ if ( ! is_array( $value ) ) $value = array( $value );
+ }
+
+ /**
+ * Returns an array containing only the specified values.
+ *
+ * @param $value
+ * @param string name The name of the parameter.
+ * @param array $parameters Array containing data about the so far
handled parameters.
+ */
+ public static function format_filtered_array( &$value, $name, array
$parameters ) {
+ // TODO: It's possible the way the allowed values are passed
here is quite inneficient...
+ $params = func_get_args();
+ array_shift( $params ); // Ommit the value
+
+ self::format_array( $value, $name, $parameters );
+ $filtered = array();
+ foreach ( $value as $item ) if ( in_array( $item, $params ) )
$filtered[] = $item;
+
+ return $filtered;
+ }
+
+ /**
+ * Changes the value to list notation, by separating items with a
delimiter,
+ * and/or adding wrappers before and after the items. Intended for
lists, but
+ * will also work for single values.
+ *
+ * @param $value
+ * @param string name The name of the parameter.
+ * @param array $parameters Array containing data about the so far
handled parameters.
+ * @param $delimiter
+ * @param $wrapper
+ */
+ public static function format_list( &$value, $name, array $parameters,
$delimiter = ',', $wrapper = '' ) {
+ self::format_array( $value, $name, $parameters );
+ $value = $wrapper . implode( $wrapper . $delimiter . $wrapper,
$value ) . $wrapper;
+ }
+
+ /**
+ * Changes every value into a boolean.
+ *
+ * TODO: work with a list of true-values.
+ *
+ * @param $value
+ * @param string name The name of the parameter.
+ * @param array $parameters Array containing data about the so far
handled parameters.
+ */
+ public static function format_boolean( &$value, $name, array
$parameters ) {
+ if ( is_array( $value ) ) {
+ $boolArray = array();
+ foreach ( $value as $item ) $boolArray[] = in_array(
$item, array( 'yes', 'on' ) );
+ $value = $boolArray;
+ }
+ else {
+ $value = in_array( $value, array( 'yes', 'on' ) );
+ }
+ }
+
+ /**
+ * Changes every value into a boolean, represented by a 'false' or
'true' string.
+ *
+ * @param $value
+ * @param string name The name of the parameter.
+ * @param array $parameters Array containing data about the so far
handled parameters.
+ */
+ public static function format_boolean_string( &$value, $name, array
$parameters ) {
+ self::format_boolean( $value, $name, $parameters );
+ if ( is_array( $value ) ) {
+ $boolArray = array();
+ foreach ( $value as $item ) $boolArray[] = $item ?
'true' : 'false';
+ $value = $boolArray;
+ }
+ else {
+ $value = $value ? 'true' : 'false';
+ }
+ }
+
+ /**
+ * Changes lists into strings, by enumerating the items using
$wgLang->listToText.
+ *
+ * @param $value
+ * @param string name The name of the parameter.
+ * @param array $parameters Array containing data about the so far
handled parameters.
+ */
+ public static function format_string( &$value, $name, array $parameters
) {
+ if ( is_array( $value ) ) {
+ global $wgLang;
+ $value = $wgLang->listToText( $value );
+ }
+ }
+
+ /**
+ * Removes duplicate items from lists.
+ *
+ * @param $value
+ * @param string name The name of the parameter.
+ * @param array $parameters Array containing data about the so far
handled parameters.
+ */
+ public static function format_unique_items( &$value, $name, array
$parameters ) {
+ if ( is_array( $value ) ) $value = array_unique( $value );
+ }
+
+}
\ No newline at end of file
Copied: trunk/extensions/Validator/includes/ValidationFunctions.php (from rev
72188, trunk/extensions/Validator/includes/Validator_Functions.php)
===================================================================
--- trunk/extensions/Validator/includes/ValidationFunctions.php
(rev 0)
+++ trunk/extensions/Validator/includes/ValidationFunctions.php 2010-09-02
10:18:20 UTC (rev 72191)
@@ -0,0 +1,148 @@
+<?php
+
+/**
+ * Class holding variouse static methods for the validation of parameters that
have to comply to cetrain criteria.
+ * Functions are called by Validator with the parameters $value, $arguments,
where $arguments is optional.
+ *
+ * @file ValidationFunctions.php
+ * @ingroup Validator
+ *
+ * @author Jeroen De Dauw
+ */
+final class ValidationFunctions {
+
+ /**
+ * Returns whether the provided value, which must be a number, is
within a certain range. Upper bound included.
+ *
+ * @param $value
+ * @param array $metaData
+ * @param mixed $lower
+ * @param mixed $upper
+ *
+ * @return boolean
+ */
+ public static function in_range( $value, $name, array $parameters,
$lower = false, $upper = false ) {
+ if ( ! is_numeric( $value ) ) return false;
+ $value = (int)$value;
+ if ( $lower !== false && $value < $lower ) return false;
+ if ( $upper !== false && $value > $upper ) return false;
+ return true;
+ }
+
+ /**
+ * Returns whether the string value is not empty. Not empty is defined
as having at least one character after trimming.
+ *
+ * @param $value
+ * @param array $metaData
+ *
+ * @return boolean
+ */
+ public static function not_empty( $value, $name, array $parameters ) {
+ return strlen( trim( $value ) ) > 0;
+ }
+
+ /**
+ * Returns whether the string value is not empty. Not empty is defined
as having at least one character after trimming.
+ *
+ * @param $value
+ * @param array $metaData
+ *
+ * @return boolean
+ */
+ public static function in_array( $value, $name, array $parameters ) {
+ // TODO: It's possible the way the allowed values are passed
here is quite inneficient...
+ $params = func_get_args();
+ array_shift( $params ); // Ommit the value
+ return in_array( $value, $params );
+ }
+
+ /**
+ * Returns whether a variable is an integer or an integer string. Uses
the native PHP function.
+ *
+ * @param $value
+ * @param array $metaData
+ *
+ * @return boolean
+ */
+ public static function is_integer( $value, $name, array $parameters ) {
+ return ctype_digit( (string)$value );
+ }
+
+ /**
+ * Returns whether the length of the value is within a certain range.
Upper bound included.
+ *
+ * @param string $value
+ * @param array $metaData
+ * @param mixed $lower
+ * @param mixed $upper
+ *
+ * @return boolean
+ */
+ public static function has_length( $value, $name, array $parameters,
$lower = false, $upper = false ) {
+ return self::in_range( strlen( $value ), $lower, $upper );
+ }
+
+ /**
+ * Returns whether the amount of items in the list is within a certain
range. Upper bound included.
+ *
+ * @param array $values
+ * @param array $metaData
+ * @param mixed $lower
+ * @param mixed $upper
+ *
+ * @return boolean
+ */
+ public static function has_item_count( array $values, $name, array
$parameters, $lower = false, $upper = false ) {
+ return self::in_range( count( $values ), $lower, $upper );
+ }
+
+ /**
+ * Returns whether the list of values does not have any duplicates.
+ *
+ * @param array $values
+ * @param array $metaData
+ *
+ * @return boolean
+ */
+ public static function has_unique_items( array $values, $name, array
$parameters ) {
+ return count( $values ) == count( array_unique( $values ) );
+ }
+
+ /**
+ * Returns the result of preg_match.
+ *
+ * @param string $value
+ * @param array $metaData
+ * @param string $pattern
+ *
+ * @return boolean
+ */
+ public static function regex( $value, $name, array $parameters,
$pattern ) {
+ return (bool)preg_match( $pattern, $value );
+ }
+
+ /**
+ * Wrapper for the native is_numeric function.
+ *
+ * @param $value
+ * @param array $metaData
+ *
+ * @return boolean
+ */
+ public static function is_numeric( $value, $name, array $parameters ) {
+ return is_numeric( $value );
+ }
+
+ /**
+ * Returns if the value is a floating point number.
+ * Does NOT check the type of the variable like the native is_float
function.
+ *
+ * @param $value
+ * @param array $metaData
+ *
+ * @return boolean
+ */
+ public static function is_float( $value, $name, array $parameters ) {
+ return preg_match( '/^\d+(\.\d+)?$/', $value );
+ }
+}
\ No newline at end of file
Copied: trunk/extensions/Validator/includes/ValidationManager.php (from rev
72188, trunk/extensions/Validator/includes/Validator_Manager.php)
===================================================================
--- trunk/extensions/Validator/includes/ValidationManager.php
(rev 0)
+++ trunk/extensions/Validator/includes/ValidationManager.php 2010-09-02
10:18:20 UTC (rev 72191)
@@ -0,0 +1,178 @@
+<?php
+
+/**
+ * Class for parameter handling.
+ *
+ * @file ValidationManager.php
+ * @ingroup Validator
+ *
+ * @author Jeroen De Dauw
+ *
+ * FIXME: missing required params should result in a no-go, no matter of the
error level, as they can/are not defaulted.
+ * TODO: make a distinction between fatal errors and regular errors by using 2
seperate error levels.
+ */
+final class ValidationManager {
+
+ /**
+ * @var Validator
+ */
+ private $validator;
+
+ /**
+ * Parses and validates the provided parameters, and corrects them
depending on the error level.
+ *
+ * @param array $rawParameters The raw parameters, as provided by the
user.
+ * @param array $parameterInfo Array containing the parameter
definitions, which are needed for validation and defaulting.
+ * @param array $defaultParams
+ *
+ * @return array or false The valid parameters or false when the output
should not be shown.
+ */
+ public function manageParameters( array $rawParameters, array
$parameterInfo, array $defaultParams = array() ) {
+ global $egValidatorErrorLevel;
+
+ $this->validator = new Validator();
+
+ $this->validator->parseAndSetParams( $rawParameters,
$parameterInfo, $defaultParams );
+ $this->validator->validateAndFormatParameters();
+
+ if ( $this->validator->hasErrors() && $egValidatorErrorLevel <
Validator_ERRORS_STRICT ) {
+ $this->validator->correctInvalidParams();
+ }
+
+ return !$this->validator->hasFatalError();
+ }
+
+ /**
+ * Validates the provided parameters, and corrects them depending on
the error level.
+ *
+ * @since 3.x
+ *
+ * @param $parameters Array
+ * @param $parameterInfo Array
+ */
+ public function manageParsedParameters( array $parameters, array
$parameterInfo ) {
+ global $egValidatorErrorLevel;
+
+ $this->validator = new Validator();
+
+ $this->validator->setParameters( $parameters, $parameterInfo );
+ $this->validator->validateAndFormatParameters();
+
+ if ( $this->validator->hasErrors() && $egValidatorErrorLevel <
Validator_ERRORS_STRICT ) {
+ $this->validator->correctInvalidParams();
+ }
+
+ return !$this->validator->hasFatalError();
+ }
+
+ /**
+ * Returns an array with the valid parameters.
+ *
+ * @since 3.x
+ *
+ * @param boolean $includeMetaData
+ *
+ * @return array
+ */
+ public function getParameters( $includeMetaData = true ) {
+ return $this->validator->getValidParams( $includeMetaData );
+ }
+
+ /**
+ * Returns a string containing an HTML error list, or an empty string
when there are no errors.
+ *
+ * @return string
+ */
+ public function getErrorList() {
+ global $wgLang, $egValidatorErrorLevel;
+
+ // This function has been deprecated in 1.16, but needed for
earlier versions.
+ // It's present in 1.16 as a stub, but lets check if it exists
in case it gets removed at some point.
+ if ( function_exists( 'wfLoadExtensionMessages' ) ) {
+ wfLoadExtensionMessages( 'Validator' );
+ }
+
+ if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW &&
$this->validator->hasErrors() ) {
+ $rawErrors = $this->validator->getErrors();
+
+ $errorList = '<b>' . wfMsgExt(
'validator_error_parameters', 'parsemag', count( $rawErrors ) ) . '</b><br
/><i>';
+
+ $errors = array();
+
+ foreach ( $rawErrors as $error ) {
+ $error['name'] = '<b>' . Sanitizer::escapeId(
$error['name'] ) . '</b>';
+
+ if ( $error['type'] == 'unknown' ) {
+ $errors[] = wfMsgExt(
'validator_error_unknown_argument', array( 'parsemag' ), $error['name'] );
+ }
+ elseif ( $error['type'] == 'missing' ) {
+ $errors[] = wfMsgExt(
'validator_error_required_missing', array( 'parsemag' ), $error['name'] );
+ }
+ elseif ( array_key_exists( 'list', $error ) &&
$error['list'] ) {
+ switch( $error['type'] ) {
+ case 'not_empty' :
+ $msg = wfMsgExt(
'validator_list_error_empty_argument', array( 'parsemag' ), $error['name'] );
+ break;
+ case 'in_range' :
+ $msg = wfMsgExt(
'validator_list_error_invalid_range', array( 'parsemag' ), $error['name'],
'<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
+ break;
+ case 'is_numeric' :
+ $msg = wfMsgExt(
'validator_list_error_must_be_number', array( 'parsemag' ), $error['name'] );
+ break;
+ case 'is_integer' :
+ $msg = wfMsgExt(
'validator_list_error_must_be_integer', array( 'parsemag' ), $error['name'] );
+ break;
+ case 'in_array' :
+ $itemsText =
$wgLang->listToText( $error['args'] );
+ $msg = wfMsgExt(
'validator_error_accepts_only', array( 'parsemag' ), $error['name'],
$itemsText, count( $error['args'] ) );
+ break;
+ case 'invalid' : default :
+ $msg = wfMsgExt(
'validator_list_error_invalid_argument', array( 'parsemag' ), $error['name'] );
+ break;
+ }
+
+ if ( array_key_exists( 'invalid-items',
$error ) ) {
+ $omitted = array();
+ foreach (
$error['invalid-items'] as $item ) $omitted[] = Sanitizer::escapeId( $item );
+ $msg .= ' ' . wfMsgExt(
'validator_list_omitted', array( 'parsemag' ),
+ $wgLang->listToText(
$omitted ), count( $omitted ) );
+ }
+
+ $errors[] = $msg;
+ }
+ else {
+ switch( $error['type'] ) {
+ case 'not_empty' :
+ $errors[] = wfMsgExt(
'validator_error_empty_argument', array( 'parsemag' ), $error['name'] );
+ break;
+ case 'in_range' :
+ $errors[] = wfMsgExt(
'validator_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' .
$error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
+ break;
+ case 'is_numeric' :
+ $errors[] = wfMsgExt(
'validator_error_must_be_number', array( 'parsemag' ), $error['name'] );
+ break;
+ case 'is_integer' :
+ $errors[] = wfMsgExt(
'validator_error_must_be_integer', array( 'parsemag' ), $error['name'] );
+ break;
+ case 'in_array' :
+ $itemsText =
$wgLang->listToText( $error['args'] );
+ $errors[] = wfMsgExt(
'validator_error_accepts_only', array( 'parsemag' ), $error['name'],
$itemsText, count( $error['args'] ) );
+ break;
+ case 'invalid' : default :
+ $errors[] = wfMsgExt(
'validator_error_invalid_argument', array( 'parsemag' ), '<b>' .
htmlspecialchars( $error['value'] ) . '</b>', $error['name'] );
+ break;
+ }
+ }
+ }
+
+ return $errorList . implode( $errors, '<br />' ) .
'</i><br />';
+ }
+ elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN &&
$this->validator->hasErrors() ) {
+ return '<b>' . wfMsgExt(
'validator_warning_parameters', array( 'parsemag' ), count(
$this->validator->getErrors() ) ) . '</b>';
+ }
+ else {
+ return '';
+ }
+ }
+
+}
\ No newline at end of file
Modified: trunk/extensions/Validator/includes/Validator.php
===================================================================
--- trunk/extensions/Validator/includes/Validator.php 2010-09-02 09:53:32 UTC
(rev 72190)
+++ trunk/extensions/Validator/includes/Validator.php 2010-09-02 10:18:20 UTC
(rev 72191)
@@ -1,25 +1,15 @@
<?php
/**
- * File holding the Validator class.
+ * Class for parameter validation.
*
+ * @since 0.1
+ *
* @file Validator.class.php
* @ingroup Validator
*
* @author Jeroen De Dauw
- */
-
-if ( !defined( 'MEDIAWIKI' ) ) {
- die( 'Not an entry point.' );
-}
-
-/**
- * Class for parameter validation.
*
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- *
* TODO: break on fatal errors, such as missing required parameters that are
dependencies
* TODO: correct invalid parameters in the main loop, as to have correct
dependency handling
* TODO: settings of defaults should happen as a default behaviour that can be
overiden by the output format,
@@ -67,35 +57,35 @@
* @var array Holder for the validation functions.
*/
private static $mValidationFunctions = array(
- 'in_array' => array( 'ValidatorFunctions', 'in_array' ),
- 'in_range' => array( 'ValidatorFunctions', 'in_range' ),
- 'is_numeric' => array( 'ValidatorFunctions', 'is_numeric' ),
- 'is_float' => array( 'ValidatorFunctions', 'is_float' ),
- 'is_integer' => array( 'ValidatorFunctions', 'is_integer' ),
- 'not_empty' => array( 'ValidatorFunctions', 'not_empty' ),
- 'has_length' => array( 'ValidatorFunctions', 'has_length' ),
- 'regex' => array( 'ValidatorFunctions', 'regex' ),
+ 'in_array' => array( 'ValidationFunctions', 'in_array' ),
+ 'in_range' => array( 'ValidationFunctions', 'in_range' ),
+ 'is_numeric' => array( 'ValidationFunctions', 'is_numeric' ),
+ 'is_float' => array( 'ValidationFunctions', 'is_float' ),
+ 'is_integer' => array( 'ValidationFunctions', 'is_integer' ),
+ 'not_empty' => array( 'ValidationFunctions', 'not_empty' ),
+ 'has_length' => array( 'ValidationFunctions', 'has_length' ),
+ 'regex' => array( 'ValidationFunctions', 'regex' ),
);
/**
* @var array Holder for the list validation functions.
*/
private static $mListValidationFunctions = array(
- 'item_count' => array( 'ValidatorFunctions', 'has_item_count' ),
- 'unique_items' => array( 'ValidatorFunctions',
'has_unique_items' ),
+ 'item_count' => array( 'ValidationFunctions', 'has_item_count'
),
+ 'unique_items' => array( 'ValidationFunctions',
'has_unique_items' ),
);
/**
* @var array Holder for the formatting functions.
*/
private static $mOutputFormats = array(
- 'array' => array( 'ValidatorFormats', 'format_array' ),
- 'list' => array( 'ValidatorFormats', 'format_list' ),
- 'boolean' => array( 'ValidatorFormats', 'format_boolean' ),
- 'boolstr' => array( 'ValidatorFormats', 'format_boolean_string'
),
- 'string' => array( 'ValidatorFormats', 'format_string' ),
- 'unique_items' => array( 'ValidatorFormats',
'format_unique_items' ),
- 'filtered_array' => array( 'ValidatorFormats',
'format_filtered_array' ),
+ 'array' => array( 'ValidationFormats', 'format_array' ),
+ 'list' => array( 'ValidationFormats', 'format_list' ),
+ 'boolean' => array( 'ValidationFormats', 'format_boolean' ),
+ 'boolstr' => array( 'ValidationFormats',
'format_boolean_string' ),
+ 'string' => array( 'ValidationFormats', 'format_string' ),
+ 'unique_items' => array( 'ValidationFormats',
'format_unique_items' ),
+ 'filtered_array' => array( 'ValidationFormats',
'format_filtered_array' ),
);
/**
Deleted: trunk/extensions/Validator/includes/Validator_Formats.php
===================================================================
--- trunk/extensions/Validator/includes/Validator_Formats.php 2010-09-02
09:53:32 UTC (rev 72190)
+++ trunk/extensions/Validator/includes/Validator_Formats.php 2010-09-02
10:18:20 UTC (rev 72191)
@@ -1,134 +0,0 @@
-<?php
-/**
- * File holding the ValidatorFormats class.
- *
- * @file ValidatorFormats.php
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- */
-
-if ( !defined( 'MEDIAWIKI' ) ) {
- die( 'Not an entry point.' );
-}
-
-/**
- * Class holding variouse static methods for the appliance of output formats.
- *
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- */
-final class ValidatorFormats {
-
- /**
- * Ensures the value is an array.
- *
- * @param $value
- * @param string name The name of the parameter.
- * @param array $parameters Array containing data about the so far
handled parameters.
- */
- public static function format_array( &$value, $name, array $parameters
) {
- if ( ! is_array( $value ) ) $value = array( $value );
- }
-
- /**
- * Returns an array containing only the specified values.
- *
- * @param $value
- * @param string name The name of the parameter.
- * @param array $parameters Array containing data about the so far
handled parameters.
- */
- public static function format_filtered_array( &$value, $name, array
$parameters ) {
- // TODO: It's possible the way the allowed values are passed
here is quite inneficient...
- $params = func_get_args();
- array_shift( $params ); // Ommit the value
-
- self::format_array( $value, $name, $parameters );
- $filtered = array();
- foreach ( $value as $item ) if ( in_array( $item, $params ) )
$filtered[] = $item;
-
- return $filtered;
- }
-
- /**
- * Changes the value to list notation, by separating items with a
delimiter,
- * and/or adding wrappers before and after the items. Intended for
lists, but
- * will also work for single values.
- *
- * @param $value
- * @param string name The name of the parameter.
- * @param array $parameters Array containing data about the so far
handled parameters.
- * @param $delimiter
- * @param $wrapper
- */
- public static function format_list( &$value, $name, array $parameters,
$delimiter = ',', $wrapper = '' ) {
- self::format_array( $value, $name, $parameters );
- $value = $wrapper . implode( $wrapper . $delimiter . $wrapper,
$value ) . $wrapper;
- }
-
- /**
- * Changes every value into a boolean.
- *
- * TODO: work with a list of true-values.
- *
- * @param $value
- * @param string name The name of the parameter.
- * @param array $parameters Array containing data about the so far
handled parameters.
- */
- public static function format_boolean( &$value, $name, array
$parameters ) {
- if ( is_array( $value ) ) {
- $boolArray = array();
- foreach ( $value as $item ) $boolArray[] = in_array(
$item, array( 'yes', 'on' ) );
- $value = $boolArray;
- }
- else {
- $value = in_array( $value, array( 'yes', 'on' ) );
- }
- }
-
- /**
- * Changes every value into a boolean, represented by a 'false' or
'true' string.
- *
- * @param $value
- * @param string name The name of the parameter.
- * @param array $parameters Array containing data about the so far
handled parameters.
- */
- public static function format_boolean_string( &$value, $name, array
$parameters ) {
- self::format_boolean( $value, $name, $parameters );
- if ( is_array( $value ) ) {
- $boolArray = array();
- foreach ( $value as $item ) $boolArray[] = $item ?
'true' : 'false';
- $value = $boolArray;
- }
- else {
- $value = $value ? 'true' : 'false';
- }
- }
-
- /**
- * Changes lists into strings, by enumerating the items using
$wgLang->listToText.
- *
- * @param $value
- * @param string name The name of the parameter.
- * @param array $parameters Array containing data about the so far
handled parameters.
- */
- public static function format_string( &$value, $name, array $parameters
) {
- if ( is_array( $value ) ) {
- global $wgLang;
- $value = $wgLang->listToText( $value );
- }
- }
-
- /**
- * Removes duplicate items from lists.
- *
- * @param $value
- * @param string name The name of the parameter.
- * @param array $parameters Array containing data about the so far
handled parameters.
- */
- public static function format_unique_items( &$value, $name, array
$parameters ) {
- if ( is_array( $value ) ) $value = array_unique( $value );
- }
-
-}
\ No newline at end of file
Deleted: trunk/extensions/Validator/includes/Validator_Functions.php
===================================================================
--- trunk/extensions/Validator/includes/Validator_Functions.php 2010-09-02
09:53:32 UTC (rev 72190)
+++ trunk/extensions/Validator/includes/Validator_Functions.php 2010-09-02
10:18:20 UTC (rev 72191)
@@ -1,159 +0,0 @@
-<?php
-/**
- * File holding the ValidatorFunctions class.
- *
- * @file ValidatorFunctions.php
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- */
-
-if ( !defined( 'MEDIAWIKI' ) ) {
- die( 'Not an entry point.' );
-}
-
-/**
- * Class holding variouse static methods for the validation of parameters that
have to comply to cetrain criteria.
- * Functions are called by Validator with the parameters $value, $arguments,
where $arguments is optional.
- *
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- */
-final class ValidatorFunctions {
-
- /**
- * Returns whether the provided value, which must be a number, is
within a certain range. Upper bound included.
- *
- * @param $value
- * @param array $metaData
- * @param mixed $lower
- * @param mixed $upper
- *
- * @return boolean
- */
- public static function in_range( $value, $name, array $parameters,
$lower = false, $upper = false ) {
- if ( ! is_numeric( $value ) ) return false;
- $value = (int)$value;
- if ( $lower !== false && $value < $lower ) return false;
- if ( $upper !== false && $value > $upper ) return false;
- return true;
- }
-
- /**
- * Returns whether the string value is not empty. Not empty is defined
as having at least one character after trimming.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function not_empty( $value, $name, array $parameters ) {
- return strlen( trim( $value ) ) > 0;
- }
-
- /**
- * Returns whether the string value is not empty. Not empty is defined
as having at least one character after trimming.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function in_array( $value, $name, array $parameters ) {
- // TODO: It's possible the way the allowed values are passed
here is quite inneficient...
- $params = func_get_args();
- array_shift( $params ); // Ommit the value
- return in_array( $value, $params );
- }
-
- /**
- * Returns whether a variable is an integer or an integer string. Uses
the native PHP function.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function is_integer( $value, $name, array $parameters ) {
- return ctype_digit( (string)$value );
- }
-
- /**
- * Returns whether the length of the value is within a certain range.
Upper bound included.
- *
- * @param string $value
- * @param array $metaData
- * @param mixed $lower
- * @param mixed $upper
- *
- * @return boolean
- */
- public static function has_length( $value, $name, array $parameters,
$lower = false, $upper = false ) {
- return self::in_range( strlen( $value ), $lower, $upper );
- }
-
- /**
- * Returns whether the amount of items in the list is within a certain
range. Upper bound included.
- *
- * @param array $values
- * @param array $metaData
- * @param mixed $lower
- * @param mixed $upper
- *
- * @return boolean
- */
- public static function has_item_count( array $values, $name, array
$parameters, $lower = false, $upper = false ) {
- return self::in_range( count( $values ), $lower, $upper );
- }
-
- /**
- * Returns whether the list of values does not have any duplicates.
- *
- * @param array $values
- * @param array $metaData
- *
- * @return boolean
- */
- public static function has_unique_items( array $values, $name, array
$parameters ) {
- return count( $values ) == count( array_unique( $values ) );
- }
-
- /**
- * Returns the result of preg_match.
- *
- * @param string $value
- * @param array $metaData
- * @param string $pattern
- *
- * @return boolean
- */
- public static function regex( $value, $name, array $parameters,
$pattern ) {
- return (bool)preg_match( $pattern, $value );
- }
-
- /**
- * Wrapper for the native is_numeric function.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function is_numeric( $value, $name, array $parameters ) {
- return is_numeric( $value );
- }
-
- /**
- * Returns if the value is a floating point number.
- * Does NOT check the type of the variable like the native is_float
function.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function is_float( $value, $name, array $parameters ) {
- return preg_match( '/^\d+(\.\d+)?$/', $value );
- }
-}
\ No newline at end of file
Deleted: trunk/extensions/Validator/includes/Validator_Manager.php
===================================================================
--- trunk/extensions/Validator/includes/Validator_Manager.php 2010-09-02
09:53:32 UTC (rev 72190)
+++ trunk/extensions/Validator/includes/Validator_Manager.php 2010-09-02
10:18:20 UTC (rev 72191)
@@ -1,190 +0,0 @@
-<?php
-
-/**
- * File holding the ValidatorManager class.
- *
- * @file ValidatorManager.php
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- */
-
-if ( !defined( 'MEDIAWIKI' ) ) {
- die( 'Not an entry point.' );
-}
-
-/**
- * Class for parameter handling.
- *
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- *
- * FIXME: missing required params should result in a no-go, no matter of the
error level, as they can/are not defaulted.
- * TODO: make a distinction between fatal errors and regular errors by using 2
seperate error levels.
- */
-final class ValidatorManager {
-
- /**
- * @var Validator
- */
- private $validator;
-
- /**
- * Parses and validates the provided parameters, and corrects them
depending on the error level.
- *
- * @param array $rawParameters The raw parameters, as provided by the
user.
- * @param array $parameterInfo Array containing the parameter
definitions, which are needed for validation and defaulting.
- * @param array $defaultParams
- *
- * @return array or false The valid parameters or false when the output
should not be shown.
- */
- public function manageParameters( array $rawParameters, array
$parameterInfo, array $defaultParams = array() ) {
- global $egValidatorErrorLevel;
-
- $this->validator = new Validator();
-
- $this->validator->parseAndSetParams( $rawParameters,
$parameterInfo, $defaultParams );
- $this->validator->validateAndFormatParameters();
-
- if ( $this->validator->hasErrors() && $egValidatorErrorLevel <
Validator_ERRORS_STRICT ) {
- $this->validator->correctInvalidParams();
- }
-
- return !$this->validator->hasFatalError();
- }
-
- /**
- * Validates the provided parameters, and corrects them depending on
the error level.
- *
- * @since 3.x
- *
- * @param $parameters Array
- * @param $parameterInfo Array
- */
- public function manageParsedParameters( array $parameters, array
$parameterInfo ) {
- global $egValidatorErrorLevel;
-
- $this->validator = new Validator();
-
- $this->validator->setParameters( $parameters, $parameterInfo );
- $this->validator->validateAndFormatParameters();
-
- if ( $this->validator->hasErrors() && $egValidatorErrorLevel <
Validator_ERRORS_STRICT ) {
- $this->validator->correctInvalidParams();
- }
-
- return !$this->validator->hasFatalError();
- }
-
- /**
- * Returns an array with the valid parameters.
- *
- * @since 3.x
- *
- * @param boolean $includeMetaData
- *
- * @return array
- */
- public function getParameters( $includeMetaData = true ) {
- return $this->validator->getValidParams( $includeMetaData );
- }
-
- /**
- * Returns a string containing an HTML error list, or an empty string
when there are no errors.
- *
- * @return string
- */
- public function getErrorList() {
- global $wgLang, $egValidatorErrorLevel;
-
- // This function has been deprecated in 1.16, but needed for
earlier versions.
- // It's present in 1.16 as a stub, but lets check if it exists
in case it gets removed at some point.
- if ( function_exists( 'wfLoadExtensionMessages' ) ) {
- wfLoadExtensionMessages( 'Validator' );
- }
-
- if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW &&
$this->validator->hasErrors() ) {
- $rawErrors = $this->validator->getErrors();
-
- $errorList = '<b>' . wfMsgExt(
'validator_error_parameters', 'parsemag', count( $rawErrors ) ) . '</b><br
/><i>';
-
- $errors = array();
-
- foreach ( $rawErrors as $error ) {
- $error['name'] = '<b>' . Sanitizer::escapeId(
$error['name'] ) . '</b>';
-
- if ( $error['type'] == 'unknown' ) {
- $errors[] = wfMsgExt(
'validator_error_unknown_argument', array( 'parsemag' ), $error['name'] );
- }
- elseif ( $error['type'] == 'missing' ) {
- $errors[] = wfMsgExt(
'validator_error_required_missing', array( 'parsemag' ), $error['name'] );
- }
- elseif ( array_key_exists( 'list', $error ) &&
$error['list'] ) {
- switch( $error['type'] ) {
- case 'not_empty' :
- $msg = wfMsgExt(
'validator_list_error_empty_argument', array( 'parsemag' ), $error['name'] );
- break;
- case 'in_range' :
- $msg = wfMsgExt(
'validator_list_error_invalid_range', array( 'parsemag' ), $error['name'],
'<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
- break;
- case 'is_numeric' :
- $msg = wfMsgExt(
'validator_list_error_must_be_number', array( 'parsemag' ), $error['name'] );
- break;
- case 'is_integer' :
- $msg = wfMsgExt(
'validator_list_error_must_be_integer', array( 'parsemag' ), $error['name'] );
- break;
- case 'in_array' :
- $itemsText =
$wgLang->listToText( $error['args'] );
- $msg = wfMsgExt(
'validator_error_accepts_only', array( 'parsemag' ), $error['name'],
$itemsText, count( $error['args'] ) );
- break;
- case 'invalid' : default :
- $msg = wfMsgExt(
'validator_list_error_invalid_argument', array( 'parsemag' ), $error['name'] );
- break;
- }
-
- if ( array_key_exists( 'invalid-items',
$error ) ) {
- $omitted = array();
- foreach (
$error['invalid-items'] as $item ) $omitted[] = Sanitizer::escapeId( $item );
- $msg .= ' ' . wfMsgExt(
'validator_list_omitted', array( 'parsemag' ),
- $wgLang->listToText(
$omitted ), count( $omitted ) );
- }
-
- $errors[] = $msg;
- }
- else {
- switch( $error['type'] ) {
- case 'not_empty' :
- $errors[] = wfMsgExt(
'validator_error_empty_argument', array( 'parsemag' ), $error['name'] );
- break;
- case 'in_range' :
- $errors[] = wfMsgExt(
'validator_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' .
$error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
- break;
- case 'is_numeric' :
- $errors[] = wfMsgExt(
'validator_error_must_be_number', array( 'parsemag' ), $error['name'] );
- break;
- case 'is_integer' :
- $errors[] = wfMsgExt(
'validator_error_must_be_integer', array( 'parsemag' ), $error['name'] );
- break;
- case 'in_array' :
- $itemsText =
$wgLang->listToText( $error['args'] );
- $errors[] = wfMsgExt(
'validator_error_accepts_only', array( 'parsemag' ), $error['name'],
$itemsText, count( $error['args'] ) );
- break;
- case 'invalid' : default :
- $errors[] = wfMsgExt(
'validator_error_invalid_argument', array( 'parsemag' ), '<b>' .
htmlspecialchars( $error['value'] ) . '</b>', $error['name'] );
- break;
- }
- }
- }
-
- return $errorList . implode( $errors, '<br />' ) .
'</i><br />';
- }
- elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN &&
$this->validator->hasErrors() ) {
- return '<b>' . wfMsgExt(
'validator_warning_parameters', array( 'parsemag' ), count(
$this->validator->getErrors() ) ) . '</b>';
- }
- else {
- return '';
- }
- }
-
-}
\ No newline at end of file
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs