Merge "Return $wgSitename as displayname for local repo in filerepoinfo API"
[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
44 return $this->getMain()->createPrinterByName( $allowed[0] );
45 }
46
47 public function execute() {
48 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
49 $params = $this->extractRequestParams();
50 $search = $params['search'];
51 $limit = $params['limit'];
52 $namespaces = $params['namespace'];
53 $suggest = $params['suggest'];
54
55 // Some script that was loaded regardless of wgEnableOpenSearchSuggest, likely cached.
56 if ( $suggest && !$wgEnableOpenSearchSuggest ) {
57 $searches = array();
58 } else {
59 // Open search results may be stored for a very long time
60 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
61 $this->getMain()->setCacheMode( 'public' );
62
63 $searcher = new StringPrefixSearch;
64 $searches = $searcher->searchWithVariants( $search, $limit, $namespaces );
65 }
66 // Set top level elements
67 $result = $this->getResult();
68 $result->addValue( null, 0, $search );
69 $result->addValue( null, 1, $searches );
70 }
71
72 public function getAllowedParams() {
73 global $wgOpenSearchDefaultLimit;
74
75 return array(
76 'search' => null,
77 'limit' => array(
78 ApiBase::PARAM_DFLT => $wgOpenSearchDefaultLimit,
79 ApiBase::PARAM_TYPE => 'limit',
80 ApiBase::PARAM_MIN => 1,
81 ApiBase::PARAM_MAX => 100,
82 ApiBase::PARAM_MAX2 => 100
83 ),
84 'namespace' => array(
85 ApiBase::PARAM_DFLT => NS_MAIN,
86 ApiBase::PARAM_TYPE => 'namespace',
87 ApiBase::PARAM_ISMULTI => true
88 ),
89 'suggest' => false,
90 'format' => array(
91 ApiBase::PARAM_DFLT => 'json',
92 ApiBase::PARAM_TYPE => array( 'json', 'jsonfm' ),
93 )
94 );
95 }
96
97 public function getParamDescription() {
98 return array(
99 'search' => 'Search string',
100 'limit' => 'Maximum amount of results to return',
101 'namespace' => 'Namespaces to search',
102 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
103 'format' => 'The format of the output',
104 );
105 }
106
107 public function getDescription() {
108 return 'Search the wiki using the OpenSearch protocol.';
109 }
110
111 public function getExamples() {
112 return array(
113 'api.php?action=opensearch&search=Te'
114 );
115 }
116
117 public function getHelpUrls() {
118 return 'https://www.mediawiki.org/wiki/API:Opensearch';
119 }
120 }