Lucas Werkmeister (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/392061 )

Change subject: Cache constraint exceptions
......................................................................

Cache constraint exceptions

Instead of parsing the constraint parameters with every check to extract
the exceptions, do it only once and cache the exceptions.

Bug: T180799
Change-Id: I50bf66f34a9499682009c6e8edd02c3cda293c41
---
M includes/ConstraintCheck/ConstraintCheckPlan.php
M tests/phpunit/ConstraintCheckPlanTest.php
2 files changed, 34 insertions(+), 13 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
 refs/changes/61/392061/1

diff --git a/includes/ConstraintCheck/ConstraintCheckPlan.php 
b/includes/ConstraintCheck/ConstraintCheckPlan.php
index 75bc0a9..c9f6a2d 100644
--- a/includes/ConstraintCheck/ConstraintCheckPlan.php
+++ b/includes/ConstraintCheck/ConstraintCheckPlan.php
@@ -205,6 +205,7 @@
         */
        public function run( callable $getCheckResult, callable $defaultResults 
= null ) {
                $result = [];
+               $exceptionsByConstraintId = [];
 
                if ( $defaultResults !== null ) {
                        foreach ( $this->contexts as $context ) {
@@ -215,14 +216,24 @@
                foreach ( $this->plan as $constraintCheck ) {
                        $context = $constraintCheck['context'];
                        $constraint = $constraintCheck['constraint'];
+                       $constraintId = $constraint->getConstraintId();
 
-                       // TODO cache exceptions
-                       $parameters = $constraint->getConstraintParameters();
-                       try {
-                               $exceptions = 
$this->constraintParameterParser->parseExceptionParameter( $parameters );
-                       } catch ( ConstraintParameterException $e ) {
-                               $result[] = new CheckResult( $context, 
$constraint, [], CheckResult::STATUS_BAD_PARAMETERS, $e->getMessage() );
-                               continue;
+                       if ( array_key_exists( $constraintId, 
$exceptionsByConstraintId ) ) {
+                               $exceptions = 
$exceptionsByConstraintId[$constraintId];
+                               if ( $exceptions instanceof 
ConstraintParameterException ) {
+                                       $result[] = new CheckResult( $context, 
$constraint, [], CheckResult::STATUS_BAD_PARAMETERS, $exceptions->getMessage() 
);
+                                       continue;
+                               }
+                       } else {
+                               $parameters = 
$constraint->getConstraintParameters();
+                               try {
+                                       $exceptions = 
$this->constraintParameterParser->parseExceptionParameter( $parameters );
+                                       
$exceptionsByConstraintId[$constraintId] = $exceptions;
+                               } catch ( ConstraintParameterException $e ) {
+                                       
$exceptionsByConstraintId[$constraintId] = $e;
+                                       $result[] = new CheckResult( $context, 
$constraint, [], CheckResult::STATUS_BAD_PARAMETERS, $e->getMessage() );
+                                       continue;
+                               }
                        }
 
                        if ( in_array( $context->getEntity()->getId(), 
$exceptions ) ) {
diff --git a/tests/phpunit/ConstraintCheckPlanTest.php 
b/tests/phpunit/ConstraintCheckPlanTest.php
index e304c60..43a2138 100644
--- a/tests/phpunit/ConstraintCheckPlanTest.php
+++ b/tests/phpunit/ConstraintCheckPlanTest.php
@@ -2,6 +2,7 @@
 
 namespace WikibaseQuality\ConstraintReport\Test\ConstraintChecker;
 
+use PHPUnit_Framework_MockObject_Matcher_InvokedCount;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\PropertyId;
 use Wikibase\Repo\Tests\NewItem;
@@ -57,13 +58,21 @@
 
        /**
         * @param string[]|ConstraintParameterException $exceptions
+        * @param PHPUnit_Framework_MockObject_Matcher_InvokedCount 
$invokedCount
         * @return ConstraintParameterParser
         */
-       private function getExceptionsParser( $exceptions = [] ) {
+       private function getExceptionsParser(
+               $exceptions = [],
+               PHPUnit_Framework_MockObject_Matcher_InvokedCount $invokedCount 
= null
+       ) {
                $mock = $this->getMockBuilder( ConstraintParameterParser::class 
)
                        ->disableOriginalConstructor()
                        ->getMock();
-               $invocation = $mock->method( 'parseExceptionParameter' );
+               if ( $invokedCount === null ) {
+                       $invokedCount = $this->any();
+               }
+               $invocation = $mock->expects( $invokedCount )
+                       ->method( 'parseExceptionParameter' );
                if ( is_array( $exceptions ) ) {
                        $exceptions = array_map( function( $exception ) {
                                return new ItemId( $exception );
@@ -189,7 +198,7 @@
                );
                $plan = new ConstraintCheckPlan(
                        $this->getConstraintLookup( [ $constraint ] ),
-                       $this->getExceptionsParser( [ 'Q3' ] ),
+                       $this->getExceptionsParser( [ 'Q3' ], $this->once() ),
                        false,
                        false
                );
@@ -261,8 +270,8 @@
                        []
                );
                $plan = new ConstraintCheckPlan(
-                       $this->getConstraintLookup( [ $constraint ] ),
-                       $this->getExceptionsParser( new 
ConstraintParameterException( '' ) ),
+                       $this->getConstraintLookup( [ $constraint, $constraint 
] ),
+                       $this->getExceptionsParser( new 
ConstraintParameterException( '' ), $this->once() ),
                        false,
                        false
                );
@@ -276,8 +285,9 @@
                        $this->fail( 'should not run any constraint checks if 
exception parsing fails' );
                } );
 
-               $this->assertCount( 1, $results );
+               $this->assertCount( 2, $results );
                $this->assertSame( CheckResult::STATUS_BAD_PARAMETERS, 
$results[0]->getStatus() );
+               $this->assertEquals( $results[0], $results[1] );
        }
 
        public function testDefaultResults() {

-- 
To view, visit https://gerrit.wikimedia.org/r/392061
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I50bf66f34a9499682009c6e8edd02c3cda293c41
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to