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