jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/336900 )
Change subject: Allow fields to define their own merge strategy via callback. ...................................................................... Allow fields to define their own merge strategy via callback. Change-Id: Ic1cc1581f07381224d3f4fcba4feb1eac7085057 --- M includes/search/SearchIndexFieldDefinition.php M tests/phpunit/includes/search/SearchIndexFieldTest.php 2 files changed, 39 insertions(+), 6 deletions(-) Approvals: EBernhardson: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/search/SearchIndexFieldDefinition.php b/includes/search/SearchIndexFieldDefinition.php index 910fd77..04344fd 100644 --- a/includes/search/SearchIndexFieldDefinition.php +++ b/includes/search/SearchIndexFieldDefinition.php @@ -34,6 +34,11 @@ protected $subfields = []; /** + * @var callable + */ + private $mergeCallback; + + /** * SearchIndexFieldDefinition constructor. * @param string $name Field name * @param int $type Index type @@ -91,6 +96,9 @@ * @return SearchIndexField|false New definition or false if not mergeable. */ public function merge( SearchIndexField $that ) { + if ( !empty( $this->mergeCallback ) ) { + return call_user_func( $this->mergeCallback, $this, $that ); + } // TODO: which definitions may be compatible? if ( ( $that instanceof self ) && $this->type === $that->type && $this->flags === $that->flags && $this->type !== self::INDEX_TYPE_NESTED @@ -125,4 +133,11 @@ */ abstract public function getMapping( SearchEngine $engine ); + /** + * Set field-specific merge strategy. + * @param callable $callback + */ + public function setMergeCallback( $callback ) { + $this->mergeCallback = $callback; + } } diff --git a/tests/phpunit/includes/search/SearchIndexFieldTest.php b/tests/phpunit/includes/search/SearchIndexFieldTest.php index 9c10e49..a5a1b7a 100644 --- a/tests/phpunit/includes/search/SearchIndexFieldTest.php +++ b/tests/phpunit/includes/search/SearchIndexFieldTest.php @@ -18,14 +18,23 @@ /** * @dataProvider getMergeCases + * @param $t1 + * @param $n1 + * @param $t2 + * @param $n2 + * @param $result */ public function testMerge( $t1, $n1, $t2, $n2, $result ) { - $field1 = $this->getMockBuilder( 'SearchIndexFieldDefinition' ) - ->setMethods( [ 'getMapping' ] ) - ->setConstructorArgs( [ $n1, $t1 ] )->getMock(); - $field2 = $this->getMockBuilder( 'SearchIndexFieldDefinition' ) - ->setMethods( [ 'getMapping' ] ) - ->setConstructorArgs( [ $n2, $t2 ] )->getMock(); + $field1 = + $this->getMockBuilder( SearchIndexFieldDefinition::class ) + ->setMethods( [ 'getMapping' ] ) + ->setConstructorArgs( [ $n1, $t1 ] ) + ->getMock(); + $field2 = + $this->getMockBuilder( SearchIndexFieldDefinition::class ) + ->setMethods( [ 'getMapping' ] ) + ->setConstructorArgs( [ $n2, $t2 ] ) + ->getMock(); if ( $result ) { $this->assertNotFalse( $field1->merge( $field2 ) ); @@ -35,5 +44,14 @@ $field1->setFlag( 0xFF ); $this->assertFalse( $field1->merge( $field2 ) ); + + $field1->setMergeCallback( + function ( $this, $that ) { + return "test"; + } + ); + $this->assertEquals( "test", $field1->merge( $field2 ) ); + } + } -- To view, visit https://gerrit.wikimedia.org/r/336900 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic1cc1581f07381224d3f4fcba4feb1eac7085057 Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Smalyshev <smalys...@wikimedia.org> Gerrit-Reviewer: DCausse <dcau...@wikimedia.org> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org> Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits