Michał Łazowik has uploaded a new change for review. https://gerrit.wikimedia.org/r/134727
Change subject: Add getting entity id from page title ...................................................................... Add getting entity id from page title Needed for things like bug64515. Bug: 65507 Change-Id: Iaf552fe76b1e2e9c8a5f33cf5c0faa43c53c849b --- M lib/includes/store/EntityTitleLookup.php M repo/includes/content/EntityContentFactory.php M repo/tests/phpunit/includes/content/EntityContentFactoryTest.php 3 files changed, 90 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/27/134727/1 diff --git a/lib/includes/store/EntityTitleLookup.php b/lib/includes/store/EntityTitleLookup.php index 9991531..ba7f5f0 100644 --- a/lib/includes/store/EntityTitleLookup.php +++ b/lib/includes/store/EntityTitleLookup.php @@ -13,6 +13,7 @@ * * @licence GNU GPL v2+ * @author Daniel Kinzler + * @author Michał Łazowik */ interface EntityTitleLookup { @@ -34,6 +35,16 @@ public function getTitleForId( EntityId $id ); /** + * @since 0.5 + * + * @param Title $title + * + * @throws InvalidArgumentException + * @return EntityId + */ + public function getIdForTitle( Title $title ); + + /** * Determines what namespace is suitable for the given type of entities. * * @since 0.5 diff --git a/repo/includes/content/EntityContentFactory.php b/repo/includes/content/EntityContentFactory.php index fa77d86..f595e23 100644 --- a/repo/includes/content/EntityContentFactory.php +++ b/repo/includes/content/EntityContentFactory.php @@ -10,6 +10,8 @@ use User; use Revision; use WikiPage; +use Wikibase\DataModel\Entity\BasicEntityIdParser; +use Wikibase\DataModel\Entity\EntityIdParsingException; /** * Factory for EntityContent objects. @@ -18,6 +20,7 @@ * * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroended...@gmail.com > + * @author Michał Łazowik */ class EntityContentFactory implements EntityTitleLookup, EntityPermissionChecker { @@ -136,6 +139,26 @@ } /** + * @since 0.5 + * + * @param Title $title + * + * @throws InvalidArgumentException + * @return EntityId + */ + public function getIdForTitle( Title $title ) { + $entityIdParser = new BasicEntityIdParser; + + try { + $entityId = $entityIdParser->parse( $title->getText() ); + } catch ( EntityIdParsingException $parseException ) { + throw new InvalidArgumentException( $title->getText() . ' is not a title of an entity' ); + } + + return $entityId; + } + + /** * Determines what namespace is suitable for the given type of entities. * * @since 0.5 diff --git a/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php b/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php index 8af5553..3ab1e26 100644 --- a/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php +++ b/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php @@ -3,8 +3,12 @@ namespace Wikibase\Test; use InvalidArgumentException; +use Title; +use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\ItemId; +use Wikibase\DataModel\Entity\PropertyId; use Wikibase\EntityContentFactory; +use Wikibase\NamespaceUtils; use Wikibase\Repo\WikibaseRepo; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\Property; @@ -23,6 +27,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroended...@gmail.com > * @author Daniel Kinzler + * @author Michał Łazowik */ class EntityContentFactoryTest extends \MediaWikiTestCase { @@ -71,6 +76,57 @@ $this->assertEquals( 'Q42', $title->getText() ); } + public function entityTitleProvider() { + $argLists = array(); + + $argLists[] = array( + Title::newFromText( + "Q42", + NamespaceUtils::getEntityNamespace( CONTENT_MODEL_WIKIBASE_ITEM ) + ), + new ItemId( 'Q42' ) + ); + + $argLists[] = array( + Title::newFromText( + "P13", + NamespaceUtils::getEntityNamespace( CONTENT_MODEL_WIKIBASE_PROPERTY ) + ), + new PropertyId( 'P13' ) + ); + + return $argLists; + } + + public function invalidEntityTitleProvider() { + $argLists = array(); + + $argLists[] = array( Title::newFromText( "Nyan", NS_SPECIAL ) ); + $argLists[] = array( Title::newFromText( "Poland", NS_MAIN ) ); + + return $argLists; + } + + /** + * @dataProvider entityTitleProvider + */ + public function testGetIdForTitle( Title $title, EntityId $expectedId ) { + $factory = $this->newFactory(); + + $actualId = $factory->getIdForTitle( $title ); + + $this->assertEquals( $expectedId, $actualId ); + } + + /** + * @dataProvider invalidEntityTitleProvider + * @expectedException InvalidArgumentException + */ + public function testInvalidGetIdForTitle( Title $title ) { + $factory = $this->newFactory(); + $actualId = $factory->getIdForTitle( $title ); + } + public function testGetNamespaceForType() { $factory = $this->newFactory(); $id = new ItemId( 'Q42' ); -- To view, visit https://gerrit.wikimedia.org/r/134727 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iaf552fe76b1e2e9c8a5f33cf5c0faa43c53c849b Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Michał Łazowik <mlazo...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits