[MediaWiki-commits] [Gerrit] mediawiki...Translate[master]: Implement plural aware message content comparison

2016-12-21 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/319571 )

Change subject: Implement plural aware message content comparison
..


Implement plural aware message content comparison

When someone uses an inline plural in a message that is exported in a
format that uses the CLDR plural format without support for the inline
syntax, on the next import that message is shown as changed. Since the
translation is in fact equal and we would like to keep the inline
version in the wiki, amend the comparison function to compare a some
kind of canonical representation instead of using plain string equality.

I moved this code from ExternalMessageSourceStateComparator to FFS since
this only applies currently to a few specific formats, and because there
might be other things that need similar handling in the future. For
example the fuzzy handling could also be moved to the FFS classes.

Change-Id: I6417ce19332ee90346a00d5b658d45b39c3a691b
---
M ffs/FFS.php
M ffs/JsonFFS.php
M ffs/SimpleFFS.php
M ffs/YamlFFS.php
M resources/css/ext.translate.messagetable.less
M tests/phpunit/utils/ArrayFlattenerTest.php
M utils/ArrayFlattener.php
M utils/ExternalMessageSourceStateComparator.php
8 files changed, 175 insertions(+), 29 deletions(-)

Approvals:
  Amire80: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/ffs/FFS.php b/ffs/FFS.php
index 3c425e9..390c8af 100644
--- a/ffs/FFS.php
+++ b/ffs/FFS.php
@@ -76,6 +76,16 @@
public function supportsFuzzy();
 
/**
+* Checks whether two strings are equal. Sometimes same content might
+* have multiple representations. The main case are inline plurals,
+* which in some formats require expansion at export time.
+*
+* @return bool
+* @since 2016.11
+*/
+   public function isContentEqual( $a, $b );
+
+   /**
 * Return the commonly used file extensions for these formats.
 * Include the dot.
 * @return string[]
diff --git a/ffs/JsonFFS.php b/ffs/JsonFFS.php
index 85b57ff..1e99dd2 100644
--- a/ffs/JsonFFS.php
+++ b/ffs/JsonFFS.php
@@ -24,6 +24,14 @@
return is_array( FormatJson::decode( $data, /*as array*/true ) 
);
}
 
+   /**
+* @param $group FileBasedMessageGroup
+*/
+   public function __construct( FileBasedMessageGroup $group ) {
+   parent::__construct( $group );
+   $this->flattener = $this->getFlattener();
+   }
+
public function getFileExtensions() {
return array( '.json' );
}
@@ -48,12 +56,8 @@
 
unset( $messages['@metadata'] );
 
-   if ( isset( $this->extra['nestingSeparator'] ) ) {
-   $parseCLDRPlurals = isset( 
$this->extra['parseCLDRPlurals'] ) ?
-   $this->extra['parseCLDRPlurals'] : false;
-   $flattener = new ArrayFlattener( 
$this->extra['nestingSeparator'],
-   $parseCLDRPlurals );
-   $messages = $flattener->flatten( $messages );
+   if ( $this->flattener ) {
+   $messages = $this->flattener->flatten( $messages );
}
 
$messages = $this->group->getMangler()->mangle( $messages );
@@ -113,12 +117,8 @@
return '';
}
 
-   if ( isset( $this->extra['nestingSeparator'] ) ) {
-   $parseCLDRPlurals = isset( 
$this->extra['parseCLDRPlurals'] ) ?
-   $this->extra['parseCLDRPlurals'] : false;
-   $flattener = new ArrayFlattener( 
$this->extra['nestingSeparator'],
-   $parseCLDRPlurals );
-   $messages = $flattener->unflatten( $messages );
+   if ( $this->flattener ) {
+   $messages = $this->flattener->unflatten( $messages );
}
 
if ( isset( $this->extra['includeMetadata'] ) && 
!$this->extra['includeMetadata'] ) {
@@ -128,6 +128,26 @@
return FormatJson::encode( $messages, "\t", FormatJson::ALL_OK 
) . "\n";
}
 
+   protected function getFlattener() {
+   if ( !isset( $this->extra['nestingSeparator'] ) ) {
+   return null;
+   }
+
+   $parseCLDRPlurals = isset( $this->extra['parseCLDRPlurals'] ) ?
+   $this->extra['parseCLDRPlurals'] : false;
+   $flattener = new ArrayFlattener( 
$this->extra['nestingSeparator'], $parseCLDRPlurals );
+
+   return $flattener;
+   }
+
+   public function isContentEqual( $a, $b ) {
+   if ( $this->flattener ) {
+   return $this->flattener->compareContent( $a, $b );
+   } else {
+   return 

[MediaWiki-commits] [Gerrit] mediawiki...Translate[master]: Implement plural aware message content comparison

2016-11-03 Thread Nikerabbit (Code Review)
Nikerabbit has uploaded a new change for review.

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

Change subject: Implement plural aware message content comparison
..

Implement plural aware message content comparison

When someone uses an inline plural in a messages that is exported
in a format that uses CLDR plural format without support for inline
syntax, on next sync that message is shown as changed. Since the
translation is in fact equal and we would like to keep the inline
version in the wiki, amend the comparison function to compare some
kind of canonical representation instead of plain string equality.

I moved this from ExternalMessageSourceStateComparator to FFS since
this only applies currently to few specific formats, and that there
might be other things that need similar handling in the future. For
example the fuzzy handling could also be moved to FFS classes.

Change-Id: I6417ce19332ee90346a00d5b658d45b39c3a691b
---
M ffs/FFS.php
M ffs/JsonFFS.php
M ffs/SimpleFFS.php
M ffs/YamlFFS.php
M resources/css/ext.translate.messagetable.less
M tests/phpunit/utils/ArrayFlattenerTest.php
M utils/ArrayFlattener.php
M utils/ExternalMessageSourceStateComparator.php
8 files changed, 175 insertions(+), 29 deletions(-)


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

diff --git a/ffs/FFS.php b/ffs/FFS.php
index 3c425e9..ccfc3c3 100644
--- a/ffs/FFS.php
+++ b/ffs/FFS.php
@@ -76,6 +76,16 @@
public function supportsFuzzy();
 
/**
+* Checks whether two strings are equal. Sometimes same content might
+* have multiple representations. The main case are inline plurals,
+* which in some formats require expansion at export time.
+*
+* @return bool
+* @since 2016.11
+*/
+   public function isContentEqual();
+
+   /**
 * Return the commonly used file extensions for these formats.
 * Include the dot.
 * @return string[]
diff --git a/ffs/JsonFFS.php b/ffs/JsonFFS.php
index b94a7ba..425879b 100644
--- a/ffs/JsonFFS.php
+++ b/ffs/JsonFFS.php
@@ -24,6 +24,14 @@
return is_array( FormatJson::decode( $data, /*as array*/true ) 
);
}
 
+   /**
+* @param $group FileBasedMessageGroup
+*/
+   public function __construct( FileBasedMessageGroup $group ) {
+   parent::__construct( $group );
+   $this->flattener = $this->getFlattener();
+   }
+
public function getFileExtensions() {
return array( '.json' );
}
@@ -48,12 +56,8 @@
 
unset( $messages['@metadata'] );
 
-   if ( isset( $this->extra['nestingSeparator'] ) ) {
-   $parseCLDRPlurals = isset( 
$this->extra['parseCLDRPlurals'] ) ?
-   $this->extra['parseCLDRPlurals'] : false;
-   $flattener = new ArrayFlattener( 
$this->extra['nestingSeparator'],
-   $parseCLDRPlurals );
-   $messages = $flattener->flatten( $messages );
+   if ( $this->flattener ) {
+   $messages = $this->flattener->flatten( $messages );
}
 
$messages = $this->group->getMangler()->mangle( $messages );
@@ -113,17 +117,33 @@
return '';
}
 
-   if ( isset( $this->extra['nestingSeparator'] ) ) {
-   $parseCLDRPlurals = isset( 
$this->extra['parseCLDRPlurals'] ) ?
-   $this->extra['parseCLDRPlurals'] : false;
-   $flattener = new ArrayFlattener( 
$this->extra['nestingSeparator'],
-   $parseCLDRPlurals );
-   $messages = $flattener->unflatten( $messages );
+   if ( $this->flattener ) {
+   $messages = $this->flattener->unflatten( $messages );
}
 
return FormatJson::encode( $messages, "\t", FormatJson::ALL_OK 
) . "\n";
}
 
+   protected function getFlattener() {
+   if ( !isset( $this->extra['nestingSeparator'] ) ) {
+   return null;
+   }
+
+   $parseCLDRPlurals = isset( $this->extra['parseCLDRPlurals'] ) ?
+   $this->extra['parseCLDRPlurals'] : false;
+   $flattener = new ArrayFlattener( 
$this->extra['nestingSeparator'], $parseCLDRPlurals );
+
+   return $flattener;
+   }
+
+   public function isContentEqual( $a, $b ) {
+   if ( $this->flattener ) {
+   return $this->flattener->compareContent( $a, $b );
+   } else {
+   return parent::isContentEqual( $a, $b );
+   }
+   }
+
public static function getExtraSchema() {
$schema = array(