| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Handler; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use ISearchResultSet; |
| 7 |
use MediaWiki\Cache\CacheKeyHelper; |
| 8 |
use MediaWiki\Config\Config; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Page\PageIdentity; |
| 11 |
use MediaWiki\Page\PageStore; |
| 12 |
use MediaWiki\Page\RedirectLookup; |
| 13 |
use MediaWiki\Permissions\PermissionManager; |
| 14 |
use MediaWiki\Rest\Handler; |
| 15 |
use MediaWiki\Rest\Handler\Helper\RestStatusTrait; |
| 16 |
use MediaWiki\Rest\LocalizedHttpException; |
| 17 |
use MediaWiki\Rest\Response; |
| 18 |
use MediaWiki\Search\Entity\SearchResultThumbnail; |
| 19 |
use MediaWiki\Search\SearchResultThumbnailProvider; |
| 20 |
use MediaWiki\Title\TitleFormatter; |
| 21 |
use SearchEngine; |
| 22 |
use SearchEngineConfig; |
| 23 |
use SearchEngineFactory; |
| 24 |
use SearchResult; |
| 25 |
use SearchSuggestion; |
| 26 |
use StatusValue; |
| 27 |
use Wikimedia\ParamValidator\ParamValidator; |
| 28 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class SearchHandler extends Handler { |
| 34 |
use RestStatusTrait; |
| 35 |
|
| 36 |
private SearchEngineFactory $searchEngineFactory; |
| 37 |
private SearchEngineConfig $searchEngineConfig; |
| 38 |
private SearchResultThumbnailProvider $searchResultThumbnailProvider; |
| 39 |
private PermissionManager $permissionManager; |
| 40 |
private RedirectLookup $redirectLookup; |
| 41 |
private PageStore $pageStore; |
| 42 |
private TitleFormatter $titleFormatter; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public const FULLTEXT_MODE = 'fulltext'; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public const COMPLETION_MODE = 'completion'; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private const SUPPORTED_MODES = [ self::FULLTEXT_MODE, self::COMPLETION_MODE ]; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private $mode = null; |
| 63 |
|
| 64 |
|
| 65 |
private const LIMIT = 50; |
| 66 |
|
| 67 |
|
| 68 |
private const MAX_LIMIT = 100; |
| 69 |
|
| 70 |
|
| 71 |
private const OFFSET = 0; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
private $completionCacheExpiry; |
| 80 |
|
| 81 |
public function __construct( |
| 82 |
Config $config, |
| 83 |
SearchEngineFactory $searchEngineFactory, |
| 84 |
SearchEngineConfig $searchEngineConfig, |
| 85 |
SearchResultThumbnailProvider $searchResultThumbnailProvider, |
| 86 |
PermissionManager $permissionManager, |
| 87 |
RedirectLookup $redirectLookup, |
| 88 |
PageStore $pageStore, |
| 89 |
TitleFormatter $titleFormatter |
| 90 |
) { |
| 91 |
$this->searchEngineFactory = $searchEngineFactory; |
| 92 |
$this->searchEngineConfig = $searchEngineConfig; |
| 93 |
$this->searchResultThumbnailProvider = $searchResultThumbnailProvider; |
| 94 |
$this->permissionManager = $permissionManager; |
| 95 |
$this->redirectLookup = $redirectLookup; |
| 96 |
$this->pageStore = $pageStore; |
| 97 |
$this->titleFormatter = $titleFormatter; |
| 98 |
|
| 99 |
|
| 100 |
$this->completionCacheExpiry = $config->get( MainConfigNames::SearchSuggestCacheExpiry ); |
| 101 |
} |
| 102 |
|
| 103 |
protected function postInitSetup() { |
| 104 |
$this->mode = $this->getConfig()['mode'] ?? self::FULLTEXT_MODE; |
| 105 |
|
| 106 |
if ( !in_array( $this->mode, self::SUPPORTED_MODES ) ) { |
| 107 |
throw new InvalidArgumentException( |
| 108 |
"Unsupported search mode `{$this->mode}` configured. Supported modes: " . |
| 109 |
implode( ', ', self::SUPPORTED_MODES ) |
| 110 |
); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private function createSearchEngine() { |
| 118 |
$limit = $this->getValidatedParams()['limit']; |
| 119 |
|
| 120 |
$searchEngine = $this->searchEngineFactory->create(); |
| 121 |
$searchEngine->setNamespaces( $this->searchEngineConfig->defaultNamespaces() ); |
| 122 |
$searchEngine->setLimitOffset( $limit, self::OFFSET ); |
| 123 |
return $searchEngine; |
| 124 |
} |
| 125 |
|
| 126 |
public function needsWriteAccess() { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private function getSearchResultsOrThrow( $results ) { |
| 137 |
if ( $results ) { |
| 138 |
if ( $results instanceof StatusValue ) { |
| 139 |
$status = $results; |
| 140 |
if ( !$status->isOK() ) { |
| 141 |
if ( $status->getMessages( 'error' ) ) { |
| 142 |
$this->throwExceptionForStatus( $status, 'rest-search-error', 500 ); |
| 143 |
} |
| 144 |
} |
| 145 |
$statusValue = $status->getValue(); |
| 146 |
if ( $statusValue instanceof ISearchResultSet ) { |
| 147 |
return $statusValue->extractResults(); |
| 148 |
} |
| 149 |
} else { |
| 150 |
return $results->extractResults(); |
| 151 |
} |
| 152 |
} |
| 153 |
return []; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private function doSearch( $searchEngine ) { |
| 164 |
$query = $this->getValidatedParams()['q']; |
| 165 |
|
| 166 |
if ( $this->mode == self::COMPLETION_MODE ) { |
| 167 |
$completionSearch = $searchEngine->completionSearchWithVariants( $query ); |
| 168 |
return $this->buildPageObjects( $completionSearch->getSuggestions() ); |
| 169 |
} else { |
| 170 |
$titleSearch = $searchEngine->searchTitle( $query ); |
| 171 |
$textSearch = $searchEngine->searchText( $query ); |
| 172 |
|
| 173 |
$titleSearchResults = $this->getSearchResultsOrThrow( $titleSearch ); |
| 174 |
$textSearchResults = $this->getSearchResultsOrThrow( $textSearch ); |
| 175 |
|
| 176 |
$mergedResults = array_merge( $titleSearchResults, $textSearchResults ); |
| 177 |
return $this->buildPageObjects( $mergedResults ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private function buildPageObjects( array $searchResponse ): array { |
| 194 |
$pageInfos = []; |
| 195 |
foreach ( $searchResponse as $response ) { |
| 196 |
$isSearchResult = $response instanceof SearchResult; |
| 197 |
if ( $isSearchResult ) { |
| 198 |
if ( $response->isBrokenTitle() || $response->isMissingRevision() ) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
$title = $response->getTitle(); |
| 202 |
} else { |
| 203 |
$title = $response->getSuggestedTitle(); |
| 204 |
} |
| 205 |
$pageObj = $this->buildSinglePage( $title, $response ); |
| 206 |
if ( $pageObj ) { |
| 207 |
$pageNsAndID = CacheKeyHelper::getKeyForPage( $pageObj['pageIdentity'] ); |
| 208 |
|
| 209 |
|
| 210 |
if ( isset( $pageInfos[$pageNsAndID] ) ) { |
| 211 |
if ( $pageInfos[$pageNsAndID]['redirect'] !== null ) { |
| 212 |
$pageInfos[$pageNsAndID]['result'] = $isSearchResult ? $response : null; |
| 213 |
$pageInfos[$pageNsAndID]['suggestion'] = $isSearchResult ? null : $response; |
| 214 |
} |
| 215 |
continue; |
| 216 |
} |
| 217 |
$pageInfos[$pageNsAndID] = $pageObj; |
| 218 |
} |
| 219 |
} |
| 220 |
return $pageInfos; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private function buildSinglePage( $title, $result ) { |
| 237 |
$redirectTarget = $title->canExist() ? $this->redirectLookup->getRedirectTarget( $title ) : null; |
| 238 |
|
| 239 |
|
| 240 |
if ( $redirectTarget && $redirectTarget->getNamespace() > -1 && !$redirectTarget->isExternal() ) { |
| 241 |
$redirectSource = $title; |
| 242 |
$title = $this->pageStore->getPageForLink( $redirectTarget ); |
| 243 |
} else { |
| 244 |
$redirectSource = null; |
| 245 |
} |
| 246 |
if ( !$title || !$this->getAuthority()->probablyCan( 'read', $title ) ) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
return [ |
| 250 |
'pageIdentity' => $title, |
| 251 |
'suggestion' => $result instanceof SearchSuggestion ? $result : null, |
| 252 |
'result' => $result instanceof SearchResult ? $result : null, |
| 253 |
'redirect' => $redirectSource |
| 254 |
]; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private function buildResultFromPageInfos( array $pageInfos, array $thumbsAndDesc ): array { |
| 270 |
$pages = []; |
| 271 |
foreach ( $pageInfos as $pageInfo ) { |
| 272 |
[ |
| 273 |
'pageIdentity' => $page, |
| 274 |
'suggestion' => $sugg, |
| 275 |
'result' => $result, |
| 276 |
'redirect' => $redirect |
| 277 |
] = $pageInfo; |
| 278 |
$excerpt = $sugg ? $sugg->getText() : $result->getTextSnippet(); |
| 279 |
$id = ( $page instanceof PageIdentity && $page->canExist() ) ? $page->getId() : 0; |
| 280 |
$pages[] = [ |
| 281 |
'id' => $id, |
| 282 |
'key' => $this->titleFormatter->getPrefixedDBkey( $page ), |
| 283 |
'title' => $this->titleFormatter->getPrefixedText( $page ), |
| 284 |
'excerpt' => $excerpt ?: null, |
| 285 |
'matched_title' => $redirect ? $this->titleFormatter->getPrefixedText( $redirect ) : null, |
| 286 |
'description' => $id > 0 ? $thumbsAndDesc[$id]['description'] : null, |
| 287 |
'thumbnail' => $id > 0 ? $thumbsAndDesc[$id]['thumbnail'] : null, |
| 288 |
]; |
| 289 |
} |
| 290 |
return $pages; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private function serializeThumbnail( ?SearchResultThumbnail $thumbnail ): ?array { |
| 301 |
if ( $thumbnail == null ) { |
| 302 |
return null; |
| 303 |
} |
| 304 |
|
| 305 |
return [ |
| 306 |
'mimetype' => $thumbnail->getMimeType(), |
| 307 |
'width' => $thumbnail->getWidth(), |
| 308 |
'height' => $thumbnail->getHeight(), |
| 309 |
'duration' => $thumbnail->getDuration(), |
| 310 |
'url' => $thumbnail->getUrl(), |
| 311 |
]; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
private function buildDescriptionsFromPageIdentities( array $pageIdentities ) { |
| 325 |
$descriptions = array_fill_keys( array_keys( $pageIdentities ), null ); |
| 326 |
|
| 327 |
$this->getHookRunner()->onSearchResultProvideDescription( $pageIdentities, $descriptions ); |
| 328 |
|
| 329 |
return array_map( static function ( $description ) { |
| 330 |
return [ 'description' => $description ]; |
| 331 |
}, $descriptions ); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
private function buildThumbnailsFromPageIdentities( array $pageIdentities ) { |
| 346 |
$thumbnails = $this->searchResultThumbnailProvider->getThumbnails( $pageIdentities ); |
| 347 |
$thumbnails += array_fill_keys( array_keys( $pageIdentities ), null ); |
| 348 |
|
| 349 |
return array_map( function ( $thumbnail ) { |
| 350 |
return [ 'thumbnail' => $this->serializeThumbnail( $thumbnail ) ]; |
| 351 |
}, $thumbnails ); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public function execute() { |
| 359 |
$searchEngine = $this->createSearchEngine(); |
| 360 |
$pageInfos = $this->doSearch( $searchEngine ); |
| 361 |
|
| 362 |
|
| 363 |
$pageIdentities = array_reduce( |
| 364 |
array_values( $pageInfos ), |
| 365 |
static function ( $realPages, $item ) { |
| 366 |
$page = $item['pageIdentity']; |
| 367 |
if ( $page instanceof PageIdentity && $page->exists() ) { |
| 368 |
$realPages[$item['pageIdentity']->getId()] = $item['pageIdentity']; |
| 369 |
} |
| 370 |
return $realPages; |
| 371 |
}, [] |
| 372 |
); |
| 373 |
|
| 374 |
$descriptions = $this->buildDescriptionsFromPageIdentities( $pageIdentities ); |
| 375 |
$thumbs = $this->buildThumbnailsFromPageIdentities( $pageIdentities ); |
| 376 |
|
| 377 |
$thumbsAndDescriptions = []; |
| 378 |
foreach ( $descriptions as $pageId => $description ) { |
| 379 |
$thumbsAndDescriptions[$pageId] = $description + $thumbs[$pageId]; |
| 380 |
} |
| 381 |
|
| 382 |
$result = $this->buildResultFromPageInfos( $pageInfos, $thumbsAndDescriptions ); |
| 383 |
|
| 384 |
$response = $this->getResponseFactory()->createJson( [ 'pages' => $result ] ); |
| 385 |
|
| 386 |
if ( $this->mode === self::COMPLETION_MODE && $this->completionCacheExpiry ) { |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
if ( $this->permissionManager->isEveryoneAllowed( 'read' ) ) { |
| 391 |
$response->setHeader( 'Cache-Control', 'public, max-age=' . $this->completionCacheExpiry ); |
| 392 |
} else { |
| 393 |
$response->setHeader( 'Cache-Control', 'no-store, max-age=0' ); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
return $response; |
| 398 |
} |
| 399 |
|
| 400 |
public function getParamSettings() { |
| 401 |
return [ |
| 402 |
'q' => [ |
| 403 |
self::PARAM_SOURCE => 'query', |
| 404 |
ParamValidator::PARAM_TYPE => 'string', |
| 405 |
ParamValidator::PARAM_REQUIRED => true, |
| 406 |
], |
| 407 |
'limit' => [ |
| 408 |
self::PARAM_SOURCE => 'query', |
| 409 |
ParamValidator::PARAM_TYPE => 'integer', |
| 410 |
ParamValidator::PARAM_REQUIRED => false, |
| 411 |
ParamValidator::PARAM_DEFAULT => self::LIMIT, |
| 412 |
IntegerDef::PARAM_MIN => 1, |
| 413 |
IntegerDef::PARAM_MAX => self::MAX_LIMIT, |
| 414 |
], |
| 415 |
]; |
| 416 |
} |
| 417 |
|
| 418 |
public function getResponseBodySchemaFileName( string $method ): ?string { |
| 419 |
return 'includes/Rest/Handler/Schema/SearchResults.json'; |
| 420 |
} |
| 421 |
} |