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

Change subject: Add support for importing properties parameters
......................................................................

Add support for importing properties parameters

ConstraintStatementParameterParser gains a new method to parse a
“properties” parameter, which is similar to a “property” parameter but
contains a list instead of a single property. This is used by the
Qualifiers constraint type.

Bug: T167415
Change-Id: I3cde7155de7ba9eed9221ab407fbb60807d7e7a9
---
M includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
M tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
2 files changed, 95 insertions(+), 0 deletions(-)


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

diff --git 
a/includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php 
b/includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
index 82bc97a..2daa8d4 100644
--- a/includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
+++ b/includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
@@ -382,4 +382,63 @@
                }
        }
 
+       private function parsePropertiesParameterFromStatement( array 
$constraintParameters ) {
+               $propertyId = $this->config->get( 
'WBQualityConstraintsPropertyId' );
+               $parameters = $constraintParameters[$propertyId];
+               if ( count( $parameters ) === 1 &&
+                       $this->snakDeserializer->deserialize( $parameters[0] ) 
instanceof PropertyNoValueSnak ) {
+                       return [];
+               }
+               $properties = [];
+               foreach ( $parameters as $parameter ) {
+                       $properties[] = $this->parsePropertyIdParameter( 
$parameter, $propertyId );
+               }
+               return $properties;
+       }
+
+       private function parsePropertiesParameterFromTemplate( array 
$constraintParameters ) {
+               if ( $constraintParameters['property'] === '' ) {
+                       return [];
+               }
+               return array_map(
+                       function( $property ) {
+                               try {
+                                       return new PropertyId( $property );
+                               } catch ( InvalidArgumentException $e ) {
+                                       throw new ConstraintParameterException(
+                                               wfMessage( 
'wbqc-violation-message-parameter-property' )
+                                                       ->rawParams(
+                                                               
$this->constraintParameterRenderer->formatPropertyId( 'property' ),
+                                                               
$this->constraintParameterRenderer->formatDataValue( new StringValue( $property 
) )
+                                                       )
+                                                       ->escaped()
+                                       );
+                               }
+                       },
+                       explode( ',', $constraintParameters['property'] )
+               );
+       }
+
+       /**
+        * @param array $constraintParameters
+        * @param string $constraintTypeName used in error messages
+        * @throws ConstraintParameterException if the parameter is invalid or 
missing
+        * @return PropertyId[]
+        */
+       public function parsePropertiesParameter( array $constraintParameters, 
$constraintTypeName ) {
+               $propertyId = $this->config->get( 
'WBQualityConstraintsPropertyId' );
+               if ( array_key_exists( $propertyId, $constraintParameters ) ) {
+                       return $this->parsePropertiesParameterFromStatement( 
$constraintParameters );
+               } elseif ( array_key_exists( 'property', $constraintParameters 
) ) {
+                       return $this->parsePropertiesParameterFromTemplate( 
$constraintParameters );
+               } else {
+                       throw new ConstraintParameterException(
+                               wfMessage( 
'wbqc-violation-message-parameter-needed' )
+                               ->params( $constraintTypeName )
+                               ->rawParams( 
$this->constraintParameterRenderer->formatPropertyId( $propertyId ) )
+                               ->escaped()
+                               );
+               }
+       }
+
 }
diff --git a/tests/phpunit/Helper/ConstraintStatementParameterParserTest.php 
b/tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
index 9e3decf..15b777c 100644
--- a/tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
+++ b/tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
@@ -448,4 +448,40 @@
                );
        }
 
+       public function testParsePropertiesParameter() {
+               $config = $this->getDefaultConfig();
+               $propertyId = $config->get( 'WBQualityConstraintsPropertyId' );
+               $parsed = 
$this->getConstraintParameterParser()->parsePropertiesParameter(
+                       [ $propertyId => [ $this->serializePropertyId( 'P100' 
), $this->serializePropertyId( 'P101' ) ] ],
+                       ''
+               );
+               $this->assertEquals( [ new PropertyId( 'P100' ), new 
PropertyId( 'P101' ) ], $parsed );
+       }
+
+       public function testParsePropertiesParameterFromTemplate() {
+               $parsed = 
$this->getConstraintParameterParser()->parsePropertiesParameter(
+                       [ 'property' => 'p100,p101' ],
+                       ''
+               );
+               $this->assertEquals( [ new PropertyId( 'P100' ), new 
PropertyId( 'P101' ) ], $parsed );
+       }
+
+       public function testParsePropertiesParameterMissing() {
+               $this->assertThrowsConstraintParameterException(
+                       'parsePropertiesParameter',
+                       [ [], 'constraint' ],
+                       'wbqc-violation-message-parameter-needed'
+               );
+       }
+
+       public function testParsePropertiesParameterNoValue() {
+               $config = $this->getDefaultConfig();
+               $propertyId = $config->get( 'WBQualityConstraintsPropertyId' );
+               $parsed = 
$this->getConstraintParameterParser()->parsePropertiesParameter(
+                       [ $propertyId => [ $this->snakSerializer->serialize( 
new PropertyNoValueSnak( new PropertyId( $propertyId ) ) ) ] ],
+                       ''
+               );
+               $this->assertEquals( [], $parsed );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3cde7155de7ba9eed9221ab407fbb60807d7e7a9
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