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 );
110 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
113 if ( $this->
getSize() > 0 && $suggestion->
getScore() >= $this->getWorstScore() ) {
116 $this->suggestions[] = $suggestion;
118 $this->pageMap[$pageID] =
true;
136 $removed = array_splice( $this->suggestions, $key, 1 );
137 unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
147 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
150 if ( $this->
getSize() > 0 && $suggestion->
getScore() <= $this->getBestScore() ) {
153 array_unshift( $this->suggestions, $suggestion );
155 $this->pageMap[$pageID] =
true;
166 foreach ( $this->suggestions as $k => $s ) {
167 $titleId = $s->getSuggestedTitleID();
168 if ( ( $titleId !=
null && $titleId === $suggestion->getSuggestedTitleID() )
169 || $s->getText() === $suggestion->getText()
171 array_splice( $this->suggestions, $k, 1 );
172 unset( $this->pageMap[$s->getSuggestedTitleID()] );
183 if ( !$this->suggestions ) {
186 return $this->suggestions[0]->getScore();
193 if ( !$this->suggestions ) {
196 return end( $this->suggestions )->getScore();
203 return count( $this->suggestions );
211 if ( count( $this->suggestions ) > $limit ) {
212 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
213 $this->pageMap = self::buildPageMap( $this->suggestions );
214 $this->hasMoreResults =
true;
224 private static function buildPageMap( array $suggestions ): array {
226 foreach ( $suggestions as $suggestion ) {
227 $pageID = $suggestion->getSuggestedTitleID();
229 $pageMap[$pageID] =
true;
245 public static function fromTitles( array $titles, $hasMoreResults =
false ) {
246 $score = count( $titles );
247 $suggestions = array_map(
static function ( $title ) use ( &$score ) {
248 return SearchSuggestion::fromTitle( $score--, $title );
262 public static function fromStrings( array $titles, $hasMoreResults =
false ) {
263 $score = count( $titles );
264 $suggestions = array_map(
static function ( $title ) use ( &$score ) {
265 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.