Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
78 / 84
75.00% covered (warning)
75.00%
9 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModuleSpecHandler
92.86% covered (success)
92.86%
78 / 84
75.00% covered (warning)
75.00%
9 / 12
21.16
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 run
78.95% covered (warning)
78.95%
15 / 19
0.00% covered (danger)
0.00%
0 / 1
4.15
 getInfoSpec
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 getLicenseSpec
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getContactSpec
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getServerSpec
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 getPathsSpec
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getRouteSpec
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getComponentsSpec
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getResponseBodySchemaFileName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 needsWriteAccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParamSettings
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Rest\Handler;
4
5use MediaWiki\Config\Config;
6use MediaWiki\Config\ServiceOptions;
7use MediaWiki\MainConfigNames;
8use MediaWiki\Rest\Handler;
9use MediaWiki\Rest\LocalizedHttpException;
10use MediaWiki\Rest\Module\Module;
11use MediaWiki\Rest\RequestData;
12use MediaWiki\Rest\ResponseFactory;
13use MediaWiki\Rest\SimpleHandler;
14use MediaWiki\Rest\Validator\Validator;
15use Wikimedia\Message\MessageValue;
16use Wikimedia\ParamValidator\ParamValidator;
17
18/**
19 * Core REST API endpoint that outputs an OpenAPI spec of a set of routes.
20 */
21class ModuleSpecHandler extends SimpleHandler {
22
23    public const MODULE_SPEC_PATH = '/coredev/v0/specs/module/{module}';
24
25    /**
26     * @internal
27     */
28    private const CONSTRUCTOR_OPTIONS = [
29        MainConfigNames::RightsUrl,
30        MainConfigNames::RightsText,
31        MainConfigNames::EmergencyContact,
32        MainConfigNames::Sitename,
33    ];
34
35    private ServiceOptions $options;
36
37    public function __construct( Config $config ) {
38        $options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
39        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
40        $this->options = $options;
41    }
42
43    /**
44     * @param string $moduleName
45     * @param string $version
46     */
47    public function run( $moduleName, $version = '' ): array {
48        // TODO: implement caching, get cache key from Router.
49
50        if ( $version !== '' ) {
51            $moduleName .= '/' . $version;
52        }
53
54        if ( $moduleName === '-' ) {
55            // Hack that allows us to fetch a spec for the empty module prefix
56            $moduleName = '';
57        }
58
59        $module = $this->getRouter()->getModule( $moduleName );
60
61        if ( !$module ) {
62            throw new LocalizedHttpException(
63                MessageValue::new( 'rest-unknown-module' )->params( $moduleName ),
64                404
65            );
66        }
67
68        $spec = [
69            'openapi' => '3.0.0',
70            'info' => $this->getInfoSpec( $module ),
71            'servers' => $this->getServerSpec( $module ),
72            'paths' => $this->getPathsSpec( $module ),
73            'components' => $this->getComponentsSpec( $module ),
74        ];
75
76        unset( $spec['info']['deprecationSettings'] );
77
78        return $spec;
79    }
80
81    /**
82     * @see https://spec.openapis.org/oas/v3.0.0#info-object
83     */
84    private function getInfoSpec( Module $module ): array {
85        // TODO: Let Modules provide their name, description, version, etc
86        $prefix = $module->getPathPrefix();
87
88        if ( $prefix === '' ) {
89            $title = $this->getJsonLocalizer()->getFormattedMessage( 'rest-default-module' );
90        } else {
91            $moduleStr = $this->getJsonLocalizer()->getFormattedMessage( 'rest-module' );
92            $title = "$prefix " . $moduleStr;
93        }
94
95        return $module->getOpenApiInfo() + [
96            'title' => $title,
97            'version' => 'undefined',
98            'license' => $this->getLicenseSpec(),
99            'contact' => $this->getContactSpec(),
100        ];
101    }
102
103    private function getLicenseSpec(): array {
104        // TODO: get terms-of-use URL, not content license.
105
106        return [
107            'name' => $this->options->get( MainConfigNames::RightsText ),
108            'url' => $this->options->get( MainConfigNames::RightsUrl ),
109        ];
110    }
111
112    private function getContactSpec(): array {
113        return [
114            'email' => $this->options->get( MainConfigNames::EmergencyContact ),
115        ];
116    }
117
118    private function getServerSpec( Module $module ): array {
119        $prefix = $module->getPathPrefix();
120
121        if ( $prefix !== '' ) {
122            $prefix = "/$prefix";
123        }
124
125        return [
126            [
127                'url' => $this->getRouter()->getRouteUrl( $prefix ),
128            ]
129        ];
130    }
131
132    private function getPathsSpec( Module $module ): array {
133        $specs = [];
134
135        // XXX: We currently don't support meta-data on OpenAPI path objects
136        //      (summary, description).
137
138        foreach ( $module->getDefinedPaths() as $path => $methods ) {
139            foreach ( $methods as $mth ) {
140                $key = strtolower( $mth );
141                $mth = strtoupper( $mth );
142                $specs[ $path ][ $key ] = $this->getRouteSpec( $module, $path, $mth );
143            }
144        }
145
146        return $specs;
147    }
148
149    private function getRouteSpec( Module $module, string $path, string $method ): array {
150        $request = new RequestData( [ 'method' => $method ] );
151        $handler = $module->getHandlerForPath( $path, $request, false );
152
153        $operationSpec = $handler->getOpenApiSpec( $method );
154
155        return $operationSpec;
156    }
157
158    private function getComponentsSpec( Module $module ): array {
159        $components = [];
160
161        // XXX: also collect reusable components from handler specs (but how to avoid name collisions?).
162        $componentsSources = [
163            [ 'schemas' => Validator::getParameterTypeSchemas() ],
164            ResponseFactory::getResponseComponents()
165        ];
166
167        // 2D merge
168        foreach ( $componentsSources as $cmps ) {
169            foreach ( $cmps as $name => $cmp ) {
170                $components[$name] = array_merge( $components[$name] ?? [], $cmp );
171            }
172        }
173
174        return $components;
175    }
176
177    protected function getResponseBodySchemaFileName( string $method ): ?string {
178        return 'includes/Rest/Handler/Schema/ModuleSpec.json';
179    }
180
181    /** @inheritDoc */
182    public function needsWriteAccess() {
183        return false;
184    }
185
186    /** @inheritDoc */
187    public function getParamSettings() {
188        return [
189            'module' => [
190                self::PARAM_SOURCE => 'path',
191                ParamValidator::PARAM_TYPE => 'string',
192                ParamValidator::PARAM_REQUIRED => true,
193                Handler::PARAM_DESCRIPTION => new MessageValue( 'rest-param-desc-module-spec-module' ),
194            ],
195            'version' => [
196                self::PARAM_SOURCE => 'path',
197                ParamValidator::PARAM_TYPE => 'string',
198                ParamValidator::PARAM_DEFAULT => '',
199                Handler::PARAM_DESCRIPTION => new MessageValue( 'rest-param-desc-module-spec-version' ),
200            ],
201        ];
202    }
203
204}