put in r110285 again now that 1.19 branched
[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 /**
29 * API module for sending out RSD information
30 * @ingroup API
31 */
32 class ApiRsd extends ApiBase {
33
34 public function __construct( $main, $action ) {
35 parent::__construct( $main, $action );
36 }
37
38 public function execute() {
39 $result = $this->getResult();
40
41 $result->addValue( null, 'version', '1.0' );
42 $result->addValue( null, 'xmlns', 'http://archipelago.phrasewise.com/rsd' );
43
44 $service = array( 'apis' => $this->formatRsdApiList() );
45 ApiResult::setContent( $service, 'MediaWiki', 'engineName' );
46 ApiResult::setContent( $service, 'https://www.mediawiki.org/', 'engineLink' );
47 ApiResult::setContent( $service, Title::newMainPage()->getCanonicalUrl(), 'homePageLink' );
48
49 $result->setIndexedTagName( $service['apis'], 'api' );
50
51 $result->addValue( null, 'service', $service );
52 }
53
54 public function getCustomPrinter() {
55 return new ApiFormatXmlRsd( $this->getMain(), 'xml' );
56 }
57
58 public function getAllowedParams() {
59 return array();
60 }
61
62 public function getParamDescription() {
63 return array();
64 }
65
66 public function getDescription() {
67 return 'Export an RSD (Really Simple Discovery) schema';
68 }
69
70 public function getExamples() {
71 return array(
72 'api.php?action=rsd'
73 );
74 }
75
76 /**
77 * Builds an internal list of APIs to expose information about.
78 * Normally this only lists the MediaWiki API, with its base URL,
79 * link to documentation, and a marker as to available authentication
80 * (to aid in OAuth client apps switching to support in the future).
81 *
82 * Extensions can expose other APIs, such as WordPress or Twitter-
83 * compatible APIs, by hooking 'ApiRsdServiceApis' and adding more
84 * elements to the array.
85 *
86 * See http://cyber.law.harvard.edu/blogs/gems/tech/rsd.html for
87 * the base RSD spec, and check WordPress and StatusNet sites for
88 * in-production examples listing several blogging and micrblogging
89 * APIs.
90 *
91 * @return array
92 */
93 protected function getRsdApiList() {
94 $apis = array(
95 'MediaWiki' => array(
96 // The API link is required for all RSD API entries.
97 'apiLink' => wfExpandUrl( wfScript( 'api' ), PROTO_CURRENT ),
98
99 // Docs link is optional, but recommended.
100 'docs' => 'https://www.mediawiki.org/wiki/API',
101
102 // Some APIs may need a blog ID, but it may be left blank.
103 'blogID' => '',
104
105 // Additional settings are optional.
106 'settings' => array(
107 // Change this to true in the future as an aid to
108 // machine discovery of OAuth for API access.
109 'OAuth' => false,
110 )
111 ),
112 );
113 wfRunHooks( 'ApiRsdServiceApis', array( &$apis ) );
114 return $apis;
115 }
116
117 /**
118 * Formats the internal list of exposed APIs into an array suitable
119 * to pass to the API's XML formatter.
120 *
121 * @return array
122 */
123 protected function formatRsdApiList() {
124 $apis = $this->getRsdApiList();
125
126 $outputData = array();
127 foreach ( $apis as $name => $info ) {
128 $data = array(
129 'name' => $name,
130 'preferred' => wfBoolToStr( $name == 'MediaWiki' ),
131 'apiLink' => $info['apiLink'],
132 'blogID' => isset( $info['blogID'] ) ? $info['blogID'] : '',
133 );
134 $settings = array();
135 if ( isset( $info['docs'] ) ) {
136 ApiResult::setContent( $settings, $info['docs'], 'docs' );
137 }
138 if ( isset( $info['settings'] ) ) {
139 foreach ( $info['settings'] as $setting => $val ) {
140 if ( is_bool( $val ) ) {
141 $xmlVal = wfBoolToStr( $val );
142 } else {
143 $xmlVal = $val;
144 }
145 $setting = array( 'name' => $setting );
146 ApiResult::setContent( $setting, $xmlVal );
147 $settings[] = $setting;
148 }
149 }
150 if ( count( $settings ) ) {
151 $this->getResult()->setIndexedTagName( $settings, 'setting' );
152 $data['settings'] = $settings;
153 }
154 $outputData[] = $data;
155 }
156 return $outputData;
157 }
158
159 public function getVersion() {
160 return __CLASS__ . ': $Id$';
161 }
162 }
163
164 class ApiFormatXmlRsd extends ApiFormatXml {
165 public function __construct( $main, $format ) {
166 parent::__construct( $main, $format );
167 $this->setRootElement( 'rsd' );
168 }
169
170 public function getMimeType() {
171 return 'application/rsd+xml';
172 }
173
174 public function getVersion() {
175 return __CLASS__ . ': $Id$';
176 }
177 }