Replace MathWikibaseConfig with ServiceOptions

MathWikibaseConfig introduced additional complexity by passing through
some services to MathWikibaseConnector.
The remaining options were just a list of properties that could also be defined as ServiceOptions.

Bug: T313646
Change-Id: Ib9083c0cb62f6d972befc7f5dc3ed47f55cf92a5
This commit is contained in:
Moritz Schubotz (physikerwelt) 2022-07-23 11:33:40 +02:00
parent 0bec9dc904
commit 882c49599b
No known key found for this signature in database
GPG key ID: F803DB146DDF36C3
5 changed files with 82 additions and 244 deletions

View file

@ -3,7 +3,6 @@
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\Math\InputCheck\InputCheckFactory;
use MediaWiki\Extension\Math\MathConfig;
use MediaWiki\Extension\Math\MathWikibaseConfig;
use MediaWiki\Extension\Math\MathWikibaseConnector;
use MediaWiki\Extension\Math\Render\RendererFactory;
use MediaWiki\Logger\LoggerFactory;
@ -40,19 +39,14 @@ return [
},
'Math.WikibaseConnector' => static function ( MediaWikiServices $services ): MathWikibaseConnector {
return new MathWikibaseConnector(
$services->get( 'Math.WikibaseConfig' ),
new ServiceOptions( MathWikibaseConnector::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ),
WikibaseClient::getRepoLinker( $services ),
$services->getLanguageFactory(),
LoggerFactory::getInstance( 'Math' )
);
},
'Math.WikibaseConfig' => static function ( MediaWikiServices $services ): MathWikibaseConfig {
return new MathWikibaseConfig(
WikibaseClient::getEntityIdParser( $services ),
WikibaseClient::getEntityRevisionLookup( $services ),
WikibaseClient::getFallbackLabelDescriptionLookupFactory( $services ),
WikibaseClient::getSite( $services ),
$services->getMainConfig()
WikibaseClient::getEntityIdParser( $services ),
LoggerFactory::getInstance( 'Math' )
);
},
}
];

View file

@ -1,130 +0,0 @@
<?php
namespace MediaWiki\Extension\Math;
use Config;
use Site;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
/**
* A config class for the MathWikibaseConnector to connect with Wikibase
* @see MathWikibaseConnector
*/
class MathWikibaseConfig {
/**
* @var EntityIdParser
*/
private $idParser;
/**
* @var EntityRevisionLookup
*/
private $entityRevisionLookup;
/**
* @var FallbackLabelDescriptionLookupFactory
*/
private $labelLookupFactory;
/**
* @var Site
*/
private $site;
/**
* @var PropertyId
*/
private $propertyIdHasPart;
/**
* @var PropertyId
*/
private $propertyIdQuantitySymbol;
/**
* @var PropertyId
*/
private $propertyIdDefiningFormula;
/**
* @param EntityIdParser $entityIdParser
* @param EntityRevisionLookup $entityRevisionLookup
* @param FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory
* @param Site $site
* @param Config $config
*/
public function __construct(
EntityIdParser $entityIdParser,
EntityRevisionLookup $entityRevisionLookup,
FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory,
Site $site,
Config $config
) {
$this->idParser = $entityIdParser;
$this->entityRevisionLookup = $entityRevisionLookup;
$this->labelLookupFactory = $labelDescriptionLookupFactory;
$this->site = $site;
$this->propertyIdHasPart = $this->idParser->parse(
$config->get( "MathWikibasePropertyIdHasPart" )
);
$this->propertyIdDefiningFormula = $this->idParser->parse(
$config->get( "MathWikibasePropertyIdDefiningFormula" )
);
$this->propertyIdQuantitySymbol = $this->idParser->parse(
$config->get( "MathWikibasePropertyIdQuantitySymbol" )
);
}
/**
* @return EntityIdParser
*/
public function getIdParser(): EntityIdParser {
return $this->idParser;
}
/**
* @return EntityRevisionLookup
*/
public function getEntityRevisionLookup(): EntityRevisionLookup {
return $this->entityRevisionLookup;
}
/**
* @return FallbackLabelDescriptionLookupFactory
*/
public function getLabelLookupFactory(): FallbackLabelDescriptionLookupFactory {
return $this->labelLookupFactory;
}
/**
* @return Site
*/
public function getSite(): Site {
return $this->site;
}
/**
* @return PropertyId
*/
public function getPropertyIdHasPart(): PropertyId {
return $this->propertyIdHasPart;
}
/**
* @return PropertyId
*/
public function getPropertyIdQuantitySymbol(): PropertyId {
return $this->propertyIdQuantitySymbol;
}
/**
* @return PropertyId
*/
public function getPropertyIdDefiningFormula(): PropertyId {
return $this->propertyIdDefiningFormula;
}
}

View file

@ -4,19 +4,25 @@ namespace MediaWiki\Extension\Math;
use DataValues\StringValue;
use InvalidArgumentException;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Languages\LanguageFactory;
use MWException;
use Psr\Log\LoggerInterface;
use Site;
use Wikibase\Client\RepoLinker;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\EntityIdParser;
use Wikibase\DataModel\Entity\EntityIdParsingException;
use Wikibase\DataModel\Entity\EntityIdValue;
use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
use Wikibase\DataModel\Snak\PropertyValueSnak;
use Wikibase\DataModel\Snak\Snak;
use Wikibase\DataModel\Statement\StatementList;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
use Wikibase\Lib\Store\RevisionedUnresolvedRedirectException;
use Wikibase\Lib\Store\StorageException;
@ -27,10 +33,12 @@ use Wikibase\Lib\Store\StorageException;
* @see MathWikibaseConnector::getInstance() to get an instance of the class
*/
class MathWikibaseConnector {
/**
* @var MathWikibaseConfig
*/
private $config;
/** @var string[] */
public const CONSTRUCTOR_OPTIONS = [
'MathWikibasePropertyIdHasPart',
'MathWikibasePropertyIdDefiningFormula',
'MathWikibasePropertyIdQuantitySymbol'
];
/** @var LoggerInterface */
private $logger;
@ -41,22 +49,65 @@ class MathWikibaseConnector {
/** @var LanguageFactory */
private $languageFactory;
/** @var EntityRevisionLookup */
private $entityRevisionLookup;
/** @var Site */
private $site;
/** @var FallbackLabelDescriptionLookupFactory */
private $labelDescriptionLookupFactory;
/** @var EntityIdParser */
private $idParser;
/** @var PropertyId */
private $propertyIdHasPart;
/** @var PropertyId */
private $propertyIdDefiningFormula;
/** @var PropertyId */
private $propertyIdQuantitySymbol;
/**
* @param MathWikibaseConfig $config
* @param ServiceOptions $options
* @param RepoLinker $repoLinker
* @param LanguageFactory $languageFactory
* @param EntityRevisionLookup $entityRevisionLookup
* @param FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory
* @param Site $site
* @param EntityIdParser $entityIdParser
* @param LoggerInterface $logger
*/
public function __construct(
MathWikibaseConfig $config,
ServiceOptions $options,
RepoLinker $repoLinker,
LanguageFactory $languageFactory,
EntityRevisionLookup $entityRevisionLookup,
FallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory,
Site $site,
EntityIdParser $entityIdParser,
LoggerInterface $logger
) {
$this->config = $config;
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->repoLinker = $repoLinker;
$this->languageFactory = $languageFactory;
$this->entityRevisionLookup = $entityRevisionLookup;
$this->labelDescriptionLookupFactory = $labelDescriptionLookupFactory;
$this->site = $site;
$this->idParser = $entityIdParser;
$this->logger = $logger;
$this->propertyIdHasPart = $this->idParser->parse(
$options->get( "MathWikibasePropertyIdHasPart" )
);
$this->propertyIdDefiningFormula = $this->idParser->parse(
$options->get( "MathWikibasePropertyIdDefiningFormula" )
);
$this->propertyIdQuantitySymbol = $this->idParser->parse(
$options->get( "MathWikibasePropertyIdQuantitySymbol" )
);
}
/**
@ -75,15 +126,10 @@ class MathWikibaseConnector {
throw new InvalidArgumentException( "Invalid language code specified." );
}
$langLookupFactory = $this->config->getLabelLookupFactory();
$langLookup = $langLookupFactory->newLabelDescriptionLookup( $lang );
$idParser = $this->config->getIdParser();
$entityRevisionLookup = $this->config->getEntityRevisionLookup();
$langLookup = $this->labelDescriptionLookupFactory->newLabelDescriptionLookup( $lang );
try {
$entityId = $idParser->parse( $qid ); // exception if the given ID is invalid
$entityRevision = $entityRevisionLookup->getEntityRevision( $entityId );
$entityId = $this->idParser->parse( $qid ); // exception if the given ID is invalid
$entityRevision = $this->entityRevisionLookup->getEntityRevision( $entityId );
} catch ( EntityIdParsingException $e ) {
throw new InvalidArgumentException( "Invalid Wikibase ID." );
} catch ( RevisionedUnresolvedRedirectException | StorageException $e ) {
@ -143,12 +189,12 @@ class MathWikibaseConnector {
LabelDescriptionLookup $langLookup ) {
$statements = $item->getStatements();
$hasPartStatements = $statements->getByPropertyId( $this->config->getPropertyIdHasPart() );
$hasPartStatements = $statements->getByPropertyId( $this->propertyIdHasPart );
$this->fetchHasPartSnaks( $output, $hasPartStatements, $langLookup );
$symbolStatement = $statements->getByPropertyId( $this->config->getPropertyIdDefiningFormula() );
$symbolStatement = $statements->getByPropertyId( $this->propertyIdDefiningFormula );
if ( $symbolStatement->count() < 1 ) { // if it's not a formula, it might be a symbol
$symbolStatement = $statements->getByPropertyId( $this->config->getPropertyIdQuantitySymbol() );
$symbolStatement = $statements->getByPropertyId( $this->propertyIdQuantitySymbol );
}
$this->fetchSymbol( $output, $symbolStatement );
return $output;
@ -198,7 +244,7 @@ class MathWikibaseConnector {
if ( $this->isQualifierDefinien( $snak ) ) {
$dataVal = $snak->getDataValue();
$symbol = new StringValue( $dataVal->getValue() );
} elseif ( $snak->getPropertyId()->equals( $this->config->getPropertyIdHasPart() ) ) {
} elseif ( $snak->getPropertyId()->equals( $this->propertyIdHasPart ) ) {
$dataVal = $snak->getDataValue();
$entityIdValue = $dataVal->getValue();
if ( $entityIdValue instanceof EntityIdValue ) {
@ -230,15 +276,13 @@ class MathWikibaseConnector {
*/
private function fetchPageUrl( EntityId $entityId ) {
try {
$entityRevisionLookup = $this->config->getEntityRevisionLookup();
$entityRevision = $entityRevisionLookup->getEntityRevision( $entityId );
$entityRevision = $this->entityRevisionLookup->getEntityRevision( $entityId );
$innerEntity = $entityRevision->getEntity();
if ( $innerEntity instanceof Item ) {
$site = $this->config->getSite();
$globalID = $site->getGlobalId();
$globalID = $this->site->getGlobalId();
if ( $innerEntity->hasLinkToSite( $globalID ) ) {
$siteLink = $innerEntity->getSiteLink( $globalID );
return $site->getPageUrl( $siteLink->getPageName() );
return $this->site->getPageUrl( $siteLink->getPageName() );
}
}
return false;
@ -255,8 +299,8 @@ class MathWikibaseConnector {
* @return bool true if the given snak is either a defining formula or a quantity symbol
*/
private function isQualifierDefinien( Snak $snak ) {
return $snak->getPropertyId()->equals( $this->config->getPropertyIdQuantitySymbol() ) ||
$snak->getPropertyId()->equals( $this->config->getPropertyIdDefiningFormula() );
return $snak->getPropertyId()->equals( $this->propertyIdQuantitySymbol ) ||
$snak->getPropertyId()->equals( $this->propertyIdDefiningFormula );
}
/**

View file

@ -1,68 +0,0 @@
<?php
namespace MediaWiki\Extension\Math\Tests;
use HashConfig;
use MediaWiki\Extension\Math\MathWikibaseConfig;
use MediaWikiUnitTestCase;
use Site;
use Wikibase\DataModel\Entity\BasicEntityIdParser;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
/**
* @covers \MediaWiki\Extension\Math\MathWikibaseConfig
*/
class MathWikibaseConfigTest extends MediaWikiUnitTestCase {
private function getConfig() {
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class );
$labelDescriptionLookupFactory = $this->createMock( FallbackLabelDescriptionLookupFactory::class );
return new MathWikibaseConfig(
new BasicEntityIdParser(),
$entityRevisionLookup,
$labelDescriptionLookupFactory,
new Site(),
new HashConfig( [
'MathWikibasePropertyIdHasPart' => 'P1',
'MathWikibasePropertyIdDefiningFormula' => 'P2',
'MathWikibasePropertyIdQuantitySymbol' => 'P3'
] ) );
}
public function testGetEntityRevisionLookup() {
$config = $this->getConfig();
$this->assertInstanceOf( 'Wikibase\Lib\Store\EntityRevisionLookup', $config->getEntityRevisionLookup() );
}
public function testGetSite() {
$config = $this->getConfig();
$this->assertInstanceOf( 'Site', $config->getSite() );
}
public function testGetPropertyIdQuantitySymbol() {
$config = $this->getConfig();
$this->assertEquals( 'P3', $config->getPropertyIdQuantitySymbol()->getLocalPart() );
}
public function testGetLabelLookupFactory() {
$config = $this->getConfig();
$this->assertInstanceOf( 'Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory',
$config->getLabelLookupFactory() );
}
public function testGetIdParser() {
$config = $this->getConfig();
$this->assertInstanceOf( 'Wikibase\DataModel\Entity\BasicEntityIdParser', $config->getIdParser() );
}
public function testGetPropertyIdHasPart() {
$config = $this->getConfig();
$this->assertEquals( 'P1', $config->getPropertyIdHasPart()->getLocalPart() );
}
public function testGetPropertyIdDefiningFormula() {
$config = $this->getConfig();
$this->assertEquals( 'P2', $config->getPropertyIdDefiningFormula()->getLocalPart() );
}
}

View file

@ -2,8 +2,7 @@
namespace MediaWiki\Extension\Math\Tests;
use HashConfig;
use MediaWiki\Extension\Math\MathWikibaseConfig;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\Math\MathWikibaseConnector;
use MediaWiki\Languages\LanguageFactory;
use MediaWikiUnitTestCase;
@ -17,7 +16,6 @@ use Wikibase\DataModel\Entity\BasicEntityIdParser;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Lib\Store\FallbackLabelDescriptionLookupFactory;
use Wikibase\Lib\SubEntityTypesMapper;
use const MWException;
/**
* @covers \MediaWiki\Extension\Math\MathWikibaseConnector
@ -79,18 +77,18 @@ class MathWikibaseConnectorTest extends MediaWikiUnitTestCase {
$entityRevisionLookup = $this->createMock( EntityRevisionLookup::class );
$labelDescriptionLookupFactory = $this->createMock( FallbackLabelDescriptionLookupFactory::class );
$languageFactory = $languageFactory ?: $this->createMock( LanguageFactory::class );
return new MathWikibaseConnector( new MathWikibaseConfig(
new BasicEntityIdParser(),
$entityRevisionLookup,
$labelDescriptionLookupFactory,
new Site(),
new HashConfig( [
return new MathWikibaseConnector(
new ServiceOptions( MathWikibaseConnector::CONSTRUCTOR_OPTIONS, [
'MathWikibasePropertyIdHasPart' => 'P1',
'MathWikibasePropertyIdDefiningFormula' => 'P2',
'MathWikibasePropertyIdQuantitySymbol' => 'P3'
] ) ),
] ),
$this->newConnector(),
$languageFactory,
$entityRevisionLookup,
$labelDescriptionLookupFactory,
new Site(),
new BasicEntityIdParser(),
new TestLogger() );
}