Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.73% |
8 / 11 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SearchResultSetTrait | |
80.00% |
8 / 10 |
|
50.00% |
2 / 4 |
6.29 | |
0.00% |
0 / 1 |
| setAugmentedData | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| augmentResult | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| getOffset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Search; |
| 4 | |
| 5 | use ArrayIterator; |
| 6 | |
| 7 | // @phan-file-suppress PhanUndeclaredMethod |
| 8 | |
| 9 | /** |
| 10 | * Trait useful for SearchResultSet implementations. |
| 11 | * It holds the functions that are rarely needed to be overridden. |
| 12 | * |
| 13 | * This trait can be used directly by extensions providing a SearchEngine. |
| 14 | * |
| 15 | * @ingroup Search |
| 16 | */ |
| 17 | trait SearchResultSetTrait { |
| 18 | /** |
| 19 | * Set of result's extra data, indexed per result id |
| 20 | * and then per data item name. |
| 21 | * The structure is: |
| 22 | * PAGE_ID => [ augmentor name => data, ... ] |
| 23 | * @var array[] |
| 24 | */ |
| 25 | private $extraData = []; |
| 26 | |
| 27 | /** |
| 28 | * Sets augmented data for result set. |
| 29 | * @param string $name Extra data item name |
| 30 | * @param array[] $data Extra data as PAGEID => data |
| 31 | */ |
| 32 | public function setAugmentedData( $name, $data ) { |
| 33 | foreach ( $data as $id => $resultData ) { |
| 34 | $this->extraData[$id][$name] = $resultData; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns extra data for specific result and store it in SearchResult object. |
| 40 | */ |
| 41 | public function augmentResult( SearchResult $result ) { |
| 42 | $id = $result->getTitle()->getArticleID(); |
| 43 | if ( $id === -1 ) { |
| 44 | return; |
| 45 | } |
| 46 | $result->setExtensionData( function () use ( $id ) { |
| 47 | return $this->extraData[$id] ?? []; |
| 48 | } ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return int|null The offset the current page starts at. Typically |
| 53 | * this should be null to allow the UI to decide on its own, but in |
| 54 | * special cases like interleaved AB tests specifying explicitly is |
| 55 | * necessary. |
| 56 | */ |
| 57 | public function getOffset() { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | final public function getIterator(): ArrayIterator { |
| 62 | return new ArrayIterator( $this->extractResults() ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** @deprecated class alias since 1.46 */ |
| 67 | class_alias( SearchResultSetTrait::class, 'SearchResultSetTrait' ); |