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 ) {
71 wfDeprecated(
'SearchEngine::replacePrefixes() (overridden by ' .
72 get_class( $search ) .
')',
'1.32' );
75 if ( $what ==
'text' ) {
76 $matches = $search->searchText( $query );
77 } elseif ( $what ==
'title' ) {
78 $matches = $search->searchTitle( $query );
79 } elseif ( $what ==
'nearmatch' ) {
83 ->getNearMatchResultSet( $params[
'search'] );
90 $matches = $search->searchTitle( $query );
98 $matches = $search->searchText( $query );
110 if ( $status->isOK() ) {
111 $this->
getMain()->getErrorFormatter()->addMessagesFromStatus(
119 $this->
dieWithError( [
'apierror-searchdisabled', $what ],
"search-{$what}-disabled" );
122 if ( $resultPageSet ===
null ) {
125 if ( isset( $searchInfo[
'totalhits'] ) ) {
126 $totalhits =
$matches->getTotalHits();
127 if ( $totalhits !==
null ) {
128 $apiResult->addValue( [
'query',
'searchinfo' ],
129 'totalhits', $totalhits );
132 if ( isset( $searchInfo[
'suggestion'] ) &&
$matches->hasSuggestion() ) {
133 $apiResult->addValue( [
'query',
'searchinfo' ],
134 'suggestion',
$matches->getSuggestionQuery() );
135 $apiResult->addValue( [
'query',
'searchinfo' ],
136 'suggestionsnippet',
$matches->getSuggestionSnippet() );
138 if ( isset( $searchInfo[
'rewrittenquery'] ) &&
$matches->hasRewrittenQuery() ) {
139 $apiResult->addValue( [
'query',
'searchinfo' ],
140 'rewrittenquery',
$matches->getQueryAfterRewrite() );
141 $apiResult->addValue( [
'query',
'searchinfo' ],
142 'rewrittenquerysnippet',
$matches->getQueryAfterRewriteSnippet() );
156 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
160 if ( $resultPageSet ===
null ) {
164 $fit = $apiResult->addValue( [
'query', $this->
getModuleName() ],
null, $vals );
171 $titles[] = $result->getTitle();
180 $canAddInterwiki = (bool)$params[
'enablerewrites'] && ( $resultPageSet ===
null );
181 if ( $canAddInterwiki ) {
183 ISearchResultSet::INLINE_RESULTS );
187 if ( $interwiki && $resultPageSet ===
null ) {
189 ISearchResultSet::SECONDARY_RESULTS );
192 if ( $resultPageSet ===
null ) {
193 $apiResult->addIndexedTagName( [
197 $resultPageSet->setRedirectMergePolicy(
function ( $current, $new ) {
198 if ( !isset( $current[
'index'] ) || $new[
'index'] < $current[
'index'] ) {
199 $current[
'index'] = $new[
'index'];
203 $resultPageSet->populateFromTitles( $titles );
204 $offset = $params[
'offset'] + 1;
205 foreach ( $titles as $index =>
$title ) {
206 $resultPageSet->setGeneratorData(
$title, [
'index' => $index + $offset ] );
219 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
225 $title = $result->getTitle();
227 $vals[
'pageid'] =
$title->getArticleID();
229 if ( isset( $prop[
'size'] ) ) {
230 $vals[
'size'] = $result->getByteSize();
232 if ( isset( $prop[
'wordcount'] ) ) {
233 $vals[
'wordcount'] = $result->getWordCount();
235 if ( isset( $prop[
'snippet'] ) ) {
236 $vals[
'snippet'] = $result->getTextSnippet();
238 if ( isset( $prop[
'timestamp'] ) ) {
239 $vals[
'timestamp'] =
wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
241 if ( isset( $prop[
'titlesnippet'] ) ) {
242 $vals[
'titlesnippet'] = $result->getTitleSnippet();
244 if ( isset( $prop[
'categorysnippet'] ) ) {
245 $vals[
'categorysnippet'] = $result->getCategorySnippet();
247 if ( !is_null( $result->getRedirectTitle() ) ) {
248 if ( isset( $prop[
'redirecttitle'] ) ) {
249 $vals[
'redirecttitle'] = $result->getRedirectTitle()->getPrefixedText();
251 if ( isset( $prop[
'redirectsnippet'] ) ) {
252 $vals[
'redirectsnippet'] = $result->getRedirectSnippet();
255 if ( !is_null( $result->getSectionTitle() ) ) {
256 if ( isset( $prop[
'sectiontitle'] ) ) {
257 $vals[
'sectiontitle'] = $result->getSectionTitle()->getFragment();
259 if ( isset( $prop[
'sectionsnippet'] ) ) {
260 $vals[
'sectionsnippet'] = $result->getSectionSnippet();
263 if ( isset( $prop[
'isfilematch'] ) ) {
264 $vals[
'isfilematch'] = $result->isFileMatch();
267 if ( isset( $prop[
'extensiondata'] ) ) {
268 $extra = $result->getExtensionData();
272 $vals[
'extensiondata'] = ApiResult::addMetadataToResultVars( $extra );
294 foreach (
$matches->getInterwikiResults(
$type ) as $interwikiMatches ) {
296 $totalhits += $interwikiMatches->getTotalHits();
298 foreach ( $interwikiMatches as $result ) {
299 $title = $result->getTitle();
302 $vals[
'namespace'] = $result->getInterwikiNamespaceText();
303 $vals[
'title'] =
$title->getText();
304 $vals[
'url'] =
$title->getFullURL();
310 $result->getInterwikiPrefix()
320 if ( $totalhits !==
null ) {
321 $apiResult->
addValue( [
'query', $section .
'searchinfo' ],
'totalhits', $totalhits );
335 if ( $this->allowedParams !==
null ) {
381 'interwiki' =>
false,
382 'enablerewrites' =>
false,
387 $services = MediaWiki\MediaWikiServices::getInstance();
388 $alternatives = $services
389 ->getSearchEngineConfig()
391 if ( count( $alternatives ) == 1 ) {
392 $this->allowedParams[
'sort'] = [
406 'profile-type' => SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE,
407 'help-message' =>
'apihelp-query+search-param-qiprofile',
414 'action=query&list=search&srsearch=meaning'
415 =>
'apihelp-query+search-example-simple',
416 'action=query&list=search&srwhat=text&srsearch=meaning'
417 =>
'apihelp-query+search-example-text',
418 'action=query&generator=search&gsrsearch=meaning&prop=info'
419 =>
'apihelp-query+search-example-generator',
424 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Search';
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
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=null)
Abort execution with an error.
const PARAM_DEPRECATED_VALUES
(array) When PARAM_TYPE is an array, this indicates which of the values are deprecated.
getMain()
Get the main module.
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
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.
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
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.