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