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

Change subject: Implemented index in SetReference API module
......................................................................


Implemented index in SetReference API module

Change-Id: Id64230e5e156506b37a2b2c56c7a9260ca252824
---
M lib/resources/wikibase.RepoApi/wikibase.AbstractedRepoApi.js
M lib/resources/wikibase.RepoApi/wikibase.RepoApi.js
M repo/includes/api/SetReference.php
M repo/tests/phpunit/includes/api/SetReferenceTest.php
4 files changed, 87 insertions(+), 16 deletions(-)

Approvals:
  Tobias Gritschacher: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/resources/wikibase.RepoApi/wikibase.AbstractedRepoApi.js 
b/lib/resources/wikibase.RepoApi/wikibase.AbstractedRepoApi.js
index 5f0eacf..9b684fd 100644
--- a/lib/resources/wikibase.RepoApi/wikibase.AbstractedRepoApi.js
+++ b/lib/resources/wikibase.RepoApi/wikibase.AbstractedRepoApi.js
@@ -68,7 +68,7 @@
        },
 
        /**
-        * Will set a new Reference for a Statement.
+        * Adds a new or updates an existing Reference of a Statement.
         *
         * @since 0.4
         *
@@ -77,13 +77,15 @@
         * @param {number} baseRevId
         * @param {string} [referenceHash] A hash of the reference that should 
be updated.
         *        If not provided, a new reference is created.
+        * @param {number} [index] The new reference's index. Only needs to be 
specified if the
+        *        reference's index within the list of all the statement's 
references shall be changed.
         * @return {jQuery.Promise} If resolved, this will get a wb.Reference 
object as first parameter
         *         and the last base revision as second parameter.
         */
-       setReference: function( statementGuid, snaks, baseRevId, referenceHash 
) {
+       setReference: function( statementGuid, snaks, baseRevId, referenceHash, 
index ) {
                var snakJson = snaks.toJSON();
                return this._abstract(
-                       this._repoApi.setReference( statementGuid, snakJson, 
baseRevId, referenceHash ),
+                       this._repoApi.setReference( statementGuid, snakJson, 
baseRevId, referenceHash, index ),
                        function( result ) {
                                return [
                                        wb.Reference.newFromJSON( 
result.reference ),
diff --git a/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js 
b/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js
index 020976d..61f52de 100644
--- a/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js
+++ b/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js
@@ -335,7 +335,7 @@
        },
 
        /**
-        * Will set a new Reference for a Statement.
+        * Adds a new or updates an existing Reference of a Statement.
         *
         * @since 0.4
         *
@@ -344,9 +344,11 @@
         * @param {number} baseRevId
         * @param {string} [referenceHash] A hash of the reference that should 
be updated.
         *        If not provided, a new reference is created.
+        * @param {number} [index] The new reference's index. Only needs to be 
specified if the
+        *        reference's index within the list of all the statement's 
references shall be changed.
         * @return {jQuery.Promise}
         */
-       setReference: function( statementGuid, snaks, baseRevId, referenceHash 
) {
+       setReference: function( statementGuid, snaks, baseRevId, referenceHash, 
index ) {
                var params = {
                        action: 'wbsetreference',
                        statement: statementGuid,
@@ -358,6 +360,10 @@
                        params.reference = referenceHash;
                }
 
+               if( index ) {
+                       params.index = index;
+               }
+
                return this.post( params );
        },
 
diff --git a/repo/includes/api/SetReference.php 
b/repo/includes/api/SetReference.php
index 33c7c18..085cf5f 100644
--- a/repo/includes/api/SetReference.php
+++ b/repo/includes/api/SetReference.php
@@ -189,14 +189,10 @@
                $params = $this->extractRequestParams();
 
                $claimGuid = $params['statement'];
+               $hash = isset( $params['reference'] ) ? $params['reference'] : 
'';
+               $index = isset( $params['index'] ) ? $params['index'] : null;
 
-               if ( isset( $params['reference'] ) ) {
-                       $changeOp = new ChangeOpReference( $claimGuid, 
$reference, $params['reference'] );
-               } else {
-                       $changeOp = new ChangeOpReference( $claimGuid, 
$reference, '' );
-               }
-
-               return $changeOp;
+               return new ChangeOpReference( $claimGuid, $reference, $hash, 
$index );
        }
 
        /**
@@ -240,6 +236,9 @@
                                'reference' => array(
                                        ApiBase::PARAM_TYPE => 'string',
                                ),
+                               'index' => array(
+                                       ApiBase::PARAM_TYPE => 'integer',
+                               ),
                        ),
                        parent::getAllowedParams()
                );
@@ -274,6 +273,7 @@
                                'statement' => 'A GUID identifying the 
statement for which a reference is being set',
                                'snaks' => 'The snaks to set the reference to. 
JSON object with property ids pointing to arrays containing the snaks for that 
property',
                                'reference' => 'A hash of the reference that 
should be updated. Optional. When not provided, a new reference is created',
+                               'index' => 'The index within the statement\'s 
list of references where to move the reference to. Optional. When not provided, 
the reference will stay in place.',
                        )
                );
        }
@@ -300,6 +300,8 @@
                                => 'Create a new reference for claim with GUID 
Q76$D4FDE516-F20C-4154-ADCE-7C5B609DFDFF',
                        
'api.php?action=wbsetreference&statement=Q76$D4FDE516-F20C-4154-ADCE-7C5B609DFDFF&reference=1eb8793c002b1d9820c833d234a1b54c8e94187e&snaks={"P212":[{"snaktype":"value","property":"P212","datavalue":{"type":"string","value":"bar"}}]}&baserevid=7201010&token=foobar'
                                => 'Set reference for claim with GUID 
Q76$D4FDE516-F20C-4154-ADCE-7C5B609DFDFF which has hash of 
1eb8793c002b1d9820c833d234a1b54c8e94187e',
+                       
'api.php?action=wbsetreference&statement=Q76$D4FDE516-F20C-4154-ADCE-7C5B609DFDFF&snaks={"P212":[{"snaktype":"novalue","property":"P212"}]}&index=0&baserevid=7201010&token=foobar'
+                               => 'Creates a new reference for the claim with 
GUID Q76$D4FDE516-F20C-4154-ADCE-7C5B609DFDFF and inserts the new reference at 
the top of the list of references instead of appending it to the bottom.',
                );
        }
 }
diff --git a/repo/tests/phpunit/includes/api/SetReferenceTest.php 
b/repo/tests/phpunit/includes/api/SetReferenceTest.php
index 9c00b76..30da997 100644
--- a/repo/tests/phpunit/includes/api/SetReferenceTest.php
+++ b/repo/tests/phpunit/includes/api/SetReferenceTest.php
@@ -2,8 +2,13 @@
 
 namespace Wikibase\Test\Api;
 
+use Wikibase\Item;
+use Wikibase\ItemContent;
 use Wikibase\PropertyContent;
+use Wikibase\PropertyNoValueSnak;
+use Wikibase\PropertySomeValueSnak;
 use Wikibase\Reference;
+use Wikibase\SnakList;
 
 /**
  * Unit tests for the Wikibase\ApiSetReference class.
@@ -116,17 +121,60 @@
                );
        }
 
+       public function testSettingIndex() {
+               $item = Item::newEmpty();
+               $content = new ItemContent( $item );
+               $content->save( '', null, EDIT_NEW );
+
+               // Create a statement to act upon:
+               $statement = $item->newClaim( new PropertyNoValueSnak( 42 ) );
+               $statement->setGuid(
+                       $item->getId()->getPrefixedId() . 
'$D8505CDA-25E4-4334-AG93-A3290BCD9C0P'
+               );
+
+               // Pre-fill statement with three references:
+               $references = array(
+                       new Reference( new SnakList( array( new 
PropertySomeValueSnak( 1 ) ) ) ),
+                       new Reference( new SnakList( array( new 
PropertySomeValueSnak( 2 ) ) ) ),
+                       new Reference( new SnakList( array( new 
PropertySomeValueSnak( 3 ) ) ) ),
+               );
+
+               foreach( $references as $reference ) {
+                       $statement->getReferences()->addReference( $reference );
+               }
+
+               $item->addClaim( $statement );
+
+               $content->save( '' );
+
+               $this->makeValidRequest(
+                       $statement->getGuid(),
+                       $references[2]->getHash(),
+                       $references[2],
+                       0
+               );
+
+               $this->assertEquals( $statement->getReferences()->indexOf( 
$references[0] ), 0 );
+       }
+
        /**
         * @param string|null $statementGuid
         * @param string $referenceHash
         * @param Reference|array $reference Reference object or serialized 
reference
+        * @param int|null $index
+        *
         * @return array Serialized reference
         */
-       protected function makeValidRequest( $statementGuid, $referenceHash, 
$reference ) {
+       protected function makeValidRequest( $statementGuid, $referenceHash, 
$reference, $index = null ) {
                $serializedReference = $this->serializeReference( $reference );
                $reference = $this->unserializeReference( $reference );
 
-               $params = $this->generateRequestParams( $statementGuid, 
$referenceHash, $serializedReference );
+               $params = $this->generateRequestParams(
+                       $statementGuid,
+                       $referenceHash,
+                       $serializedReference,
+                       $index
+               );
 
                list( $resultArray, ) = $this->doApiRequestWithToken( $params );
 
@@ -195,16 +243,29 @@
         * @param string $statementGuid
         * @param string $referenceHash
         * @param array $serializedReference
+        * @param int|null $index
+        *
         * @return array
         */
-       protected function generateRequestParams( $statementGuid, 
$referenceHash, $serializedReference ) {
-               return array(
+       protected function generateRequestParams(
+               $statementGuid,
+               $referenceHash,
+               $serializedReference,
+               $index = null
+       ) {
+               $params = array(
                        'action' => 'wbsetreference',
                        'statement' => $statementGuid,
                        'reference' => $referenceHash,
                        'snaks' => \FormatJson::encode( 
$serializedReference['snaks'] ),
                        'snaks-order' => \FormatJson::encode( 
$serializedReference['snaks-order'] ),
                );
+
+               if( !is_null( $index ) ) {
+                       $params['index'] = $index;
+               }
+
+               return $params;
        }
 
        /**

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id64230e5e156506b37a2b2c56c7a9260ca252824
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to