Thiemo Mättig (WMDE) has uploaded a new change for review.

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

Change subject: Turn JSON fields into textareas for the ApiSandbox
......................................................................

Turn JSON fields into textareas for the ApiSandbox

I'm also doing a bit of refactoring in very closely related code:

* Avoid duplicate code.
* Rename some "claimGuid" variables and private methods into
  "statementGuid" or "guid" if it's already obvious from the context
  that the guid describes a statement.

Bug: T98458
Change-Id: I54f5c7973622c8cd6c7ad2f0834b82b872f37c0e
---
M repo/includes/api/ClaimModificationHelper.php
M repo/includes/api/CreateClaim.php
M repo/includes/api/EditEntity.php
M repo/includes/api/FormatSnakValue.php
M repo/includes/api/GetClaims.php
M repo/includes/api/RemoveQualifiers.php
M repo/includes/api/SetClaim.php
M repo/includes/api/SetClaimValue.php
M repo/includes/api/SetQualifier.php
M repo/includes/api/SetReference.php
10 files changed, 39 insertions(+), 40 deletions(-)


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

diff --git a/repo/includes/api/ClaimModificationHelper.php 
b/repo/includes/api/ClaimModificationHelper.php
index 548215d..8344a2b 100644
--- a/repo/includes/api/ClaimModificationHelper.php
+++ b/repo/includes/api/ClaimModificationHelper.php
@@ -109,17 +109,19 @@
        /**
         * @since 0.4
         *
-        * @param array $params
+        * @param string[] $params Array with a 'snaktype' and an optional 
'value' element.
         * @param PropertyId $propertyId
         *
         * @throws UsageException
         * @throws LogicException
         * @return Snak
         */
-       public function getSnakInstance( $params, PropertyId $propertyId ) {
+       public function getSnakInstance( array $params, PropertyId $propertyId 
) {
                $valueData = null;
+
                if ( isset( $params['value'] ) ) {
                        $valueData = FormatJson::decode( $params['value'], true 
);
+
                        if ( $valueData === null ) {
                                $this->errorReporter->dieError( 'Could not 
decode snak value', 'invalid-snak' );
                        }
diff --git a/repo/includes/api/CreateClaim.php 
b/repo/includes/api/CreateClaim.php
index 54b9e77..e198e89 100644
--- a/repo/includes/api/CreateClaim.php
+++ b/repo/includes/api/CreateClaim.php
@@ -85,11 +85,10 @@
         * @params array $params
         */
        private function validateParameters( array $params ) {
-               if ( $params['snaktype'] == 'value' XOR isset( $params['value'] 
) ) {
-                       if ( $params['snaktype'] == 'value' ) {
+               if ( $params['snaktype'] === 'value' XOR isset( 
$params['value'] ) ) {
+                       if ( $params['snaktype'] === 'value' ) {
                                $this->dieError( 'A value needs to be provided 
when creating a claim with PropertyValueSnak snak', 'param-missing' );
-                       }
-                       else {
+                       } else {
                                $this->dieError( 'You cannot provide a value 
when creating a claim with no PropertyValueSnak as main snak', 'param-illegal' 
);
                        }
                }
@@ -98,7 +97,7 @@
                        $this->dieError( 'A property ID needs to be provided 
when creating a claim with a Snak', 'param-missing' );
                }
 
-               if ( isset( $params['value'] ) && \FormatJson::decode( 
$params['value'], true ) == null ) {
+               if ( isset( $params['value'] ) && \FormatJson::decode( 
$params['value'], true ) === null ) {
                        $this->dieError( 'Could not decode snak value', 
'invalid-snak' );
                }
        }
@@ -122,7 +121,7 @@
                                        ApiBase::PARAM_REQUIRED => false,
                                ),
                                'value' => array(
-                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_TYPE => 'text',
                                        ApiBase::PARAM_REQUIRED => false,
                                ),
                        ),
diff --git a/repo/includes/api/EditEntity.php b/repo/includes/api/EditEntity.php
index e2807d5..b3f5ead 100644
--- a/repo/includes/api/EditEntity.php
+++ b/repo/includes/api/EditEntity.php
@@ -724,7 +724,7 @@
                        parent::getAllowedParams(),
                        array(
                                'data' => array(
-                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_TYPE => 'text',
                                ),
                                'clear' => array(
                                        ApiBase::PARAM_TYPE => 'boolean',
diff --git a/repo/includes/api/FormatSnakValue.php 
b/repo/includes/api/FormatSnakValue.php
index 43c3012..3d4e241 100644
--- a/repo/includes/api/FormatSnakValue.php
+++ b/repo/includes/api/FormatSnakValue.php
@@ -176,7 +176,7 @@
                                ApiBase::PARAM_REQUIRED => false,
                        ),
                        'datavalue' => array(
-                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_TYPE => 'text',
                                ApiBase::PARAM_REQUIRED => true,
                        ),
                        'datatype' => array(
@@ -184,7 +184,7 @@
                                ApiBase::PARAM_REQUIRED => false,
                        ),
                        'options' => array(
-                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_TYPE => 'text',
                                ApiBase::PARAM_REQUIRED => false,
                        ),
                );
diff --git a/repo/includes/api/GetClaims.php b/repo/includes/api/GetClaims.php
index ec9a9f7..2cd753c 100644
--- a/repo/includes/api/GetClaims.php
+++ b/repo/includes/api/GetClaims.php
@@ -165,26 +165,26 @@
         */
        private function getIdentifiers( $params ) {
                if ( isset( $params['claim'] ) ) {
-                       $claimGuid = $params['claim'];
-                       $entityId = $this->getEntityIdFromClaimGuid( 
$params['claim'] );
+                       $statementGuid = $params['claim'];
+                       $entityId = $this->getEntityIdFromStatementGuid( 
$params['claim'] );
 
-                       if( isset( $params['entity'] ) && $entityId !== 
$params['entity'] ) {
+                       if ( isset( $params['entity'] ) && $entityId !== 
$params['entity'] ) {
                                $this->dieError( 'If both entity id and claim 
key are provided they need to point to the same entity', 'param-illegal' );
                        }
                } else {
-                       $claimGuid = null;
+                       $statementGuid = null;
                        $entityId = $params['entity'];
                }
 
-               return array( $entityId, $claimGuid );
+               return array( $entityId, $statementGuid );
        }
 
-       private function getEntityIdFromClaimGuid( $claimGuid ) {
-               if ( $this->claimGuidValidator->validateFormat( $claimGuid ) 
=== false ) {
+       private function getEntityIdFromStatementGuid( $guid ) {
+               if ( $this->claimGuidValidator->validateFormat( $guid ) === 
false ) {
                        $this->dieError( 'Invalid claim guid' , 'invalid-guid' 
);
                }
 
-               return $this->claimGuidParser->parse( $claimGuid 
)->getEntityId()->getSerialization();
+               return $this->claimGuidParser->parse( $guid 
)->getEntityId()->getSerialization();
        }
 
        /**
diff --git a/repo/includes/api/RemoveQualifiers.php 
b/repo/includes/api/RemoveQualifiers.php
index 982792c..53461a6 100644
--- a/repo/includes/api/RemoveQualifiers.php
+++ b/repo/includes/api/RemoveQualifiers.php
@@ -48,19 +48,19 @@
                $params = $this->extractRequestParams();
                $this->validateParameters( $params );
 
-               $claimGuid = $params['claim'];
-               $entityId = $this->claimGuidParser->parse( $claimGuid 
)->getEntityId();
+               $statementGuid = $params['claim'];
+               $entityId = $this->claimGuidParser->parse( $statementGuid 
)->getEntityId();
                $baseRevisionId = isset( $params['baserevid'] ) ? 
(int)$params['baserevid'] : null;
                $entityRevision = $this->loadEntityRevision( $entityId, 
$baseRevisionId );
                $entity = $entityRevision->getEntity();
                $summary = $this->claimModificationHelper->createSummary( 
$params, $this );
 
-               $claim = $this->claimModificationHelper->getClaimFromEntity( 
$claimGuid, $entity );
+               $claim = $this->claimModificationHelper->getClaimFromEntity( 
$statementGuid, $entity );
 
                $qualifierHashes = $this->getQualifierHashesFromParams( 
$params, $claim );
 
                $changeOps = new ChangeOps();
-               $changeOps->add( $this->getChangeOps( $claimGuid, 
$qualifierHashes ) );
+               $changeOps->add( $this->getChangeOps( $statementGuid, 
$qualifierHashes ) );
 
                try {
                        $changeOps->apply( $entity, $summary );
@@ -82,16 +82,16 @@
        }
 
        /**
-        * @param string $claimGuid
+        * @param string $statementGuid
         * @param string[] $qualifierHashes
         *
         * @return ChangeOp[]
         */
-       private function getChangeOps( $claimGuid, array $qualifierHashes ) {
+       private function getChangeOps( $statementGuid, array $qualifierHashes ) 
{
                $changeOps = array();
 
                foreach ( $qualifierHashes as $qualifierHash ) {
-                       $changeOps[] = 
$this->claimChangeOpFactory->newRemoveQualifierOp( $claimGuid, $qualifierHash );
+                       $changeOps[] = 
$this->claimChangeOpFactory->newRemoveQualifierOp( $statementGuid, 
$qualifierHash );
                }
 
                return $changeOps;
diff --git a/repo/includes/api/SetClaim.php b/repo/includes/api/SetClaim.php
index e1e93f8..7f2156e 100644
--- a/repo/includes/api/SetClaim.php
+++ b/repo/includes/api/SetClaim.php
@@ -158,7 +158,7 @@
                return array_merge(
                        array(
                                'claim' => array(
-                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_TYPE => 'text',
                                        ApiBase::PARAM_REQUIRED => true
                                ),
                                'index' => array(
diff --git a/repo/includes/api/SetClaimValue.php 
b/repo/includes/api/SetClaimValue.php
index 84e5b20..fc42e5f 100644
--- a/repo/includes/api/SetClaimValue.php
+++ b/repo/includes/api/SetClaimValue.php
@@ -44,20 +44,20 @@
                $params = $this->extractRequestParams();
                $this->validateParameters( $params );
 
-               $claimGuid = $params['claim'];
-               $entityId = $this->claimGuidParser->parse( $claimGuid 
)->getEntityId();
+               $statementGuid = $params['claim'];
+               $entityId = $this->claimGuidParser->parse( $statementGuid 
)->getEntityId();
                $baseRevisionId = isset( $params['baserevid'] ) ? 
(int)$params['baserevid'] : null;
                $entityRevision = $this->loadEntityRevision( $entityId, 
$baseRevisionId );
                $entity = $entityRevision->getEntity();
 
-               $claim = $this->claimModificationHelper->getClaimFromEntity( 
$claimGuid, $entity );
+               $claim = $this->claimModificationHelper->getClaimFromEntity( 
$statementGuid, $entity );
 
                $snak = $this->claimModificationHelper->getSnakInstance( 
$params, $claim->getMainSnak()->getPropertyId() );
 
                $summary = $this->claimModificationHelper->createSummary( 
$params, $this );
 
                $changeOp = $this->claimChangeOpFactory->newSetMainSnakOp(
-                       $claimGuid,
+                       $statementGuid,
                        $snak
                );
 
@@ -88,7 +88,7 @@
                                        ApiBase::PARAM_REQUIRED => true,
                                ),
                                'value' => array(
-                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_TYPE => 'text',
                                        ApiBase::PARAM_REQUIRED => false,
                                ),
                                'snaktype' => array(
diff --git a/repo/includes/api/SetQualifier.php 
b/repo/includes/api/SetQualifier.php
index 63c4c20..be781e9 100644
--- a/repo/includes/api/SetQualifier.php
+++ b/repo/includes/api/SetQualifier.php
@@ -109,7 +109,7 @@
        private function getChangeOp() {
                $params = $this->extractRequestParams();
 
-               $claimGuid = $params['claim'];
+               $statementGuid = $params['claim'];
 
                $propertyId = 
$this->claimModificationHelper->getEntityIdFromString( $params['property'] );
                if ( !$propertyId instanceof PropertyId ) {
@@ -118,13 +118,11 @@
                                'param-illegal'
                        );
                }
+
                $newQualifier = 
$this->claimModificationHelper->getSnakInstance( $params, $propertyId );
 
-               if ( isset( $params['snakhash'] ) ) {
-                       $changeOp = 
$this->claimChangeOpFactory->newSetQualifierOp( $claimGuid, $newQualifier, 
$params['snakhash'] );
-               } else {
-                       $changeOp = 
$this->claimChangeOpFactory->newSetQualifierOp( $claimGuid, $newQualifier, '' );
-               }
+               $snakHash = isset( $params['snakhash'] ) ? $params['snakhash'] 
: '';
+               $changeOp = $this->claimChangeOpFactory->newSetQualifierOp( 
$statementGuid, $newQualifier, $snakHash );
 
                return $changeOp;
        }
@@ -144,7 +142,7 @@
                                        ApiBase::PARAM_REQUIRED => false,
                                ),
                                'value' => array(
-                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_TYPE => 'text',
                                        ApiBase::PARAM_REQUIRED => false,
                                ),
                                'snaktype' => array(
diff --git a/repo/includes/api/SetReference.php 
b/repo/includes/api/SetReference.php
index 20fe65e..02acb74 100644
--- a/repo/includes/api/SetReference.php
+++ b/repo/includes/api/SetReference.php
@@ -198,7 +198,7 @@
                                        ApiBase::PARAM_REQUIRED => true,
                                ),
                                'snaks' => array(
-                                       ApiBase::PARAM_TYPE => 'string',
+                                       ApiBase::PARAM_TYPE => 'text',
                                        ApiBase::PARAM_REQUIRED => true,
                                ),
                                'snaks-order' => array(

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I54f5c7973622c8cd6c7ad2f0834b82b872f37c0e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

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

Reply via email to