Eloquence has uploaded a new change for review.

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

Change subject: Deprecate RubyYamlFFS without destroying it
......................................................................

Deprecate RubyYamlFFS without destroying it

Per Niklas' request, so we can deploy this in stages, this commit
leaves RubyYamlFFS intact but switches YamlFFS to ArrayFlattener.
https://gerrit.wikimedia.org/r/#/c/315939/ can be merged ater this,
and once that's all working, we can remove RubyYamlFFS.

Change-Id: Id07d72897b26013eee3434ecd0c12d70738100db
---
M ffs/RubyYamlFFS.php
M ffs/YamlFFS.php
2 files changed, 147 insertions(+), 106 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate 
refs/changes/43/316043/1

diff --git a/ffs/RubyYamlFFS.php b/ffs/RubyYamlFFS.php
index f0c978d..92d65c6 100644
--- a/ffs/RubyYamlFFS.php
+++ b/ffs/RubyYamlFFS.php
@@ -20,6 +20,116 @@
                return array( '.yml', '.yaml' );
        }
 
+       protected function getFlattener() {
+               return $this;
+       }
+
+       /**
+        * 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;
+       }
+
        /**
         * Flattens ruby plural arrays into special plural syntax.
         *
diff --git a/ffs/YamlFFS.php b/ffs/YamlFFS.php
index 41fe5ad..4586c6f 100644
--- a/ffs/YamlFFS.php
+++ b/ffs/YamlFFS.php
@@ -8,6 +8,15 @@
  * @ingroup FFS
  */
 class YamlFFS extends SimpleFFS implements MetaYamlSchemaExtender {
+
+       /**
+   * @param $group FileBasedMessageGroup
+   */
+  public function __construct( FileBasedMessageGroup $group ) {
+         parent::__construct( $group );
+               $this->flattener = $this->getFlattener();
+       }
+
        public function getFileExtensions() {
                return array( '.yaml', '.yml' );
        }
@@ -123,6 +132,23 @@
        }
 
        /**
+        * Obtains object used to flatten and unflatten arrays. In this 
implementation
+        * we use the ArrayFlattener class which also supports CLDR 
pluralization rules.
+        *
+        * @param $messages array
+        * @return object with flatten, unflatten methods
+        */
+       protected function getFlattener() {
+               $nestingSeparator = isset( $this->extra['nestingSeparator'] ) ?
+                       $this->extra['nestingSeparator'] : '.';
+               $parseCLDRPlurals = isset( $this->extra['parseCLDRPlurals'] ) ?
+                       $this->extra['parseCLDRPlurals'] : false;
+
+               // Instantiate helper class for flattening and unflattening 
nested arrays
+               return new ArrayFlattener( $nestingSeparator, $parseCLDRPlurals 
);
+       }
+
+       /**
         * Flattens multidimensional array by using the path to the value as key
         * with each individual key separated by a dot.
         *
@@ -131,121 +157,20 @@
         * @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;
+               return $this->flattener->flatten( $messages );
        }
 
+
        /**
-        * Performs the reverse operation of flatten. Each dot in the key 
starts a
-        * new subarray in the final array.
+        * Performs the reverse operation of flatten. Each dot (or custom 
separator)
+        * 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 );
+               return $this->flattener->unflatten( $messages );
        }
 
        public static function getExtraSchema() {
@@ -259,6 +184,12 @@
                                                        'codeAsRoot' => array(
                                                                '_type' => 
'boolean',
                                                        ),
+                                                       'nestingSeparator' => 
array(
+                                                               '_type' => 
'text',
+                                                       ),
+                                                       'parseCLDRPlurals' => 
array(
+                                                               '_type' => 
'boolean',
+                                                       )
                                                )
                                        )
                                )

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id07d72897b26013eee3434ecd0c12d70738100db
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Eloquence <eloque...@gmail.com>

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

Reply via email to