mwdocgen: support multiple --file values
[lhc/web/wiklou.git] / includes / api / ApiOpenSearch.php
1 <?php
2 /**
3 * Created on Oct 13, 2006
4 *
5 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 */
24
25 /**
26 * @ingroup API
27 */
28 class ApiOpenSearch extends ApiBase {
29
30 /**
31 * Override built-in handling of format parameter.
32 * Only JSON is supported.
33 *
34 * @return ApiFormatBase
35 */
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 );
42 }
43 return $this->getMain()->createPrinterByName( $allowed[0] );
44 }
45
46 public function execute() {
47 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
48 $params = $this->extractRequestParams();
49 $search = $params['search'];
50 $limit = $params['limit'];
51 $namespaces = $params['namespace'];
52 $suggest = $params['suggest'];
53
54 // Some script that was loaded regardless of wgEnableOpenSearchSuggest, likely cached.
55 if ( $suggest && !$wgEnableOpenSearchSuggest ) {
56 $searches = array();
57 } else {
58 // Open search results may be stored for a very long time
59 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
60 $this->getMain()->setCacheMode( 'public' );
61
62 $searches = PrefixSearch::titleSearch( $search, $limit,
63 $namespaces );
64
65 // if the content language has variants, try to retrieve fallback results
66 $fallbackLimit = $limit - count( $searches );
67 if ( $fallbackLimit > 0 ) {
68 global $wgContLang;
69
70 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
71 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
72
73 foreach ( $fallbackSearches as $fbs ) {
74 $fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
75 $namespaces );
76 $searches = array_merge( $searches, $fallbackSearchResult );
77 $fallbackLimit -= count( $fallbackSearchResult );
78
79 if ( $fallbackLimit == 0 ) {
80 break;
81 }
82 }
83 }
84 }
85 // Set top level elements
86 $result = $this->getResult();
87 $result->addValue( null, 0, $search );
88 $result->addValue( null, 1, $searches );
89 }
90
91 public function getAllowedParams() {
92 return array(
93 'search' => null,
94 'limit' => array(
95 ApiBase::PARAM_DFLT => 10,
96 ApiBase::PARAM_TYPE => 'limit',
97 ApiBase::PARAM_MIN => 1,
98 ApiBase::PARAM_MAX => 100,
99 ApiBase::PARAM_MAX2 => 100
100 ),
101 'namespace' => array(
102 ApiBase::PARAM_DFLT => NS_MAIN,
103 ApiBase::PARAM_TYPE => 'namespace',
104 ApiBase::PARAM_ISMULTI => true
105 ),
106 'suggest' => false,
107 'format' => array(
108 ApiBase::PARAM_DFLT => 'json',
109 ApiBase::PARAM_TYPE => array( 'json', 'jsonfm' ),
110 )
111 );
112 }
113
114 public function getParamDescription() {
115 return array(
116 'search' => 'Search string',
117 'limit' => 'Maximum amount of results to return',
118 'namespace' => 'Namespaces to search',
119 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
120 'format' => 'The format of the output',
121 );
122 }
123
124 public function getDescription() {
125 return 'Search the wiki using the OpenSearch protocol';
126 }
127
128 public function getExamples() {
129 return array(
130 'api.php?action=opensearch&search=Te'
131 );
132 }
133
134 public function getHelpUrls() {
135 return 'https://www.mediawiki.org/wiki/API:Opensearch';
136 }
137 }