35 parent::__construct( $query, $moduleName,
'sr' );
43 $this->
run( $resultPageSet );
50 private function run( $resultPageSet =
null ) {
54 $query = $params[
'search'];
55 $what = $params[
'what'];
56 $interwiki = $params[
'interwiki'];
57 $searchInfo = array_flip( $params[
'info'] );
58 $prop = array_flip( $params[
'prop'] );
62 if ( isset( $params[
'sort'] ) ) {
63 $search->setSort( $params[
'sort'] );
65 $search->setFeatureData(
'rewrite', (
bool)$params[
'enablerewrites'] );
66 $search->setFeatureData(
'interwiki', (
bool)$interwiki );
68 $nquery = $search->replacePrefixes( $query );
69 if ( $nquery !== $query ) {
72 get_class( $search ) .
', this was deprecated in MediaWiki 1.32',
76 if ( $what ==
'text' ) {
77 $matches = $search->searchText( $query );
78 } elseif ( $what ==
'title' ) {
79 $matches = $search->searchTitle( $query );
80 } elseif ( $what ==
'nearmatch' ) {
84 ->getNearMatchResultSet( $params[
'search'] );
91 $matches = $search->searchTitle( $query );
99 $matches = $search->searchText( $query );
111 if ( $status->isOK() ) {
112 $this->
getMain()->getErrorFormatter()->addMessagesFromStatus(
120 $this->
dieWithError( [
'apierror-searchdisabled', $what ],
"search-{$what}-disabled" );
123 if ( $resultPageSet ===
null ) {
126 if ( isset( $searchInfo[
'totalhits'] ) ) {
127 $totalhits =
$matches->getTotalHits();
128 if ( $totalhits !==
null ) {
129 $apiResult->addValue( [
'query',
'searchinfo' ],
130 'totalhits', $totalhits );
133 if ( isset( $searchInfo[
'suggestion'] ) &&
$matches->hasSuggestion() ) {
134 $apiResult->addValue( [
'query',
'searchinfo' ],
135 'suggestion',
$matches->getSuggestionQuery() );
136 $apiResult->addValue( [
'query',
'searchinfo' ],
137 'suggestionsnippet', HtmlArmor::getHtml(
$matches->getSuggestionSnippet() ) );
139 if ( isset( $searchInfo[
'rewrittenquery'] ) &&
$matches->hasRewrittenQuery() ) {
140 $apiResult->addValue( [
'query',
'searchinfo' ],
141 'rewrittenquery',
$matches->getQueryAfterRewrite() );
142 $apiResult->addValue( [
'query',
'searchinfo' ],
143 'rewrittenquerysnippet', HtmlArmor::getHtml(
$matches->getQueryAfterRewriteSnippet() ) );
157 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
161 if ( $resultPageSet ===
null ) {
165 $fit = $apiResult->addValue( [
'query', $this->
getModuleName() ],
null, $vals );
172 $titles[] = $result->getTitle();
181 $canAddInterwiki = (bool)$params[
'enablerewrites'] && ( $resultPageSet ===
null );
182 if ( $canAddInterwiki ) {
184 ISearchResultSet::INLINE_RESULTS );
188 if ( $interwiki && $resultPageSet ===
null ) {
190 ISearchResultSet::SECONDARY_RESULTS );
193 if ( $resultPageSet ===
null ) {
194 $apiResult->addIndexedTagName( [
198 $resultPageSet->setRedirectMergePolicy(
function ( $current, $new ) {
199 if ( !isset( $current[
'index'] ) || $new[
'index'] < $current[
'index'] ) {
200 $current[
'index'] = $new[
'index'];
204 $resultPageSet->populateFromTitles( $titles );
205 $offset = $params[
'offset'] + 1;
206 foreach ( $titles as $index =>
$title ) {
207 $resultPageSet->setGeneratorData(
$title, [
'index' => $index + $offset ] );
220 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
226 $title = $result->getTitle();
228 $vals[
'pageid'] =
$title->getArticleID();
230 if ( isset( $prop[
'size'] ) ) {
231 $vals[
'size'] = $result->getByteSize();
233 if ( isset( $prop[
'wordcount'] ) ) {
234 $vals[
'wordcount'] = $result->getWordCount();
236 if ( isset( $prop[
'snippet'] ) ) {
237 $vals[
'snippet'] = $result->getTextSnippet();
239 if ( isset( $prop[
'timestamp'] ) ) {
240 $vals[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
242 if ( isset( $prop[
'titlesnippet'] ) ) {
243 $vals[
'titlesnippet'] = $result->getTitleSnippet();
245 if ( isset( $prop[
'categorysnippet'] ) ) {
246 $vals[
'categorysnippet'] = $result->getCategorySnippet();
248 if ( $result->getRedirectTitle() !==
null ) {
249 if ( isset( $prop[
'redirecttitle'] ) ) {
250 $vals[
'redirecttitle'] = $result->getRedirectTitle()->getPrefixedText();
252 if ( isset( $prop[
'redirectsnippet'] ) ) {
253 $vals[
'redirectsnippet'] = $result->getRedirectSnippet();
256 if ( $result->getSectionTitle() !==
null ) {
257 if ( isset( $prop[
'sectiontitle'] ) ) {
258 $vals[
'sectiontitle'] = $result->getSectionTitle()->getFragment();
260 if ( isset( $prop[
'sectionsnippet'] ) ) {
261 $vals[
'sectionsnippet'] = $result->getSectionSnippet();
264 if ( isset( $prop[
'isfilematch'] ) ) {
265 $vals[
'isfilematch'] = $result->isFileMatch();
268 if ( isset( $prop[
'extensiondata'] ) ) {
269 $extra = $result->getExtensionData();
273 $vals[
'extensiondata'] = ApiResult::addMetadataToResultVars( $extra );
295 foreach (
$matches->getInterwikiResults(
$type ) as $interwikiMatches ) {
297 $totalhits += $interwikiMatches->getTotalHits();
299 foreach ( $interwikiMatches as $result ) {
300 $title = $result->getTitle();
303 $vals[
'namespace'] = $result->getInterwikiNamespaceText();
304 $vals[
'title'] =
$title->getText();
305 $vals[
'url'] =
$title->getFullURL();
311 $result->getInterwikiPrefix()
321 if ( $totalhits !==
null ) {
322 $apiResult->
addValue( [
'query', $section .
'searchinfo' ],
'totalhits', $totalhits );
336 if ( $this->allowedParams !==
null ) {
382 'interwiki' =>
false,
383 'enablerewrites' =>
false,
388 $services = MediaWiki\MediaWikiServices::getInstance();
389 $alternatives = $services
390 ->getSearchEngineConfig()
392 if ( count( $alternatives ) == 1 ) {
393 $this->allowedParams[
'sort'] = [
407 'profile-type' => SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE,
408 'help-message' =>
'apihelp-query+search-param-qiprofile',
415 'action=query&list=search&srsearch=meaning'
416 =>
'apihelp-query+search-example-simple',
417 'action=query&list=search&srwhat=text&srsearch=meaning'
418 =>
'apihelp-query+search-example-text',
419 'action=query&generator=search&gsrsearch=meaning&prop=info'
420 =>
'apihelp-query+search-example-generator',
425 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.
Query module to perform full text search within wiki titles and content.
run( $resultPageSet=null)
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.
__construct(ApiQuery $query, $moduleName)
getSearchResultData(SearchResult $result, $prop)
Assemble search result data.
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.
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.
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.