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
110 public function append( SearchSuggestion $suggestion ) {
111 $pageID = $suggestion->getSuggestedTitleID();
112 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
113 return;
114 }
115 if ( $this->getSize() > 0 && $suggestion->getScore() >= $this->getWorstScore() ) {
116 $suggestion->setScore( $this->getWorstScore() - 1 );
117 }
118 $this->suggestions[] = $suggestion;
119 if ( $pageID ) {
120 $this->pageMap[$pageID] = true;
121 }
122 }
123
128 public function appendAll( SearchSuggestionSet $set ) {
129 foreach ( $set->getSuggestions() as $sugg ) {
130 $this->append( $sugg );
131 }
132 }
133
138 public function rescore( $key ) {
139 $removed = array_splice( $this->suggestions, $key, 1 );
140 unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
141 $this->prepend( $removed[0] );
142 }
143
149 public function prepend( SearchSuggestion $suggestion ) {
150 $pageID = $suggestion->getSuggestedTitleID();
151 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
152 return;
153 }
154 if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
155 $suggestion->setScore( $this->getBestScore() + 1 );
156 }
157 array_unshift( $this->suggestions, $suggestion );
158 if ( $pageID ) {
159 $this->pageMap[$pageID] = true;
160 }
161 }
162
169 public function remove( SearchSuggestion $suggestion ): bool {
170 foreach ( $this->suggestions as $k => $s ) {
171 $titleId = $s->getSuggestedTitleID();
172 if ( ( $titleId != null && $titleId === $suggestion->getSuggestedTitleID() )
173 || $s->getText() === $suggestion->getText()
174 ) {
175 array_splice( $this->suggestions, $k, 1 );
176 unset( $this->pageMap[$s->getSuggestedTitleID()] );
177 return true;
178 }
179 }
180 return false;
181 }
182
186 public function getBestScore() {
187 if ( !$this->suggestions ) {
188 return 0;
189 }
190 return $this->suggestions[0]->getScore();
191 }
192
196 public function getWorstScore() {
197 if ( !$this->suggestions ) {
198 return 0;
199 }
200 return end( $this->suggestions )->getScore();
201 }
202
206 public function getSize() {
207 return count( $this->suggestions );
208 }
209
214 public function shrink( $limit ) {
215 if ( count( $this->suggestions ) > $limit ) {
216 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
217 $this->pageMap = self::buildPageMap( $this->suggestions );
218 $this->hasMoreResults = true;
219 }
220 }
221
228 private static function buildPageMap( array $suggestions ): array {
229 $pageMap = [];
230 foreach ( $suggestions as $suggestion ) {
231 $pageID = $suggestion->getSuggestedTitleID();
232 if ( $pageID ) {
233 $pageMap[$pageID] = true;
234 }
235 }
236 return $pageMap;
237 }
238
249 public static function fromTitles( array $titles, $hasMoreResults = false ) {
250 $score = count( $titles );
251 $suggestions = array_map( static function ( $title ) use ( &$score ) {
252 return SearchSuggestion::fromTitle( $score--, $title );
253 }, $titles );
254 return new SearchSuggestionSet( $suggestions, $hasMoreResults );
255 }
256
266 public static function fromStrings( array $titles, $hasMoreResults = false ) {
267 $score = count( $titles );
268 $suggestions = array_map( static function ( $title ) use ( &$score ) {
269 return SearchSuggestion::fromText( $score--, $title );
270 }, $titles );
271 return new SearchSuggestionSet( $suggestions, $hasMoreResults );
272 }
273
277 public static function emptySuggestionSet() {
278 return new SearchSuggestionSet( [] );
279 }
280}
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.