diff --git "a/include/class-wb\\Claim.php" "b/include/class-wb\\Claim.php" index b1ccabf..f03ad20 100644 --- "a/include/class-wb\\Claim.php" +++ "b/include/class-wb\\Claim.php" @@ -1,170 +1,173 @@ . # 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 { /** * Snak * * @var Snak|null */ //var $mainsnak; //var $id; //var $qualifiers; /** * Constructor * * @param $mainsnak Snak|null Main snak */ public function __construct( $mainsnak = null ) { - $this->setMainsnak( $mainsnak ); + // 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 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->removed ) && $this->removed; + return isset( $this->remove ) && $this->remove; } /** - * Mark this claim as to be removed + * 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->removed = 1; + $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; } } diff --git "a/include/class-wb\\Claims.php" "b/include/class-wb\\Claims.php" index d6aedea..47f350d 100644 --- "a/include/class-wb\\Claims.php" +++ "b/include/class-wb\\Claims.php" @@ -1,112 +1,126 @@ . # 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 ) { - if( $claim->getMainSnak()->getProperty() === $property ) { + $snak = $claim->getMainsnak(); + if( $snak && $snak->getProperty() === $property ) { return true; } } return false; } /** - * Get all the claims indexed by their property + * 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' + * + * @return array + */ + public function getAllGrouped() { $properties = []; - foreach( $this->claims as $claim ) { - $property = $claim->getMainSnak()->getProperty(); + foreach( $this->getAll() as $claim ) { + $snak = $claim->getMainsnak(); + // https://phabricator.wikimedia.org/T203572 + $property = $snak ? $snak->getProperty() : 'ASD-ASD-ASD'; if( ! isset( $properties[ $property ] ) ) { $properties[ $property ] = []; } $properties[ $property ][] = $claim; } return $properties; } } diff --git "a/include/class-wb\\DataModel.php" "b/include/class-wb\\DataModel.php" index d092cac..a281469 100644 --- "a/include/class-wb\\DataModel.php" +++ "b/include/class-wb\\DataModel.php" @@ -1,389 +1,400 @@ . # Wikibase namespace wb; use \mw\WikibaseSite; /** * Wikibase data container */ class DataModel { /** * Dependency injection of a Wikibase site * * @var WikibaseSite|null */ private $site; /** * Dependency injection of the entity Q-ID this data refers to * * @var string|null */ private $entityID; /** * @var Labels */ private $labels; /** * @var Descriptions */ private $descriptions; /* * @var Claims */ private $claims; /** * Constructor * * @param $site WikibaseSite */ public function __construct( $site = null ) { $this->site = $site; $this->labels = new Labels(); $this->descriptions = new Descriptions(); $this->claims = new Claims(); } /** * Get the Wikibase site if available * * @return WikibaseSite */ public function getWikibaseSite() { if( ! isset( $this->site ) ) { throw new \Exception( 'can\'t access to undefined Wikibase site' ); } return $this->site; } /** * Set the entity Q-ID * * @param $entity_ID string Q-ID * @return self */ public function setEntityID( $entity_ID ) { $this->entityID = $entity_ID; return $this; } /** * Get the entity Q-ID * * @return string|null */ public function getEntityID() { return $this->entityID; } /** * Get all the labels * * @return array */ public function getLabels() { return $this->labels->getAll(); } /** * Get all the descriptions * * @return array */ public function getDescriptions() { return $this->descriptions->getAll(); } /** * Get all the claims * * @return array */ public function getClaims() { return $this->claims->getAll(); } + /** + * Get all the claims grouped by property + * + * n.b. The claims without a property will be indexed by 'asd' + * + * @return array + */ + public function getClaimsGrouped() { + return $this->claims->getAllGrouped(); + } + /** * Add a claim * * @param $claim Claim * @return self */ public function addClaim( $claim ) { $this->claims->add( $claim ); return $this; } public function setClaims( $claims ) { $this->claims->set( $claims ); return $this; } /** * Check if some claims exist in a certain property * * @param $property string * @return bool */ public function hasClaimsInProperty( $property ) { return $this->claims->haveProperty( $property ); } public function getClaimsInProperty( $property ) { return $this->claims->getInProperty( $property ); } /** * Count all the claims * * @return int */ public function countClaims() { return $this->claims->count(); } /** * Check if a label of a certain language exists * * @param $language string * @return bool */ public function hasLabelInLanguage( $language ) { return $this->labels->have( $language ); } /** * Set, delete, preserve if it exists, a label. */ public function setLabel( $label ) { $this->labels->set( $label ); return $this; } /** * Check if a label of a certain language exists * * @param $language string * @return bool */ public function hasDescriptionInLanguage( $language ) { return $this->descriptions->have( $language ); } /** * Set, delete, preserve if it exists, a description. * * @param $description Description * @return self */ public function setDescription( $description ) { $this->descriptions->set( $description ); return $this; } /** * Get a pure data rappresentation * * @param $clear bool Flag to be enabled to strip out empty elements * @return array */ public function get( $clear = false ) { $data = [ 'labels' => $this->getLabels(), 'descriptions' => $this->getDescriptions(), - 'claims' => $this->getClaims() + 'claims' => $this->getClaimsGrouped() ]; foreach( $data as $k => $v ) { if( 0 === count( $v ) ) { unset( $data[ $k ] ); } } if( $clear ) { $data['clear'] = true; } return $data; } /** * Get a JSON rappresentation of this data * * @param $args int Options for json_encode() * @return string */ public function getJSON( $options = 0 ) { return json_encode( $this->get(), $options ); } /** * Get a JSON rappresentation of this data (without empty elements) * * @param $args int Options for json_encode() * @return string */ public function getJSONClearing( $args = null ) { return json_encode( $this->get( true ), $args ); } /** * Print the changes in order to confirm them */ public function printChanges() { $labels = $this->getLabels(); if( $labels ) { \cli\Log::info( "Languages:" ); foreach( $labels as $label ) { \cli\Log::info( "\t" . $label ); } } $descriptions = $this->getDescriptions(); if( $descriptions ) { mw\Log::info( "descriptions: "); foreach( $descriptions as $description ) { \cli\Log::info( "\t" . $description ); } } $properties = $this->getClaims(); if( $properties ) { \cli\Log::info( "claims:" ); foreach( $properties as $property => $claims ) { \cli\Log::info( "\t$property:" ); foreach( $claims as $claim ) { if( $snak = $claim->getMainsnak() ) { \cli\Log::info( "\t\t" . $snak->getDataValue() ); } } } } } /** * Generate an edit summary for the data contained in here * * @return string */ public function getEditSummary() { $changes = []; $labels = $this->getLabels(); if( $labels ) { $changes[] = $labels->__toString(); } $descriptions = $this->getDescriptions(); if( $descriptions ) { $changes[] = $descriptions->__toString(); } - $properties = $this->getClaims(); - if( $properties ) { - foreach( $properties as $property => $claims ) { - foreach( $claims as $claim ) { - if( $snak = $claim->getMainsnak() ) { - $changes[] = '+' . $snak; - } - } + foreach( $this->getClaims() as $claim ) { + $snak = $claim->getMainsnak(); + if( $snak ) { + $changes[] = '+' . $snak; + } elseif( $claim->isMarkedForRemoval() ) { + $changes[] = '-whole claim ' . $claim->getID(); + } else { + throw new \Exception( "unexpected undefined main snak, that it's allowed only in claims marked for deletion" ); } } return implode( '; ', $changes ); } /** * Edit a Wikibase entity using the wbgetentities API * * It works only if the Wikibase site is specified * You can not specify the 'id' if you specified the entity ID * * @param $data array API data request * @return mixed * @see https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities */ public function editEntity( $data = [] ) { $data = array_replace( [ 'id' => $this->getEntityID(), 'summary' => $this->getEditSummary(), 'data' => $this->getJSON(), ], $data ); return $this->getWikibaseSite()->editEntity( $data ); } /** * Static constructor from an array * * @param $data array * @param $site WikibaseSite * @return self */ public static function createFromData( $data, $site = null ) { $dataModel = new self( $site ); if( ! empty( $data[ 'labels' ] ) ) { foreach( $data[ 'labels' ] as $label ) { $dataModel->setLabel( Label::createFromData( $label ) ); } } if( ! empty( $data[ 'descriptions' ] ) ) { foreach( $data[ 'descriptions' ] as $description ) { $dataModel->setDescription( Description::createFromData( $description ) ); } } if( ! empty( $data[ 'claims' ] ) ) { foreach( $data[ 'claims' ] as $claims ) { foreach( $claims as $claim ) { $dataModel->addClaim( Claim::createFromData( $claim ) ); } } } return $dataModel; } /** * Static constructor from an object * * @param $object object * @return self */ public static function createFromObject( $object ) { return self::createFromData( self::object2array( $object ) ); } /** * Convert an object to an array * * @param $object object * @return array */ private static function object2array( $object ) { if( ! is_object( $object) && ! is_array( $object ) ) { return $object; } $array = []; foreach( $object as $k => $v ) { $array[ $k ] = self::object2array( $v ); } return $array; } }