Debian Codegraph @

extensions/Gadgets/includes/Content/GadgetDefinitionValidator.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Gadgets\Content;
4
5 use MediaWiki\Status\Status;
6
7 /**
8 * Validate the content of a gadget definition page.
9 *
10 * @see MediaWikiGadgetsJsonRepo
11 * @see GadgetDefinitionContent
12 * @internal
13 */
14 class GadgetDefinitionValidator {
15 private const TYPE_ARRAY = [ 'callback' => 'is_array', 'expect' => 'array' ];
16 private const TYPE_BOOL = [ 'callback' => 'is_bool', 'expect' => 'boolean' ];
17 private const TYPE_INT = [ 'callback' => 'is_int', 'expect' => 'number' ];
18 private const TYPE_STRING = [ 'callback' => 'is_string', 'expect' => 'string' ];
19 private const TYPE_PAGE_SUFFIX = [
20 'callback' => [ __CLASS__, 'isResourcePageSuffix' ], 'expect' => '.js, .css, .json'
21 ];
22 private const TYPE_MODULE_TYPE = [
23 'callback' => [ __CLASS__, 'isModuleType' ], 'expect' => '"", "general", "styles"',
24 ];
25
26 /**
27 * @var array Schema for the definition.
28 *
29 * - callback: boolean check.
30 * - expect: human-readable description for the gadgets-validate-wrongtype message.
31 * - child: optional type+expect for each array item.
32 */
33 protected static $schema = [
34 'settings' => self::TYPE_ARRAY,
35 'settings.actions' => self::TYPE_ARRAY + [ 'child' => self::TYPE_STRING ],
36 'settings.categories' => self::TYPE_ARRAY + [ 'child' => self::TYPE_STRING ],
37 'settings.category' => self::TYPE_STRING,
38 'settings.contentModels' => self::TYPE_ARRAY + [ 'child' => self::TYPE_STRING ],
39 'settings.default' => self::TYPE_BOOL,
40 'settings.hidden' => self::TYPE_BOOL,
41 'settings.namespaces' => self::TYPE_ARRAY + [ 'child' => self::TYPE_INT ],
42 'settings.package' => self::TYPE_BOOL,
43 'settings.requiresES6' => self::TYPE_BOOL,
44 'settings.rights' => self::TYPE_ARRAY + [ 'child' => self::TYPE_STRING ],
45 'settings.skins' => self::TYPE_ARRAY + [ 'child' => self::TYPE_STRING ],
46 'settings.supportsUrlLoad' => self::TYPE_BOOL,
47
48 'module' => self::TYPE_ARRAY,
49 'module.dependencies' => self::TYPE_ARRAY + [ 'child' => self::TYPE_STRING ],
50 'module.messages' => self::TYPE_ARRAY + [ 'child' => self::TYPE_STRING ],
51 'module.pages' => self::TYPE_ARRAY + [ 'child' => self::TYPE_PAGE_SUFFIX ],
52 'module.peers' => self::TYPE_ARRAY + [ 'child' => self::TYPE_STRING ],
53 'module.type' => self::TYPE_MODULE_TYPE,
54 ];
55
56 public static function isResourcePageSuffix( $title ): bool {
57 return is_string( $title ) && (
58 str_ends_with( $title, '.js' ) || str_ends_with( $title, '.css' ) || str_ends_with( $title, '.json' )
59 );
60 }
61
62 public static function isModuleType( string $type ): bool {
63 return $type === '' || $type === 'general' || $type === 'styles';
64 }
65
66 /**
67 * Check the validity of known properties in a gadget definition array.
68 *
69 * @param array $definition
70 * @param bool $tolerateMissing If true, don't complain about missing keys
71 * @return Status object with error message if applicable
72 */
73 public function validate( array $definition, $tolerateMissing = false ) {
74 foreach ( self::$schema as $property => $validation ) {
75 // Access the property by walking from the root to the specified property
76 $val = $definition;
77 foreach ( explode( '.', $property ) as $propName ) {
78 if ( !array_key_exists( $propName, $val ) ) {
79 if ( $tolerateMissing ) {
80 // Skip validation of this property altogether
81 continue 2;
82 }
83
84 return Status::newFatal( 'gadgets-validate-notset', $property );
85 }
86 $val = $val[$propName];
87 }
88
89 // Validate this property
90 $isValid = ( $validation['callback'] )( $val );
91 if ( !$isValid ) {
92 return Status::newFatal(
93 'gadgets-validate-wrongtype', $property,
94 $validation['expect']
95 );
96 }
97
98 // Descend into the array and validate each array item
99 if ( isset( $validation['child'] ) && is_array( $val ) ) {
100 foreach ( $val as $key => $item ) {
101 $isValid = ( $validation['child']['callback'] )( $item );
102 if ( !$isValid ) {
103 return Status::newFatal(
104 'gadgets-validate-wrongtype',
105 "{$property}[{$key}]",
106 $validation['child']['expect']
107 );
108 }
109 }
110 }
111 }
112
113 return Status::newGood();
114 }
115 }