Merge "Declare visibility for class properties of LocalisationCacheBulkLoad"
[mediawiki.git] / includes / api / ApiOpenSearch.php
blob433b743a720299fb004c891af5b0684731669175
1 <?php
2 /**
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
22 * @file
25 /**
26 * @ingroup API
28 class ApiOpenSearch extends ApiBase {
30 /**
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 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
49 $params = $this->extractRequestParams();
50 $search = $params['search'];
51 $limit = $params['limit'];
52 $namespaces = $params['namespace'];
53 $suggest = $params['suggest'];
55 // Some script that was loaded regardless of wgEnableOpenSearchSuggest, likely cached.
56 if ( $suggest && !$wgEnableOpenSearchSuggest ) {
57 $searches = array();
58 } else {
59 // Open search results may be stored for a very long time
60 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
61 $this->getMain()->setCacheMode( 'public' );
63 $searches = PrefixSearch::titleSearch( $search, $limit,
64 $namespaces );
66 // if the content language has variants, try to retrieve fallback results
67 $fallbackLimit = $limit - count( $searches );
68 if ( $fallbackLimit > 0 ) {
69 global $wgContLang;
71 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
72 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
74 foreach ( $fallbackSearches as $fbs ) {
75 $fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
76 $namespaces );
77 $searches = array_merge( $searches, $fallbackSearchResult );
78 $fallbackLimit -= count( $fallbackSearchResult );
80 if ( $fallbackLimit == 0 ) {
81 break;
86 // Set top level elements
87 $result = $this->getResult();
88 $result->addValue( null, 0, $search );
89 $result->addValue( null, 1, $searches );
92 public function getAllowedParams() {
93 return array(
94 'search' => null,
95 'limit' => array(
96 ApiBase::PARAM_DFLT => 10,
97 ApiBase::PARAM_TYPE => 'limit',
98 ApiBase::PARAM_MIN => 1,
99 ApiBase::PARAM_MAX => 100,
100 ApiBase::PARAM_MAX2 => 100
102 'namespace' => array(
103 ApiBase::PARAM_DFLT => NS_MAIN,
104 ApiBase::PARAM_TYPE => 'namespace',
105 ApiBase::PARAM_ISMULTI => true
107 'suggest' => false,
108 'format' => array(
109 ApiBase::PARAM_DFLT => 'json',
110 ApiBase::PARAM_TYPE => array( 'json', 'jsonfm' ),
115 public function getParamDescription() {
116 return array(
117 'search' => 'Search string',
118 'limit' => 'Maximum amount of results to return',
119 'namespace' => 'Namespaces to search',
120 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
121 'format' => 'The format of the output',
125 public function getDescription() {
126 return 'Search the wiki using the OpenSearch protocol';
129 public function getExamples() {
130 return array(
131 'api.php?action=opensearch&search=Te'
135 public function getHelpUrls() {
136 return 'https://www.mediawiki.org/wiki/API:Opensearch';