Follow r79649 and r79650. Errrr, remove the redundant minus
[lhc/web/wiklou.git] / includes / api / ApiOpenSearch.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 13, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * @ingroup API
34 */
35 class ApiOpenSearch extends ApiBase {
36
37 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
39 }
40
41 public function getCustomPrinter() {
42 return $this->getMain()->createPrinterByName( 'json' );
43 }
44
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'];
52
53 // MWSuggest or similar hit
54 if ( $suggest && !$wgEnableOpenSearchSuggest ) {
55 $srchres = array();
56 } else {
57 // Open search results may be stored for a very long
58 // time
59 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
60 $this->getMain()->setCacheMode( 'public' );
61
62 $srchres = PrefixSearch::titleSearch( $search, $limit,
63 $namespaces );
64
65 // if the content language has variants, try to retrieve fallback results
66 $fblimit = $limit - count( $srchres );
67 if ( $fblimit > 0 ) {
68 global $wgContLang;
69 $fbsearchs = $wgContLang->autoConvertToAllVariants( $search );
70 $fbsearchs = array_diff( array_unique( $fbsearchs ), ( array ) $search );
71 foreach ( $fbsearchs as $fbsearch ) {
72 $_srchres = PrefixSearch::titleSearch( $fbsearch, $fblimit,
73 $namespaces );
74 $srchres = array_merge( $srchres, $_srchres );
75 $fblimit -= count( $_srchres );
76 if ( $fblimit == 0 ) {
77 break;
78 }
79 }
80 }
81 }
82 // Set top level elements
83 $result = $this->getResult();
84 $result->addValue( null, 0, $search );
85 $result->addValue( null, 1, $srchres );
86 }
87
88 public function getAllowedParams() {
89 return array(
90 'search' => null,
91 'limit' => array(
92 ApiBase::PARAM_DFLT => 10,
93 ApiBase::PARAM_TYPE => 'limit',
94 ApiBase::PARAM_MIN => 1,
95 ApiBase::PARAM_MAX => 100,
96 ApiBase::PARAM_MAX2 => 100
97 ),
98 'namespace' => array(
99 ApiBase::PARAM_DFLT => NS_MAIN,
100 ApiBase::PARAM_TYPE => 'namespace',
101 ApiBase::PARAM_ISMULTI => true
102 ),
103 'suggest' => false,
104 );
105 }
106
107 public function getParamDescription() {
108 return array(
109 'search' => 'Search string',
110 'limit' => 'Maximum amount of results to return',
111 'namespace' => 'Namespaces to search',
112 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
113 );
114 }
115
116 public function getDescription() {
117 return 'This module implements OpenSearch protocol';
118 }
119
120 protected function getExamples() {
121 return array(
122 'api.php?action=opensearch&search=Te'
123 );
124 }
125
126 public function getVersion() {
127 return __CLASS__ . ': $Id$';
128 }
129 }