| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Module; |
| 4 |
|
| 5 |
use AppendIterator; |
| 6 |
use ArrayIterator; |
| 7 |
use Iterator; |
| 8 |
use MediaWiki\Rest\BasicAccess\BasicAuthorizerInterface; |
| 9 |
use MediaWiki\Rest\Handler\RedirectHandler; |
| 10 |
use MediaWiki\Rest\PathTemplateMatcher\ModuleConfigurationException; |
| 11 |
use MediaWiki\Rest\Reporter\ErrorReporter; |
| 12 |
use MediaWiki\Rest\ResponseFactory; |
| 13 |
use MediaWiki\Rest\RouteDefinitionException; |
| 14 |
use MediaWiki\Rest\Router; |
| 15 |
use MediaWiki\Rest\Validator\Validator; |
| 16 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
class ExtraRoutesModule extends MatcherBasedModule { |
| 63 |
|
| 64 |
|
| 65 |
private array $routeFiles; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private array $extraRoutes; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private ?array $routesFromFiles = null; |
| 77 |
|
| 78 |
|
| 79 |
private ?array $routeFileTimestamps = null; |
| 80 |
|
| 81 |
|
| 82 |
private ?string $configHash = null; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function __construct( |
| 93 |
array $routeFiles, |
| 94 |
array $extraRoutes, |
| 95 |
Router $router, |
| 96 |
ResponseFactory $responseFactory, |
| 97 |
BasicAuthorizerInterface $basicAuth, |
| 98 |
ObjectFactory $objectFactory, |
| 99 |
Validator $restValidator, |
| 100 |
ErrorReporter $errorReporter |
| 101 |
) { |
| 102 |
parent::__construct( |
| 103 |
$router, |
| 104 |
'', |
| 105 |
$responseFactory, |
| 106 |
$basicAuth, |
| 107 |
$objectFactory, |
| 108 |
$restValidator, |
| 109 |
$errorReporter |
| 110 |
); |
| 111 |
$this->routeFiles = $routeFiles; |
| 112 |
$this->extraRoutes = $extraRoutes; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
protected function getConfigHash(): string { |
| 121 |
if ( $this->configHash === null ) { |
| 122 |
$this->configHash = md5( json_encode( [ |
| 123 |
'class' => __CLASS__, |
| 124 |
'version' => 1, |
| 125 |
'extraRoutes' => $this->extraRoutes, |
| 126 |
'fileTimestamps' => $this->getRouteFileTimestamps() |
| 127 |
] ) ); |
| 128 |
} |
| 129 |
return $this->configHash; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
private function getRoutesFromFiles(): array { |
| 140 |
if ( $this->routesFromFiles !== null ) { |
| 141 |
return $this->routesFromFiles; |
| 142 |
} |
| 143 |
|
| 144 |
$this->routesFromFiles = []; |
| 145 |
$this->routeFileTimestamps = []; |
| 146 |
foreach ( $this->routeFiles as $fileName ) { |
| 147 |
$this->routeFileTimestamps[$fileName] = filemtime( $fileName ); |
| 148 |
|
| 149 |
$routes = $this->loadJsonFile( $fileName ); |
| 150 |
|
| 151 |
$this->routesFromFiles = array_merge( $this->routesFromFiles, $routes ); |
| 152 |
} |
| 153 |
|
| 154 |
return $this->routesFromFiles; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private function getRouteFileTimestamps(): array { |
| 163 |
if ( $this->routeFileTimestamps === null ) { |
| 164 |
$this->routeFileTimestamps = []; |
| 165 |
foreach ( $this->routeFiles as $fileName ) { |
| 166 |
$this->routeFileTimestamps[$fileName] = filemtime( $fileName ); |
| 167 |
} |
| 168 |
} |
| 169 |
return $this->routeFileTimestamps; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function getDefinedPaths(): array { |
| 176 |
$paths = []; |
| 177 |
foreach ( $this->getAllRoutes() as $spec ) { |
| 178 |
$key = $spec['path']; |
| 179 |
|
| 180 |
$methods = isset( $spec['method'] ) ? (array)$spec['method'] : [ 'GET' ]; |
| 181 |
|
| 182 |
$paths[$key] = array_merge( $paths[$key] ?? [], $methods ); |
| 183 |
} |
| 184 |
|
| 185 |
return $paths; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
private function getAllRoutes() { |
| 192 |
$iterator = new AppendIterator; |
| 193 |
$iterator->append( new ArrayIterator( $this->getRoutesFromFiles() ) ); |
| 194 |
$iterator->append( new ArrayIterator( $this->extraRoutes ) ); |
| 195 |
return $iterator; |
| 196 |
} |
| 197 |
|
| 198 |
protected function initRoutes(): void { |
| 199 |
$routeDefs = $this->getAllRoutes(); |
| 200 |
|
| 201 |
foreach ( $routeDefs as $route ) { |
| 202 |
if ( !isset( $route['path'] ) ) { |
| 203 |
throw new RouteDefinitionException( 'Missing path' ); |
| 204 |
} |
| 205 |
|
| 206 |
$path = $route['path']; |
| 207 |
$method = $route['method'] ?? 'GET'; |
| 208 |
$info = $this->makeRouteInfo( $route ); |
| 209 |
|
| 210 |
$this->addRoute( $method, $path, $info ); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
private function makeRouteInfo( array $route ): array { |
| 220 |
static $objectSpecKeys = [ |
| 221 |
'class', |
| 222 |
'factory', |
| 223 |
'services', |
| 224 |
'optional_services', |
| 225 |
'args', |
| 226 |
]; |
| 227 |
|
| 228 |
if ( isset( $route['redirect'] ) ) { |
| 229 |
|
| 230 |
$info = [ |
| 231 |
'spec' => [ 'class' => RedirectHandler::class ], |
| 232 |
'config' => $route, |
| 233 |
]; |
| 234 |
} else { |
| 235 |
|
| 236 |
$info = [ |
| 237 |
'spec' => array_intersect_key( $route, array_flip( $objectSpecKeys ) ), |
| 238 |
'config' => array_diff_key( $route, array_flip( $objectSpecKeys ) ), |
| 239 |
]; |
| 240 |
} |
| 241 |
|
| 242 |
$info['path'] = $route['path']; |
| 243 |
return $info; |
| 244 |
} |
| 245 |
|
| 246 |
public function getOpenApiInfo() { |
| 247 |
|
| 248 |
|
| 249 |
return [ |
| 250 |
'title' => 'Extra Routes', |
| 251 |
'description' => 'REST endpoints not associated with a module', |
| 252 |
'version' => 'undefined', |
| 253 |
]; |
| 254 |
} |
| 255 |
|
| 256 |
} |