32 private $suggestions = [];
37 private $pageMap = [];
42 private $hasMoreResults;
56 public function __construct( array $suggestions, $hasMoreResults =
false ) {
58 foreach ( $suggestions as $suggestion ) {
59 $pageID = $suggestion->getSuggestedTitleID();
60 if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
61 $this->pageMap[$pageID] =
true;
63 $this->suggestions[] = $suggestion;
71 return $this->hasMoreResults;
79 return $this->suggestions;
87 public function map( $callback ) {
88 return array_map( $callback, $this->suggestions );
97 public function filter( $callback ) {
98 $before = count( $this->suggestions );
99 $this->suggestions = array_values( array_filter( $this->suggestions, $callback ) );
100 return $before - count( $this->suggestions );
112 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
115 if ( $this->
getSize() > 0 && $suggestion->
getScore() >= $this->getWorstScore() ) {
118 $this->suggestions[] = $suggestion;
120 $this->pageMap[$pageID] =
true;
139 $removed = array_splice( $this->suggestions, $key, 1 );
140 unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
151 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
154 if ( $this->
getSize() > 0 && $suggestion->
getScore() <= $this->getBestScore() ) {
157 array_unshift( $this->suggestions, $suggestion );
159 $this->pageMap[$pageID] =
true;
170 foreach ( $this->suggestions as $k => $s ) {
171 $titleId = $s->getSuggestedTitleID();
172 if ( ( $titleId !=
null && $titleId === $suggestion->getSuggestedTitleID() )
173 || $s->getText() === $suggestion->getText()
175 array_splice( $this->suggestions, $k, 1 );
176 unset( $this->pageMap[$s->getSuggestedTitleID()] );
187 if ( !$this->suggestions ) {
190 return $this->suggestions[0]->getScore();
197 if ( !$this->suggestions ) {
200 return end( $this->suggestions )->getScore();
207 return count( $this->suggestions );
215 if ( count( $this->suggestions ) > $limit ) {
216 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
217 $this->pageMap = self::buildPageMap( $this->suggestions );
218 $this->hasMoreResults =
true;
228 private static function buildPageMap( array $suggestions ): array {
230 foreach ( $suggestions as $suggestion ) {
231 $pageID = $suggestion->getSuggestedTitleID();
233 $pageMap[$pageID] =
true;
249 public static function fromTitles( array $titles, $hasMoreResults =
false ) {
250 $score = count( $titles );
251 $suggestions = array_map(
static function ( $title ) use ( &$score ) {
252 return SearchSuggestion::fromTitle( $score--, $title );
266 public static function fromStrings( array $titles, $hasMoreResults =
false ) {
267 $score = count( $titles );
268 $suggestions = array_map(
static function ( $title ) use ( &$score ) {
269 return SearchSuggestion::fromText( $score--, $title );
A set of search suggestions.
append(SearchSuggestion $suggestion)
Add a new suggestion at the end.
filter( $callback)
Filter the suggestions array.
rescore( $key)
Move the suggestion at index $key to the first position.
shrink( $limit)
Remove any extra elements in the suggestions set.
static fromStrings(array $titles, $hasMoreResults=false)
Builds a new set of suggestion based on a string array.
appendAll(SearchSuggestionSet $set)
Add suggestion set to the end of the current one.
static fromTitles(array $titles, $hasMoreResults=false)
Builds a new set of suggestion based on a title array.
getSuggestions()
Get the list of suggestions.
static emptySuggestionSet()
map( $callback)
Call array_map on the suggestions array.
prepend(SearchSuggestion $suggestion)
Add a new suggestion at the top.
__construct(array $suggestions, $hasMoreResults=false)
Builds a new set of suggestions.
getSuggestedTitleID()
Title ID in the case this suggestion is based on a title.
setScore( $score)
Set the suggestion score.
getScore()
Suggestion score.