John Erling Blad has submitted this change and it was merged.
Change subject: Added EntityIdHandler
......................................................................
Added EntityIdHandler
Change-Id: Iadc1dc2e42326f1b01f923d56e24860e36f99b22
---
M repo/config/Wikibase.experimental.php
A repo/includes/Query/SQLStore/DVHandler/EntityIdHandler.php
A repo/tests/phpunit/includes/Query/SQLStore/DVHandler/EntityIdHandlerTest.php
3 files changed, 230 insertions(+), 0 deletions(-)
Approvals:
John Erling Blad: Verified; Looks good to me, approved
jenkins-bot: Checked
diff --git a/repo/config/Wikibase.experimental.php
b/repo/config/Wikibase.experimental.php
index c9a56ce..2787742 100644
--- a/repo/config/Wikibase.experimental.php
+++ b/repo/config/Wikibase.experimental.php
@@ -62,6 +62,7 @@
'Wikibase\Repo\Query\QueryResult',
'Wikibase\Repo\Query\QueryStore',
+ 'Wikibase\Repo\Query\SQLStore\DVHandler\EntityIdHandler',
'Wikibase\Repo\Query\SQLStore\DVHandler\GeoCoordinateHandler',
'Wikibase\Repo\Query\SQLStore\DataValueHandler',
@@ -138,6 +139,7 @@
'Query/QueryEngineResult',
+ 'Query/SQLStore/DVHandler/EntityIdHandler',
'Query/SQLStore/DVHandler/GeoCoordinateHandler',
'Query/SQLStore/DataValueHandler',
diff --git a/repo/includes/Query/SQLStore/DVHandler/EntityIdHandler.php
b/repo/includes/Query/SQLStore/DVHandler/EntityIdHandler.php
new file mode 100644
index 0000000..a275ef3
--- /dev/null
+++ b/repo/includes/Query/SQLStore/DVHandler/EntityIdHandler.php
@@ -0,0 +1,153 @@
+<?php
+
+namespace Wikibase\Repo\Query\SQLStore\DVHandler;
+
+use Wikibase\Repo\Query\SQLStore\DataValueHandler;
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\Database\FieldDefinition;
+use DataValues\DataValue;
+use Wikibase\EntityId;
+use InvalidArgumentException;
+
+/**
+ * Represents the mapping between Wikibase\EntityId and
+ * the corresponding table in the store.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since wd.qe
+ *
+ * @file
+ * @ingroup WikibaseSQLStore
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class EntityIdHandler extends DataValueHandler {
+
+ /**
+ * @see DataValueHandler::getTableDefinition
+ *
+ * @since wd.qe
+ *
+ * @return TableDefinition
+ */
+ public function getTableDefinition() {
+ $fields = array(
+ new FieldDefinition( 'type',
FieldDefinition::TYPE_TEXT, false ),
+ new FieldDefinition( 'number',
FieldDefinition::TYPE_INTEGER, false ),
+ new FieldDefinition( 'json',
FieldDefinition::TYPE_TEXT, false ),
+ );
+
+ return new TableDefinition( 'entityid', $fields );
+ }
+
+ /**
+ * @see DataValueHandler::getValueFieldName
+ *
+ * @since wd.qe
+ *
+ * @return string
+ */
+ public function getValueFieldName() {
+ return 'json';
+ }
+
+ /**
+ * @see DataValueHandler::getSortFieldName
+ *
+ * @since wd.qe
+ *
+ * @return string
+ */
+ public function getSortFieldName() {
+ return 'number';
+ }
+
+ /**
+ * @see DataValueHandler::newDataValueFromValueField
+ *
+ * @since wd.qe
+ *
+ * @param $valueFieldValue // TODO: mixed or string?
+ *
+ * @return DataValue
+ */
+ public function newDataValueFromValueField( $valueFieldValue ) {
+ return EntityId::newFromArray( json_decode( $valueFieldValue,
true ) );
+ }
+
+ /**
+ * @see DataValueHandler::getLabelFieldName
+ *
+ * @since wd.qe
+ *
+ * @return string|null
+ */
+ public function getLabelFieldName() {
+ return null;
+ }
+
+ /**
+ * @see DataValueHandler::getWhereConditions
+ *
+ * @since wd.qe
+ *
+ * @param DataValue $value
+ *
+ * @return array
+ * @throws InvalidArgumentException
+ */
+ public function getWhereConditions( DataValue $value ) {
+ if ( !( $value instanceof EntityId ) ) {
+ throw new InvalidArgumentException( 'Value is not a
EntityId' );
+ }
+
+ 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 EntityId ) ) {
+ throw new InvalidArgumentException( 'Value is not a
EntityId' );
+ }
+
+ $values = array(
+ 'type' => $value->getEntityType(),
+ 'number' => $value->getNumericId(),
+
+ // Note: the code in this package is not dependent on
MW.
+ // So do not replace this with FormatJSON::encode.
+ 'json' => json_encode( $value->getArrayValue() ),
+ );
+
+ return $values;
+ }
+
+}
\ No newline at end of file
diff --git
a/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/EntityIdHandlerTest.php
b/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/EntityIdHandlerTest.php
new file mode 100644
index 0000000..9d46833
--- /dev/null
+++
b/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/EntityIdHandlerTest.php
@@ -0,0 +1,75 @@
+<?php
+
+namespace Wikibase\Repo\Test\Query\SQLStore\DVHandler;
+
+use Wikibase\Repo\Query\SQLStore\DataValueHandler;
+use Wikibase\Repo\Query\SQLStore\DVHandler\EntityIdHandler;
+use Wikibase\Repo\Test\Query\SQLStore\DataValueHandlerTest;
+use Wikibase\EntityId;
+
+/**
+ * Unit tests for the Wikibase\Repo\Query\SQLStore\DVHandler\EntityId class.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since wd.qe
+ *
+ * @ingroup WikibaseRepoTest
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group WikibaseQuery
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class EntityIdHandlerTest extends DataValueHandlerTest {
+
+ /**
+ * @see DataValueHandlerTest::getInstances
+ *
+ * @since wd.qe
+ *
+ * @return DataValueHandler[]
+ */
+ protected function getInstances() {
+ $instances = array();
+
+ $instances[] = new EntityIdHandler();
+
+ return $instances;
+ }
+
+ /**
+ * @see DataValueHandlerTest::getValues
+ *
+ * @since wd.qe
+ *
+ * @return EntityId[]
+ */
+ protected function getValues() {
+ $values = array();
+
+ $values[] = new EntityId( 'item', 42 );
+ $values[] = new EntityId( 'item', 9001 );
+ $values[] = new EntityId( 'property', 23 );
+ $values[] = new EntityId( 'query', 7201010 );
+
+ return $values;
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/52198
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iadc1dc2e42326f1b01f923d56e24860e36f99b22
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: John Erling Blad <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits