| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Module; |
| 4 |
|
| 5 |
use MediaWiki\Rest\BasicAccess\BasicAuthorizerInterface; |
| 6 |
use MediaWiki\Rest\Handler\RedirectHandler; |
| 7 |
use MediaWiki\Rest\PathTemplateMatcher\ModuleConfigurationException; |
| 8 |
use MediaWiki\Rest\Reporter\ErrorReporter; |
| 9 |
use MediaWiki\Rest\ResponseFactory; |
| 10 |
use MediaWiki\Rest\RouteDefinitionException; |
| 11 |
use MediaWiki\Rest\Router; |
| 12 |
use MediaWiki\Rest\Validator\Validator; |
| 13 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class SpecBasedModule extends MatcherBasedModule { |
| 40 |
|
| 41 |
private string $definitionFile; |
| 42 |
|
| 43 |
private ?array $moduleDef = null; |
| 44 |
|
| 45 |
private ?int $routeFileTimestamp = null; |
| 46 |
|
| 47 |
private ?string $configHash = null; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct( |
| 53 |
string $definitionFile, |
| 54 |
Router $router, |
| 55 |
string $pathPrefix, |
| 56 |
ResponseFactory $responseFactory, |
| 57 |
BasicAuthorizerInterface $basicAuth, |
| 58 |
ObjectFactory $objectFactory, |
| 59 |
Validator $restValidator, |
| 60 |
ErrorReporter $errorReporter |
| 61 |
) { |
| 62 |
parent::__construct( |
| 63 |
$router, |
| 64 |
$pathPrefix, |
| 65 |
$responseFactory, |
| 66 |
$basicAuth, |
| 67 |
$objectFactory, |
| 68 |
$restValidator, |
| 69 |
$errorReporter |
| 70 |
); |
| 71 |
$this->definitionFile = $definitionFile; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
protected function getConfigHash(): string { |
| 80 |
if ( $this->configHash === null ) { |
| 81 |
$this->configHash = md5( json_encode( [ |
| 82 |
'class' => __CLASS__, |
| 83 |
'version' => 1, |
| 84 |
'fileTimestamps' => $this->getRouteFileTimestamp() |
| 85 |
] ) ); |
| 86 |
} |
| 87 |
return $this->configHash; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private function getModuleDefinition(): array { |
| 96 |
if ( $this->moduleDef !== null ) { |
| 97 |
return $this->moduleDef; |
| 98 |
} |
| 99 |
|
| 100 |
$this->routeFileTimestamp = filemtime( $this->definitionFile ); |
| 101 |
$moduleDef = $this->loadJsonFile( $this->definitionFile ); |
| 102 |
|
| 103 |
if ( !$moduleDef ) { |
| 104 |
throw new ModuleConfigurationException( |
| 105 |
'Malformed module definition file: ' . $this->definitionFile |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
if ( !isset( $moduleDef['mwapi'] ) ) { |
| 110 |
throw new ModuleConfigurationException( |
| 111 |
'Missing mwapi version field in ' . $this->definitionFile |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
if ( !version_compare( $moduleDef['mwapi'], '1.0.999', '<=' ) || |
| 117 |
!version_compare( $moduleDef['mwapi'], '1.0.0', '>=' ) |
| 118 |
) { |
| 119 |
throw new ModuleConfigurationException( |
| 120 |
"Unsupported openapi version {$moduleDef['mwapi']} in " |
| 121 |
. $this->definitionFile |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$this->moduleDef = $moduleDef; |
| 126 |
return $this->moduleDef; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private function getRouteFileTimestamp(): int { |
| 133 |
if ( $this->routeFileTimestamp === null ) { |
| 134 |
$this->routeFileTimestamp = filemtime( $this->definitionFile ); |
| 135 |
} |
| 136 |
return $this->routeFileTimestamp; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function getDefinedPaths(): array { |
| 145 |
$paths = []; |
| 146 |
$moduleDef = $this->getModuleDefinition(); |
| 147 |
|
| 148 |
foreach ( $moduleDef['paths'] as $path => $pSpec ) { |
| 149 |
$paths[$path] = []; |
| 150 |
foreach ( $pSpec as $method => $opSpec ) { |
| 151 |
$paths[$path][] = strtoupper( $method ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return $paths; |
| 156 |
} |
| 157 |
|
| 158 |
protected function initRoutes(): void { |
| 159 |
$moduleDef = $this->getModuleDefinition(); |
| 160 |
|
| 161 |
|
| 162 |
foreach ( $moduleDef['paths'] as $path => $pathSpec ) { |
| 163 |
foreach ( $pathSpec as $method => $opSpec ) { |
| 164 |
$info = $this->makeRouteInfo( $path, $opSpec ); |
| 165 |
$this->addRoute( $method, $path, $info ); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private function makeRouteInfo( string $path, array $opSpec ): array { |
| 176 |
static $objectSpecKeys = [ |
| 177 |
'class', |
| 178 |
'factory', |
| 179 |
'services', |
| 180 |
'optional_services', |
| 181 |
'args', |
| 182 |
]; |
| 183 |
|
| 184 |
static $oasKeys = [ |
| 185 |
'parameters', |
| 186 |
'responses', |
| 187 |
'summary', |
| 188 |
'description', |
| 189 |
'tags', |
| 190 |
'externalDocs', |
| 191 |
]; |
| 192 |
|
| 193 |
if ( isset( $opSpec['redirect'] ) ) { |
| 194 |
|
| 195 |
$opSpec['handler'] = [ |
| 196 |
'class' => RedirectHandler::class, |
| 197 |
'redirect' => $opSpec['redirect'], |
| 198 |
]; |
| 199 |
unset( $opSpec['redirect'] ); |
| 200 |
} |
| 201 |
|
| 202 |
$handlerSpec = $opSpec['handler'] ?? null; |
| 203 |
if ( !$handlerSpec ) { |
| 204 |
throw new RouteDefinitionException( 'Missing handler spec' ); |
| 205 |
} |
| 206 |
|
| 207 |
$info = [ |
| 208 |
'spec' => array_intersect_key( $handlerSpec, array_flip( $objectSpecKeys ) ), |
| 209 |
'config' => array_diff_key( $handlerSpec, array_flip( $objectSpecKeys ) ), |
| 210 |
'OAS' => array_intersect_key( $opSpec, array_flip( $oasKeys ) ), |
| 211 |
'path' => $path, |
| 212 |
]; |
| 213 |
|
| 214 |
return $info; |
| 215 |
} |
| 216 |
|
| 217 |
public function getOpenApiInfo() { |
| 218 |
$def = $this->getModuleDefinition(); |
| 219 |
return $def['info'] ?? []; |
| 220 |
} |
| 221 |
|
| 222 |
} |