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