jenkins-bot has submitted this change and it was merged.
Change subject: (bug 57231) Use ResultBuilder for EntityData.
......................................................................
(bug 57231) Use ResultBuilder for EntityData.
This changes the EntityData facility to make use of the ResultBuilder
class for constructing API results. This ensures that the generated
output is the same as returned by the API.
Incidentally, this also causes each snak's data type to be included
in the output.
Change-Id: Ib7391bebf5bf62f947b81ac5cf233467862196be
---
M repo/includes/LinkedData/EntityDataSerializationService.php
M repo/includes/api/ResultBuilder.php
M repo/includes/specials/SpecialEntityData.php
M repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
M repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
5 files changed, 102 insertions(+), 61 deletions(-)
Approvals:
Addshore: Looks good to me, approved
jenkins-bot: Verified
diff --git a/repo/includes/LinkedData/EntityDataSerializationService.php
b/repo/includes/LinkedData/EntityDataSerializationService.php
index 2ff4bb5..eb8e267 100644
--- a/repo/includes/LinkedData/EntityDataSerializationService.php
+++ b/repo/includes/LinkedData/EntityDataSerializationService.php
@@ -2,8 +2,9 @@
namespace Wikibase\LinkedData;
-use Wikibase\Entity;
+use Wikibase\Api\ResultBuilder;
use Wikibase\EntityLookup;
+use Wikibase\EntityRevision;
use MWException;
use EasyRdf_Format;
use ApiFormatBase;
@@ -13,9 +14,8 @@
use DerivativeContext;
use DerivativeRequest;
use RequestContext;
-use Wikibase\EntityRevision;
+use Wikibase\EntityTitleLookup;
use Wikibase\Lib\Serializers\SerializationOptions;
-use Wikibase\Lib\Serializers\EntitySerializer;
use Wikibase\Lib\Serializers\SerializerFactory;
use Wikibase\RdfSerializer;
@@ -88,22 +88,39 @@
protected $fileExtensions = null;
/**
+ * @var EntityTitleLookup
+ */
+ protected $entityTitleLookup;
+
+ /**
+ * @var SerializerFactory
+ */
+ protected $serializerFactory;
+
+ /**
* Constructor.
*
* @param string $rdfBaseURI
* @param string $rdfDataURI
* @param EntityLookup $entityLookup
*
+ * @param EntityTitleLookup $entityTitleLookup
+ * @param SerializerFactory $serializerFactory
+ *
* @since 0.4
*/
public function __construct(
$rdfBaseURI,
$rdfDataURI,
- EntityLookup $entityLookup
+ EntityLookup $entityLookup,
+ EntityTitleLookup $entityTitleLookup,
+ SerializerFactory $serializerFactory
) {
$this->rdfBaseURI = $rdfBaseURI;
$this->rdfDataURI = $rdfDataURI;
$this->entityLookup = $entityLookup;
+ $this->entityTitleLookup = $entityTitleLookup;
+ $this->serializerFactory = $serializerFactory;
}
/**
@@ -291,7 +308,7 @@
$this->fileExtensions[ $ext ] = $name;
}
- if ( \Wikibase\RdfSerializer::isSupported() ) {
+ if ( RdfSerializer::isSupported() ) {
$formats = EasyRdf_Format::getFormats();
/* @var EasyRdf_Format $format */
@@ -461,7 +478,7 @@
*/
public function createRdfSerializer( $format ) {
//MediaWiki formats
- $rdfFormat = \Wikibase\RdfSerializer::getFormat( $format );
+ $rdfFormat = RdfSerializer::getFormat( $format );
if ( !$rdfFormat ) {
return null;
@@ -483,18 +500,17 @@
* result, if $printer was generated from that same ApiMain module, as
* createApiPrinter() does.
*
- * @param Entity $entity The entity to convert ot an ApiResult
+ * @param EntityRevision $entityRevision The entity to convert ot an
ApiResult
* @param ApiFormatBase $printer The output printer that will be used
for serialization.
* Used to provide context for generating the ApiResult, and may also
be manipulated
* to fine-tune the output.
*
* @return ApiResult
*/
- protected function generateApiResult( Entity $entity, ApiFormatBase
$printer ) {
+ protected function generateApiResult( EntityRevision $entityRevision,
ApiFormatBase $printer ) {
wfProfileIn( __METHOD__ );
$entityKey = 'entity'; //XXX: perhaps better:
$entity->getType();
- $basePath = array();
$res = $printer->getResult();
@@ -503,6 +519,7 @@
$res->reset();
if ( $printer->getNeedsRawData() ) {
+ // force raw mode, to trigger indexed tag names
$res->setRawMode();
}
@@ -511,18 +528,11 @@
$printer->setRootElement( $entityKey );
}
- $serializerFactory = new SerializerFactory();
- $serializationOptions = new SerializationOptions();
- $serializationOptions->setIndexTags( $res->getIsRawMode() );
- $serializationOptions->setOption( EntitySerializer::OPT_PARTS,
$this->fieldsToShow );
- $serializer = $serializerFactory->newSerializerForObject(
$entity, $serializationOptions );
+ //TODO: apply language filter/Fallback via options!
+ $options = new SerializationOptions();
- $arr = $serializer->getSerialized( $entity );
-
- // we want the entity to *be* the result, not *in* the result
- foreach ( $arr as $key => $value ) {
- $res->addValue( $basePath, $key, $value );
- }
+ $resultBuilder = new ResultBuilder( $res,
$this->entityTitleLookup, $this->serializerFactory );
+ $resultBuilder->addEntityRevision( $entityRevision, $options );
wfProfileOut( __METHOD__ );
return $res;
@@ -546,20 +556,13 @@
// is owned by the ApiMain module provided by
newApiMain().
// Pushes $entity into the ApiResult held by the ApiMain module
- $res = $this->generateApiResult( $entityRevision->getEntity(),
$printer );
-
-
- //XXX: really inject meta-info? where else should we put it?
- $basePath = array();
-
- $res->addValue( $basePath , '_revision_', intval(
$entityRevision->getRevision() ) );
- $res->addValue( $basePath , '_modified_', wfTimestamp(
TS_ISO_8601, $entityRevision->getTimestamp() ) );
+ $this->generateApiResult( $entityRevision, $printer );
$printer->profileIn();
$printer->initPrinter( false );
$printer->setBufferResult( true );
- // Outputs the ApiResult held by the ApiMain module, which is
hopefully the same as $res
+ // Outputs the ApiResult held by the ApiMain module, which is
hopefully the one we added the entity data to.
//NOTE: this can and will mess with the HTTP response!
$printer->execute();
$data = $printer->getBuffer();
diff --git a/repo/includes/api/ResultBuilder.php
b/repo/includes/api/ResultBuilder.php
index 850c4df..c46f671 100644
--- a/repo/includes/api/ResultBuilder.php
+++ b/repo/includes/api/ResultBuilder.php
@@ -252,9 +252,9 @@
*
* @param EntityRevision $entityRevision
* @param SerializationOptions|null $options
- * @param array $props
+ * @param array|string $props a list of fields to include, or "all"
*/
- public function addEntityRevision( EntityRevision $entityRevision,
SerializationOptions $options = null, array $props = array() ) {
+ public function addEntityRevision( EntityRevision $entityRevision,
SerializationOptions $options = null, $props = 'all' ) {
$entity = $entityRevision->getEntity();
$entityId = $entity->getId();
$record = array();
@@ -272,7 +272,7 @@
$record['id'] = $entityId->getSerialization();
$record['type'] = $entityId->getEntityType();
} else {
- if ( in_array( 'info', $props ) ) {
+ if ( $props == 'all' || in_array( 'info', $props ) ) {
$title =
$this->entityTitleLookup->getTitleForId( $entityId );
$record['pageid'] = $title->getArticleID();
$record['ns'] = intval( $title->getNamespace()
);
@@ -281,8 +281,9 @@
$record['modified'] = wfTimestamp( TS_ISO_8601,
$entityRevision->getTimestamp() );
}
- $serializerFactory = new SerializerFactory();
- $entitySerializer =
$serializerFactory->newSerializerForObject( $entity, $serializerOptions );
+ //FIXME: $props should be used to filter
$entitySerialization!
+ // as in, $entitySerialization = array_intersect_key(
$entitySerialization, array_flip( $props ) )
+ $entitySerializer =
$this->serializerFactory->newSerializerForObject( $entity, $options );
$entitySerialization =
$entitySerializer->getSerialized( $entity );
$record = array_merge( $record, $entitySerialization );
diff --git a/repo/includes/specials/SpecialEntityData.php
b/repo/includes/specials/SpecialEntityData.php
index 79476c9..858c73e 100644
--- a/repo/includes/specials/SpecialEntityData.php
+++ b/repo/includes/specials/SpecialEntityData.php
@@ -3,12 +3,14 @@
namespace Wikibase\Repo\Specials;
use HttpError;
+use Wikibase\EntityFactory;
+use Wikibase\Lib\Serializers\SerializationOptions;
+use Wikibase\Lib\Serializers\SerializerFactory;
use Wikibase\Lib\Specials\SpecialWikibasePage;
use Wikibase\LinkedData\EntityDataRequestHandler;
use Wikibase\LinkedData\EntityDataSerializationService;
use Wikibase\LinkedData\EntityDataUriManager;
use Wikibase\Repo\WikibaseRepo;
-use Wikibase\Settings;
use Wikibase\StoreFactory;
/**
@@ -30,7 +32,7 @@
/**
* @var EntityDataRequestHandler
*/
- protected $requestHandler;
+ protected $requestHandler = null;
/**
* Constructor.
@@ -48,23 +50,29 @@
protected function initDependencies() {
global $wgUseSquid, $wgApiFrameOptions;
- // Initialize serialization service.
- // TODO: use reverse DI facility (global registry/factory)
$repo = WikibaseRepo::getDefaultInstance();
- $entityTitleLookup = $repo->getEntityTitleLookup();
- $entityRevisionLookup = $repo->getEntityRevisionLookup();
- $entityContentFactory = $repo->getEntityContentFactory();
+ $entityRevisionLookup = $repo->getEntityRevisionLookup();
+ $titleLookup = $repo->getEntityTitleLookup();
$entityIdParser = $repo->getEntityIdParser();
+
+ $serializationOptions = new SerializationOptions();
+ $serializerFactory = new SerializerFactory(
+ $serializationOptions,
+ $repo->getPropertyDataTypeLookup(),
+ EntityFactory::singleton()
+ );
$serializationService = new EntityDataSerializationService(
$repo->getRdfBaseURI(),
$this->getPageTitle()->getCanonicalURL() . '/',
- StoreFactory::getStore()->getEntityLookup()
+ $repo->getStore()->getEntityLookup(),
+ $titleLookup,
+ $serializerFactory
);
- $maxAge = Settings::get( 'dataSquidMaxage' );
- $formats = Settings::get( 'entityDataFormats' );
+ $maxAge = $repo->getSettings()->getSetting( 'dataSquidMaxage' );
+ $formats = $repo->getSettings()->getSetting(
'entityDataFormats' );
$serializationService->setFormatWhiteList( $formats );
$defaultFormat = empty( $formats ) ? 'html' : $formats[0];
@@ -83,12 +91,12 @@
$uriManager = new EntityDataUriManager(
$this->getPageTitle(),
$supportedExtensions,
- $entityContentFactory
+ $titleLookup
);
$this->requestHandler = new EntityDataRequestHandler(
$uriManager,
- $entityTitleLookup,
+ $titleLookup,
$entityRevisionLookup,
$entityIdParser,
$serializationService,
diff --git
a/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
b/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
index 8a166b9..25cc9b9 100644
--- a/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
+++ b/repo/tests/phpunit/includes/LinkedData/EntityDataRequestHandlerTest.php
@@ -7,13 +7,13 @@
use OutputPage;
use RequestContext;
use Title;
-use ValueFormatters\FormatterOptions;
use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\DataModel\Entity\EntityId;
use Wikibase\Entity;
-use Wikibase\EntityContentFactory;
use Wikibase\Item;
use Wikibase\ItemContent;
-use Wikibase\Lib\EntityIdFormatter;
+use Wikibase\Lib\Serializers\SerializationOptions;
+use Wikibase\Lib\Serializers\SerializerFactory;
use Wikibase\LinkedData\EntityDataSerializationService;
use Wikibase\LinkedData\EntityDataRequestHandler;
use Wikibase\LinkedData\EntityDataUriManager;
@@ -75,19 +75,27 @@
$idParser = new BasicEntityIdParser(); // we only test for
items and properties here.
- //TODO: get rid of the dependency on EntityContentFactory!
- $contentFactory = new EntityContentFactory(
- new EntityIdFormatter( new FormatterOptions() ),
- array(
- CONTENT_MODEL_WIKIBASE_ITEM,
- CONTENT_MODEL_WIKIBASE_PROPERTY
- )
- );
+ $dataTypeLookup = $this->getMock(
'Wikibase\Lib\PropertyDataTypeLookup' );
+ $dataTypeLookup->expects( $this->any() )
+ ->method( 'getDataTypeIdForProperty' )
+ ->will( $this->returnValue( 'string' ) );
+
+ $titleLookup = $this->getMock( 'Wikibase\EntityTitleLookup' );
+ $titleLookup->expects( $this->any() )
+ ->method( 'getTitleForId' )
+ ->will( $this->returnCallback( function( EntityId $id )
{
+ return Title::newFromText( $id->getEntityType()
. ':' . $id->getSerialization() );
+ } ) );
+
+ $serializerOptions = new SerializationOptions();
+ $serializerFactory = new SerializerFactory( $serializerOptions,
$dataTypeLookup );
$service = new EntityDataSerializationService(
EntityDataSerializationServiceTest::URI_BASE,
EntityDataSerializationServiceTest::URI_DATA,
- $entityLookup
+ $entityLookup,
+ $titleLookup,
+ $serializerFactory
);
$service->setFormatWhiteList(
@@ -121,12 +129,12 @@
$uriManager = new EntityDataUriManager(
$this->interfaceTitle,
$extensions,
- $contentFactory
+ $titleLookup
);
$handler = new EntityDataRequestHandler(
$uriManager,
- $contentFactory,
+ $titleLookup,
$entityLookup,
$idParser,
$service,
diff --git
a/repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
b/repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
index fdd2d63..fc3e182 100644
---
a/repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
+++
b/repo/tests/phpunit/includes/LinkedData/EntityDataSerializationServiceTest.php
@@ -2,9 +2,13 @@
namespace Wikibase\Test;
+use Title;
+use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\EntityRevision;
use Wikibase\Item;
+use Wikibase\Lib\Serializers\SerializationOptions;
+use Wikibase\Lib\Serializers\SerializerFactory;
use Wikibase\LinkedData\EntityDataSerializationService;
/**
@@ -27,10 +31,27 @@
protected function newService() {
$entityLookup = new MockRepository();
+ $dataTypeLookup = $this->getMock(
'Wikibase\Lib\PropertyDataTypeLookup' );
+ $dataTypeLookup->expects( $this->any() )
+ ->method( 'getDataTypeIdForProperty' )
+ ->will( $this->returnValue( 'string' ) );
+
+ $titleLookup = $this->getMock( 'Wikibase\EntityTitleLookup' );
+ $titleLookup->expects( $this->any() )
+ ->method( 'getTitleForId' )
+ ->will( $this->returnCallback( function( EntityId $id )
{
+ return Title::newFromText( $id->getEntityType()
. ':' . $id->getSerialization() );
+ } ) );
+
+ $serializerOptions = new SerializationOptions();
+ $serializerFactory = new SerializerFactory( $serializerOptions,
$dataTypeLookup );
+
$service = new EntityDataSerializationService(
self::URI_BASE,
self::URI_DATA,
- $entityLookup
+ $entityLookup,
+ $titleLookup,
+ $serializerFactory
);
$service->setFormatWhiteList(
--
To view, visit https://gerrit.wikimedia.org/r/101855
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib7391bebf5bf62f947b81ac5cf233467862196be
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits