| 1 |
<?php |
| 2 |
|
| 3 |
" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use HtmlArmor; |
| 26 |
use ISearchResultSet; |
| 27 |
use MediaWiki\Search\TitleMatcher; |
| 28 |
use MediaWiki\Status\Status; |
| 29 |
use SearchEngine; |
| 30 |
use SearchEngineConfig; |
| 31 |
use SearchEngineFactory; |
| 32 |
use SearchResult; |
| 33 |
use Wikimedia\ParamValidator\ParamValidator; |
| 34 |
use Wikimedia\ParamValidator\TypeDef\EnumDef; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class ApiQuerySearch extends ApiQueryGeneratorBase { |
| 42 |
use \MediaWiki\Api\SearchApi; |
| 43 |
|
| 44 |
private TitleMatcher $titleMatcher; |
| 45 |
|
| 46 |
public function __construct( |
| 47 |
ApiQuery $query, |
| 48 |
string $moduleName, |
| 49 |
SearchEngineConfig $searchEngineConfig, |
| 50 |
SearchEngineFactory $searchEngineFactory, |
| 51 |
TitleMatcher $titleMatcher |
| 52 |
) { |
| 53 |
parent::__construct( $query, $moduleName, 'sr' ); |
| 54 |
|
| 55 |
$this->searchEngineConfig = $searchEngineConfig; |
| 56 |
$this->searchEngineFactory = $searchEngineFactory; |
| 57 |
$this->titleMatcher = $titleMatcher; |
| 58 |
} |
| 59 |
|
| 60 |
public function execute() { |
| 61 |
$this->run(); |
| 62 |
} |
| 63 |
|
| 64 |
public function executeGenerator( $resultPageSet ) { |
| 65 |
$this->run( $resultPageSet ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private function run( $resultPageSet = null ) { |
| 73 |
$params = $this->extractRequestParams(); |
| 74 |
|
| 75 |
|
| 76 |
$query = $params['search']; |
| 77 |
$what = $params['what']; |
| 78 |
$interwiki = $params['interwiki']; |
| 79 |
$searchInfo = array_fill_keys( $params['info'], true ); |
| 80 |
$prop = array_fill_keys( $params['prop'], true ); |
| 81 |
|
| 82 |
|
| 83 |
$search = $this->buildSearchEngine( $params ); |
| 84 |
if ( isset( $params['sort'] ) ) { |
| 85 |
$search->setSort( $params['sort'] ); |
| 86 |
} |
| 87 |
$search->setFeatureData( 'rewrite', (bool)$params['enablerewrites'] ); |
| 88 |
$search->setFeatureData( 'interwiki', (bool)$interwiki ); |
| 89 |
|
| 90 |
$search->setFeatureData( 'snippets', $this->decideSnippets( $prop ) ); |
| 91 |
|
| 92 |
$nquery = $search->replacePrefixes( $query ); |
| 93 |
if ( $nquery !== $query ) { |
| 94 |
$query = $nquery; |
| 95 |
wfDeprecatedMsg( 'SearchEngine::replacePrefixes() is overridden by ' . |
| 96 |
get_class( $search ) . ', this was deprecated in MediaWiki 1.32', |
| 97 |
'1.32' ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( $what == 'text' ) { |
| 101 |
$matches = $search->searchText( $query ); |
| 102 |
} elseif ( $what == 'title' ) { |
| 103 |
$matches = $search->searchTitle( $query ); |
| 104 |
} elseif ( $what == 'nearmatch' ) { |
| 105 |
|
| 106 |
|
| 107 |
$matches = $this->titleMatcher->getNearMatchResultSet( $params['search'] ); |
| 108 |
} else { |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
$what = 'title'; |
| 114 |
$matches = $search->searchTitle( $query ); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if ( $matches === null ) { |
| 121 |
$what = 'text'; |
| 122 |
$matches = $search->searchText( $query ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if ( $matches instanceof Status ) { |
| 127 |
$status = $matches; |
| 128 |
$matches = $status->getValue(); |
| 129 |
} else { |
| 130 |
$status = null; |
| 131 |
} |
| 132 |
|
| 133 |
if ( $status ) { |
| 134 |
if ( $status->isOK() ) { |
| 135 |
$this->getMain()->getErrorFormatter()->addMessagesFromStatus( |
| 136 |
$this->getModuleName(), |
| 137 |
$status |
| 138 |
); |
| 139 |
} else { |
| 140 |
$this->dieStatus( $status ); |
| 141 |
} |
| 142 |
} elseif ( $matches === null ) { |
| 143 |
$this->dieWithError( [ 'apierror-searchdisabled', $what ], "search-{$what}-disabled" ); |
| 144 |
} |
| 145 |
|
| 146 |
$apiResult = $this->getResult(); |
| 147 |
|
| 148 |
if ( isset( $searchInfo['totalhits'] ) ) { |
| 149 |
$totalhits = $matches->getTotalHits(); |
| 150 |
if ( $totalhits !== null ) { |
| 151 |
$apiResult->addValue( [ 'query', 'searchinfo' ], |
| 152 |
'totalhits', $totalhits ); |
| 153 |
} |
| 154 |
} |
| 155 |
if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) { |
| 156 |
$apiResult->addValue( [ 'query', 'searchinfo' ], |
| 157 |
'suggestion', $matches->getSuggestionQuery() ); |
| 158 |
$apiResult->addValue( [ 'query', 'searchinfo' ], |
| 159 |
'suggestionsnippet', HtmlArmor::getHtml( $matches->getSuggestionSnippet() ) ); |
| 160 |
} |
| 161 |
if ( isset( $searchInfo['rewrittenquery'] ) && $matches->hasRewrittenQuery() ) { |
| 162 |
$apiResult->addValue( [ 'query', 'searchinfo' ], |
| 163 |
'rewrittenquery', $matches->getQueryAfterRewrite() ); |
| 164 |
$apiResult->addValue( [ 'query', 'searchinfo' ], |
| 165 |
'rewrittenquerysnippet', HtmlArmor::getHtml( $matches->getQueryAfterRewriteSnippet() ) ); |
| 166 |
} |
| 167 |
|
| 168 |
$titles = []; |
| 169 |
$data = []; |
| 170 |
$count = 0; |
| 171 |
|
| 172 |
if ( $matches->hasMoreResults() ) { |
| 173 |
$this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] ); |
| 174 |
} |
| 175 |
|
| 176 |
foreach ( $matches as $result ) { |
| 177 |
$count++; |
| 178 |
|
| 179 |
if ( $result->isBrokenTitle() || $result->isMissingRevision() ) { |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
$vals = $this->getSearchResultData( $result, $prop ); |
| 184 |
|
| 185 |
if ( $resultPageSet === null ) { |
| 186 |
if ( $vals ) { |
| 187 |
|
| 188 |
$fit = $apiResult->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
| 189 |
if ( !$fit ) { |
| 190 |
$this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 ); |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
} else { |
| 195 |
$titles[] = $result->getTitle(); |
| 196 |
$data[] = $vals ?: []; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
$canAddInterwiki = (bool)$params['enablerewrites'] && ( $resultPageSet === null ); |
| 206 |
if ( $canAddInterwiki ) { |
| 207 |
$this->addInterwikiResults( $matches, $apiResult, $prop, 'additional', |
| 208 |
ISearchResultSet::INLINE_RESULTS ); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
if ( $interwiki && $resultPageSet === null ) { |
| 213 |
$this->addInterwikiResults( $matches, $apiResult, $prop, 'interwiki', |
| 214 |
ISearchResultSet::SECONDARY_RESULTS ); |
| 215 |
} |
| 216 |
|
| 217 |
if ( $resultPageSet === null ) { |
| 218 |
$apiResult->addIndexedTagName( [ |
| 219 |
'query', $this->getModuleName() |
| 220 |
], 'p' ); |
| 221 |
} else { |
| 222 |
$resultPageSet->setRedirectMergePolicy( static function ( $current, $new ) { |
| 223 |
if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) { |
| 224 |
$current['index'] = $new['index']; |
| 225 |
} |
| 226 |
return $current; |
| 227 |
} ); |
| 228 |
$resultPageSet->populateFromTitles( $titles ); |
| 229 |
$offset = $params['offset'] + 1; |
| 230 |
foreach ( $titles as $index => $title ) { |
| 231 |
$resultPageSet->setGeneratorData( |
| 232 |
$title, |
| 233 |
$data[ $index ] + [ 'index' => $index + $offset ] |
| 234 |
); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private function getSearchResultData( SearchResult $result, $prop ) { |
| 246 |
|
| 247 |
if ( $result->isBrokenTitle() || $result->isMissingRevision() ) { |
| 248 |
return null; |
| 249 |
} |
| 250 |
|
| 251 |
$vals = []; |
| 252 |
|
| 253 |
$title = $result->getTitle(); |
| 254 |
ApiQueryBase::addTitleInfo( $vals, $title ); |
| 255 |
$vals['pageid'] = $title->getArticleID(); |
| 256 |
|
| 257 |
if ( isset( $prop['size'] ) ) { |
| 258 |
$vals['size'] = $result->getByteSize(); |
| 259 |
} |
| 260 |
if ( isset( $prop['wordcount'] ) ) { |
| 261 |
$vals['wordcount'] = $result->getWordCount(); |
| 262 |
} |
| 263 |
if ( isset( $prop['snippet'] ) ) { |
| 264 |
$vals['snippet'] = $result->getTextSnippet(); |
| 265 |
} |
| 266 |
if ( isset( $prop['timestamp'] ) ) { |
| 267 |
$vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() ); |
| 268 |
} |
| 269 |
if ( isset( $prop['titlesnippet'] ) ) { |
| 270 |
$vals['titlesnippet'] = $result->getTitleSnippet(); |
| 271 |
} |
| 272 |
if ( isset( $prop['categorysnippet'] ) ) { |
| 273 |
$vals['categorysnippet'] = $result->getCategorySnippet(); |
| 274 |
} |
| 275 |
if ( $result->getRedirectTitle() !== null ) { |
| 276 |
if ( isset( $prop['redirecttitle'] ) ) { |
| 277 |
$vals['redirecttitle'] = $result->getRedirectTitle()->getPrefixedText(); |
| 278 |
} |
| 279 |
if ( isset( $prop['redirectsnippet'] ) ) { |
| 280 |
$vals['redirectsnippet'] = $result->getRedirectSnippet(); |
| 281 |
} |
| 282 |
} |
| 283 |
if ( $result->getSectionTitle() !== null ) { |
| 284 |
if ( isset( $prop['sectiontitle'] ) ) { |
| 285 |
$vals['sectiontitle'] = $result->getSectionTitle()->getFragment(); |
| 286 |
} |
| 287 |
if ( isset( $prop['sectionsnippet'] ) ) { |
| 288 |
$vals['sectionsnippet'] = $result->getSectionSnippet(); |
| 289 |
} |
| 290 |
} |
| 291 |
if ( isset( $prop['isfilematch'] ) ) { |
| 292 |
$vals['isfilematch'] = $result->isFileMatch(); |
| 293 |
} |
| 294 |
|
| 295 |
if ( isset( $prop['extensiondata'] ) ) { |
| 296 |
$extra = $result->getExtensionData(); |
| 297 |
|
| 298 |
|
| 299 |
if ( $extra ) { |
| 300 |
$vals['extensiondata'] = ApiResult::addMetadataToResultVars( $extra ); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return $vals; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
private function addInterwikiResults( |
| 317 |
ISearchResultSet $matches, ApiResult $apiResult, $prop, |
| 318 |
$section, $type |
| 319 |
) { |
| 320 |
$totalhits = null; |
| 321 |
if ( $matches->hasInterwikiResults( $type ) ) { |
| 322 |
foreach ( $matches->getInterwikiResults( $type ) as $interwikiMatches ) { |
| 323 |
|
| 324 |
$totalhits += $interwikiMatches->getTotalHits(); |
| 325 |
|
| 326 |
foreach ( $interwikiMatches as $result ) { |
| 327 |
$title = $result->getTitle(); |
| 328 |
$vals = $this->getSearchResultData( $result, $prop ); |
| 329 |
|
| 330 |
$vals['namespace'] = $result->getInterwikiNamespaceText(); |
| 331 |
$vals['title'] = $title->getText(); |
| 332 |
$vals['url'] = $title->getFullURL(); |
| 333 |
|
| 334 |
|
| 335 |
$fit = $apiResult->addValue( [ |
| 336 |
'query', |
| 337 |
$section . $this->getModuleName(), |
| 338 |
$result->getInterwikiPrefix() |
| 339 |
], null, $vals ); |
| 340 |
|
| 341 |
if ( !$fit ) { |
| 342 |
|
| 343 |
|
| 344 |
break; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
if ( $totalhits !== null ) { |
| 349 |
$apiResult->addValue( [ 'query', $section . 'searchinfo' ], 'totalhits', $totalhits ); |
| 350 |
$apiResult->addIndexedTagName( [ |
| 351 |
'query', $section . $this->getModuleName() |
| 352 |
], 'p' ); |
| 353 |
} |
| 354 |
} |
| 355 |
return $totalhits; |
| 356 |
} |
| 357 |
|
| 358 |
private function decideSnippets( array $prop ): array { |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
$fields = []; |
| 363 |
if ( isset( $prop['titlesnippet'] ) ) { |
| 364 |
$fields[] = 'title'; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if ( isset( $prop['redirectsnippet'] ) || isset( $prop['redirecttitle'] ) ) { |
| 370 |
$fields[] = 'redirect'; |
| 371 |
} |
| 372 |
if ( isset( $prop['categorysnippet'] ) ) { |
| 373 |
$fields[] = 'category'; |
| 374 |
} |
| 375 |
if ( isset( $prop['sectionsnippet'] ) || isset( $prop['sectiontitle'] ) ) { |
| 376 |
$fields[] = 'heading'; |
| 377 |
} |
| 378 |
return $fields; |
| 379 |
} |
| 380 |
|
| 381 |
public function getCacheMode( $params ) { |
| 382 |
return 'public'; |
| 383 |
} |
| 384 |
|
| 385 |
public function getAllowedParams() { |
| 386 |
$allowedParams = $this->buildCommonApiParams() + [ |
| 387 |
'what' => [ |
| 388 |
ParamValidator::PARAM_TYPE => [ |
| 389 |
'title', |
| 390 |
'text', |
| 391 |
'nearmatch', |
| 392 |
] |
| 393 |
], |
| 394 |
'info' => [ |
| 395 |
ParamValidator::PARAM_DEFAULT => 'totalhits|suggestion|rewrittenquery', |
| 396 |
ParamValidator::PARAM_TYPE => [ |
| 397 |
'totalhits', |
| 398 |
'suggestion', |
| 399 |
'rewrittenquery', |
| 400 |
], |
| 401 |
ParamValidator::PARAM_ISMULTI => true, |
| 402 |
], |
| 403 |
'prop' => [ |
| 404 |
ParamValidator::PARAM_DEFAULT => 'size|wordcount|timestamp|snippet', |
| 405 |
ParamValidator::PARAM_TYPE => [ |
| 406 |
'size', |
| 407 |
'wordcount', |
| 408 |
'timestamp', |
| 409 |
'snippet', |
| 410 |
'titlesnippet', |
| 411 |
'redirecttitle', |
| 412 |
'redirectsnippet', |
| 413 |
'sectiontitle', |
| 414 |
'sectionsnippet', |
| 415 |
'isfilematch', |
| 416 |
'categorysnippet', |
| 417 |
'score', |
| 418 |
'hasrelated', |
| 419 |
'extensiondata', |
| 420 |
], |
| 421 |
ParamValidator::PARAM_ISMULTI => true, |
| 422 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 423 |
EnumDef::PARAM_DEPRECATED_VALUES => [ |
| 424 |
'score' => true, |
| 425 |
'hasrelated' => true |
| 426 |
], |
| 427 |
], |
| 428 |
'interwiki' => false, |
| 429 |
'enablerewrites' => false, |
| 430 |
]; |
| 431 |
|
| 432 |
|
| 433 |
if ( $this->isInGeneratorMode() ) { |
| 434 |
$allowedParams['prop'][ParamValidator::PARAM_DEFAULT] = ''; |
| 435 |
$allowedParams['info'][ParamValidator::PARAM_DEFAULT] = ''; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
$alternatives = $this->searchEngineConfig->getSearchTypes(); |
| 441 |
if ( count( $alternatives ) == 1 ) { |
| 442 |
$allowedParams['sort'] = [ |
| 443 |
ParamValidator::PARAM_DEFAULT => SearchEngine::DEFAULT_SORT, |
| 444 |
ParamValidator::PARAM_TYPE => $this->searchEngineFactory->create()->getValidSorts(), |
| 445 |
]; |
| 446 |
} |
| 447 |
|
| 448 |
return $allowedParams; |
| 449 |
} |
| 450 |
|
| 451 |
public function getSearchProfileParams() { |
| 452 |
return [ |
| 453 |
'qiprofile' => [ |
| 454 |
'profile-type' => SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE, |
| 455 |
'help-message' => 'apihelp-query+search-param-qiprofile', |
| 456 |
], |
| 457 |
]; |
| 458 |
} |
| 459 |
|
| 460 |
protected function getExamplesMessages() { |
| 461 |
return [ |
| 462 |
'action=query&list=search&srsearch=meaning' |
| 463 |
=> 'apihelp-query+search-example-simple', |
| 464 |
'action=query&list=search&srwhat=text&srsearch=meaning' |
| 465 |
=> 'apihelp-query+search-example-text', |
| 466 |
'action=query&generator=search&gsrsearch=meaning&prop=info' |
| 467 |
=> 'apihelp-query+search-example-generator', |
| 468 |
]; |
| 469 |
} |
| 470 |
|
| 471 |
public function getHelpUrls() { |
| 472 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Search'; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
class_alias( ApiQuerySearch::class, 'ApiQuerySearch' ); |