MediaWiki REL1_35
SearchSuggestionSet.php
Go to the documentation of this file.
1<?php
2
30 private $suggestions = [];
31
35 private $pageMap = [];
36
41
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;
60 }
61 $this->suggestions[] = $suggestion;
62 }
63 }
64
68 public function hasMoreResults() {
70 }
71
76 public function getSuggestions() {
77 return $this->suggestions;
78 }
79
85 public function map( $callback ) {
86 return array_map( $callback, $this->suggestions );
87 }
88
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 );
99 }
100
108 public function append( SearchSuggestion $suggestion ) {
109 $pageID = $suggestion->getSuggestedTitleID();
110 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
111 return;
112 }
113 if ( $this->getSize() > 0 && $suggestion->getScore() >= $this->getWorstScore() ) {
114 $suggestion->setScore( $this->getWorstScore() - 1 );
115 }
116 $this->suggestions[] = $suggestion;
117 if ( $pageID ) {
118 $this->pageMap[$pageID] = true;
119 }
120 }
121
126 public function appendAll( SearchSuggestionSet $set ) {
127 foreach ( $set->getSuggestions() as $sugg ) {
128 $this->append( $sugg );
129 }
130 }
131
136 public function rescore( $key ) {
137 $removed = array_splice( $this->suggestions, $key, 1 );
138 unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
139 $this->prepend( $removed[0] );
140 }
141
147 public function prepend( SearchSuggestion $suggestion ) {
148 $pageID = $suggestion->getSuggestedTitleID();
149 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
150 return;
151 }
152 if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
153 $suggestion->setScore( $this->getBestScore() + 1 );
154 }
155 array_unshift( $this->suggestions, $suggestion );
156 if ( $pageID ) {
157 $this->pageMap[$pageID] = true;
158 }
159 }
160
164 public function getBestScore() {
165 if ( empty( $this->suggestions ) ) {
166 return 0;
167 }
168 return $this->suggestions[0]->getScore();
169 }
170
174 public function getWorstScore() {
175 if ( empty( $this->suggestions ) ) {
176 return 0;
177 }
178 return end( $this->suggestions )->getScore();
179 }
180
184 public function getSize() {
185 return count( $this->suggestions );
186 }
187
192 public function shrink( $limit ) {
193 if ( count( $this->suggestions ) > $limit ) {
194 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
195 $this->hasMoreResults = true;
196 }
197 }
198
209 public static function fromTitles( array $titles, $hasMoreResults = false ) {
210 $score = count( $titles );
211 $suggestions = array_map( function ( $title ) use ( &$score ) {
212 return SearchSuggestion::fromTitle( $score--, $title );
213 }, $titles );
215 }
216
226 public static function fromStrings( array $titles, $hasMoreResults = false ) {
227 $score = count( $titles );
228 $suggestions = array_map( function ( $title ) use ( &$score ) {
229 return SearchSuggestion::fromText( $score--, $title );
230 }, $titles );
232 }
233
237 public static function emptySuggestionSet() {
238 return new SearchSuggestionSet( [] );
239 }
240}
Search suggestion sets.
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.
map( $callback)
Call array_map on the suggestions array.
SearchSuggestion[] $suggestions
prepend(SearchSuggestion $suggestion)
Add a new suggestion at the top.
__construct(array $suggestions, $hasMoreResults=false)
Builds a new set of suggestions.
bool $hasMoreResults
Are more results available?
Search suggestion.
getSuggestedTitleID()
Title ID in the case this suggestion is based on a title.
setScore( $score)
Set the suggestion score.
getScore()
Suggestion score.