Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / api / ApiOpenSearch.php
blob0727cffd45a1b0f508bdb5dab439d4ece96da561
1 <?php
2 /**
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
24 * @file
27 /**
28 * @ingroup API
30 class ApiOpenSearch extends ApiBase {
32 public function __construct( $main, $action ) {
33 parent::__construct( $main, $action );
36 public function getCustomPrinter() {
37 return $this->getMain()->createPrinterByName( 'json' );
40 public function execute() {
41 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
42 $params = $this->extractRequestParams();
43 $search = $params['search'];
44 $limit = $params['limit'];
45 $namespaces = $params['namespace'];
46 $suggest = $params['suggest'];
48 // MWSuggest or similar hit
49 if ( $suggest && !$wgEnableOpenSearchSuggest ) {
50 $searches = array();
51 } else {
52 // Open search results may be stored for a very long time
53 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
54 $this->getMain()->setCacheMode( 'public' );
56 $searches = PrefixSearch::titleSearch( $search, $limit,
57 $namespaces );
59 // if the content language has variants, try to retrieve fallback results
60 $fallbackLimit = $limit - count( $searches );
61 if ( $fallbackLimit > 0 ) {
62 global $wgContLang;
64 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
65 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
67 foreach ( $fallbackSearches as $fbs ) {
68 $fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
69 $namespaces );
70 $searches = array_merge( $searches, $fallbackSearchResult );
71 $fallbackLimit -= count( $fallbackSearchResult );
73 if ( $fallbackLimit == 0 ) {
74 break;
79 // Set top level elements
80 $result = $this->getResult();
81 $result->addValue( null, 0, $search );
82 $result->addValue( null, 1, $searches );
85 public function getAllowedParams() {
86 return array(
87 'search' => null,
88 'limit' => array(
89 ApiBase::PARAM_DFLT => 10,
90 ApiBase::PARAM_TYPE => 'limit',
91 ApiBase::PARAM_MIN => 1,
92 ApiBase::PARAM_MAX => 100,
93 ApiBase::PARAM_MAX2 => 100
95 'namespace' => array(
96 ApiBase::PARAM_DFLT => NS_MAIN,
97 ApiBase::PARAM_TYPE => 'namespace',
98 ApiBase::PARAM_ISMULTI => true
100 'suggest' => false,
104 public function getParamDescription() {
105 return array(
106 'search' => 'Search string',
107 'limit' => 'Maximum amount of results to return',
108 'namespace' => 'Namespaces to search',
109 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
113 public function getDescription() {
114 return 'Search the wiki using the OpenSearch protocol';
117 public function getExamples() {
118 return array(
119 'api.php?action=opensearch&search=Te'
123 public function getHelpUrls() {
124 return 'https://www.mediawiki.org/wiki/API:Opensearch';
127 public function getVersion() {
128 return __CLASS__ . ': $Id$';