Explicit variable definition, tweak documentation
[lhc/web/wiklou.git] / includes / api / ApiOpenSearch.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
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 $srchres = array();
56 } else {
57 // Open search results may be stored for a very long
58 // time
59 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
60 $this->getMain()->setCacheMode( 'public' );
61
62 $srchres = PrefixSearch::titleSearch( $search, $limit,
63 $namespaces );
64 }
65 // Set top level elements
66 $result = $this->getResult();
67 $result->addValue( null, 0, $search );
68 $result->addValue( null, 1, $srchres );
69 }
70
71 public function getAllowedParams() {
72 return array(
73 'search' => null,
74 'limit' => array(
75 ApiBase::PARAM_DFLT => 10,
76 ApiBase::PARAM_TYPE => 'limit',
77 ApiBase::PARAM_MIN => 1,
78 ApiBase::PARAM_MAX => 100,
79 ApiBase::PARAM_MAX2 => 100
80 ),
81 'namespace' => array(
82 ApiBase::PARAM_DFLT => NS_MAIN,
83 ApiBase::PARAM_TYPE => 'namespace',
84 ApiBase::PARAM_ISMULTI => true
85 ),
86 'suggest' => false,
87 );
88 }
89
90 public function getParamDescription() {
91 return array(
92 'search' => 'Search string',
93 'limit' => 'Maximum amount of results to return',
94 'namespace' => 'Namespaces to search',
95 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
96 );
97 }
98
99 public function getDescription() {
100 return 'This module implements OpenSearch protocol';
101 }
102
103 protected function getExamples() {
104 return array(
105 'api.php?action=opensearch&search=Te'
106 );
107 }
108
109 public function getVersion() {
110 return __CLASS__ . ': $Id$';
111 }
112 }