3 * Created on Oct 13, 2006
5 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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
28 class ApiOpenSearch
extends ApiBase
{
31 * Override built-in handling of format parameter.
32 * Only JSON is supported.
34 * @return ApiFormatBase
36 public function getCustomPrinter() {
37 $params = $this->extractRequestParams();
38 $format = $params['format'];
39 $allowed = array( 'json', 'jsonfm' );
40 if ( in_array( $format, $allowed ) ) {
41 return $this->getMain()->createPrinterByName( $format );
44 return $this->getMain()->createPrinterByName( $allowed[0] );
47 public function execute() {
48 $params = $this->extractRequestParams();
49 $search = $params['search'];
50 $limit = $params['limit'];
51 $namespaces = $params['namespace'];
52 $suggest = $params['suggest'];
54 // Some script that was loaded regardless of wgEnableOpenSearchSuggest, likely cached.
55 if ( $suggest && !$this->getConfig()->get( 'EnableOpenSearchSuggest' ) ) {
58 // Open search results may be stored for a very long time
59 $this->getMain()->setCacheMaxAge( $this->getConfig()->get( 'SearchSuggestCacheExpiry' ) );
60 $this->getMain()->setCacheMode( 'public' );
62 $searcher = new StringPrefixSearch
;
63 $searches = $searcher->searchWithVariants( $search, $limit, $namespaces );
65 // Set top level elements
66 $result = $this->getResult();
67 $result->addValue( null, 0, $search );
68 $result->addValue( null, 1, $searches );
71 public function getAllowedParams() {
75 ApiBase
::PARAM_DFLT
=> $this->getConfig()->get( 'OpenSearchDefaultLimit' ),
76 ApiBase
::PARAM_TYPE
=> 'limit',
77 ApiBase
::PARAM_MIN
=> 1,
78 ApiBase
::PARAM_MAX
=> 100,
79 ApiBase
::PARAM_MAX2
=> 100
82 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
83 ApiBase
::PARAM_TYPE
=> 'namespace',
84 ApiBase
::PARAM_ISMULTI
=> true
88 ApiBase
::PARAM_DFLT
=> 'json',
89 ApiBase
::PARAM_TYPE
=> array( 'json', 'jsonfm' ),
94 public function getParamDescription() {
96 'search' => 'Search string',
97 'limit' => 'Maximum amount of results to return',
98 'namespace' => 'Namespaces to search',
99 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
100 'format' => 'The format of the output',
104 public function getDescription() {
105 return 'Search the wiki using the OpenSearch protocol.';
108 public function getExamples() {
110 'api.php?action=opensearch&search=Te'
114 public function getHelpUrls() {
115 return 'https://www.mediawiki.org/wiki/API:Opensearch';