Merge "(bug 44459) Ensure jqueryMsg treats plain messages as plain, with tests."
[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 /**
28 * @ingroup API
29 */
30 class ApiOpenSearch extends ApiBase {
31
32 public function getCustomPrinter() {
33 return $this->getMain()->createPrinterByName( 'json' );
34 }
35
36 public function execute() {
37 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
38 $params = $this->extractRequestParams();
39 $search = $params['search'];
40 $limit = $params['limit'];
41 $namespaces = $params['namespace'];
42 $suggest = $params['suggest'];
43
44 // Some script that was loaded regardless of wgEnableOpenSearchSuggest, likely cached.
45 if ( $suggest && !$wgEnableOpenSearchSuggest ) {
46 $searches = array();
47 } else {
48 // Open search results may be stored for a very long time
49 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
50 $this->getMain()->setCacheMode( 'public' );
51
52 $searches = PrefixSearch::titleSearch( $search, $limit,
53 $namespaces );
54
55 // if the content language has variants, try to retrieve fallback results
56 $fallbackLimit = $limit - count( $searches );
57 if ( $fallbackLimit > 0 ) {
58 global $wgContLang;
59
60 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
61 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
62
63 foreach ( $fallbackSearches as $fbs ) {
64 $fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
65 $namespaces );
66 $searches = array_merge( $searches, $fallbackSearchResult );
67 $fallbackLimit -= count( $fallbackSearchResult );
68
69 if ( $fallbackLimit == 0 ) {
70 break;
71 }
72 }
73 }
74 }
75 // Set top level elements
76 $result = $this->getResult();
77 $result->addValue( null, 0, $search );
78 $result->addValue( null, 1, $searches );
79 }
80
81 public function getAllowedParams() {
82 return array(
83 'search' => null,
84 'limit' => array(
85 ApiBase::PARAM_DFLT => 10,
86 ApiBase::PARAM_TYPE => 'limit',
87 ApiBase::PARAM_MIN => 1,
88 ApiBase::PARAM_MAX => 100,
89 ApiBase::PARAM_MAX2 => 100
90 ),
91 'namespace' => array(
92 ApiBase::PARAM_DFLT => NS_MAIN,
93 ApiBase::PARAM_TYPE => 'namespace',
94 ApiBase::PARAM_ISMULTI => true
95 ),
96 'suggest' => false,
97 );
98 }
99
100 public function getParamDescription() {
101 return array(
102 'search' => 'Search string',
103 'limit' => 'Maximum amount of results to return',
104 'namespace' => 'Namespaces to search',
105 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
106 );
107 }
108
109 public function getDescription() {
110 return 'Search the wiki using the OpenSearch protocol';
111 }
112
113 public function getExamples() {
114 return array(
115 'api.php?action=opensearch&search=Te'
116 );
117 }
118
119 public function getHelpUrls() {
120 return 'https://www.mediawiki.org/wiki/API:Opensearch';
121 }
122 }