| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest; |
| 4 |
|
| 5 |
use HttpStatus; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\HookContainer\HookContainer; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\MainConfigSchema; |
| 10 |
use MediaWiki\Permissions\Authority; |
| 11 |
use MediaWiki\Rest\BasicAccess\BasicAuthorizerInterface; |
| 12 |
use MediaWiki\Rest\Module\ExtraRoutesModule; |
| 13 |
use MediaWiki\Rest\Module\Module; |
| 14 |
use MediaWiki\Rest\Module\SpecBasedModule; |
| 15 |
use MediaWiki\Rest\PathTemplateMatcher\ModuleConfigurationException; |
| 16 |
use MediaWiki\Rest\Reporter\ErrorReporter; |
| 17 |
use MediaWiki\Rest\Validator\Validator; |
| 18 |
use MediaWiki\Session\Session; |
| 19 |
use Throwable; |
| 20 |
use Wikimedia\Message\MessageValue; |
| 21 |
use Wikimedia\ObjectCache\BagOStuff; |
| 22 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 23 |
use Wikimedia\Stats\StatsFactory; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class Router { |
| 31 |
private const PREFIX_PATTERN = '!^/([-_.\w]+(?:/v\d+)?)(/.*)$!'; |
| 32 |
|
| 33 |
|
| 34 |
private $routeFiles; |
| 35 |
|
| 36 |
|
| 37 |
private $extraRoutes; |
| 38 |
|
| 39 |
|
| 40 |
private $moduleMap = null; |
| 41 |
|
| 42 |
|
| 43 |
private $modules = []; |
| 44 |
|
| 45 |
|
| 46 |
private $moduleFileTimestamps = null; |
| 47 |
|
| 48 |
|
| 49 |
private $baseUrl; |
| 50 |
|
| 51 |
|
| 52 |
private $privateBaseUrl; |
| 53 |
|
| 54 |
|
| 55 |
private $rootPath; |
| 56 |
|
| 57 |
|
| 58 |
private $scriptPath; |
| 59 |
|
| 60 |
|
| 61 |
private $configHash = null; |
| 62 |
|
| 63 |
|
| 64 |
private $cors; |
| 65 |
|
| 66 |
private BagOStuff $cacheBag; |
| 67 |
private ResponseFactory $responseFactory; |
| 68 |
private BasicAuthorizerInterface $basicAuth; |
| 69 |
private Authority $authority; |
| 70 |
private ObjectFactory $objectFactory; |
| 71 |
private Validator $restValidator; |
| 72 |
private ErrorReporter $errorReporter; |
| 73 |
private HookContainer $hookContainer; |
| 74 |
private Session $session; |
| 75 |
|
| 76 |
|
| 77 |
private $stats = null; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public const CONSTRUCTOR_OPTIONS = [ |
| 83 |
MainConfigNames::CanonicalServer, |
| 84 |
MainConfigNames::InternalServer, |
| 85 |
MainConfigNames::RestPath, |
| 86 |
MainConfigNames::ScriptPath, |
| 87 |
]; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function __construct( |
| 105 |
array $routeFiles, |
| 106 |
array $extraRoutes, |
| 107 |
ServiceOptions $options, |
| 108 |
BagOStuff $cacheBag, |
| 109 |
ResponseFactory $responseFactory, |
| 110 |
BasicAuthorizerInterface $basicAuth, |
| 111 |
Authority $authority, |
| 112 |
ObjectFactory $objectFactory, |
| 113 |
Validator $restValidator, |
| 114 |
ErrorReporter $errorReporter, |
| 115 |
HookContainer $hookContainer, |
| 116 |
Session $session |
| 117 |
) { |
| 118 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 119 |
|
| 120 |
$this->routeFiles = $routeFiles; |
| 121 |
$this->extraRoutes = $extraRoutes; |
| 122 |
$this->baseUrl = $options->get( MainConfigNames::CanonicalServer ); |
| 123 |
$this->privateBaseUrl = $options->get( MainConfigNames::InternalServer ); |
| 124 |
$this->rootPath = $options->get( MainConfigNames::RestPath ); |
| 125 |
$this->scriptPath = $options->get( MainConfigNames::ScriptPath ); |
| 126 |
$this->cacheBag = $cacheBag; |
| 127 |
$this->responseFactory = $responseFactory; |
| 128 |
$this->basicAuth = $basicAuth; |
| 129 |
$this->authority = $authority; |
| 130 |
$this->objectFactory = $objectFactory; |
| 131 |
$this->restValidator = $restValidator; |
| 132 |
$this->errorReporter = $errorReporter; |
| 133 |
$this->hookContainer = $hookContainer; |
| 134 |
$this->session = $session; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private function getRelativePath( $path ) { |
| 149 |
$allowed = [ |
| 150 |
$this->rootPath, |
| 151 |
MainConfigSchema::getDefaultRestPath( $this->scriptPath ) |
| 152 |
]; |
| 153 |
|
| 154 |
foreach ( $allowed as $prefix ) { |
| 155 |
if ( str_starts_with( $path, $prefix ) ) { |
| 156 |
return substr( $path, strlen( $prefix ) ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private function splitPath( string $fullPath ): array { |
| 169 |
$pathWithModule = $this->getRelativePath( $fullPath ); |
| 170 |
|
| 171 |
if ( $pathWithModule === false ) { |
| 172 |
throw new LocalizedHttpException( |
| 173 |
( new MessageValue( 'rest-prefix-mismatch' ) ) |
| 174 |
->plaintextParams( $fullPath, $this->rootPath ), |
| 175 |
404 |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
if ( preg_match( self::PREFIX_PATTERN, $pathWithModule, $matches ) ) { |
| 180 |
[ , $module, $pathUnderModule ] = $matches; |
| 181 |
} else { |
| 182 |
|
| 183 |
$module = ''; |
| 184 |
$pathUnderModule = $pathWithModule; |
| 185 |
} |
| 186 |
|
| 187 |
if ( $module !== '' && !$this->getModuleInfo( $module ) ) { |
| 188 |
|
| 189 |
|
| 190 |
$module = ''; |
| 191 |
$pathUnderModule = $pathWithModule; |
| 192 |
} |
| 193 |
|
| 194 |
return [ $module, $pathUnderModule ]; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
private function fetchCachedModuleMap(): ?array { |
| 203 |
$moduleMapCacheKey = $this->getModuleMapCacheKey(); |
| 204 |
$cacheData = $this->cacheBag->get( $moduleMapCacheKey ); |
| 205 |
if ( $cacheData && $cacheData[Module::CACHE_CONFIG_HASH_KEY] === $this->getModuleMapHash() ) { |
| 206 |
unset( $cacheData[Module::CACHE_CONFIG_HASH_KEY] ); |
| 207 |
return $cacheData; |
| 208 |
} else { |
| 209 |
return null; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
private function fetchCachedModuleData( string $module ): ?array { |
| 214 |
$moduleDataCacheKey = $this->getModuleDataCacheKey( $module ); |
| 215 |
$cacheData = $this->cacheBag->get( $moduleDataCacheKey ); |
| 216 |
return $cacheData ?: null; |
| 217 |
} |
| 218 |
|
| 219 |
private function cacheModuleMap( array $map ) { |
| 220 |
$map[Module::CACHE_CONFIG_HASH_KEY] = $this->getModuleMapHash(); |
| 221 |
$moduleMapCacheKey = $this->getModuleMapCacheKey(); |
| 222 |
$this->cacheBag->set( $moduleMapCacheKey, $map ); |
| 223 |
} |
| 224 |
|
| 225 |
private function cacheModuleData( string $module, array $map ) { |
| 226 |
$moduleDataCacheKey = $this->getModuleDataCacheKey( $module ); |
| 227 |
$this->cacheBag->set( $moduleDataCacheKey, $map ); |
| 228 |
} |
| 229 |
|
| 230 |
private function getModuleDataCacheKey( string $module ): string { |
| 231 |
if ( $module === '' ) { |
| 232 |
|
| 233 |
$module = '-'; |
| 234 |
} |
| 235 |
return $this->cacheBag->makeKey( __CLASS__, 'module', $module ); |
| 236 |
} |
| 237 |
|
| 238 |
private function getModuleMapCacheKey(): string { |
| 239 |
return $this->cacheBag->makeKey( __CLASS__, 'map', '1' ); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private function getModuleMapHash(): string { |
| 246 |
if ( $this->configHash === null ) { |
| 247 |
$this->configHash = md5( json_encode( [ |
| 248 |
$this->extraRoutes, |
| 249 |
$this->getModuleFileTimestamps() |
| 250 |
] ) ); |
| 251 |
} |
| 252 |
return $this->configHash; |
| 253 |
} |
| 254 |
|
| 255 |
private function buildModuleMap(): array { |
| 256 |
$modules = []; |
| 257 |
$noPrefixFiles = []; |
| 258 |
$id = ''; |
| 259 |
|
| 260 |
foreach ( $this->routeFiles as $file ) { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
$spec = Module::loadJsonFile( $file ); |
| 265 |
|
| 266 |
if ( isset( $spec['mwapi'] ) || isset( $spec['moduleId'] ) || isset( $spec['routes'] ) ) { |
| 267 |
|
| 268 |
if ( !isset( $spec['moduleId'] ) ) { |
| 269 |
throw new ModuleConfigurationException( |
| 270 |
"Missing 'moduleId' field in $file" |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
$id = $spec['moduleId']; |
| 275 |
|
| 276 |
$moduleInfo = [ |
| 277 |
'class' => SpecBasedModule::class, |
| 278 |
'pathPrefix' => $id, |
| 279 |
'specFile' => $file |
| 280 |
]; |
| 281 |
} else { |
| 282 |
|
| 283 |
$noPrefixFiles[] = $file; |
| 284 |
$moduleInfo = null; |
| 285 |
} |
| 286 |
|
| 287 |
if ( $moduleInfo ) { |
| 288 |
if ( isset( $modules[$id] ) ) { |
| 289 |
$otherFiles = implode( ' and ', $modules[$id]['routeFiles'] ); |
| 290 |
throw new ModuleConfigurationException( |
| 291 |
"Duplicate module $id in $file, also used in $otherFiles" |
| 292 |
); |
| 293 |
} |
| 294 |
|
| 295 |
$modules[$id] = $moduleInfo; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if ( $noPrefixFiles || $this->extraRoutes ) { |
| 303 |
$modules[''] = [ |
| 304 |
'class' => ExtraRoutesModule::class, |
| 305 |
'pathPrefix' => '', |
| 306 |
'routeFiles' => $noPrefixFiles, |
| 307 |
'extraRoutes' => $this->extraRoutes, |
| 308 |
]; |
| 309 |
} |
| 310 |
|
| 311 |
return $modules; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private function getModuleFileTimestamps() { |
| 320 |
if ( $this->moduleFileTimestamps === null ) { |
| 321 |
$this->moduleFileTimestamps = []; |
| 322 |
foreach ( $this->routeFiles as $fileName ) { |
| 323 |
$this->moduleFileTimestamps[$fileName] = filemtime( $fileName ); |
| 324 |
} |
| 325 |
} |
| 326 |
return $this->moduleFileTimestamps; |
| 327 |
} |
| 328 |
|
| 329 |
private function getModuleMap(): array { |
| 330 |
if ( !$this->moduleMap ) { |
| 331 |
$map = $this->fetchCachedModuleMap(); |
| 332 |
|
| 333 |
if ( !$map ) { |
| 334 |
$map = $this->buildModuleMap(); |
| 335 |
$this->cacheModuleMap( $map ); |
| 336 |
} |
| 337 |
|
| 338 |
$this->moduleMap = $map; |
| 339 |
} |
| 340 |
return $this->moduleMap; |
| 341 |
} |
| 342 |
|
| 343 |
private function getModuleInfo( $module ): ?array { |
| 344 |
$map = $this->getModuleMap(); |
| 345 |
return $map[$module] ?? null; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public function getModuleIds(): array { |
| 352 |
return array_keys( $this->getModuleMap() ); |
| 353 |
} |
| 354 |
|
| 355 |
public function getModuleForPath( string $fullPath ): ?Module { |
| 356 |
[ $moduleName, ] = $this->splitPath( $fullPath ); |
| 357 |
return $this->getModule( $moduleName ); |
| 358 |
} |
| 359 |
|
| 360 |
public function getModule( string $name ): ?Module { |
| 361 |
if ( isset( $this->modules[$name] ) ) { |
| 362 |
return $this->modules[$name]; |
| 363 |
} |
| 364 |
|
| 365 |
$info = $this->getModuleInfo( $name ); |
| 366 |
|
| 367 |
if ( !$info ) { |
| 368 |
return null; |
| 369 |
} |
| 370 |
|
| 371 |
$module = $this->instantiateModule( $info, $name ); |
| 372 |
|
| 373 |
$cacheData = $this->fetchCachedModuleData( $name ); |
| 374 |
|
| 375 |
if ( $cacheData !== null ) { |
| 376 |
$cacheOk = $module->initFromCacheData( $cacheData ); |
| 377 |
} else { |
| 378 |
$cacheOk = false; |
| 379 |
} |
| 380 |
|
| 381 |
if ( !$cacheOk ) { |
| 382 |
$cacheData = $module->getCacheData(); |
| 383 |
$this->cacheModuleData( $name, $cacheData ); |
| 384 |
} |
| 385 |
|
| 386 |
if ( $this->cors ) { |
| 387 |
$module->setCors( $this->cors ); |
| 388 |
} |
| 389 |
|
| 390 |
if ( $this->stats ) { |
| 391 |
$module->setStats( $this->stats ); |
| 392 |
} |
| 393 |
|
| 394 |
$this->modules[$name] = $module; |
| 395 |
return $module; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public function getRoutePath( |
| 402 |
string $routeWithModulePrefix, |
| 403 |
array $pathParams = [], |
| 404 |
array $queryParams = [] |
| 405 |
): string { |
| 406 |
$routeWithModulePrefix = $this->substPathParams( $routeWithModulePrefix, $pathParams ); |
| 407 |
$path = $this->rootPath . $routeWithModulePrefix; |
| 408 |
return wfAppendQuery( $path, $queryParams ); |
| 409 |
} |
| 410 |
|
| 411 |
public function getRouteUrl( |
| 412 |
string $routeWithModulePrefix, |
| 413 |
array $pathParams = [], |
| 414 |
array $queryParams = [] |
| 415 |
): string { |
| 416 |
return $this->baseUrl . $this->getRoutePath( $routeWithModulePrefix, $pathParams, $queryParams ); |
| 417 |
} |
| 418 |
|
| 419 |
public function getPrivateRouteUrl( |
| 420 |
string $routeWithModulePrefix, |
| 421 |
array $pathParams = [], |
| 422 |
array $queryParams = [] |
| 423 |
): string { |
| 424 |
return $this->privateBaseUrl . $this->getRoutePath( $routeWithModulePrefix, $pathParams, $queryParams ); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
protected function substPathParams( string $route, array $pathParams ): string { |
| 434 |
foreach ( $pathParams as $param => $value ) { |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
$route = str_replace( '{' . $param . '}', rawurlencode( (string)$value ), $route ); |
| 439 |
} |
| 440 |
return $route; |
| 441 |
} |
| 442 |
|
| 443 |
public function execute( RequestInterface $request ): ResponseInterface { |
| 444 |
try { |
| 445 |
$fullPath = $request->getUri()->getPath(); |
| 446 |
$response = $this->doExecute( $fullPath, $request ); |
| 447 |
} catch ( HttpException $e ) { |
| 448 |
$extraData = []; |
| 449 |
if ( $this->isRestbaseCompatEnabled( $request ) |
| 450 |
&& $e instanceof LocalizedHttpException |
| 451 |
) { |
| 452 |
$extraData = $this->getRestbaseCompatErrorData( $request, $e ); |
| 453 |
} |
| 454 |
$response = $this->responseFactory->createFromException( $e, $extraData ); |
| 455 |
} catch ( Throwable $e ) { |
| 456 |
$this->errorReporter->reportError( $e, null, $request ); |
| 457 |
$response = $this->responseFactory->createFromException( $e ); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
$this->varyOnRestbaseCompat( $response ); |
| 463 |
|
| 464 |
return $response; |
| 465 |
} |
| 466 |
|
| 467 |
private function doExecute( string $fullPath, RequestInterface $request ): ResponseInterface { |
| 468 |
[ $modulePrefix, $path ] = $this->splitPath( $fullPath ); |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
if ( $modulePrefix === '' && $path === '' ) { |
| 473 |
$target = $this->getRoutePath( '/' ); |
| 474 |
return $this->responseFactory->createRedirect( $target, 308 ); |
| 475 |
} |
| 476 |
|
| 477 |
$module = $this->getModule( $modulePrefix ); |
| 478 |
|
| 479 |
if ( !$module ) { |
| 480 |
throw new LocalizedHttpException( |
| 481 |
MessageValue::new( 'rest-unknown-module' )->plaintextParams( $modulePrefix ), |
| 482 |
404, |
| 483 |
[ 'prefix' => $modulePrefix ] |
| 484 |
); |
| 485 |
} |
| 486 |
|
| 487 |
return $module->execute( $path, $request ); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
public function prepareHandler( Handler $handler ) { |
| 497 |
|
| 498 |
|
| 499 |
$handler->initServices( |
| 500 |
$this->authority, |
| 501 |
$this->responseFactory, |
| 502 |
$this->hookContainer |
| 503 |
); |
| 504 |
|
| 505 |
$handler->initSession( $this->session ); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public function setCors( CorsUtils $cors ): self { |
| 513 |
$this->cors = $cors; |
| 514 |
|
| 515 |
return $this; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public function setStats( StatsFactory $stats ): self { |
| 526 |
$this->stats = $stats; |
| 527 |
|
| 528 |
return $this; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
private function instantiateModule( array $info, string $name ): Module { |
| 536 |
if ( $info['class'] === SpecBasedModule::class ) { |
| 537 |
$module = new SpecBasedModule( |
| 538 |
$info['specFile'], |
| 539 |
$this, |
| 540 |
$info['pathPrefix'] ?? $name, |
| 541 |
$this->responseFactory, |
| 542 |
$this->basicAuth, |
| 543 |
$this->objectFactory, |
| 544 |
$this->restValidator, |
| 545 |
$this->errorReporter |
| 546 |
); |
| 547 |
} else { |
| 548 |
$module = new ExtraRoutesModule( |
| 549 |
$info['routeFiles'] ?? [], |
| 550 |
$info['extraRoutes'] ?? [], |
| 551 |
$this, |
| 552 |
$this->responseFactory, |
| 553 |
$this->basicAuth, |
| 554 |
$this->objectFactory, |
| 555 |
$this->restValidator, |
| 556 |
$this->errorReporter |
| 557 |
); |
| 558 |
} |
| 559 |
|
| 560 |
return $module; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public function isRestbaseCompatEnabled( RequestInterface $request ): bool { |
| 569 |
|
| 570 |
return $request->getHeaderLine( 'x-restbase-compat' ) === 'true'; |
| 571 |
} |
| 572 |
|
| 573 |
private function varyOnRestbaseCompat( ResponseInterface $response ) { |
| 574 |
|
| 575 |
$response->addHeader( 'Vary', 'x-restbase-compat' ); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public function getRestbaseCompatErrorData( RequestInterface $request, LocalizedHttpException $e ): array { |
| 584 |
$msg = $e->getMessageValue(); |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
return [ |
| 589 |
'type' => "MediaWikiError/" . |
| 590 |
str_replace( ' ', '_', HttpStatus::getMessage( $e->getCode() ) ), |
| 591 |
'title' => $msg->getKey(), |
| 592 |
'method' => strtolower( $request->getMethod() ), |
| 593 |
'detail' => $this->responseFactory->getFormattedMessage( $msg, 'en' ), |
| 594 |
'uri' => (string)$request->getUri() |
| 595 |
]; |
| 596 |
} |
| 597 |
} |