Addshore has uploaded a new change for review.

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


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, 66 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/27/94327/1

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..4f341c4 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 );
@@ -40,8 +71,8 @@
         * @dataProvider provideData
         */
        public function testCanApply( $fromData, $toData, $expectedFromData, 
$expectedToData ) {
-               $from = $this->getItemContent( 'Q111', $fromData );
-               $to = $this->getItemContent( 'Q222', $toData );
+               $from = self::getItemContent( 'Q111', $fromData );
+               $to = self::getItemContent( 'Q222', $toData );
                $changeOps = new ChangeOpsMerge( $from, $to );
 
                $this->assertTrue( $from->getEntity()->equals( new Item( 
$fromData ) ), 'FromItem was not filled correctly' );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I406a34b75f0556d15ef80d7ee551d159d51bb7d0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>

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

Reply via email to