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;
167 if ( empty( $this->suggestions ) ) {
170 return $this->suggestions[0]->getScore();
177 if ( empty( $this->suggestions ) ) {
180 return end( $this->suggestions )->getScore();
187 return count( $this->suggestions );
195 if ( count( $this->suggestions ) > $limit ) {
196 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
211 public static function fromTitles( array $titles, $hasMoreResults =
false ) {
212 $score = count( $titles );
213 $suggestions = array_map(
static function (
$title ) use ( &$score ) {
214 return SearchSuggestion::fromTitle( $score--,
$title );
228 public static function fromStrings( array $titles, $hasMoreResults =
false ) {
229 $score = count( $titles );
230 $suggestions = array_map(
static function (
$title ) use ( &$score ) {
231 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.