Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/360873 )
Change subject: Add support for importing format parameters
......................................................................
Add support for importing format parameters
ConstraintStatementParameterParser gains a new method to parse a format
parameter. This is used by the Format constraint type.
(The configuration setting name is deliberately bulky, instead of just
WBQualityConstraintsFormatId – it’s possible that we’ll need a different
kind of format (not regular expression) as part of T102752, and in that
case we’ll want to use a different configuration setting.)
Bug: T167418
Change-Id: Iea4cbd6d31fc60512c48312484ea7ceb7dfca944
---
M extension.json
M includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
M tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
3 files changed, 105 insertions(+), 0 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
refs/changes/73/360873/1
diff --git a/extension.json b/extension.json
index 8275d1e..9e61b60 100644
--- a/extension.json
+++ b/extension.json
@@ -244,6 +244,11 @@
"description": "The property ID of the 'namespace'
property (data type: string), which specifies the namespace parameter of a
'commons link' constraint.",
"public": true
},
+ "WBQualityConstraintsFormatAsARegularExpressionId": {
+ "value": "P2307",
+ "description": "The property ID of the 'format as a
regular expression' property (data type: string), which specifies the format
parameter of a 'format' constraint.",
+ "public": true
+ },
"WBQualityConstraintsYearUnit": {
"value": "http://www.wikidata.org/entity/Q577",
"description": "The unit used for a quantity that
represents a difference between two dates, in years.",
diff --git
a/includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
b/includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
index 7a36d0f..7644673 100644
--- a/includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
+++ b/includes/ConstraintCheck/Helper/ConstraintStatementParameterParser.php
@@ -599,4 +599,36 @@
}
}
+ private function parseFormatParameterFromStatement( array
$constraintParameters ) {
+ $formatId = $this->config->get(
'WBQualityConstraintsFormatAsARegularExpressionId' );
+ $this->requireSingleParameter( $constraintParameters, $formatId
);
+ return $this->parseStringParameter(
$constraintParameters[$formatId][0], $formatId );
+ }
+
+ private function parseFormatParameterFromTemplate( array
$constraintParameters ) {
+ return $constraintParameters['pattern'];
+ }
+
+ /**
+ * @param array $constraintParameters see {@link
\WikibaseQuality\Constraint::getConstraintParameters()}
+ * @param string $constraintTypeName used in error messages
+ * @throws ConstraintParameterException if the parameter is invalid or
missing
+ * @return string
+ */
+ public function parseFormatParameter( array $constraintParameters,
$constraintTypeName ) {
+ $formatId = $this->config->get(
'WBQualityConstraintsFormatAsARegularExpressionId' );
+ if ( array_key_exists( $formatId, $constraintParameters ) ) {
+ return $this->parseFormatParameterFromStatement(
$constraintParameters );
+ } elseif ( array_key_exists( 'pattern', $constraintParameters )
) {
+ return $this->parseFormatParameterFromTemplate(
$constraintParameters );
+ } else {
+ throw new ConstraintParameterException(
+ wfMessage(
'wbqc-violation-message-parameter-needed' )
+ ->params( $constraintTypeName )
+ ->rawParams(
$this->constraintParameterRenderer->formatPropertyId( $formatId ) )
+ ->escaped()
+ );
+ }
+ }
+
}
diff --git a/tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
b/tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
index 30eb9bf..afda1f7 100644
--- a/tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
+++ b/tests/phpunit/Helper/ConstraintStatementParameterParserTest.php
@@ -716,4 +716,72 @@
);
}
+ public function testParseFormatParameter() {
+ $formatId = $this->getDefaultConfig()->get(
'WBQualityConstraintsFormatAsARegularExpressionId' );
+ $value = new StringValue(
'\d\.(\d{1,2}|-{1})\.(\d{1,2}|-{1})\.(\d{1,3}|-{1})' );
+ $snak = new PropertyValueSnak( new PropertyId( 'P1' ), $value );
+
+ $parsed =
$this->getConstraintParameterParser()->parseFormatParameter(
+ [ $formatId => [ $this->snakSerializer->serialize(
$snak ) ] ],
+ ''
+ );
+
+ $this->assertEquals(
'\d\.(\d{1,2}|-{1})\.(\d{1,2}|-{1})\.(\d{1,3}|-{1})', $parsed );
+ }
+
+ public function testParseFormatParameterMissing() {
+ $this->assertThrowsConstraintParameterException(
+ 'parseFormatParameter',
+ [
+ [],
+ 'constraint'
+ ],
+ 'wbqc-violation-message-parameter-needed'
+ );
+ }
+
+ public function testParseFormatParameterFromTemplate() {
+ $parsed =
$this->getConstraintParameterParser()->parseFormatParameter(
+ [ 'pattern' =>
'\d\.(\d{1,2}|-{1})\.(\d{1,2}|-{1})\.(\d{1,3}|-{1})' ],
+ ''
+ );
+
+ $this->assertEquals(
'\d\.(\d{1,2}|-{1})\.(\d{1,2}|-{1})\.(\d{1,3}|-{1})', $parsed );
+ }
+
+ public function testParseFormatParameterItemId() {
+ $formatId = $this->getDefaultConfig()->get(
'WBQualityConstraintsFormatAsARegularExpressionId' );
+ $value = new EntityIdValue( new ItemId( 'Q1' ) );
+ $snak = new PropertyValueSnak( new PropertyId( 'P1' ), $value );
+
+ $this->assertThrowsConstraintParameterException(
+ 'parseFormatParameter',
+ [
+ [ $formatId => [
$this->snakSerializer->serialize( $snak ) ] ],
+ 'constraint'
+ ],
+ 'wbqc-violation-message-parameter-string'
+ );
+ }
+
+ public function testParseFormatParameterMultiple() {
+ $formatId = $this->getDefaultConfig()->get(
'WBQualityConstraintsFormatAsARegularExpressionId' );
+ $value1 = new StringValue(
'\d\.(\d{1,2}|-{1})\.(\d{1,2}|-{1})\.(\d{1,3}|-{1})' );
+ $snak1 = new PropertyValueSnak( new PropertyId( 'P1' ), $value1
);
+ $value2 = new StringValue( '\d+' );
+ $snak2 = new PropertyValueSnak( new PropertyId( 'P1' ), $value2
);
+
+ $this->assertThrowsConstraintParameterException(
+ 'parseFormatParameter',
+ [
+ [ $formatId => [
+ $this->snakSerializer->serialize(
$snak1 ),
+ $this->snakSerializer->serialize(
$snak2 )
+ ] ],
+ 'constraint'
+ ],
+ 'wbqc-violation-message-parameter-single'
+ );
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/360873
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iea4cbd6d31fc60512c48312484ea7ceb7dfca944
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