Nikerabbit has uploaded a new change for review.
https://gerrit.wikimedia.org/r/89537
Change subject: JsonFFS: Support non-flat json arrays
......................................................................
JsonFFS: Support non-flat json arrays
Change-Id: Iec2b38c7ffb6a52b65418d77fc87b3b16a47a003
---
M ffs/JsonFFS.php
1 file changed, 129 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate
refs/changes/37/89537/1
diff --git a/ffs/JsonFFS.php b/ffs/JsonFFS.php
index 7d9647e..175fde6 100644
--- a/ffs/JsonFFS.php
+++ b/ffs/JsonFFS.php
@@ -49,6 +49,7 @@
unset( $messages['@metadata'] );
+ $messages = $this->flatten( $messages );
$messages = $this->group->getMangler()->mangle( $messages );
return array(
@@ -101,6 +102,134 @@
return '';
}
+ $messages = $this->unflatten( $messages );
+
return FormatJSON::encode( $messages, /*pretty*/true );
}
+
+ /**
+ * Flattens multidimensional array by using the path to the value as key
+ * with each individual key separated by a dot.
+ *
+ * @param $messages array
+ *
+ * @return array
+ */
+ protected function flatten( $messages ) {
+ $flat = true;
+
+ foreach ( $messages as $v ) {
+ if ( !is_array( $v ) ) {
+ continue;
+ }
+
+ $flat = false;
+ break;
+ }
+
+ if ( $flat ) {
+ return $messages;
+ }
+
+ $array = array();
+ foreach ( $messages as $key => $value ) {
+ if ( !is_array( $value ) ) {
+ $array[$key] = $value;
+ } else {
+ $plural = $this->flattenPlural( $value );
+ if ( $plural ) {
+ $array[$key] = $plural;
+ } else {
+ $newArray = array();
+ foreach ( $value as $newKey =>
$newValue ) {
+ $newArray["$key.$newKey"] =
$newValue;
+ }
+ $array += $this->flatten( $newArray );
+ }
+ }
+
+ /**
+ * Can as well keep only one copy around.
+ */
+ unset( $messages[$key] );
+ }
+
+ return $array;
+ }
+
+ /**
+ * Performs the reverse operation of flatten. Each dot in the key
starts a
+ * new subarray in the final array.
+ *
+ * @param $messages array
+ *
+ * @return array
+ */
+ protected function unflatten( $messages ) {
+ $array = array();
+ foreach ( $messages as $key => $value ) {
+ $plurals = $this->unflattenPlural( $key, $value );
+
+ if ( $plurals === false ) {
+ continue;
+ }
+
+ foreach ( $plurals as $keyPlural => $valuePlural ) {
+ $path = explode( '.', $keyPlural );
+ if ( count( $path ) == 1 ) {
+ $array[$keyPlural] = $valuePlural;
+ continue;
+ }
+
+ $pointer = &$array;
+ do {
+ /**
+ * Extract the level and make sure it
exists.
+ */
+ $level = array_shift( $path );
+ if ( !isset( $pointer[$level] ) ) {
+ $pointer[$level] = array();
+ }
+
+ /**
+ * Update the pointer to the new
reference.
+ */
+ $tmpPointer = &$pointer[$level];
+ unset( $pointer );
+ $pointer = &$tmpPointer;
+ unset( $tmpPointer );
+
+ /**
+ * If next level is the last, add it
into the array.
+ */
+ if ( count( $path ) === 1 ) {
+ $lastKey = array_shift( $path );
+ $pointer[$lastKey] =
$valuePlural;
+ }
+ } while ( count( $path ) );
+ }
+ }
+
+ return $array;
+ }
+
+ /**
+ * @param $value
+ * @return bool
+ */
+ public function flattenPlural( $value ) {
+ return false;
+ }
+
+ /**
+ * Override this. Return false to skip processing this value. Otherwise
+ *
+ * @param $key string
+ * @param $value string
+ *
+ * @return array with keys and values.
+ */
+ public function unflattenPlural( $key, $value ) {
+ return array( $key => $value );
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/89537
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec2b38c7ffb6a52b65418d77fc87b3b16a47a003
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits