MediaWiki master
SearchSuggestionSet.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Search;
4
12
21 private $suggestions = [];
22
26 private $pageMap = [];
27
31 private $hasMoreResults;
32
45 public function __construct( array $suggestions, $hasMoreResults = false ) {
46 $this->hasMoreResults = $hasMoreResults;
47 foreach ( $suggestions as $suggestion ) {
48 $pageID = $suggestion->getSuggestedTitleID();
49 if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
50 $this->pageMap[$pageID] = true;
51 }
52 $this->suggestions[] = $suggestion;
53 }
54 }
55
59 public function hasMoreResults() {
60 return $this->hasMoreResults;
61 }
62
67 public function getSuggestions() {
68 return $this->suggestions;
69 }
70
76 public function map( $callback ) {
77 return array_map( $callback, $this->suggestions );
78 }
79
86 public function filter( $callback ) {
87 $before = count( $this->suggestions );
88 $this->suggestions = array_values( array_filter( $this->suggestions, $callback ) );
89 return $before - count( $this->suggestions );
90 }
91
97 public function append( SearchSuggestion $suggestion ) {
98 $pageID = $suggestion->getSuggestedTitleID();
99 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
100 return;
101 }
102 if ( $this->getSize() > 0 && $suggestion->getScore() >= $this->getWorstScore() ) {
103 $suggestion->setScore( $this->getWorstScore() - 1 );
104 }
105 $this->suggestions[] = $suggestion;
106 if ( $pageID ) {
107 $this->pageMap[$pageID] = true;
108 }
109 }
110
114 public function appendAll( SearchSuggestionSet $set ) {
115 foreach ( $set->getSuggestions() as $sugg ) {
116 $this->append( $sugg );
117 }
118 }
119
124 public function rescore( $key ) {
125 $removed = array_splice( $this->suggestions, $key, 1 );
126 unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
127 $this->prepend( $removed[0] );
128 }
129
134 public function prepend( SearchSuggestion $suggestion ) {
135 $pageID = $suggestion->getSuggestedTitleID();
136 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
137 return;
138 }
139 if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
140 $suggestion->setScore( $this->getBestScore() + 1 );
141 }
142 array_unshift( $this->suggestions, $suggestion );
143 if ( $pageID ) {
144 $this->pageMap[$pageID] = true;
145 }
146 }
147
154 public function remove( SearchSuggestion $suggestion ): bool {
155 foreach ( $this->suggestions as $k => $s ) {
156 $titleId = $s->getSuggestedTitleID();
157 if ( ( $titleId != null && $titleId === $suggestion->getSuggestedTitleID() )
158 || $s->getText() === $suggestion->getText()
159 ) {
160 array_splice( $this->suggestions, $k, 1 );
161 unset( $this->pageMap[$s->getSuggestedTitleID()] );
162 return true;
163 }
164 }
165 return false;
166 }
167
171 public function getBestScore() {
172 if ( !$this->suggestions ) {
173 return 0;
174 }
175 return $this->suggestions[0]->getScore();
176 }
177
181 public function getWorstScore() {
182 if ( !$this->suggestions ) {
183 return 0;
184 }
185 return end( $this->suggestions )->getScore();
186 }
187
191 public function getSize() {
192 return count( $this->suggestions );
193 }
194
199 public function shrink( $limit ) {
200 if ( count( $this->suggestions ) > $limit ) {
201 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
202 $this->pageMap = self::buildPageMap( $this->suggestions );
203 $this->hasMoreResults = true;
204 }
205 }
206
213 private static function buildPageMap( array $suggestions ): array {
214 $pageMap = [];
215 foreach ( $suggestions as $suggestion ) {
216 $pageID = $suggestion->getSuggestedTitleID();
217 if ( $pageID ) {
218 $pageMap[$pageID] = true;
219 }
220 }
221 return $pageMap;
222 }
223
234 public static function fromTitles( array $titles, $hasMoreResults = false ) {
235 $score = count( $titles );
236 $suggestions = array_map( static function ( $title ) use ( &$score ) {
237 return SearchSuggestion::fromTitle( $score--, $title );
238 }, $titles );
239 return new SearchSuggestionSet( $suggestions, $hasMoreResults );
240 }
241
251 public static function fromStrings( array $titles, $hasMoreResults = false ) {
252 $score = count( $titles );
253 $suggestions = array_map( static function ( $title ) use ( &$score ) {
254 return SearchSuggestion::fromText( $score--, $title );
255 }, $titles );
256 return new SearchSuggestionSet( $suggestions, $hasMoreResults );
257 }
258
262 public static function emptySuggestionSet() {
263 return new SearchSuggestionSet( [] );
264 }
265}
266
268class_alias( SearchSuggestionSet::class, 'SearchSuggestionSet' );
filter( $callback)
Filter the suggestions array.
rescore( $key)
Move the suggestion at index $key to the first position.
getSuggestions()
Get the list of suggestions.
map( $callback)
Call array_map on the suggestions array.
static fromStrings(array $titles, $hasMoreResults=false)
Builds a new set of suggestion based on a string array.
append(SearchSuggestion $suggestion)
Add a new suggestion at the end.
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.
shrink( $limit)
Remove any extra elements in the suggestions set.
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.
Represents a title within MediaWiki.
Definition Title.php:69
Definition of a mapping for the search index field.