Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/68622
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, 107 insertions(+), 31 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/22/68622/1
diff --git a/client/includes/WikibaseClient.php
b/client/includes/WikibaseClient.php
index 70e4413..39d18c3 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\WikiBaseTypeBuilders;
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 WikiBaseTypeBuilders( $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 302096b..a2ebbed 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..84e79b1 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\WikiBaseTypeBuilders;
/**
* 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 WikiBaseTypeBuilders( $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..ffdfd74
--- /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 WikiBaseTypeBuilders {
+
+ 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..062469c 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 = $factory = 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..a913149 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,8 +163,9 @@
protected function getMockEntityLoader() {
$entityLoader = new \Wikibase\Test\MockRepository();
- $libRegistry = new LibRegistry( Settings::singleton() );
- $dataTypeFactory = $libRegistry->getDataTypeFactory();
+ $dataTypeFactory = new DataTypeFactory();
+ $dataTypeFactory->registerDataType( new DataType(
'commonsMedia', 'string', array(), array(), array() ) );
+ $dataTypeFactory->registerDataType( new DataType(
'wikibase-item', 'wikibase-entityid', array(), array(), array() ) );
$stringProp = Property::newEmpty();
$stringProp->setId( 1 );
diff --git a/lib/tests/phpunit/serializers/PropertySerializerTest.php
b/lib/tests/phpunit/serializers/PropertySerializerTest.php
index 5416020..00f3ae6 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,8 @@
$property = $this->getEntityInstance();
- $libRegistry = new \Wikibase\LibRegistry(
\Wikibase\Settings::singleton() );
- $dataTypes = $libRegistry->getDataTypeFactory()->getTypes();
-
- $property->setDataType( array_shift( $dataTypes ) );
+ $type = new DataType( 'string', 'string', array(), array(),
array() );
+ $property->setDataType( $type );
$validArgs[] = array(
$property,
diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php
index 3c5751c..75f05f1 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\WikiBaseTypeBuilders;
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 WikiBaseTypeBuilders( $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: newchange
Gerrit-Change-Id: Ie4d0aca547340a413872b32bb644171d151004ef
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits