Fix use of GenderCache in ApiPageSet::processTitlesArray
[lhc/web/wiklou.git] / includes / search / SearchResultSetTrait.php
1 <?php
2
3 /**
4 * Trait useful for SearchResultSet implementations.
5 * It holds the functions that are rarely needed to be overridden.
6 *
7 * This trait can be used directly by extensions providing a SearchEngine.
8 *
9 * @ingroup Search
10 */
11 trait SearchResultSetTrait {
12 /**
13 * Set of result's extra data, indexed per result id
14 * and then per data item name.
15 * The structure is:
16 * PAGE_ID => [ augmentor name => data, ... ]
17 * @var array[]
18 */
19 private $extraData = [];
20
21 /**
22 * Sets augmented data for result set.
23 * @param string $name Extra data item name
24 * @param array[] $data Extra data as PAGEID => data
25 */
26 public function setAugmentedData( $name, $data ) {
27 foreach ( $data as $id => $resultData ) {
28 $this->extraData[$id][$name] = $resultData;
29 }
30 }
31
32 /**
33 * Returns extra data for specific result and store it in SearchResult object.
34 * @param SearchResult $result
35 */
36 public function augmentResult( SearchResult $result ) {
37 $id = $result->getTitle()->getArticleID();
38 if ( $id === -1 ) {
39 return;
40 }
41 $result->setExtensionData( function () use ( $id ) {
42 return $this->extraData[$id] ?? [];
43 } );
44 }
45
46 /**
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
50 * necessary.
51 */
52 public function getOffset() {
53 return null;
54 }
55
56 final public function getIterator() {
57 return new ArrayIterator( $this->extractResults() );
58 }
59 }