[MediaWiki-commits] [Gerrit] mediawiki...WikibaseQualityConstraints[master]: Add scope support to DelegatingConstraintChecker
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/402884 ) Change subject: Add scope support to DelegatingConstraintChecker .. Add scope support to DelegatingConstraintChecker The “constraint scope” parameter’s validity is checked along with other common parameters like “exception to constraint” or “constraint status”, and when it comes to actually checking a constraint, DelegatingConstraintChecker also takes care of assigning the default scope and testing whether a constraint can even be checked on a certain context. Bug: T183542 Change-Id: Ie9d7c1c9e7a57d0e03ca0cb0f96d47d0f1869921 --- M src/ConstraintCheck/DelegatingConstraintChecker.php M tests/phpunit/Api/CheckConstraintsTest.php M tests/phpunit/DelegatingConstraintCheckerTest.php 3 files changed, 233 insertions(+), 0 deletions(-) Approvals: Jonas Kress (WMDE): Looks good to me, approved jenkins-bot: Verified diff --git a/src/ConstraintCheck/DelegatingConstraintChecker.php b/src/ConstraintCheck/DelegatingConstraintChecker.php index cb71871..9cd62cb 100644 --- a/src/ConstraintCheck/DelegatingConstraintChecker.php +++ b/src/ConstraintCheck/DelegatingConstraintChecker.php @@ -190,6 +190,23 @@ return []; } + private function getAllowedContextTypes( Constraint $constraint ) { + if ( !array_key_exists( $constraint->getConstraintTypeItemId(), $this->checkerMap ) ) { + return [ + Context::TYPE_STATEMENT, + Context::TYPE_QUALIFIER, + Context::TYPE_REFERENCE, + ]; + } + + return array_keys( array_filter( + $this->checkerMap[$constraint->getConstraintTypeItemId()]->getSupportedContextTypes(), + function ( $resultStatus ) { + return $resultStatus !== CheckResult::STATUS_NOT_IN_SCOPE; + } + ) ); + } + /** * Like ConstraintChecker::checkConstraintParameters, * but for meta-parameters common to all checkers. @@ -214,6 +231,15 @@ } try { $this->constraintParameterParser->parseConstraintStatusParameter( $constraintParameters ); + } catch ( ConstraintParameterException $e ) { + $problems[] = $e; + } + try { + $this->constraintParameterParser->parseConstraintScopeParameter( + $constraintParameters, + $constraint->getConstraintTypeItemId(), + $this->getAllowedContextTypes( $constraint ) + ); } catch ( ConstraintParameterException $e ) { $problems[] = $e; } @@ -505,6 +531,12 @@ private function getCheckResultFor( Context $context, Constraint $constraint ) { if ( array_key_exists( $constraint->getConstraintTypeItemId(), $this->checkerMap ) ) { $checker = $this->checkerMap[$constraint->getConstraintTypeItemId()]; + $result = $this->handleScope( $checker, $context, $constraint ); + + if ( $result !== null ) { + $this->addMetadata( $result ); + return $result; + } $startTime = microtime( true ); try { @@ -558,6 +590,31 @@ } } + private function handleScope( + ConstraintChecker $checker, + Context $context, + Constraint $constraint + ) { + try { + $checkedContextTypes = $this->constraintParameterParser->parseConstraintScopeParameter( + $constraint->getConstraintParameters(), + $constraint->getConstraintTypeItemId() + ); + } catch ( ConstraintParameterException $e ) { + return new CheckResult( $context, $constraint, [], CheckResult::STATUS_BAD_PARAMETERS, $e->getMessage() ); + } + if ( $checkedContextTypes === null ) { + $checkedContextTypes = $checker->getDefaultContextTypes(); + } + if ( !in_array( $context->getType(), $checkedContextTypes ) ) { + return new CheckResult( $context, $constraint, [], CheckResult::STATUS_NOT_IN_SCOPE, null ); + } + if ( $checker->getSupportedContextTypes()[$context->getType()] === CheckResult::STATUS_TODO ) { + return new CheckResult( $context, $constraint, [], CheckR
[MediaWiki-commits] [Gerrit] mediawiki...WikibaseQualityConstraints[master]: Add scope support to DelegatingConstraintChecker
Lucas Werkmeister (WMDE) has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/402884 ) Change subject: Add scope support to DelegatingConstraintChecker .. Add scope support to DelegatingConstraintChecker The “constraint scope” parameter’s validity is checked along with other common parameters like “exception to constraint” or “constraint status”, and when it comes to actually checking a constraint, DelegatingConstraintChecker also takes care of assigning the default scope and testing whether a constraint can even be checked on a certain context. Bug: T183542 Change-Id: Ie9d7c1c9e7a57d0e03ca0cb0f96d47d0f1869921 --- M src/ConstraintCheck/DelegatingConstraintChecker.php M tests/phpunit/Api/CheckConstraintsTest.php M tests/phpunit/DelegatingConstraintCheckerTest.php 3 files changed, 237 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints refs/changes/84/402884/1 diff --git a/src/ConstraintCheck/DelegatingConstraintChecker.php b/src/ConstraintCheck/DelegatingConstraintChecker.php index cb71871..eb88ab5 100644 --- a/src/ConstraintCheck/DelegatingConstraintChecker.php +++ b/src/ConstraintCheck/DelegatingConstraintChecker.php @@ -190,6 +190,42 @@ return []; } + private function getAllowedContextTypes( Constraint $constraint ) { + if ( !array_key_exists( $constraint->getConstraintTypeItemId(), $this->checkerMap ) ) { + return [ + Context::TYPE_STATEMENT, + Context::TYPE_QUALIFIER, + Context::TYPE_REFERENCE, + ]; + } + + return array_keys( array_filter( + $this->checkerMap[$constraint->getConstraintTypeItemId()]->getSupportedContextTypes(), + function ( array $supportedContextTypes ) { + list( $resultStatus, $default ) = $supportedContextTypes; + return $resultStatus !== CheckResult::STATUS_NOT_IN_SCOPE; + } + ) ); + } + + private function getDefaultContextTypes( Constraint $constraint ) { + if ( !array_key_exists( $constraint->getConstraintTypeItemId(), $this->checkerMap ) ) { + return [ + Context::TYPE_STATEMENT, + Context::TYPE_QUALIFIER, + Context::TYPE_REFERENCE, + ]; + } + + return array_keys( array_filter( + $this->checkerMap[$constraint->getConstraintTypeItemId()]->getSupportedContextTypes(), + function ( array $supportedContextTypes ) { + list( $resultStatus, $default ) = $supportedContextTypes; + return $default; + } + ) ); + } + /** * Like ConstraintChecker::checkConstraintParameters, * but for meta-parameters common to all checkers. @@ -214,6 +250,15 @@ } try { $this->constraintParameterParser->parseConstraintStatusParameter( $constraintParameters ); + } catch ( ConstraintParameterException $e ) { + $problems[] = $e; + } + try { + $this->constraintParameterParser->parseConstraintScopeParameter( + $constraintParameters, + $constraint->getConstraintTypeItemId(), + $this->getAllowedContextTypes( $constraint ) + ); } catch ( ConstraintParameterException $e ) { $problems[] = $e; } @@ -505,6 +550,30 @@ private function getCheckResultFor( Context $context, Constraint $constraint ) { if ( array_key_exists( $constraint->getConstraintTypeItemId(), $this->checkerMap ) ) { $checker = $this->checkerMap[$constraint->getConstraintTypeItemId()]; + $result = null; + + try { + $checkedContextTypes = $this->constraintParameterParser->parseConstraintScopeParameter( + $constraint->getConstraintParameters(), + $constraint->getConstraintTypeItemId() + ); + } catch ( ConstraintParameterException $e ) { + $result = new CheckResult( $context, $constraint, [], CheckResult::STATUS_BAD_PARAMETERS, $e->getMessage() ); + } + if ( $checkedCo