diff --git "a/include/class-wb\\Claim.php" "b/include/class-wb\\Claim.php" index f03ad20..9eb9084 100644 --- "a/include/class-wb\\Claim.php" +++ "b/include/class-wb\\Claim.php" @@ -1,173 +1,207 @@ . # Wikibase namespace wb; /** * A Claim consists of a Snak and Qualifiers. * * Optionally, it can have qualifiers. * * @see https://www.wikidata.org/wiki/Wikidata:Glossary#Claim */ class Claim { + /** + * When a claim has not a main snak, we assume that it has this dummy property. + * + * It's very useful to send claims to be deleted. It works. asd + * + * @see https://phabricator.wikimedia.org/T203572 + */ + const DUMMY_PROPERTY = 'ASD-ASD-ASD'; + /** * Snak * * @var Snak|null */ //var $mainsnak; //var $id; //var $qualifiers; /** * Constructor * * @param $mainsnak Snak|null Main snak */ public function __construct( $mainsnak = null ) { // avoid to set a NULL value in order to do not send { 'mainsnak': null } to the API if( $mainsnak ) { $this->setMainsnak( $mainsnak ); } } /** * Get the mainsnak * * @return Snak|null */ public function getMainsnak() { return isset( $this->mainsnak ) ? $this->mainsnak : null; } /** * Get the mainsnak * * @return Snak|null */ public function hasQualifiers() { return ! empty( $this->qualifiers ); } + /** + * Get a property. Also a dummy one. asd + * + * @return string + */ + public function getPropertyAlsoDummy() { + $snak = $this->getMainsnak(); + return $snak ? $snak->getProperty() : self::DUMMY_PROPERTY; + } + /** * Get the qualifiers */ public function getQualifiers() { return $this->qualifiers; } /** * Set the mainsnak * * @param $mainsnak Snak|null * @return self */ public function setMainsnak( $mainsnak ) { $this->mainsnak = $mainsnak; return $this; } /** * Set the qualifiers * * @param $qualifiers * @return self */ public function setQualifiers( $qualifiers ) { $this->qualifiers = $qualifiers; return $this; } /** * Set the claim ID * * @param $id string * @return string */ public function setID( $id ) { $this->id = $id; return $this; } /** * Get the claim ID * * @return string */ public function getID() { if( empty( $this->id ) ) { throw new \Exception( 'missing id' ); } return $this->id; } /** * Check if this claim is marked for removal * * @return array */ public function isMarkedForRemoval() { return isset( $this->remove ) && $this->remove; } /** * Mark this claim as to be remove * * @see https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity * @return self */ public function markForRemoval() { $this->remove = 1; return $this; } /** * Clone this claim and obtain a claim marked for removal * * @return self */ public function cloneForRemoval() { return ( new self() ) ->setID( $this->getID() ) ->markForRemoval(); } /** * Create a claim from raw data * * @param $data array * @return self */ public static function createFromData( $data ) { if( ! isset( $data[ 'mainsnak' ] ) ) { throw new WrongDataException( __CLASS__ ); } $claim = new static( Snak::createFromData( $data[ 'mainsnak' ] ) ); if( isset( $data[ 'qualifiers' ] ) ) { $claim->setQualifiers( $data[ 'qualifiers' ] ); } if( isset( $data[ 'id' ] ) ) { $claim->setID( $data[ 'id' ] ); } return $claim; } + + /** + * @override + */ + public function __toString() { + $snak = $this->getMainsnak(); + if( $snak ) { + return $snak->getDataValue(); + } + $id = $this->getID(); + if( $id && $this->isMarkedForRemoval() ) { + return "remove claim id = $id"; + } + throw new \Exception( 'empty claim' ); + } } diff --git "a/include/class-wb\\Claims.php" "b/include/class-wb\\Claims.php" index 47f350d..9673456 100644 --- "a/include/class-wb\\Claims.php" +++ "b/include/class-wb\\Claims.php" @@ -1,126 +1,124 @@ . # Wikibase namespace wb; /** * Claim collector */ class Claims { /** * All the claims * * @TODO: check if it can be declared as private without breaking casts :^) * @var array */ public $claims = []; /** * Constructor * * @param $claims array */ public function __construct( $claims = [] ) { foreach( $claims as $claim ) { $this->add( $claim ); } } /** * Add a claim * * @param $claim $Claim * @return self */ public function add( Claim $claim ) { $this->claims[] = $claim; return $this; } /** * Count all * * @return int */ public function count() { return count( $this->claims ); } /** * Get all the claims in a certain property * * @param $property string * @return array */ public function getInProperty( $property ) { $claims = []; foreach( $this->claims as $claim ) { if( $claim->getMainSnak()->getProperty() === $property ) { $claims[] = $claim; } } return $claims; } /** * Check if there are claims in a certain property * * @param $property string * @return bool */ public function haveProperty( $property ) { foreach( $this->claims as $claim ) { $snak = $claim->getMainsnak(); if( $snak && $snak->getProperty() === $property ) { return true; } } return false; } /** * Get all the claims * * @return array */ public function getAll() { return $this->claims; } /** * Get all the claims indexed by their property * - * n.b. The claims without a property will be indexed by 'asd' + * Note that you can obtain also a dummy property * * @return array */ public function getAllGrouped() { $properties = []; foreach( $this->getAll() as $claim ) { - $snak = $claim->getMainsnak(); - // https://phabricator.wikimedia.org/T203572 - $property = $snak ? $snak->getProperty() : 'ASD-ASD-ASD'; + $property = $claim->getPropertyAlsoDummy(); if( ! isset( $properties[ $property ] ) ) { $properties[ $property ] = []; } $properties[ $property ][] = $claim; } return $properties; } }