r75621 copy-paste fail
[lhc/web/wiklou.git] / includes / api / ApiRsd.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.17+
5 *
6 * Created on October 26, 2010
7 *
8 * Copyright © 2010 Bryan Tong Minh and Brion Vibber
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 */
27
28 if ( !defined( 'MEDIAWIKI' ) ) {
29 require_once( 'ApiBase.php' );
30 }
31
32 /**
33 * API module for sending out RSD information
34 * @ingroup API
35 */
36 class ApiRsd extends ApiBase {
37
38 public function __construct( $main, $action ) {
39 parent::__construct( $main, $action );
40 }
41
42 public function execute() {
43 $result = $this->getResult();
44
45 $result->addValue( null, 'version', '1.0' );
46 $result->addValue( null, 'xmlns', 'http://archipelago.phrasewise.com/rsd' );
47
48 $service = array(
49 'engineName' => array(
50 '*' => 'MediaWiki'
51 ),
52 'engineLink' => array(
53 '*' => 'http://www.mediawiki.org/'
54 ),
55 'apis' => $this->formatRsdApiList()
56 );
57
58 $result->setIndexedTagName( $service['apis'], 'api' );
59
60 $result->addValue( null, 'service', $service );
61 }
62
63 public function getCustomPrinter() {
64 return new ApiFormatXmlRsd( $this->getMain(), 'xml' );
65 }
66
67 public function getAllowedParams() {
68 return array();
69 }
70
71 public function getParamDescription() {
72 return array();
73 }
74
75 public function getDescription() {
76 return 'Export an RSD schema';
77 }
78
79 protected function getExamples() {
80 return array(
81 'api.php?action=rsd'
82 );
83 }
84
85 /**
86 * Builds an internal list of APIs to expose information about.
87 * Normally this only lists the MediaWiki API, with its base URL,
88 * link to documentation, and a marker as to available authentication
89 * (to aid in OAuth client apps switching to support in the future).
90 *
91 * Extensions can expose other APIs, such as WordPress or Twitter-
92 * compatible APIs, by hooking 'ApiRsdServiceApis' and adding more
93 * elements to the array.
94 *
95 * See http://cyber.law.harvard.edu/blogs/gems/tech/rsd.html for
96 * the base RSD spec, and check WordPress and StatusNet sites for
97 * in-production examples listing several blogging and micrblogging
98 * APIs.
99 *
100 * @return array
101 */
102 protected function getRsdApiList() {
103 $apis = array(
104 'MediaWiki' => array(
105 // The API link is required for all RSD API entries.
106 'apiLink' => wfExpandUrl( wfScript( 'api' ) ),
107
108 // Docs link is optional, but recommended.
109 'docs' => 'http://mediawiki.org/wiki/API',
110
111 // Some APIs may need a blog ID, but it may be left blank.
112 'blogID' => '',
113
114 // Additional settings are optional.
115 'settings' => array(
116 // Change this to true in the future as an aid to
117 // machine discovery of OAuth for API access.
118 'OAuth' => false,
119 )
120 ),
121 );
122 wfRunHooks( 'ApiRsdServiceApis', array( &$apis ) );
123 return $apis;
124 }
125
126 /**
127 * Formats the internal list of exposed APIs into an array suitable
128 * to pass to the API's XML formatter.
129 *
130 * @return array
131 */
132 protected function formatRsdApiList() {
133 $apis = $this->getRsdApiList();
134
135 $outputData = array();
136 foreach ( $apis as $name => $info ) {
137 $data = array(
138 'name' => $name,
139 'preferred' => wfBoolToStr( $name == 'MediaWiki' ),
140 'apiLink' => $info['apiLink'],
141 'blogID' => isset( $info['blogID'] ) ? $info['blogID'] : ''
142 );
143 if ( isset( $info['docs'] ) ) {
144 $data['settings']['docs'] = array(
145 '*' => $info['docs'],
146 );
147 }
148 if ( isset( $info['settings'] ) ) {
149 foreach ( $info['settings'] as $setting => $val ) {
150 if ( is_bool( $val ) ) {
151 $xmlVal = wfBoolToStr( $val );
152 } else {
153 $xmlVal = $val;
154 }
155 $data['settings'][] = array(
156 'name' => $setting,
157 '*' => $xmlVal,
158 );
159 }
160 }
161 if ( isset( $data['settings'] ) ) {
162 $data['settings']['_element'] = 'setting';
163 }
164 $outputData[] = $data;
165 }
166 return $outputData;
167 }
168
169 public function getVersion() {
170 return __CLASS__ . ': $Id$';
171 }
172 }
173
174 class ApiFormatXmlRsd extends ApiFormatXml {
175 public function __construct( $main, $format ) {
176 parent::__construct( $main, $format );
177 $this->setRootElement( 'rsd' );
178 }
179
180 public function getMimeType() {
181 return 'application/rsd+xml';
182 }
183
184 public function getVersion() {
185 return __CLASS__ . ': $Id$';
186 }
187 }