Remove unreachable line in DifferenceEngine
[mediawiki.git] / includes / api / ApiOpenSearch.php
blob19741b00fa53a2f4835b2f2c30bce0a9be563a5e
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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
32 /**
33 * @ingroup API
35 class ApiOpenSearch extends ApiBase {
37 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
41 public function getCustomPrinter() {
42 return $this->getMain()->createPrinterByName( 'json' );
45 public function execute() {
46 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
47 $params = $this->extractRequestParams();
48 $search = $params['search'];
49 $limit = $params['limit'];
50 $namespaces = $params['namespace'];
51 $suggest = $params['suggest'];
53 // MWSuggest or similar hit
54 if ( $suggest && !$wgEnableOpenSearchSuggest ) {
55 $searches = array();
56 } else {
57 // Open search results may be stored for a very long time
58 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
59 $this->getMain()->setCacheMode( 'public' );
61 $searches = PrefixSearch::titleSearch( $search, $limit,
62 $namespaces );
64 // if the content language has variants, try to retrieve fallback results
65 $fallbackLimit = $limit - count( $searches );
66 if ( $fallbackLimit > 0 ) {
67 global $wgContLang;
69 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
70 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
72 foreach ( $fallbackSearches as $fbs ) {
73 $fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
74 $namespaces );
75 $searches = array_merge( $searches, $fallbackSearchResult );
76 $fallbackLimit -= count( $fallbackSearchResult );
78 if ( $fallbackLimit == 0 ) {
79 break;
84 // Set top level elements
85 $result = $this->getResult();
86 $result->addValue( null, 0, $search );
87 $result->addValue( null, 1, $searches );
90 public function getAllowedParams() {
91 return array(
92 'search' => null,
93 'limit' => array(
94 ApiBase::PARAM_DFLT => 10,
95 ApiBase::PARAM_TYPE => 'limit',
96 ApiBase::PARAM_MIN => 1,
97 ApiBase::PARAM_MAX => 100,
98 ApiBase::PARAM_MAX2 => 100
100 'namespace' => array(
101 ApiBase::PARAM_DFLT => NS_MAIN,
102 ApiBase::PARAM_TYPE => 'namespace',
103 ApiBase::PARAM_ISMULTI => true
105 'suggest' => false,
109 public function getParamDescription() {
110 return array(
111 'search' => 'Search string',
112 'limit' => 'Maximum amount of results to return',
113 'namespace' => 'Namespaces to search',
114 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
118 public function getDescription() {
119 return 'This module implements OpenSearch protocol';
122 protected function getExamples() {
123 return array(
124 'api.php?action=opensearch&search=Te'
128 public function getVersion() {
129 return __CLASS__ . ': $Id$';