http://www.mediawiki.org/wiki/Special:Code/MediaWiki/72283

Revision: 72283
Author:   jeroendedauw
Date:     2010-09-03 13:48:19 +0000 (Fri, 03 Sep 2010)

Log Message:
-----------
Changes for 0.4 - added Parameter class with method to create a new instance 
from a Validator 3.x-style definition

Modified Paths:
--------------
    trunk/extensions/Validator/Validator.php
    trunk/extensions/Validator/includes/ValidationManager.php

Added Paths:
-----------
    trunk/extensions/Validator/includes/Parameter.php

Removed Paths:
-------------
    trunk/extensions/Validator/includes/ValidationErrors.php

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2010-09-03 13:00:46 UTC (rev 
72282)
+++ trunk/extensions/Validator/Validator.php    2010-09-03 13:48:19 UTC (rev 
72283)
@@ -53,12 +53,13 @@
 
 // Autoload the general classes.
 $incDir = dirname( __FILE__ ) . '/includes/';
+$wgAutoloadClasses['Parameter']                        = $incDir . 
'Parameter.php';
+$wgAutoloadClasses['ParserHook']                       = $incDir . 
'ParserHook.php';
 $wgAutoloadClasses['Validator']                        = $incDir . 
'Validator.php';
-$wgAutoloadClasses['ParserHook']                       = $incDir . 
'ParserHook.php';
+$wgAutoloadClasses['TopologicalSort']          = $incDir . 
'TopologicalSort.php';
+$wgAutoloadClasses['ValidationFormats']        = $incDir . 
'ValidationFormats.php';
 $wgAutoloadClasses['ValidationFunctions']      = $incDir . 
'ValidationFunctions.php';
-$wgAutoloadClasses['ValidationFormats']        = $incDir . 
'ValidationFormats.php';
-$wgAutoloadClasses['ValidationManager']        = $incDir . 
'ValidationManager.php';
-$wgAutoloadClasses['TopologicalSort']          = $incDir . 
'TopologicalSort.php';
+$wgAutoloadClasses['ValidationManager']        = $incDir . 
'ValidationManager.php'; // TODO: remove
 $wgAutoloadClasses['ValidatorError']           = $incDir . 
'Validator_Error.php';
 $wgAutoloadClasses['ValidatorErrorHandler']    = $incDir . 
'Validator_ErrorHandler.php';
 $wgAutoloadClasses['ValidatorListErrors']      = $incDir . 
'parserHooks/Validator_ListErrors.php';

Added: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php                           
(rev 0)
+++ trunk/extensions/Validator/includes/Parameter.php   2010-09-03 13:48:19 UTC 
(rev 72283)
@@ -0,0 +1,120 @@
+<?php
+
+/**
+ * Parameter definition class.
+ * 
+ * @since 0.4
+ * 
+ * @file Parameter.php
+ * @ingroup Validator
+ * 
+ * @author Jeroen De Dauw
+ */
+class Parameter {
+       
+       const TYPE_STRING = 'string';
+       
+       protected $name;
+       
+       protected $type;
+       
+       protected $default;
+       
+       protected $aliases;
+       
+       protected $criteria;
+       
+       protected $outputTypes;
+       
+       /**
+        * Returns a new instance of Parameter by converting a Validator 
3.x-style parameter array definition.
+        * 
+        * @since 0.4
+        * 
+        * @param string $name
+        * @param array $definition
+        * 
+        * @return Parameter
+        */
+       public static function newFromArray( $name, array $definition ) {
+               if ( array_key_exists( 'type', $definition ) ) {
+                       if ( is_array( $definition['type'] ) ) {
+                               if ( count( $definition['type'] ) > 1 ) {
+                                       if ( count( $definition['type'] ) > 2 ) 
{
+                                               $type = array( 
$definition['type'][0], $definition['type'][2] );
+                                       }
+                                       else {
+                                               $type = array( 
$definition['type'][0] );
+                                       }
+                               }
+                               else {
+                                       $type = $definition['type'][0];
+                               }
+                       }
+                       else {
+                               $type = $definition['type'];
+                       }
+               }
+               else {
+                       $type = 'string';
+               }
+               
+               if ( array_key_exists( 'required', $definition ) && 
$definition['required'] ) {
+                       $default = null;
+               }
+               else {
+                       $default = array_key_exists( 'default', $definition ) ? 
$definition['default'] : '';
+               }
+               
+               $parameter = new Parameter(
+                       $name,
+                       $type,
+                       $default,
+                       array_key_exists( 'aliases', $definition ) ? 
$definition['aliases'] : array(),
+                       array_key_exists( 'criteria', $definition ) ? 
$definition['criteria'] : array()
+               );
+               
+               if ( array_key_exists( 'output-types', $definition ) ) {
+                       $types = array();
+                       
+                       for ( $i = 0, $c = count( $definition['output-types'] 
); $i < $c; $i++ ) {
+                               if ( ! is_array( 
$definition['output-types'][$i] ) ) {
+                                       $definition['output-types'][$i] = 
array( $definition['output-types'][$i] );
+                               }
+                               
+                               $types[$name] = $definition['output-types'][$i];
+                       }
+                       
+                       $parameter->setOutputTypes( $types );
+               }
+               elseif ( array_key_exists( 'output-type', $definition ) ) {
+                       if ( ! is_array( $definition['output-type'] ) ) {
+                               $definition['output-type'] = array( 
$definition['output-type'] );
+                       }
+                       
+                       $parameter->setOutputTypes( array( $name => 
$definition['output-type'] ) );
+               }               
+               
+               return $parameter;
+       }
+       
+       /**
+        * Constructor.
+        * 
+        * @since 0.4
+        * 
+        * @param string $name
+        * @param string $type
+        * @param mixed $default Use null for no default (which makes the 
parameter required)
+        * @param array $aliases
+        * @param array $criteria
+        */
+       public function __construct( $name, $type = Parameter::TYPE_STRING, 
$default = null, array $aliases = array(), array $criteria = array() ) {
+               
+       }
+       
+       public function setOutputTypes( $outputTypes ) {
+               $this->outputTypes = outputTypes;
+       }
+       
+}
\ No newline at end of file


Property changes on: trunk/extensions/Validator/includes/Parameter.php
___________________________________________________________________
Added: svn:eol-style
   + native

Deleted: trunk/extensions/Validator/includes/ValidationErrors.php
===================================================================
--- trunk/extensions/Validator/includes/ValidationErrors.php    2010-09-03 
13:00:46 UTC (rev 72282)
+++ trunk/extensions/Validator/includes/ValidationErrors.php    2010-09-03 
13:48:19 UTC (rev 72283)
@@ -1 +0,0 @@
-<?php

Modified: trunk/extensions/Validator/includes/ValidationManager.php
===================================================================
--- trunk/extensions/Validator/includes/ValidationManager.php   2010-09-03 
13:00:46 UTC (rev 72282)
+++ trunk/extensions/Validator/includes/ValidationManager.php   2010-09-03 
13:48:19 UTC (rev 72283)
@@ -80,103 +80,4 @@
                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

Reply via email to