2007-07-30 08:09:15 +00:00
|
|
|
<?php
|
2010-02-24 14:45:19 +00:00
|
|
|
/**
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
|
2007-07-30 08:09:15 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2007-07-30 08:09:15 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2007-07-30 08:09:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Query module to perform full text search within wiki titles and content
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-07-30 08:09:15 +00:00
|
|
|
*/
|
|
|
|
|
class ApiQuerySearch extends ApiQueryGeneratorBase {
|
2016-03-31 09:13:21 +00:00
|
|
|
use SearchApi;
|
|
|
|
|
|
|
|
|
|
/** @var array list of api allowed params */
|
|
|
|
|
private $allowedParams;
|
2007-07-30 08:09:15 +00:00
|
|
|
|
2014-03-25 17:22:11 +00:00
|
|
|
public function __construct( ApiQuery $query, $moduleName ) {
|
2010-02-24 14:45:19 +00:00
|
|
|
parent::__construct( $query, $moduleName, 'sr' );
|
2007-07-30 08:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$this->run();
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-25 20:18:12 +00:00
|
|
|
public function executeGenerator( $resultPageSet ) {
|
|
|
|
|
$this->run( $resultPageSet );
|
2007-07-30 08:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-19 00:30:18 +00:00
|
|
|
/**
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param ApiPageSet $resultPageSet
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-02-19 00:30:18 +00:00
|
|
|
*/
|
2009-08-25 20:18:12 +00:00
|
|
|
private function run( $resultPageSet = null ) {
|
2007-07-30 08:09:15 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
2009-08-25 20:18:12 +00:00
|
|
|
// Extract parameters
|
2008-04-14 07:45:50 +00:00
|
|
|
$query = $params['search'];
|
2008-07-27 21:31:11 +00:00
|
|
|
$what = $params['what'];
|
2014-02-12 17:30:22 +00:00
|
|
|
$interwiki = $params['interwiki'];
|
2009-08-25 20:18:12 +00:00
|
|
|
$searchInfo = array_flip( $params['info'] );
|
|
|
|
|
$prop = array_flip( $params['prop'] );
|
2010-02-24 14:45:19 +00:00
|
|
|
|
2009-08-25 20:18:12 +00:00
|
|
|
// Create search engine instance and set options
|
2016-03-31 09:13:21 +00:00
|
|
|
$search = $this->buildSearchEngine( $params );
|
2018-05-19 13:42:54 +00:00
|
|
|
if ( isset( $params['sort'] ) ) {
|
|
|
|
|
$search->setSort( $params['sort'] );
|
|
|
|
|
}
|
2015-07-28 18:26:21 +00:00
|
|
|
$search->setFeatureData( 'rewrite', (bool)$params['enablerewrites'] );
|
2016-09-30 14:41:00 +00:00
|
|
|
$search->setFeatureData( 'interwiki', (bool)$interwiki );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2018-07-10 12:10:09 +00:00
|
|
|
$nquery = $search->replacePrefixes( $query );
|
|
|
|
|
if ( $nquery !== $query ) {
|
|
|
|
|
$query = $nquery;
|
|
|
|
|
wfDeprecated( 'SearchEngine::replacePrefixes() (overridden by ' .
|
|
|
|
|
get_class( $search ) . ')', '1.32' );
|
|
|
|
|
}
|
2009-08-25 20:18:12 +00:00
|
|
|
// Perform the actual search
|
|
|
|
|
if ( $what == 'text' ) {
|
2007-07-30 08:09:15 +00:00
|
|
|
$matches = $search->searchText( $query );
|
2010-01-11 15:55:52 +00:00
|
|
|
} elseif ( $what == 'title' ) {
|
2007-07-30 08:09:15 +00:00
|
|
|
$matches = $search->searchTitle( $query );
|
2010-07-09 11:08:18 +00:00
|
|
|
} elseif ( $what == 'nearmatch' ) {
|
2015-04-30 23:59:49 +00:00
|
|
|
// near matches must receive the user input as provided, otherwise
|
|
|
|
|
// the near matches within namespaces are lost.
|
2016-04-03 08:37:11 +00:00
|
|
|
$matches = $search->getNearMatcher( $this->getConfig() )
|
|
|
|
|
->getNearMatchResultSet( $params['search'] );
|
2008-07-27 21:31:11 +00:00
|
|
|
} else {
|
|
|
|
|
// We default to title searches; this is a terrible legacy
|
|
|
|
|
// of the way we initially set up the MySQL fulltext-based
|
|
|
|
|
// search engine with separate title and text fields.
|
|
|
|
|
// In the future, the default should be for a combined index.
|
2008-12-03 19:33:57 +00:00
|
|
|
$what = 'title';
|
2008-07-27 21:31:11 +00:00
|
|
|
$matches = $search->searchTitle( $query );
|
2010-02-24 14:45:19 +00:00
|
|
|
|
2008-07-27 21:31:11 +00:00
|
|
|
// Not all search engines support a separate title search,
|
|
|
|
|
// for instance the Lucene-based engine we use on Wikipedia.
|
|
|
|
|
// In this case, fall back to full-text search (which will
|
|
|
|
|
// include titles in it!)
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( is_null( $matches ) ) {
|
2008-12-03 19:33:57 +00:00
|
|
|
$what = 'text';
|
2008-07-27 21:31:11 +00:00
|
|
|
$matches = $search->searchText( $query );
|
2008-12-03 19:33:57 +00:00
|
|
|
}
|
2008-07-27 21:31:11 +00:00
|
|
|
}
|
2016-11-09 00:41:11 +00:00
|
|
|
|
|
|
|
|
if ( $matches instanceof Status ) {
|
|
|
|
|
$status = $matches;
|
|
|
|
|
$matches = $status->getValue();
|
|
|
|
|
} else {
|
|
|
|
|
$status = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $status ) {
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
$this->getMain()->getErrorFormatter()->addMessagesFromStatus(
|
|
|
|
|
$this->getModuleName(),
|
|
|
|
|
$status
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieStatus( $status );
|
2016-11-09 00:41:11 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( is_null( $matches ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( [ 'apierror-searchdisabled', $what ], "search-{$what}-disabled" );
|
2010-02-24 14:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-12 19:16:30 +00:00
|
|
|
if ( $resultPageSet === null ) {
|
|
|
|
|
$apiResult = $this->getResult();
|
|
|
|
|
// Add search meta data to result
|
|
|
|
|
if ( isset( $searchInfo['totalhits'] ) ) {
|
|
|
|
|
$totalhits = $matches->getTotalHits();
|
|
|
|
|
if ( $totalhits !== null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$apiResult->addValue( [ 'query', 'searchinfo' ],
|
2014-11-12 19:16:30 +00:00
|
|
|
'totalhits', $totalhits );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$apiResult->addValue( [ 'query', 'searchinfo' ],
|
2014-11-12 19:16:30 +00:00
|
|
|
'suggestion', $matches->getSuggestionQuery() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$apiResult->addValue( [ 'query', 'searchinfo' ],
|
2015-04-29 20:42:03 +00:00
|
|
|
'suggestionsnippet', $matches->getSuggestionSnippet() );
|
2009-08-25 20:18:12 +00:00
|
|
|
}
|
2015-07-28 18:26:21 +00:00
|
|
|
if ( isset( $searchInfo['rewrittenquery'] ) && $matches->hasRewrittenQuery() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$apiResult->addValue( [ 'query', 'searchinfo' ],
|
2015-07-28 18:26:21 +00:00
|
|
|
'rewrittenquery', $matches->getQueryAfterRewrite() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$apiResult->addValue( [ 'query', 'searchinfo' ],
|
2015-07-28 18:26:21 +00:00
|
|
|
'rewrittenquerysnippet', $matches->getQueryAfterRewriteSnippet() );
|
|
|
|
|
}
|
* Added fields to list=search output: size, wordcount, timestamp, snippet
* Where supported by backend, list=search adds a 'searchinfo' element with
optional info: 'totalhits' count and 'suggestion' alternate query term
Snippets added to result items earlier by Roan; extended this with the other
byte size, word count, and timestamp available on the result items and exposed
through the regular UI.
Had to work out a backwards-compatible method for the search meta-information
with Roan; added a second 'searchinfo' element since adding attributes to
'search' would break compatibility for JSON output (despite being safe in XML).
'searchinfo' is present only if the backend supports the extra info and has
something available; 'totalhits' with a total hit count and 'suggestion' for
an alternate query suggestion (exposed as "Did you mean X?" link in UI).
Note that total hit counts can be enabled for MySQL backend now by setting
the experimental option $wgSearchMySQLTotalHits, but did-you-mean suggestions
are not yet supported and need to be tested with a hack or another backend.
Sample XML and JSON output with the new searchinfo items (which can be
present whether or not there are any result items):
<?xml version="1.0"?>
<api>
<query>
<searchinfo totalhits="0" suggestion="joe momma" />
<search />
</query>
</api>
{
"query": {
"searchinfo": {
"totalhits": 0,
"suggestion": "joe momma"
},
"search": [
]
}
}
The suggestion value is suitable for plugging back in as a search term,
if present.
2009-07-28 21:13:48 +00:00
|
|
|
}
|
2007-07-30 08:09:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$titles = [];
|
2007-07-30 08:09:15 +00:00
|
|
|
$count = 0;
|
2011-07-06 20:18:22 +00:00
|
|
|
|
2017-12-19 22:19:49 +00:00
|
|
|
if ( $matches->hasMoreResults() ) {
|
|
|
|
|
$this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
|
|
|
|
|
}
|
2007-07-30 08:09:15 +00:00
|
|
|
|
2017-12-19 22:19:49 +00:00
|
|
|
foreach ( $matches as $result ) {
|
|
|
|
|
$count++;
|
2008-08-16 21:00:19 +00:00
|
|
|
// Silently skip broken and missing titles
|
2010-02-24 14:45:19 +00:00
|
|
|
if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
|
2016-12-19 17:37:01 +00:00
|
|
|
continue;
|
2010-02-24 14:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-12 19:16:30 +00:00
|
|
|
if ( $resultPageSet === null ) {
|
2019-06-12 09:02:10 +00:00
|
|
|
$vals = $this->getSearchResultData( $result, $prop );
|
2016-12-01 04:22:59 +00:00
|
|
|
if ( $vals ) {
|
|
|
|
|
// Add item to results and see whether it fits
|
|
|
|
|
$fit = $apiResult->addValue( [ 'query', $this->getModuleName() ], null, $vals );
|
|
|
|
|
if ( !$fit ) {
|
|
|
|
|
$this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
|
|
|
|
|
break;
|
2010-11-01 18:14:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-07-30 08:09:15 +00:00
|
|
|
} else {
|
2016-12-01 04:22:59 +00:00
|
|
|
$titles[] = $result->getTitle();
|
2007-07-30 08:09:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-01 04:22:59 +00:00
|
|
|
// Here we assume interwiki results do not count with
|
|
|
|
|
// regular search results. We may want to reconsider this
|
|
|
|
|
// if we ever return a lot of interwiki results or want pagination
|
|
|
|
|
// for them.
|
|
|
|
|
// Interwiki results inside main result set
|
|
|
|
|
$canAddInterwiki = (bool)$params['enablerewrites'] && ( $resultPageSet === null );
|
|
|
|
|
if ( $canAddInterwiki ) {
|
2019-06-12 09:02:10 +00:00
|
|
|
$this->addInterwikiResults( $matches, $apiResult, $prop, 'additional',
|
2019-07-22 15:28:48 +00:00
|
|
|
ISearchResultSet::INLINE_RESULTS );
|
2016-12-01 04:22:59 +00:00
|
|
|
}
|
2014-02-12 17:30:22 +00:00
|
|
|
|
2016-12-01 04:22:59 +00:00
|
|
|
// Interwiki results outside main result set
|
|
|
|
|
if ( $interwiki && $resultPageSet === null ) {
|
2019-06-12 09:02:10 +00:00
|
|
|
$this->addInterwikiResults( $matches, $apiResult, $prop, 'interwiki',
|
2019-07-22 15:28:48 +00:00
|
|
|
ISearchResultSet::SECONDARY_RESULTS );
|
2014-02-12 17:30:22 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-12 19:16:30 +00:00
|
|
|
if ( $resultPageSet === null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$apiResult->addIndexedTagName( [
|
2013-11-14 13:00:02 +00:00
|
|
|
'query', $this->getModuleName()
|
2016-02-17 09:09:32 +00:00
|
|
|
], 'p' );
|
2007-07-30 08:09:15 +00:00
|
|
|
} else {
|
2015-10-07 21:03:15 +00:00
|
|
|
$resultPageSet->setRedirectMergePolicy( function ( $current, $new ) {
|
|
|
|
|
if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
|
|
|
|
|
$current['index'] = $new['index'];
|
|
|
|
|
}
|
|
|
|
|
return $current;
|
|
|
|
|
} );
|
2009-08-25 20:18:12 +00:00
|
|
|
$resultPageSet->populateFromTitles( $titles );
|
2014-11-24 21:21:49 +00:00
|
|
|
$offset = $params['offset'] + 1;
|
|
|
|
|
foreach ( $titles as $index => $title ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset ] );
|
2014-11-24 21:21:49 +00:00
|
|
|
}
|
2007-07-30 08:09:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-01 04:22:59 +00:00
|
|
|
/**
|
|
|
|
|
* Assemble search result data.
|
|
|
|
|
* @param SearchResult $result Search result
|
|
|
|
|
* @param array $prop Props to extract (as keys)
|
|
|
|
|
* @return array|null Result data or null if result is broken in some way.
|
|
|
|
|
*/
|
2019-06-12 09:02:10 +00:00
|
|
|
private function getSearchResultData( SearchResult $result, $prop ) {
|
2016-12-01 04:22:59 +00:00
|
|
|
// Silently skip broken and missing titles
|
|
|
|
|
if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$vals = [];
|
|
|
|
|
|
|
|
|
|
$title = $result->getTitle();
|
|
|
|
|
ApiQueryBase::addTitleInfo( $vals, $title );
|
2017-08-09 21:54:44 +00:00
|
|
|
$vals['pageid'] = $title->getArticleID();
|
2016-12-01 04:22:59 +00:00
|
|
|
|
|
|
|
|
if ( isset( $prop['size'] ) ) {
|
|
|
|
|
$vals['size'] = $result->getByteSize();
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $prop['wordcount'] ) ) {
|
|
|
|
|
$vals['wordcount'] = $result->getWordCount();
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $prop['snippet'] ) ) {
|
2019-06-12 09:02:10 +00:00
|
|
|
$vals['snippet'] = $result->getTextSnippet();
|
2016-12-01 04:22:59 +00:00
|
|
|
}
|
|
|
|
|
if ( isset( $prop['timestamp'] ) ) {
|
|
|
|
|
$vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $prop['titlesnippet'] ) ) {
|
|
|
|
|
$vals['titlesnippet'] = $result->getTitleSnippet();
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $prop['categorysnippet'] ) ) {
|
|
|
|
|
$vals['categorysnippet'] = $result->getCategorySnippet();
|
|
|
|
|
}
|
|
|
|
|
if ( !is_null( $result->getRedirectTitle() ) ) {
|
|
|
|
|
if ( isset( $prop['redirecttitle'] ) ) {
|
|
|
|
|
$vals['redirecttitle'] = $result->getRedirectTitle()->getPrefixedText();
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $prop['redirectsnippet'] ) ) {
|
|
|
|
|
$vals['redirectsnippet'] = $result->getRedirectSnippet();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( !is_null( $result->getSectionTitle() ) ) {
|
|
|
|
|
if ( isset( $prop['sectiontitle'] ) ) {
|
|
|
|
|
$vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $prop['sectionsnippet'] ) ) {
|
|
|
|
|
$vals['sectionsnippet'] = $result->getSectionSnippet();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $prop['isfilematch'] ) ) {
|
|
|
|
|
$vals['isfilematch'] = $result->isFileMatch();
|
|
|
|
|
}
|
2017-12-06 00:10:45 +00:00
|
|
|
|
|
|
|
|
if ( isset( $prop['extensiondata'] ) ) {
|
|
|
|
|
$extra = $result->getExtensionData();
|
|
|
|
|
// Add augmented data to the result. The data would be organized as a map:
|
|
|
|
|
// augmentorName => data
|
|
|
|
|
if ( $extra ) {
|
|
|
|
|
$vals['extensiondata'] = ApiResult::addMetadataToResultVars( $extra );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-01 04:22:59 +00:00
|
|
|
return $vals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add interwiki results as a section in query results.
|
2019-07-22 15:28:48 +00:00
|
|
|
* @param ISearchResultSet $matches
|
2016-12-01 04:22:59 +00:00
|
|
|
* @param ApiResult $apiResult
|
|
|
|
|
* @param array $prop Props to extract (as keys)
|
|
|
|
|
* @param string $section Section name where results would go
|
|
|
|
|
* @param int $type Interwiki result type
|
|
|
|
|
* @return int|null Number of total hits in the data or null if none was produced
|
|
|
|
|
*/
|
|
|
|
|
private function addInterwikiResults(
|
2019-07-22 15:28:48 +00:00
|
|
|
ISearchResultSet $matches, ApiResult $apiResult, $prop,
|
2019-06-12 09:02:10 +00:00
|
|
|
$section, $type
|
2016-12-01 04:22:59 +00:00
|
|
|
) {
|
|
|
|
|
$totalhits = null;
|
|
|
|
|
if ( $matches->hasInterwikiResults( $type ) ) {
|
|
|
|
|
foreach ( $matches->getInterwikiResults( $type ) as $interwikiMatches ) {
|
|
|
|
|
// Include number of results if requested
|
|
|
|
|
$totalhits += $interwikiMatches->getTotalHits();
|
|
|
|
|
|
2018-05-10 22:03:55 +00:00
|
|
|
foreach ( $interwikiMatches as $result ) {
|
2016-12-01 04:22:59 +00:00
|
|
|
$title = $result->getTitle();
|
2019-06-12 09:02:10 +00:00
|
|
|
$vals = $this->getSearchResultData( $result, $prop );
|
2016-12-01 04:22:59 +00:00
|
|
|
|
|
|
|
|
$vals['namespace'] = $result->getInterwikiNamespaceText();
|
|
|
|
|
$vals['title'] = $title->getText();
|
|
|
|
|
$vals['url'] = $title->getFullURL();
|
|
|
|
|
|
|
|
|
|
// Add item to results and see whether it fits
|
|
|
|
|
$fit = $apiResult->addValue( [
|
|
|
|
|
'query',
|
|
|
|
|
$section . $this->getModuleName(),
|
|
|
|
|
$result->getInterwikiPrefix()
|
|
|
|
|
], null, $vals );
|
|
|
|
|
|
|
|
|
|
if ( !$fit ) {
|
|
|
|
|
// We hit the limit. We can't really provide any meaningful
|
|
|
|
|
// pagination info so just bail out
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $totalhits !== null ) {
|
|
|
|
|
$apiResult->addValue( [ 'query', $section . 'searchinfo' ], 'totalhits', $totalhits );
|
|
|
|
|
$apiResult->addIndexedTagName( [
|
|
|
|
|
'query', $section . $this->getModuleName()
|
|
|
|
|
], 'p' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $totalhits;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
public function getCacheMode( $params ) {
|
|
|
|
|
return 'public';
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2016-03-31 09:13:21 +00:00
|
|
|
if ( $this->allowedParams !== null ) {
|
|
|
|
|
return $this->allowedParams;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 20:32:59 +00:00
|
|
|
$this->allowedParams = $this->buildCommonApiParams() + [
|
2016-02-17 09:09:32 +00:00
|
|
|
'what' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => [
|
2007-07-30 08:09:15 +00:00
|
|
|
'title',
|
|
|
|
|
'text',
|
2010-07-09 11:08:18 +00:00
|
|
|
'nearmatch',
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'info' => [
|
2015-07-28 18:26:21 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'totalhits|suggestion|rewrittenquery',
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_TYPE => [
|
2009-08-25 20:18:12 +00:00
|
|
|
'totalhits',
|
|
|
|
|
'suggestion',
|
2015-07-28 18:26:21 +00:00
|
|
|
'rewrittenquery',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2010-02-24 14:45:19 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'prop' => [
|
2010-02-24 14:45:19 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_TYPE => [
|
2009-08-25 20:18:12 +00:00
|
|
|
'size',
|
|
|
|
|
'wordcount',
|
|
|
|
|
'timestamp',
|
|
|
|
|
'snippet',
|
2010-11-01 18:14:45 +00:00
|
|
|
'titlesnippet',
|
|
|
|
|
'redirecttitle',
|
|
|
|
|
'redirectsnippet',
|
|
|
|
|
'sectiontitle',
|
|
|
|
|
'sectionsnippet',
|
2015-04-29 20:42:03 +00:00
|
|
|
'isfilematch',
|
|
|
|
|
'categorysnippet',
|
2015-08-06 07:38:57 +00:00
|
|
|
'score', // deprecated
|
|
|
|
|
'hasrelated', // deprecated
|
2017-12-06 00:10:45 +00:00
|
|
|
'extensiondata',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2010-02-24 14:45:19 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
|
2017-05-25 20:07:25 +00:00
|
|
|
ApiBase::PARAM_DEPRECATED_VALUES => [
|
|
|
|
|
'score' => true,
|
|
|
|
|
'hasrelated' => true
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-02-12 17:30:22 +00:00
|
|
|
'interwiki' => false,
|
2015-07-28 18:26:21 +00:00
|
|
|
'enablerewrites' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-07-31 18:33:57 +00:00
|
|
|
|
2018-05-19 13:42:54 +00:00
|
|
|
// If we have more than one engine the list of available sorts is
|
|
|
|
|
// difficult to represent. For now don't expose it.
|
2018-08-18 04:02:39 +00:00
|
|
|
$services = MediaWiki\MediaWikiServices::getInstance();
|
|
|
|
|
$alternatives = $services
|
2018-05-19 13:42:54 +00:00
|
|
|
->getSearchEngineConfig()
|
|
|
|
|
->getSearchTypes();
|
|
|
|
|
if ( count( $alternatives ) == 1 ) {
|
|
|
|
|
$this->allowedParams['sort'] = [
|
|
|
|
|
ApiBase::PARAM_DFLT => 'relevance',
|
2018-08-18 04:02:39 +00:00
|
|
|
ApiBase::PARAM_TYPE => $services
|
2018-05-19 13:42:54 +00:00
|
|
|
->newSearchEngine()
|
|
|
|
|
->getValidSorts(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 09:13:21 +00:00
|
|
|
return $this->allowedParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getSearchProfileParams() {
|
2016-06-02 20:32:59 +00:00
|
|
|
return [
|
|
|
|
|
'qiprofile' => [
|
|
|
|
|
'profile-type' => SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE,
|
|
|
|
|
'help-message' => 'apihelp-query+search-param-qiprofile',
|
|
|
|
|
],
|
|
|
|
|
];
|
2007-07-30 08:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=query&list=search&srsearch=meaning'
|
|
|
|
|
=> 'apihelp-query+search-example-simple',
|
|
|
|
|
'action=query&list=search&srwhat=text&srsearch=meaning'
|
|
|
|
|
=> 'apihelp-query+search-example-text',
|
|
|
|
|
'action=query&generator=search&gsrsearch=meaning&prop=info'
|
|
|
|
|
=> 'apihelp-query+search-example-generator',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2007-07-30 08:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-17 17:02:06 +00:00
|
|
|
public function getHelpUrls() {
|
2017-04-04 22:52:57 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Search';
|
2011-07-17 17:02:06 +00:00
|
|
|
}
|
2009-08-26 21:13:06 +00:00
|
|
|
}
|