Yurik has uploaded a new change for review.
https://gerrit.wikimedia.org/r/176023
Change subject: Fix JsonEncode/Decode, error detection
......................................................................
Fix JsonEncode/Decode, error detection
* jsonDecode & jsonEncode - recodes 1-based to 0-based
* getRawParameter() - better parameter error detection
Change-Id: I1d7376358fa70805fc9dbb02f535eeb7ef4ec5ed
---
M includes/LuaLibrary.php
1 file changed, 42 insertions(+), 9 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ZeroPortal
refs/changes/23/176023/1
diff --git a/includes/LuaLibrary.php b/includes/LuaLibrary.php
index 116b312..0746080 100644
--- a/includes/LuaLibrary.php
+++ b/includes/LuaLibrary.php
@@ -80,6 +80,26 @@
}
}
+ private static function recurseValue( array $value, $isEncoding ) {
+ $isAssoc = false;
+ foreach ( $value as $k => &$val ) {
+ if ( is_array( $val ) ) {
+ $val = self::recurseValue( $val, $isEncoding );
+ }
+ $isIntKey = is_int( $k ) || is_string( $k ) && strval(
intval( $k ) ) === $k;
+ if ( !$isIntKey ) {
+ $isAssoc = true;
+ }
+ }
+ if ( $isAssoc ) {
+ return $value;
+ } elseif ( $isEncoding ) {
+ return array_values( $value );
+ } else {
+ return array_combine( range( 1, count( $value ) ),
$value );
+ }
+ }
+
public function register() {
$title = $this->getTitle();
if ( $title && $title->inNamespace( NS_SPECIAL ) &&
$title->getText() === 'ZeroPortal' ) {
@@ -138,13 +158,16 @@
/**
* Get a value from the request (GET or POST). Do not echo this value,
as it gets sent from the client
* @param string $name
- * @param mixed $default
- * @return mixed[]
+ * @param string|null $default
+ * @return string[]|null[]
* @throws Scribunto_LuaError
*/
public function getRawParameter( $name = null, $default = null ) {
$funcName = self::luaNamespace . __FUNCTION__;
$this->checkType( $funcName, 1, $name, 'string' );
+ if ( $default !== null ) {
+ $this->checkType( $funcName, 2, $default, 'string' );
+ }
if ( substr( $name, 0, 2 ) !== 'zp' ) {
throw new Scribunto_LuaError( "bad argument #1 to
'$funcName' (name must start with 'zp')" );
}
@@ -186,24 +209,34 @@
* Decode value as a JSON string.
* NOTE: this function uses FormatJson::decode() for simplicity sake.
* TODO: remove this function once Scribunto gets its own json
encode/decode
- * @param mixed $value
+ * @param string $value
+ * @param bool $preserveKeys
+ * @return \mixed[]
* @throws Scribunto_LuaError
- * @return mixed[]
*/
- public function jsonDecode( $value = null ) {
+ public function jsonDecode( $value = null, $preserveKeys = null ) {
$this->checkType( self::luaNamespace . __FUNCTION__, 1, $value,
'string' );
- return array( FormatJson::decode( $value, true ) );
+ $this->checkTypeOptional( self::luaNamespace . __FUNCTION__, 2,
$preserveKeys, 'boolean', false );
+ $val = FormatJson::decode( $value, true );
+ if ( !$preserveKeys && is_array( $val ) ) {
+ $val = self::recurseValue( $val, false );
+ }
+ return array( $val );
}
/**
* Encode value as a JSON string.
* TODO: remove this function once Scribunto gets its own json
encode/decode
* @param mixed $value
- * @throws Scribunto_LuaError
+ * @param bool $preserveKeys
* @return string
+ * @throws Scribunto_LuaError
*/
- public function jsonEncode( $value = null ) {
- $this->checkTypeOptional( self::luaNamespace . __FUNCTION__, 2,
$escapeHtml, 'boolean', false );
+ public function jsonEncode( $value = null, $preserveKeys = null ) {
+ $this->checkTypeOptional( self::luaNamespace . __FUNCTION__, 2,
$preserveKeys, 'boolean', false );
+ if ( !$preserveKeys && is_array( $value ) ) {
+ $value = self::recurseValue( $value, true );
+ }
$result = FormatJson::encode( $value, false, FormatJson::ALL_OK
);
if ( $result === false ) {
throw new Scribunto_LuaError( 'Unable to encode value'
);
--
To view, visit https://gerrit.wikimedia.org/r/176023
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I1d7376358fa70805fc9dbb02f535eeb7ef4ec5ed
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ZeroPortal
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits