52 parent::__construct( $query, $moduleName,
'sr' );
63 $this->
run( $resultPageSet );
70 private function run( $resultPageSet =
null ) {
74 $query = $params[
'search'];
75 $what = $params[
'what'];
76 $interwiki = $params[
'interwiki'];
77 $searchInfo = array_fill_keys( $params[
'info'],
true );
78 $prop = array_fill_keys( $params[
'prop'],
true );
82 if ( isset( $params[
'sort'] ) ) {
83 $search->setSort( $params[
'sort'] );
85 $search->setFeatureData(
'rewrite', (
bool)$params[
'enablerewrites'] );
86 $search->setFeatureData(
'interwiki', (
bool)$interwiki );
88 $nquery = $search->replacePrefixes( $query );
89 if ( $nquery !== $query ) {
92 get_class( $search ) .
', this was deprecated in MediaWiki 1.32',
96 if ( $what ==
'text' ) {
97 $matches = $search->searchText( $query );
98 } elseif ( $what ==
'title' ) {
99 $matches = $search->searchTitle( $query );
100 } elseif ( $what ==
'nearmatch' ) {
104 ->getNearMatchResultSet( $params[
'search'] );
111 $matches = $search->searchTitle( $query );
119 $matches = $search->searchText( $query );
131 if ( $status->isOK() ) {
132 $this->
getMain()->getErrorFormatter()->addMessagesFromStatus(
140 $this->
dieWithError( [
'apierror-searchdisabled', $what ],
"search-{$what}-disabled" );
145 if ( isset( $searchInfo[
'totalhits'] ) ) {
146 $totalhits =
$matches->getTotalHits();
147 if ( $totalhits !==
null ) {
148 $apiResult->addValue( [
'query',
'searchinfo' ],
149 'totalhits', $totalhits );
152 if ( isset( $searchInfo[
'suggestion'] ) &&
$matches->hasSuggestion() ) {
153 $apiResult->addValue( [
'query',
'searchinfo' ],
154 'suggestion',
$matches->getSuggestionQuery() );
155 $apiResult->addValue( [
'query',
'searchinfo' ],
156 'suggestionsnippet', HtmlArmor::getHtml(
$matches->getSuggestionSnippet() ) );
158 if ( isset( $searchInfo[
'rewrittenquery'] ) &&
$matches->hasRewrittenQuery() ) {
159 $apiResult->addValue( [
'query',
'searchinfo' ],
160 'rewrittenquery',
$matches->getQueryAfterRewrite() );
161 $apiResult->addValue( [
'query',
'searchinfo' ],
162 'rewrittenquerysnippet', HtmlArmor::getHtml(
$matches->getQueryAfterRewriteSnippet() ) );
176 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
182 if ( $resultPageSet ===
null ) {
185 $fit = $apiResult->addValue( [
'query', $this->
getModuleName() ],
null, $vals );
192 $titles[] = $result->getTitle();
193 $data[] = $vals ?: [];
202 $canAddInterwiki = (bool)$params[
'enablerewrites'] && ( $resultPageSet ===
null );
203 if ( $canAddInterwiki ) {
205 ISearchResultSet::INLINE_RESULTS );
209 if ( $interwiki && $resultPageSet ===
null ) {
211 ISearchResultSet::SECONDARY_RESULTS );
214 if ( $resultPageSet ===
null ) {
215 $apiResult->addIndexedTagName( [
219 $resultPageSet->setRedirectMergePolicy(
static function ( $current, $new ) {
220 if ( !isset( $current[
'index'] ) || $new[
'index'] < $current[
'index'] ) {
221 $current[
'index'] = $new[
'index'];
225 $resultPageSet->populateFromTitles( $titles );
226 $offset = $params[
'offset'] + 1;
227 foreach ( $titles as $index =>
$title ) {
228 $resultPageSet->setGeneratorData(
230 $data[ $index ] + [
'index' => $index + $offset ]
244 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
250 $title = $result->getTitle();
252 $vals[
'pageid'] =
$title->getArticleID();
254 if ( isset( $prop[
'size'] ) ) {
255 $vals[
'size'] = $result->getByteSize();
257 if ( isset( $prop[
'wordcount'] ) ) {
258 $vals[
'wordcount'] = $result->getWordCount();
260 if ( isset( $prop[
'snippet'] ) ) {
261 $vals[
'snippet'] = $result->getTextSnippet();
263 if ( isset( $prop[
'timestamp'] ) ) {
264 $vals[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
266 if ( isset( $prop[
'titlesnippet'] ) ) {
267 $vals[
'titlesnippet'] = $result->getTitleSnippet();
269 if ( isset( $prop[
'categorysnippet'] ) ) {
270 $vals[
'categorysnippet'] = $result->getCategorySnippet();
272 if ( $result->getRedirectTitle() !==
null ) {
273 if ( isset( $prop[
'redirecttitle'] ) ) {
274 $vals[
'redirecttitle'] = $result->getRedirectTitle()->getPrefixedText();
276 if ( isset( $prop[
'redirectsnippet'] ) ) {
277 $vals[
'redirectsnippet'] = $result->getRedirectSnippet();
280 if ( $result->getSectionTitle() !==
null ) {
281 if ( isset( $prop[
'sectiontitle'] ) ) {
282 $vals[
'sectiontitle'] = $result->getSectionTitle()->getFragment();
284 if ( isset( $prop[
'sectionsnippet'] ) ) {
285 $vals[
'sectionsnippet'] = $result->getSectionSnippet();
288 if ( isset( $prop[
'isfilematch'] ) ) {
289 $vals[
'isfilematch'] = $result->isFileMatch();
292 if ( isset( $prop[
'extensiondata'] ) ) {
293 $extra = $result->getExtensionData();
297 $vals[
'extensiondata'] = ApiResult::addMetadataToResultVars( $extra );
319 foreach (
$matches->getInterwikiResults(
$type ) as $interwikiMatches ) {
321 $totalhits += $interwikiMatches->getTotalHits();
323 foreach ( $interwikiMatches as $result ) {
324 $title = $result->getTitle();
327 $vals[
'namespace'] = $result->getInterwikiNamespaceText();
328 $vals[
'title'] =
$title->getText();
329 $vals[
'url'] =
$title->getFullURL();
335 $result->getInterwikiPrefix()
345 if ( $totalhits !==
null ) {
346 $apiResult->
addValue( [
'query', $section .
'searchinfo' ],
'totalhits', $totalhits );
360 if ( $this->allowedParams !==
null ) {
406 'interwiki' =>
false,
407 'enablerewrites' =>
false,
418 $alternatives = $this->searchEngineConfig->getSearchTypes();
419 if ( count( $alternatives ) == 1 ) {
420 $this->allowedParams[
'sort'] = [
432 'profile-type' => SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE,
433 'help-message' =>
'apihelp-query+search-param-qiprofile',
440 'action=query&list=search&srsearch=meaning'
441 =>
'apihelp-query+search-example-simple',
442 'action=query&list=search&srwhat=text&srsearch=meaning'
443 =>
'apihelp-query+search-example-text',
444 'action=query&generator=search&gsrsearch=meaning&prop=info'
445 =>
'apihelp-query+search-example-generator',
450 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Search';
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
buildSearchEngine(array $params=null)
Build the search engine to use.
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
const PARAM_DEPRECATED_VALUES
getMain()
Get the main module.
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
getModuleName()
Get the name of the module being executed by this instance.
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
isInGeneratorMode()
Indicate whether the module is in generator mode.
Query module to perform full text search within wiki titles and content.
run( $resultPageSet=null)
SearchEngineFactory $searchEngineFactory
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
getSearchResultData(SearchResult $result, $prop)
Assemble search result data.
__construct(ApiQuery $query, $moduleName, SearchEngineConfig $searchEngineConfig, SearchEngineFactory $searchEngineFactory)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
addInterwikiResults(ISearchResultSet $matches, ApiResult $apiResult, $prop, $section, $type)
Add interwiki results as a section in query results.
executeGenerator( $resultPageSet)
Execute this module as a generator.
SearchEngineConfig $searchEngineConfig
array $allowedParams
list of api allowed params
This is the main query class.
This class represents the result of the API operations.
addIndexedTagName( $path, $tag)
Set the tag name for numeric-keyed values in XML format.
addValue( $path, $name, $value, $flags=0)
Add value to the output data at the given path.
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
NOTE: this class is being refactored into an abstract base class.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
trait SearchApi
Traits for API components that use a SearchEngine.
A set of SearchEngine results.