Skip to content

Commit 672e323

Browse files
committed
Merge pull request #158 from wmde/statement
Add StatementList
2 parents 811e2d7 + 59f1440 commit 672e323

17 files changed

Lines changed: 425 additions & 69 deletions

Aliases.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Claims extends \Wikibase\DataModel\Claim\Claims {}
8383
/**
8484
* @deprecated since 0.6, use the base class instead.
8585
*/
86-
class Statement extends \Wikibase\DataModel\Claim\Statement {}
86+
class Statement extends DataModel\Statement\Statement {}
8787
/**
8888
* @deprecated since 0.6, use the base class instead.
8989
*/
@@ -160,3 +160,12 @@ class ItemDiff extends \Wikibase\DataModel\Entity\Diff\ItemDiff {}
160160
class EntityDiff extends \Wikibase\DataModel\Entity\Diff\EntityDiff {}
161161

162162
}
163+
164+
namespace Wikibase\DataModel\Claim {
165+
166+
/**
167+
* @deprecated since 1.0, use the base class instead.
168+
*/
169+
class Statement extends \Wikibase\DataModel\Statement\Statement {}
170+
171+
}

RELEASE-NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Other breaking changes:
5454
#### Additions
5555

5656
* Added `ClaimList`
57+
* Added `StatementList`
5758
* Added `ClaimListDiffer`
5859
* Added `PropertyDataTypeLookup` and trivial implementation `InMemoryDataTypeLookup`
5960
* Added `PropertyNotFoundException`
@@ -67,6 +68,7 @@ Other breaking changes:
6768
* Undeprecated passing an integer to `Item::setId` and `Property::setId`
6869
* Deprecated `Entity::setId`, `Entity::newClaim` and `Entity::getAllSnaks`
6970
* Deprecated `Item::getClaims` in favour of `Item::getStatements`
71+
* The FQN of `Statement` is now `Wikibase\DataModel\Statement\Statement`. The old FQN is deprecated.
7072

7173
## Version 0.9.1 (2014-08-26)
7274

WikibaseDataModel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class_alias( 'Wikibase\DataModel\ByPropertyIdArray', 'Wikibase\ByPropertyIdArray
3737
class_alias( 'Wikibase\DataModel\Claim\Claim', 'Wikibase\Claim' );
3838
class_alias( 'Wikibase\DataModel\Claim\ClaimListAccess', 'Wikibase\ClaimListAccess' );
3939
class_alias( 'Wikibase\DataModel\Claim\Claims', 'Wikibase\Claims' );
40-
class_alias( 'Wikibase\DataModel\Claim\Statement', 'Wikibase\Statement' );
40+
class_alias( 'Wikibase\DataModel\Statement\Statement', 'Wikibase\Statement' );
4141
class_alias( 'Wikibase\DataModel\Entity\Entity', 'Wikibase\Entity' );
4242
class_alias( 'Wikibase\DataModel\Entity\Item', 'Wikibase\Item' );
4343
class_alias( 'Wikibase\DataModel\Entity\Property', 'Wikibase\Property' );
@@ -59,3 +59,4 @@ class_alias( 'Wikibase\DataModel\LegacyIdInterpreter', 'Wikibase\DataModel\Inter
5959
// Aliases introduced in 1.0
6060
class_alias( 'Wikibase\DataModel\Entity\Diff\EntityDiff', 'Wikibase\DataModel\Entity\EntityDiff' );
6161
class_alias( 'Wikibase\DataModel\Entity\Diff\ItemDiff', 'Wikibase\DataModel\Entity\ItemDiff' );
62+
class_alias( 'Wikibase\DataModel\Statement\Statement', 'Wikibase\DataModel\Claim\Statement' );

src/Claim/Claim.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Wikibase\DataModel\Snak\Snak;
1010
use Wikibase\DataModel\Snak\SnakList;
1111
use Wikibase\DataModel\Snak\Snaks;
12+
use Wikibase\DataModel\Statement\Statement;
1213

1314
/**
1415
* Class that represents a single Wikibase claim.

src/Claim/ClaimList.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,6 @@ public function __construct( $claims = array() ) {
4141
$this->claims = $claims;
4242
}
4343

44-
/**
45-
* Returns the best claims per property.
46-
* The best claims are those with the highest rank for a particular property.
47-
* Deprecated ranks are never included.
48-
*
49-
* Caution: the ranking is done per property, not globally, as in the Claims class.
50-
*
51-
* @return self
52-
*/
53-
public function getBestClaimPerProperty() {
54-
$claimList = new self();
55-
56-
foreach ( $this->getPropertyIds() as $propertyId ) {
57-
$claims = new Claims( $this->claims );
58-
$claimList->addClaims( $claims->getClaimsForProperty( $propertyId )->getBestClaims() );
59-
}
60-
61-
return $claimList;
62-
}
63-
6444
/**
6545
* Returns the property ids used by the claims.
6646
* The keys of the returned array hold the serializations of the property ids.
@@ -77,12 +57,6 @@ public function getPropertyIds() {
7757
return $propertyIds;
7858
}
7959

80-
private function addClaims( Claims $claims ) {
81-
foreach ( $claims as $claim ) {
82-
$this->addClaim( $claim );
83-
}
84-
}
85-
8660
public function addClaim( Claim $claim ) {
8761
$this->claims[] = $claim;
8862
}

src/Entity/Item.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use OutOfBoundsException;
1111
use Wikibase\DataModel\Claim\Claim;
1212
use Wikibase\DataModel\Claim\Claims;
13-
use Wikibase\DataModel\Claim\Statement;
13+
use Wikibase\DataModel\Statement\Statement;
1414
use Wikibase\DataModel\Entity\Diff\EntityDiff;
1515
use Wikibase\DataModel\Entity\Diff\ItemDiff;
1616
use Wikibase\DataModel\SiteLink;
@@ -339,7 +339,7 @@ public function addClaim( Claim $claim ) {
339339
/**
340340
* @since 1.0
341341
*
342-
* @return Statement[]
342+
* @return \Wikibase\DataModel\Statement\Statement[]
343343
*/
344344
public function getStatements() {
345345
return $this->statements;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

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

55
use InvalidArgumentException;
6+
use Wikibase\DataModel\Claim\Claim;
67
use Wikibase\DataModel\Reference;
78
use Wikibase\DataModel\ReferenceList;
89
use Wikibase\DataModel\References;

src/Statement/StatementList.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Statement;
4+
5+
use InvalidArgumentException;
6+
use Traversable;
7+
use Wikibase\DataModel\Claim\Claims;
8+
use Wikibase\DataModel\Entity\PropertyId;
9+
use Wikibase\DataModel\Reference;
10+
use Wikibase\DataModel\ReferenceList;
11+
use Wikibase\DataModel\References;
12+
use Wikibase\DataModel\Snak\Snak;
13+
use Wikibase\DataModel\Snak\SnakList;
14+
use Wikibase\DataModel\Snak\Snaks;
15+
16+
/**
17+
* Ordered, non-unique, collection of Statement objects.
18+
* Provides various filter operations though does not do any indexing by default.
19+
*
20+
* @since 1.0
21+
*
22+
* @licence GNU GPL v2+
23+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
24+
*/
25+
class StatementList implements \IteratorAggregate {
26+
27+
/**
28+
* @var Statement[]
29+
*/
30+
private $statements = array();
31+
32+
/**
33+
* @param Statement[]|Traversable $statements
34+
*
35+
* @throws InvalidArgumentException
36+
*/
37+
public function __construct( $statements = array() ) {
38+
$this->addStatements( $statements );
39+
}
40+
41+
/**
42+
* Returns the best statements per property.
43+
* The best statements are those with the highest rank for a particular property.
44+
* Deprecated ranks are never included.
45+
*
46+
* @return self
47+
*/
48+
public function getBestStatementPerProperty() {
49+
$statementList = new self();
50+
51+
foreach ( $this->getPropertyIds() as $propertyId ) {
52+
$claims = new Claims( $this->statements );
53+
$statementList->addStatements( $claims->getClaimsForProperty( $propertyId )->getBestClaims() );
54+
}
55+
56+
return $statementList;
57+
}
58+
59+
private function addStatements( $statements ) {
60+
$this->assertAreStatements( $statements );
61+
62+
foreach ( $statements as $statement ) {
63+
$this->statements[] = $statement;
64+
}
65+
}
66+
67+
private function assertAreStatements( $statements ) {
68+
if ( !is_array( $statements ) && !( $statements instanceof Traversable ) ) {
69+
throw new InvalidArgumentException( '$statements should be an array or a Traversable' );
70+
}
71+
72+
foreach ( $statements as $statement ) {
73+
if ( !( $statement instanceof Statement ) ) {
74+
throw new InvalidArgumentException( 'All elements need to be of type Statement' );
75+
}
76+
}
77+
}
78+
79+
/**
80+
* Returns the property ids used by the statements.
81+
* The keys of the returned array hold the serializations of the property ids.
82+
*
83+
* @return PropertyId[]
84+
*/
85+
public function getPropertyIds() {
86+
$propertyIds = array();
87+
88+
foreach ( $this->statements as $statement ) {
89+
$propertyIds[$statement->getPropertyId()->getSerialization()] = $statement->getPropertyId();
90+
}
91+
92+
return $propertyIds;
93+
}
94+
95+
public function addStatement( Statement $statement ) {
96+
$this->statements[] = $statement;
97+
}
98+
99+
/**
100+
* @param Snak $mainSnak
101+
* @param Snak[]|Snaks|null $qualifiers
102+
* @param Reference[]|References|null $references
103+
* @param string|null $guid
104+
*/
105+
public function addNewStatement( Snak $mainSnak, $qualifiers = null, $references = null, $guid = null ) {
106+
$qualifiers = is_array( $qualifiers ) ? new SnakList( $qualifiers ) : $qualifiers;
107+
$references = is_array( $references ) ? new ReferenceList( $references ) : $references;
108+
109+
$statement = new Statement( $mainSnak, $qualifiers, $references );
110+
$statement->setGuid( $guid );
111+
112+
$this->addStatement( $statement );
113+
}
114+
115+
/**
116+
* Statements that have a main snak already in the list are filtered out.
117+
* The last occurrences are retained.
118+
*
119+
* @return self
120+
*/
121+
public function getWithUniqueMainSnaks() {
122+
$statements = array();
123+
124+
foreach ( $this->statements as $statement ) {
125+
$statements[$statement->getMainSnak()->getHash()] = $statement;
126+
}
127+
128+
return new self( $statements );
129+
}
130+
131+
/**
132+
* @return Traversable
133+
*/
134+
public function getIterator() {
135+
return new \ArrayIterator( $this->statements );
136+
}
137+
138+
/**
139+
* @return Statement[]
140+
*/
141+
public function toArray() {
142+
return $this->statements;
143+
}
144+
145+
}

tests/unit/ByPropertyIdArrayTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Wikibase\Claims;
99
use Wikibase\DataModel\ByPropertyIdArray;
1010
use Wikibase\DataModel\Claim\Claim;
11-
use Wikibase\DataModel\Claim\Statement;
11+
use Wikibase\DataModel\Statement\Statement;
1212
use Wikibase\DataModel\Entity\PropertyId;
1313
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
1414
use Wikibase\DataModel\Snak\PropertySomeValueSnak;

tests/unit/Claim/ClaimListTest.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,6 @@ public function testCanIterate() {
8989
}
9090
}
9191

92-
public function testGetBestClaimPerProperty() {
93-
$list = new ClaimList( array(
94-
$this->getStubClaim( 1, 'one', Claim::RANK_PREFERRED ),
95-
$this->getStubClaim( 1, 'two', Claim::RANK_NORMAL ),
96-
$this->getStubClaim( 1, 'three', Claim::RANK_PREFERRED ),
97-
98-
$this->getStubClaim( 2, 'four', Claim::RANK_DEPRECATED ),
99-
100-
$this->getStubClaim( 3, 'five', Claim::RANK_DEPRECATED ),
101-
$this->getStubClaim( 3, 'six', Claim::RANK_NORMAL ),
102-
103-
$this->getStubClaim( 4, 'seven', Claim::RANK_PREFERRED ),
104-
$this->getStubClaim( 4, 'eight', Claim::RANK_TRUTH ),
105-
) );
106-
107-
$this->assertEquals(
108-
array(
109-
$this->getStubClaim( 1, 'one', Claim::RANK_PREFERRED ),
110-
$this->getStubClaim( 1, 'three', Claim::RANK_PREFERRED ),
111-
112-
$this->getStubClaim( 3, 'six', Claim::RANK_NORMAL ),
113-
114-
$this->getStubClaim( 4, 'eight', Claim::RANK_TRUTH ),
115-
),
116-
$list->getBestClaimPerProperty()->toArray()
117-
);
118-
}
119-
12092
public function testGetUniqueMainSnaksReturnsListWithoutDuplicates() {
12193
$list = new ClaimList( array(
12294
$this->getClaimWithSnak( 1, 'foo' ),

0 commit comments

Comments
 (0)