| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use MediaWiki\Title\Title; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class SearchSuggestionSet { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private $suggestions = []; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private $pageMap = []; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private $hasMoreResults; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function hasMoreResults() { |
| 71 |
return $this->hasMoreResults; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function getSuggestions() { |
| 79 |
return $this->suggestions; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function map( $callback ) { |
| 88 |
return array_map( $callback, $this->suggestions ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function appendAll( SearchSuggestionSet $set ) { |
| 129 |
foreach ( $set->getSuggestions() as $sugg ) { |
| 130 |
$this->append( $sugg ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 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 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function remove( SearchSuggestion $suggestion ): bool { |
| 170 |
foreach ( $this->suggestions as $k => $s ) { |
| 171 |
$titleId = $s->getSuggestedTitleID(); |
| 172 |
if ( ( $titleId != null && $titleId === $suggestion->getSuggestedTitleID() ) |
| 173 |
|| $s->getText() === $suggestion->getText() |
| 174 |
) { |
| 175 |
array_splice( $this->suggestions, $k, 1 ); |
| 176 |
unset( $this->pageMap[$s->getSuggestedTitleID()] ); |
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function getBestScore() { |
| 187 |
if ( !$this->suggestions ) { |
| 188 |
return 0; |
| 189 |
} |
| 190 |
return $this->suggestions[0]->getScore(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function getWorstScore() { |
| 197 |
if ( !$this->suggestions ) { |
| 198 |
return 0; |
| 199 |
} |
| 200 |
return end( $this->suggestions )->getScore(); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function getSize() { |
| 207 |
return count( $this->suggestions ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public function shrink( $limit ) { |
| 215 |
if ( count( $this->suggestions ) > $limit ) { |
| 216 |
$this->suggestions = array_slice( $this->suggestions, 0, $limit ); |
| 217 |
$this->pageMap = self::buildPageMap( $this->suggestions ); |
| 218 |
$this->hasMoreResults = true; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private static function buildPageMap( array $suggestions ): array { |
| 229 |
$pageMap = []; |
| 230 |
foreach ( $suggestions as $suggestion ) { |
| 231 |
$pageID = $suggestion->getSuggestedTitleID(); |
| 232 |
if ( $pageID ) { |
| 233 |
$pageMap[$pageID] = true; |
| 234 |
} |
| 235 |
} |
| 236 |
return $pageMap; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public static function fromTitles( array $titles, $hasMoreResults = false ) { |
| 250 |
$score = count( $titles ); |
| 251 |
$suggestions = array_map( static function ( $title ) use ( &$score ) { |
| 252 |
return SearchSuggestion::fromTitle( $score--, $title ); |
| 253 |
}, $titles ); |
| 254 |
return new SearchSuggestionSet( $suggestions, $hasMoreResults ); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public static function fromStrings( array $titles, $hasMoreResults = false ) { |
| 267 |
$score = count( $titles ); |
| 268 |
$suggestions = array_map( static function ( $title ) use ( &$score ) { |
| 269 |
return SearchSuggestion::fromText( $score--, $title ); |
| 270 |
}, $titles ); |
| 271 |
return new SearchSuggestionSet( $suggestions, $hasMoreResults ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public static function emptySuggestionSet() { |
| 278 |
return new SearchSuggestionSet( [] ); |
| 279 |
} |
| 280 |
} |