MediaWiki  master
SearchSuggestionSet.php
Go to the documentation of this file.
1 <?php
2 
30  private $suggestions = [];
31 
35  private $pageMap = [];
36 
40  private $hasMoreResults;
41 
54  public function __construct( array $suggestions, $hasMoreResults = false ) {
56  foreach ( $suggestions as $suggestion ) {
57  $pageID = $suggestion->getSuggestedTitleID();
58  if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
59  $this->pageMap[$pageID] = true;
60  }
61  $this->suggestions[] = $suggestion;
62  }
63  }
64 
68  public function hasMoreResults() {
69  return $this->hasMoreResults;
70  }
71 
76  public function getSuggestions() {
77  return $this->suggestions;
78  }
79 
85  public function map( $callback ) {
86  return array_map( $callback, $this->suggestions );
87  }
88 
95  public function filter( $callback ) {
96  $before = count( $this->suggestions );
97  $this->suggestions = array_values( array_filter( $this->suggestions, $callback ) );
98  return $before - count( $this->suggestions );
99  }
100 
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 
126  public function appendAll( SearchSuggestionSet $set ) {
127  foreach ( $set->getSuggestions() as $sugg ) {
128  $this->append( $sugg );
129  }
130  }
131 
136  public function rescore( $key ) {
137  $removed = array_splice( $this->suggestions, $key, 1 );
138  unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
139  $this->prepend( $removed[0] );
140  }
141 
147  public function prepend( SearchSuggestion $suggestion ) {
148  $pageID = $suggestion->getSuggestedTitleID();
149  if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
150  return;
151  }
152  if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
153  $suggestion->setScore( $this->getBestScore() + 1 );
154  }
155  array_unshift( $this->suggestions, $suggestion );
156  if ( $pageID ) {
157  $this->pageMap[$pageID] = true;
158  }
159  }
160 
164  public function getBestScore() {
165  if ( empty( $this->suggestions ) ) {
166  return 0;
167  }
168  return $this->suggestions[0]->getScore();
169  }
170 
174  public function getWorstScore() {
175  if ( empty( $this->suggestions ) ) {
176  return 0;
177  }
178  return end( $this->suggestions )->getScore();
179  }
180 
184  public function getSize() {
185  return count( $this->suggestions );
186  }
187 
192  public function shrink( $limit ) {
193  if ( count( $this->suggestions ) > $limit ) {
194  $this->suggestions = array_slice( $this->suggestions, 0, $limit );
195  $this->hasMoreResults = true;
196  }
197  }
198 
209  public static function fromTitles( array $titles, $hasMoreResults = false ) {
210  $score = count( $titles );
211  $suggestions = array_map( function ( $title ) use ( &$score ) {
212  return SearchSuggestion::fromTitle( $score--, $title );
213  }, $titles );
215  }
216 
226  public static function fromStrings( array $titles, $hasMoreResults = false ) {
227  $score = count( $titles );
228  $suggestions = array_map( function ( $title ) use ( &$score ) {
229  return SearchSuggestion::fromText( $score--, $title );
230  }, $titles );
232  }
233 
237  public static function emptySuggestionSet() {
238  return new SearchSuggestionSet( [] );
239  }
240 }
SearchSuggestionSet\fromStrings
static fromStrings(array $titles, $hasMoreResults=false)
Builds a new set of suggestion based on a string array.
Definition: SearchSuggestionSet.php:226
SearchSuggestionSet\map
map( $callback)
Call array_map on the suggestions array.
Definition: SearchSuggestionSet.php:85
SearchSuggestionSet\getSize
getSize()
Definition: SearchSuggestionSet.php:184
SearchSuggestionSet\$hasMoreResults
bool $hasMoreResults
Are more results available?
Definition: SearchSuggestionSet.php:40
SearchSuggestion\setScore
setScore( $score)
Set the suggestion score.
Definition: SearchSuggestion.php:138
SearchSuggestionSet\prepend
prepend(SearchSuggestion $suggestion)
Add a new suggestion at the top.
Definition: SearchSuggestionSet.php:147
SearchSuggestionSet\hasMoreResults
hasMoreResults()
Definition: SearchSuggestionSet.php:68
SearchSuggestionSet\__construct
__construct(array $suggestions, $hasMoreResults=false)
Builds a new set of suggestions.
Definition: SearchSuggestionSet.php:54
SearchSuggestion\fromTitle
static fromTitle( $score, Title $title)
Create suggestion from Title.
Definition: SearchSuggestion.php:165
SearchSuggestionSet\fromTitles
static fromTitles(array $titles, $hasMoreResults=false)
Builds a new set of suggestion based on a title array.
Definition: SearchSuggestionSet.php:209
SearchSuggestionSet\getSuggestions
getSuggestions()
Get the list of suggestions.
Definition: SearchSuggestionSet.php:76
SearchSuggestionSet\getBestScore
getBestScore()
Definition: SearchSuggestionSet.php:164
SearchSuggestion\getSuggestedTitleID
getSuggestedTitleID()
Title ID in the case this suggestion is based on a title.
Definition: SearchSuggestion.php:114
SearchSuggestionSet\appendAll
appendAll(SearchSuggestionSet $set)
Add suggestion set to the end of the current one.
Definition: SearchSuggestionSet.php:126
SearchSuggestion
Search suggestion.
Definition: SearchSuggestion.php:25
$title
$title
Definition: testCompression.php:38
SearchSuggestionSet\$pageMap
array $pageMap
Definition: SearchSuggestionSet.php:35
SearchSuggestionSet\filter
filter( $callback)
Filter the suggestions array.
Definition: SearchSuggestionSet.php:95
SearchSuggestionSet\shrink
shrink( $limit)
Remove any extra elements in the suggestions set.
Definition: SearchSuggestionSet.php:192
SearchSuggestionSet\rescore
rescore( $key)
Move the suggestion at index $key to the first position.
Definition: SearchSuggestionSet.php:136
SearchSuggestionSet
Search suggestion sets.
Definition: SearchSuggestionSet.php:26
SearchSuggestionSet\getWorstScore
getWorstScore()
Definition: SearchSuggestionSet.php:174
SearchSuggestionSet\emptySuggestionSet
static emptySuggestionSet()
Definition: SearchSuggestionSet.php:237
SearchSuggestionSet\append
append(SearchSuggestion $suggestion)
Add a new suggestion at the end.
Definition: SearchSuggestionSet.php:108
SearchSuggestion\fromText
static fromText( $score, $text)
Create suggestion from text Will also create a title if text if not empty.
Definition: SearchSuggestion.php:176
SearchSuggestion\getScore
getScore()
Suggestion score.
Definition: SearchSuggestion.php:130
SearchSuggestionSet\$suggestions
SearchSuggestion[] $suggestions
Definition: SearchSuggestionSet.php:30