diff --git "a/class-wb\\DataValue.php" "b/class-wb\\DataValue.php" index 7c703d5..4465a40 100644 --- "a/class-wb\\DataValue.php" +++ "b/class-wb\\DataValue.php" @@ -1,62 +1,69 @@ . # Wikibase namespace wb; /** * A generic DataValue is part of a Snak. */ class DataValue { var $type; var $value; /** * @param $type string Type * @param $value mixed Value */ public function __construct( $type, $value ) { $this->setType( $type ) ->setValue( $value ); } public function getType() { return $this->type; } public function getValue() { return $this->value; } public function setType( $type ) { $this->type = $type; return $this; } public function setValue( $value ) { $this->value = $value; return $this; } public static function createFromData( $data ) { if( ! isset( $data['type'], $data['value'] ) ) { throw new WrongDataException( __CLASS__ ); } return new self( $data['type'], $data['value'] ); } + + /** + * @return string + */ + public function __toString() { + return $this->getValue(); + } } diff --git "a/class-wb\\DataValueGlobeCoordinate.php" "b/class-wb\\DataValueGlobeCoordinate.php" index a76414e..2251af4 100644 --- "a/class-wb\\DataValueGlobeCoordinate.php" +++ "b/class-wb\\DataValueGlobeCoordinate.php" @@ -1,46 +1,56 @@ . # Wikibase DataValue namespace wb; /** * A DataValue for a globe coordinate. */ class DataValueGlobeCoordinate extends DataValue { /** * @param $latitude float * @param $longitude float * @param $precision float * @param $altitude float * @param $globe string */ public function __construct( $latitude, $longitude, $precision, $altitude = null, $globe = null ) { if( null === $globe ) { $globe = 'Q2'; } parent::__construct( DataType::GLOBE_COORDINATE, [ 'latitude' => $latitude, 'longitude' => $longitude, 'precision' => $precision, 'altitude' => $altitude, 'globe' => "http://www.wikidata.org/entity/$globe" ] ); } + /** + * @return string + */ + public function __toString() { + $value = $this->getValue(); + return sprintf( '%f %f', + $value['latitude'], + $value['longitude'] + ); + } } diff --git "a/class-wb\\DataValueItem.php" "b/class-wb\\DataValueItem.php" index 71c5db1..482cc3f 100644 --- "a/class-wb\\DataValueItem.php" +++ "b/class-wb\\DataValueItem.php" @@ -1,36 +1,46 @@ . # Wikibase namespace wb; /** * A DataValue for a wikibase Item. */ class DataValueItem extends DataValue { /** * @param $qcode string Entity Q-code as 'Q1' */ public function __construct( $qcode ) { parent::__construct( DataType::ENTITY_ID, [ 'entity-type' => 'item', 'numeric-id' => Item::numericQCode( $qcode ), 'id' => $qcode ] ); } + + /** + * @return string + */ + public function __toString() { + return sprintf( + '[[%s]]', + $this->getValue()['id'] + ); + } } diff --git "a/class-wb\\DataValueQuantity.php" "b/class-wb\\DataValueQuantity.php" index 058da89..e5c3d14 100644 --- "a/class-wb\\DataValueQuantity.php" +++ "b/class-wb\\DataValueQuantity.php" @@ -1,55 +1,65 @@ . # Wikibase namespace wb; /** * A DataValue for a quantity. */ class DataValueQuantity extends DataValue { /** * @param $value mixed amount Any number e.g. 0 * @param $wikidata_unit_id string Wikidata ID of a unit e.g. 'Q11573' for metre * @param $upper_bound mixed The $value at its maximum error e.g. +1 * @param $lower_bound mixed The $value at its minimum error e.g. -1 */ public function __construct( $amount, $wikidata_unit_id, $upper_bound = null, $lower_bound = null ) { $value = [ 'amount' => self::int2string( $amount ), 'unit' => "http://www.wikidata.org/entity/$wikidata_unit_id" ]; if( null !== $upper_bound ) { $value['upperBound'] = self::int2string( $upper_bound ); } if( null !== $lower_bound ) { $value['lowerBound'] = self::int2string( $lower_bound ); } parent::__construct( DataType::QUANTITY, $value ); } /** * Convert a number to a prefixed string e.g. 1 is converted to '+1' * * @param $n mixed * @return string */ private static function int2string( $n ) { return $n > 0 ? '+' . $n : '-' . $n; } + + /** + * @return string + */ + public function __toString() { + $value = $this->getValue(); + return sprintf( '%f', + $value['amount'] + ); + } } diff --git "a/class-wb\\DataValueTime.php" "b/class-wb\\DataValueTime.php" index 84bda23..bc93302 100644 --- "a/class-wb\\DataValueTime.php" +++ "b/class-wb\\DataValueTime.php" @@ -1,65 +1,149 @@ . # Wikibase namespace wb; +use DateTime; + /** * A DataValue for a time. */ class DataValueTime extends DataValue { const PRECISION_YEARS = 9; const PRECISION_MONTHS = 10; const PRECISION_DAYS = 11; + /** + * Gregorian calendar + * + * @var string + */ + static $DEFAULT_CALENDAR = 'Q1985727'; + + /** + * Human precisions indexed + * + * @var array + */ + static $HUMAN_PRECISIONS = [ + '1 Gigayear', + '100 Megayears', + '10 Megayears', + 'Megayear', + '100 Kiloyears', + '10 Kiloyears', + 'Kiloyear', + '100 years', + '10 years', + 'years', + 'months', + 'days', + 'hours', // unused + 'minutes', // unused + 'seconds', // unused + ]; + /** * @param $time string example "+1539-00-00T00:00:00Z" * @param $precision int * 0: 1 Gigayear * 1: 100 Megayears * 2: 10 Megayears * 3: Megayear * 4: 100 Kiloyears * 5: 10 Kiloyears * 6: Kiloyear * 7: 100 years * 8: 10 years * 9: years * 10: months * 11: days * @param $args array example: * timezone: 0, * before: 0, * after: 0, * precision: 9, - * calendarmodel: "Q1985786" + * calendarmodel: "Q12138" * @see https://www.mediawiki.org/wiki/Wikibase/DataModel/JSON */ public function __construct( $time, $precision, $args = [] ) { $args = array_replace( [ 'time' => $time, 'precision' => $precision, - 'calendarmodel' => 'Q1985786', + 'calendarmodel' => self::$DEFAULT_CALENDAR, + 'timezone' => 0, // unused + 'before' => 0, // unused + 'after' => 0, // unused ], $args ); - $args['calendarmodel'] = "http://www.wikidata.org/entity/{$args['calendarmodel']}"; + $args['calendarmodel'] = 'http://www.wikidata.org/entity/' . $args['calendarmodel']; parent::__construct( DataType::TIME, $args ); } + + /** + * Return an human rappresentation of the precision + * + * @param $precision int + * @return string + */ + public static function humanPrecision( $precision ) { + return self::$HUMAN_PRECISIONS[ $precision ]; + } + + /** + * Return an human rappresentation of the time + * + * @param $time string + * @param $precision int + * @return string + */ + public static function humanTime( $time, $precision ) { + $sign = substr( $time, 0, 1 ); + $rest = substr( $time, 1 ); + $date = new DateTime( $rest ); + return $date->format( self::humanTimeFormat( $precision ) ); + } + + /** + * Return a format for the time + * + * @param $time string + * @return string + */ + private static function humanTimeFormat( $precision ) { + if( 9 >= $precision ) return 'Y'; + if( self::PRECISION_MONTHS === $precision ) return 'Y-m'; + if( self::PRECISION_DAYS === $precision ) return 'Y-m-d'; + return 'Y-m-d H:i:s'; + } + + /** + * @return string + */ + public function __toString() { + $value = $this->getValue(); + $precision = $value['precision']; + return sprintf( "%s±%s", + self::humanTime( $value['time'], $precision ), + self::humanPrecision( $precision ) + ); + } } diff --git "a/class-wb\\Snak.php" "b/class-wb\\Snak.php" index 186fca1..8141801 100644 --- "a/class-wb\\Snak.php" +++ "b/class-wb\\Snak.php" @@ -1,115 +1,125 @@ . # Wikibase namespace wb; /** * A generic Snak is a combination of a property and a datatype + datavalue. * * A Snak is part of a Claim or a Reference. It's based on a DataValue. */ class Snak { //var $hash; var $snaktype; var $property; var $datatype; var $datavalue; /** * @param $snaktype string * @param $property string * @param $datatype string * @param $datavalue mixed */ public function __construct( $snaktype, $property, $datatype, $datavalue = null ) { $this->setSnakType( $snaktype ) ->setProperty( $property ) ->setDataType( $datatype ); if( null !== $datavalue ) { $this->setDataValue( $datavalue ); } } public function getProperty() { return $this->property; } public function getSnakType() { return $this->snaktype; } public function getDataType() { return $this->datatype; } public function getDataValue() { return $this->datavalue; } public function setSnakType( $snaktype ) { $this->snaktype = $snaktype; return $this; } public function setProperty( $property ) { $this->property = $property; return $this; } public function setDataType( $datatype ) { $this->datatype = $datatype; return $this; } public function setDataValue( DataValue $datavalue ) { $this->datavalue = $datavalue; return $this; } public function hasHash() { return isset( $this->hash ); } public function getHash() { return $this->hash; } public function setHash( $hash ) { $this->hash = $hash; } public static function createFromData( $data ) { if( ! isset( $data['snaktype'], $data['property'], $data['datatype'] ) ) { throw new WrongDataException( __CLASS__ ); } $snak = new self( $data['snaktype'], $data['property'], $data['datatype'] ); if( isset( $data['datavalue'] ) ) { $snak->setDataValue( DataValue::createFromData( $data['datavalue'] ) ); } if( isset( $data['hash'] ) ) { $snak->setHash( $data['hash'] ); } return $snak; } + + /** + * @return string + */ + public function __toString() { + return sprintf( '%s = %s', + $this->getProperty(), + $this->getDataValue() + ); + } }