Jeroen De Dauw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/53367


Change subject: Added extra SetClaim tests and split of non-api code to own 
class
......................................................................

Added extra SetClaim tests and split of non-api code to own class

Change-Id: I0bf92351294c61ac92865fbe67229c3964fbe523
---
M repo/includes/api/SetClaim.php
M repo/tests/phpunit/includes/api/SetClaimTest.php
M repo/tests/phpunit/includes/api/SetClaimValueTest.php
3 files changed, 248 insertions(+), 102 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/67/53367/1

diff --git a/repo/includes/api/SetClaim.php b/repo/includes/api/SetClaim.php
index 7a06131..d9e8dde 100644
--- a/repo/includes/api/SetClaim.php
+++ b/repo/includes/api/SetClaim.php
@@ -49,48 +49,30 @@
        public function execute() {
                $claim = $this->getClaimFromRequest();
 
-               $entityId = $this->getEntityIdForClaim( $claim );
+               $claimSetter = new ClaimSetter();
 
-               $content = $this->getEntityContent( $entityId );
+               $params = $this->extractRequestParams();
+               $baseRevisionId = isset( $params['baserevid'] ) ? intval( 
$params['baserevid'] ) : null;
+               $token = isset( $params['token'] ) ? $params['token'] : '';
 
-               $this->setClaim( $content->getEntity(), $claim );
-
-               $this->saveChanges( $content );
-
-               $this->outputClaim( $claim );
-       }
-
-       /**
-        * @param Claim $claim
-        *
-        * @return EntityId
-        */
-       protected function getEntityIdForClaim( Claim $claim ) {
-               $guid = $claim->getGuid();
-
-               if ( $guid === null ) {
-                       $this->dieUsage( 'The ID of the claim needs to be set', 
'setclaim-no-guid' );
-               }
+               $newRevisionId = null;
 
                try {
-                       $entityId = Entity::getIdFromClaimGuid( $guid );
+                       $newRevisionId = $claimSetter->setClaim( $claim, 
$baseRevisionId, $token, $this->getUser() );
                }
-               catch ( MWException $exception ) {
-                       $this->dieUsage( $exception->getMessage(), 
'setclaim-invalid-guid' );
+               catch ( ExceptionWithCode $exception ) {
+                       $this->dieUsage( $exception->getMessage(), 
$exception->getErrorCode() );
                }
 
-               $libRegistry = new \Wikibase\LibRegistry( 
\Wikibase\Settings::singleton() );
-               $idParser = $libRegistry->getEntityIdParser();
-
-               $parseResult = $idParser->parse( $entityId );
-
-               if ( $parseResult->isValid() ) {
-                       $entityId = $parseResult->getValue();
-                       assert( $entityId instanceof EntityId );
-                       return $entityId;
+               if ( $newRevisionId !== null ) {
+                       $this->getResult()->addValue(
+                               'pageinfo',
+                               'lastrevid',
+                               $newRevisionId
+                       );
                }
 
-               $this->dieUsage( $parseResult->getError()->getText(), 
'setclaim-invalid-guid' );
+               $this->outputClaim( $claim );
        }
 
        /**
@@ -108,72 +90,6 @@
                assert( $claim instanceof Claim );
 
                return $claim;
-       }
-
-       /**
-        * @since 0.4
-        *
-        * @param Entity $entity
-        * @param Claim $claim
-        */
-       protected function setClaim( Entity $entity, Claim $claim ) {
-               $claims = new \Wikibase\Claims( $entity->getClaims() );
-               $claims->addClaim( $claim );
-               $entity->setClaims( $claims );
-       }
-
-       /**
-        * @since 0.4
-        *
-        * @param EntityId $entityId
-        *
-        * @return EntityContent
-        */
-       protected function getEntityContent( EntityId $entityId ) {
-               $params = $this->extractRequestParams();
-
-               $entityTitle = 
EntityContentFactory::singleton()->getTitleForId( $entityId );
-
-               if ( $entityTitle === null ) {
-                       $this->dieUsage( 'No such entity', 
'setclaim-entity-not-found' );
-               }
-
-               $baseRevisionId = isset( $params['baserevid'] ) ? intval( 
$params['baserevid'] ) : null;
-
-               return $this->loadEntityContent( $entityTitle, $baseRevisionId 
);
-       }
-
-       /**
-        * @since 0.4
-        *
-        * @param EntityContent $content
-        */
-       protected function saveChanges( EntityContent $content ) {
-               $params = $this->extractRequestParams();
-
-               $baseRevisionId = isset( $params['baserevid'] ) ? intval( 
$params['baserevid'] ) : null;
-               $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
-               $editEntity = new \Wikibase\EditEntity( $content, 
$this->getUser(), $baseRevisionId, $this->getContext() );
-
-               $status = $editEntity->attemptSave(
-                       '', // TODO: automcomment
-                       EDIT_UPDATE,
-                       isset( $params['token'] ) ? $params['token'] : ''
-               );
-
-               if ( !$status->isGood() ) {
-                       $this->dieUsage( $status->getMessage(), 
'setclaim-save-failed' );
-               }
-
-               $statusValue = $status->getValue();
-
-               if ( isset( $statusValue['revision'] ) ) {
-                       $this->getResult()->addValue(
-                               'pageinfo',
-                               'lastrevid',
-                               (int)$statusValue['revision']->getId()
-                       );
-               }
        }
 
        /**
@@ -280,3 +196,226 @@
        }
 
 }
+
+/**
+ * Exception with a string error code.
+ *
+ * TODO: to own file
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @ingroup WikibaseRepo
+ * @ingroup API
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class ExceptionWithCode extends \Exception {
+
+       /**
+        * @var string
+        */
+       private $stringCode;
+
+       /**
+        * @param string $message
+        * @param string $code
+        */
+       public function __construct( $message, $code ) {
+               parent::__construct( $message );
+               $this->stringCode = $code;
+       }
+
+       /**
+        * @return string
+        */
+       public function getErrorCode() {
+               return $this->stringCode;
+       }
+
+}
+
+use User;
+
+/**
+ * Class for updating a claim in the primary storage.
+ *
+ * TODO: to own file
+ * FIXME: entity content fetching pulls in global factory
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class ClaimSetter {
+
+       /**
+        * @see ApiBase::execute
+        *
+        * @since 0.4
+        *
+        * @param Claim $claim
+        * @param int|null $baseRevId
+        * @param string $token
+        * @param User $user
+        *
+        * @return int
+        */
+       public function setClaim( Claim $claim, $baseRevId, $token, User $user 
) {
+               $entityId = $this->getEntityIdForClaim( $claim );
+
+               $content = $this->getEntityContent( $entityId, $baseRevId );
+
+               $this->updateClaim( $content->getEntity(), $claim );
+
+               $newRevisionId = $this->saveChanges( $content, $baseRevId, 
$token, $user );
+
+               return $newRevisionId;
+       }
+
+       /**
+        * @param Claim $claim
+        *
+        * @return EntityId
+        * @throws ExceptionWithCode
+        */
+       protected function getEntityIdForClaim( Claim $claim ) {
+               $guid = $claim->getGuid();
+
+               if ( $guid === null ) {
+                       throw new ExceptionWithCode( 'The ID of the claim needs 
to be set', 'setclaim-no-guid' );
+               }
+
+               try {
+                       $entityId = Entity::getIdFromClaimGuid( $guid );
+               }
+               catch ( MWException $exception ) {
+                       throw new ExceptionWithCode( $exception->getMessage(), 
'setclaim-invalid-guid' );
+               }
+
+               $libRegistry = new \Wikibase\LibRegistry( 
\Wikibase\Settings::singleton() );
+               $idParser = $libRegistry->getEntityIdParser();
+
+               $parseResult = $idParser->parse( $entityId );
+
+               if ( $parseResult->isValid() ) {
+                       $entityId = $parseResult->getValue();
+                       assert( $entityId instanceof EntityId );
+                       return $entityId;
+               }
+
+               throw new ExceptionWithCode( 
$parseResult->getError()->getText(), 'setclaim-invalid-guid' );
+       }
+
+       /**
+        * @since 0.4
+        *
+        * @param Entity $entity
+        * @param Claim $claim
+        */
+       protected function updateClaim( Entity $entity, Claim $claim ) {
+               $claims = new \Wikibase\Claims( $entity->getClaims() );
+
+               if ( $claims->hasClaimWithGuid( $claim->getGuid() ) ) {
+                       $claims->removeClaimWithGuid( $claim->getGuid() );
+               }
+
+               $claims->addClaim( $claim );
+
+               $entity->setClaims( $claims );
+       }
+
+       /**
+        * @since 0.4
+        *
+        * @param EntityId $entityId
+        * @param int|null $revisionId
+        *
+        * @return EntityContent
+        * @throws ExceptionWithCode
+        */
+       protected function getEntityContent( EntityId $entityId, $revisionId ) {
+               if ( $revisionId === null ) {
+                       $content = 
EntityContentFactory::singleton()->getFromId( $entityId );
+               }
+               else {
+                       $content = 
EntityContentFactory::singleton()->getFromRevision( $revisionId );
+               }
+
+               if ( $content === null ) {
+                       throw new ExceptionWithCode( 'No such entity', 
'setclaim-entity-not-found' );
+               }
+
+               if ( !$content->getEntity()->getId()->equals( $entityId ) ) {
+                       throw new ExceptionWithCode(
+                               'The provided revision belongs to the wrong 
entity',
+                               'setclaim-revision-wrong-entity'
+                       );
+               }
+
+               return $content;
+       }
+
+       /**
+        * @since 0.4
+        *
+        * @param EntityContent $content
+        * @param int|null $baseRevisionId
+        * @param string $token
+        * @param User $user
+        *
+        * @return int
+        * @throws ExceptionWithCode
+        */
+       protected function saveChanges( EntityContent $content, 
$baseRevisionId, $token, User $user ) {
+               $baseRevisionId = is_int( $baseRevisionId ) && $baseRevisionId 
> 0 ? $baseRevisionId : false;
+               $editEntity = new \Wikibase\EditEntity( $content, $user, 
$baseRevisionId );
+
+               $status = $editEntity->attemptSave(
+                       '', // TODO: automcomment
+                       EDIT_UPDATE,
+                       $token
+               );
+
+               if ( !$status->isGood() ) {
+                       throw new ExceptionWithCode( $status->getMessage(), 
'setclaim-save-failed' );
+               }
+
+               $statusValue = $status->getValue();
+               return (int)$statusValue['revision']->getId();
+       }
+
+}
\ No newline at end of file
diff --git a/repo/tests/phpunit/includes/api/SetClaimTest.php 
b/repo/tests/phpunit/includes/api/SetClaimTest.php
index 2fa2e03..242cbe3 100644
--- a/repo/tests/phpunit/includes/api/SetClaimTest.php
+++ b/repo/tests/phpunit/includes/api/SetClaimTest.php
@@ -103,17 +103,17 @@
                        $claim->setGuid( $guid );
 
                        // Addition request
-                       $this->makeRequest( $claim, $item->getId() );
+                       $this->makeRequest( $claim, $item->getId(), 1 );
 
                        $claim = new \Wikibase\Statement( new 
\Wikibase\PropertyNoValueSnak( 1234 ) );
                        $claim->setGuid( $guid );
 
                        // Update request
-                       $this->makeRequest( $claim, $item->getId() );
+                       $this->makeRequest( $claim, $item->getId(), 1 );
                }
        }
 
-       protected function makeRequest( Claim $claim, \Wikibase\EntityId 
$entityId ) {
+       protected function makeRequest( Claim $claim, \Wikibase\EntityId 
$entityId, $claimCount ) {
                $serializerFactory = new 
\Wikibase\Lib\Serializers\SerializerFactory();
                $serializer = $serializerFactory->newSerializerForObject( 
$claim );
 
@@ -132,6 +132,8 @@
                $claims = new \Wikibase\Claims( 
$content->getEntity()->getClaims() );
 
                $this->assertTrue( $claims->hasClaim( $claim ) );
+
+               $this->assertEquals( $claimCount, $claims->count() );
        }
 
        protected function makeValidRequest( array $params ) {
diff --git a/repo/tests/phpunit/includes/api/SetClaimValueTest.php 
b/repo/tests/phpunit/includes/api/SetClaimValueTest.php
index 4fbda90..2520708 100644
--- a/repo/tests/phpunit/includes/api/SetClaimValueTest.php
+++ b/repo/tests/phpunit/includes/api/SetClaimValueTest.php
@@ -101,6 +101,9 @@
        }
 
        public function doTestValidRequest( Entity $entity, $claimGuid, $value 
) {
+               $content = 
\Wikibase\EntityContentFactory::singleton()->getFromId( $entity->getId() );
+               $claimCount = count( $content->getEntity()->getClaims() );
+
                $params = array(
                        'action' => 'wbsetclaimvalue',
                        'claim' => $claimGuid,
@@ -123,6 +126,8 @@
 
                $claims = new \Wikibase\Claims( $obtainedEntity->getClaims() );
 
+               $this->assertEquals( $claimCount, $claims->count(), 'Claim 
count should not change after doing a setclaimvalue request' );
+
                $this->assertTrue( $claims->hasClaimWithGuid( $claimGuid ) );
 
                $dataValue = 
\DataValues\DataValueFactory::singleton()->newFromArray( 
$claim['mainsnak']['datavalue'] );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0bf92351294c61ac92865fbe67229c3964fbe523
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to