Yurik has uploaded a new change for review.

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

Change subject: WIP: implement string limiting
......................................................................

WIP: implement string limiting

Change-Id: Ib229c04a624a423d7073fdf13b52bca741510f4e
---
M i18n/en.json
M i18n/qqq.json
M includes/JCDataContent.php
M includes/JCValidators.php
4 files changed, 27 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/JsonConfig 
refs/changes/16/316516/1

diff --git a/i18n/en.json b/i18n/en.json
index 36d6113..43d5b33 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -19,6 +19,7 @@
        "jsonconfig-err-root-array-expected": "JSON data should be a list, 
surrounded by the [...] brackets",
        "jsonconfig-err-root-object-expected": "JSON data should be an object, 
surrounded by the {...} braces",
        "jsonconfig-err-string": "Parameter \"$1\" must be a string",
+       "jsonconfig-err-stringline": "Parameter \"$1\" must be a single line 
string without surrounding spaces and no longer than $2 characters",
        "jsonconfig-err-bad-header-string": "Header \"$1\" must only consist of 
letters, numbers, and underscore characters, and must not begin with a number",
        "jsonconfig-err-bad-type": "Parameter \"$1\" must be a valid type like 
\"string\", \"number\", or \"boolean\"",
        "jsonconfig-err-unique-strings": "Parameter \"$1\" must be a list of 
unique non-empty strings",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 0254459..c013423 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -27,6 +27,7 @@
        "jsonconfig-err-root-array-expected": "Entire JSON data was not a list 
surrounded by the [...] brackets",
        "jsonconfig-err-root-object-expected": "Entire JSON data was not an 
object, surrounded by the {...} braces",
        "jsonconfig-err-string": "A field named \"$1\" is not a valid string. 
Parameters:\n* $1 - field name",
+       "jsonconfig-err-stringline": "A field named \"$1\" is not a valid 
string. Parameters:\n* $1 - field name\n* $2 - maximum allowed string length",
        "jsonconfig-err-bad-header-string": "A field named \"$1\" must contain 
only the allowed characters. Parameters:\n* $1 - field name",
        "jsonconfig-err-bad-type": "A field named \"$1\" must be set to one of 
the allowed values. Parameters:\n* $1 - field name",
        "jsonconfig-err-unique-strings": "A list named \"$1\" must only contain 
non-empty strings that do not repeat. Parameters:\n* $1 - field name",
diff --git a/includes/JCDataContent.php b/includes/JCDataContent.php
index f9ae379..f8f3b8a 100644
--- a/includes/JCDataContent.php
+++ b/includes/JCDataContent.php
@@ -24,7 +24,7 @@
                }
 
                // TODO: handle well-known licenses and link to them
-               $this->test( 'license', JCValidators::isString() );
+               $this->test( 'license', JCValidators::isStringLine() );
                $this->test( 'info', JCValidators::isLocalizedString() );
        }
 
diff --git a/includes/JCValidators.php b/includes/JCValidators.php
index f3caa17..a112775 100644
--- a/includes/JCValidators.php
+++ b/includes/JCValidators.php
@@ -54,6 +54,24 @@
                };
        }
 
+       /** Returns a validator function to check if the value is a valid 
single line string
+        * @param int $maxlength maximum allowed string size
+        * @return callable
+        */
+       public static function isStringLine( $maxlength = 400 ) {
+               return function ( JCValue $v, array $path ) use ( $maxlength ) {
+                       $str = $v->getValue();
+                       if ( !is_string( $str ) ||
+                                mb_strlen( $str ) > $maxlength ||
+                                preg_match( "/^\s|[\r\n\t]|\s$/", $str )
+                       ) {
+                               $v->error( 'jsonconfig-err-stringline', $path, 
$maxlength );
+                               return false;
+                       }
+                       return true;
+               };
+       }
+
        /** Returns a validator function to check if the value is a valid 
integer
         * @return callable
         */
@@ -185,8 +203,11 @@
                                }
                        }
                        if ( is_array( $v ) ) {
-                               if ( JCUtils::isListOfLangs( array_keys( $v ) ) 
&&
-                                        JCUtils::allValuesAreStrings( $v )
+                               if ( is_array( $v ) &&
+                                        JCUtils::isListOfLangs( array_keys( $v 
) ) &&
+                                        count( array_filter( $v, 'is_string' ) 
) === count( $v );
+
+                               JCUtils::allValuesAreStrings( $v )
                                ) {
                                        // Sort array so that the values are 
sorted alphabetically,
                                        // except 'en' which will be shown first
@@ -270,7 +291,7 @@
                        if ( is_string( $value ) ) {
                                switch ( $value ) {
                                        case 'string':
-                                               $validator = 
JCValidators::isString();
+                                               $validator = 
JCValidators::isStringLine();
                                                break;
                                        case 'boolean':
                                                $validator = 
JCValidators::isBool();

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib229c04a624a423d7073fdf13b52bca741510f4e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/JsonConfig
Gerrit-Branch: master
Gerrit-Owner: Yurik <yu...@wikimedia.org>

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

Reply via email to