MediaWiki 1.42.1
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 array_splice( $this->suggestions, $k, 1 );
175 unset( $this->pageMap[$s->getSuggestedTitleID()] );
176 return true;
177 }
178 }
179 return false;
180 }
181
185 public function getBestScore() {
186 if ( !$this->suggestions ) {
187 return 0;
188 }
189 return $this->suggestions[0]->getScore();
190 }
191
195 public function getWorstScore() {
196 if ( !$this->suggestions ) {
197 return 0;
198 }
199 return end( $this->suggestions )->getScore();
200 }
201
205 public function getSize() {
206 return count( $this->suggestions );
207 }
208
213 public function shrink( $limit ) {
214 if ( count( $this->suggestions ) > $limit ) {
215 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
216 $this->pageMap = self::buildPageMap( $this->suggestions );
217 $this->hasMoreResults = true;
218 }
219 }
220
227 private static function buildPageMap( array $suggestions ): array {
228 $pageMap = [];
229 foreach ( $suggestions as $suggestion ) {
230 $pageID = $suggestion->getSuggestedTitleID();
231 if ( $pageID ) {
232 $pageMap[$pageID] = true;
233 }
234 }
235 return $pageMap;
236 }
237
248 public static function fromTitles( array $titles, $hasMoreResults = false ) {
249 $score = count( $titles );
250 $suggestions = array_map( static function ( $title ) use ( &$score ) {
251 return SearchSuggestion::fromTitle( $score--, $title );
252 }, $titles );
253 return new SearchSuggestionSet( $suggestions, $hasMoreResults );
254 }
255
265 public static function fromStrings( array $titles, $hasMoreResults = false ) {
266 $score = count( $titles );
267 $suggestions = array_map( static function ( $title ) use ( &$score ) {
268 return SearchSuggestion::fromText( $score--, $title );
269 }, $titles );
270 return new SearchSuggestionSet( $suggestions, $hasMoreResults );
271 }
272
276 public static function emptySuggestionSet() {
277 return new SearchSuggestionSet( [] );
278 }
279}
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.