jenkins-bot has submitted this change and it was merged.

Change subject: Use EntityDocument in ModifyEntity and derivatives
......................................................................


Use EntityDocument in ModifyEntity and derivatives

Change-Id: Ib27173a2e2c0c74696bcb04b9210b92e8868b1cb
---
M repo/includes/api/EditEntity.php
M repo/includes/api/EntitySavingHelper.php
M repo/includes/api/ModifyEntity.php
M repo/includes/api/SetAliases.php
M repo/includes/api/SetDescription.php
M repo/includes/api/SetLabel.php
M repo/includes/api/SetSiteLink.php
7 files changed, 87 insertions(+), 37 deletions(-)

Approvals:
  Jeroen De Dauw: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/includes/api/EditEntity.php b/repo/includes/api/EditEntity.php
index 007fcdd..48e9966 100644
--- a/repo/includes/api/EditEntity.php
+++ b/repo/includes/api/EditEntity.php
@@ -16,15 +16,15 @@
 use Wikibase\ChangeOp\FingerprintChangeOpFactory;
 use Wikibase\ChangeOp\SiteLinkChangeOpFactory;
 use Wikibase\ChangeOp\StatementChangeOpFactory;
-use Wikibase\DataModel\Entity\Entity;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\Property;
-use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\DataModel\Statement\StatementListProvider;
 use Wikibase\DataModel\Term\FingerprintProvider;
+use Wikibase\EntityFactory;
 use Wikibase\Lib\ContentLanguages;
 use Wikibase\Lib\Store\EntityRevisionLookup;
 use Wikibase\Repo\WikibaseRepo;
@@ -86,6 +86,11 @@
        private $statementDeserializer;
 
        /**
+        * @var EntityFactory
+        */
+       private $entityFactory;
+
+       /**
         * @see ModifyEntity::__construct
         *
         * @param ApiMain $mainModule
@@ -104,6 +109,7 @@
                $this->revisionLookup = $wikibaseRepo->getEntityRevisionLookup( 
'uncached' );
                $this->idParser = $wikibaseRepo->getEntityIdParser();
                $this->statementDeserializer = 
$wikibaseRepo->getStatementDeserializer();
+               $this->entityFactory = $wikibaseRepo->getEntityFactory();
 
                $changeOpFactoryProvider = 
$wikibaseRepo->getChangeOpFactoryProvider();
                $this->termChangeOpFactory = 
$changeOpFactoryProvider->getFingerprintChangeOpFactory();
@@ -168,14 +174,13 @@
         *
         * @throws UsageException
         * @throws LogicException
-        * @return Entity
+        * @return EntityDocument
         */
        protected function createEntity( $entityType ) {
                $this->flags |= EDIT_NEW;
-               $entityFactory = 
WikibaseRepo::getDefaultInstance()->getEntityFactory();
 
                try {
-                       return $entityFactory->newEmpty( $entityType );
+                       return $this->entityFactory->newEmpty( $entityType );
                } catch ( InvalidArgumentException $ex ) {
                        $this->errorReporter->dieError( "No such entity type: 
'$entityType'", 'no-such-entity-type' );
                }
@@ -218,7 +223,7 @@
        /**
         * @see ModifyEntity::modifyEntity
         */
-       protected function modifyEntity( Entity &$entity, array $params, 
$baseRevId ) {
+       protected function modifyEntity( EntityDocument &$entity, array 
$params, $baseRevId ) {
                $this->validateDataParameter( $params );
                $data = json_decode( $params['data'], true );
                $this->validateDataProperties( $data, $entity, $baseRevId );
@@ -239,7 +244,8 @@
                                        );
                                }
                        }
-                       $entity->clear();
+
+                       $entity = $this->clearEntity( $entity );
                }
 
                // if we create a new property, make sure we set the datatype
@@ -257,6 +263,24 @@
 
                $this->buildResult( $entity );
                return $this->getSummary( $params );
+       }
+
+       /**
+        * @param EntityDocument $entity
+        *
+        * @return EntityDocument
+        */
+       private function clearEntity( EntityDocument $entity ) {
+               $newEntity = $this->entityFactory->newEmpty( $entity->getType() 
);
+               $newEntity->setId( $entity->getId() );
+
+               // FIXME how to avoid special case handling here?
+               if ( $entity instanceof Property ) {
+                       /** @var Property $newEntity */
+                       $newEntity->setDataTypeId( $entity->getDataTypeId() );
+               }
+
+               return $newEntity;
        }
 
        /**
@@ -771,7 +795,7 @@
                                        self::PARAM_DFLT => false
                                ),
                                'new' => array(
-                                       self::PARAM_TYPE => 
WikibaseRepo::getDefaultInstance()->getEntityFactory()->getEntityTypes(),
+                                       self::PARAM_TYPE => 
$this->entityFactory->getEntityTypes(),
                                ),
                        )
                );
diff --git a/repo/includes/api/EntitySavingHelper.php 
b/repo/includes/api/EntitySavingHelper.php
index 83d5449..d997529 100644
--- a/repo/includes/api/EntitySavingHelper.php
+++ b/repo/includes/api/EntitySavingHelper.php
@@ -6,7 +6,7 @@
 use LogicException;
 use Status;
 use UsageException;
-use Wikibase\DataModel\Entity\Entity;
+use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\EditEntity as EditEntityHandler;
 use Wikibase\EditEntityFactory;
 use Wikibase\Summary;
@@ -67,7 +67,7 @@
         * warnings, they will automatically be included in the API call's 
output (again, via
         * handleStatus()).
         *
-        * @param Entity $entity The entity to save
+        * @param EntityDocument $entity The entity to save
         * @param string|Summary $summary The edit summary
         * @param int $flags The edit flags (see WikiPage::doEditContent)
         *
@@ -75,7 +75,7 @@
         * @return Status the status of the save operation, as returned by 
EditEntityHandler::attemptSave()
         * @see  EditEntityHandler::attemptSave()
         */
-       public function attemptSaveEntity( Entity $entity, $summary, $flags = 0 
) {
+       public function attemptSaveEntity( EntityDocument $entity, $summary, 
$flags = 0 ) {
                if ( !$this->apiBase->isWriteMode() ) {
                        // sanity/safety check
                        throw new LogicException(
diff --git a/repo/includes/api/ModifyEntity.php 
b/repo/includes/api/ModifyEntity.php
index 9d14028..eb41031 100644
--- a/repo/includes/api/ModifyEntity.php
+++ b/repo/includes/api/ModifyEntity.php
@@ -12,12 +12,11 @@
 use Wikibase\ChangeOp\ChangeOp;
 use Wikibase\ChangeOp\ChangeOpException;
 use Wikibase\ChangeOp\ChangeOpValidationException;
-use Wikibase\DataModel\Entity\Entity;
 use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\EntityId;
-use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Entity\EntityIdParsingException;
+use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\DataModel\Services\Lookup\EntityLookupException;
 use Wikibase\EntityRevision;
 use Wikibase\Lib\Store\EntityRevisionLookup;
@@ -162,7 +161,7 @@
        /**
         * @see EntitySavingHelper::attemptSaveEntity
         */
-       private function attemptSaveEntity( Entity $entity, $summary, $flags = 
0 ) {
+       private function attemptSaveEntity( EntityDocument $entity, $summary, 
$flags = 0 ) {
                return $this->entitySavingHelper->attemptSaveEntity( $entity, 
$summary, $flags );
        }
 
@@ -321,7 +320,7 @@
         *
         * @param string $entityType
         *
-        * @return Entity Newly created entity
+        * @return EntityDocument Newly created entity
         */
        protected function createEntity( $entityType ) {
                $this->errorReporter->dieError( 'Could not find an existing 
entity', 'no-such-entity' );
@@ -345,13 +344,13 @@
         *
         * @since 0.1
         *
-        * @param Entity &$entity
+        * @param EntityDocument &$entity
         * @param array $params
         * @param int $baseRevId
         *
         * @return Summary|null a summary of the modification, or null to 
indicate failure.
         */
-       abstract protected function modifyEntity( Entity &$entity, array 
$params, $baseRevId );
+       abstract protected function modifyEntity( EntityDocument &$entity, 
array $params, $baseRevId );
 
        /**
         * Applies the given ChangeOp to the given Entity.
@@ -360,12 +359,12 @@
         * @since 0.5
         *
         * @param ChangeOp $changeOp
-        * @param Entity $entity
+        * @param EntityDocument $entity
         * @param Summary|null $summary The summary object to update with 
information about the change.
         *
         * @throws UsageException
         */
-       protected function applyChangeOp( ChangeOp $changeOp, Entity $entity, 
Summary $summary = null ) {
+       protected function applyChangeOp( ChangeOp $changeOp, EntityDocument 
$entity, Summary $summary = null ) {
                try {
                        $result = $changeOp->validate( $entity );
 
diff --git a/repo/includes/api/SetAliases.php b/repo/includes/api/SetAliases.php
index b0f1df8..5c29c4d 100644
--- a/repo/includes/api/SetAliases.php
+++ b/repo/includes/api/SetAliases.php
@@ -8,8 +8,8 @@
 use Wikibase\ChangeOp\ChangeOpAliases;
 use Wikibase\ChangeOp\ChangeOps;
 use Wikibase\ChangeOp\FingerprintChangeOpFactory;
-use Wikibase\DataModel\Entity\Entity;
 use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\FingerprintProvider;
 use Wikibase\Repo\WikibaseRepo;
 
 /**
@@ -99,16 +99,13 @@
        }
 
        /**
-        * @see ModifyEntity::createEntity
-        */
-       protected function createEntity( $entityType ) {
-               $this->errorReporter->dieError( 'Could not find an existing 
entity', 'no-such-entity' );
-       }
-
-       /**
         * @see ModifyEntity::modifyEntity
         */
-       protected function modifyEntity( Entity &$entity, array $params, 
$baseRevId ) {
+       protected function modifyEntity( EntityDocument &$entity, array 
$params, $baseRevId ) {
+               if ( !( $entity instanceof FingerprintProvider ) ) {
+                       $this->errorReporter->dieError( 'The given entity does 
not contain aliases', 'no-aliases' );
+               }
+
                $summary = $this->createSummary( $params );
                $language = $params['language'];
 
diff --git a/repo/includes/api/SetDescription.php 
b/repo/includes/api/SetDescription.php
index 69ce5f3..c64f33c 100644
--- a/repo/includes/api/SetDescription.php
+++ b/repo/includes/api/SetDescription.php
@@ -3,9 +3,11 @@
 namespace Wikibase\Repo\Api;
 
 use ApiMain;
+use InvalidArgumentException;
 use Wikibase\ChangeOp\ChangeOpDescription;
 use Wikibase\ChangeOp\FingerprintChangeOpFactory;
-use Wikibase\DataModel\Entity\Entity;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\FingerprintProvider;
 use Wikibase\Repo\WikibaseRepo;
 
 /**
@@ -27,6 +29,11 @@
        private $termChangeOpFactory;
 
        /**
+        * @var ApiErrorReporter
+        */
+       private $errorReporter;
+
+       /**
         * @param ApiMain $mainModule
         * @param string $moduleName
         * @param string $modulePrefix
@@ -34,14 +41,22 @@
        public function __construct( ApiMain $mainModule, $moduleName, 
$modulePrefix = '' ) {
                parent::__construct( $mainModule, $moduleName, $modulePrefix );
 
-               $changeOpFactoryProvider = 
WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider();
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+               $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( 
$this->getContext() );
+               $changeOpFactoryProvider = 
$wikibaseRepo->getChangeOpFactoryProvider();
+
+               $this->errorReporter = $apiHelperFactory->getErrorReporter( 
$this );
                $this->termChangeOpFactory = 
$changeOpFactoryProvider->getFingerprintChangeOpFactory();
        }
 
        /**
         * @see ModifyEntity::modifyEntity
         */
-       protected function modifyEntity( Entity &$entity, array $params, 
$baseRevId ) {
+       protected function modifyEntity( EntityDocument &$entity, array 
$params, $baseRevId ) {
+               if ( !( $entity instanceof FingerprintProvider ) ) {
+                       $this->errorReporter->dieError( 'The given entity does 
not contain descriptions', 'no-descriptions' );
+               }
+
                $summary = $this->createSummary( $params );
                $language = $params['language'];
 
diff --git a/repo/includes/api/SetLabel.php b/repo/includes/api/SetLabel.php
index 725abda..6ac651d 100644
--- a/repo/includes/api/SetLabel.php
+++ b/repo/includes/api/SetLabel.php
@@ -3,9 +3,11 @@
 namespace Wikibase\Repo\Api;
 
 use ApiMain;
+use InvalidArgumentException;
 use Wikibase\ChangeOp\ChangeOpLabel;
 use Wikibase\ChangeOp\FingerprintChangeOpFactory;
-use Wikibase\DataModel\Entity\Entity;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\FingerprintProvider;
 use Wikibase\Repo\WikibaseRepo;
 
 /**
@@ -27,6 +29,11 @@
        private $termChangeOpFactory;
 
        /**
+        * @var ApiErrorReporter
+        */
+       private $errorReporter;
+
+       /**
         * @param ApiMain $mainModule
         * @param string $moduleName
         * @param string $modulePrefix
@@ -34,14 +41,22 @@
        public function __construct( ApiMain $mainModule, $moduleName, 
$modulePrefix = '' ) {
                parent::__construct( $mainModule, $moduleName, $modulePrefix );
 
-               $changeOpFactoryProvider = 
WikibaseRepo::getDefaultInstance()->getChangeOpFactoryProvider();
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+               $apiHelperFactory = $wikibaseRepo->getApiHelperFactory( 
$this->getContext() );
+               $changeOpFactoryProvider = 
$wikibaseRepo->getChangeOpFactoryProvider();
+
+               $this->errorReporter = $apiHelperFactory->getErrorReporter( 
$this );
                $this->termChangeOpFactory = 
$changeOpFactoryProvider->getFingerprintChangeOpFactory();
        }
 
        /**
         * @see ModifyEntity::modifyEntity
         */
-       protected function modifyEntity( Entity &$entity, array $params, 
$baseRevId ) {
+       protected function modifyEntity( EntityDocument &$entity, array 
$params, $baseRevId ) {
+               if ( !( $entity instanceof FingerprintProvider ) ) {
+                       $this->errorReporter->dieError( 'The given entity does 
not contain labels', 'no-labels' );
+               }
+
                $summary = $this->createSummary( $params );
                $language = $params['language'];
 
diff --git a/repo/includes/api/SetSiteLink.php 
b/repo/includes/api/SetSiteLink.php
index 0b0931f..562f805 100644
--- a/repo/includes/api/SetSiteLink.php
+++ b/repo/includes/api/SetSiteLink.php
@@ -5,7 +5,7 @@
 use ApiMain;
 use Wikibase\ChangeOp\ChangeOpSiteLink;
 use Wikibase\ChangeOp\SiteLinkChangeOpFactory;
-use Wikibase\DataModel\Entity\Entity;
+use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\SiteLinkList;
 use Wikibase\Repo\WikibaseRepo;
@@ -96,7 +96,7 @@
                        $this->errorReporter->dieError(
                                'The content on the found page is not of 
correct type',
                                'wrong-class'
-                                );
+                       );
                }
 
                return $entityRev;
@@ -105,7 +105,7 @@
        /**
         * @see ModifyEntity::modifyEntity
         */
-       protected function modifyEntity( Entity &$entity, array $params, 
$baseRevId ) {
+       protected function modifyEntity( EntityDocument &$entity, array 
$params, $baseRevId ) {
                if ( !( $entity instanceof Item ) ) {
                        $this->errorReporter->dieError( "The given entity is 
not an item", "not-item" );
                }

-- 
To view, visit https://gerrit.wikimedia.org/r/268970
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib27173a2e2c0c74696bcb04b9210b92e8868b1cb
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to