30 private $suggestions = [];
35 private $pageMap = [];
40 private $hasMoreResults;
54 public function __construct( array $suggestions, $hasMoreResults =
false ) {
56 foreach ( $suggestions as $suggestion ) {
57 $pageID = $suggestion->getSuggestedTitleID();
58 if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
59 $this->pageMap[$pageID] =
true;
61 $this->suggestions[] = $suggestion;
69 return $this->hasMoreResults;
77 return $this->suggestions;
85 public function map( $callback ) {
86 return array_map( $callback, $this->suggestions );
95 public function filter( $callback ) {
96 $before = count( $this->suggestions );
97 $this->suggestions = array_values( array_filter( $this->suggestions, $callback ) );
98 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;
137 $removed = array_splice( $this->suggestions, $key, 1 );
138 unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
149 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
152 if ( $this->
getSize() > 0 && $suggestion->
getScore() <= $this->getBestScore() ) {
155 array_unshift( $this->suggestions, $suggestion );
157 $this->pageMap[$pageID] =
true;
165 if ( empty( $this->suggestions ) ) {
168 return $this->suggestions[0]->getScore();
175 if ( empty( $this->suggestions ) ) {
178 return end( $this->suggestions )->getScore();
185 return count( $this->suggestions );
193 if ( count( $this->suggestions ) > $limit ) {
194 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
209 public static function fromTitles( array $titles, $hasMoreResults =
false ) {
210 $score = count( $titles );
211 $suggestions = array_map(
static function (
$title ) use ( &$score ) {
212 return SearchSuggestion::fromTitle( $score--,
$title );
226 public static function fromStrings( array $titles, $hasMoreResults =
false ) {
227 $score = count( $titles );
228 $suggestions = array_map(
static function (
$title ) use ( &$score ) {
229 return SearchSuggestion::fromText( $score--,
$title );
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.