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.
10 * @phan-file-suppress PhanUndeclaredMethod
12 trait SearchResultSetTrait
{
14 * Set of result's extra data, indexed per result id
15 * and then per data item name.
17 * PAGE_ID => [ augmentor name => data, ... ]
20 private $extraData = [];
23 * Sets augmented data for result set.
24 * @param string $name Extra data item name
25 * @param array[] $data Extra data as PAGEID => data
27 public function setAugmentedData( $name, $data ) {
28 foreach ( $data as $id => $resultData ) {
29 $this->extraData
[$id][$name] = $resultData;
34 * Returns extra data for specific result and store it in SearchResult object.
35 * @param SearchResult $result
37 public function augmentResult( SearchResult
$result ) {
38 $id = $result->getTitle()->getArticleID();
42 $result->setExtensionData( function () use ( $id ) {
43 return $this->extraData
[$id] ??
[];
48 * @return int|null The offset the current page starts at. Typically
49 * this should be null to allow the UI to decide on its own, but in
50 * special cases like interleaved AB tests specifying explicitly is
53 public function getOffset() {
57 final public function getIterator(): ArrayIterator
{
58 return new ArrayIterator( $this->extractResults() );