Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/307038

Change subject: Get rid of JsonUtil::uiMessage(), pass code to exception 
directly
......................................................................

Get rid of JsonUtil::uiMessage(), pass code to exception directly

JsonUtil::uiMessage() was always a hack that pretended not to rely upon
MediaWiki. Instead, just pass the message and arguments to the
exception, and leave it up to the caller to handle displaying them.

Change-Id: I9e8d2da23109080b76fd60c8fce30d5702eb8209
---
M includes/JsonSchema.php
M includes/JsonSchemaContent.php
M includes/JsonSchemaHooks.php
3 files changed, 28 insertions(+), 34 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventLogging 
refs/changes/38/307038/1

diff --git a/includes/JsonSchema.php b/includes/JsonSchema.php
index 0a32df0..9194d1f 100644
--- a/includes/JsonSchema.php
+++ b/includes/JsonSchema.php
@@ -44,8 +44,25 @@
  */
 
 class JsonSchemaException extends Exception {
+
+       /**
+        * Arguments for the message
+        *
+        * @var array
+        */
+       public $args;
+
+       /**
+        * @var string 'validate-fail' or 'validate-fail-null'
+        */
        public $subtype;
-       // subtypes: "validate-fail", "validate-fail-null"
+
+       public function __construct( $code /* ... */ ) {
+               parent::__construct( $code );
+               $this->code = $code;
+               $this->args = func_get_args();
+               array_shift( $this->args );
+       }
 }
 
 class JsonUtil {
@@ -59,8 +76,7 @@
                } elseif ( is_string( $var ) ) {
                        return preg_replace( '/[^a-z0-9\-_:\.]/i', '', $var );
                } else {
-                       $msg = JsonUtil::uiMessage( 'jsonschema-idconvert', 
JsonUtil::encodeForMsg( $var ) );
-                       throw new JsonSchemaException( $msg );
+                       throw new JsonSchemaException( 'jsonschema-idconvert', 
JsonUtil::encodeForMsg( $var ) );
                }
 
        }
@@ -166,21 +182,6 @@
                }
 
                return $schema;
-       }
-
-       /**
-        * User interface messages suitable for translation.
-        * Note: this merely acts as a passthrough to MediaWiki's wfMessage 
call.
-        */
-       public static function uiMessage() {
-               if ( function_exists( 'wfMessage' ) ) {
-                       return call_user_func_array( 'wfMessage', $params = 
func_get_args() );
-               } else {
-                       // TODO: replace this with a real solution that works 
without
-                       // MediaWiki
-                       $params = func_get_args();
-                       return implode( " ", $params );
-               }
        }
 }
 
@@ -363,9 +364,8 @@
                        // defined as a schema (an object)
                        if ( gettype( $snode['additionalProperties'] ) == 
"boolean" ) {
                                if ( !$snode['additionalProperties'] ) {
-                                       $msg = JsonUtil::uiMessage( 
'jsonschema-invalidkey',
+                                       throw new JsonSchemaException( 
'jsonschema-invalidkey',
                                                                                
                $key, $this->getDataPathTitles() );
-                                       throw new JsonSchemaException( $msg );
                                }
                        } else {
                                $schemadata = $snode['additionalProperties'];
@@ -402,9 +402,8 @@
        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',
+                       $e = new JsonSchemaException( 
'jsonschema-invalid-notinenum',
                                JsonUtil::encodeForMsg( $this->node ), 
$this->getDataPathTitles() );
-                       $e = new JsonSchemaException( $msg );
                        $e->subtype = "validate-fail";
                        throw( $e );
                }
@@ -421,15 +420,13 @@
                }
                if ( $datatype != $schematype ) {
                        if ( is_null( $datatype ) && !is_object( $this->parent 
) ) {
-                               $msg = JsonUtil::uiMessage( 
'jsonschema-invalidempty' );
-                               $e = new JsonSchemaException( $msg );
+                               $e = new JsonSchemaException( 
'jsonschema-invalidempty' );
                                $e->subtype = "validate-fail-null";
                                throw( $e );
                        } else {
                                $datatype = is_null( $datatype ) ? "null" : 
$datatype;
-                               $msg = JsonUtil::uiMessage( 
'jsonschema-invalidnode',
+                               $e = new JsonSchemaException( 
'jsonschema-invalidnode',
                                        $schematype, $datatype, 
$this->getDataPathTitles() );
-                               $e = new JsonSchemaException( $msg );
                                $e->subtype = "validate-fail";
                                throw( $e );
                        }
@@ -452,8 +449,7 @@
                        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 ) ) {
-                                       $msg = JsonUtil::uiMessage( 
'jsonschema-invalid-missingfield', $skey );
-                                       $e = new JsonSchemaException( $msg );
+                                       $e = new JsonSchemaException( 
'jsonschema-invalid-missingfield', $skey );
                                        $e->subtype = 
"validate-fail-missingfield";
                                        throw( $e );
                                }
@@ -535,16 +531,14 @@
        public function newRef( $node, $parent, $nodeindex, $nodename ) {
                if ( array_key_exists( '$ref', $node ) ) {
                        if ( strspn( $node['$ref'], '#' ) != 1 ) {
-                               $error = JsonUtil::uiMessage( 
'jsonschema-badidref', $node['$ref'] );
-                               throw new JsonSchemaException( $error );
+                               throw new JsonSchemaException( 
'jsonschema-badidref', $node['$ref'] );
                        }
                        $idref = $node['$ref'];
                        try {
                                $node = $this->idtable[$idref];
                        }
                        catch ( Exception $e ) {
-                               $error = JsonUtil::uiMessage( 
'jsonschema-badidref', $node['$ref'] );
-                               throw new JsonSchemaException( $error );
+                               throw new JsonSchemaException( 
'jsonschema-badidref', $node['$ref'] );
                        }
                }
 
diff --git a/includes/JsonSchemaContent.php b/includes/JsonSchemaContent.php
index 05deaea..6fecfa9 100644
--- a/includes/JsonSchemaContent.php
+++ b/includes/JsonSchemaContent.php
@@ -66,7 +66,7 @@
        public function validate() {
                $schema = $this->getJsonData();
                if ( !is_array( $schema ) ) {
-                       throw new JsonSchemaException( wfMessage( 
'eventlogging-invalid-json' )->parse() );
+                       throw new JsonSchemaException( 
'eventlogging-invalid-json' );
                }
                return EventLogging::schemaValidate( $schema );
        }
diff --git a/includes/JsonSchemaHooks.php b/includes/JsonSchemaHooks.php
index 273fe0f..7ed2e9c 100644
--- a/includes/JsonSchemaHooks.php
+++ b/includes/JsonSchemaHooks.php
@@ -83,7 +83,7 @@
                try {
                        $content->validate();
                } catch ( JsonSchemaException $e ) {
-                       $error = $e->getMessage();
+                       $error = wfMessage( $e->getCode(), $e->args )->parse();
                }
 
                return true;

-- 
To view, visit https://gerrit.wikimedia.org/r/307038
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9e8d2da23109080b76fd60c8fce30d5702eb8209
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to