diff --git "a/class-wb\\DataModel.php" "b/class-wb\\DataModel.php" index f4d7735..20e28a0 100644 --- "a/class-wb\\DataModel.php" +++ "b/class-wb\\DataModel.php" @@ -1,252 +1,318 @@ . # Wikibase namespace wb; /** * Wikibase data container */ class DataModel { /** * @var Labels */ private $labels; /** * @var Descriptions */ private $descriptions; /* * @var Claims */ private $claims; /** * Constructor */ public function __construct() { $this->labels = new Labels(); $this->descriptions = new Descriptions(); $this->claims = new Claims(); } /** * 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(); } /** * 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() ]; 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; + } + } + } + } + + return implode( '; ', $changes ); + } + /** * Static constructor from an array * * @param $data array * @return self */ public static function createFromData( $data ) { $dataModel = new self(); 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; } }