Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Claims extends \Wikibase\DataModel\Claim\Claims {}
/**
* @deprecated since 0.6, use the base class instead.
*/
class Statement extends \Wikibase\DataModel\Claim\Statement {}
class Statement extends DataModel\Statement\Statement {}
/**
* @deprecated since 0.6, use the base class instead.
*/
Expand Down Expand Up @@ -160,3 +160,12 @@ class ItemDiff extends \Wikibase\DataModel\Entity\Diff\ItemDiff {}
class EntityDiff extends \Wikibase\DataModel\Entity\Diff\EntityDiff {}

}

namespace Wikibase\DataModel\Claim {

/**
* @deprecated since 1.0, use the base class instead.
*/
class Statement extends \Wikibase\DataModel\Statement\Statement {}

}
2 changes: 2 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Other breaking changes:
#### Additions

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

## Version 0.9 (2014-08-15)

Expand Down
3 changes: 2 additions & 1 deletion WikibaseDataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class_alias( 'Wikibase\DataModel\ByPropertyIdArray', 'Wikibase\ByPropertyIdArray
class_alias( 'Wikibase\DataModel\Claim\Claim', 'Wikibase\Claim' );
class_alias( 'Wikibase\DataModel\Claim\ClaimListAccess', 'Wikibase\ClaimListAccess' );
class_alias( 'Wikibase\DataModel\Claim\Claims', 'Wikibase\Claims' );
class_alias( 'Wikibase\DataModel\Claim\Statement', 'Wikibase\Statement' );
class_alias( 'Wikibase\DataModel\Statement\Statement', 'Wikibase\Statement' );
class_alias( 'Wikibase\DataModel\Entity\Entity', 'Wikibase\Entity' );
class_alias( 'Wikibase\DataModel\Entity\Item', 'Wikibase\Item' );
class_alias( 'Wikibase\DataModel\Entity\Property', 'Wikibase\Property' );
Expand All @@ -59,3 +59,4 @@ class_alias( 'Wikibase\DataModel\LegacyIdInterpreter', 'Wikibase\DataModel\Inter
// Aliases introduced in 1.0
class_alias( 'Wikibase\DataModel\Entity\Diff\EntityDiff', 'Wikibase\DataModel\Entity\EntityDiff' );
class_alias( 'Wikibase\DataModel\Entity\Diff\ItemDiff', 'Wikibase\DataModel\Entity\ItemDiff' );
class_alias( 'Wikibase\DataModel\Statement\Statement', 'Wikibase\DataModel\Claim\Statement' );
1 change: 1 addition & 0 deletions src/Claim/Claim.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Wikibase\DataModel\Snak\Snak;
use Wikibase\DataModel\Snak\SnakList;
use Wikibase\DataModel\Snak\Snaks;
use Wikibase\DataModel\Statement\Statement;

/**
* Class that represents a single Wikibase claim.
Expand Down
26 changes: 0 additions & 26 deletions src/Claim/ClaimList.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,6 @@ public function __construct( $claims = array() ) {
$this->claims = $claims;
}

/**
* Returns the best claims per property.
* The best claims are those with the highest rank for a particular property.
* Deprecated ranks are never included.
*
* Caution: the ranking is done per property, not globally, as in the Claims class.
*
* @return self
*/
public function getBestClaimPerProperty() {
$claimList = new self();

foreach ( $this->getPropertyIds() as $propertyId ) {
$claims = new Claims( $this->claims );
$claimList->addClaims( $claims->getClaimsForProperty( $propertyId )->getBestClaims() );
}

return $claimList;
}

/**
* Returns the property ids used by the claims.
* The keys of the returned array hold the serializations of the property ids.
Expand All @@ -77,12 +57,6 @@ public function getPropertyIds() {
return $propertyIds;
}

private function addClaims( Claims $claims ) {
foreach ( $claims as $claim ) {
$this->addClaim( $claim );
}
}

public function addClaim( Claim $claim ) {
$this->claims[] = $claim;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use OutOfBoundsException;
use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Claim\Claims;
use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Entity\Diff\EntityDiff;
use Wikibase\DataModel\Entity\Diff\ItemDiff;
use Wikibase\DataModel\SiteLink;
Expand Down Expand Up @@ -339,7 +339,7 @@ public function addClaim( Claim $claim ) {
/**
* @since 1.0
*
* @return Statement[]
* @return \Wikibase\DataModel\Statement\Statement[]
*/
public function getStatements() {
return $this->statements;
Expand Down
3 changes: 2 additions & 1 deletion src/Claim/Statement.php → src/Statement/Statement.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace Wikibase\DataModel\Claim;
namespace Wikibase\DataModel\Statement;

use InvalidArgumentException;
use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Reference;
use Wikibase\DataModel\ReferenceList;
use Wikibase\DataModel\References;
Expand Down
145 changes: 145 additions & 0 deletions src/Statement/StatementList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Wikibase\DataModel\Statement;

use InvalidArgumentException;
use Traversable;
use Wikibase\DataModel\Claim\Claims;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Reference;
use Wikibase\DataModel\ReferenceList;
use Wikibase\DataModel\References;
use Wikibase\DataModel\Snak\Snak;
use Wikibase\DataModel\Snak\SnakList;
use Wikibase\DataModel\Snak\Snaks;

/**
* Ordered, non-unique, collection of Statement objects.
* Provides various filter operations though does not do any indexing by default.
*
* @since 1.0
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class StatementList implements \IteratorAggregate {

/**
* @var Statement[]
*/
private $statements = array();

/**
* @param Statement[]|Traversable $statements
*
* @throws InvalidArgumentException
*/
public function __construct( $statements = array() ) {
$this->addStatements( $statements );
}

/**
* Returns the best statements per property.
* The best statements are those with the highest rank for a particular property.
* Deprecated ranks are never included.
*
* @return self
*/
public function getBestStatementPerProperty() {
$statementList = new self();

foreach ( $this->getPropertyIds() as $propertyId ) {
$claims = new Claims( $this->statements );
$statementList->addStatements( $claims->getClaimsForProperty( $propertyId )->getBestClaims() );
}

return $statementList;
}

private function addStatements( $statements ) {
$this->assertAreStatements( $statements );

foreach ( $statements as $statement ) {
$this->statements[] = $statement;
}
}

private function assertAreStatements( $statements ) {
if ( !is_array( $statements ) && !( $statements instanceof Traversable ) ) {
throw new InvalidArgumentException( '$statements should be an array or a Traversable' );
}

foreach ( $statements as $statement ) {
if ( !( $statement instanceof Statement ) ) {
throw new InvalidArgumentException( 'All elements need to be of type Statement' );
}
}
}

/**
* Returns the property ids used by the statements.
* The keys of the returned array hold the serializations of the property ids.
*
* @return PropertyId[]
*/
public function getPropertyIds() {
$propertyIds = array();

foreach ( $this->statements as $statement ) {
$propertyIds[$statement->getPropertyId()->getSerialization()] = $statement->getPropertyId();
}

return $propertyIds;
}

public function addStatement( Statement $statement ) {
$this->statements[] = $statement;
}

/**
* @param Snak $mainSnak
* @param Snak[]|Snaks|null $qualifiers
* @param Reference[]|References|null $references
* @param string|null $guid
*/
public function addNewStatement( Snak $mainSnak, $qualifiers = null, $references = null, $guid = null ) {
$qualifiers = is_array( $qualifiers ) ? new SnakList( $qualifiers ) : $qualifiers;
$references = is_array( $references ) ? new ReferenceList( $references ) : $references;

$statement = new Statement( $mainSnak, $qualifiers, $references );
$statement->setGuid( $guid );

$this->addStatement( $statement );
}

/**
* Statements that have a main snak already in the list are filtered out.
* The last occurrences are retained.
*
* @return self
*/
public function getWithUniqueMainSnaks() {
$statements = array();

foreach ( $this->statements as $statement ) {
$statements[$statement->getMainSnak()->getHash()] = $statement;
}

return new self( $statements );
}

/**
* @return Traversable
*/
public function getIterator() {
return new \ArrayIterator( $this->statements );
}

/**
* @return Statement[]
*/
public function toArray() {
return $this->statements;
}

}
2 changes: 1 addition & 1 deletion tests/unit/ByPropertyIdArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Wikibase\Claims;
use Wikibase\DataModel\ByPropertyIdArray;
use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
Expand Down
28 changes: 0 additions & 28 deletions tests/unit/Claim/ClaimListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,6 @@ public function testCanIterate() {
}
}

public function testGetBestClaimPerProperty() {
$list = new ClaimList( array(
$this->getStubClaim( 1, 'one', Claim::RANK_PREFERRED ),
$this->getStubClaim( 1, 'two', Claim::RANK_NORMAL ),
$this->getStubClaim( 1, 'three', Claim::RANK_PREFERRED ),

$this->getStubClaim( 2, 'four', Claim::RANK_DEPRECATED ),

$this->getStubClaim( 3, 'five', Claim::RANK_DEPRECATED ),
$this->getStubClaim( 3, 'six', Claim::RANK_NORMAL ),

$this->getStubClaim( 4, 'seven', Claim::RANK_PREFERRED ),
$this->getStubClaim( 4, 'eight', Claim::RANK_TRUTH ),
) );

$this->assertEquals(
array(
$this->getStubClaim( 1, 'one', Claim::RANK_PREFERRED ),
$this->getStubClaim( 1, 'three', Claim::RANK_PREFERRED ),

$this->getStubClaim( 3, 'six', Claim::RANK_NORMAL ),

$this->getStubClaim( 4, 'eight', Claim::RANK_TRUTH ),
),
$list->getBestClaimPerProperty()->toArray()
);
}

public function testGetUniqueMainSnaksReturnsListWithoutDuplicates() {
$list = new ClaimList( array(
$this->getClaimWithSnak( 1, 'foo' ),
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Claim/ClaimStandaloneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Wikibase\Test;

use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
use Wikibase\DataModel\Snak\SnakList;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Claim/ClaimsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Wikibase\DataModel\ByPropertyIdArray;
use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Claim\Claims;
use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Reference;
use Wikibase\DataModel\ReferenceList;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Entity/Diff/ItemDifferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpChange;
use Diff\DiffOp\DiffOpRemove;
use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Entity\Diff\ItemDiffer;
use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpChange;
use Diff\DiffOp\DiffOpRemove;
use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Entity\Diff\EntityDiff;
use Wikibase\DataModel\Entity\Entity;
use Wikibase\DataModel\Entity\Item;
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Entity/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Diff\DiffOp\DiffOpRemove;
use Wikibase\DataModel\Claim\Claim;
use Wikibase\DataModel\Claim\Claims;
use Wikibase\DataModel\Claim\Statement;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Entity\Diff\ItemDiff;
use Wikibase\DataModel\Entity\EntityIdValue;
use Wikibase\DataModel\Entity\Item;
Expand Down Expand Up @@ -691,7 +691,7 @@ public function testNewClaimReturnsStatementWithProvidedMainSnak() {
$item = Item::newEmpty();
$statement = $item->newClaim( $snak );

$this->assertInstanceOf( 'Wikibase\DataModel\Claim\Statement', $statement );
$this->assertInstanceOf( 'Wikibase\DataModel\Statement\Statement', $statement );
$this->assertEquals( $snak, $statement->getMainSnak() );
}

Expand Down
Loading