MediaWiki master
SearchSuggestionSet.php
Go to the documentation of this file.
1<?php
2
23
32 private $suggestions = [];
33
37 private $pageMap = [];
38
42 private $hasMoreResults;
43
56 public function __construct( array $suggestions, $hasMoreResults = false ) {
57 $this->hasMoreResults = $hasMoreResults;
58 foreach ( $suggestions as $suggestion ) {
59 $pageID = $suggestion->getSuggestedTitleID();
60 if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
61 $this->pageMap[$pageID] = true;
62 }
63 $this->suggestions[] = $suggestion;
64 }
65 }
66
70 public function hasMoreResults() {
71 return $this->hasMoreResults;
72 }
73
78 public function getSuggestions() {
79 return $this->suggestions;
80 }
81
87 public function map( $callback ) {
88 return array_map( $callback, $this->suggestions );
89 }
90
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 );
101 }
102
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
125 public function appendAll( SearchSuggestionSet $set ) {
126 foreach ( $set->getSuggestions() as $sugg ) {
127 $this->append( $sugg );
128 }
129 }
130
135 public function rescore( $key ) {
136 $removed = array_splice( $this->suggestions, $key, 1 );
137 unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
138 $this->prepend( $removed[0] );
139 }
140
145 public function prepend( SearchSuggestion $suggestion ) {
146 $pageID = $suggestion->getSuggestedTitleID();
147 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
148 return;
149 }
150 if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
151 $suggestion->setScore( $this->getBestScore() + 1 );
152 }
153 array_unshift( $this->suggestions, $suggestion );
154 if ( $pageID ) {
155 $this->pageMap[$pageID] = true;
156 }
157 }
158
165 public function remove( SearchSuggestion $suggestion ): bool {
166 foreach ( $this->suggestions as $k => $s ) {
167 $titleId = $s->getSuggestedTitleID();
168 if ( ( $titleId != null && $titleId === $suggestion->getSuggestedTitleID() )
169 || $s->getText() === $suggestion->getText()
170 ) {
171 array_splice( $this->suggestions, $k, 1 );
172 unset( $this->pageMap[$s->getSuggestedTitleID()] );
173 return true;
174 }
175 }
176 return false;
177 }
178
182 public function getBestScore() {
183 if ( !$this->suggestions ) {
184 return 0;
185 }
186 return $this->suggestions[0]->getScore();
187 }
188
192 public function getWorstScore() {
193 if ( !$this->suggestions ) {
194 return 0;
195 }
196 return end( $this->suggestions )->getScore();
197 }
198
202 public function getSize() {
203 return count( $this->suggestions );
204 }
205
210 public function shrink( $limit ) {
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;
215 }
216 }
217
224 private static function buildPageMap( array $suggestions ): array {
225 $pageMap = [];
226 foreach ( $suggestions as $suggestion ) {
227 $pageID = $suggestion->getSuggestedTitleID();
228 if ( $pageID ) {
229 $pageMap[$pageID] = true;
230 }
231 }
232 return $pageMap;
233 }
234
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 );
249 }, $titles );
250 return new SearchSuggestionSet( $suggestions, $hasMoreResults );
251 }
252
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 );
266 }, $titles );
267 return new SearchSuggestionSet( $suggestions, $hasMoreResults );
268 }
269
273 public static function emptySuggestionSet() {
274 return new SearchSuggestionSet( [] );
275 }
276}
Represents a title within MediaWiki.
Definition Title.php:78
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.
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.
A search suggestion.
getSuggestedTitleID()
Title ID in the case this suggestion is based on a title.
setScore( $score)
Set the suggestion score.
getScore()
Suggestion score.