Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.20% |
164 / 167 |
|
84.62% |
11 / 13 |
CRAP | |
0.00% |
0 / 1 |
| ModuleManager | |
98.20% |
164 / 167 |
|
84.62% |
11 / 13 |
46 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getModuleMode | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getModuleAudience | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getModuleGroups | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolveGroups | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
7 | |||
| getRouteFiles | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getDisabledRouteFiles | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| initRouteFiles | |
90.91% |
20 / 22 |
|
0.00% |
0 / 1 |
9.06 | |||
| getModuleInfos | |
100.00% |
59 / 59 |
|
100.00% |
1 / 1 |
7 | |||
| getModuleInfo | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| hasApiSpecs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getApiSpecs | |
96.55% |
28 / 29 |
|
0.00% |
0 / 1 |
9 | |||
| getModuleDefinitionInfo | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest\Module; |
| 4 | |
| 5 | use MediaWiki\Config\ServiceOptions; |
| 6 | use MediaWiki\MainConfigNames; |
| 7 | use MediaWiki\Rest\JsonLocalizer; |
| 8 | use Wikimedia\ObjectCache\BagOStuff; |
| 9 | |
| 10 | /** |
| 11 | * Manages information related to REST modules, such as routes and specs |
| 12 | * |
| 13 | * TODO: consider whether some code in Router or SpecBasedModule might be better placed here. |
| 14 | * TODO: consider whether ModuleManager and Router can share caching. |
| 15 | * |
| 16 | * @since 1.46 |
| 17 | */ |
| 18 | class ModuleManager { |
| 19 | // These modules will be enabled. No config entry is needed. |
| 20 | private const CORE_ROUTE_FILES = [ |
| 21 | 'includes/Rest/coreRoutes.json', |
| 22 | 'includes/Rest/site.v1.json', |
| 23 | 'includes/Rest/specs.v0.json', |
| 24 | 'includes/Rest/fragments.v0-internal.json', |
| 25 | 'includes/Rest/content.v2-beta.json', |
| 26 | ]; |
| 27 | |
| 28 | /** |
| 29 | * Static route path prefix for local OpenAPI specification endpoints. |
| 30 | * Corresponds to the route template in Router::ROUTE_MODULE_SPEC. |
| 31 | */ |
| 32 | private const ROUTE_MODULE_SPEC_PREFIX = '/specs/v0/module/'; |
| 33 | |
| 34 | // These specs will be available in the REST Sandbox. No config change is needed. |
| 35 | // Preserved for legacy compatibility with Special:RestSandbox. Can and should |
| 36 | // update for consistency as a dedicated extension, and remove once MediaWiki |
| 37 | // transitions to Unified Developer Front Door |
| 38 | // (https://phabricator.wikimedia.org/project/board/9020/). |
| 39 | private const CORE_SPECS = [ |
| 40 | 'mw-extra' => [ |
| 41 | 'url' => self::ROUTE_MODULE_SPEC_PREFIX . '-', |
| 42 | 'name' => 'MediaWiki REST API (routes not in modules)', |
| 43 | ], |
| 44 | 'specs.v0' => [ |
| 45 | 'file' => "./includes/Rest/specs.v0.json", |
| 46 | ], |
| 47 | 'site.v1' => [ |
| 48 | 'file' => "./includes/Rest/site.v1.json", |
| 49 | ], |
| 50 | 'fragments.v0-internal' => [ |
| 51 | 'file' => "./includes/Rest/fragments.v0-internal.json", |
| 52 | ], |
| 53 | 'content.v2-beta' => [ |
| 54 | 'file' => "./includes/Rest/content.v2-beta.json", |
| 55 | ], |
| 56 | ]; |
| 57 | |
| 58 | /** Seconds to persist module definitions on cache */ |
| 59 | private const MODULE_DEFINITION_TTL = 60; |
| 60 | |
| 61 | /** @var string[]|null */ |
| 62 | private ?array $routeFiles = null; |
| 63 | private ?array $disabledRouteFiles = null; |
| 64 | |
| 65 | private readonly string $extensionDirectory; |
| 66 | |
| 67 | private readonly array $restApiAdditionalRouteFiles; |
| 68 | private readonly array $restExternalModules; |
| 69 | private readonly array $restModuleOverrides; |
| 70 | |
| 71 | private string $rootPath; |
| 72 | private ?array $moduleInfos = null; |
| 73 | |
| 74 | /** |
| 75 | * @internal |
| 76 | */ |
| 77 | public const CONSTRUCTOR_OPTIONS = [ |
| 78 | MainConfigNames::ExtensionDirectory, |
| 79 | MainConfigNames::RestAPIAdditionalRouteFiles, |
| 80 | MainConfigNames::RestExternalModules, |
| 81 | MainConfigNames::RestModuleOverrides, |
| 82 | MainConfigNames::RestPath |
| 83 | ]; |
| 84 | |
| 85 | /** |
| 86 | * @param ServiceOptions $options |
| 87 | * @param string[] $extensionModuleFiles |
| 88 | * @param BagOStuff $srvCache Optional BagOStuff instance to an APC-style cache. |
| 89 | * @param JsonLocalizer $jsonLocalizer |
| 90 | * |
| 91 | * @internal |
| 92 | */ |
| 93 | public function __construct( |
| 94 | ServiceOptions $options, |
| 95 | private readonly array $extensionModuleFiles, |
| 96 | private readonly BagOStuff $srvCache, |
| 97 | private readonly JsonLocalizer $jsonLocalizer, |
| 98 | ) { |
| 99 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 100 | |
| 101 | $this->extensionDirectory = $options->get( MainConfigNames::ExtensionDirectory ); |
| 102 | $this->restApiAdditionalRouteFiles = $options->get( MainConfigNames::RestAPIAdditionalRouteFiles ); |
| 103 | $this->restExternalModules = $options->get( MainConfigNames::RestExternalModules ); |
| 104 | $this->restModuleOverrides = $options->get( MainConfigNames::RestModuleOverrides ); |
| 105 | $this->rootPath = $options->get( MainConfigNames::RestPath ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Gets the module mode for a given module id. |
| 110 | * |
| 111 | * @param string $moduleId The module id |
| 112 | * |
| 113 | * @return ModuleMode |
| 114 | * @since 1.47 |
| 115 | */ |
| 116 | public function getModuleMode( string $moduleId ): ModuleMode { |
| 117 | // If an override is attempted, but the mode is unrecognized, disable the module. This |
| 118 | // helps guard against undesired module activation/publishing due to configuration typos. |
| 119 | if ( isset( $this->restModuleOverrides[$moduleId]['availability'] ) ) { |
| 120 | $mm = ModuleMode::tryFrom( $this->restModuleOverrides[$moduleId]['availability'] ); |
| 121 | $mm ??= ModuleMode::DISABLED; |
| 122 | } |
| 123 | |
| 124 | return $mm ?? ModuleMode::getModuleMode( $this->getModuleAudience( $moduleId ) ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Gets a module's audience designation from its module id, or null if it is malformed or |
| 129 | * unrecognized. Local and external modules follow the same rules. The prefix-less flat route |
| 130 | * module has none (AudienceDesignation::NONE). Unlike the module's mode, its audience |
| 131 | * designation cannot be overridden by RestModuleOverrides. |
| 132 | * |
| 133 | * @param string $moduleId The module id |
| 134 | * |
| 135 | * @return ?AudienceDesignation |
| 136 | * @since 1.47 |
| 137 | */ |
| 138 | public function getModuleAudience( string $moduleId ): ?AudienceDesignation { |
| 139 | return AudienceDesignation::fromModuleId( $moduleId ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Gets the REST discovery groups for a module by id, or an empty array if the module is |
| 144 | * unknown. This is a convenience wrapper around getModuleInfo(), so it always agrees with |
| 145 | * the groups exposed via ModuleInfo::getGroups(). |
| 146 | * |
| 147 | * @param string $moduleId The module id |
| 148 | * |
| 149 | * @return string[] |
| 150 | * @since 1.47 |
| 151 | */ |
| 152 | public function getModuleGroups( string $moduleId ): array { |
| 153 | return $this->getModuleInfo( $moduleId )?->getGroups() ?? []; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Resolves the REST discovery groups for a module. Modules that are not listed in discovery |
| 158 | * (hidden or disabled) have no groups. Otherwise, the groups come from (in order of |
| 159 | * precedence) RestModuleOverrides, the module definition file, or the audience designation's |
| 160 | * defaults. Deprecated modules additionally get the 'deprecated' group. |
| 161 | * |
| 162 | * @param string $moduleId The module id |
| 163 | * @param ModuleMode $availability The module's resolved mode |
| 164 | * @param array $moduleDefInfo The module definition info, for local modules |
| 165 | * |
| 166 | * @return string[] |
| 167 | */ |
| 168 | private function resolveGroups( |
| 169 | string $moduleId, |
| 170 | ModuleMode $availability, |
| 171 | array $moduleDefInfo = [] |
| 172 | ): array { |
| 173 | if ( $availability === ModuleMode::DISABLED || $availability === ModuleMode::HIDDEN ) { |
| 174 | return []; |
| 175 | } |
| 176 | |
| 177 | if ( isset( $this->restModuleOverrides[$moduleId]['groups'] ) ) { |
| 178 | $groups = (array)$this->restModuleOverrides[$moduleId]['groups']; |
| 179 | } elseif ( isset( $moduleDefInfo['groups'] ) ) { |
| 180 | $groups = $moduleDefInfo['groups']; |
| 181 | } else { |
| 182 | $groups = $this->getModuleAudience( $moduleId )?->getDefaultGroups() ?? []; |
| 183 | } |
| 184 | |
| 185 | if ( isset( $moduleDefInfo['deprecationSettings'] ) && !in_array( 'deprecated', $groups, true ) ) { |
| 186 | $groups[] = 'deprecated'; |
| 187 | } |
| 188 | |
| 189 | return $groups; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @return string[] |
| 194 | */ |
| 195 | public function getRouteFiles(): array { |
| 196 | if ( $this->routeFiles === null ) { |
| 197 | $this->initRouteFiles(); |
| 198 | } |
| 199 | |
| 200 | return $this->routeFiles; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Useful for testing or error status. The return value will include route files that |
| 205 | * were disabled by either audience designation or configuration. |
| 206 | * |
| 207 | * @since 1.47 |
| 208 | * @return string[] |
| 209 | */ |
| 210 | public function getDisabledRouteFiles(): array { |
| 211 | if ( $this->disabledRouteFiles === null ) { |
| 212 | $this->initRouteFiles(); |
| 213 | } |
| 214 | |
| 215 | return $this->disabledRouteFiles; |
| 216 | } |
| 217 | |
| 218 | private function initRouteFiles() { |
| 219 | // Always include the "official" routes. Include additional routes if specified. |
| 220 | // Extension module files are added to extension.json via the RestModuleFiles attribute |
| 221 | // and passed to ModuleManager via the extensionModuleFiles constructor parameter. |
| 222 | $routeFiles = array_merge( |
| 223 | self::CORE_ROUTE_FILES, |
| 224 | $this->extensionModuleFiles, |
| 225 | $this->restApiAdditionalRouteFiles |
| 226 | ); |
| 227 | $disabledRouteFiles = []; |
| 228 | |
| 229 | foreach ( $routeFiles as &$file ) { |
| 230 | if ( str_starts_with( $file, 'extensions/' ) ) { |
| 231 | // Support hacks like Wikibase.ci.php |
| 232 | $file = substr_replace( $file, $this->extensionDirectory, |
| 233 | 0, strlen( 'extensions' ) ); |
| 234 | } elseif ( !str_starts_with( $file, '/' ) |
| 235 | && !( wfIsWindows() && preg_match( '!^[a-zA-Z]:[/\\\\]!', $file ) ) |
| 236 | ) { |
| 237 | $file = MW_INSTALL_PATH . '/' . $file; |
| 238 | } |
| 239 | } |
| 240 | unset( $file ); |
| 241 | |
| 242 | // If the module's audience designation or configuration settings say it should be |
| 243 | // disabled, then don't include it at all. Old-style flat route files cannot be disabled. |
| 244 | foreach ( $routeFiles as $key => $file ) { |
| 245 | $moduleDefInfo = $this->getModuleDefinitionInfo( $file ); |
| 246 | if ( |
| 247 | $moduleDefInfo && |
| 248 | $this->getModuleMode( $moduleDefInfo['moduleId'] ) === ModuleMode::DISABLED |
| 249 | ) { |
| 250 | $disabledRouteFiles[$key] = $file; |
| 251 | unset( $routeFiles[$key] ); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | $this->routeFiles = $routeFiles; |
| 256 | $this->disabledRouteFiles = $disabledRouteFiles; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Returns an array of ModuleInfo objects for all registered modules, indexed by module ID. |
| 261 | * This method aggregates and returns both local (Core and Extension) and external modules. |
| 262 | * |
| 263 | * @return ModuleInfo[] |
| 264 | * @since 1.47 |
| 265 | */ |
| 266 | public function getModuleInfos(): array { |
| 267 | if ( $this->moduleInfos !== null ) { |
| 268 | return $this->moduleInfos; |
| 269 | } |
| 270 | |
| 271 | $modules = []; |
| 272 | |
| 273 | // Gather local modules. |
| 274 | $routeFiles = $this->getRouteFiles(); |
| 275 | $disabledRouteFiles = $this->getDisabledRouteFiles(); |
| 276 | $allLocalRouteFiles = array_merge( $routeFiles, $disabledRouteFiles ); |
| 277 | |
| 278 | foreach ( $allLocalRouteFiles as $file ) { |
| 279 | $moduleDefInfo = $this->getModuleDefinitionInfo( $file ); |
| 280 | if ( !$moduleDefInfo ) { |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | $moduleId = $moduleDefInfo['moduleId']; |
| 285 | $availability = $this->getModuleMode( $moduleId ); |
| 286 | $groups = $this->resolveGroups( $moduleId, $availability, $moduleDefInfo ); |
| 287 | |
| 288 | $modules[$moduleId] = new ModuleInfo( |
| 289 | $moduleId, |
| 290 | $availability, |
| 291 | false, // isExternal |
| 292 | $moduleDefInfo['title'] ?? $moduleId, |
| 293 | $moduleDefInfo['description'] ?? null, |
| 294 | $moduleDefInfo['version'] ?? null, |
| 295 | $moduleDefInfo['oadSpecPath'] ?? null, |
| 296 | $groups |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | // Add the prefix-less module. |
| 301 | $prefixlessModuleAvailability = $this->getModuleMode( '' ); |
| 302 | $modules[''] = new ModuleInfo( |
| 303 | '', |
| 304 | $prefixlessModuleAvailability, |
| 305 | false, // isExternal |
| 306 | self::CORE_SPECS['mw-extra']['name'], |
| 307 | $this->jsonLocalizer->getFormattedMessage( 'rest-module-extra-routes-desc' ), |
| 308 | '0.1.0', |
| 309 | null, |
| 310 | $this->resolveGroups( '', $prefixlessModuleAvailability ) |
| 311 | ); |
| 312 | |
| 313 | // Gather external modules. |
| 314 | foreach ( $this->restExternalModules as $externalModuleId => $externalModuleConfig ) { |
| 315 | $availability = $this->getModuleMode( $externalModuleId ); |
| 316 | $externalModuleConfig = $this->jsonLocalizer->localizeJson( $externalModuleConfig ); |
| 317 | $groups = $this->resolveGroups( $externalModuleId, $availability ); |
| 318 | |
| 319 | $modules[$externalModuleId] = new ModuleInfo( |
| 320 | $externalModuleId, |
| 321 | $availability, |
| 322 | true, // isExternal |
| 323 | $externalModuleConfig['info']['title'] ?? $externalModuleId, |
| 324 | $externalModuleConfig['info']['description'] ?? null, |
| 325 | $externalModuleConfig['info']['version'] ?? null, |
| 326 | null, |
| 327 | $groups, |
| 328 | $externalModuleConfig['base'] ?? null, |
| 329 | $externalModuleConfig['spec'] ?? null |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | // Sort modules case-insensitively, putting prefix-less module first. |
| 334 | uksort( $modules, static function ( $a, $b ) { |
| 335 | if ( $a === '' ) { |
| 336 | return -1; |
| 337 | } |
| 338 | if ( $b === '' ) { |
| 339 | return 1; |
| 340 | } |
| 341 | return strnatcasecmp( $a, $b ); |
| 342 | } ); |
| 343 | |
| 344 | $this->moduleInfos = $modules; |
| 345 | return $this->moduleInfos; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Gets the ModuleInfo for a single module by ID, or null if not found. |
| 350 | * |
| 351 | * @param string $moduleId |
| 352 | * @return ?ModuleInfo |
| 353 | * @since 1.47 |
| 354 | */ |
| 355 | public function getModuleInfo( string $moduleId ): ?ModuleInfo { |
| 356 | $infos = $this->getModuleInfos(); |
| 357 | return $infos[$moduleId] ?? null; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Returns true if any api specs are available, or false otherwise. |
| 362 | * |
| 363 | * @return bool |
| 364 | */ |
| 365 | public function hasApiSpecs(): bool { |
| 366 | // mw-extra is always available, so there is no need for logic here. |
| 367 | // |
| 368 | // TODO: deprecate this function once audience designations are fully implemented and |
| 369 | // rolled out, and it is clear that this function is no longer useful. |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Returns the available choices for APIs to explore in the REST Sandbox. |
| 375 | * |
| 376 | * Preserved for legacy compatibility with Special:RestSandbox. Can and should |
| 377 | * update for consistency as a dedicated extension, and remove once MediaWiki |
| 378 | * transitions to Unified Developer Front Door. |
| 379 | * |
| 380 | * @return array<string,array<string,mixed>> |
| 381 | */ |
| 382 | public function getApiSpecs(): array { |
| 383 | $specs = []; |
| 384 | $defaultName = self::CORE_SPECS['mw-extra']['name']; |
| 385 | |
| 386 | foreach ( $this->getModuleInfos() as $info ) { |
| 387 | if ( $info->getAvailability() !== ModuleMode::PUBLISHED ) { |
| 388 | continue; |
| 389 | } |
| 390 | |
| 391 | if ( $info->isExternal() ) { |
| 392 | $key = $info->getId(); |
| 393 | $url = $info->getExternalSpecUrl(); |
| 394 | } elseif ( $info->getId() === '' ) { |
| 395 | $key = 'mw-extra'; |
| 396 | $url = $this->rootPath . self::ROUTE_MODULE_SPEC_PREFIX . '-'; |
| 397 | } else { |
| 398 | $key = str_replace( '/', '.', $info->getId() ); |
| 399 | $specPath = $info->getLocalDescriptionSpecPath(); |
| 400 | $url = ( $specPath !== null ) |
| 401 | ? $this->rootPath . '/' . $info->getId() . $specPath |
| 402 | : $this->rootPath . self::ROUTE_MODULE_SPEC_PREFIX . $info->getId(); |
| 403 | } |
| 404 | |
| 405 | $specs[$key] = [ |
| 406 | 'url' => $url, |
| 407 | 'name' => $info->getTitle() ?? ( $info->getId() === '' ? $defaultName : $info->getId() ), |
| 408 | 'groups' => $info->getGroups(), |
| 409 | ]; |
| 410 | } |
| 411 | |
| 412 | // This will put the "routes not in modules" entry first. |
| 413 | uasort( $specs, static function ( $a, $b ) use ( $defaultName ) { |
| 414 | if ( $a['name'] === $defaultName ) { |
| 415 | return -1; |
| 416 | } elseif ( $b['name'] === $defaultName ) { |
| 417 | return 1; |
| 418 | } else { |
| 419 | return strnatcasecmp( $a['name'], $b['name'] ); |
| 420 | } |
| 421 | } ); |
| 422 | |
| 423 | return $specs; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Gets necessary info from the module definition info, from cache if possible, |
| 428 | * from the definition file otherwise. |
| 429 | * |
| 430 | * This returns null for legacy flat-route definition files. |
| 431 | * |
| 432 | * @param string $file The module definition file to load |
| 433 | * @return ?array{moduleId: string, title: string, oadSpecPath: ?string} |
| 434 | */ |
| 435 | private function getModuleDefinitionInfo( string $file ): ?array { |
| 436 | $key = $this->srvCache->makeKey( |
| 437 | __CLASS__, |
| 438 | 'definition', |
| 439 | 'v3', |
| 440 | sha1( $file ), |
| 441 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 442 | (int)@filemtime( $file ), |
| 443 | $this->jsonLocalizer->getLangCode() |
| 444 | ); |
| 445 | |
| 446 | return $this->srvCache->getWithSetCallback( |
| 447 | $key, |
| 448 | self::MODULE_DEFINITION_TTL, |
| 449 | function () use ( $file ) { |
| 450 | // An exception here almost certainly means this is an old-style flat route file. |
| 451 | try { |
| 452 | $md = SpecBasedModule::loadModuleDefinition( $file, $this->jsonLocalizer ); |
| 453 | return [ |
| 454 | 'moduleId' => $md['moduleId'], |
| 455 | 'title' => $md['info']['title'] ?? null, |
| 456 | 'version' => $md['info']['version'] ?? null, |
| 457 | 'description' => $md['info']['description'] ?? null, |
| 458 | 'deprecationSettings' => $md['info']['deprecationSettings'] ?? null, |
| 459 | 'groups' => isset( $md['info']['groups'] ) ? (array)$md['info']['groups'] : null, |
| 460 | 'oadSpecPath' => $md['info']['oadSpecPath'] ?? null |
| 461 | ]; |
| 462 | } catch ( ModuleFormatException ) { |
| 463 | return null; |
| 464 | } |
| 465 | } |
| 466 | ); |
| 467 | } |
| 468 | } |