jenkins-bot has submitted this change and it was merged. Change subject: \SMW\Subobject, SubobjectParserFunction use HashIdGenerator ......................................................................
\SMW\Subobject, SubobjectParserFunction use HashIdGenerator Will allow [1] to pass [1] https://gerrit.wikimedia.org/r/#/c/80365/ Change-Id: I5045bd114e0e9d845b42013fdcc6a97b5a107652 --- M includes/HashIdGenerator.php M includes/Subobject.php M includes/parserhooks/SubobjectParserFunction.php M includes/query/QueryData.php M tests/phpunit/includes/HashIdGeneratorTest.php M tests/phpunit/includes/SubobjectTest.php 6 files changed, 36 insertions(+), 13 deletions(-) Approvals: Mwjames: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/HashIdGenerator.php b/includes/HashIdGenerator.php index f357f0b..42320b6 100644 --- a/includes/HashIdGenerator.php +++ b/includes/HashIdGenerator.php @@ -38,6 +38,20 @@ } /** + * Sets prefix + * + * @since 1.9 + * + * @param string $prefix + * + * @return HashIdGenerator + */ + public function setPrefix( $prefix ) { + $this->prefix = $prefix; + return $this; + } + + /** * Returns prefix * * @since 1.9 @@ -66,4 +80,5 @@ public function generateId() { return $this->getPrefix() . md5( json_encode( array( $this->hashable ) ) ); } + } diff --git a/includes/Subobject.php b/includes/Subobject.php index b4250dd..435ceb2 100644 --- a/includes/Subobject.php +++ b/includes/Subobject.php @@ -82,16 +82,16 @@ } /** - * Returns an anonymous identifier + * Returns an generated identifier * * @since 1.9 * - * @param string + * @param IdGenerator $id * * @return string */ - public function getAnonymousIdentifier( $string ) { - return '_' . hash( 'md4', $string , false ); + public function generateId( IdGenerator $id ) { + return $id->generateId(); } /** diff --git a/includes/parserhooks/SubobjectParserFunction.php b/includes/parserhooks/SubobjectParserFunction.php index ba1d784..e485af6 100644 --- a/includes/parserhooks/SubobjectParserFunction.php +++ b/includes/parserhooks/SubobjectParserFunction.php @@ -89,7 +89,7 @@ $isAnonymous = in_array( $parameters->getFirst(), array( null, '' ,'-' ) ); if ( $this->objectReference || $isAnonymous ) { - $id = $this->subobject->getAnonymousIdentifier( serialize( $parameters ) ); + $id = $this->subobject->generateId( new HashIdGenerator( $parameters->toArray(), '_' ) ); } else { $id = $parameters->getFirst(); } diff --git a/includes/query/QueryData.php b/includes/query/QueryData.php index 8afb800..231913d 100644 --- a/includes/query/QueryData.php +++ b/includes/query/QueryData.php @@ -61,8 +61,8 @@ * * @param IdGenerator $generator */ - public function setQueryId( IdGenerator $generator ) { - $this->queryId = '_QUERY' . $generator->generateId(); + public function setQueryId( IdGenerator $id ) { + $this->queryId = $this->subobject->generateId( $id->setPrefix( '_QUERY' ) ); } /** diff --git a/tests/phpunit/includes/HashIdGeneratorTest.php b/tests/phpunit/includes/HashIdGeneratorTest.php index e585fc6..2b64a68 100644 --- a/tests/phpunit/includes/HashIdGeneratorTest.php +++ b/tests/phpunit/includes/HashIdGeneratorTest.php @@ -66,6 +66,11 @@ $instance = $this->getInstance( null, $prefix ); $this->assertEquals( $prefix, $instance->getPrefix() ); + // Set prefix + $prefix = $this->getRandomString(); + $instance = $this->getInstance( null, null ); + $this->assertEquals( $prefix, $instance->setPrefix( $prefix )->getPrefix() ); + } /** diff --git a/tests/phpunit/includes/SubobjectTest.php b/tests/phpunit/includes/SubobjectTest.php index 5580f7f..a16fb56 100644 --- a/tests/phpunit/includes/SubobjectTest.php +++ b/tests/phpunit/includes/SubobjectTest.php @@ -3,6 +3,7 @@ namespace SMW\Test; use SMW\DataValueFactory; +use SMW\HashIdGenerator; use SMW\DIProperty; use SMW\Subobject; @@ -68,7 +69,7 @@ $instance = new Subobject( $title ); if ( $id === '' && $id !== null ) { - $id = $instance->getAnonymousIdentifier( $this->getRandomString() ); + $id = $instance->generateId( new HashIdGenerator( $this->getRandomString(), '_' ) ); } $instance->setSemanticData( $id ); @@ -123,6 +124,9 @@ * @test Subobject::getId * @dataProvider getDataProvider * + * @note For an anonymous identifier we only use the first character + * as comparison + * * @since 1.9 * * @param array $test @@ -132,9 +136,8 @@ public function testGetId( array $test, array $expected, array $info ) { $subobject = $this->getInstance( $this->getTitle(), $test['identifier'] ); - // For an anonymous identifier we only use the first character as comparison - $id = $expected['identifier'] === '_' ? substr( $subobject->getId(), 0, 1 ) : $subobject->getId(); + $id = $expected['identifier'] === '_' ? substr( $subobject->getId(), 0, 1 ) : $subobject->getId(); $this->assertEquals( $expected['identifier'], $id, $info['msg'] ); } @@ -230,7 +233,7 @@ } /** - * @test Subobject::getAnonymousIdentifier + * @test Subobject::generateId * @dataProvider getDataProvider * * @since 1.9 @@ -239,12 +242,12 @@ * @param array $expected * @param array $info */ - public function testGetAnonymousIdentifier( array $test, array $expected, array $info ) { + public function testGenerateId( array $test, array $expected, array $info ) { $subobject = $this->getInstance( $this->getTitle() ); $this->assertEquals( '_', - substr( $subobject->getAnonymousIdentifier( $test['identifier'] ), 0, 1 ), + substr( $subobject->generateId( new HashIdGenerator( $test['identifier'], '_' ) ), 0, 1 ), $info['msg'] ); -- To view, visit https://gerrit.wikimedia.org/r/80502 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I5045bd114e0e9d845b42013fdcc6a97b5a107652 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/SemanticMediaWiki Gerrit-Branch: master Gerrit-Owner: Mwjames <[email protected]> Gerrit-Reviewer: Mwjames <[email protected]> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
