MediaWiki REL1_31
SearchSuggestionSet.php
Go to the documentation of this file.
1<?php
2
30 private $suggestions = [];
31
36 private $pageMap = [];
37
49 public function __construct( array $suggestions ) {
50 foreach ( $suggestions as $suggestion ) {
51 $pageID = $suggestion->getSuggestedTitleID();
52 if ( $pageID && empty( $this->pageMap[$pageID] ) ) {
53 $this->pageMap[$pageID] = true;
54 }
55 $this->suggestions[] = $suggestion;
56 }
57 }
58
63 public function getSuggestions() {
64 return $this->suggestions;
65 }
66
72 public function map( $callback ) {
73 return array_map( $callback, $this->suggestions );
74 }
75
83 public function append( SearchSuggestion $suggestion ) {
84 $pageID = $suggestion->getSuggestedTitleID();
85 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
86 return;
87 }
88 if ( $this->getSize() > 0 && $suggestion->getScore() >= $this->getWorstScore() ) {
89 $suggestion->setScore( $this->getWorstScore() - 1 );
90 }
91 $this->suggestions[] = $suggestion;
92 if ( $pageID ) {
93 $this->pageMap[$pageID] = true;
94 }
95 }
96
101 public function appendAll( SearchSuggestionSet $set ) {
102 foreach ( $set->getSuggestions() as $sugg ) {
103 $this->append( $sugg );
104 }
105 }
106
111 public function rescore( $key ) {
112 $removed = array_splice( $this->suggestions, $key, 1 );
113 unset( $this->pageMap[$removed[0]->getSuggestedTitleID()] );
114 $this->prepend( $removed[0] );
115 }
116
122 public function prepend( SearchSuggestion $suggestion ) {
123 $pageID = $suggestion->getSuggestedTitleID();
124 if ( $pageID && isset( $this->pageMap[$pageID] ) ) {
125 return;
126 }
127 if ( $this->getSize() > 0 && $suggestion->getScore() <= $this->getBestScore() ) {
128 $suggestion->setScore( $this->getBestScore() + 1 );
129 }
130 array_unshift( $this->suggestions, $suggestion );
131 if ( $pageID ) {
132 $this->pageMap[$pageID] = true;
133 }
134 }
135
139 public function getBestScore() {
140 if ( empty( $this->suggestions ) ) {
141 return 0;
142 }
143 return $this->suggestions[0]->getScore();
144 }
145
149 public function getWorstScore() {
150 if ( empty( $this->suggestions ) ) {
151 return 0;
152 }
153 return end( $this->suggestions )->getScore();
154 }
155
159 public function getSize() {
160 return count( $this->suggestions );
161 }
162
167 public function shrink( $limit ) {
168 if ( count( $this->suggestions ) > $limit ) {
169 $this->suggestions = array_slice( $this->suggestions, 0, $limit );
170 }
171 }
172
182 public static function fromTitles( array $titles ) {
183 $score = count( $titles );
184 $suggestions = array_map( function ( $title ) use ( &$score ) {
185 return SearchSuggestion::fromTitle( $score--, $title );
186 }, $titles );
187 return new SearchSuggestionSet( $suggestions );
188 }
189
198 public static function fromStrings( array $titles ) {
199 $score = count( $titles );
200 $suggestions = array_map( function ( $title ) use ( &$score ) {
201 return SearchSuggestion::fromText( $score--, $title );
202 }, $titles );
203 return new SearchSuggestionSet( $suggestions );
204 }
205
209 public static function emptySuggestionSet() {
210 return new SearchSuggestionSet( [] );
211 }
212}
Search suggestion sets.
append(SearchSuggestion $suggestion)
Add a new suggestion at the end.
rescore( $key)
Move the suggestion at index $key to the first position.
shrink( $limit)
Remove any extra elements in the suggestions set.
appendAll(SearchSuggestionSet $set)
Add suggestion set to the end of the current one.
__construct(array $suggestions)
Builds a new set of suggestions.
static fromStrings(array $titles)
Builds a new set of suggestion based on a string array.
getSuggestions()
Get the list of suggestions.
map( $callback)
Call array_map on the suggestions array.
static fromTitles(array $titles)
Builds a new set of suggestion based on a title array.
SearchSuggestion[] $suggestions
prepend(SearchSuggestion $suggestion)
Add a new suggestion at the top.
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.
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition linkcache.txt:17