Ori.livneh has uploaded a new change for review. https://gerrit.wikimedia.org/r/274631
Change subject: Lint fixes suggested by phpcs ...................................................................... Lint fixes suggested by phpcs * Remove underscore prefix from two test method names. * Declare visibility on all class members (I don't think this constitutes an improvement, but I have given up on that fight. May as well be compliant.) * Update array syntax for PHP >= 5.4. * Break up a few long lines. Change-Id: I8ffa9bc9ed0f5e8d8f91a295ca32a006f9f072de --- M EventLogging.i18n.php M EventLogging.namespaces.php M EventLogging.php M includes/ApiJsonSchema.php M includes/EventLogging.php M includes/EventLoggingHooks.php M includes/JsonSchema.i18n.php M includes/JsonSchema.php M includes/JsonSchemaContent.php M includes/JsonSchemaContentHandler.php M includes/RemoteSchema.php M includes/ResourceLoaderSchemaModule.php M tests/EventLoggingExtensionFunctionsTest.php M tests/JsonSchemaTest.php M tests/RemoteSchemaTest.php M tests/ResourceLoaderSchemaModuleTest.php M tests/SerializeEventTest.php M tests/ValidateSchemaTest.php 18 files changed, 179 insertions(+), 207 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventLogging refs/changes/31/274631/1 diff --git a/EventLogging.i18n.php b/EventLogging.i18n.php index a5eeda7..0c264f9 100644 --- a/EventLogging.i18n.php +++ b/EventLogging.i18n.php @@ -10,12 +10,12 @@ * * This shim maintains compatibility back to MediaWiki 1.17. */ -$messages = array(); +$messages = []; if ( !function_exists( 'wfJsonI18nShim58963441fa6d0066' ) ) { function wfJsonI18nShim58963441fa6d0066( $cache, $code, &$cachedData ) { - $codeSequence = array_merge( array( $code ), $cachedData['fallbackSequence'] ); + $codeSequence = array_merge( [ $code ], $cachedData['fallbackSequence'] ); foreach ( $codeSequence as $csCode ) { - $fileName = dirname( __FILE__ ) . "/i18n/core/$csCode.json"; + $fileName = __DIR__ . "/i18n/core/$csCode.json"; if ( is_readable( $fileName ) ) { $data = FormatJson::decode( file_get_contents( $fileName ), true ); foreach ( array_keys( $data ) as $key ) { diff --git a/EventLogging.namespaces.php b/EventLogging.namespaces.php index 9ee028c..d67ca14 100644 --- a/EventLogging.namespaces.php +++ b/EventLogging.namespaces.php @@ -7,9 +7,9 @@ * @ingroup Extensions */ -$namespaceNames = array(); +$namespaceNames = []; -$namespaceNames['en'] = array( +$namespaceNames['en'] = [ 470 => 'Schema', 471 => 'Schema_talk', -); +]; diff --git a/EventLogging.php b/EventLogging.php index cfb3316..5af465f 100644 --- a/EventLogging.php +++ b/EventLogging.php @@ -14,20 +14,20 @@ // Credits -$wgExtensionCredits[ 'other' ][] = array( +$wgExtensionCredits[ 'other' ][] = [ 'path' => __FILE__, 'name' => 'EventLogging', - 'author' => array( + 'author' => [ 'Ori Livneh', 'Timo Tijhof', 'S Page', 'Matthew Flaschen', - ), + ], 'version' => '0.8.0', 'url' => 'https://www.mediawiki.org/wiki/Extension:EventLogging', 'descriptionmsg' => 'eventlogging-desc', 'license-name' => 'GPL-2.0+' -); +]; // Namespaces define( 'NS_SCHEMA', 470 ); @@ -44,9 +44,7 @@ }; $wgContentHandlers[ 'JsonSchema' ] = 'JsonSchemaContentHandler'; $wgNamespaceContentModels[ NS_SCHEMA ] = 'JsonSchema'; -$wgNamespaceProtection[ NS_SCHEMA ] = array( 'autoconfirmed' ); - - +$wgNamespaceProtection[ NS_SCHEMA ] = [ 'autoconfirmed' ]; // Configuration @@ -77,7 +75,7 @@ * @var array: A map of event schema names to revision IDs. * @example array: array( 'MultimediaViewerNetworkPerformance' => 7917896 ); */ -$wgEventLoggingSchemas = isset( $wgEventLoggingSchemas ) ? $wgEventLoggingSchemas : array(); +$wgEventLoggingSchemas = isset( $wgEventLoggingSchemas ) ? $wgEventLoggingSchemas : []; // Helpers @@ -89,8 +87,8 @@ * @param array $schema Schema to validate against (default: JSON Schema). * @return bool: True. */ -function efSchemaValidate( $object, $schema = NULL ) { - if ( $schema === NULL ) { +function efSchemaValidate( $object, $schema = null ) { + if ( $schema === null ) { // Default to JSON Schema $json = file_get_contents( __DIR__ . '/schemas/schemaschema.json' ); $schema = FormatJson::decode( $json, true ); @@ -118,7 +116,7 @@ */ function efStripKeyRecursive( &$array, $key ) { unset( $array[ $key ] ); - foreach( $array as $k => &$v ) { + foreach ( $array as $k => &$v ) { if ( is_array( $v ) ) { efStripKeyRecursive( $v, $key ); } @@ -127,7 +125,7 @@ // Classes -$wgAutoloadClasses += array( +$wgAutoloadClasses += [ 'EventLogging' => __DIR__ . '/includes/EventLogging.php', // Hooks @@ -151,63 +149,63 @@ // API 'ApiJsonSchema' => __DIR__ . '/includes/ApiJsonSchema.php', -); +]; // Messages $wgMessagesDirs['EventLogging'] = __DIR__ . '/i18n/core'; $wgMessagesDirs['JsonSchema'] = __DIR__ . '/i18n/jsonschema'; -$wgExtensionMessagesFiles += array( +$wgExtensionMessagesFiles += [ 'EventLogging' => __DIR__ . '/EventLogging.i18n.php', 'EventLoggingNamespaces' => __DIR__ . '/EventLogging.namespaces.php', 'JsonSchema' => __DIR__ . '/includes/JsonSchema.i18n.php', -); +]; // Modules -$wgResourceModules[ 'ext.eventLogging' ] = array( +$wgResourceModules[ 'ext.eventLogging' ] = [ 'scripts' => 'modules/ext.eventLogging.core.js', 'localBasePath' => __DIR__, 'remoteExtPath' => 'EventLogging', - 'dependencies' => array( + 'dependencies' => [ 'json', 'ext.eventLogging.subscriber', - ), - 'targets' => array( 'desktop', 'mobile' ), -); + ], + 'targets' => [ 'desktop', 'mobile' ], +]; -$wgResourceModules[ 'ext.eventLogging.subscriber' ] = array( - 'scripts' => array( +$wgResourceModules[ 'ext.eventLogging.subscriber' ] = [ + 'scripts' => [ 'modules/ext.eventLogging.subscriber.js', 'modules/ext.eventLogging.Schema.js', - ), + ], 'localBasePath' => __DIR__, 'remoteExtPath' => 'EventLogging', - 'dependencies' => array( 'mediawiki.user' ), - 'targets' => array( 'desktop', 'mobile' ), -); + 'dependencies' => [ 'mediawiki.user' ], + 'targets' => [ 'desktop', 'mobile' ], +]; // Back-compatibility alias for subscriber -$wgResourceModules[ 'ext.eventLogging.Schema' ] = array( - 'dependencies' => array( +$wgResourceModules[ 'ext.eventLogging.Schema' ] = [ + 'dependencies' => [ 'ext.eventLogging.subscriber' - ), - 'targets' => array( 'desktop', 'mobile' ), -); + ], + 'targets' => [ 'desktop', 'mobile' ], +]; -$wgResourceModules[ 'ext.eventLogging.jsonSchema' ] = array( +$wgResourceModules[ 'ext.eventLogging.jsonSchema' ] = [ 'scripts' => 'modules/ext.eventLogging.jsonSchema.js', 'localBasePath' => __DIR__, 'remoteExtPath' => 'EventLogging', 'position' => 'top', -); +]; -$wgResourceModules[ 'ext.eventLogging.jsonSchema.styles' ] = array( +$wgResourceModules[ 'ext.eventLogging.jsonSchema.styles' ] = [ 'styles' => 'modules/ext.eventLogging.jsonSchema.css', 'localBasePath' => __DIR__, 'remoteExtPath' => 'EventLogging', 'position' => 'top', -); +]; // Hooks @@ -216,7 +214,8 @@ $wgHooks[ 'BeforePageDisplay' ][] = 'EventLoggingHooks::onBeforePageDisplay'; $wgHooks[ 'ResourceLoaderGetConfigVars' ][] = 'EventLoggingHooks::onResourceLoaderGetConfigVars'; $wgHooks[ 'ResourceLoaderTestModules' ][] = 'EventLoggingHooks::onResourceLoaderTestModules'; -$wgHooks[ 'ResourceLoaderRegisterModules' ][] = 'EventLoggingHooks::onResourceLoaderRegisterModules'; +$wgHooks[ 'ResourceLoaderRegisterModules' ][] = \ + 'EventLoggingHooks::onResourceLoaderRegisterModules'; // Registers hook and content handlers for JSON schema content iff // running on the MediaWiki instance housing the schemas. diff --git a/includes/ApiJsonSchema.php b/includes/ApiJsonSchema.php index ab4dea2..907f608 100644 --- a/includes/ApiJsonSchema.php +++ b/includes/ApiJsonSchema.php @@ -32,25 +32,25 @@ } public function getAllowedParams() { - return array( - 'revid' => array( + return [ + 'revid' => [ ApiBase::PARAM_TYPE => 'integer', ApiBase::PARAM_REQUIRED => true, - ), - 'title' => array( + ], + 'title' => [ ApiBase::PARAM_TYPE => 'string', - ), - ); + ], + ]; } /** * @deprecated since MediaWiki core 1.25 */ public function getParamDescription() { - return array( + return [ 'revid' => 'Schema revision ID', 'title' => 'Schema name', - ); + ]; } /** @@ -64,17 +64,17 @@ * @deprecated since MediaWiki core 1.25 */ public function getExamples() { - return array( 'api.php?action=jsonschema&revid=1234' => 'Retrieve schema for revision 1234' ); + return [ 'api.php?action=jsonschema&revid=1234' => 'Retrieve schema for revision 1234' ]; } /** * @see ApiBase::getExamplesMessages() */ protected function getExamplesMessages() { - return array( + return [ 'action=jsonschema&revid=1234' => 'apihelp-jsonschema-example-1', - ); + ]; } /** @@ -95,7 +95,7 @@ * HTTP 400 ('Bad Request') status code. * @param array|string: user error array */ - public function dieUsageMsg( $error ) { + public function dieUsageMsg( $error ) { $parsed = $this->parseMsg( (array)$error ); $this->dieUsage( $parsed['info'], $parsed['code'], 400 ); } @@ -105,26 +105,26 @@ $rev = Revision::newFromID( $params['revid'] ); if ( !$rev ) { - $this->dieUsageMsg( array( 'nosuchrevid', $params['revid'] ) ); + $this->dieUsageMsg( [ 'nosuchrevid', $params['revid'] ] ); } $title = $rev->getTitle(); if ( !$title || !$title->inNamespace( NS_SCHEMA ) ) { - $this->dieUsageMsg( array( 'invalidtitle', $title ) ); + $this->dieUsageMsg( [ 'invalidtitle', $title ] ); } /** @var JsonSchemaContent $content */ $content = $rev->getContent(); if ( !$content ) { - $this->dieUsageMsg( array( 'nosuchrevid', $params['revid'] ) ); + $this->dieUsageMsg( [ 'nosuchrevid', $params['revid'] ] ); } // We use the revision ID for lookup; the 'title' parameter is // optional. If present, it is used to assert that the specified // revision ID is indeed a revision of a page with the specified // title. (Bug 46174) - if ( $params['title'] && !$title->equals( Title::newFromText( $params['title'], NS_SCHEMA ) ) ) { - $this->dieUsageMsg( array( 'revwrongpage', $params['revid'], $params['title'] ) ); + if ( $params['title'] && !$title->equals( Title::newFromText( $params['title'], NS_SCHEMA ) ) ) { + $this->dieUsageMsg( [ 'revwrongpage', $params['revid'], $params['title'] ] ); } $this->markCacheable( $rev ); @@ -135,7 +135,7 @@ foreach ( $schema as $k => &$v ) { if ( $k === 'properties' ) { foreach ( $v as &$properties ) { - $properties[ApiResult::META_BC_BOOLS] = array( 'required' ); + $properties[ApiResult::META_BC_BOOLS] = [ 'required' ]; } } $result->addValue( null, $k, $v ); diff --git a/includes/EventLogging.php b/includes/EventLogging.php index d20a825..0559a12 100644 --- a/includes/EventLogging.php +++ b/includes/EventLogging.php @@ -54,13 +54,13 @@ $isValid = false; } - $encapsulated = array( + $encapsulated = [ 'event' => $event, 'schema' => $schemaName, 'revision' => $revId, 'clientValidated' => $isValid, 'wiki' => $wgDBname, - ); + ]; if ( isset( $_SERVER[ 'HTTP_HOST' ] ) ) { $encapsulated[ 'webHost' ] = $_SERVER[ 'HTTP_HOST' ]; } @@ -80,7 +80,7 @@ * @param array $encapsulatedEvent Encapsulated event * @return string $json **/ - static function serializeEvent($encapsulatedEvent) { + static function serializeEvent( $encapsulatedEvent ) { $event = $encapsulatedEvent['event']; diff --git a/includes/EventLoggingHooks.php b/includes/EventLoggingHooks.php index 15860f4..f755d86 100644 --- a/includes/EventLoggingHooks.php +++ b/includes/EventLoggingHooks.php @@ -23,11 +23,11 @@ wfDebugLog( 'EventLogging', 'No suitable memcached driver found.' ); } - foreach ( array( + foreach ( [ 'wgEventLoggingBaseUri', 'wgEventLoggingDBname', 'wgEventLoggingSchemaApiUri' - ) as $configVar ) { + ] as $configVar ) { if ( !isset( $GLOBALS[ $configVar ] ) || $GLOBALS[ $configVar ] === false ) { wfDebugLog( 'EventLogging', "$configVar has not been configured." ); } @@ -39,7 +39,7 @@ * @param Skin &$skin */ public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) { - $out->addModules( array( 'ext.eventLogging.subscriber' ) ); + $out->addModules( [ 'ext.eventLogging.subscriber' ] ); } /** @@ -76,15 +76,15 @@ $schemas = $extRegistry->getAttribute( 'EventLoggingSchemas' ) + $wgEventLoggingSchemas; - Hooks::run( 'EventLoggingRegisterSchemas', array( &$schemas ) ); + Hooks::run( 'EventLoggingRegisterSchemas', [ &$schemas ] ); - $modules = array(); + $modules = []; foreach ( $schemas as $schemaName => $rev ) { - $modules[ "schema.$schemaName" ] = array( + $modules[ "schema.$schemaName" ] = [ 'class' => 'ResourceLoaderSchemaModule', 'schema' => $schemaName, 'revision' => $rev, - ); + ]; } $resourceLoader->register( $modules ); } @@ -106,12 +106,12 @@ * @return bool */ public static function onResourceLoaderTestModules( &$testModules, &$resourceLoader ) { - $testModules[ 'qunit' ][ 'ext.eventLogging.tests' ] = array( - 'scripts' => array( 'tests/ext.eventLogging.tests.js' ), - 'dependencies' => array( 'ext.eventLogging' ), + $testModules[ 'qunit' ][ 'ext.eventLogging.tests' ] = [ + 'scripts' => [ 'tests/ext.eventLogging.tests.js' ], + 'dependencies' => [ 'ext.eventLogging' ], 'localBasePath' => __DIR__ . '/..', 'remoteExtPath' => 'EventLogging', - ); + ]; return true; } } diff --git a/includes/JsonSchema.i18n.php b/includes/JsonSchema.i18n.php index 34afe65..464d678 100644 --- a/includes/JsonSchema.i18n.php +++ b/includes/JsonSchema.i18n.php @@ -10,12 +10,12 @@ * * This shim maintains compatibility back to MediaWiki 1.17. */ -$messages = array(); +$messages = []; if ( !function_exists( 'wfJsonI18nShim081e52cd9a576eeb0' ) ) { function wfJsonI18nShim081e52cd9a576eeb0( $cache, $code, &$cachedData ) { - $codeSequence = array_merge( array( $code ), $cachedData['fallbackSequence'] ); + $codeSequence = array_merge( [ $code ], $cachedData['fallbackSequence'] ); foreach ( $codeSequence as $csCode ) { - $fileName = dirname( __FILE__ ) . "/../i18n/$csCode.json"; + $fileName = __DIR__ . "/../i18n/$csCode.json"; if ( is_readable( $fileName ) ) { $data = FormatJson::decode( file_get_contents( $fileName ), true ); foreach ( array_keys( $data ) as $key ) { diff --git a/includes/JsonSchema.php b/includes/JsonSchema.php index 41152b0..5d65cd0 100644 --- a/includes/JsonSchema.php +++ b/includes/JsonSchema.php @@ -56,9 +56,7 @@ public static function stringToId( $var ) { if ( is_int( $var ) ) { return (string)$var; - } - - elseif ( is_string( $var ) ) { + } elseif ( is_string( $var ) ) { return preg_replace( '/[^a-z0-9\-_:\.]/i', '', $var ); } else { $msg = JsonUtil::uiMessage( 'jsonschema-idconvert', JsonUtil::encodeForMsg( $var ) ); @@ -87,12 +85,12 @@ * value for that type. */ public static function getNewValueForType( $thistype ) { - switch( $thistype ) { + switch ( $thistype ) { case 'object': - $newvalue = array(); + $newvalue = []; break; case 'array': - $newvalue = array(); + $newvalue = []; break; case 'number': case 'integer': @@ -120,7 +118,7 @@ return null; } - switch( gettype( $foo ) ) { + switch ( gettype( $foo ) ) { case "array": $retval = "array"; foreach ( array_keys( $foo ) as $key ) { @@ -151,18 +149,18 @@ * Generate a schema from a data example ($parent) */ public static function getSchemaArray( $parent ) { - $schema = array(); + $schema = []; $schema['type'] = JsonUtil::getType( $parent ); switch ( $schema['type'] ) { case 'object': - $schema['properties'] = array(); + $schema['properties'] = []; foreach ( $parent as $name ) { $schema['properties'][$name] = JsonUtil::getSchemaArray( $parent[$name] ); } break; case 'array': - $schema['items'] = array(); + $schema['items'] = []; $schema['items'][0] = JsonUtil::getSchemaArray( $parent[0] ); break; } @@ -175,10 +173,9 @@ * Note: this merely acts as a passthrough to MediaWiki's wfMessage call. */ public static function uiMessage() { - if( function_exists( 'wfMessage' ) ) { + if ( function_exists( 'wfMessage' ) ) { return call_user_func_array( 'wfMessage', $params = func_get_args() ); - } - else { + } else { // TODO: replace this with a real solution that works without // MediaWiki $params = func_get_args(); @@ -186,7 +183,6 @@ } } } - /* * Internal terminology: @@ -220,14 +216,15 @@ */ class JsonTreeRef { - public function __construct( $node, $parent = null, $nodeindex = null, $nodename = null, $schemaref = null ) { + public function __construct( $node, $parent = null, $nodeindex = null, + $nodename = null, $schemaref = null ) { $this->node = $node; $this->parent = $parent; $this->nodeindex = $nodeindex; $this->nodename = $nodename; $this->schemaref = $schemaref; $this->fullindex = $this->getFullIndex(); - $this->datapath = array(); + $this->datapath = []; if ( !is_null( $schemaref ) ) { $this->attachSchema(); } @@ -242,8 +239,7 @@ $this->nodename = isset( $schema['title'] ) ? $schema['title'] : "Root node"; $this->schemaref = $this->schemaindex->newRef( $schema, null, null, $this->nodename ); - } - elseif ( !is_null( $this->parent ) ) { + } elseif ( !is_null( $this->parent ) ) { $this->schemaindex = $this->parent->schemaindex; } } @@ -280,10 +276,9 @@ * infer it from the data. */ public function getType() { - if( array_key_exists( 'type', $this->schemaref->node ) ) { + if ( array_key_exists( 'type', $this->schemaref->node ) ) { $nodetype = $this->schemaref->node['type']; - } - else { + } else { $nodetype = 'any'; } @@ -318,7 +313,7 @@ */ public function getDataPath() { if ( !is_object( $this->parent ) ) { - return array(); + return []; } else { $retval = $this->parent->getDataPath(); $retval[] = $this->nodeindex; @@ -335,7 +330,7 @@ */ public function getDataPathAsString() { $retval = ""; - foreach( $this->getDataPath() as $item ) { + foreach ( $this->getDataPath() as $item ) { $retval .= '[' . json_encode( $item ) . ']'; } return $retval; @@ -359,14 +354,13 @@ */ public function getMappingChildRef( $key ) { $snode = $this->schemaref->node; - $schemadata = array(); + $schemadata = []; $nodename = $key; - if( array_key_exists( 'properties', $snode ) && + if ( array_key_exists( 'properties', $snode ) && array_key_exists( $key, $snode['properties'] ) ) { $schemadata = $snode['properties'][$key]; $nodename = isset( $schemadata['title'] ) ? $schemadata['title'] : $key; - } - elseif ( array_key_exists( 'additionalProperties', $snode ) ) { + } elseif ( array_key_exists( 'additionalProperties', $snode ) ) { // additionalProperties can *either* be a boolean or can be // defined as a schema (an object) if ( gettype( $snode['additionalProperties'] ) == "boolean" ) { @@ -375,8 +369,7 @@ $key, $this->getDataPathTitles() ); throw new JsonSchemaException( $msg ); } - } - else { + } else { $schemadata = $snode['additionalProperties']; $nodename = $key; } @@ -392,11 +385,10 @@ */ public function getSequenceChildRef( $i ) { // TODO: make this conform to draft-03 by also allowing single object - if( array_key_exists( 'items', $this->schemaref->node ) ) { + if ( array_key_exists( 'items', $this->schemaref->node ) ) { $schemanode = $this->schemaref->node['items'][0]; - } - else { - $schemanode = array(); + } else { + $schemanode = []; } $itemname = isset( $schemanode['title'] ) ? $schemanode['title'] : "Item"; $nodename = $itemname . " #" . ( (string)$i + 1 ); @@ -410,12 +402,13 @@ * Return true on success, and throw a JsonSchemaException on failure. */ public function validate() { - if( array_key_exists( 'enum', $this->schemaref->node ) && - !in_array( $this->node, $this->schemaref->node['enum'] ) ) { - $msg = JsonUtil::uiMessage( 'jsonschema-invalid-notinenum', JsonUtil::encodeForMsg( $this->node ), $this->getDataPathTitles() ); - $e = new JsonSchemaException( $msg ); - $e->subtype = "validate-fail"; - throw( $e ); + if ( array_key_exists( 'enum', $this->schemaref->node ) && + !in_array( $this->node, $this->schemaref->node['enum'] ) ) { + $msg = JsonUtil::uiMessage( 'jsonschema-invalid-notinenum', + JsonUtil::encodeForMsg( $this->node ), $this->getDataPathTitles() ); + $e = new JsonSchemaException( $msg ); + $e->subtype = "validate-fail"; + throw( $e ); } $datatype = JsonUtil::getType( $this->node ); $schematype = $this->getType(); @@ -424,19 +417,20 @@ $datatype = 'object'; } if ( $datatype == 'number' && $schematype == 'integer' && - $this->node == (int)$this->node) { + $this->node == (int)$this->node ) { // Alright, it'll work as an int $datatype = 'integer'; } if ( $datatype != $schematype ) { - if ( is_null( $datatype ) && !is_object( $this->parent )) { + if ( is_null( $datatype ) && !is_object( $this->parent ) ) { $msg = JsonUtil::uiMessage( 'jsonschema-invalidempty' ); $e = new JsonSchemaException( $msg ); $e->subtype = "validate-fail-null"; throw( $e ); } else { $datatype = is_null( $datatype ) ? "null" : $datatype; - $msg = JsonUtil::uiMessage( 'jsonschema-invalidnode', $schematype, $datatype, $this->getDataPathTitles() ); + $msg = JsonUtil::uiMessage( 'jsonschema-invalidnode', + $schematype, $datatype, $this->getDataPathTitles() ); $e = new JsonSchemaException( $msg ); $e->subtype = "validate-fail"; throw( $e ); @@ -459,7 +453,7 @@ if ( array_key_exists( 'properties', $this->schemaref->node ) ) { foreach ( $this->schemaref->node['properties'] as $skey => $svalue ) { $keyRequired = array_key_exists( 'required', $svalue ) ? $svalue['required'] : false; - if( $keyRequired && !array_key_exists( $skey, $this->node ) ) { + if ( $keyRequired && !array_key_exists( $skey, $this->node ) ) { $msg = JsonUtil::uiMessage( 'jsonschema-invalid-missingfield', $skey ); $e = new JsonSchemaException( $msg ); $e->subtype = "validate-fail-missingfield"; @@ -478,13 +472,13 @@ /* */ private function validateArrayChildren() { - for ( $i = 0; $i < count( $this->node ); $i++ ) { + $length = count( $this->node ); + for ( $i = 0; $i < $length; $i++ ) { $jsoni = $this->getSequenceChildRef( $i ); $jsoni->validate(); } } } - /* * The JsonSchemaIndex object holds all schema refs with an "id", and is used @@ -499,7 +493,7 @@ */ public function __construct( $schema ) { $this->root = $schema; - $this->idtable = array(); + $this->idtable = []; if ( is_null( $this->root ) ) { return null; @@ -513,11 +507,11 @@ * index. */ public function indexSubtree( $schemanode ) { - if( !array_key_exists( 'type', $schemanode ) ) { + if ( !array_key_exists( 'type', $schemanode ) ) { $schemanode['type'] = 'any'; } $nodetype = $schemanode['type']; - switch( $nodetype ) { + switch ( $nodetype ) { case 'object': foreach ( $schemanode['properties'] as $value ) { $this->indexSubtree( $value ); diff --git a/includes/JsonSchemaContent.php b/includes/JsonSchemaContent.php index 6f02553..8f7965a 100644 --- a/includes/JsonSchemaContent.php +++ b/includes/JsonSchemaContent.php @@ -37,7 +37,8 @@ * @param int $recursionLimit Maximum recursion limit * @return array Expanded schema object */ - public static function expand( $schema, $recursionLimit = JsonSchemaContent::DEFAULT_RECURSION_LIMIT ) { + public static function expand( $schema, + $recursionLimit = JsonSchemaContent::DEFAULT_RECURSION_LIMIT ) { return array_map( function ( $value ) use( $recursionLimit ) { if ( is_array( $value ) && $recursionLimit > 0 ) { if ( isset( $value['$ref'] ) ) { @@ -94,12 +95,12 @@ if ( !isset( $valParts[1] ) ) { $revId = $valParts[1]; $title = Revision::newFromId( $revId )->getTitle(); - $link = Linker::link( $title, htmlspecialchars( $val ), array(), - array( 'oldid' => $revId ) ); + $link = Linker::link( $title, htmlspecialchars( $val ), [], + [ 'oldid' => $revId ] ); - $th = Xml::elementClean( 'th', array(), $key ); - $td = Xml::tags( 'td', array( 'class' => 'value' ), $link ); - return Html::rawElement( 'tr', array(), $th . $td ); + $th = Xml::elementClean( 'th', [], $key ); + $td = Xml::tags( 'td', [ 'class' => 'value' ], $link ); + return Html::rawElement( 'tr', [], $th . $td ); } } @@ -115,23 +116,21 @@ * (message key), and code */ public function getCodeSamples( $dbKey, $revId ) { - return array( - array( + return [ + [ 'language' => 'php', 'header' => 'eventlogging-code-sample-logging-on-server-side', 'code' => "EventLogging::logEvent( '$dbKey', $revId, \$event );", - ), - array( + ], [ 'language' => 'php', 'header' => 'eventlogging-code-sample-module-setup', 'code' => "\$wgEventLoggingSchemas[ '{$dbKey}' ] = {$revId};", - ), - array( + ], [ 'language' => 'javascript', 'header' => 'eventlogging-code-sample-logging-on-client-side', 'code' => "mw.eventLog.logEvent( '{$dbKey}', { /* ... */ } );", - ), - ); + ], + ]; } /** @@ -161,15 +160,15 @@ $code = $sample['code']; $highlighted = $highlighter->highlight( $code, $lang )->getValue(); $html .= Html::element( 'h2', - array(), + [], wfMessage( $sample['header'] )->text() ) . $highlighted; } // The glyph is '< >' from the icon font 'Entypo' (see ../modules). - $html = Xml::tags( 'div', array( 'class' => 'mw-json-schema-code-glyph' ), '' ) . - Xml::tags( 'div', array( 'class' => 'mw-json-schema-code-samples' ), $html ); + $html = Xml::tags( 'div', [ 'class' => 'mw-json-schema-code-glyph' ], '' ) . + Xml::tags( 'div', [ 'class' => 'mw-json-schema-code-samples' ], $html ); $out->setIndicator( 'schema-code-samples', $html ); - $out->addModules( array( 'ext.eventLogging.jsonSchema', 'ext.pygments' ) ); + $out->addModules( [ 'ext.eventLogging.jsonSchema', 'ext.pygments' ] ); $out->addModuleStyles( 'ext.eventLogging.jsonSchema.styles' ); } diff --git a/includes/JsonSchemaContentHandler.php b/includes/JsonSchemaContentHandler.php index 04cfba4..b1d8a2d 100644 --- a/includes/JsonSchemaContentHandler.php +++ b/includes/JsonSchemaContentHandler.php @@ -12,10 +12,10 @@ class JsonSchemaContentHandler extends JsonContentHandler { public function __construct( $modelId = 'JsonSchema' ) { - parent::__construct( $modelId, array( CONTENT_FORMAT_JSON ) ); + parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] ); } - public function canBeUsedOn ( Title $title ) { + public function canBeUsedOn( Title $title ) { return $title->inNamespace( NS_SCHEMA ); } diff --git a/includes/RemoteSchema.php b/includes/RemoteSchema.php index d4e3588..5251094 100644 --- a/includes/RemoteSchema.php +++ b/includes/RemoteSchema.php @@ -15,7 +15,6 @@ public $key; public $content = false; - /** * Constructor. * @param string $title @@ -23,7 +22,7 @@ * @param ObjectCache $cache (optional) cache client. * @param Http $http (optional) HTTP client. */ - public function __construct( $title, $revision, $cache = NULL, $http = NULL ) { + public function __construct( $title, $revision, $cache = null, $http = null ) { global $wgEventLoggingDBname; $this->title = $title; @@ -60,12 +59,11 @@ * @implements JsonSerializable */ public function jsonSerialize() { - return array( + return [ 'schema' => $this->get() ?: new StdClass(), 'revision' => $this->revision - ); + ]; } - /** * Retrieves content from memcached. @@ -75,14 +73,12 @@ return $this->cache->get( $this->key ); } - /** * Store content in memcached. */ protected function memcSet() { return $this->cache->set( $this->key, $this->content ); } - /** * Retrieves the schema using HTTP. @@ -94,16 +90,15 @@ return false; } $uri = $this->getUri(); - $raw = $this->http->get( $uri, array( + $raw = $this->http->get( $uri, [ 'timeout' => self::LOCK_TIMEOUT * 0.8 - ) ); + ] ); $content = FormatJson::decode( $raw, true ); if ( !$content ) { wfDebugLog( 'EventLogging', "Request to $uri failed." ); } return $content ?: false; } - /** * Acquire a mutex lock for HTTP retrieval. @@ -113,7 +108,6 @@ return $this->cache->add( $this->key . ':lock', 1, self::LOCK_TIMEOUT ); } - /** * Constructs URI for retrieving schema from remote wiki. * @return string URI. @@ -121,10 +115,10 @@ protected function getUri() { global $wgEventLoggingSchemaApiUri; - return wfAppendQuery( $wgEventLoggingSchemaApiUri, array( + return wfAppendQuery( $wgEventLoggingSchemaApiUri, [ 'action' => 'jsonschema', 'revid' => $this->revision, 'formatversion' => 2, - ) ); + ] ); } } diff --git a/includes/ResourceLoaderSchemaModule.php b/includes/ResourceLoaderSchemaModule.php index 38b4a51..d86a05f 100644 --- a/includes/ResourceLoaderSchemaModule.php +++ b/includes/ResourceLoaderSchemaModule.php @@ -37,7 +37,7 @@ * @param array $args */ function __construct( $args ) { - foreach( array( 'schema', 'revision' ) as $key ) { + foreach ( [ 'schema', 'revision' ] as $key ) { if ( !isset( $args[ $key ] ) ) { throw new Exception( "ResourceLoaderSchemaModule params must set '$key' key." ); } @@ -51,7 +51,7 @@ } $this->schema = new RemoteSchema( $args['schema'], $args['revision'] ); - $this->targets = array( 'desktop', 'mobile' ); + $this->targets = [ 'desktop', 'mobile' ]; } /** @@ -59,7 +59,7 @@ * @return array Module names */ public function getDependencies( ResourceLoaderContext $context = null ) { - return array( 'ext.eventLogging' ); + return [ 'ext.eventLogging' ]; } /** @@ -70,9 +70,9 @@ */ public function getDefinitionSummary( ResourceLoaderContext $context ) { $summary = parent::getDefinitionSummary( $context ); - $summary[] = array( + $summary[] = [ 'revision' => $this->schema->revision, - ); + ]; return $summary; } @@ -87,7 +87,7 @@ public function getScript( ResourceLoaderContext $context ) { $schema = $this->schema->jsonSerialize(); efStripKeyRecursive( $schema, 'description' ); - $params = array( $this->schema->title, $schema ); + $params = [ $this->schema->title, $schema ]; return Xml::encodeJsCall( 'mediaWiki.eventLog.declareSchema', $params ); } } diff --git a/tests/EventLoggingExtensionFunctionsTest.php b/tests/EventLoggingExtensionFunctionsTest.php index 97e59f2..b41f6a0 100644 --- a/tests/EventLoggingExtensionFunctionsTest.php +++ b/tests/EventLoggingExtensionFunctionsTest.php @@ -16,15 +16,15 @@ class EventLoggingExtensionFunctionsTest extends MediaWikiTestCase { /** @var array: a basic JSON schema, decoded to associative array. **/ - static $validSchema = array( - 'properties' => array( - 'valid' => array( + private static $validSchema = [ + 'properties' => [ + 'valid' => [ 'type' => 'boolean', 'required' => true, - ), - 'action' => array( + ], + 'action' => [ 'type' => 'string', - 'enum' => array( + 'enum' => [ 'delete', 'edit', 'history', @@ -32,19 +32,18 @@ 'purge', 'submit', 'view', - ), - ), - ) - ); + ], + ], + ] + ]; /** @var array: conforms to $validSchema. **/ - static $validObject = array( 'valid' => true, 'action' => 'history' ); + private static $validObject = [ 'valid' => true, 'action' => 'history' ]; /** @var array: does not conform to $validSchema. **/ - static $invalidObject = array( 'valid' => true, 'action' => 'cache' ); + private static $invalidObject = [ 'valid' => true, 'action' => 'cache' ]; const UGLY_JSON = '{"nested":{"value":"{}"}}'; - /** * Tests validation of objects against schema. @@ -62,7 +61,6 @@ $this->assertTrue( efSchemaValidate( self::$validSchema ), 'efSchemaValidate() defaults to validating against the schema schema.' ); } - /** * Tests invalidation of objects that deviate from schema. diff --git a/tests/JsonSchemaTest.php b/tests/JsonSchemaTest.php index 972d74f..c0b8848 100644 --- a/tests/JsonSchemaTest.php +++ b/tests/JsonSchemaTest.php @@ -19,7 +19,6 @@ const VALID_JSON_SCHEMA = '{"properties":{"valid":{"type":"boolean","required":true}}}'; const EVIL_JSON = '{"title":"<script>alert(document.cookie);</script>"}'; - /** * Tests handling of invalid JSON. * @covers JsonSchemaContent::isValid @@ -28,7 +27,6 @@ $content = new JsonSchemaContent( self::INVALID_JSON ); $this->assertFalse( $content->isValid(), 'Malformed JSON should be detected.' ); } - /** * Tests handling of valid JSON that is not valid JSON Schema. @@ -39,7 +37,6 @@ $this->assertFalse( $content->isValid(), 'Malformed JSON Schema should be detected.' ); } - /** * Tests successful validation of well-formed JSON Schema. * @covers JsonSchemaContent::isValid @@ -48,7 +45,6 @@ $content = new JsonSchemaContent( self::VALID_JSON_SCHEMA ); $this->assertTrue( $content->isValid(), 'Valid JSON Schema should be recognized as valid.' ); } - /** * Tests JSON pretty-printing. @@ -66,7 +62,6 @@ 'Beautification does not alter JSON value.' ); } - /** * Tests JSON->HTML representation. diff --git a/tests/RemoteSchemaTest.php b/tests/RemoteSchemaTest.php index 61ccf2b..be9c877 100644 --- a/tests/RemoteSchemaTest.php +++ b/tests/RemoteSchemaTest.php @@ -21,7 +21,7 @@ /** @var RemoteSchema */ private $schema; - public $statusSchema = array( 'status' => array( 'type' => 'string' ) ); + public $statusSchema = [ 'status' => [ 'type' => 'string' ] ]; function setUp() { parent::setUp(); @@ -31,10 +31,9 @@ ->disableOriginalConstructor() ->getMock(); - $this->http = $this->getMock( 'stdClass', array( 'get' ) ); + $this->http = $this->getMock( 'stdClass', [ 'get' ] ); $this->schema = new RemoteSchema( 'Test', 99, $this->cache, $this->http ); } - /** * Tests behavior when content is in memcached. @@ -63,7 +62,6 @@ $this->assertEquals( $this->statusSchema, $this->schema->get() ); } - /** * Calling get() multiple times should not result in multiple * memcached calls; instead, once the content is retrieved, it @@ -79,7 +77,6 @@ $this->schema->get(); $this->schema->get(); } - /** * Tests behavior when content is missing from memcached and has to @@ -109,15 +106,14 @@ ->method( 'get' ) ->with( $this->stringContains( '?' ), - $this->equalTo( array( + $this->equalTo( [ 'timeout' => RemoteSchema::LOCK_TIMEOUT * 0.8 - ) ) + ] ) ) ->will( $this->returnValue( FormatJson::encode( $this->statusSchema ) ) ); $this->assertEquals( $this->statusSchema, $this->schema->get() ); } - /** * Tests behavior when content is missing from memcached and an diff --git a/tests/ResourceLoaderSchemaModuleTest.php b/tests/ResourceLoaderSchemaModuleTest.php index c6e1aff..81ba385 100644 --- a/tests/ResourceLoaderSchemaModuleTest.php +++ b/tests/ResourceLoaderSchemaModuleTest.php @@ -34,19 +34,18 @@ function getMockSchemaModule( $title, $revid ) { $schema = $this ->getMockBuilder( 'RemoteSchema' ) - ->setConstructorArgs( array( $title, $revid ) ) + ->setConstructorArgs( [ $title, $revid ] ) ->getMock(); - $module = new ResourceLoaderSchemaModule( array( + $module = new ResourceLoaderSchemaModule( [ 'schema' => $title, 'revision' => $revid - ) ); + ] ); // Inject mock $module->schema = $schema; return $module; } - /** * When the RemoteSchema dependency can be loaded, the modified time @@ -54,8 +53,6 @@ * @covers ResourceLoaderSchemaModule::getModifiedTime */ function testModuleVersion() { - global $wgCacheEpoch; - $version1 = $this->module->getVersionHash( $this->context ); $module2 = self::getMockSchemaModule( self::TITLE, self::REV + 1 ); diff --git a/tests/SerializeEventTest.php b/tests/SerializeEventTest.php index 0f30e81..2f55f14 100644 --- a/tests/SerializeEventTest.php +++ b/tests/SerializeEventTest.php @@ -19,10 +19,10 @@ * Empty event should be returned as an object. **/ function testSerializeEventEmptyEvent() { - $encapsulatedEvent = array( - 'event' => array(), + $encapsulatedEvent = [ + 'event' => [], 'other' => 'some', - ); + ]; $expectedJson = "{\"event\":{},\"other\":\"some\"}"; $json = EventLogging::serializeEvent( $encapsulatedEvent ); $this->assertEquals( $expectedJson, $json, @@ -34,12 +34,12 @@ * Event should be returned without modifications **/ function testSerializeEventHappyCase() { - $event = array(); + $event = []; $event['prop1'] = 'blah'; - $encapsulatedEvent = array( + $encapsulatedEvent = [ 'event' => $event, 'other' => 'some', - ); + ]; $expectedJson = "{\"event\":{\"prop1\":\"blah\"},\"other\":\"some\"}"; $json = EventLogging::serializeEvent( $encapsulatedEvent ); $this->assertEquals( $expectedJson, $json, diff --git a/tests/ValidateSchemaTest.php b/tests/ValidateSchemaTest.php index 69a1e44..a1a27c0 100644 --- a/tests/ValidateSchemaTest.php +++ b/tests/ValidateSchemaTest.php @@ -138,7 +138,7 @@ **/ function testEventNonMandatoryProperties() { $valid = efSchemaValidate( - json_decode( '{"Happy": "true"}', true ), + json_decode( '{"Happy": "true"}', true ), json_decode( self::VALID_JSON_SCHEMA_NON_MANDATORY_EVENT_PROPERTIES, true ) ); $this->assertTrue( $valid, 'Event with non mandatory properties validates' ); @@ -170,10 +170,10 @@ * Empty event like [] should not validate if event has mandatory properties * Test failing -> prefixing with "_" * bug : https://bugzilla.wikimedia.org/show_bug.cgi?id=65607 - + * * @expectedException JsonSchemaException **/ - function _testEmptyEventForEventWithMandatoryPropertiesIsNotValidBadSerialization() { + function testEmptyEventForEventWithMandatoryPropertiesIsNotValidBadSerialization() { $valid = efSchemaValidate( json_decode( '[]', true ), json_decode( self::VALID_JSON_SCHEMA_MANDATORY_EVENT_PROPERTIES, true ) @@ -186,7 +186,7 @@ * @covers efSchemaValidate * @expectedException JsonSchemaException **/ - function _testEmptyEventForEventWithMandatoryPropertiesIsNotValid() { + function testEmptyEventForEventWithMandatoryPropertiesIsNotValid() { $valid = efSchemaValidate( json_decode( '{}', true ), json_decode( self::VALID_JSON_SCHEMA_MANDATORY_EVENT_PROPERTIES, true ) -- To view, visit https://gerrit.wikimedia.org/r/274631 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8ffa9bc9ed0f5e8d8f91a295ca32a006f9f072de Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/EventLogging Gerrit-Branch: master Gerrit-Owner: Ori.livneh <o...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits