Debian Codegraph @

includes/Rest/Handler/ModuleSpecHandler.php

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