Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
41 / 41 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| TemplateDataNormalizer | |
100.00% |
41 / 41 |
|
100.00% |
4 / 4 |
15 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalize | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
8 | |||
| normalizeParameter | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
| normaliseInterfaceText | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\TemplateData; |
| 4 | |
| 5 | use stdClass; |
| 6 | |
| 7 | /** |
| 8 | * @license GPL-2.0-or-later |
| 9 | */ |
| 10 | class TemplateDataNormalizer { |
| 11 | |
| 12 | public const DEPRECATED_PARAMETER_TYPES = [ |
| 13 | 'string/line' => 'line', |
| 14 | 'string/wiki-page-name' => 'wiki-page-name', |
| 15 | 'string/wiki-user-name' => 'wiki-user-name', |
| 16 | 'string/wiki-file-name' => 'wiki-file-name', |
| 17 | ]; |
| 18 | |
| 19 | public function __construct( |
| 20 | private readonly string $contentLanguageCode, |
| 21 | ) { |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param stdClass $data Expected to be valid according to the {@see TemplateDataValidator} |
| 26 | */ |
| 27 | public function normalize( stdClass $data ): void { |
| 28 | $data->description ??= null; |
| 29 | $data->sets ??= []; |
| 30 | $data->maps ??= (object)[]; |
| 31 | $data->format ??= null; |
| 32 | $data->params ??= (object)[]; |
| 33 | |
| 34 | $this->normaliseInterfaceText( $data->description ); |
| 35 | foreach ( $data->sets as $setObj ) { |
| 36 | $this->normaliseInterfaceText( $setObj->label ); |
| 37 | } |
| 38 | |
| 39 | foreach ( $data->params as $param ) { |
| 40 | if ( isset( $param->inherits ) && isset( $data->params->{ $param->inherits } ) ) { |
| 41 | $parent = $data->params->{ $param->inherits }; |
| 42 | foreach ( $parent as $key => $value ) { |
| 43 | if ( !isset( $param->$key ) ) { |
| 44 | $param->$key = is_object( $parent->$key ) ? |
| 45 | clone $parent->$key : |
| 46 | $parent->$key; |
| 47 | } |
| 48 | } |
| 49 | unset( $param->inherits ); |
| 50 | } |
| 51 | $this->normalizeParameter( $param ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private function normalizeParameter( stdClass $paramObj ): void { |
| 56 | $paramObj->label ??= null; |
| 57 | $paramObj->description ??= null; |
| 58 | $paramObj->required ??= false; |
| 59 | $paramObj->suggested ??= false; |
| 60 | $paramObj->deprecated ??= false; |
| 61 | $paramObj->aliases ??= []; |
| 62 | $paramObj->type ??= 'unknown'; |
| 63 | $paramObj->autovalue ??= null; |
| 64 | $paramObj->default ??= null; |
| 65 | $paramObj->suggestedvalues ??= []; |
| 66 | $paramObj->example ??= null; |
| 67 | |
| 68 | $this->normaliseInterfaceText( $paramObj->label ); |
| 69 | $this->normaliseInterfaceText( $paramObj->description ); |
| 70 | $this->normaliseInterfaceText( $paramObj->default ); |
| 71 | $this->normaliseInterfaceText( $paramObj->example ); |
| 72 | |
| 73 | foreach ( $paramObj->aliases as &$alias ) { |
| 74 | if ( is_int( $alias ) ) { |
| 75 | $alias = (string)$alias; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Map deprecated types to newer versions |
| 80 | if ( isset( self::DEPRECATED_PARAMETER_TYPES[$paramObj->type] ) ) { |
| 81 | $paramObj->type = self::DEPRECATED_PARAMETER_TYPES[$paramObj->type]; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param string|stdClass &$text |
| 87 | */ |
| 88 | private function normaliseInterfaceText( &$text ): void { |
| 89 | if ( is_string( $text ) ) { |
| 90 | $text = (object)[ $this->contentLanguageCode => $text ]; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } |