jenkins-bot has submitted this change and it was merged.

Change subject: ChangeOpsMerge can ignore certain merge conflicts
......................................................................


ChangeOpsMerge can ignore certain merge conflicts

Change-Id: I406a34b75f0556d15ef80d7ee551d159d51bb7d0
---
M repo/includes/ChangeOp/ChangeOpsMerge.php
M repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
2 files changed, 116 insertions(+), 13 deletions(-)

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



diff --git a/repo/includes/ChangeOp/ChangeOpsMerge.php 
b/repo/includes/ChangeOp/ChangeOpsMerge.php
index 80a6e34..64b0925 100644
--- a/repo/includes/ChangeOp/ChangeOpsMerge.php
+++ b/repo/includes/ChangeOp/ChangeOpsMerge.php
@@ -2,9 +2,9 @@
 
 namespace Wikibase\ChangeOp;
 
+use InvalidArgumentException;
 use Wikibase\ItemContent;
 use Wikibase\Lib\ClaimGuidGenerator;
-use Wikibase\Repo\WikibaseRepo;
 
 /**
  * @since 0.5
@@ -18,17 +18,37 @@
        private $toItemContent;
        private $fromChangeOps;
        private $toChangeOps;
+       /** @var array */
+       private $ignoreConflicts;
 
        /**
         * @param ItemContent $fromItemContent
         * @param ItemContent $toItemContent
+        * @param array $ignoreConflicts list of elements to ignore conflicts 
for
+        *   can only contain 'label' and or 'description'
         */
-       public function __construct( ItemContent $fromItemContent, ItemContent 
$toItemContent ) {
+       public function __construct(
+               ItemContent $fromItemContent,
+               ItemContent $toItemContent,
+               $ignoreConflicts = array()
+       ) {
                $this->fromItemContent = $fromItemContent;
                $this->toItemContent = $toItemContent;
                $this->fromChangeOps = new ChangeOps();
                $this->toChangeOps = new ChangeOps();
+               $this->ignoreConflicts = $ignoreConflicts;
+               $this->assertValidIgnoreConflictValues();
+       }
 
+       private function assertValidIgnoreConflictValues() {
+               if( !is_array( $this->ignoreConflicts ) ){
+                       throw new InvalidArgumentException( '$ignoreConflicts 
must be an array' );
+               }
+               foreach( $this->ignoreConflicts as $ignoreConflict ){
+                       if( $ignoreConflict !== 'label' && $ignoreConflict !== 
'description' ){
+                               throw new InvalidArgumentException( 
'$ignoreConflicts array can only contain "label" or "description"' );
+                       }
+               }
        }
 
        public function apply() {
@@ -53,7 +73,9 @@
                                $this->toChangeOps->add( new ChangeOpLabel( 
$langCode, $label ) );
                        } else {
                                //todo add the option to merge conflicting 
labels into the aliases
-                               throw new ChangeOpException( "Conflicting 
labels for language {$langCode}" );
+                               if( !in_array( 'label', $this->ignoreConflicts 
) ){
+                                       throw new ChangeOpException( 
"Conflicting labels for language {$langCode}" );
+                               }
                        }
                }
        }
@@ -66,7 +88,9 @@
                                $this->toChangeOps->add( new 
ChangeOpDescription( $langCode, $desc ) );
                        } else {
                                //todo add the option to ignore description 
conflicts, or prioritise one
-                               throw new ChangeOpException( "Conflicting 
descriptions for language {$langCode}" );
+                               if( !in_array( 'description', 
$this->ignoreConflicts ) ){
+                                       throw new ChangeOpException( 
"Conflicting descriptions for language {$langCode}" );
+                               }
                        }
                }
        }
diff --git a/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php 
b/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
index e54bc22..b78832f 100644
--- a/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
+++ b/repo/tests/phpunit/includes/ChangeOp/ChangeOpsMergeTest.php
@@ -22,14 +22,45 @@
  */
 class ChangeOpsMergeTest extends \PHPUnit_Framework_TestCase {
 
-       public function testCanConstruct() {
-               $from = $this->getItemContent( 'Q111' );
-               $to = $this->getItemContent( 'Q222' );
-               $changeOps = new ChangeOpsMerge( $from, $to );
+       /**
+        * @dataProvider provideValidConstruction
+        */
+       public function testCanConstruct( $from, $to, $ignoreConflicts ) {
+               $changeOps = new ChangeOpsMerge( $from, $to, $ignoreConflicts );
                $this->assertInstanceOf( '\Wikibase\ChangeOp\ChangeOpsMerge', 
$changeOps );
        }
 
-       public function getItemContent( $id, $data = array() ) {
+       public static function provideValidConstruction(){
+               $from = self::getItemContent( 'Q111' );
+               $to = self::getItemContent( 'Q222' );
+               return array(
+                       array( $from, $to, array() ),
+                       array( $from, $to, array( 'label' ) ),
+                       array( $from, $to, array( 'description' ) ),
+                       array( $from, $to, array( 'description', 'label' ) ),
+               );
+       }
+
+       /**
+        * @dataProvider provideInvalidConstruction
+        */
+       public function testInvalidIgnoreConflicts( $from, $to, 
$ignoreConflicts ) {
+               $this->setExpectedException( 'InvalidArgumentException' );
+               new ChangeOpsMerge( $from, $to, $ignoreConflicts );
+       }
+
+       public static function provideInvalidConstruction(){
+               $from = self::getItemContent( 'Q111' );
+               $to = self::getItemContent( 'Q222' );
+               return array(
+                       array( $from, $to, 'foo' ),
+                       array( $from, $to, array( 'foo' ) ),
+                       array( $from, $to, array( 'label', 'foo' ) ),
+                       array( $from, $to, null ),
+               );
+       }
+
+       public static function getItemContent( $id, $data = array() ) {
                $item = new Item( $data );
                $item->setId( new ItemId( $id ) );
                $itemContent = new ItemContent( $item );
@@ -39,10 +70,10 @@
        /**
         * @dataProvider provideData
         */
-       public function testCanApply( $fromData, $toData, $expectedFromData, 
$expectedToData ) {
-               $from = $this->getItemContent( 'Q111', $fromData );
-               $to = $this->getItemContent( 'Q222', $toData );
-               $changeOps = new ChangeOpsMerge( $from, $to );
+       public function testCanApply( $fromData, $toData, $expectedFromData, 
$expectedToData, $ignoreConflicts = array() ) {
+               $from = self::getItemContent( 'Q111', $fromData );
+               $to = self::getItemContent( 'Q222', $toData );
+               $changeOps = new ChangeOpsMerge( $from, $to, $ignoreConflicts );
 
                $this->assertTrue( $from->getEntity()->equals( new Item( 
$fromData ) ), 'FromItem was not filled correctly' );
                $this->assertTrue( $to->getEntity()->equals( new Item( $toData 
) ), 'ToItem was not filled correctly' );
@@ -94,6 +125,13 @@
                        array(),
                        array( 'label' => array( 'en' => 'foo' ) ),
                );
+               $testCases['ignoreConflictLabelMerge'] = array(
+                       array( 'label' => array( 'en' => 'foo' ) ),
+                       array( 'label' => array( 'en' => 'bar' ) ),
+                       array( 'label' => array( 'en' => 'foo' ) ),
+                       array( 'label' => array( 'en' => 'bar' ) ),
+                       array( 'label' )
+               );
                $testCases['descriptionMerge'] = array(
                        array( 'description' => array( 'en' => 'foo' ) ),
                        array(),
@@ -105,6 +143,13 @@
                        array( 'description' => array( 'en' => 'foo' ) ),
                        array(),
                        array( 'description' => array( 'en' => 'foo' ) ),
+               );
+               $testCases['ignoreConflictDescriptionMerge'] = array(
+                       array( 'description' => array( 'en' => 'foo' ) ),
+                       array( 'description' => array( 'en' => 'bar' ) ),
+                       array( 'description' => array( 'en' => 'foo' ) ),
+                       array( 'description' => array( 'en' => 'bar' ) ),
+                       array( 'description' )
                );
                $testCases['aliasMerge'] = array(
                        array( 'aliases' => array( 'en' => array( 'foo', 'bar' 
) ) ),
@@ -185,6 +230,40 @@
                                ),
                        ),
                );
+               $testCases['ignoreConflictItemMerge'] = array(
+                       array(
+                               'label' => array( 'en' => 'foo', 'pt' => 
'ptfoo' ),
+                               'description' => array( 'en' => 'foo', 'pl' => 
'pldesc'  ),
+                               'aliases' => array( 'en' => array( 'foo', 'bar' 
), 'de' => array( 'defoo', 'debar' ) ),
+                               'links' => array( 'dewiki' => array( 'name' => 
'foo', 'badges' => array() ) ),
+                               'claims' => array(
+                                       array(
+                                               'm' => array( 'novalue', 88 ),
+                                               'q' => array( array(  
'novalue', 88  ) ),
+                                               'g' => 
'Q111$D8404CDA-25E4-4334-AF88-A3290BCD9C0F' )
+                               ),
+                       ),
+                       array(
+                               'label' => array( 'en' => 'toLabel' ),
+                               'description' => array( 'pl' => 'toLabel' )
+                       ),
+                       array(
+                               'label' => array( 'en' => 'foo' ),
+                               'description' => array( 'pl' => 'pldesc' )
+                       ),
+                       array(
+                               'label' => array( 'en' => 'toLabel', 'pt' => 
'ptfoo'  ),
+                               'description' => array( 'en' => 'foo', 'pl' => 
'toLabel' ),
+                               'aliases' => array( 'en' => array( 'foo', 'bar' 
), 'de' => array( 'defoo', 'debar' ) ),
+                               'links' => array( 'dewiki' => array( 'name' => 
'foo', 'badges' => array() ) ),
+                               'claims' => array(
+                                       array(
+                                               'm' => array( 'novalue', 88 ),
+                                               'q' => array( array(  
'novalue', 88  ) ) )
+                               ),
+                       ),
+                       array( 'label', 'description' )
+               );
                return $testCases;
        }
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I406a34b75f0556d15ef80d7ee551d159d51bb7d0
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to