Daniel Werner has submitted this change and it was merged.

Change subject: Provide alternative to methods using SiteLink in item and 
deprecate the SiteLink using ones
......................................................................


Provide alternative to methods using SiteLink in item and deprecate the 
SiteLink using ones

Change-Id: I6a2e0b4762bdb420d9793deaf6f9a44d6e6eb395
---
M DataModel/DataModel/Entity/Item.php
M DataModel/tests/phpunit/Entity/ItemTest.php
2 files changed, 117 insertions(+), 1 deletion(-)

Approvals:
  Daniel Werner: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/DataModel/DataModel/Entity/Item.php 
b/DataModel/DataModel/Entity/Item.php
index c3fa49e..01ccdca 100644
--- a/DataModel/DataModel/Entity/Item.php
+++ b/DataModel/DataModel/Entity/Item.php
@@ -4,6 +4,8 @@
 
 use Diff\Patcher;
 use MWException;
+use OutOfBoundsException;
+use Wikibase\DataModel\SimpleSiteLink;
 
 /**
  * Represents a single Wikibase item.
@@ -48,6 +50,7 @@
         * Adds a site link.
         *
         * @since 0.1
+        * @deprecated since 0.4, use addSiteLink instead
         *
         * @param SiteLink $link the link to the target page
         * @param string $updateType
@@ -70,6 +73,19 @@
        }
 
        /**
+        * Adds a site link to the list of site links.
+        * If there already is a site link with the site id of the provided 
site link,
+        * then that one will be overridden by the provided one.
+        *
+        * @since 0.4
+        *
+        * @param SimpleSiteLink $siteLink
+        */
+       public function addSimpleSiteLink( SimpleSiteLink $siteLink ) {
+               $this->data['links'][$siteLink->getSiteId()] = 
$siteLink->getPageName();
+       }
+
+       /**
         * Removes the sitelink with specified site ID if the Item has such a 
sitelink.
         * A page name can be provided to have removal only happen when it 
matches what is set.
         * A boolean is returned indicating if a link got removed or not.
@@ -82,7 +98,7 @@
         * @return bool Success indicator
         */
        public function removeSiteLink( $siteId, $pageName = false ) {
-               if ( $pageName !== false) {
+               if ( $pageName !== false ) {
                        $success = array_key_exists( $siteId, 
$this->data['links'] ) && $this->data['links'][$siteId] === $pageName;
                }
                else {
@@ -101,6 +117,7 @@
         * site id (str) => SiteLink
         *
         * @since 0.1
+        * @deprecated since 0.4, use getSimpleSiteLinks instead
         *
         * @return array a list of SiteLink objects
         */
@@ -118,9 +135,25 @@
        }
 
        /**
+        * @since 0.4
+        *
+        * @return SimpleSiteLink[]
+        */
+       public function getSimpleSiteLinks() {
+               $links = array();
+
+               foreach ( $this->data['links'] as $siteId => $pageName ) {
+                       $links[] = new SimpleSiteLink( $siteId, $pageName );
+               }
+
+               return $links;
+       }
+
+       /**
         * Returns the site link for the given site id, or null.
         *
         * @since 0.1
+        * @deprecated since 0.4, use getSimpleSiteLink instead
         *
         * @param String $siteId the id of the site to which to get the lin
         *
@@ -135,6 +168,20 @@
        }
 
        /**
+        * @param string $siteId
+        *
+        * @return SimpleSiteLink
+        * @throws OutOfBoundsException
+        */
+       public function getSimpleSiteLink( $siteId ) {
+               if ( !array_key_exists( $siteId, $this->data['links'] ) ) {
+                       throw new OutOfBoundsException( "There is no site link 
with site id '$siteId'" );
+               }
+
+               return new SimpleSiteLink( $siteId, 
$this->data['links'][$siteId] );
+       }
+
+       /**
         * @see Entity::isEmpty
         *
         * @since 0.1
diff --git a/DataModel/tests/phpunit/Entity/ItemTest.php 
b/DataModel/tests/phpunit/Entity/ItemTest.php
index 997424b..4e867ac 100644
--- a/DataModel/tests/phpunit/Entity/ItemTest.php
+++ b/DataModel/tests/phpunit/Entity/ItemTest.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Test;
 
+use Wikibase\DataModel\SimpleSiteLink;
 use Wikibase\Item;
 use Wikibase\SiteLink;
 
@@ -262,4 +263,72 @@
                return $argLists;
        }
 
+       public function testGetSimpleSiteLinkWithNonSetSiteId() {
+               $item = Item::newEmpty();
+
+               $this->setExpectedException( 'OutOfBoundsException' );
+               $item->getSimpleSiteLink( 'enwiki' );
+       }
+
+       /**
+        * @dataProvider simpleSiteLinkProvider
+        */
+       public function testAddSimpleSiteLink( SimpleSiteLink $siteLink ) {
+               $item = Item::newEmpty();
+
+               $item->addSimpleSiteLink( $siteLink );
+
+               $this->assertEquals(
+                       $siteLink,
+                       $item->getSimpleSiteLink( $siteLink->getSiteId() )
+               );
+       }
+
+       public function simpleSiteLinkProvider() {
+               $argLists = array();
+
+               $argLists[] = array( new SimpleSiteLink( 'enwiki', 'Wikidata' ) 
);
+               $argLists[] = array( new SimpleSiteLink( 'nlwiki', 'Wikidata' ) 
);
+               $argLists[] = array( new SimpleSiteLink( 'nlwiki', 'Nyan!' ) );
+               $argLists[] = array( new SimpleSiteLink( 'foo bar', 'baz bah' ) 
);
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider simpleSiteLinksProvider
+        */
+       public function testGetSimpleSiteLinks() {
+               $siteLinks = func_get_args();
+               $item = Item::newEmpty();
+
+               foreach ( $siteLinks as $siteLink ) {
+                       $item->addSimpleSiteLink( $siteLink );
+               }
+
+               $this->assertInternalType( 'array', $item->getSimpleSiteLinks() 
);
+               $this->assertEquals( $siteLinks, $item->getSimpleSiteLinks() );
+       }
+
+       public function simpleSiteLinksProvider() {
+               $argLists = array();
+
+               $argLists[] = array();
+
+               $argLists[] = array( new SimpleSiteLink( 'enwiki', 'Wikidata' ) 
);
+
+               $argLists[] = array(
+                       new SimpleSiteLink( 'enwiki', 'Wikidata' ),
+                       new SimpleSiteLink( 'nlwiki', 'Wikidata' )
+               );
+
+               $argLists[] = array(
+                       new SimpleSiteLink( 'enwiki', 'Wikidata' ),
+                       new SimpleSiteLink( 'nlwiki', 'Wikidata' ),
+                       new SimpleSiteLink( 'foo bar', 'baz bah' )
+               );
+
+               return $argLists;
+       }
+
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I6a2e0b4762bdb420d9793deaf6f9a44d6e6eb395
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Werner <[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