mediawiki-extensions-Gadgets/includes/content/GadgetDefinitionValidator.php

90 lines
2.8 KiB
PHP
Raw Normal View History

<?php
/**
* Class responsible for validating Gadget definition contents
*
* @todo maybe this should use a formal JSON schema validator or something
*/
class GadgetDefinitionValidator {
/**
* Validation metadata.
* 'foo.bar.baz' => array( 'type check callback', 'type name' [, 'member type check callback', 'member type name'] )
*/
protected static $propertyValidation = array(
'settings' => array( 'is_array', 'array' ),
'settings.rights' => array( 'is_array', 'array' , 'is_string', 'string' ),
'settings.default' => array( 'is_bool', 'boolean' ),
'settings.hidden' => array( 'is_bool', 'boolean' ),
'settings.skins' => array( array( __CLASS__, 'isArrayOrTrue' ), 'array or true', 'is_string', 'string' ),
'settings.category' => array( 'is_string', 'string' ),
'module' => array( 'is_array', 'array' ),
'module.scripts' => array( 'is_array', 'array', 'is_string', 'string' ),
'module.styles' => array( 'is_array', 'array', 'is_string', 'string' ),
'module.dependencies' => array( 'is_array', 'array', 'is_string', 'string' ),
'module.messages' => array( 'is_array', 'array', 'is_string', 'string' ),
Implement support for specifying type=styles T87871 formally introduced the concept of a styles module, which sets mw.loader.state to "ready" when loaded through addModuleStyles(). Previously, addModuleStyles couldn't safely do that because a module may contain scripts also, in which case mw.loader must still load the (rest) of the module (causes styles to load twice). In MediaWiki core or extensions this is easily avoided by calling not calling both addModules() and addModuleStyles(). For Gadgets we call both as a workaround to allow users to provide styles (without a FOUC), but also to provide scripts+styles. Since we don't declare which one is intended (and some gadgets do both), we loaded them both ways. This will no longer be allowed in the future (see T92459). The new 'type=styles' Gadget attribute promises to ResourceLoader that a gadget only contains styles. Impact: * [Bug fix] When mw.loader requires a styles module that already loaded, it will not load again. * [Feature] It is possible for a general scripts+styles gadget to depend on a styles gadget. Previously this caused the styles to load twice. * Specifying type=styles will load the module through addModuleStyles() only. Use this for modules that contain styles that relate to elements already on the page (e.g. when customising the skin, layout, or article content). * Specifying type=general will load the module through addModules() only. Use this if your module contains both scripts and styles and the styles only relate to elements created by the script. This means the styles do not need to be loaded separately through addModuleStyles() and will not apply to noscript mode. Effective difference: * Gadgets with only styles: We assume type=styles. This fixes the main bug (styles loading twice) and requires no migration! * Gadgets with only scripts: We assume type=general. This requires no migration! (And: No more empty stylesheet request) * Gadgets with scripts (with or without styles): We assume type=general, but unless type=general was explicitly set we'll still load it both ways so that the styles apply directly on page load. If this is not needed, set type=general. If this is needed, it should become two separate modules. We do not support a single module having two purposes (1: apply styles to the page, 2: provide scripts+styles). The styles module should be separate. It can be made hidden, and listed as dependency of the other module. The latter case is detected on page load and results in a console warning with a link to T42284. Bug: T42284 Bug: T92459 Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
'module.type' => array( 'is_string', 'string' ),
);
/**
* @param mixed $value
* @return bool
*/
public static function isArrayOrTrue( $value ) {
return is_array( $value ) || $value === true;
}
/**
* Check the validity of the given properties array
* @param array $properties Return value of FormatJson::decode( $blob, true )
* @param bool $tolerateMissing If true, don't complain about missing keys
* @return Status object with error message if applicable
*/
public function validate( array $properties, $tolerateMissing = false ) {
foreach ( self::$propertyValidation as $property => $validation ) {
$path = explode( '.', $property );
$val = $properties;
// Walk down and verify that the path from the root to this property exists
foreach ( $path as $p ) {
if ( !array_key_exists( $p, $val ) ) {
if ( $tolerateMissing ) {
// Skip validation of this property altogether
continue 2;
} else {
return Status::newFatal( 'gadgets-validate-notset', $property );
}
}
$val = $val[$p];
}
// Do the actual validation of this property
$func = $validation[0];
if ( !call_user_func( $func, $val ) ) {
return Status::newFatal(
'gadgets-validate-wrongtype',
$property,
$validation[1],
gettype( $val )
);
}
if ( isset( $validation[2] ) && is_array( $val ) ) {
// Descend into the array and check the type of each element
$func = $validation[2];
foreach ( $val as $i => $v ) {
if ( !call_user_func( $func, $v ) ) {
return Status::newFatal(
'gadgets-validate-wrongtype',
"{$property}[{$i}]",
$validation[3],
gettype( $v )
);
}
}
}
}
return Status::newGood();
}
}