Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/402880 )
Change subject: Add support for constraint scopes to ConstraintParameterRenderer
......................................................................
Add support for constraint scopes to ConstraintParameterRenderer
ConstraintParameterParser turns constraint scopes from their special
items into the corresponding Role constants, but for error messages we’d
like to have the items again.
(As an alternative, I first considered having ConstraintParameterParser
return the item IDs as well, but that only gets us “X is not a valid
scope” messages, not “the valid scopes are X, Y, Z”.)
Change-Id: I0d6e77a14b79dc42e8dc2ca4f99d3ad7c24f5ee6
---
M src/Api/CheckConstraints.php
M src/ConstraintParameterRenderer.php
M src/ConstraintReportFactory.php
M src/Specials/SpecialConstraintReport.php
M tests/phpunit/Api/CheckConstraintsTest.php
M tests/phpunit/Api/CheckingResultsBuilderTest.php
M tests/phpunit/ConstraintParameters.php
M tests/phpunit/Specials/SpecialConstraintReportTest.php
8 files changed, 101 insertions(+), 8 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
refs/changes/80/402880/1
diff --git a/src/Api/CheckConstraints.php b/src/Api/CheckConstraints.php
index d30ad93..bc4b96e 100644
--- a/src/Api/CheckConstraints.php
+++ b/src/Api/CheckConstraints.php
@@ -88,11 +88,15 @@
$entityIdHtmlLinkFormatter =
$entityIdHtmlLinkFormatterFactory->getEntityIdFormatter(
$labelDescriptionLookup );
$entityIdLabelFormatterFactory = new
EntityIdLabelFormatterFactory();
$entityIdLabelFormatter =
$entityIdLabelFormatterFactory->getEntityIdFormatter( $labelDescriptionLookup );
- $constraintParameterRenderer = new ConstraintParameterRenderer(
$entityIdHtmlLinkFormatter, $valueFormatter );
$config = MediaWikiServices::getInstance()->getMainConfig();
$titleParser =
MediaWikiServices::getInstance()->getTitleParser();
$unitConverter = $repo->getUnitConverter();
$dataFactory =
MediaWikiServices::getInstance()->getStatsdDataFactory();
+ $constraintParameterRenderer = new ConstraintParameterRenderer(
+ $entityIdHtmlLinkFormatter,
+ $valueFormatter,
+ $config
+ );
$constraintReportFactory = new ConstraintReportFactory(
$repo->getEntityLookup(),
$repo->getPropertyDataTypeLookup(),
diff --git a/src/ConstraintParameterRenderer.php
b/src/ConstraintParameterRenderer.php
index 0735604..334185f 100644
--- a/src/ConstraintParameterRenderer.php
+++ b/src/ConstraintParameterRenderer.php
@@ -1,6 +1,7 @@
<?php
namespace WikibaseQuality\ConstraintReport;
+use Config;
use DataValues\DataValue;
use InvalidArgumentException;
use ValueFormatters\ValueFormatter;
@@ -8,6 +9,7 @@
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Services\EntityId\EntityIdFormatter;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
use WikibaseQuality\ConstraintReport\ConstraintCheck\ItemIdSnakValue;
/**
@@ -36,15 +38,23 @@
private $dataValueFormatter;
/**
+ * @var Config
+ */
+ private $config;
+
+ /**
* @param EntityIdFormatter $entityIdFormatter should return HTML
* @param ValueFormatter $dataValueFormatter should return HTML
+ * @param Config $config used to look up item IDs of constraint scopes
(Context::TYPE_* constants)
*/
public function __construct(
EntityIdFormatter $entityIdFormatter,
- ValueFormatter $dataValueFormatter
+ ValueFormatter $dataValueFormatter,
+ Config $config
) {
$this->entityIdLabelFormatter = $entityIdFormatter;
$this->dataValueFormatter = $dataValueFormatter;
+ $this->config = $config;
}
/**
@@ -223,6 +233,40 @@
}
/**
+ * Format a constraint scope (check on main snak, on qualifiers, or on
references).
+ *
+ * @param string $scope one of the Context::TYPE_* constants
+ * @param string|null $role one of the Role constants or null
+ * @return string HTML
+ */
+ public function formatConstraintScope( $scope, $role = null ) {
+ switch ( $scope ) {
+ case Context::TYPE_STATEMENT:
+ $itemId = $this->config->get(
+
'WBQualityConstraintsConstraintCheckedOnMainValueId'
+ );
+ break;
+ case Context::TYPE_QUALIFIER:
+ $itemId = $this->config->get(
+
'WBQualityConstraintsConstraintCheckedOnQualifiersId'
+ );
+ break;
+ case Context::TYPE_REFERENCE:
+ $itemId = $this->config->get(
+
'WBQualityConstraintsConstraintCheckedOnReferencesId'
+ );
+ break;
+ default:
+ // callers should never let this happen, but if
it does happen,
+ // showing “unknown value” seems reasonable
+ // @codeCoverageIgnoreStart
+ return $this->formatItemIdSnakValue(
ItemIdSnakValue::someValue(), $role );
+ // @codeCoverageIgnoreEnd
+ }
+ return $this->formatItemId( $itemId, $role );
+ }
+
+ /**
* Format a list of (potentially unparsed) property IDs.
*
* The returned array begins with an HTML list of the formatted
property IDs
@@ -331,4 +375,36 @@
return $formattedValues;
}
+ /**
+ * Format a list of constraint scopes (check on main snak, qualifiers,
and/or references).
+ *
+ * The returned array begins with an HTML list of the formatted scopes
+ * and then contains all the individual formatted scopes.
+ *
+ * @param string[] $scopes
+ * @param string|null $role one of the Role constants or null
+ * @return string[] HTML
+ */
+ public function formatConstraintScopeList( array $scopes, $role = null
) {
+ if ( empty( $scopes ) ) {
+ return [ '<ul></ul>' ];
+ }
+ $scopes = $this->limitArrayLength( $scopes );
+ $formattedScopes = array_map(
+ function( $scope ) use ( $role ) {
+ if ( $scope === '...' ) {
+ return '...';
+ } else {
+ return $this->formatConstraintScope(
$scope, $role );
+ }
+ },
+ $scopes
+ );
+ array_unshift(
+ $formattedScopes,
+ '<ul><li>' . implode( '</li><li>', $formattedScopes ) .
'</li></ul>'
+ );
+ return $formattedScopes;
+ }
+
}
diff --git a/src/ConstraintReportFactory.php b/src/ConstraintReportFactory.php
index 0c03ae6..1147a80 100644
--- a/src/ConstraintReportFactory.php
+++ b/src/ConstraintReportFactory.php
@@ -151,7 +151,8 @@
$wikibaseRepo->getValueFormatterFactory()->getValueFormatter(
SnakFormatter::FORMAT_HTML,
new FormatterOptions()
- )
+ ),
+ $config
);
$instance = new self(
$wikibaseRepo->getEntityLookup(),
diff --git a/src/Specials/SpecialConstraintReport.php
b/src/Specials/SpecialConstraintReport.php
index 3a711f8..bee8dac 100644
--- a/src/Specials/SpecialConstraintReport.php
+++ b/src/Specials/SpecialConstraintReport.php
@@ -166,7 +166,11 @@
$this->constraintChecker = $constraintChecker;
- $this->constraintParameterRenderer = new
ConstraintParameterRenderer( $this->entityIdLabelFormatter,
$this->dataValueFormatter );
+ $this->constraintParameterRenderer = new
ConstraintParameterRenderer(
+ $this->entityIdLabelFormatter,
+ $this->dataValueFormatter,
+ $config
+ );
$this->config = $config;
$this->dataFactory = $dataFactory;
diff --git a/tests/phpunit/Api/CheckConstraintsTest.php
b/tests/phpunit/Api/CheckConstraintsTest.php
index 2937182..4f1b9a3 100644
--- a/tests/phpunit/Api/CheckConstraintsTest.php
+++ b/tests/phpunit/Api/CheckConstraintsTest.php
@@ -115,7 +115,11 @@
'WBQualityConstraintsIncludeDetailInApi' =>
true,
] );
$entityIdParser = new ItemIdParser();
- $constraintParameterRenderer = new
ConstraintParameterRenderer( $entityIdFormatter, $valueFormatter );
+ $constraintParameterRenderer = new
ConstraintParameterRenderer(
+ $entityIdFormatter,
+ $valueFormatter,
+ $config
+ );
$constraintParameterParser = new
ConstraintParameterParser(
$config,
$repo->getBaseDataModelDeserializerFactory(),
diff --git a/tests/phpunit/Api/CheckingResultsBuilderTest.php
b/tests/phpunit/Api/CheckingResultsBuilderTest.php
index 267b9d9..49ee639 100644
--- a/tests/phpunit/Api/CheckingResultsBuilderTest.php
+++ b/tests/phpunit/Api/CheckingResultsBuilderTest.php
@@ -68,7 +68,9 @@
$entityTitleLookup,
$entityIdFormatter,
new ConstraintParameterRenderer(
- $entityIdFormatter, $this->getMock(
ValueFormatter::class )
+ $entityIdFormatter,
+ $this->getMock( ValueFormatter::class ),
+ $this->getDefaultConfig()
),
$this->getDefaultConfig()
);
diff --git a/tests/phpunit/ConstraintParameters.php
b/tests/phpunit/ConstraintParameters.php
index 59ef20f..7c16529 100644
--- a/tests/phpunit/ConstraintParameters.php
+++ b/tests/phpunit/ConstraintParameters.php
@@ -67,7 +67,8 @@
$entityIdFormatter = new PlainEntityIdFormatter();
$this->renderer = new ConstraintParameterRenderer(
$entityIdFormatter,
- $valueFormatter
+ $valueFormatter,
+ $this->getDefaultConfig()
);
}
diff --git a/tests/phpunit/Specials/SpecialConstraintReportTest.php
b/tests/phpunit/Specials/SpecialConstraintReportTest.php
index 890fd7b..704a331 100644
--- a/tests/phpunit/Specials/SpecialConstraintReportTest.php
+++ b/tests/phpunit/Specials/SpecialConstraintReportTest.php
@@ -71,7 +71,8 @@
$wikibaseRepo->getValueFormatterFactory()->getValueFormatter(
SnakFormatter::FORMAT_HTML,
new FormatterOptions()
- )
+ ),
+ $config
);
$constraintReportFactory = new ConstraintReportFactory(
$wikibaseRepo->getEntityLookup(),
--
To view, visit https://gerrit.wikimedia.org/r/402880
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0d6e77a14b79dc42e8dc2ca4f99d3ad7c24f5ee6
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