Bene has uploaded a new change for review. https://gerrit.wikimedia.org/r/108337
Change subject: Reduce badges to items listed in config ...................................................................... Reduce badges to items listed in config Created a config variable "badgeItems" where the item ids are stored that may be used as badges. Also updated the api module, special pages and tests. Bug: 40810 Change-Id: If3e7ed5af4009f028884368ec45045b6f691d396 --- M lib/config/WikibaseLib.default.php M repo/Wikibase.i18n.php M repo/includes/ChangeOp/ChangeOpSiteLink.php M repo/includes/api/ModifyEntity.php M repo/includes/specials/SpecialSetSiteLink.php M repo/tests/phpunit/includes/ChangeOp/ChangeOpSiteLinkTest.php M repo/tests/phpunit/includes/api/EditEntityTest.php M repo/tests/phpunit/includes/api/SetSiteLinkTest.php 8 files changed, 73 insertions(+), 5 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/37/108337/1 diff --git a/lib/config/WikibaseLib.default.php b/lib/config/WikibaseLib.default.php index fb27142..009105f 100644 --- a/lib/config/WikibaseLib.default.php +++ b/lib/config/WikibaseLib.default.php @@ -35,6 +35,12 @@ 'specialSiteLinkGroups' => array(), + // items allowed to be used as badges + 'badgeItems' => array( + 'Q149' => '', + 'Q42' => '', + ), + // local by default. Set to something LBFactory understands. 'changesDatabase' => false, diff --git a/repo/Wikibase.i18n.php b/repo/Wikibase.i18n.php index bd11436..4eee730 100644 --- a/repo/Wikibase.i18n.php +++ b/repo/Wikibase.i18n.php @@ -183,6 +183,7 @@ 'wikibase-setsitelink-warning-remove' => 'Do you really want to remove the site link of [[$1]]?', 'wikibase-setsitelink-invalid-site' => 'The site id "$1" is unknown. Please use an existing site id, such as "enwiki".', 'wikibase-setsitelink-not-item' => 'The id $1 does not belong to an item.', + 'wikibase-setsitelink-not-badge' => 'The id $1 is not specified to be a badge.', 'wikibase-setsitelink-add-failed' => 'The site link could not be saved.', 'wikibase-setsitelink-remove-failed' => 'The site link could not be removed.', 'wikibase-setsitelink-not-itemid' => '"$1" is not a valid item id. Site links can only be set for items.', @@ -899,6 +900,10 @@ Parameters: * $1 - the invalid id', + 'wikibase-setsitelink-not-item' => 'Response informing that the given id is not specified to be used as a badge. + +Parameters: +* $1 - the invalid id', 'wikibase-setsitelink-add-failed' => 'Error message when the site link could not be saved.', 'wikibase-setsitelink-remove-failed' => 'Error message when the site link could not be removed.', 'wikibase-setsitelink-not-itemid' => 'Error message when an entity id, other than item id, is entered. The message advises users that the id is invalid and only items have site links. diff --git a/repo/includes/ChangeOp/ChangeOpSiteLink.php b/repo/includes/ChangeOp/ChangeOpSiteLink.php index e71a691..d90c4a9 100644 --- a/repo/includes/ChangeOp/ChangeOpSiteLink.php +++ b/repo/includes/ChangeOp/ChangeOpSiteLink.php @@ -8,6 +8,7 @@ use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\SiteLink; use Wikibase\Summary; +use Wikibase\Settings; /** * Class for sitelink change operation @@ -68,6 +69,9 @@ if ( !( $badge instanceof ItemId ) ) { throw new InvalidArgumentException( '$badges need to be an array of ItemIds or null' ); } + if ( !in_array( $badge->getPrefixedId(), array_keys( Settings::get( 'badgeItems' ) ) ) ) { + throw new InvalidArgumentException( 'Only items specified in the config can be badges' ); + } } $badges = $this->removeDuplicateBadges( $badges ); diff --git a/repo/includes/api/ModifyEntity.php b/repo/includes/api/ModifyEntity.php index 6f1c6af..7f069c8 100644 --- a/repo/includes/api/ModifyEntity.php +++ b/repo/includes/api/ModifyEntity.php @@ -175,6 +175,10 @@ $this->dieUsage( "Badges: entity with id '{$badgeSerialization}' is not an item", 'not-item' ); } + if ( !in_array( $badgeId->getPrefixedId(), array_keys( Settings::get( 'badgeItems' ) ) ) ) { + $this->dieUsage( "Badges: '{$badgeSerialization}' is not specified to be a badge", 'not-badge' ); + } + $itemTitle = $entityContentFactory->getTitleForId( $badgeId, Revision::FOR_THIS_USER ); if ( is_null( $itemTitle ) || !$itemTitle->exists() ) { diff --git a/repo/includes/specials/SpecialSetSiteLink.php b/repo/includes/specials/SpecialSetSiteLink.php index 432d84e..c44bd69 100644 --- a/repo/includes/specials/SpecialSetSiteLink.php +++ b/repo/includes/specials/SpecialSetSiteLink.php @@ -14,6 +14,7 @@ use Wikibase\ChangeOp\ChangeOpException; use Wikibase\DataModel\Entity\ItemId; use Wikibase\Summary; +use Wikibase\Settings; use Wikibase\Repo\WikibaseRepo; use ValueParsers\ParseException; @@ -368,6 +369,11 @@ return false; } + if ( !in_array( $badgeId->getPrefixedId(), array_keys( Settings::get( 'badgeItems' ) ) ) ) { + $status->fatal( 'wikibase-setsitelink-not-badge', $badgeId->getPrefixedId() ); + return false; + } + $itemTitle = $entityContentFactory->getTitleForId( $badgeId, Revision::FOR_THIS_USER ); if ( is_null( $itemTitle ) || !$itemTitle->exists() ) { diff --git a/repo/tests/phpunit/includes/ChangeOp/ChangeOpSiteLinkTest.php b/repo/tests/phpunit/includes/ChangeOp/ChangeOpSiteLinkTest.php index c593e82..a2d90f7 100644 --- a/repo/tests/phpunit/includes/ChangeOp/ChangeOpSiteLinkTest.php +++ b/repo/tests/phpunit/includes/ChangeOp/ChangeOpSiteLinkTest.php @@ -25,6 +25,15 @@ */ class ChangeOpSiteLinkTest extends \PHPUnit_Framework_TestCase { + public function setup() { + parent::setup(); + + Settings::singleton()->setSetting( 'badgeItems', array( + 'Q42' => '', + 'Q149' => '', + ) ); + } + /** * @dataProvider invalidConstructorProvider * @expectedException InvalidArgumentException @@ -42,6 +51,7 @@ $argLists[] = array( 'plwiki', 'Warszawa', array( 'FA', 'GA' ) ); $argLists[] = array( 'plwiki', 'Warszawa', array( new ItemId( 'Q42' ), 'FA' ) ); $argLists[] = array( 'plwiki', 'Warszawa', array( new PropertyId( 'P42' ) ) ); + $argLists[] = array( 'plwiki', 'Warszawa', array( new ItemId( 'Q32' ) ) ); return $argLists; } diff --git a/repo/tests/phpunit/includes/api/EditEntityTest.php b/repo/tests/phpunit/includes/api/EditEntityTest.php index ad2386c..5ee6ef2 100644 --- a/repo/tests/phpunit/includes/api/EditEntityTest.php +++ b/repo/tests/phpunit/includes/api/EditEntityTest.php @@ -4,6 +4,7 @@ use Wikibase\ItemContent; use Wikibase\PropertyContent; +use Wikibase\Settings; /** * @covers Wikibase\Api\EditEntity @@ -52,6 +53,16 @@ $badge = ItemContent::newEmpty(); $this->assertTrue( $badge->save( 'EditEntityTestQ149', null, EDIT_NEW )->isOK() ); self::$idMap['%Q149%'] = $badge->getEntity()->getId()->getSerialization(); + + $badge = ItemContent::newEmpty(); + $this->assertTrue( $badge->save( 'EditEntityTestQ32', null, EDIT_NEW )->isOK() ); + self::$idMap['%Q32%'] = $badge->getEntity()->getId()->getSerialization(); + + Settings::singleton()->setSetting( 'badgeItems', array( + self::$idMap['%Q42%'] => '', + self::$idMap['%Q149%'] => '', + 'Q99999' => '', // Just in case we have a wrong config + ) ); } self::$hasSetup = true; } @@ -474,6 +485,9 @@ 'badge id is not an item id' => array( // badge id is not an item id 'p' => array( 'site' => 'enwiki', 'title' => 'Berlin', 'data' => '{"sitelinks":{"dewiki":{"site":"dewiki","title":"TestPage!","badges":["P2","%Q149%"]}}}' ), 'e' => array( 'exception' => array( 'type' => 'UsageException', 'code' => 'not-item' ) ) ), + 'badge id is not specified' => array( // badge id is not specified + 'p' => array( 'site' => 'enwiki', 'title' => 'Berlin', 'data' => '{"sitelinks":{"dewiki":{"site":"dewiki","title":"TestPage!","badges":["%Q149%","%Q32%"]}}}' ), + 'e' => array( 'exception' => array( 'type' => 'UsageException', 'code' => 'not-badge' ) ) ), 'badge item does not exist' => array( // badge item does not exist 'p' => array( 'site' => 'enwiki', 'title' => 'Berlin', 'data' => '{"sitelinks":{"dewiki":{"site":"dewiki","title":"TestPage!","badges":["Q99999","%Q149%"]}}}' ), 'e' => array( 'exception' => array( 'type' => 'UsageException', 'code' => 'no-such-entity' ) ) ), diff --git a/repo/tests/phpunit/includes/api/SetSiteLinkTest.php b/repo/tests/phpunit/includes/api/SetSiteLinkTest.php index 1a0e47a..854d63d 100644 --- a/repo/tests/phpunit/includes/api/SetSiteLinkTest.php +++ b/repo/tests/phpunit/includes/api/SetSiteLinkTest.php @@ -4,6 +4,7 @@ use Wikibase\DataModel\Entity\ItemId; use Wikibase\ItemContent; +use Wikibase\Settings; /** * @covers Wikibase\Api\SetSiteLink @@ -42,6 +43,8 @@ private static $gaItemId; /* @var ItemId */ private static $faItemId; + /* @var ItemId */ + private static $otherItemId; public static function provideData() { return array( @@ -119,7 +122,10 @@ array( //8 badge item does not exist 'p' => array( 'site' => 'enwiki', 'title' => 'Berlin', 'linksite' => 'enwiki', 'linktitle' => 'Berlin', 'badges' => 'Q99999|{faItem}' ), 'e' => array( 'exception' => array( 'type' => 'UsageException', 'code' => 'no-such-entity' ) ) ), - array( //9 no sitelink - cannot change badges + array( //9 badge id is not specified + 'p' => array( 'site' => 'enwiki', 'title' => 'Berlin', 'linksite' => 'enwiki', 'linktitle' => 'Berlin', 'badges' => '{faItem}|{otherItem}' ), + 'e' => array( 'exception' => array( 'type' => 'UsageException', 'code' => 'not-badge' ) ) ), + array( //10 no sitelink - cannot change badges 'p' => array( 'site' => 'enwiki', 'title' => 'Berlin', 'linksite' => 'svwiki', 'badges' => '{gaItem}|{faItem}' ), 'e' => array( 'exception' => array( 'type' => 'UsageException', 'code' => 'no-such-sitelink' ) ) ), ); @@ -140,6 +146,17 @@ $status = $badge->save( 'SetSiteLinkTestFA', null, EDIT_NEW ); $this->assertTrue( $status->isOK() ); self::$faItemId = $badge->getEntity()->getId(); + + $badge = ItemContent::newEmpty(); + $status = $badge->save( 'SetSiteLinkTestOther', null, EDIT_NEW ); + $this->assertTrue( $status->isOK() ); + self::$otherItemId = $badge->getEntity()->getId(); + + Settings::singleton()->setSetting( 'badgeItems', array( + self::$gaItemId->getPrefixedId() => '', + self::$faItemId->getPrefixedId() => '', + 'Q99999' => '', // Just in case we have a wrong config + ) ); } self::$hasSetup = true; } @@ -160,6 +177,8 @@ $dummy = self::$gaItemId->getPrefixedId(); } elseif ( $dummy === '{faItem}' ) { $dummy = self::$faItemId->getPrefixedId(); + } elseif ( $dummy === '{otherItem}' ) { + $dummy = self::$otherItemId->getPrefixedId(); } } } @@ -180,8 +199,8 @@ // Replace the placeholder item ids in the API params if ( isset( $params['badges'] ) ) { $params['badges'] = str_replace( - array( '{gaItem}', '{faItem}' ), - array( self::$gaItemId->getPrefixedId(), self::$faItemId->getPrefixedId() ), + array( '{gaItem}', '{faItem}', '{otherItem}' ), + array( self::$gaItemId->getPrefixedId(), self::$faItemId->getPrefixedId(), self::$otherItemId->getPrefixedId() ), $params['badges'] ); } @@ -288,8 +307,8 @@ // Replace the placeholder item ids in the API params if ( isset( $params['badges'] ) ) { $params['badges'] = str_replace( - array( '{gaItem}', '{faItem}' ), - array( self::$gaItemId->getPrefixedId(), self::$faItemId->getPrefixedId() ), + array( '{gaItem}', '{faItem}', '{otherItem}' ), + array( self::$gaItemId->getPrefixedId(), self::$faItemId->getPrefixedId(), self::$otherItemId->getPrefixedId() ), $params['badges'] ); } -- To view, visit https://gerrit.wikimedia.org/r/108337 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If3e7ed5af4009f028884368ec45045b6f691d396 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Bene <benestar.wikime...@googlemail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits