From: David Causse Date: Thu, 25 Jul 2019 13:48:34 +0000 (+0200) Subject: Add SearchResultSetTrait X-Git-Tag: 1.34.0-rc.0~841^2 X-Git-Url: http://git.heureux-cyclage.org/?a=commitdiff_plain;h=ff6b6a758abdf3d3e452e4e22c35f2287e9070b7;p=lhc%2Fweb%2Fwiklou.git Add SearchResultSetTrait trait meant to hold methods that rarely overridden or that are non trivial to implement. Bug: T228626 Change-Id: I4fba27b22860109d648e830a435d4a036d26ad06 --- diff --git a/autoload.php b/autoload.php index 88d77770bc..3525b48b23 100644 --- a/autoload.php +++ b/autoload.php @@ -1322,6 +1322,7 @@ $wgAutoloadLocalClasses = [ 'SearchPostgres' => __DIR__ . '/includes/search/SearchPostgres.php', 'SearchResult' => __DIR__ . '/includes/search/SearchResult.php', 'SearchResultSet' => __DIR__ . '/includes/search/SearchResultSet.php', + 'SearchResultSetTrait' => __DIR__ . '/includes/search/SearchResultSetTrait.php', 'SearchSqlite' => __DIR__ . '/includes/search/SearchSqlite.php', 'SearchSuggestion' => __DIR__ . '/includes/search/SearchSuggestion.php', 'SearchSuggestionSet' => __DIR__ . '/includes/search/SearchSuggestionSet.php', diff --git a/includes/search/SearchResultSet.php b/includes/search/SearchResultSet.php index b8e40d5c5b..73e33a2df9 100644 --- a/includes/search/SearchResultSet.php +++ b/includes/search/SearchResultSet.php @@ -25,6 +25,7 @@ * @ingroup Search */ class SearchResultSet extends BaseSearchResultSet { + use SearchResultSetTrait; protected $containedSyntax = false; @@ -234,43 +235,4 @@ class SearchResultSet extends BaseSearchResultSet { } return $this->titles; } - - /** - * Sets augmented data for result set. - * @param string $name Extra data item name - * @param array[] $data Extra data as PAGEID => data - */ - public function setAugmentedData( $name, $data ) { - foreach ( $data as $id => $resultData ) { - $this->extraData[$id][$name] = $resultData; - } - } - - /** - * Returns extra data for specific result and store it in SearchResult object. - * @param SearchResult $result - */ - public function augmentResult( SearchResult $result ) { - $id = $result->getTitle()->getArticleID(); - if ( $id === -1 ) { - return; - } - $result->setExtensionData( function () use ( $id ) { - return $this->extraData[$id] ?? []; - } ); - } - - /** - * @return int|null The offset the current page starts at. Typically - * this should be null to allow the UI to decide on its own, but in - * special cases like interleaved AB tests specifying explicitly is - * necessary. - */ - public function getOffset() { - return null; - } - - final public function getIterator() { - return new ArrayIterator( $this->extractResults() ); - } } diff --git a/includes/search/SearchResultSetTrait.php b/includes/search/SearchResultSetTrait.php new file mode 100644 index 0000000000..f36a7b513f --- /dev/null +++ b/includes/search/SearchResultSetTrait.php @@ -0,0 +1,59 @@ + [ augmentor name => data, ... ] + * @var array[] + */ + private $extraData = []; + + /** + * Sets augmented data for result set. + * @param string $name Extra data item name + * @param array[] $data Extra data as PAGEID => data + */ + public function setAugmentedData( $name, $data ) { + foreach ( $data as $id => $resultData ) { + $this->extraData[$id][$name] = $resultData; + } + } + + /** + * Returns extra data for specific result and store it in SearchResult object. + * @param SearchResult $result + */ + public function augmentResult( SearchResult $result ) { + $id = $result->getTitle()->getArticleID(); + if ( $id === -1 ) { + return; + } + $result->setExtensionData( function () use ( $id ) { + return $this->extraData[$id] ?? []; + } ); + } + + /** + * @return int|null The offset the current page starts at. Typically + * this should be null to allow the UI to decide on its own, but in + * special cases like interleaved AB tests specifying explicitly is + * necessary. + */ + public function getOffset() { + return null; + } + + final public function getIterator() { + return new ArrayIterator( $this->extractResults() ); + } +} diff --git a/tests/phpunit/includes/search/SearchResultSetTest.php b/tests/phpunit/includes/search/SearchResultSetTest.php index 2d00a0de17..e9efb227a3 100644 --- a/tests/phpunit/includes/search/SearchResultSetTest.php +++ b/tests/phpunit/includes/search/SearchResultSetTest.php @@ -29,8 +29,8 @@ class SearchResultSetTest extends MediaWikiTestCase { } /** - * @covers SearchResultSet::augmentResult - * @covers SearchResultSet::setAugmentedData + * @covers SearchResultSetTrait::augmentResult + * @covers SearchResultSetTrait::setAugmentedData */ public function testDelayedResultAugment() { $result = SearchResult::newFromTitle( Title::newMainPage() );