MediaWiki  1.34.0
SearchSuggestionSet.php
Go to the documentation of this file.
1 <?php
2 
30  private $suggestions = [];
31 
36  private $pageMap = [];
37 
41  private $hasMoreResults;
42 
55  public function __construct( array $suggestions, $hasMoreResults = false ) {
57  foreach ( $suggestions as $suggestion ) {
58  $pageID = $suggestion->getSuggestedTitleID();
59  if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
60  $this->pageMap[$pageID] = true;
61  }
62  $this->suggestions[] = $suggestion;
63  }
64  }
65 
69  public function hasMoreResults() {
70  return $this->hasMoreResults;
71  }
72 
77  public function getSuggestions() {
78  return $this->suggestions;
79  }
80 
86  public function map( $callback ) {
87  return array_map( $callback, $this->suggestions );
88  }
89 
96  public function filter( $callback ) {
97  $before = count( $this->suggestions );
98  $this->suggestions = array_values( array_filter( $this->suggestions, $callback ) );
99  return $before - count( $this->suggestions );
100  }
101 
109  public function append( SearchSuggestion $suggestion ) {
110  $pageID = $suggestion->getSuggestedTitleID();
111  if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
112  return;
113  }
114  if ( $this->getSize() > 0 && $suggestion->getScore() >= $this->getWorstScore() ) {
115  $suggestion->setScore( $this->getWorstScore() - 1 );
116  }
117  $this->suggestions[] = $suggestion;
118  if ( $pageID ) {
119  $this->pageMap[$pageID] = true;
120  }
121  }
122 
127  public function appendAll( SearchSuggestionSet $set ) {
128  foreach ( $set->getSuggestions() as $sugg ) {
129  $this->append( $sugg );
130  }
131  }
132 
137  public function rescore( $key ) {
138  $removed = array_splice( $this->suggestions, $key, 1 );
139  unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
140  $this->prepend( $removed[0] );
141  }
142 
148  public function prepend( SearchSuggestion $suggestion ) {
149  $pageID = $suggestion->getSuggestedTitleID();
150  if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
151  return;
152  }
153  if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
154  $suggestion->setScore( $this->getBestScore() + 1 );
155  }
156  array_unshift( $this->suggestions, $suggestion );
157  if ( $pageID ) {
158  $this->pageMap[$pageID] = true;
159  }
160  }
161 
165  public function getBestScore() {
166  if ( empty( $this->suggestions ) ) {
167  return 0;
168  }
169  return $this->suggestions[0]->getScore();
170  }
171 
175  public function getWorstScore() {
176  if ( empty( $this->suggestions ) ) {
177  return 0;
178  }
179  return end( $this->suggestions )->getScore();
180  }
181 
185  public function getSize() {
186  return count( $this->suggestions );
187  }
188 
193  public function shrink( $limit ) {
194  if ( count( $this->suggestions ) > $limit ) {
195  $this->suggestions = array_slice( $this->suggestions, 0, $limit );
196  $this->hasMoreResults = true;
197  }
198  }
199 
210  public static function fromTitles( array $titles, $hasMoreResults = false ) {
211  $score = count( $titles );
212  $suggestions = array_map( function ( $title ) use ( &$score ) {
213  return SearchSuggestion::fromTitle( $score--, $title );
214  }, $titles );
216  }
217 
227  public static function fromStrings( array $titles, $hasMoreResults = false ) {
228  $score = count( $titles );
229  $suggestions = array_map( function ( $title ) use ( &$score ) {
230  return SearchSuggestion::fromText( $score--, $title );
231  }, $titles );
233  }
234 
238  public static function emptySuggestionSet() {
239  return new SearchSuggestionSet( [] );
240  }
241 }
SearchSuggestionSet\fromStrings
static fromStrings(array $titles, $hasMoreResults=false)
Builds a new set of suggestion based on a string array.
Definition: SearchSuggestionSet.php:227
SearchSuggestionSet\map
map( $callback)
Call array_map on the suggestions array.
Definition: SearchSuggestionSet.php:86
SearchSuggestionSet\getSize
getSize()
Definition: SearchSuggestionSet.php:185
SearchSuggestionSet\$hasMoreResults
bool $hasMoreResults
Are more results available?
Definition: SearchSuggestionSet.php:41
SearchSuggestion\setScore
setScore( $score)
Set the suggestion score.
Definition: SearchSuggestion.php:139
SearchSuggestionSet\prepend
prepend(SearchSuggestion $suggestion)
Add a new suggestion at the top.
Definition: SearchSuggestionSet.php:148
SearchSuggestionSet\hasMoreResults
hasMoreResults()
Definition: SearchSuggestionSet.php:69
SearchSuggestionSet\__construct
__construct(array $suggestions, $hasMoreResults=false)
Builds a new set of suggestions.
Definition: SearchSuggestionSet.php:55
SearchSuggestion\fromTitle
static fromTitle( $score, Title $title)
Create suggestion from Title.
Definition: SearchSuggestion.php:166
SearchSuggestionSet\fromTitles
static fromTitles(array $titles, $hasMoreResults=false)
Builds a new set of suggestion based on a title array.
Definition: SearchSuggestionSet.php:210
SearchSuggestionSet\getSuggestions
getSuggestions()
Get the list of suggestions.
Definition: SearchSuggestionSet.php:77
SearchSuggestionSet\getBestScore
getBestScore()
Definition: SearchSuggestionSet.php:165
SearchSuggestion\getSuggestedTitleID
getSuggestedTitleID()
Title ID in the case this suggestion is based on a title.
Definition: SearchSuggestion.php:115
SearchSuggestionSet\appendAll
appendAll(SearchSuggestionSet $set)
Add suggestion set to the end of the current one.
Definition: SearchSuggestionSet.php:127
SearchSuggestion
Search suggestion.
Definition: SearchSuggestion.php:25
$title
$title
Definition: testCompression.php:34
SearchSuggestionSet\$pageMap
array $pageMap
Definition: SearchSuggestionSet.php:36
SearchSuggestionSet\filter
filter( $callback)
Filter the suggestions array.
Definition: SearchSuggestionSet.php:96
SearchSuggestionSet\shrink
shrink( $limit)
Remove any extra elements in the suggestions set.
Definition: SearchSuggestionSet.php:193
SearchSuggestionSet\rescore
rescore( $key)
Move the suggestion at index $key to the first position.
Definition: SearchSuggestionSet.php:137
SearchSuggestionSet
Search suggestion sets.
Definition: SearchSuggestionSet.php:26
SearchSuggestionSet\getWorstScore
getWorstScore()
Definition: SearchSuggestionSet.php:175
SearchSuggestionSet\emptySuggestionSet
static emptySuggestionSet()
Definition: SearchSuggestionSet.php:238
SearchSuggestionSet\append
append(SearchSuggestion $suggestion)
Add a new suggestion at the end.
Definition: SearchSuggestionSet.php:109
SearchSuggestion\fromText
static fromText( $score, $text)
Create suggestion from text Will also create a title if text if not empty.
Definition: SearchSuggestion.php:177
SearchSuggestion\getScore
getScore()
Suggestion score.
Definition: SearchSuggestion.php:131
SearchSuggestionSet\$suggestions
SearchSuggestion[] $suggestions
Definition: SearchSuggestionSet.php:30