Jeroen De Dauw has uploaded a new change for review.
https://gerrit.wikimedia.org/r/52197
Change subject: Added getWhereConditions and getInsertValues to the DVHandlers
......................................................................
Added getWhereConditions and getInsertValues to the DVHandlers
Change-Id: I99120fe730a1abb2f1e4e01bceef8215b9af1c8f
---
M repo/includes/Query/SQLStore/DVHandler/GeoCoordinateHandler.php
M repo/includes/Query/SQLStore/DataValueHandler.php
M
repo/tests/phpunit/includes/Query/SQLStore/DVHandler/GeoCoordinateHandlerTest.php
M repo/tests/phpunit/includes/Query/SQLStore/DataValueHandlerTest.php
4 files changed, 198 insertions(+), 19 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/97/52197/1
diff --git a/repo/includes/Query/SQLStore/DVHandler/GeoCoordinateHandler.php
b/repo/includes/Query/SQLStore/DVHandler/GeoCoordinateHandler.php
index 62c9d98..5b6c7ca 100644
--- a/repo/includes/Query/SQLStore/DVHandler/GeoCoordinateHandler.php
+++ b/repo/includes/Query/SQLStore/DVHandler/GeoCoordinateHandler.php
@@ -6,6 +6,8 @@
use Wikibase\Repo\Database\TableDefinition;
use Wikibase\Repo\Database\FieldDefinition;
use DataValues\DataValue;
+use DataValues\GeoCoordinateValue;
+use InvalidArgumentException;
/**
* Represents the mapping between DataValues\GeoCoordinateValue and
@@ -48,6 +50,7 @@
new FieldDefinition( 'lat',
FieldDefinition::TYPE_FLOAT, false ),
new FieldDefinition( 'lon',
FieldDefinition::TYPE_FLOAT, false ),
new FieldDefinition( 'alt',
FieldDefinition::TYPE_FLOAT, true ),
+ new FieldDefinition( 'globe',
FieldDefinition::TYPE_TEXT, true ),
new FieldDefinition( 'json',
FieldDefinition::TYPE_TEXT, false ),
);
@@ -77,16 +80,16 @@
}
/**
- * @see DataValueHandler::newDataValueFromDbValue
+ * @see DataValueHandler::newDataValueFromValueField
*
* @since wd.qe
*
- * @param $dbValue // TODO: mixed or string?
+ * @param $valueFieldValue // TODO: mixed or string?
*
* @return DataValue
*/
- public function newDataValueFromDbValue( $dbValue ) {
- // TODO
+ public function newDataValueFromValueField( $valueFieldValue ) {
+ return GeoCoordinateValue::newFromArray( json_decode(
$valueFieldValue, true ) );
}
/**
@@ -100,4 +103,61 @@
return null;
}
+ /**
+ * @see DataValueHandler::getWhereConditions
+ *
+ * @since wd.qe
+ *
+ * @param DataValue $value
+ *
+ * @return array
+ * @throws InvalidArgumentException
+ */
+ public function getWhereConditions( DataValue $value ) {
+ if ( !( $value instanceof GeoCoordinateValue ) ) {
+ throw new InvalidArgumentException( 'Value is not a
GeoCoordinateValue' );
+ }
+
+ return array(
+ // Note: the code in this package is not dependent on
MW.
+ // So do not replace this with FormatJSON::encode.
+ 'json' => json_encode( $value->getArrayValue() ),
+ );
+ }
+
+ /**
+ * @see DataValueHandler::getInsertValues
+ *
+ * @since wd.qe
+ *
+ * @param DataValue $value
+ *
+ * @return array
+ * @throws InvalidArgumentException
+ */
+ public function getInsertValues( DataValue $value ) {
+ if ( !( $value instanceof GeoCoordinateValue ) ) {
+ throw new InvalidArgumentException( 'Value is not a
GeoCoordinateValue' );
+ }
+
+ $values = array(
+ 'lat' => $value->getLatitude(),
+ 'lon' => $value->getLongitude(),
+
+ // Note: the code in this package is not dependent on
MW.
+ // So do not replace this with FormatJSON::encode.
+ 'json' => json_encode( $value->getArrayValue() ),
+ );
+
+ if ( $value->getAltitude() !== null ) {
+ $values['alt'] = $value->getAltitude();
+ }
+
+ if ( $value->getGlobe() !== null ) {
+ $values['globe'] = $value->getGlobe();
+ }
+
+ return $values;
+ }
+
}
\ No newline at end of file
diff --git a/repo/includes/Query/SQLStore/DataValueHandler.php
b/repo/includes/Query/SQLStore/DataValueHandler.php
index 7012370..2cbe77a 100644
--- a/repo/includes/Query/SQLStore/DataValueHandler.php
+++ b/repo/includes/Query/SQLStore/DataValueHandler.php
@@ -9,7 +9,8 @@
* Represents the mapping between a DataValue type and the
* associated implementation in the store.
*
- * Based on SMWDataItemHandler by Nischay Nahata and Markus Krötzsch.
+ * Based on, and containing snippets from, SMWDataItemHandler from Semantic
MediaWiki.
+ * SMWDataItemHandler was written by Nischay Nahata and Markus Krötzsch.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -75,11 +76,11 @@
*
* @since wd.qe
*
- * @param $dbValue // TODO: mixed or string?
+ * @param $valueFieldValue // TODO: mixed or string?
*
* @return DataValue
*/
- abstract public function newDataValueFromDbValue( $dbValue );
+ abstract public function newDataValueFromValueField( $valueFieldValue );
/**
* Return the label field for this type of DataValue. This should be
@@ -99,6 +100,32 @@
return null;
}
- // TODO: getInsertValues and getWhereConds
+ /**
+ * Return an array of fields=>values to conditions (WHERE part) in SQL
+ * queries for the given DataValue. This method can return fewer
+ * fields than getInsertValues as long as they are enough to identify
+ * an item for search.
+ *
+ * @since wd.qe
+ *
+ * @param DataValue $value
+ *
+ * @return array
+ */
+ abstract public function getWhereConditions( DataValue $value );
-}
\ No newline at end of file
+ /**
+ * Return an array of fields=>values that is to be inserted when
+ * writing the given DataValue to the database. Values should be set
+ * for all columns, even if NULL. This array is used to perform all
+ * insert operations into the DB.
+ *
+ * @since wd.qe
+ *
+ * @param DataValue $value
+ *
+ * @return array
+ */
+ abstract public function getInsertValues( DataValue $value );
+
+}
diff --git
a/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/GeoCoordinateHandlerTest.php
b/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/GeoCoordinateHandlerTest.php
index b49d9fd..b055bcc 100644
---
a/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/GeoCoordinateHandlerTest.php
+++
b/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/GeoCoordinateHandlerTest.php
@@ -5,6 +5,7 @@
use Wikibase\Repo\Query\SQLStore\DataValueHandler;
use Wikibase\Repo\Query\SQLStore\DVHandler\GeoCoordinateHandler;
use Wikibase\Repo\Test\Query\SQLStore\DataValueHandlerTest;
+use DataValues\GeoCoordinateValue;
/**
* Unit tests for the
Wikibase\Repo\Query\SQLStore\DVHandler\GeoCoordinateHandler class.
@@ -39,6 +40,8 @@
class GeoCoordinateHandlerTest extends DataValueHandlerTest {
/**
+ * @see DataValueHandlerTest::getInstances
+ *
* @since wd.qe
*
* @return DataValueHandler[]
@@ -51,4 +54,22 @@
return $instances;
}
+ /**
+ * @see DataValueHandlerTest::getValues
+ *
+ * @since wd.qe
+ *
+ * @return GeoCoordinateValue[]
+ */
+ protected function getValues() {
+ $values = array();
+
+ $values[] = new GeoCoordinateValue( 0, 0 );
+ $values[] = new GeoCoordinateValue( 23, 42 );
+ $values[] = new GeoCoordinateValue( 2.3, 4.2, 9000.1 );
+ $values[] = new GeoCoordinateValue( -2.3, -4.2, -9000.1, 'mars'
);
+
+ return $values;
+ }
+
}
diff --git
a/repo/tests/phpunit/includes/Query/SQLStore/DataValueHandlerTest.php
b/repo/tests/phpunit/includes/Query/SQLStore/DataValueHandlerTest.php
index 9f5c9e7..c5cde6c 100644
--- a/repo/tests/phpunit/includes/Query/SQLStore/DataValueHandlerTest.php
+++ b/repo/tests/phpunit/includes/Query/SQLStore/DataValueHandlerTest.php
@@ -3,6 +3,7 @@
namespace Wikibase\Repo\Test\Query\SQLStore;
use Wikibase\Repo\Query\SQLStore\DataValueHandler;
+use DataValues\DataValue;
/**
* Unit tests for the Wikibase\Repo\Query\SQLStore\DataValueHandler
implementing classes.
@@ -42,10 +43,36 @@
/**
* @since wd.qe
*
+ * @return DataValue[]
+ */
+ protected abstract function getValues();
+
+ /**
+ * @since wd.qe
+ *
* @return DataValueHandler[][]
*/
public function instanceProvider() {
return $this->arrayWrap( $this->getInstances() );
+ }
+
+ /**
+ * @since wd.qe
+ *
+ * @return DataValue[][]
+ */
+ public function valueProvider() {
+ return $this->arrayWrap( $this->getValues() );
+ }
+
+ /**
+ * @since wd.qe
+ *
+ * @return DataValueHandler
+ */
+ protected function newInstance() {
+ $instances = $this->getInstances();
+ return reset( $instances );
}
protected function arrayWrap( array $elements ) {
@@ -103,16 +130,6 @@
*
* @param DataValueHandler $dvHandler
*/
- public function testNewDataValueFromDbValue( DataValueHandler
$dvHandler ) {
- // TODO
- $this->assertTrue( true );
- }
-
- /**
- * @dataProvider instanceProvider
- *
- * @param DataValueHandler $dvHandler
- */
public function testGetLabelFieldNameReturnValue( DataValueHandler
$dvHandler ) {
$labelFieldName = $dvHandler->getLabelFieldName();
@@ -129,4 +146,58 @@
}
}
+ /**
+ * @dataProvider valueProvider
+ *
+ * @param DataValue $value
+ */
+ public function testGetWhereConditionsReturnType( DataValue $value ) {
+ $instance = $this->newInstance();
+
+ $whereConditions = $instance->getWhereConditions( $value );
+
+ $this->assertInternalType( 'array', $whereConditions );
+ $this->assertNotEmpty( $whereConditions );
+ }
+
+ /**
+ * @dataProvider valueProvider
+ *
+ * @param DataValue $value
+ */
+ public function testGetInsertValuesReturnType( DataValue $value ) {
+ $instance = $this->newInstance();
+
+ $insertValues = $instance->getInsertValues( $value );
+
+ $this->assertInternalType( 'array', $insertValues );
+ $this->assertNotEmpty( $insertValues );
+
+ $this->assertArrayHasKey( $instance->getValueFieldName(),
$insertValues );
+ $this->assertArrayHasKey( $instance->getSortFieldName(),
$insertValues );
+
+ if ( $instance->getLabelFieldName() !== null ) {
+ $this->assertArrayHasKey(
$instance->getLabelFieldName(), $insertValues );
+ }
+ }
+
+ /**
+ * @dataProvider valueProvider
+ *
+ * @param DataValue $value
+ */
+ public function testNewDataValueFromValueFieldValue( DataValue $value )
{
+ $instance = $this->newInstance();
+
+ $fieldValues = $instance->getInsertValues( $value );
+ $valueFieldValue = $fieldValues[$instance->getValueFieldName()];
+
+ $newValue = $instance->newDataValueFromValueField(
$valueFieldValue );
+
+ $this->assertTrue(
+ $value->equals( $newValue ),
+ 'Newly constructed DataValue equals the old one'
+ );
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/52197
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I99120fe730a1abb2f1e4e01bceef8215b9af1c8f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits