Thiemo Mättig (WMDE) has uploaded a new change for review. https://gerrit.wikimedia.org/r/178922
Change subject: Avoid RegexValidator if possible ...................................................................... Avoid RegexValidator if possible ... and a bit of refactoring, mainly: * Removing optional parameters. * MembershipValidator uses strict comparison now. * Commons file name check is a little bit more strict. Change-Id: I85e908dfd8ae36a6c6bead024005fadb47837b21 --- M lib/includes/Validators/MembershipValidator.php M lib/includes/WikibaseDataTypeBuilders.php 2 files changed, 52 insertions(+), 51 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/22/178922/1 diff --git a/lib/includes/Validators/MembershipValidator.php b/lib/includes/Validators/MembershipValidator.php index 042d570..aa07154 100644 --- a/lib/includes/Validators/MembershipValidator.php +++ b/lib/includes/Validators/MembershipValidator.php @@ -33,17 +33,17 @@ /** * @param array $allowed The allowed values * @param string $errorCode Code to use in Errors; should indicate what kind of value would have been allowed. - * @param callable|string|null $normalizer An optional function to normalize the value before + * @param callable|null $normalizer An optional function to normalize the value before * comparing it to the list of allowed values, e.g. 'strtolower'. * * @throws InvalidArgumentException */ - public function __construct( array $allowed, $errorCode = 'not-allowed', $normalizer = null ) { + public function __construct( array $allowed, $errorCode = 'not-allowed', callable $normalizer = null ) { if ( !is_string( $errorCode ) ) { throw new InvalidArgumentException( 'Error code must be a string' ); } - if ( !is_null( $normalizer ) && !is_callable( $normalizer ) ) { + if ( !is_callable( $normalizer ) && !is_null( $normalizer ) ) { throw new InvalidArgumentException( 'Normalizer must be callable (or null)' ); } @@ -53,7 +53,7 @@ } /** - * @see ValueValidator::validate() + * @see ValueValidator::validate * * @param string $value The value to validate * @@ -64,9 +64,14 @@ $value = call_user_func( $this->normalizer, $value ); } - if ( !in_array( $value, $this->allowed ) ) { + if ( !in_array( $value, $this->allowed, true ) ) { return Result::newError( array( - Error::newError( 'not a legal value: ' . $value, null, $this->errorCode, array( $value ) ) + Error::newError( + 'Not a legal value: ' . $value, + null, + $this->errorCode, + array( $value ) + ) ) ); } @@ -74,7 +79,7 @@ } /** - * @see ValueValidator::setOptions() + * @see ValueValidator::setOptions * * @param array $options */ diff --git a/lib/includes/WikibaseDataTypeBuilders.php b/lib/includes/WikibaseDataTypeBuilders.php index 1bdfade..1aa3a29 100644 --- a/lib/includes/WikibaseDataTypeBuilders.php +++ b/lib/includes/WikibaseDataTypeBuilders.php @@ -169,13 +169,14 @@ $validators = $this->getCommonStringValidators( 240 ); $validators[] = new RegexValidator( '@[#/:\\\\]@u', true ); // no nasty chars - $validators[] = new RegexValidator( '@\..+@u', false ); // must contain a file extension + // Must contain a non-empty file name and a non-empty, character-only file extension. + $validators[] = new RegexValidator( '/.\.\w+$/u' ); //TODO: add a validator that checks the rules that MediaWiki imposes on filenames for uploads. // $wgLegalTitleChars and $wgIllegalFileChars define this, but we need these for the *target* wiki. //TODO: add a validator that uses a foreign DB query to check whether the file actually exists on commons. - $topValidator = new DataValueValidator( //Note: validate the DataValue's native value. - new CompositeValidator( $validators, true ) //Note: each validator is fatal + $topValidator = new DataValueValidator( + new CompositeValidator( $validators ) //Note: each validator is fatal ); return new DataType( $id, 'string', array( new TypeValidator( 'DataValues\DataValue' ), $topValidator ) ); @@ -189,8 +190,8 @@ public function buildStringType( $id ) { $validators = $this->getCommonStringValidators(); - $topValidator = new DataValueValidator( //Note: validate the DataValue's native value. - new CompositeValidator( $validators, true ) //Note: each validator is fatal + $topValidator = new DataValueValidator( + new CompositeValidator( $validators ) //Note: each validator is fatal ); return new DataType( $id, 'string', array( new TypeValidator( 'DataValues\DataValue' ), $topValidator ) ); @@ -204,10 +205,7 @@ public function buildMonolingualTextType( $id ) { $textValidator = new DataFieldValidator( 'text', - new CompositeValidator( - $this->getCommonStringValidators(), - true //Note: each validator is fatal - ) + new CompositeValidator( $this->getCommonStringValidators() ) //Note: each validator is fatal ); $languageValidator = new DataFieldValidator( @@ -216,8 +214,7 @@ ); $topValidator = new DataValueValidator( new CompositeValidator( - array( $textValidator, $languageValidator ), - true + array( $textValidator, $languageValidator ) ) ); return new DataType( $id, 'monolingualtext', array( new TypeValidator( 'DataValues\DataValue' ), $topValidator ) ); @@ -238,8 +235,9 @@ $calendarIdValidators[] = $urlValidator = $this->buildUrlValidator( array( 'http', 'https' ), 255 ); //TODO: enforce well known calendar models from config - $validators[] = new DataFieldValidator( 'calendarmodel', // Note: validate the 'calendarmodel' field - new CompositeValidator( $calendarIdValidators, true ) //Note: each validator is fatal + $validators[] = new DataFieldValidator( + 'calendarmodel', + new CompositeValidator( $calendarIdValidators ) //Note: each validator is fatal ); // time string field @@ -256,21 +254,22 @@ $timeStringValidators[] = new RegexValidator( $isoDataPattern ); - $validators[] = new DataFieldValidator( 'time', // Note: validate the 'time' field - new CompositeValidator( $timeStringValidators, true ) //Note: each validator is fatal + $validators[] = new DataFieldValidator( + 'time', + new CompositeValidator( $timeStringValidators ) //Note: each validator is fatal ); $precisionValidators = array(); $precisionValidators[] = new TypeValidator( 'integer' ); $precisionValidators[] = new NumberRangeValidator( TimeValue::PRECISION_Ga, $maxPrecision ); - $validators[] = new DataFieldValidator( 'precision', // Note: validate the 'precision' field - new CompositeValidator( $precisionValidators, true ) //Note: each validator is fatal + $validators[] = new DataFieldValidator( + 'precision', + new CompositeValidator( $precisionValidators ) //Note: each validator is fatal ); - // top validator - $topValidator = new DataValueValidator( //Note: validate the DataValue's native value. - new CompositeValidator( $validators, true ) //Note: each validator is fatal + $topValidator = new DataValueValidator( + new CompositeValidator( $validators ) //Note: each validator is fatal ); return new DataType( $id, 'time', array( new TypeValidator( 'DataValues\DataValue' ), $topValidator ) ); @@ -293,17 +292,18 @@ $precisionValidators = array(); $precisionValidators[] = new NumberValidator(); - $validators[] = new DataFieldValidator( 'precision', - new CompositeValidator( $precisionValidators, true ) + $validators[] = new DataFieldValidator( + 'precision', + new CompositeValidator( $precisionValidators ) ); - $validators[] = new DataFieldValidator( 'globe', // Note: validate the 'globe' field - new CompositeValidator( $globeIdValidators, true ) //Note: each validator is fatal + $validators[] = new DataFieldValidator( + 'globe', + new CompositeValidator( $globeIdValidators ) //Note: each validator is fatal ); - // top validator - $topValidator = new DataValueValidator( //Note: validate the DataValue's native value. - new CompositeValidator( $validators, true ) //Note: each validator is fatal + $topValidator = new DataValueValidator( + new CompositeValidator( $validators ) //Note: each validator is fatal ); return new DataType( $id, 'globecoordinate', array( new TypeValidator( 'DataValues\DataValue' ), $topValidator ) ); @@ -325,7 +325,7 @@ $urlSchemeValidators = $urlValidators->getValidators( $urlSchemes ); $validators[] = new UrlValidator( $urlSchemeValidators ); - return new CompositeValidator( $validators, true ); //Note: each validator is fatal + return new CompositeValidator( $validators ); //Note: each validator is fatal } /** @@ -336,7 +336,7 @@ public function buildUrlType( $id ) { $urlValidator = $this->buildUrlValidator( $this->urlSchemes ); - $topValidator = new DataValueValidator( //Note: validate the DataValue's native value. + $topValidator = new DataValueValidator( $urlValidator ); @@ -355,23 +355,19 @@ // the 'amount' field is already validated by QuantityValue's constructor // the 'digits' field is already validated by QuantityValue's constructor - // only allow the '1' unit for now: - $unitValidators = array(); - $unitValidators[] = new AlternativeValidator( array ( - // NOTE: "1" is always considered legal for historical reasons, - // since we use it to represent "unitless" quantities. We could also use - // http://qudt.org/vocab/unit#Unitless or https://www.wikidata.org/entity/Q199 - new RegexValidator( '/^1$/' ), - $this->buildUrlValidator( array( 'http', 'https' ), 255 ), - ) ); - - $validators[] = new DataFieldValidator( 'unit', // Note: validate the 'unit' field - new CompositeValidator( $unitValidators, true ) //Note: each validator is fatal + $validators[] = new DataFieldValidator( + 'unit', + new AlternativeValidator( array ( + // NOTE: "1" is always considered legal for historical reasons, + // since we use it to represent "unitless" quantities. We could also use + // http://qudt.org/vocab/unit#Unitless or https://www.wikidata.org/entity/Q199 + new MembershipValidator( array( '1' ) ), + $this->buildUrlValidator( array( 'http', 'https' ), 255 ), + ) ) ); - // top validator - $topValidator = new DataValueValidator( //Note: validate the DataValue's native value. - new CompositeValidator( $validators, true ) //Note: each validator is fatal + $topValidator = new DataValueValidator( + new CompositeValidator( $validators ) //Note: each validator is fatal ); return new DataType( $id, 'quantity', array( new TypeValidator( 'DataValues\QuantityValue' ), $topValidator ) ); -- To view, visit https://gerrit.wikimedia.org/r/178922 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I85e908dfd8ae36a6c6bead024005fadb47837b21 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits