Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/294365
Change subject: Allow entities to be created from all API modules.
......................................................................
Allow entities to be created from all API modules.
With this, all API modules that derive from ModifyEntity can be used to
create entities, explicity (using the "new" parameter) or implicitly
(for entities that support custom ids).
Bug: T134259
Change-Id: Iee4f083c08d988974c1338615e28a04dd2ecf03b
---
M repo/includes/Api/EditEntity.php
M repo/includes/Api/ModifyEntity.php
M repo/tests/phpunit/includes/Api/SetAliasesTest.php
3 files changed, 68 insertions(+), 71 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/65/294365/1
diff --git a/repo/includes/Api/EditEntity.php b/repo/includes/Api/EditEntity.php
index d615e8b..390d8f7 100644
--- a/repo/includes/Api/EditEntity.php
+++ b/repo/includes/Api/EditEntity.php
@@ -6,7 +6,6 @@
use DataValues\IllegalValueException;
use Deserializers\Deserializer;
use InvalidArgumentException;
-use LogicException;
use MWException;
use SiteList;
use Title;
@@ -26,7 +25,6 @@
use Wikibase\DataModel\Term\AliasesProvider;
use Wikibase\DataModel\Term\DescriptionsProvider;
use Wikibase\DataModel\Term\LabelsProvider;
-use Wikibase\EntityFactory;
use Wikibase\Lib\ContentLanguages;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Repo\WikibaseRepo;
@@ -78,24 +76,9 @@
private $revisionLookup;
/**
- * @var EntityIdParser
- */
- private $idParser;
-
- /**
* @var Deserializer
*/
private $statementDeserializer;
-
- /**
- * @var EntityFactory
- */
- private $entityFactory;
-
- /**
- * @var string[]
- */
- private $enabledEntityTypes;
/**
* @see ModifyEntity::__construct
@@ -116,8 +99,6 @@
$this->revisionLookup = $wikibaseRepo->getEntityRevisionLookup(
'uncached' );
$this->idParser = $wikibaseRepo->getEntityIdParser();
$this->statementDeserializer =
$wikibaseRepo->getExternalFormatStatementDeserializer();
- $this->entityFactory = $wikibaseRepo->getEntityFactory();
- $this->enabledEntityTypes =
$wikibaseRepo->getEnabledEntityTypes();
$changeOpFactoryProvider =
$wikibaseRepo->getChangeOpFactoryProvider();
$this->termChangeOpFactory =
$changeOpFactoryProvider->getFingerprintChangeOpFactory();
@@ -173,51 +154,6 @@
private function entityExists( EntityId $entityId ) {
$title = $entityId === null ? null :
$this->getTitleLookup()->getTitleForId( $entityId );
return ( $title !== null && $title->exists() );
- }
-
- /**
- * Create an empty entity.
- *
- * @since 0.1
- *
- * @param string|null $entityType The type of entity to be created
(ignored if $id is given)
- * @param EntityId|null $id The ID of the entity to be created
(optional if $entityType is
- * given)
- *
- * @throws UsageException
- * @throws LogicException
- * @return EntityDocument Newly created entity
- */
- protected function createEntity( $entityType, EntityId $id = null ) {
- // TODO: pull this up into ModifyEntity.
-
- if ( $id ) {
- $entityType = $id->getEntityType();
- } elseif ( !$entityType ) {
- $this->errorReporter->dieError( "No entity type
provided for creation!", 'no-entity-type' );
- throw new LogicException( 'ApiErrorReporter::dieError
did not throw an exception' );
- }
-
- try {
- $entity = $this->entityFactory->newEmpty( $entityType );
- } catch ( InvalidArgumentException $ex ) {
- $this->errorReporter->dieError( "No such entity type:
'$entityType'", 'no-such-entity-type' );
- throw new LogicException( 'ApiErrorReporter::dieError
did not throw an exception' );
- }
-
- if ( $id !== null ) {
- if ( !$this->entityStore->canCreateWithCustomId( $id )
) {
- $this->errorReporter->dieError( "Cannot create
entity with ID: '$id'", 'bad-entity-id' );
- throw new LogicException(
'ApiErrorReporter::dieError did not throw an exception' );
- }
-
- $entity->setId( $id );
- } else {
- // NOTE: We need to assign an ID early, for things like
the ClaimIdGenerator.
- $this->entityStore->assignFreshId( $entity );
- }
-
- return $entity;
}
/**
@@ -842,9 +778,6 @@
'clear' => array(
self::PARAM_TYPE => 'boolean',
self::PARAM_DFLT => false
- ),
- 'new' => array(
- self::PARAM_TYPE =>
$this->enabledEntityTypes,
),
)
);
diff --git a/repo/includes/Api/ModifyEntity.php
b/repo/includes/Api/ModifyEntity.php
index 2b31b1d..c094fcf 100644
--- a/repo/includes/Api/ModifyEntity.php
+++ b/repo/includes/Api/ModifyEntity.php
@@ -18,6 +18,7 @@
use Wikibase\DataModel\Entity\EntityIdParsingException;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
+use Wikibase\EntityFactory;
use Wikibase\EntityRevision;
use Wikibase\Lib\Store\EntityRevisionLookup;
use Wikibase\Lib\Store\EntityStore;
@@ -107,7 +108,17 @@
/**
* @var EntityIdParser
*/
- private $idParser;
+ protected $idParser;
+
+ /**
+ * @var EntityFactory
+ */
+ protected $entityFactory;
+
+ /**
+ * @var string[]
+ */
+ private $enabledEntityTypes;
/**
* Flags to pass to EditEntity::attemptSave; use with the EDIT_XXX
constants.
@@ -139,6 +150,8 @@
$this->entitySavingHelper =
$apiHelperFactory->getEntitySavingHelper( $this );
$this->stringNormalizer = $wikibaseRepo->getStringNormalizer();
$this->idParser = $wikibaseRepo->getEntityIdParser();
+ $this->entityFactory = $wikibaseRepo->getEntityFactory();
+ $this->enabledEntityTypes =
$wikibaseRepo->getEnabledEntityTypes();
$this->setServices( new SiteLinkTargetProvider(
$wikibaseRepo->getSiteStore(),
@@ -327,7 +340,33 @@
* @return EntityDocument Newly created entity
*/
protected function createEntity( $entityType, EntityId $id = null ) {
- $this->errorReporter->dieError( 'Could not find an existing
entity', 'no-such-entity' );
+ if ( $id ) {
+ $entityType = $id->getEntityType();
+ } elseif ( !$entityType ) {
+ $this->errorReporter->dieError( "No entity type
provided for creation!", 'no-entity-type' );
+ throw new LogicException( 'ApiErrorReporter::dieError
did not throw an exception' );
+ }
+
+ try {
+ $entity = $this->entityFactory->newEmpty( $entityType );
+ } catch ( InvalidArgumentException $ex ) {
+ $this->errorReporter->dieError( "No such entity type:
'$entityType'", 'no-such-entity-type' );
+ throw new LogicException( 'ApiErrorReporter::dieError
did not throw an exception' );
+ }
+
+ if ( $id !== null ) {
+ if ( !$this->entityStore->canCreateWithCustomId( $id )
) {
+ $this->errorReporter->dieError( "Cannot create
entity with ID: '$id'", 'bad-entity-id' );
+ throw new LogicException(
'ApiErrorReporter::dieError did not throw an exception' );
+ }
+
+ $entity->setId( $id );
+ } else {
+ // NOTE: We need to assign an ID early, for things like
the ClaimIdGenerator.
+ $this->entityStore->assignFreshId( $entity );
+ }
+
+ return $entity;
}
/**
@@ -393,8 +432,9 @@
* @param array $params
*/
protected function validateParameters( array $params ) {
- // note that this is changed back and could fail
- if ( !( isset( $params['id'] ) xor ( isset( $params['site'] )
&& isset( $params['title'] ) ) ) ) {
+ if ( ( isset( $params['id'] ) || isset( $params['new'] ) )
+ === ( isset( $params['site'] ) && isset(
$params['title'] ) )
+ ) {
$this->errorReporter->dieError(
'Either provide the item "id" or pairs of
"site" and "title" for a corresponding page',
'param-illegal'
@@ -555,6 +595,9 @@
'id' => array(
self::PARAM_TYPE => 'string',
),
+ 'new' => array(
+ self::PARAM_TYPE => $this->enabledEntityTypes,
+ ),
);
}
diff --git a/repo/tests/phpunit/includes/Api/SetAliasesTest.php
b/repo/tests/phpunit/includes/Api/SetAliasesTest.php
index bd175f4..31a3fd5 100644
--- a/repo/tests/phpunit/includes/Api/SetAliasesTest.php
+++ b/repo/tests/phpunit/includes/Api/SetAliasesTest.php
@@ -35,6 +35,27 @@
self::$hasSetup = true;
}
+ public function testSetAliases_create() {
+ $params = array(
+ 'action' => self::$testAction,
+ 'new' => 'property',
+ 'language' => 'en',
+ 'set' => 'Foo',
+ );
+
+ // -- do the request
--------------------------------------------------
+ list( $result, , ) = $this->doApiRequestWithToken( $params );
+
+ // -- check the result
------------------------------------------------
+ $this->assertArrayHasKey( 'success', $result, "Missing
'success' marker in response." );
+ $this->assertResultHasEntityType( $result );
+ $this->assertArrayHasKey( 'entity', $result, "Missing 'entity'
section in response." );
+
+ $resAliases = $this->flattenArray(
$result['entity']['aliases'], 'language', 'value', true );
+ $this->assertArrayHasKey( $params['language'], $resAliases );
+ $this->assertArrayEquals( explode( '|', $params['set'] ),
$resAliases[ $params['language'] ] );
+ }
+
public function provideData() {
return array(
// p => params, e => expected
--
To view, visit https://gerrit.wikimedia.org/r/294365
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iee4f083c08d988974c1338615e28a04dd2ecf03b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits