jenkins-bot has submitted this change and it was merged. Change subject: Inject access to entities into EntityView. ......................................................................
Inject access to entities into EntityView. This greatly reduces the reliance of EntityView on global state and thus allows tests to instantiate EntityView in isolation. Change-Id: I34717dedd7095e64c32728868a8c0b06af40475c --- M repo/includes/EntityView.php M repo/includes/specials/SpecialItemResolver.php 2 files changed, 52 insertions(+), 40 deletions(-) Approvals: Aude: Looks good to me, approved jenkins-bot: Verified diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php index 6d1b11d..e5a0adc 100644 --- a/repo/includes/EntityView.php +++ b/repo/includes/EntityView.php @@ -54,6 +54,7 @@ * @licence GNU GPL v2+ * @author H. Snater < mediawiki at snater.com > * @author Daniel Werner + * @author Daniel Kinzler */ abstract class EntityView extends \ContextSource { @@ -68,6 +69,21 @@ * @var EntityIdFormatter */ protected $idFormatter; + + /** + * @var EntityLookup + */ + protected $entityLookup; + + /** + * @var EntityTitleLookup + */ + protected $entityTitleLookup; + + /** + * @var PropertyDataTypeLookup + */ + protected $dataTypeLookup; /** * Maps entity types to the corresponding entity view. @@ -86,35 +102,29 @@ ); /** - * Constructor. + * @since 0.1 * - * @todo think about using IContextSource here. Parser for example uses parser options (which also can be generated - * from an IContextSource) but this seems sufficient for now. - * - * @since 0.1 - * + * @param IContextSource|null $context * @param ValueFormatterFactory $valueFormatters * @param Lib\PropertyDataTypeLookup $dataTypeLookup - * @param EntityLookup $entityLoader - * @param IContextSource|null $context + * @param EntityLookup $entityLookup + * @param EntityTitleLookup $entityTitleLookup + * @param Lib\EntityIdFormatter $idFormatter */ public function __construct( + IContextSource $context, ValueFormatterFactory $valueFormatters, PropertyDataTypeLookup $dataTypeLookup, - EntityLookup $entityLoader, - IContextSource $context = null ) { - + EntityLookup $entityLookup, + EntityTitleLookup $entityTitleLookup, + EntityIdFormatter $idFormatter + ) { + $this->setContext( $context ); $this->valueFormatters = $valueFormatters; $this->dataTypeLookup = $dataTypeLookup; - $this->entityLoader = $entityLoader; - - if ( !$context ) { - $context = \RequestContext::getMain(); - } - $this->setContext( $context ); - - // TODO: this need to be properly injected - $this->idFormatter = WikibaseRepo::getDefaultInstance()->getIdFormatter(); + $this->entityLookup = $entityLookup; + $this->entityTitleLookup = $entityTitleLookup; + $this->idFormatter = $idFormatter; } /** @@ -263,7 +273,7 @@ $contentFactory = EntityContentFactory::singleton(); foreach ( $usedEntityIds as $entityId ) { - $pout->addLink( $contentFactory->getTitleForId( $entityId ) ); + $pout->addLink( $this->entityTitleLookup->getTitleForId( $entityId ) ); } // treat URL values as external links ------ @@ -555,12 +565,12 @@ $propertyHtml = ''; $propertyId = $claims[0]->getMainSnak()->getPropertyId(); - $property = EntityContentFactory::singleton()->getFromId( $propertyId ); + $property = $this->entityLookup->getEntity( $propertyId ); $propertyLink = ''; - if ( isset( $property ) ) { + if ( $property ) { $propertyLink = \Linker::link( - $property->getTitle(), - htmlspecialchars( $property->getEntity()->getLabel( $languageCode ) ) + $this->entityTitleLookup->getTitleForId( $property->getId() ), + htmlspecialchars( $property->getLabel( $languageCode ) ) ); } @@ -883,17 +893,16 @@ protected function getBasicEntityInfo( array $entityIds, $langCode ) { wfProfileIn( __METHOD__ ); - $entityContentFactory = EntityContentFactory::singleton(); - $entities = $this->entityLoader->getEntities( $entityIds ); + $entities = $this->entityLookup->getEntities( $entityIds ); $entityInfo = array(); $serializerFactory = new SerializerFactory(); - // TODO: use injected id formatter - $serializationOptions = new EntitySerializationOptions( WikibaseRepo::getDefaultInstance()->getIdFormatter() ); + $serializationOptions = new EntitySerializationOptions( $this->idFormatter ); $serializationOptions->setProps( array( 'labels', 'descriptions', 'datatype' ) ); $serializationOptions->setLanguages( array( $langCode ) ); + /* @var Entity $entity */ foreach( $entities as $prefixedId => $entity ) { if( $entity === null ) { continue; @@ -901,12 +910,12 @@ $serializer = $serializerFactory->newSerializerForObject( $entity, $serializationOptions ); $entityInfo[ $prefixedId ] = $serializer->getSerialized( $entity ); - $entityContent = $entityContentFactory->getFromId( $entity->getId() ); + $title = $this->entityTitleLookup->getTitleForId( $entity->getId() ); // TODO: should perhaps implement and use a EntityContentSerializer since this is mixed, // serialized Entity and EntityContent data because of adding the URL: - $entityInfo[ $prefixedId ]['title'] = $entityContent->getTitle()->getPrefixedText(); - $entityInfo[ $prefixedId ]['lastrevid'] = $entityContent->getWikiPage()->getRevision()->getId(); + $entityInfo[ $prefixedId ]['title'] = $title->getPrefixedText(); + $entityInfo[ $prefixedId ]['lastrevid'] = $title->getLatestRevID(); } wfProfileOut( __METHOD__ ); @@ -921,7 +930,7 @@ * @param EntityContent $entity * @param ValueFormatterFactory $valueFormatters * @param Lib\PropertyDataTypeLookup $dataTypeLookup - * @param EntityLookup $entityLoader + * @param EntityLookup $entityLookup * @param IContextSource|null $context * * @throws \MWException @@ -931,7 +940,7 @@ EntityContent $entity, ValueFormatterFactory $valueFormatters, PropertyDataTypeLookup $dataTypeLookup, - EntityLookup $entityLoader, + EntityLookup $entityLookup, IContextSource $context = null ) { $type = $entity->getEntity()->getType(); @@ -940,11 +949,14 @@ throw new MWException( "No entity view known for handling entities of type '$type'" ); } - $instance = new self::$typeMap[ $type ]( - $valueFormatters, - $dataTypeLookup, - $entityLoader, - $context ); + if ( !$context ) { + $context = \RequestContext::getMain(); + } + + $idFormatter = WikibaseRepo::getDefaultInstance()->getIdFormatter(); + $entityTitleLookup = EntityContentFactory::singleton(); + + $instance = new self::$typeMap[ $type ]( $context, $valueFormatters, $dataTypeLookup, $entityLookup, $entityTitleLookup, $idFormatter ); return $instance; } } diff --git a/repo/includes/specials/SpecialItemResolver.php b/repo/includes/specials/SpecialItemResolver.php index 029739e..6ebf92f 100644 --- a/repo/includes/specials/SpecialItemResolver.php +++ b/repo/includes/specials/SpecialItemResolver.php @@ -108,7 +108,7 @@ protected function displayItem( Wikibase\ItemContent $itemContent ) { $valueFormatters = new ValueFormatterFactory( $GLOBALS['wgValueFormatters'] ); - $view = new Wikibase\ItemView( $valueFormatters, $this->getContext() ); + $view = \Wikibase\EntityView::newForEntityContent( $itemContent, $valueFormatters, $this->getContext() ); $view->render( $itemContent ); $this->getOutput()->setPageTitle( $itemContent->getItem()->getLabel( $this->getLanguage()->getCode() ) ); -- To view, visit https://gerrit.wikimedia.org/r/75886 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I34717dedd7095e64c32728868a8c0b06af40475c Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Daniel Werner <daniel.wer...@wikimedia.de> Gerrit-Reviewer: Henning Snater <henning.sna...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Liangent <liang...@gmail.com> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits