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