4 * Trait useful for SearchResultSet implementations.
5 * It holds the functions that are rarely needed to be overridden.
7 * This trait can be used directly by extensions providing a SearchEngine.
11 trait SearchResultSetTrait
{
13 * Set of result's extra data, indexed per result id
14 * and then per data item name.
16 * PAGE_ID => [ augmentor name => data, ... ]
19 private $extraData = [];
22 * Sets augmented data for result set.
23 * @param string $name Extra data item name
24 * @param array[] $data Extra data as PAGEID => data
26 public function setAugmentedData( $name, $data ) {
27 foreach ( $data as $id => $resultData ) {
28 $this->extraData
[$id][$name] = $resultData;
33 * Returns extra data for specific result and store it in SearchResult object.
34 * @param SearchResult $result
36 public function augmentResult( SearchResult
$result ) {
37 $id = $result->getTitle()->getArticleID();
41 $result->setExtensionData( function () use ( $id ) {
42 return $this->extraData
[$id] ??
[];
47 * @return int|null The offset the current page starts at. Typically
48 * this should be null to allow the UI to decide on its own, but in
49 * special cases like interleaved AB tests specifying explicitly is
52 public function getOffset() {
56 final public function getIterator() {
57 return new ArrayIterator( $this->extractResults() );