Skip to content

Commit 2185a05

Browse files
committed
Merge pull request #166 from wmde/StatementListDiffer
ClaimListDiffer -> StatementListDiffer
2 parents 935186e + 58a50ae commit 2185a05

8 files changed

Lines changed: 154 additions & 273 deletions

File tree

RELEASE-NOTES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ Other breaking changes:
5050
* Removed `ClaimAggregate` interface, which is thus no longer implemented by `Entity`
5151
* `HashableObjectStorage::getValueHash` no longer accepts a first optional parameter
5252
* `MapHasher` and `MapValueHasher` are now package private
53+
* Removed `Claims::getDiff`
5354

5455
#### Additions
5556

5657
* Added `ClaimList`
5758
* Added `StatementList`
58-
* Added `ClaimListDiffer`
59+
* Added `StatementListDiffer`
5960
* Added `PropertyDataTypeLookup` and trivial implementation `InMemoryDataTypeLookup`
6061
* Added `PropertyNotFoundException`
6162
* Added `ItemDiffer` and `PropertyDiffer`
@@ -66,6 +67,7 @@ Other breaking changes:
6667

6768
#### Deprecations
6869

70+
* Deprecated `Claims`
6971
* Deprecated `Entity::setId`
7072
* Deprecated `Entity::newClaim`
7173
* Deprecated `Entity::getAllSnaks`

src/Claim/Claims.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Wikibase\DataModel\ByPropertyIdArray;
1313
use Wikibase\DataModel\Entity\PropertyId;
1414
use Wikibase\DataModel\Snak\Snak;
15+
use Wikibase\DataModel\Statement\StatementListDiffer;
1516

1617
/**
1718
* A claim (identified using it's GUID) can only be added once.
@@ -367,19 +368,6 @@ public function getHashes() {
367368
return $snaks;
368369
}
369370

370-
/**
371-
* @since 0.4
372-
*
373-
* @param Claims $claims
374-
*
375-
* @return Diff
376-
* @throws UnexpectedValueException
377-
*/
378-
public function getDiff( Claims $claims ) {
379-
$differ = new ClaimListDiffer();
380-
return $differ->getDiff( $this, $claims );
381-
}
382-
383371
/**
384372
* Returns a hash based on the value of the object.
385373
* The hash is based on the hashes of the claims contained,

src/Entity/Diff/ItemDiffer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Wikibase\DataModel\Entity\Item;
1010
use Wikibase\DataModel\Entity\ItemId;
1111
use Wikibase\DataModel\SiteLink;
12+
use Wikibase\DataModel\Statement\StatementListDiffer;
1213

1314
/**
1415
* @since 1.0
@@ -51,8 +52,8 @@ public function diffItems( Item $from, Item $to ) {
5152
$differ = new MapDiffer( true );
5253
$diffOps = $differ->doDiff( $this->toDiffArray( $from ), $this->toDiffArray( $to ) );
5354

54-
$claims = new Claims( $from->getClaims() );
55-
$diffOps['claim'] = $claims->getDiff( new Claims( $to->getClaims() ) );
55+
$statementListDiffer = new StatementListDiffer();
56+
$diffOps['claim'] = $statementListDiffer->getDiff( $from->getStatements(), $to->getStatements() );
5657

5758
return new ItemDiff( $diffOps );
5859
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Wikibase\DataModel\Claim;
3+
namespace Wikibase\DataModel\Statement;
44

55
use Diff\Differ\MapDiffer;
66
use Diff\DiffOp\Diff\Diff;
@@ -9,36 +9,40 @@
99
use Diff\DiffOp\DiffOpChange;
1010
use Diff\DiffOp\DiffOpRemove;
1111
use UnexpectedValueException;
12+
use Wikibase\DataModel\Claim\Claim;
13+
use Wikibase\DataModel\Claim\Claims;
1214

1315
/**
1416
* @since 1.0
1517
*
1618
* @licence GNU GPL v2+
1719
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
1820
*/
19-
class ClaimListDiffer {
21+
class StatementListDiffer {
2022

2123
/**
2224
* @since 1.0
2325
*
24-
* @param Claims $fromClaims
25-
* @param Claims $toClaims
26+
* @param StatementList $fromStatements
27+
* @param StatementList $toStatements
2628
*
2729
* @return Diff
2830
* @throws UnexpectedValueException
2931
*/
30-
public function getDiff( Claims $fromClaims, Claims $toClaims ) {
32+
public function getDiff( StatementList $fromStatements, StatementList $toStatements ) {
3133
$differ = new MapDiffer();
34+
$fromStatements = new Claims( $fromStatements->toArray() );
35+
$toStatements = new Claims( $toStatements->toArray() );
3236

3337
$hashDifferences = $differ->doDiff(
34-
$fromClaims->getHashes(),
35-
$toClaims->getHashes()
38+
$fromStatements->getHashes(),
39+
$toStatements->getHashes()
3640
);
3741

3842
$diff = new Diff( array(), true );
3943

4044
foreach ( $hashDifferences as $guid => $diffOp ) {
41-
$diff[$guid] = $this->getDiffOp( $diffOp, $guid, $toClaims, $fromClaims );
45+
$diff[$guid] = $this->getDiffOp( $diffOp, $guid, $toStatements, $fromStatements );
4246
}
4347

4448
return $diff;

tests/unit/Claim/ClaimListDifferTest.php

Lines changed: 0 additions & 130 deletions
This file was deleted.

tests/unit/Claim/ClaimsTest.php

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -503,120 +503,6 @@ public function testDuplicateClaims() {
503503
$this->assertNull( $list->getClaimWithGuid( $secondClaim->getGuid() ) );
504504
}
505505

506-
public function getDiffProvider() {
507-
$argLists = array();
508-
509-
$claim0 = $this->makeClaim( new PropertyNoValueSnak( 42 ) );
510-
$claim1 = $this->makeClaim( new PropertySomeValueSnak( 42 ) );
511-
$claim2 = $this->makeClaim( new PropertyValueSnak( 42, new StringValue( 'ohi' ) ) );
512-
$claim3 = $this->makeClaim( new PropertyNoValueSnak( 1 ) );
513-
$claim4 = $this->makeClaim( new PropertyNoValueSnak( 2 ) );
514-
515-
$statement0 = $this->makeStatement( new PropertyNoValueSnak( 5 ) );
516-
$statement0->setRank( Statement::RANK_PREFERRED );
517-
518-
$statement1 = $this->makeStatement( new PropertyNoValueSnak( 5 ) );
519-
$statement1->setReferences( new ReferenceList( array( new Reference(
520-
new SnakList( array( new PropertyValueSnak( 10, new StringValue( 'spam' ) ) ) )
521-
) ) ) );
522-
523-
// same GUID, changed main snak
524-
$claim2v2 = unserialize( serialize( $claim2 ) );
525-
$claim2v2->setMainSnak( new PropertyValueSnak( 42, new StringValue( 'omnomnom' ) ) );
526-
527-
// different GUID, same contents, same hash
528-
$claim0a = unserialize( serialize( $claim0 ) );
529-
$claim0a->setGuid( 'TEST$claim0x' );
530-
$claim0a->setMainSnak( new PropertyValueSnak( 99, new StringValue( 'frob' ) ) );
531-
532-
// same GUID as $claim0a, same content/hash as $claim0
533-
$claim0b = unserialize( serialize( $claim0 ) );
534-
$claim0b->setGuid( 'TEST$claim0x' );
535-
536-
$source = new Claims();
537-
$target = new Claims();
538-
$expected = new Diff( array(), true );
539-
$argLists[] = array( $source, $target, $expected, 'Two empty lists should result in an empty diff' );
540-
541-
542-
$source = new Claims();
543-
$target = new Claims( array( $claim0 ) );
544-
$expected = new Diff( array( $claim0->getGuid() => new DiffOpAdd( $claim0 ) ), true );
545-
$argLists[] = array( $source, $target, $expected, 'List with no entries to list with one should result in one add op' );
546-
547-
548-
$source = new Claims( array( $claim0 ) );
549-
$target = new Claims();
550-
$expected = new Diff( array( $claim0->getGuid() => new DiffOpRemove( $claim0 ) ), true );
551-
$argLists[] = array( $source, $target, $expected, 'List with one entry to an empty list should result in one remove op' );
552-
553-
554-
$source = new Claims( array( $claim0, $claim3, $claim2 ) );
555-
$target = new Claims( array( $claim0, $claim2, $claim3 ) );
556-
$expected = new Diff( array(), true );
557-
$argLists[] = array( $source, $target, $expected, 'Two identical lists should result in an empty diff' );
558-
559-
560-
$source = new Claims( array( $claim0 ) );
561-
$target = new Claims( array( $claim1 ) );
562-
$expected = new Diff( array(
563-
$claim1->getGuid() => new DiffOpAdd( $claim1 ),
564-
$claim0->getGuid() => new DiffOpRemove( $claim0 )
565-
), true );
566-
$argLists[] = array( $source, $target, $expected, 'Two lists with each a single different entry should result into one add and one remove op' );
567-
568-
569-
$source = new Claims( array( $claim2, $claim3, $claim0, $claim4 ) );
570-
$target = new Claims( array( $claim2, $claim1, $claim3, $claim4 ) );
571-
$expected = new Diff( array(
572-
$claim1->getGuid() => new DiffOpAdd( $claim1 ),
573-
$claim0->getGuid() => new DiffOpRemove( $claim0 )
574-
), true );
575-
$argLists[] = array( $source, $target, $expected, 'Two lists with identical items except for one change should result in one add and one remove op' );
576-
577-
578-
$source = new Claims( array( $claim0, $claim0, $claim3, $claim2, $claim2, $claim2, $statement0 ) );
579-
$target = new Claims( array( $claim0, $claim0, $claim2, $claim3, $claim2, $claim2, $statement0 ) );
580-
$expected = new Diff( array(), true );
581-
$argLists[] = array( $source, $target, $expected, 'Two identical lists with duplicate items should result in an empty diff' );
582-
583-
584-
$source = new Claims( array( $statement0, $statement1, $claim0 ) );
585-
$target = new Claims( array( $claim1, $claim1, $claim0, $statement1 ) );
586-
$expected = new Diff( array(
587-
$claim1->getGuid() => new DiffOpAdd( $claim1 ),
588-
$statement0->getGuid() => new DiffOpRemove( $statement0 ),
589-
), true );
590-
$argLists[] = array( $source, $target, $expected, 'Two lists with duplicate items and a different entry should result into one add and one remove op' );
591-
592-
$source = new Claims( array( $claim0, $claim3, $claim2 ) );
593-
$target = new Claims( array( $claim0, $claim2v2, $claim3 ) );
594-
$expected = new Diff( array( $claim2->getGuid() => new DiffOpChange( $claim2, $claim2v2 ) ), true );
595-
$argLists[] = array( $source, $target, $expected, 'Changing the value of a claim should result in a change op' );
596-
597-
$source = new Claims( array( $claim0, $claim0a ) );
598-
$target = new Claims( array( $claim0, $claim0b ) );
599-
$expected = new Diff( array( $claim0a->getGuid() => new DiffOpChange( $claim0a, $claim0b ) ), true );
600-
$argLists[] = array( $source, $target, $expected, 'It should be possible for a claim to become the same as another claim' );
601-
602-
return $argLists;
603-
}
604-
605-
/**
606-
* @dataProvider getDiffProvider
607-
*
608-
* @param Claims $source
609-
* @param Claims $target
610-
* @param Diff $expected
611-
* @param string $message
612-
*/
613-
public function testGetDiff( Claims $source, Claims $target, Diff $expected, $message ) {
614-
$actual = $source->getDiff( $target );
615-
616-
// Note: this makes order of inner arrays relevant, and this order is not guaranteed by the interface
617-
$this->assertEquals( $expected->getOperations(), $actual->getOperations(), $message );
618-
}
619-
620506
public function testGetHash() {
621507
$claimsA = new Claims();
622508
$claimsB = new Claims();

0 commit comments

Comments
 (0)