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