jenkins-bot has submitted this change and it was merged.
Change subject: Add multilingual and minor util support
......................................................................
Add multilingual and minor util support
Change-Id: I45a8c3faa2b3878cf3cbdc1b8eb47e1f6844a00d
---
M i18n/en.json
M i18n/qqq.json
M includes/JCUtils.php
M includes/JCValidators.php
4 files changed, 101 insertions(+), 3 deletions(-)
Approvals:
MaxSem: Looks good to me, approved
jenkins-bot: Verified
diff --git a/i18n/en.json b/i18n/en.json
index 0fac117..2f97e6d 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -11,6 +11,8 @@
"jsonconfig-err-assoc-array": "Parameter \"$1\" must be an associative
array (dictionary), e.g. {'key': 'value', ...}",
"jsonconfig-err-bool": "Parameter \"$1\" must be either set to true or
false",
"jsonconfig-err-integer": "Parameter \"$1\" must be an integer",
+ "jsonconfig-err-number": "Parameter \"$1\" must be a number",
+ "jsonconfig-err-localized": "Parameter \"$1\" must be an object that
maps valid language codes to strings, e.g. { \"en\":\"String in English\", ...
}",
"jsonconfig-err-array-expected": "The value at \"$1\" was expected to
be a list surrounded by the [...] brackets",
"jsonconfig-err-object-expected": "The value at \"$1\" was expected to
be an object surrounded by the {...} braces",
"jsonconfig-err-root-array-expected": "JSON data should be a list,
surrounded by the [...] brackets",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 836a1ee..5a15627 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -17,6 +17,8 @@
"jsonconfig-err-assoc-array": "A field named \"$1\" is not a valid
associative array (not a dictionary). Parameters:\n* $1 - field name",
"jsonconfig-err-bool": "A field named \"$1\" is not a valid boolean.
Parameters:\n* $1 - parameter name",
"jsonconfig-err-integer": "A field named \"$1\" is not a valid integer.
Parameters:\n* $1 - field name",
+ "jsonconfig-err-number": "A field named \"$1\" is not a valid number.
Parameters:\n* $1 - field name",
+ "jsonconfig-err-localized": "A field named \"$1\" is not a valid
localized string (string available in multiple languages). Parameters:\n* $1 -
field name.",
"jsonconfig-err-array-expected": "The field named \"$1\" is not a valid
list",
"jsonconfig-err-object-expected": "The field named \"$1\" is not a
valid object",
"jsonconfig-err-root-array-expected": "Entire JSON data was not a list
surrounded by the [...] brackets",
diff --git a/includes/JCUtils.php b/includes/JCUtils.php
index 134bec6..9317cd4 100644
--- a/includes/JCUtils.php
+++ b/includes/JCUtils.php
@@ -4,6 +4,7 @@
use FormatJson;
use Exception;
+use Language;
use MWHttpRequest;
use stdClass;
@@ -219,4 +220,43 @@
}
return $result;
}
+
+
+ /**
+ * Returns true if each of the array's values is a valid language code
+ * @param array $arr
+ * @return bool
+ */
+ public static function isListOfLangs( $arr ) {
+ return count( $arr ) === count( array_filter( $arr, function (
$v ) {
+ return Language::isValidCode( $v );
+ } ) );
+ }
+
+ /**
+ * Find a message in a dictionary for the given language,
+ * or use language fallbacks if message is not defined.
+ * @param stdClass $map Dictionary of languageCode => string
+ * @param Language $lang language object
+ * @return string message from the dictionary or "" if nothing found
+ */
+ public static function pickLocalizedString( stdClass $map, $lang ) {
+ $langCode = $lang->getCode();
+ if ( property_exists( $map, $langCode ) ) {
+ return $map->$langCode;
+ }
+ foreach ( $lang->getFallbackLanguages() as $l ) {
+ if ( property_exists( $map, $l ) ) {
+ return $map->$l;
+ }
+ }
+ // If fallbacks fail, check if english is defined
+ if ( property_exists( $map, 'en' ) ) {
+ return $map->en;
+ }
+ // Return first available value, or an empty string
+ // There might be a better way to get the first value from an
object
+ $map = (array)$map;
+ return reset( $map ) ? : '';
+ }
}
diff --git a/includes/JCValidators.php b/includes/JCValidators.php
index 343a821..7bcb42c 100644
--- a/includes/JCValidators.php
+++ b/includes/JCValidators.php
@@ -1,5 +1,6 @@
<?php
namespace JsonConfig;
+use Closure;
/**
* Class JCValidators contains various static validation functions
@@ -66,6 +67,19 @@
};
}
+ /** Returns a validator function to check if the value is a valid
integer
+ * @return callable
+ */
+ public static function isNumber() {
+ return function ( JCValue $v, array $path ) {
+ if ( !is_double( $v->getValue() ) && !is_int(
$v->getValue() ) ) {
+ $v->error( 'jsonconfig-err-number', $path );
+ return false;
+ }
+ return true;
+ };
+ }
+
/** Returns a validator function to check if the value is an
non-associative array (list)
* @return callable
*/
@@ -106,13 +120,16 @@
}
/** Returns a validator function that will substitute missing value
with default
- * @param mixed $default value to use in case field is not present
+ * @param mixed $default value to use in case field is not present, or
a closure function to generate that value
* @param bool $validateDefault if true, the default value will be
verified by the validators
* @return callable
*/
public static function useDefault( $default, $validateDefault = true ) {
return function ( JCValue $v ) use ( $default, $validateDefault
) {
if ( $v->isMissing() ) {
+ if ( is_object( $default ) && ( $default
instanceof Closure ) ) {
+ $default = $default();
+ }
$v->setValue( $default );
return $validateDefault;
}
@@ -131,12 +148,12 @@
};
}
- /** Returns a validator function that will wraps a string value into an
array
+ /** Returns a validator function that will wrap a string value into an
array
* @return callable
*/
public static function stringToList() {
return function ( JCValue $v ) {
- if ( !$v->isMissing() && is_string( $v->getValue() ) ) {
+ if ( is_string( $v->getValue() ) ) {
$v->setValue( array( $v->getValue() ) );
}
return true;
@@ -156,4 +173,41 @@
return true;
};
}
+
+ public static function isLocalizedString() {
+ return function ( JCValue $jcv, array $path ) {
+ if ( $jcv->isMissing() ) {
+ $v = array();
+ } else {
+ $v = $jcv->getValue();
+ if ( is_object( $v ) ) {
+ $v = (array)$v;
+ }
+ }
+ if ( is_array( $v ) ) {
+ if ( JCUtils::isListOfLangs( array_keys( $v ) )
&&
+ JCUtils::allValuesAreStrings( $v )
+ ) {
+ // Sort array so that the values are
sorted alphabetically,
+ // except 'en' which will be shown first
+ uksort( $v,
+ function ( $a, $b ) {
+ if ( $a === $b ) {
+ return 0;
+ } elseif ( $a === 'en'
) {
+ return -1;
+ } elseif ( $b === 'en'
) {
+ return 1;
+ } else {
+ return
strcasecmp( $a, $b );
+ }
+ } );
+ $jcv->setValue( (object)$v );
+ return true;
+ }
+ }
+ $jcv->error( 'jsonconfig-err-localized', $path );
+ return false;
+ };
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/282972
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I45a8c3faa2b3878cf3cbdc1b8eb47e1f6844a00d
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/JsonConfig
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Yurik <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits