jenkins-bot has submitted this change and it was merged.
Change subject: Use custom type builders to define data types.
......................................................................
Use custom type builders to define data types.
This eliminates the dependency on $wgDataTypes. We are
no longer using the data types defined per default in the DataType
module, but define our own instead, using WikibaseDataTypeBuilders.
This allows for complex validators to be set up, and allows the
injection of services into the validators. THis is however left
for later.
NOTE: this requires I38c8daa77 to be merged in the DataValues module.
Change-Id: Ie4d0aca547340a413872b32bb644171d151004ef
---
M client/includes/WikibaseClient.php
M lib/WikibaseLib.classes.php
M lib/WikibaseLib.php
M lib/includes/LibRegistry.php
A lib/includes/WikibaseDataTypeBuilders.php
M lib/tests/phpunit/EntityLookupTest.php
M lib/tests/phpunit/ReferencedEntitiesFinderTest.php
M lib/tests/phpunit/serializers/PropertySerializerTest.php
M repo/includes/WikibaseRepo.php
9 files changed, 105 insertions(+), 34 deletions(-)
Approvals:
Jeroen De Dauw: Looks good to me, approved
jenkins-bot: Verified
diff --git a/client/includes/WikibaseClient.php
b/client/includes/WikibaseClient.php
index 70e4413..f700f56 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -15,6 +15,7 @@
use Wikibase\Lib\PropertyDataTypeLookup;
use Wikibase\Lib\SnakFormatter;
use Wikibase\Lib\TypedValueFormatter;
+use Wikibase\Lib\WikibaseDataTypeBuilders;
use Wikibase\RepoLinker;
use Wikibase\Settings;
use Wikibase\SettingsArray;
@@ -88,14 +89,15 @@
*/
public function getDataTypeFactory() {
if ( $this->dataTypeFactory === null ) {
- global $wgDataTypes;
- $dataTypes = array_intersect_key(
- $wgDataTypes,
+ $builders = new WikibaseDataTypeBuilders( $this );
+
+ $typeBuilderSpecs = array_intersect_key(
+ $builders->getDataTypeBuilders(),
array_flip( $this->settings->getSetting(
'dataTypes' ) )
);
- $this->dataTypeFactory = new DataTypeFactory(
$dataTypes );
+ $this->dataTypeFactory = new DataTypeFactory(
$typeBuilderSpecs );
}
return $this->dataTypeFactory;
diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php
index cfa402d..c61b578 100644
--- a/lib/WikibaseLib.classes.php
+++ b/lib/WikibaseLib.classes.php
@@ -60,6 +60,7 @@
'Wikibase\Lib\TypedValueFormatter' =>
'includes/TypedValueFormatter.php',
'Wikibase\Utils' => 'includes/Utils.php',
'Wikibase\WikibaseDiffOpFactory' =>
'includes/WikibaseDiffOpFactory.php',
+ 'Wikibase\Lib\WikibaseDataTypeBuilders' =>
'includes/WikibaseDataTypeBuilders.php',
// includes/changes
'Wikibase\Change' => 'includes/changes/Change.php',
diff --git a/lib/WikibaseLib.php b/lib/WikibaseLib.php
index 2f4ab62..c446d02 100644
--- a/lib/WikibaseLib.php
+++ b/lib/WikibaseLib.php
@@ -101,19 +101,8 @@
// TODO: this is not nice, figure out a better design
$wgExtensionFunctions[] = function() {
- global $wgDataTypes;
-
- $libRegistry = new \Wikibase\LibRegistry(
\Wikibase\Settings::singleton() );
-
- $wgDataTypes['wikibase-item'] = array(
- 'datavalue' => 'wikibase-entityid',
- 'parser' => $libRegistry->getEntityIdParser(),
- //'formatter' => evilGetEntityidFormatter(), // TODO
- );
-
\Wikibase\TemplateRegistry::singleton()->addTemplates( include( __DIR__
. "/resources/templates.php" ) );
-
- return true;
+ return true;
};
$wgValueParsers['wikibase-entityid'] = 'Wikibase\Lib\EntityIdParser';
diff --git a/lib/includes/LibRegistry.php b/lib/includes/LibRegistry.php
index 15639b4..651bd47 100644
--- a/lib/includes/LibRegistry.php
+++ b/lib/includes/LibRegistry.php
@@ -8,6 +8,7 @@
use Wikibase\Lib\EntityIdFormatter;
use Wikibase\Lib\EntityIdLabelFormatter;
use Wikibase\Lib\EntityIdParser;
+use Wikibase\Lib\WikibaseDataTypeBuilders;
/**
* Application registry for Wikibase Lib.
@@ -71,14 +72,15 @@
*/
public function getDataTypeFactory() {
if ( $this->dataTypeFactory === null ) {
- global $wgDataTypes;
- $dataTypes = array_intersect_key(
- $wgDataTypes,
+ $builders = new WikibaseDataTypeBuilders( $this );
+
+ $typeBuilderSpecs = array_intersect_key(
+ $builders->getDataTypeBuilders(),
array_flip( $this->settings->getSetting(
'dataTypes' ) )
);
- $this->dataTypeFactory = new DataTypeFactory(
$dataTypes );
+ $this->dataTypeFactory = new DataTypeFactory(
$typeBuilderSpecs );
}
return $this->dataTypeFactory;
diff --git a/lib/includes/WikibaseDataTypeBuilders.php
b/lib/includes/WikibaseDataTypeBuilders.php
new file mode 100644
index 0000000..91d3a56
--- /dev/null
+++ b/lib/includes/WikibaseDataTypeBuilders.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use DataTypes\DataType;
+
+/**
+ * Defines the data types supported by Wikibase.
+ *
+ * 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 0.4
+ *
+ * @file
+ * @ingroup WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class WikibaseDataTypeBuilders {
+
+ public function __construct() {
+ //TODO: take a service registry as a parameter. That's OK for a
builder class.
+ }
+
+ /**
+ * @return array DataType builder specs
+ */
+ public function getDataTypeBuilders() {
+ return array(
+ 'wikibase-item' => array( $this, 'buildItemType' ),
+ 'commonsMedia' => array( $this, 'buildMediaType' ),
+ 'string' => array( $this, 'buildStringType' ),
+ 'time' => array( $this, 'buildTimeType' ),
+ 'globe-coordinate' => array( $this,
'buildCoordinateType' ),
+ );
+ }
+
+ public function buildItemType( $id ) {
+ return new DataType( $id, 'wikibase-entityid', array(),
array(), array() );
+ }
+
+ public function buildMediaType( $id ) {
+ return new DataType( $id, 'string', array(), array(), array() );
+ }
+
+ public function buildStringType( $id ) {
+ return new DataType( $id, 'string', array(), array(), array() );
+ }
+
+ public function buildTimeType( $id ) {
+ return new DataType( $id, 'time', array(), array(), array() );
+ }
+
+ public function buildCoordinateType( $id ) {
+ return new DataType( $id, 'globecoordinate', array(), array(),
array() );
+ }
+
+}
diff --git a/lib/tests/phpunit/EntityLookupTest.php
b/lib/tests/phpunit/EntityLookupTest.php
index caaaef2..7b9076b 100644
--- a/lib/tests/phpunit/EntityLookupTest.php
+++ b/lib/tests/phpunit/EntityLookupTest.php
@@ -60,7 +60,13 @@
$item->setId( 42 );
$entities[$item->getPrefixedId()] = $item;
- $dtf = $factory = new DataTypeFactory(
$GLOBALS['wgDataTypes'] );
+ $dataTypes = array(
+ 'string' => array(
+ 'datavalue' => 'string'
+ )
+ );
+
+ $dtf = new DataTypeFactory( $dataTypes );
$prop = Property::newEmpty();
$prop->setId( 753 );
diff --git a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
index 6d57275..8453a5a 100644
--- a/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
+++ b/lib/tests/phpunit/ReferencedEntitiesFinderTest.php
@@ -1,6 +1,8 @@
<?php
namespace Wikibase\Lib\Test;
+use DataTypes\DataType;
+use DataTypes\DataTypeFactory;
use Wikibase\Claims;
use Wikibase\ReferencedEntitiesFinder;
use Wikibase\Claim;
@@ -161,17 +163,14 @@
protected function getMockEntityLoader() {
$entityLoader = new \Wikibase\Test\MockRepository();
- $libRegistry = new LibRegistry( Settings::singleton() );
- $dataTypeFactory = $libRegistry->getDataTypeFactory();
-
$stringProp = Property::newEmpty();
$stringProp->setId( 1 );
- $stringProp->setDataType( $dataTypeFactory->getType(
'commonsMedia' ) );
+ $stringProp->setDataTypeId( 'commonsMedia' );
$entityLoader->putEntity( $stringProp );
$itemProp = Property::newEmpty();
$itemProp->setId( 2 );
- $itemProp->setDataType( $dataTypeFactory->getType(
'wikibase-item' ) );
+ $itemProp->setDataTypeId( 'wikibase-item' );
$entityLoader->putEntity( $itemProp );
return $entityLoader;
diff --git a/lib/tests/phpunit/serializers/PropertySerializerTest.php
b/lib/tests/phpunit/serializers/PropertySerializerTest.php
index 5416020..bd86aa8 100644
--- a/lib/tests/phpunit/serializers/PropertySerializerTest.php
+++ b/lib/tests/phpunit/serializers/PropertySerializerTest.php
@@ -1,6 +1,7 @@
<?php
namespace Wikibase\Test;
+use DataTypes\DataType;
use Wikibase\Property;
/**
@@ -73,10 +74,7 @@
$property = $this->getEntityInstance();
- $libRegistry = new \Wikibase\LibRegistry(
\Wikibase\Settings::singleton() );
- $dataTypes = $libRegistry->getDataTypeFactory()->getTypes();
-
- $property->setDataType( array_shift( $dataTypes ) );
+ $property->setDataTypeId( 'string' );
$validArgs[] = array(
$property,
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 3c5751c..8da42ba 100644
--- a/repo/includes/WikibaseRepo.php
+++ b/repo/includes/WikibaseRepo.php
@@ -9,6 +9,7 @@
use Wikibase\Lib\EntityIdFormatter;
use Wikibase\Lib\EntityIdLabelFormatter;
use Wikibase\Lib\EntityIdParser;
+use Wikibase\Lib\WikibaseDataTypeBuilders;
use Wikibase\Settings;
use Wikibase\SettingsArray;
@@ -69,14 +70,15 @@
*/
public function getDataTypeFactory() {
if ( $this->dataTypeFactory === null ) {
- global $wgDataTypes;
- $dataTypes = array_intersect_key(
- $wgDataTypes,
+ $builders = new WikibaseDataTypeBuilders( $this );
+
+ $typeBuilderSpecs = array_intersect_key(
+ $builders->getDataTypeBuilders(),
array_flip( $this->settings->getSetting(
'dataTypes' ) )
);
- $this->dataTypeFactory = new DataTypeFactory(
$dataTypes );
+ $this->dataTypeFactory = new DataTypeFactory(
$typeBuilderSpecs );
}
return $this->dataTypeFactory;
--
To view, visit https://gerrit.wikimedia.org/r/68622
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie4d0aca547340a413872b32bb644171d151004ef
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits