Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
16 / 17 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| AudienceDesignation | |
94.12% |
16 / 17 |
|
50.00% |
1 / 2 |
6.01 | |
0.00% |
0 / 1 |
| fromModuleId | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |||
| getDefaultGroups | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest\Module; |
| 4 | |
| 5 | /** |
| 6 | * Describes the set of audience designations available to REST modules. |
| 7 | * Modules without a specific designation are assumed to be "public". |
| 8 | * |
| 9 | * @since 1.47 |
| 10 | */ |
| 11 | enum AudienceDesignation: string { |
| 12 | // This is the default if no audience designation is specified. We therefore don't expect any |
| 13 | // module to actually specify this (although it would work as expected if one did). |
| 14 | case PUBLIC = 'public'; |
| 15 | |
| 16 | case INTERNAL = 'internal'; |
| 17 | |
| 18 | case BETA = 'beta'; |
| 19 | |
| 20 | // The prefix-less flat route module has no audience designation at all, which is valid |
| 21 | // (unlike a malformed id, represented by null). The empty backing value can never be |
| 22 | // produced by a module id suffix, so no module can declare itself as NONE. It is not meant |
| 23 | // to be output as an audience string. |
| 24 | case NONE = ''; |
| 25 | |
| 26 | /** |
| 27 | * Gets a module's audience designation from its module id. |
| 28 | * |
| 29 | * @param string $moduleId The module id |
| 30 | * |
| 31 | * @return ?AudienceDesignation |
| 32 | */ |
| 33 | public static function fromModuleId( string $moduleId ): ?AudienceDesignation { |
| 34 | if ( $moduleId === '' ) { |
| 35 | return self::NONE; |
| 36 | } |
| 37 | |
| 38 | // Module ids with no audience designation are assumed to be "public". |
| 39 | // |
| 40 | // Return null for module ids of invalid format, or whose audience designation is present |
| 41 | // but unrecognized. Generally, structure tests should identify invalid module ids and |
| 42 | // audience designations, so this should be a rare case. |
| 43 | $pattern = '!^([-.\w]+)/v[0-9]+(-[a-zA-Z]+)?(?:[0-9]+)?$!'; |
| 44 | if ( !preg_match( $pattern, $moduleId, $matches ) ) { |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | // $match[1] is the (required) module name, e.g. the "mymodule" in "mymodule/v1-beta". |
| 49 | if ( !isset( $matches[1] ) ) { |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | // $match[2], if present, is the audience designation, including its leading dash. |
| 54 | // For example, the "-beta" in "mymodule/v1-beta". |
| 55 | if ( !isset( $matches[2] ) ) { |
| 56 | return self::PUBLIC; |
| 57 | } |
| 58 | |
| 59 | // The leading character of $matches[2] is guaranteed to be a dash. Strip it. |
| 60 | $adStr = substr( $matches[2], 1 ); |
| 61 | return self::tryFrom( $adStr ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets the default REST discovery groups for this audience designation. Every published |
| 66 | * module is expected to have at least one group (T429399), so modules without an audience |
| 67 | * fall back to the generic 'default' group rather than an empty list. |
| 68 | * |
| 69 | * @return string[] |
| 70 | */ |
| 71 | public function getDefaultGroups(): array { |
| 72 | return match ( $this ) { |
| 73 | self::PUBLIC => [ 'preferred' ], |
| 74 | self::INTERNAL => [ 'internal' ], |
| 75 | self::BETA => [ 'beta' ], |
| 76 | self::NONE => [ 'default' ], |
| 77 | }; |
| 78 | } |
| 79 | } |