5 * Created on Oct 13, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
30 class ApiOpenSearch
extends ApiBase
{
32 public function getCustomPrinter() {
33 return $this->getMain()->createPrinterByName( 'json' );
36 public function execute() {
37 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
38 $params = $this->extractRequestParams();
39 $search = $params['search'];
40 $limit = $params['limit'];
41 $namespaces = $params['namespace'];
42 $suggest = $params['suggest'];
44 // Some script that was loaded regardless of wgEnableOpenSearchSuggest, likely cached.
45 if ( $suggest && !$wgEnableOpenSearchSuggest ) {
48 // Open search results may be stored for a very long time
49 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
50 $this->getMain()->setCacheMode( 'public' );
52 $searches = PrefixSearch
::titleSearch( $search, $limit,
55 // if the content language has variants, try to retrieve fallback results
56 $fallbackLimit = $limit - count( $searches );
57 if ( $fallbackLimit > 0 ) {
60 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
61 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
63 foreach ( $fallbackSearches as $fbs ) {
64 $fallbackSearchResult = PrefixSearch
::titleSearch( $fbs, $fallbackLimit,
66 $searches = array_merge( $searches, $fallbackSearchResult );
67 $fallbackLimit -= count( $fallbackSearchResult );
69 if ( $fallbackLimit == 0 ) {
75 // Set top level elements
76 $result = $this->getResult();
77 $result->addValue( null, 0, $search );
78 $result->addValue( null, 1, $searches );
81 public function getAllowedParams() {
85 ApiBase
::PARAM_DFLT
=> 10,
86 ApiBase
::PARAM_TYPE
=> 'limit',
87 ApiBase
::PARAM_MIN
=> 1,
88 ApiBase
::PARAM_MAX
=> 100,
89 ApiBase
::PARAM_MAX2
=> 100
92 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
93 ApiBase
::PARAM_TYPE
=> 'namespace',
94 ApiBase
::PARAM_ISMULTI
=> true
100 public function getParamDescription() {
102 'search' => 'Search string',
103 'limit' => 'Maximum amount of results to return',
104 'namespace' => 'Namespaces to search',
105 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
109 public function getDescription() {
110 return 'Search the wiki using the OpenSearch protocol';
113 public function getExamples() {
115 'api.php?action=opensearch&search=Te'
119 public function getHelpUrls() {
120 return 'https://www.mediawiki.org/wiki/API:Opensearch';