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

Change subject: Improved reordering mechanics of ByPropertyIdArray
......................................................................


Improved reordering mechanics of ByPropertyIdArray

Change-Id: I3d4ff8648dad27ce3754a3d302af43797a5a202c
(cherry picked from commit 997b499b166b41c21c2ef110112c8ddf3b4dd0c7)
---
M DataModel/ByPropertyIdArray.php
M tests/phpunit/ByPropertyIdArrayTest.php
2 files changed, 51 insertions(+), 17 deletions(-)

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



diff --git a/DataModel/ByPropertyIdArray.php b/DataModel/ByPropertyIdArray.php
index 6ee5f86..1a86ee6 100644
--- a/DataModel/ByPropertyIdArray.php
+++ b/DataModel/ByPropertyIdArray.php
@@ -213,13 +213,17 @@
                        throw new OutOfBoundsException( 'Object cannot be moved 
to ' . $toIndex );
                }
 
-               $propertyGroup = array_combine( $numericIndices, 
$this->getByPropertyId( $propertyId ) );
-
-               if( $toIndex > $lastIndex ) {
+               if( $toIndex >= $lastIndex ) {
                        $this->moveObjectToEndOfPropertyGroup( $object );
                } else {
-                       $insertBefore = $propertyGroup[$toIndex];
                        $this->removeObject( $object );
+
+                       $propertyGroup = array_combine(
+                               $this->getFlatArrayIndices( $propertyId ),
+                               $this->getByPropertyId( $propertyId )
+                       );
+
+                       $insertBefore = $propertyGroup[$toIndex];
                        $this->insertObjectAtIndex( $object, 
$this->getFlatArrayIndexOfObject( $insertBefore ) );
                }
        }
@@ -235,7 +239,10 @@
 
                $propertyId = $object->getPropertyId();
                $propertyIdSerialization = $propertyId->getSerialization();
-               $propertyGroup = $this->getByPropertyId( $propertyId );
+
+               $propertyGroup = in_array( $propertyIdSerialization, 
$this->getPropertyIds() )
+                       ? $this->getByPropertyId( $propertyId )
+                       : array();
 
                $propertyGroup[] = $object;
                $this->byId[$propertyIdSerialization] = $propertyGroup;
@@ -294,6 +301,18 @@
                 */
                $insertBefore = null;
 
+               $oldIndex = $this->getPropertyGroupIndex( $propertyId );
+               $byIdClone = $this->byId;
+
+               // Remove "property group" to calculate the groups new index:
+               unset( $this->byId[$propertyId->getSerialization()] );
+
+               if( $toIndex > $oldIndex ) {
+                       // If the group shall be moved towards the bottom, the 
number of objects within the
+                       // group needs to be subtracted from the absolute 
toIndex:
+                       $toIndex -= count( 
$byIdClone[$propertyId->getSerialization()] );
+               }
+
                foreach( $this->getPropertyIds() as $pId ) {
                        // Accepting other than the exact index by using <= 
letting the "property group" "latch"
                        // in the next slot.
@@ -303,26 +322,21 @@
                        }
                }
 
-               if( $propertyId->equals( $insertBefore ) ) {
-                       return;
-               }
-
-               $clone = $this->byId;
                $serializedPropertyId = $propertyId->getSerialization();
                $this->byId = array();
 
-               foreach( $clone as $serializedPId => $objects ) {
+               foreach( $byIdClone as $serializedPId => $objects ) {
                        $pId = new PropertyId( $serializedPId );
                        if( $pId->equals( $propertyId ) ) {
                                continue;
                        } elseif( $pId->equals( $insertBefore ) ) {
-                               $this->byId[$serializedPropertyId] = 
$clone[$serializedPropertyId];
+                               $this->byId[$serializedPropertyId] = 
$byIdClone[$serializedPropertyId];
                        }
                        $this->byId[$serializedPId] = $objects;
                }
 
                if( is_null( $insertBefore ) ) {
-                       $this->byId[$serializedPropertyId] = 
$clone[$serializedPropertyId];
+                       $this->byId[$serializedPropertyId] = 
$byIdClone[$serializedPropertyId];
                }
 
                $this->exchangeArray( $this->toFlatArray() );
@@ -374,12 +388,11 @@
 
                // Determine whether to simply reindex the object within its 
"property group":
                $propertyIndices = $this->getFlatArrayIndices( 
$object->getPropertyId() );
-               $propertyIndices[] = $propertyIndices[count( $propertyIndices ) 
- 1] + 1;
 
                if( in_array( $toIndex, $propertyIndices ) ) {
                        $this->moveObjectInPropertyGroup( $object, $toIndex );
                } else {
-                       $edgeIndex = ( $toIndex < $propertyIndices[0] )
+                       $edgeIndex = ( $toIndex <= $propertyIndices[0] )
                                ? $propertyIndices[0]
                                : $propertyIndices[count( $propertyIndices ) - 
1];
 
diff --git a/tests/phpunit/ByPropertyIdArrayTest.php 
b/tests/phpunit/ByPropertyIdArrayTest.php
index e8b2d61..b7d9c68 100644
--- a/tests/phpunit/ByPropertyIdArrayTest.php
+++ b/tests/phpunit/ByPropertyIdArrayTest.php
@@ -198,7 +198,7 @@
                $argLists = array();
 
                $argLists[] = array( $c, $c[0], 0, $c );
-               $argLists[] = array( $c, $c[0], 1, $c );
+               $argLists[] = array( $c, $c[0], 1, array( $c[1], $c[0], $c[2], 
$c[3], $c[4], $c[5] ) );
                $argLists[] = array( $c, $c[0], 2, array( $c[1], $c[0], $c[2], 
$c[3], $c[4], $c[5] ) );
                $argLists[] = array( $c, $c[0], 3, array( $c[2], $c[3], $c[4], 
$c[1], $c[0], $c[5] ) );
                $argLists[] = array( $c, $c[0], 4, array( $c[2], $c[3], $c[4], 
$c[1], $c[0], $c[5] ) );
@@ -206,23 +206,44 @@
                $argLists[] = array( $c, $c[0], 6, array( $c[2], $c[3], $c[4], 
$c[5], $c[1], $c[0] ) );
 
                $argLists[] = array( $c, $c[1], 0, array( $c[1], $c[0], $c[2], 
$c[3], $c[4], $c[5] ) );
+               $argLists[] = array( $c, $c[1], 1, $c );
+               $argLists[] = array( $c, $c[1], 2, $c );
+               $argLists[] = array( $c, $c[1], 3, array( $c[2], $c[3], $c[4], 
$c[0], $c[1], $c[5] ) ); //
+               $argLists[] = array( $c, $c[1], 4, array( $c[2], $c[3], $c[4], 
$c[0], $c[1], $c[5] ) );
                $argLists[] = array( $c, $c[1], 5, array( $c[2], $c[3], $c[4], 
$c[0], $c[1], $c[5] ) );
+               $argLists[] = array( $c, $c[1], 6, array( $c[2], $c[3], $c[4], 
$c[5], $c[0], $c[1] ) );
 
                $argLists[] = array( $c, $c[2], 0, array( $c[2], $c[3], $c[4], 
$c[0], $c[1], $c[5] ) );
-               $argLists[] = array( $c, $c[2], 4, array( $c[0], $c[1], $c[3], 
$c[2], $c[4], $c[5] ) );
+               $argLists[] = array( $c, $c[2], 1, $c );
+               $argLists[] = array( $c, $c[2], 2, $c );
+               $argLists[] = array( $c, $c[2], 3, array( $c[0], $c[1], $c[3], 
$c[2], $c[4], $c[5] ) );
+               $argLists[] = array( $c, $c[2], 4, array( $c[0], $c[1], $c[3], 
$c[4], $c[2], $c[5] ) );
                $argLists[] = array( $c, $c[2], 5, array( $c[0], $c[1], $c[3], 
$c[4], $c[2], $c[5] ) );
                $argLists[] = array( $c, $c[2], 6, array( $c[0], $c[1], $c[5], 
$c[3], $c[4], $c[2] ) );
 
                $argLists[] = array( $c, $c[3], 0, array( $c[3], $c[2], $c[4], 
$c[0], $c[1], $c[5] ) );
                $argLists[] = array( $c, $c[3], 1, array( $c[0], $c[1], $c[3], 
$c[2], $c[4], $c[5] ) );
                $argLists[] = array( $c, $c[3], 2, array( $c[0], $c[1], $c[3], 
$c[2], $c[4], $c[5] ) );
+               $argLists[] = array( $c, $c[3], 3, $c );
+               $argLists[] = array( $c, $c[3], 4, array( $c[0], $c[1], $c[2], 
$c[4], $c[3], $c[5] ) );
+               $argLists[] = array( $c, $c[3], 5, array( $c[0], $c[1], $c[2], 
$c[4], $c[3], $c[5] ) );
+               $argLists[] = array( $c, $c[3], 6, array( $c[0], $c[1], $c[5], 
$c[2], $c[4], $c[3] ) );
 
                $argLists[] = array( $c, $c[4], 0, array( $c[4], $c[2], $c[3], 
$c[0], $c[1], $c[5] ) );
+               $argLists[] = array( $c, $c[4], 1, array( $c[0], $c[1], $c[4], 
$c[2], $c[3], $c[5] ) );
                $argLists[] = array( $c, $c[4], 2, array( $c[0], $c[1], $c[4], 
$c[2], $c[3], $c[5] ) );
+               $argLists[] = array( $c, $c[4], 3, array( $c[0], $c[1], $c[2], 
$c[4], $c[3], $c[5] ) );
+               $argLists[] = array( $c, $c[4], 4, $c );
+               $argLists[] = array( $c, $c[4], 5, $c );
+               $argLists[] = array( $c, $c[4], 6, array( $c[0], $c[1], $c[5], 
$c[2], $c[3], $c[4] ) );
 
                $argLists[] = array( $c, $c[5], 0, array( $c[5], $c[0], $c[1], 
$c[2], $c[3], $c[4] ) );
                $argLists[] = array( $c, $c[5], 1, array( $c[0], $c[1], $c[5], 
$c[2], $c[3], $c[4] ) );
                $argLists[] = array( $c, $c[5], 2, array( $c[0], $c[1], $c[5], 
$c[2], $c[3], $c[4] ) );
+               $argLists[] = array( $c, $c[5], 3, $c );
+               $argLists[] = array( $c, $c[5], 4, $c );
+               $argLists[] = array( $c, $c[5], 5, $c );
+               $argLists[] = array( $c, $c[5], 6, $c );
 
                return $argLists;
        }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I3d4ff8648dad27ce3754a3d302af43797a5a202c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: mw1.23-wmf6
Gerrit-Owner: Aude <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Henning Snater <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to