Followup r92396, add help urls for most of the core (non query) modules
[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 $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' );
60
61 $searches = PrefixSearch::titleSearch( $search, $limit,
62 $namespaces );
63
64 // if the content language has variants, try to retrieve fallback results
65 $fallbackLimit = $limit - count( $searches );
66 if ( $fallbackLimit > 0 ) {
67 global $wgContLang;
68
69 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
70 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
71
72 foreach ( $fallbackSearches as $fbs ) {
73 $fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
74 $namespaces );
75 $searches = array_merge( $searches, $fallbackSearchResult );
76 $fallbackLimit -= count( $fallbackSearchResult );
77
78 if ( $fallbackLimit == 0 ) {
79 break;
80 }
81 }
82 }
83 }
84 // Set top level elements
85 $result = $this->getResult();
86 $result->addValue( null, 0, $search );
87 $result->addValue( null, 1, $searches );
88 }
89
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
99 ),
100 'namespace' => array(
101 ApiBase::PARAM_DFLT => NS_MAIN,
102 ApiBase::PARAM_TYPE => 'namespace',
103 ApiBase::PARAM_ISMULTI => true
104 ),
105 'suggest' => false,
106 );
107 }
108
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',
115 );
116 }
117
118 public function getDescription() {
119 return 'Searches the wiki using the OpenSearch protocol';
120 }
121
122 protected function getExamples() {
123 return array(
124 'api.php?action=opensearch&search=Te'
125 );
126 }
127
128 public function getHelpUrls() {
129 return '';
130 }
131
132 public function getVersion() {
133 return __CLASS__ . ': $Id$';
134 }
135 }