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