r96954 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96953‎ | r96954 | r96955 >
Date:11:24, 13 September 2011
Author:catrope
Status:ok
Tags:
Comment:
RL2: Put in some more sophisticate validation code for gadget property arrays. Not using it in the constructor because I think it's probably slow-ish. I want to use it when saving changes, will look at doing that later
Modified paths:
  • /branches/RL2/extensions/Gadgets/backend/Gadget.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/backend/Gadget.php
@@ -43,11 +43,66 @@
4444 /** array of module settings, see "module" key in the JSON blob */
4545 protected $moduleData;
4646
 47+ /**
 48+ * Validation metadata.
 49+ * 'foo.bar.baz' => array( 'callback', 'type name' [, 'membercallback', 'member type name'] )
 50+ */
 51+ protected static $propertyValidation = array(
 52+ 'settings' => array( 'is_array', 'array' ),
 53+ 'settings.rights' => array( 'is_array', 'array' , 'is_string', 'string' ),
 54+ 'settings.default' => array( 'is_bool', 'boolean' ),
 55+ 'settings.hidden' => array( 'is_bool', 'boolean' ),
 56+ 'settings.shared' => array( 'is_bool', 'boolean' ),
 57+ 'settings.category' => array( 'is_string', 'string' ),
 58+ 'module' => array( 'is_array', 'array' ),
 59+ 'module.scripts' => array( 'is_array', 'array', 'is_string', 'string' ),
 60+ 'module.styles' => array( 'is_array', 'array', 'is_string', 'string' ),
 61+ 'module.dependencies' => array( 'is_array', 'array', 'is_string', 'string' ),
 62+ 'module.messages' => array( 'is_array', 'array', 'is_string', 'string' ),
 63+ );
 64+
4765 /*** Public static methods ***/
4866
49 - public static function isValidPropertiesArray( $properties ) {
50 - // TODO: Also validate existence of individual properties
51 - return is_array( $properties ) && isset( $properties['settings'] ) && isset( $properties['module'] );
 67+ /**
 68+ * Check the validity of the given properties array
 69+ * @param $properties Return value of FormatJson::decode( $blob, true )
 70+ * @return Status object with error message if applicable
 71+ */
 72+ public static function validatePropertiesArray( $properties ) {
 73+ if ( $properties === null ) {
 74+ return Status::newFatal( 'gadgets-validate-invalidjson' );
 75+ }
 76+ if ( !is_array( $properties ) ) {
 77+ return Status::newFatal( 'gadgets-validate-notanobject', gettype( $properties ) ); // Use JSON terminology
 78+ }
 79+
 80+ foreach ( self::$propertyValidation as $property => $validation ) {
 81+ $path = explode( '.', $property );
 82+ $var = $properties;
 83+ foreach ( $path as $p ) {
 84+ if ( !isset( $var[$p] ) ) {
 85+ return Status::newFatal( 'gadgets-validate-notset', $property );
 86+ }
 87+ $var = $var[$p];
 88+ }
 89+
 90+ $func = $validation[0];
 91+ if ( !$func( $var ) ) {
 92+ return Status::newFatal( 'gadgets-validate-wrongtype', $property, $validation[1], gettype( $var ) );
 93+ }
 94+
 95+ if ( isset( $validation[2] ) ) {
 96+ // Descend into the array and check the type of each element
 97+ $func = $validation[2];
 98+ foreach ( $var as $i => $v ) {
 99+ if ( !$func( $v ) ){
 100+ return Status::newFatal( 'gadgets-validate-wrongtype', "{$property}[{$i}]", $validation[3], gettype( $v ) );
 101+ }
 102+ }
 103+ }
 104+ }
 105+
 106+ return Status::newGood();
52107 }
53108
54109 /*** Public methods ***/
@@ -65,8 +120,8 @@
66121 $properties = FormatJson::decode( $properties, true );
67122 }
68123
69 -
70 - if ( !self::isValidPropertiesArray( $properties ) ) {
 124+ // Do a quick sanity check rather than full validation
 125+ if ( !is_array( $properties ) || !isset( $properties['settings'] ) || !isset( $properties['module'] ) ) {
71126 throw new MWException( 'Invalid property array passed to ' . __METHOD__ );
72127 }
73128

Follow-up revisions

RevisionCommit summaryAuthorDate
r97005R2: Adding some comments that helped me understand Gadget::validateProperties...krinkle21:45, 13 September 2011
r97052RL2: Followup to r96954 and r97005: add i18n messages for the validation stuf...catrope13:10, 14 September 2011

Status & tagging log