Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/353975 )
Change subject: Inject ConstraintParameterRenderer into ConstraintReportFactory
......................................................................
Inject ConstraintParameterRenderer into ConstraintReportFactory
The ConstraintReportFactory can then inject this renderer into the
individual checkers instead of the current EntityIdFormatter (which will
eventually be removed).
Includes refactorings to the two places that instantiate a
ConstraintReportFactory directly (all other places use
getDefaultInstance()). The API factory function is heavily refactored to
get most of its services from the Wikibase repo instead of instantiating
them itself.
Change-Id: Ib6ee98b5a9f0c6c5800bfa3bd2fccf9b94f39a15
---
M api/CheckConstraints.php
M includes/ConstraintReportFactory.php
M tests/phpunit/DelegatingConstraintCheckerTest.php
3 files changed, 53 insertions(+), 52 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
refs/changes/75/353975/1
diff --git a/api/CheckConstraints.php b/api/CheckConstraints.php
index 9761df0..85a039d 100644
--- a/api/CheckConstraints.php
+++ b/api/CheckConstraints.php
@@ -99,30 +99,30 @@
*/
public static function newFromGlobalState( ApiMain $main, $name,
$prefix = '' ) {
$repo = WikibaseRepo::getDefaultInstance();
- $constraintReportFactory =
ConstraintReportFactory::getDefaultInstance();
- $termLookup = $repo->getTermLookup();
- $termBuffer = $repo->getTermBuffer();
- $languageFallbackChainFactory = new
LanguageFallbackChainFactory();
- $fallbackLabelDescLookupFactory = new
LanguageFallbackLabelDescriptionLookupFactory( $languageFallbackChainFactory,
$termLookup, $termBuffer );
- $factory = new EntityIdLabelFormatterFactory();
- $language = new Language();
- $labelLookup =
$fallbackLabelDescLookupFactory->newLabelDescriptionLookup( $language );
-
+ $language = $repo->getUserLanguage();
$formatterOptions = new FormatterOptions();
- $factoryFunctions = [];
- Assert::parameterElementType( 'callable', $factoryFunctions,
'$factoryFunctions' );
$formatterOptions->setOption( SnakFormatter::OPT_LANG,
$language->getCode() );
- $valueFormatterFactory = new OutputFormatValueFormatterFactory(
- $factoryFunctions,
- $language,
- $languageFallbackChainFactory
- );
+ $valueFormatterFactory = $repo->getValueFormatterFactory();
$valueFormatter = $valueFormatterFactory->getValueFormatter(
SnakFormatter::FORMAT_HTML, $formatterOptions );
+ $languageFallbackLabelDescriptionLookupFactory =
$repo->getLanguageFallbackLabelDescriptionLookupFactory();
+ $labelDescriptionLookup =
$languageFallbackLabelDescriptionLookupFactory->newLabelDescriptionLookup(
$language );
+ $entityIdHtmlLinkFormatterFactory =
$repo->getEntityIdHtmlLinkFormatterFactory();
+ $entityIdFormatter =
$entityIdHtmlLinkFormatterFactory->getEntityIdFormatter(
$labelDescriptionLookup );
+ $statementGuidParser = $repo->getStatementGuidParser();
+ $constraintParameterRenderer = new ConstraintParameterRenderer(
$entityIdFormatter, $valueFormatter );
+ $constraintReportFactory = new ConstraintReportFactory(
+ $repo->getEntityLookup(),
+ $statementGuidParser,
+ MediaWikiServices::getInstance()->getMainConfig(),
+ $entityIdFormatter,
+ $constraintParameterRenderer
+ );
+
return new CheckConstraints( $main, $name, $prefix,
$repo->getEntityIdParser(),
- $repo->getStatementGuidValidator(),
$repo->getStatementGuidParser(),
$constraintReportFactory->getConstraintChecker(),
- new ConstraintParameterRenderer(
$factory->getEntityIdFormatter( $labelLookup ), $valueFormatter ),
+ $repo->getStatementGuidValidator(),
$statementGuidParser, $constraintReportFactory->getConstraintChecker(),
+ $constraintParameterRenderer,
$repo->getApiHelperFactory( RequestContext::getMain() )
);
}
diff --git a/includes/ConstraintReportFactory.php
b/includes/ConstraintReportFactory.php
index aebdaeb..74e168e 100644
--- a/includes/ConstraintReportFactory.php
+++ b/includes/ConstraintReportFactory.php
@@ -4,8 +4,10 @@
use Config;
use MediaWiki\MediaWikiServices;
+use ValueFormatters\FormatterOptions;
use Wikibase\DataModel\Services\EntityId\EntityIdFormatter;
use Wikibase\DataModel\Services\Lookup\EntityLookup;
+use Wikibase\Lib\SnakFormatter;
use Wikibase\Repo\WikibaseRepo;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
@@ -76,6 +78,11 @@
private $entityIdFormatter;
/**
+ * @var ConstraintParameterRenderer
+ */
+ private $constraintParameterRenderer;
+
+ /**
* Returns the default instance.
* IMPORTANT: Use only when it is not feasible to inject an instance
properly.
*
@@ -86,13 +93,21 @@
if ( $instance === null ) {
$wikibaseRepo = WikibaseRepo::getDefaultInstance();
+ $entityIdFormatter =
$wikibaseRepo->getEntityIdHtmlLinkFormatterFactory()->getEntityIdFormatter(
+
$wikibaseRepo->getLanguageFallbackLabelDescriptionLookupFactory()->newLabelDescriptionLookup(
+ $wikibaseRepo->getUserLanguage()
+ )
+ );
$instance = new self(
$wikibaseRepo->getEntityLookup(),
$wikibaseRepo->getStatementGuidParser(),
MediaWikiServices::getInstance()->getMainConfig(),
-
$wikibaseRepo->getEntityIdHtmlLinkFormatterFactory()->getEntityIdFormatter(
-
$wikibaseRepo->getLanguageFallbackLabelDescriptionLookupFactory()->newLabelDescriptionLookup(
- $wikibaseRepo->getUserLanguage()
+ $entityIdFormatter,
+ new ConstraintParameterRenderer(
+ $entityIdFormatter,
+
$wikibaseRepo->getValueFormatterFactory()->getValueFormatter(
+ SnakFormatter::FORMAT_HTML,
+ new FormatterOptions()
)
)
);
@@ -103,15 +118,16 @@
public function __construct(
EntityLookup $lookup,
- StatementGuidParser
- $statementGuidParser,
+ StatementGuidParser $statementGuidParser,
Config $config,
- EntityIdFormatter $entityIdFormatter
+ EntityIdFormatter $entityIdFormatter,
+ ConstraintParameterRenderer $constraintParameterRenderer
) {
$this->lookup = $lookup;
$this->statementGuidParser = $statementGuidParser;
$this->config = $config;
$this->entityIdFormatter = $entityIdFormatter;
+ $this->constraintParameterRenderer =
$constraintParameterRenderer;
}
/**
diff --git a/tests/phpunit/DelegatingConstraintCheckerTest.php
b/tests/phpunit/DelegatingConstraintCheckerTest.php
index 5da6094..4efe254 100644
--- a/tests/phpunit/DelegatingConstraintCheckerTest.php
+++ b/tests/phpunit/DelegatingConstraintCheckerTest.php
@@ -2,11 +2,13 @@
namespace WikibaseQuality\ConstraintReport\Test\ConstraintChecker;
+use ValueFormatters\ValueFormatter;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\ItemIdParser;
use Wikibase\DataModel\Services\EntityId\PlainEntityIdFormatter;
use Wikibase\DataModel\Services\Statement\StatementGuidParser;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\DelegatingConstraintChecker;
+use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
use WikibaseQuality\ConstraintReport\ConstraintReportFactory;
use WikibaseQuality\ConstraintReport\Tests\DefaultConfig;
use WikibaseQuality\Tests\Helper\JsonFileEntityLookup;
@@ -65,11 +67,18 @@
parent::setUp();
$this->lookup = $this->createEntityLookup();
$this->statementGuidParser = new StatementGuidParser( new
ItemIdParser() );
+ $valueFormatter = $this->getMock( ValueFormatter::class );
+ $valueFormatter->method( 'format' )->willReturn( '' );
+ $entityIdFormatter = new PlainEntityIdFormatter();
$factory = new ConstraintReportFactory(
$this->lookup,
$this->statementGuidParser,
$this->getDefaultConfig(),
- new PlainEntityIdFormatter()
+ $entityIdFormatter,
+ new ConstraintParameterRenderer(
+ $entityIdFormatter,
+ $valueFormatter
+ )
);
$this->constraintChecker = $factory->getConstraintChecker();
@@ -294,45 +303,21 @@
}
public function testCheckAgainstConstraints_ByClaims() {
- $factory = new ConstraintReportFactory(
- $this->createEntityLookup(),
- $this->statementGuidParser,
- $this->getDefaultConfig(),
- new PlainEntityIdFormatter()
- );
- $constraintChecker = $factory->getConstraintChecker();
-
- $result = $constraintChecker->checkAgainstConstraintsOnClaimId(
+ $result =
$this->constraintChecker->checkAgainstConstraintsOnClaimId(
$this->statementGuidParser->parse(
'Q1$c0f25a6f-9e33-41c8-be34-c86a730ff30b' ) );
$this->assertCount( 18, $result, 'Every constraint should be
represented by one result' );
}
public function
testCheckAgainstConstraintsDoesNotCrashWhenResultIsEmpty_ByClaims() {
- $factory = new ConstraintReportFactory(
- $this->createEntityLookup(),
- $this->statementGuidParser,
- $this->getDefaultConfig(),
- new PlainEntityIdFormatter()
- );
- $constraintChecker = $factory->getConstraintChecker();
-
- $result = $constraintChecker->checkAgainstConstraintsOnClaimId(
+ $result =
$this->constraintChecker->checkAgainstConstraintsOnClaimId(
$this->statementGuidParser->parse(
'Q2$c0f25a6f-9e33-41c8-be34-c86a730ff30b' ) );
$this->assertCount( 0, $result, 'Should be empty' );
}
public function
testCheckAgainstConstraintsDoesNotCrashWhenClaimDoesNotExist() {
- $factory = new ConstraintReportFactory(
- $this->createEntityLookup(),
- $this->statementGuidParser,
- $this->getDefaultConfig(),
- new PlainEntityIdFormatter()
- );
- $constraintChecker = $factory->getConstraintChecker();
-
- $result = $constraintChecker->checkAgainstConstraintsOnClaimId(
+ $result =
$this->constraintChecker->checkAgainstConstraintsOnClaimId(
$this->statementGuidParser->parse( 'Q99$does-not-exist'
) );
$this->assertCount( 0, $result, 'Should be empty' );
--
To view, visit https://gerrit.wikimedia.org/r/353975
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib6ee98b5a9f0c6c5800bfa3bd2fccf9b94f39a15
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