diff --git "a/include/class-wb\\DataModel.php" "b/include/class-wb\\DataModel.php" index 391d990..b542d6e 100644 --- "a/include/class-wb\\DataModel.php" +++ "b/include/class-wb\\DataModel.php" @@ -1,445 +1,509 @@ . # 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; + /* + * @var Sitelinks + */ + private $sitelinks; + /** * Constructor * * @param $site WikibaseSite * @param $entity_id string Entity Q-ID */ public function __construct( $site = null, $entity_id = null ) { $this->site = $site; $this->labels = new Labels(); $this->descriptions = new Descriptions(); $this->claims = new Claims(); + $this->setSitelinks( new Sitelinks() ); if( $entity_id ) { $this->setEntityID( $entity_id ); } } /** * Get the Wikibase site * * @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; } /** * Check if it has the entity Q-ID * * @return bool */ public function hasEntityID() { return isset( $this->entityID ); } /** * Get the entity Q-ID * * @return string */ public function getEntityID() { if( isset( $this->entityID ) ) { return $this->entityID; } throw new \Exception( 'undefined entity ID' ); } /** * 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 sitelinks + * + * @return array + */ + public function getSitelinks() { + return $this->sitelinks; + } + + /** + * Set all the sitelinks + * + * @param $sitelinks + * @return array + */ + public function setSitelinks( Sitelinks $sitelinks ) { + $this->sitelinks = $sitelinks; + return $this; + } + /** * 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; } + /** + * Set claims + * + * @param $claims array + * @return self + */ 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 ); } + /** + * Get claims in a property + * + * @param $property string + * @return array + */ public function getClaimsInProperty( $property ) { return $this->claims->getInProperty( $property ); } + /** + * Has sitelink in site + * + * @param $site string + * @return boolean + */ + public function hasSitelinkInSite( $site ) { + return false !== $this->getSitelinkInSite( $site ); + } + /** * 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(), + 'labels' => $this->getLabels(), // TODO: $this->getLabelsData() + 'descriptions' => $this->getDescriptions(), // TODO: $this->getDescriptionsData() + 'sitelinks' => $this->sitelinks->toData(), '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 * * @return self */ public function printChanges() { if( $this->hasEntityID() ) { \cli\Log::info( "-- changes for {$this->getEntityID()} --" ); } $labels = $this->getLabels(); if( $labels ) { \cli\Log::info( "labels:" ); 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->getClaimsGrouped(); if( $properties ) { \cli\Log::info( "claims:" ); foreach( $properties as $property => $claims ) { if( $property === Claim::DUMMY_PROPERTY ) { \cli\Log::info( "\twithout property:" ); } else { \cli\Log::info( "\t$property:" ); } foreach( $claims as $claim ) { $snak = $claim->getMainsnak(); if( $snak ) { \cli\Log::info( "\t\t" . $snak->getDataValue() ); } else { \cli\Log::info( "\t\t" . $claim ); } } } } return $this; } /** * Generate an edit summary for the data contained in here * * @return string */ public function getEditSummary() { $changes = []; $labels = $this->getLabels(); if( $labels ) { $changes[] = $this->labels->__toString(); } $descriptions = $this->getDescriptions(); if( $descriptions ) { $changes[] = $this->descriptions->__toString(); } + $sitelinks = $this->getSitelinks(); + if( $sitelinks->getAll() ) { + $changes[] = $this->sitelinks->__toString(); + } + foreach( $this->getClaims() as $claim ) { $snak = $claim->getMainsnak(); if( $snak ) { $changes[] = '+' . $snak; } elseif( $claim->isMarkedForRemoval() ) { $changes[] = '-claim id=' . $claim->getID(); } else { throw new \Exception( "unexpected undefined main snak, that it's allowed only in claims marked for deletion" ); } } return implode( '; ', $changes ); } /** * Obtain an empty clone of this data container * * @return self */ public function cloneEmpty() { $cloned = new self( $this->getWikibaseSite() ); if( $this->hasEntityID() ) { $cloned->setEntityID( $this->getEntityID() ); } return $cloned; } /** * 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 + * @see https://www.wikidata.org/w/api.php?action=help&modules=wbeditentity */ public function editEntity( $data = [] ) { $data = array_replace( [ 'id' => $this->hasEntityID() ? $this->getEntityID() : null, 'summary' => $this->getEditSummary(), 'data' => $this->getJSON(), ], $data ); return $this->getWikibaseSite()->editEntity( $data ); } /** - * Static constructor from an array + * Static constructor from an associative array + * + * This method is used to import an array obtained + * from JSON-decoding the Wikibase wbgetentity API result * - * @param $data array + * @param $data array Response of wbgetentity API result * @param $site WikibaseSite * @param $entity_id string * @return self + * @see https://www.wikidata.org/w/api.php?action=help&modules=wbgetentities */ public static function createFromData( $data, $site = null, $entity_id = null ) { $dataModel = new self( $site, $entity_id ); if( ! empty( $data[ 'labels' ] ) ) { + // TODO: Labels::createFromData() foreach( $data[ 'labels' ] as $label ) { $dataModel->setLabel( Label::createFromData( $label ) ); } } if( ! empty( $data[ 'descriptions' ] ) ) { + // TODO: Descriptions::createFromData() foreach( $data[ 'descriptions' ] as $description ) { $dataModel->setDescription( Description::createFromData( $description ) ); } } if( ! empty( $data[ 'claims' ] ) ) { + // TODO: Claims::createFromData() foreach( $data[ 'claims' ] as $claims ) { foreach( $claims as $claim ) { $dataModel->addClaim( Claim::createFromData( $claim ) ); } } } + if( ! empty( $data[ 'sitelinks' ] ) ) { + $dataModel->setSitelinks( Sitelinks::createFromData( $data[ 'sitelinks' ] ) ); + } return $dataModel; } /** * Static constructor from an object * * @param $object object * @param $site WikibaseSite * @param $entity_id string * @return self */ public static function createFromObject( $object, $site = null, $entity_id = null ) { return self::createFromData( self::object2array( $object ), $site, $entity_id ); } /** * 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; } } diff --git "a/include/class-wb\\Sitelink.php" "b/include/class-wb\\Sitelink.php" new file mode 100644 index 0000000..868e0fa --- /dev/null +++ "b/include/class-wb\\Sitelink.php" @@ -0,0 +1,148 @@ +. + +# Wikibase +namespace wb; + +/** + * An entity Sitelink + * + * @see https://www.wikidata.org/wiki/Wikidata:Glossary#Sitelink + */ +class Sitelink { + + /** + * @var string + */ + private $site; + + /** + * @var string + */ + private $title; + + /** + * @var array + */ + private $badges = []; + + /** + * Constructor + * + * @param $site string + * @param $title string + */ + public function __construct( $site, $title, $badges = [] ) { + $this->setsite( $site ) + ->setTitle( $title ) + ->setBadges( $badges ); + } + + /** + * Get the site + * + * @return string + */ + public function getSite() { + return $this->site; + } + + /** + * Get the title + * + * @return string + */ + public function getTitle() { + return $this->title; + } + + /** + * Set the site + * + * @param $site string + * @return self + */ + public function setSite( $site ) { + $this->site = $site; + return $this; + } + + /** + * Get the badges + * + * @return array + */ + public function getBadges() { + return $this->badges; + } + + /** + * Set the title + * + * @param $site string + * @return self + */ + public function setTitle( $title ) { + $this->title = $title; + return $this; + } + + /** + * Set the badges + * + * @param $badges array + * @return self + */ + public function setBadges( $badges ) { + $this->badges = $badges; + return $this; + } + + /** + * Static constructor from an array of data + * + * @param $data array + * @return self + */ + public static function createFromData( $data ) { + if( ! isset( $data[ 'site' ], $data[ 'title' ] ) ) { + throw new WrongDataException( __CLASS__ ); + } + return new self( $data[ 'site' ], $data[ 'title' ] ); + } + + /** + * Get as an associative array + * + * @return array + */ + public function toData() { + return [ + 'site' => $this->getSite(), + 'title' => $this->getTitle(), + ]; + } + + /** + * String rappresentation + * + * @return string + */ + public function __toString() { + return sprintf( '%s: %s', $this->getSite(), $this->getTitle() ); + } +} diff --git "a/include/class-wb\\Sitelinks.php" "b/include/class-wb\\Sitelinks.php" new file mode 100644 index 0000000..1785776 --- /dev/null +++ "b/include/class-wb\\Sitelinks.php" @@ -0,0 +1,137 @@ +. + +# Wikibase +namespace wb; + +/** + * Sitelink collector + */ +class Sitelinks { + + /** + * @var array + */ + private $sitelinks = []; + + /** + * Constructor + * + * @param $sitelinks array + */ + public function __construct( $sitelinks = [] ) { + foreach( $sitelinks as $sitelink ) { + $this->set( $sitelink ); + } + } + + /** + * Get a certain site + * + * @param $site string + * @return Sitelink|false + */ + public function get( $site ) { + if( isset( $this->sitelinks[ $site ] ) ) { + return $this->sitelinks[ $site ]; + } + return false; + } + + /** + * Does it have a certain sitelink? + * + * @param $site string + * @return bool + */ + public function have( $site ) { + return false !== $this->get( $site ); + } + + /** + * Set/add a certain sitelink + * + * @param $sitelink Sitelink + * @return self + */ + public function set( Sitelink $sitelink ) { + $this->sitelinks[ $sitelink->getSite() ] = $sitelink; + return $this; + } + + /** + * Get all the sitelinks + * + * @return array + */ + public function getAll() { + return $this->sitelinks; + } + + /** + * Get as an associative array + * + * @return array + */ + public function toData() { + $all = $this->getAll(); + foreach( $all as $site => $sitelink ) { + $all[ $site ] = $sitelink->toData(); + } + return $all; + } + + /** + * Create from an associative array (JSON response part) + * + * @param $data array + * @return self + */ + public static function createFromData( $data ) { + if( ! is_array( $data ) ) { + throw new WrongDataException( __CLASS__ ); + } + $sitelinks = new self(); + foreach( $data as $sitelink ) { + $sitelinks->set( Sitelink::createFromData( $sitelink ) ); + } + return $sitelinks; + } + + /** + * Get all the sites imploded + * + * @param $glue string + * @return string + */ + protected function getImplodedSites( $glue = ',' ) { + $codes = []; + foreach( $this->getAll() as $sitelink ) { + $codes[] = $sitelink->getSite(); + } + return implode( $glue, $codes ); + } + + /** + * String rappresentation + * + * @return string + */ + public function __toString() { + return sprintf( 'sitelink: %s', $this->getImplodedsites() ); + } +}