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 
166  public function getBestScore() {
167  if ( !$this->suggestions ) {
168  return 0;
169  }
170  return $this->suggestions[0]->getScore();
171  }
172 
176  public function getWorstScore() {
177  if ( !$this->suggestions ) {
178  return 0;
179  }
180  return end( $this->suggestions )->getScore();
181  }
182 
186  public function getSize() {
187  return count( $this->suggestions );
188  }
189 
194  public function shrink( $limit ) {
195  if ( count( $this->suggestions ) > $limit ) {
196  $this->suggestions = array_slice( $this->suggestions, 0, $limit );
197  $this->hasMoreResults = true;
198  }
199  }
200 
211  public static function fromTitles( array $titles, $hasMoreResults = false ) {
212  $score = count( $titles );
213  $suggestions = array_map( static function ( $title ) use ( &$score ) {
214  return SearchSuggestion::fromTitle( $score--, $title );
215  }, $titles );
216  return new SearchSuggestionSet( $suggestions, $hasMoreResults );
217  }
218 
228  public static function fromStrings( array $titles, $hasMoreResults = false ) {
229  $score = count( $titles );
230  $suggestions = array_map( static function ( $title ) use ( &$score ) {
231  return SearchSuggestion::fromText( $score--, $title );
232  }, $titles );
233  return new SearchSuggestionSet( $suggestions, $hasMoreResults );
234  }
235 
239  public static function emptySuggestionSet() {
240  return new SearchSuggestionSet( [] );
241  }
242 }
Represents a title within MediaWiki.
Definition: Title.php:76
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.
static fromText( $score, $text)
Create suggestion from text Will also create a title if text if not empty.
getSuggestedTitleID()
Title ID in the case this suggestion is based on a title.
setScore( $score)
Set the suggestion score.
getScore()
Suggestion score.
static fromTitle( $score, Title $title)
Create suggestion from Title.