3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 * Copyright © 2008 Brooke Vibber <bvibber@wikimedia.org>
5 * Copyright © 2014 Wikimedia Foundation and contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
25 namespace MediaWiki\Api
;
27 use InvalidArgumentException
;
28 use MediaWiki\Cache\LinkBatchFactory
;
29 use MediaWiki\MainConfigNames
;
30 use MediaWiki\MediaWikiServices
;
31 use MediaWiki\Title\Title
;
32 use MediaWiki\Utils\UrlUtils
;
34 use SearchEngineConfig
;
35 use SearchEngineFactory
;
36 use Wikimedia\ParamValidator\ParamValidator
;
41 class ApiOpenSearch
extends ApiBase
{
42 use \MediaWiki\Api\SearchApi
;
44 /** @var string|null */
45 private $format = null;
46 /** @var string|null */
49 private LinkBatchFactory
$linkBatchFactory;
50 private UrlUtils
$urlUtils;
52 public function __construct(
55 LinkBatchFactory
$linkBatchFactory,
56 SearchEngineConfig
$searchEngineConfig,
57 SearchEngineFactory
$searchEngineFactory,
60 parent
::__construct( $mainModule, $moduleName );
61 $this->linkBatchFactory
= $linkBatchFactory;
62 // Services needed in SearchApi trait
63 $this->searchEngineConfig
= $searchEngineConfig;
64 $this->searchEngineFactory
= $searchEngineFactory;
65 $this->urlUtils
= $urlUtils;
69 * Get the output format
73 protected function getFormat() {
74 if ( $this->format
=== null ) {
75 $format = $this->getParameter( 'format' );
77 if ( str_ends_with( $format, 'fm' ) ) {
78 $this->format
= substr( $format, 0, -2 );
81 $this->format
= $format;
88 public function getCustomPrinter() {
89 switch ( $this->getFormat() ) {
91 return new ApiOpenSearchFormatJson(
92 $this->getMain(), $this->fm
, $this->getParameter( 'warningsaserror' )
96 $printer = $this->getMain()->createPrinterByName( 'xml' . $this->fm
);
97 '@phan-var ApiFormatXml $printer';
98 /** @var ApiFormatXml $printer */
99 $printer->setRootElement( 'SearchSuggestion' );
103 ApiBase
::dieDebug( __METHOD__
, "Unsupported format '{$this->getFormat()}'" );
107 public function execute() {
108 $params = $this->extractRequestParams();
109 $search = $params['search'];
111 // Open search results may be stored for a very long time
112 $this->getMain()->setCacheMaxAge(
113 $this->getConfig()->get( MainConfigNames
::SearchSuggestCacheExpiry
) );
114 $this->getMain()->setCacheMode( 'public' );
115 $results = $this->search( $search, $params );
117 // Allow hooks to populate extracts and images
118 $this->getHookRunner()->onApiOpenSearchSuggest( $results );
120 // Trim extracts, if necessary
121 $length = $this->getConfig()->get( MainConfigNames
::OpenSearchDescriptionLength
);
122 foreach ( $results as &$r ) {
123 if ( is_string( $r['extract'] ) && !$r['extract trimmed'] ) {
124 $r['extract'] = self
::trimExtract( $r['extract'], $length );
128 // Populate result object
129 $this->populateResult( $search, $results );
134 * @param string $search the search query
135 * @param array $params api request params
136 * @return array search results. Keys are integers.
137 * @phan-return array<array{title:Title,redirect_from:?Title,extract:false,extract_trimmed:false,image:false,url:string}>
138 * Note that phan annotations don't support keys containing a space.
140 private function search( $search, array $params ) {
141 $searchEngine = $this->buildSearchEngine( $params );
142 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
149 // Special pages need unique integer ids in the return list, so we just
150 // assign them negative numbers because those won't clash with the
151 // always positive articleIds that non-special pages get.
152 $nextSpecialPageId = -1;
154 if ( $params['redirects'] === null ) {
155 // Backwards compatibility, don't resolve for JSON.
156 $resolveRedir = $this->getFormat() !== 'json';
158 $resolveRedir = $params['redirects'] === 'resolve';
161 if ( $resolveRedir ) {
162 // Query for redirects
164 $lb = $this->linkBatchFactory
->newLinkBatch( $titles );
165 if ( !$lb->isEmpty() ) {
166 $db = $this->getDB();
167 $res = $db->newSelectQueryBuilder()
168 ->select( [ 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ] )
170 ->join( 'redirect', null, [ 'rd_from = page_id' ] )
172 'rd_interwiki' => '',
173 $lb->constructSet( 'page', $db )
175 ->caller( __METHOD__
)
177 foreach ( $res as $row ) {
178 $redirects[$row->page_namespace
][$row->page_title
] =
179 [ $row->rd_namespace
, $row->rd_title
];
183 // Bypass any redirects
185 foreach ( $titles as $title ) {
186 $ns = $title->getNamespace();
187 $dbkey = $title->getDBkey();
189 if ( isset( $redirects[$ns][$dbkey] ) ) {
190 [ $ns, $dbkey ] = $redirects[$ns][$dbkey];
192 $title = Title
::makeTitle( $ns, $dbkey );
194 if ( !isset( $seen[$ns][$dbkey] ) ) {
195 $seen[$ns][$dbkey] = true;
196 $resultId = $title->getArticleID();
197 if ( $resultId === 0 ) {
198 $resultId = $nextSpecialPageId;
199 $nextSpecialPageId--;
201 $results[$resultId] = [
203 'redirect from' => $from,
205 'extract trimmed' => false,
207 'url' => (string)$this->urlUtils
->expand( $title->getFullURL(), PROTO_CURRENT
),
212 foreach ( $titles as $title ) {
213 $resultId = $title->getArticleID();
214 if ( $resultId === 0 ) {
215 $resultId = $nextSpecialPageId;
216 $nextSpecialPageId--;
218 $results[$resultId] = [
220 'redirect from' => null,
222 'extract trimmed' => false,
224 'url' => (string)$this->urlUtils
->expand( $title->getFullURL(), PROTO_CURRENT
),
233 * @param string $search
234 * @param array[] &$results
236 protected function populateResult( $search, &$results ) {
237 $result = $this->getResult();
239 switch ( $this->getFormat() ) {
241 // http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
242 $result->addArrayType( null, 'array' );
243 $result->addValue( null, 0, strval( $search ) );
247 foreach ( $results as $r ) {
248 $terms[] = $r['title']->getPrefixedText();
249 $descriptions[] = strval( $r['extract'] );
252 $result->addValue( null, 1, $terms );
253 $result->addValue( null, 2, $descriptions );
254 $result->addValue( null, 3, $urls );
258 // https://msdn.microsoft.com/en-us/library/cc891508(v=vs.85).aspx
267 foreach ( $results as $r ) {
269 'Text' => $r['title']->getPrefixedText(),
272 if ( is_string( $r['extract'] ) && $r['extract'] !== '' ) {
273 $item['Description'] = $r['extract'];
275 if ( is_array( $r['image'] ) && isset( $r['image']['source'] ) ) {
276 $item['Image'] = array_intersect_key( $r['image'], $imageKeys );
278 ApiResult
::setSubelementsList( $item, array_keys( $item ) );
281 ApiResult
::setIndexedTagName( $items, 'Item' );
282 $result->addValue( null, 'version', '2.0' );
283 $result->addValue( null, 'xmlns', 'http://opensearch.org/searchsuggest2' );
284 $result->addValue( null, 'Query', strval( $search ) );
285 $result->addSubelementsList( null, 'Query' );
286 $result->addValue( null, 'Section', $items );
290 ApiBase
::dieDebug( __METHOD__
, "Unsupported format '{$this->getFormat()}'" );
294 public function getAllowedParams() {
295 $allowedParams = $this->buildCommonApiParams( false ) +
[
297 ParamValidator
::PARAM_DEFAULT
=> false,
298 // Deprecated since 1.35
299 ParamValidator
::PARAM_DEPRECATED
=> true,
302 ParamValidator
::PARAM_TYPE
=> [ 'return', 'resolve' ],
303 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
304 ApiBase
::PARAM_HELP_MSG_APPEND
=> [ 'apihelp-opensearch-param-redirects-append' ],
307 ParamValidator
::PARAM_DEFAULT
=> 'json',
308 ParamValidator
::PARAM_TYPE
=> [ 'json', 'jsonfm', 'xml', 'xmlfm' ],
310 'warningsaserror' => false,
313 // Use open search specific default limit
314 $allowedParams['limit'][ParamValidator
::PARAM_DEFAULT
] = $this->getConfig()->get(
315 MainConfigNames
::OpenSearchDefaultLimit
318 return $allowedParams;
321 public function getSearchProfileParams() {
324 'profile-type' => SearchEngine
::COMPLETION_PROFILE_TYPE
,
325 'help-message' => 'apihelp-query+prefixsearch-param-profile'
330 protected function getExamplesMessages() {
332 'action=opensearch&search=Te'
333 => 'apihelp-opensearch-example-te',
337 public function getHelpUrls() {
338 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Opensearch';
342 * Trim an extract to a sensible length.
344 * Adapted from Extension:OpenSearchXml, which adapted it from
345 * Extension:ActiveAbstract.
347 * @param string $text
348 * @param int $length Target length; actual result will continue to the end of a sentence.
351 public static function trimExtract( $text, $length ) {
352 static $regex = null;
354 if ( $regex === null ) {
356 '([^\d])\.\s', '\!\s', '\?\s', // regular ASCII
357 '。', // full-width ideographic full-stop
358 '.', '!', '?', // double-width roman forms
359 '。', // half-width ideographic full stop
361 $endgroup = implode( '|', $endchars );
362 $end = "(?:$endgroup)";
363 $sentence = ".{{$length},}?$end+";
364 $regex = "/^($sentence)/u";
368 if ( preg_match( $regex, $text, $matches ) ) {
369 return trim( $matches[1] );
371 // Just return the first line
372 return trim( explode( "\n", $text )[0] );
377 * Fetch the template for a type.
379 * @param string $type MIME type
382 public static function getOpenSearchTemplate( $type ) {
383 $services = MediaWikiServices
::getInstance();
384 $canonicalServer = $services->getMainConfig()->get( MainConfigNames
::CanonicalServer
);
385 $searchEngineConfig = $services->getSearchEngineConfig();
386 $ns = implode( '|', $searchEngineConfig->defaultNamespaces() );
392 case 'application/x-suggestions+json':
393 return $canonicalServer .
394 wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
396 case 'application/x-suggestions+xml':
397 return $canonicalServer .
399 '?action=opensearch&format=xml&search={searchTerms}&namespace=' . $ns;
402 throw new InvalidArgumentException( __METHOD__
. ": Unknown type '$type'" );
407 /** @deprecated class alias since 1.43 */
408 class_alias( ApiOpenSearch
::class, 'ApiOpenSearch' );