Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiRsd
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 7
182
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getCustomPrinter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 isReadMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRsdApiList
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 formatRsdApiList
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
56
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * API for MediaWiki 1.17+
5 *
6 * Copyright © 2010 Bryan Tong Minh and Brooke Vibber
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26use MediaWiki\MediaWikiServices;
27use MediaWiki\Title\Title;
28
29/**
30 * API module for sending out RSD information
31 * @ingroup API
32 */
33class ApiRsd extends ApiBase {
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        // Loaded here rather than injected due to the direct extension of ApiBase.
87        $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
88        $apis = [
89            'MediaWiki' => [
90                // The API link is required for all RSD API entries.
91                'apiLink' => (string)$urlUtils->expand( wfScript( 'api' ), PROTO_CURRENT ),
92
93                // Docs link is optional, but recommended.
94                'docs' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/API',
95
96                // Some APIs may need a blog ID, but it may be left blank.
97                'blogID' => '',
98
99                // Additional settings are optional.
100                'settings' => [
101                    // Change this to true in the future as an aid to
102                    // machine discovery of OAuth for API access.
103                    'OAuth' => false,
104                ]
105            ],
106        ];
107        $this->getHookRunner()->onApiRsdServiceApis( $apis );
108
109        return $apis;
110    }
111
112    /**
113     * Formats the internal list of exposed APIs into an array suitable
114     * to pass to the API's XML formatter.
115     *
116     * @return array
117     */
118    protected function formatRsdApiList() {
119        $apis = $this->getRsdApiList();
120
121        $outputData = [];
122        foreach ( $apis as $name => $info ) {
123            $data = [
124                'name' => $name,
125                'preferred' => wfBoolToStr( $name == 'MediaWiki' ),
126                'apiLink' => $info['apiLink'],
127                'blogID' => $info['blogID'] ?? '',
128            ];
129            $settings = [];
130            if ( isset( $info['docs'] ) ) {
131                $settings['docs'] = $info['docs'];
132                ApiResult::setSubelementsList( $settings, 'docs' );
133            }
134            if ( isset( $info['settings'] ) ) {
135                foreach ( $info['settings'] as $setting => $val ) {
136                    if ( is_bool( $val ) ) {
137                        $xmlVal = wfBoolToStr( $val );
138                    } else {
139                        $xmlVal = $val;
140                    }
141                    $setting = [ 'name' => $setting ];
142                    ApiResult::setContentValue( $setting, 'value', $xmlVal );
143                    $settings[] = $setting;
144                }
145            }
146            if ( count( $settings ) ) {
147                ApiResult::setIndexedTagName( $settings, 'setting' );
148                $data['settings'] = $settings;
149            }
150            $outputData[] = $data;
151        }
152
153        return $outputData;
154    }
155
156    public function getHelpUrls() {
157        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Rsd';
158    }
159}