John Erling Blad has submitted this change and it was merged.

Change subject: Moved ClaimSaver and ExceptionWithCode to their own files
......................................................................


Moved ClaimSaver and ExceptionWithCode to their own files

Change-Id: I266f251a243651836d095cee12eaf3ef39ddff87
---
M repo/Wikibase.php
A repo/includes/ClaimSaver.php
A repo/includes/ExceptionWithCode.php
M repo/includes/api/SetClaim.php
4 files changed, 242 insertions(+), 227 deletions(-)

Approvals:
  John Erling Blad: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index 47b7488..822c046 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -97,6 +97,7 @@
 // includes
 $wgAutoloadClasses['Wikibase\Autocomment']                             = $dir 
. 'includes/Autocomment.php';
 $wgAutoloadClasses['Wikibase\CachingEntityLoader']      = $dir . 
'includes/CachingEntityLoader.php';
+$wgAutoloadClasses['Wikibase\ClaimSaver']                              = $dir 
. 'includes/ClaimSaver.php';
 $wgAutoloadClasses['Wikibase\DataTypeSelector']                        = $dir 
. 'includes/DataTypeSelector.php';
 $wgAutoloadClasses['Wikibase\Repo\DBConnectionProvider']               = $dir 
. 'includes/DBConnectionProvider.php';
 $wgAutoloadClasses['Wikibase\EditEntity']                              = $dir 
. 'includes/EditEntity.php';
@@ -104,6 +105,7 @@
 $wgAutoloadClasses['Wikibase\ItemContentDiffView']             = $dir . 
'includes/ItemContentDiffView.php';
 $wgAutoloadClasses['Wikibase\ItemDisambiguation']              = $dir . 
'includes/ItemDisambiguation.php';
 $wgAutoloadClasses['Wikibase\EntityView']                              = $dir 
. 'includes/EntityView.php';
+$wgAutoloadClasses['Wikibase\ExceptionWithCode']               = $dir . 
'includes/ExceptionWithCode.php';
 $wgAutoloadClasses['Wikibase\ItemView']                                = $dir 
. 'includes/ItemView.php';
 $wgAutoloadClasses['Wikibase\LabelDescriptionDuplicateDetector'] = $dir . 
'includes/LabelDescriptionDuplicateDetector.php';
 $wgAutoloadClasses['Wikibase\Repo\LazyDBConnectionProvider']   = $dir . 
'includes/LazyDBConnectionProvider.php';
diff --git a/repo/includes/ClaimSaver.php b/repo/includes/ClaimSaver.php
new file mode 100644
index 0000000..38a7e7d
--- /dev/null
+++ b/repo/includes/ClaimSaver.php
@@ -0,0 +1,175 @@
+<?php
+
+namespace Wikibase;
+
+use User;
+use MWException;
+use Wikibase\ExceptionWithCode;
+
+/**
+ * Class for updating a claim in the primary storage.
+ *
+ * TODO: add dedicated tests (now tested though SetClaim API module)
+ * 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 ClaimSaver {
+
+       /**
+        * @see ApiBase::execute
+        *
+        * @since 0.4
+        *
+        * @param Claim $claim
+        * @param int|null $baseRevId
+        * @param string $token
+        * @param User $user
+        *
+        * @return int
+        */
+       public function saveClaim( 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/includes/ExceptionWithCode.php 
b/repo/includes/ExceptionWithCode.php
new file mode 100644
index 0000000..756d154
--- /dev/null
+++ b/repo/includes/ExceptionWithCode.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Wikibase;
+
+/**
+ * Exception with a string error code.
+ *
+ * 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 ExceptionWithCode extends \Exception {
+
+       /**
+        * @since 0.4
+        *
+        * @var string
+        */
+       private $stringCode;
+
+       /**
+        * Constructor.
+        *
+        * @since 0.4
+        *
+        * @param string $message
+        * @param string $code
+        */
+       public function __construct( $message, $code ) {
+               parent::__construct( $message );
+               $this->stringCode = $code;
+       }
+
+       /**
+        * @since 0.4
+        *
+        * @return string
+        */
+       public function getErrorCode() {
+               return $this->stringCode;
+       }
+
+}
diff --git a/repo/includes/api/SetClaim.php b/repo/includes/api/SetClaim.php
index d9e8dde..7396cb4 100644
--- a/repo/includes/api/SetClaim.php
+++ b/repo/includes/api/SetClaim.php
@@ -5,11 +5,11 @@
 use MWException;
 use ApiBase;
 
-use Wikibase\Entity;
 use Wikibase\EntityContent;
 use Wikibase\Claim;
-use Wikibase\EntityId;
 use Wikibase\EntityContentFactory;
+use Wikibase\ClaimSaver;
+use Wikibase\ExceptionWithCode;
 
 /**
  * API module for creating or updating an entire Claim.
@@ -49,7 +49,7 @@
        public function execute() {
                $claim = $this->getClaimFromRequest();
 
-               $claimSetter = new ClaimSetter();
+               $claimSetter = new ClaimSaver();
 
                $params = $this->extractRequestParams();
                $baseRevisionId = isset( $params['baserevid'] ) ? intval( 
$params['baserevid'] ) : null;
@@ -58,7 +58,7 @@
                $newRevisionId = null;
 
                try {
-                       $newRevisionId = $claimSetter->setClaim( $claim, 
$baseRevisionId, $token, $this->getUser() );
+                       $newRevisionId = $claimSetter->saveClaim( $claim, 
$baseRevisionId, $token, $this->getUser() );
                }
                catch ( ExceptionWithCode $exception ) {
                        $this->dieUsage( $exception->getMessage(), 
$exception->getErrorCode() );
@@ -196,226 +196,3 @@
        }
 
 }
-
-/**
- * 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

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I266f251a243651836d095cee12eaf3ef39ddff87
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Henning Snater <henning.sna...@wikimedia.de>
Gerrit-Reviewer: John Erling Blad <john.b...@wikimedia.de>
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

Reply via email to