jenkins-bot has submitted this change and it was merged.
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(-)
Approvals:
Mwjames: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/ParserData.php b/includes/ParserData.php
index 1e67575..3f82fda 100644
--- a/includes/ParserData.php
+++ b/includes/ParserData.php
@@ -322,18 +322,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
@@ -341,26 +335,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 435ceb2..fe4561f 100644
--- a/includes/Subobject.php
+++ b/includes/Subobject.php
@@ -198,25 +198,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 0fb63dc..dab38d9 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;
/**
@@ -55,4 +56,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: merged
Gerrit-Change-Id: I12b197e7b589561c03b8c44c07be5b763f82c307
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Mwjames <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits