Mwjames has uploaded a new change for review.
https://gerrit.wikimedia.org/r/75295
Change subject: Remove addPropertyValue logic from ParserData (further
compartmentalize of ParserData)
......................................................................
Remove addPropertyValue logic from ParserData (further compartmentalize of
ParserData)
Move addPropertyValue logic into the SemanticData addDataValue method.
The logic provided by addPropertyValue has no "real" contract to the
ParserData object (but due to legacy conformism was transferred from
SMWParseData into ParserData which dilutes the responsibility of the
ParserData object)
addPropertyValue should be removed from the ParserData but this
needs to be done in a follow up in order to keep tests stable.
Change-Id: I12b197e7b589561c03b8c44c07be5b763f82c307
---
M includes/ParserData.php
M includes/SMW_SemanticData.php
M includes/Subobject.php
M tests/phpunit/includes/SemanticDataTest.php
4 files changed, 184 insertions(+), 48 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki
refs/changes/95/75295/1
diff --git a/includes/ParserData.php b/includes/ParserData.php
index 0acc9c8..d4dd7a5 100644
--- a/includes/ParserData.php
+++ b/includes/ParserData.php
@@ -311,18 +311,12 @@
}
/**
- * This method adds a data value to the semantic data container
+ * Adds a data value to the semantic data container
*
* @par Example:
* @code
- * $parserData = new SMW\ParserData(
- * $parser->getTitle(),
- * $parser->getOutput(),
- * $settings;
- * )
- *
- * $dataValue = SMWDataValueFactory::newPropertyValue( $userProperty,
$userValue )
- * $parserData->addPropertyValue( $dataValue )
+ * $dataValue = DataValueFactory::newPropertyValue( $userProperty,
$userValue )
+ * $parserData->addPropertyValue( $dataValue )
* @endcode
*
* @since 1.9
@@ -330,26 +324,11 @@
* @param SMWDataValue $dataValue
*/
public function addPropertyValue( SMWDataValue $dataValue ) {
- Profiler::In( __METHOD__, true );
- if ( $dataValue->getProperty() instanceof SMWDIProperty ) {
- if ( !$dataValue->isValid() ) {
- $this->semanticData->addPropertyObjectValue(
- new SMWDIProperty(
SMWDIProperty::TYPE_ERROR ),
-
$dataValue->getProperty()->getDiWikiPage()
- );
- $this->addError( $dataValue->getErrors() );
- } else {
- $this->semanticData->addPropertyObjectValue(
- $dataValue->getProperty(),
- $dataValue->getDataItem()
- );
- }
- } else {
- $this->addError( $dataValue->getErrors() );
- }
-
- Profiler::Out( __METHOD__, true );
+ // FIXME Remove the addPropertyValue method from
+ // the ParserData object
+ $this->semanticData->addDataValue( $dataValue );
+ $this->addError( $this->semanticData->getErrors() );
}
/**
diff --git a/includes/SMW_SemanticData.php b/includes/SMW_SemanticData.php
index f4f9e08..6a63b36 100644
--- a/includes/SMW_SemanticData.php
+++ b/includes/SMW_SemanticData.php
@@ -118,6 +118,9 @@
*/
protected $subDataAllowed = true;
+ /** @var array */
+ protected $errors = array();
+
/**
* Constructor.
*
@@ -182,6 +185,28 @@
} else {
return array();
}
+ }
+
+ /**
+ * Returns collected errors occurred during processing
+ *
+ * @since 1.9
+ *
+ * @return array
+ */
+ public function getErrors() {
+ return $this->errors;
+ }
+
+ /**
+ * Adds an error array
+ *
+ * @since 1.9
+ *
+ * @return array
+ */
+ public function addError( array $errors ) {
+ return $this->errors = array_merge( $errors, $this->errors );
}
/**
@@ -332,6 +357,42 @@
}
/**
+ * Adds a DataValue object to the semantic data container
+ *
+ * @par Example:
+ * @code
+ * $dataValue = DataValueFactory::newPropertyValue( $userProperty,
$userValue )
+ * $semanticData->addDataValue( $dataValue )
+ * @endcode
+ *
+ * @since 1.9
+ *
+ * @param DataValue $dataValue
+ */
+ public function addDataValue( SMWDataValue $dataValue ) {
+ \SMW\Profiler::In( __METHOD__, true );
+
+ if ( $dataValue->getProperty() instanceof \SMW\DIProperty ) {
+ if ( !$dataValue->isValid() ) {
+ $this->addPropertyObjectValue(
+ new \SMW\DIProperty(
\SMW\DIProperty::TYPE_ERROR ),
+
$dataValue->getProperty()->getDiWikiPage()
+ );
+ $this->addError( $dataValue->getErrors() );
+ } else {
+ $this->addPropertyObjectValue(
+ $dataValue->getProperty(),
+ $dataValue->getDataItem()
+ );
+ }
+ } else {
+ $this->addError( $dataValue->getErrors() );
+ }
+
+ \SMW\Profiler::Out( __METHOD__, true );
+ }
+
+ /**
* Remove a value for a property identified by its SMWDataItem object.
* This method removes a property-value specified by the property and
* dataitem. If there are no more property-values for this property it
diff --git a/includes/Subobject.php b/includes/Subobject.php
index e591462..c806e2b 100644
--- a/includes/Subobject.php
+++ b/includes/Subobject.php
@@ -213,25 +213,8 @@
throw new InvalidSemanticDataException( 'The semantic
data container is not initialized' );
}
- wfProfileIn( __METHOD__ );
-
- if ( $dataValue->getProperty() instanceof DIProperty ) {
- if ( $dataValue->isValid() ) {
- $this->semanticData->addPropertyObjectValue(
- $dataValue->getProperty(),
- $dataValue->getDataItem()
- );
- } else {
- $this->semanticData->addPropertyObjectValue(
- new DIProperty( DIProperty::TYPE_ERROR
),
-
$dataValue->getProperty()->getDiWikiPage()
- );
- $this->addError( $dataValue->getErrors() );
- }
- } else {
- $this->addError( $dataValue->getErrors() );
- }
-
- wfProfileOut( __METHOD__ );
+ $this->semanticData->addDataValue( $dataValue );
+ $this->addError( $this->semanticData->getErrors() );
}
+
}
diff --git a/tests/phpunit/includes/SemanticDataTest.php
b/tests/phpunit/includes/SemanticDataTest.php
index b71deee..c1aee9b 100644
--- a/tests/phpunit/includes/SemanticDataTest.php
+++ b/tests/phpunit/includes/SemanticDataTest.php
@@ -2,6 +2,7 @@
namespace SMW\Test;
+use SMW\DataValueFactory;
use SMW\SemanticData;
/**
@@ -70,4 +71,116 @@
$this->assertInstanceOf( 'SMWSemanticData',
$this->getInstance() );
}
+ /**
+ * @test SemanticData::addPropertyValue
+ * @dataProvider dataValueDataProvider
+ *
+ * @since 1.9
+ *
+ * @param $dataValues
+ * @param $expected
+ */
+ public function testAddDataValue( $dataValues, $expected ) {
+
+ $instance = $this->getInstance();
+
+ foreach ( $dataValues as $dataValue ) {
+ $instance->addDataValue( $dataValue );
+ }
+
+ if ( $expected['error'] === 0 ){
+ $this->assertSemanticData( $instance, $expected );
+ } else {
+ $this->assertCount( $expected['error'],
$instance->getErrors() );
+ }
+ }
+
+ /**
+ * @return array
+ */
+ public function dataValueDataProvider() {
+
+ $provider = array();
+
+ // #0 Single DataValue is added
+ $provider[] = array(
+ array(
+ DataValueFactory::newPropertyValue( 'Foo',
'Bar' ),
+ ),
+ array(
+ 'error' => 0,
+ 'propertyCount' => 1,
+ 'propertyLabel' => 'Foo',
+ 'propertyValue' => 'Bar'
+ )
+ );
+
+ // #1 Equal Datavalues will only result in one added object
+ $provider[] = array(
+ array(
+ DataValueFactory::newPropertyValue( 'Foo',
'Bar' ),
+ DataValueFactory::newPropertyValue( 'Foo',
'Bar' ),
+ ),
+ array(
+ 'error' => 0,
+ 'propertyCount' => 1,
+ 'propertyLabel' => 'Foo',
+ 'propertyValue' => 'Bar'
+ )
+ );
+
+ // #2 Two different DataValue objects
+ $provider[] = array(
+ array(
+ DataValueFactory::newPropertyValue( 'Foo',
'Bar' ),
+ DataValueFactory::newPropertyValue( 'Lila',
'Lula' ),
+ ),
+ array(
+ 'error' => 0,
+ 'propertyCount' => 2,
+ 'propertyLabel' => array( 'Foo', 'Lila' ),
+ 'propertyValue' => array( 'Bar', 'Lula' )
+ )
+ );
+
+ // #3 Error (Inverse)
+ $provider[] = array(
+ array(
+ DataValueFactory::newPropertyValue( '-Foo',
'Bar' ),
+ ),
+ array(
+ 'error' => 1,
+ 'propertyCount' => 0,
+ )
+ );
+
+ // #4 One valid DataValue + an error object
+ $provider[] = array(
+ array(
+ DataValueFactory::newPropertyValue( 'Foo',
'Bar' ),
+ DataValueFactory::newPropertyValue( '-Foo',
'bar' ),
+ ),
+ array(
+ 'error' => 1,
+ 'propertyCount' => 1,
+ 'propertyLabel' => array( 'Foo' ),
+ 'propertyValue' => array( 'Bar' )
+ )
+ );
+
+
+ // #5 Error (Predefined)
+ $provider[] = array(
+ array(
+ DataValueFactory::newPropertyValue( '_Foo',
'Bar' ),
+ ),
+ array(
+ 'error' => 1,
+ 'propertyCount' => 0,
+ )
+ );
+
+ return $provider;
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/75295
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I12b197e7b589561c03b8c44c07be5b763f82c307
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits