Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/391280 )
Change subject: Add CachedEntityIds container and use in SparqlHelper
......................................................................
Add CachedEntityIds container and use in SparqlHelper
CachedEntityIds is, like CachedQueryResults, a functionally identical
subclass of CachedArray with some extra documentation. The
findEntitiesWithSame* methods are updated to return CachedEntityIds
(copying the CachingMetadata of the incoming CachedQueryResults).
UniqueValueChecker then throws the CachingMetadata away, but later
commits will fix that.
Bug: T179844
Change-Id: Ie9c9fb52759f8e0d572fa547d09b94794abd6e0f
---
A includes/ConstraintCheck/Cache/CachedEntityIds.php
M includes/ConstraintCheck/Checker/UniqueValueChecker.php
M includes/ConstraintCheck/Helper/SparqlHelper.php
A tests/phpunit/Cache/CachedEntityIdsTest.php
M tests/phpunit/Helper/SparqlHelperTest.php
M tests/phpunit/SparqlHelperMock.php
6 files changed, 66 insertions(+), 11 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
refs/changes/80/391280/1
diff --git a/includes/ConstraintCheck/Cache/CachedEntityIds.php
b/includes/ConstraintCheck/Cache/CachedEntityIds.php
new file mode 100644
index 0000000..15e2385
--- /dev/null
+++ b/includes/ConstraintCheck/Cache/CachedEntityIds.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Cache;
+
+use Wikibase\DataModel\Entity\EntityId;
+
+/**
+ * A list of entity IDs, along with information whether and how they were
cached.
+ *
+ * (Note that list entries may also be null,
+ * in case it was not possible to parse an entity ID from the SPARQL response.)
+ */
+class CachedEntityIds extends CachedArray {
+
+ /**
+ * @return (EntityId|null) The entity IDs.
+ */
+ public function getArray() {
+ return parent::getArray();
+ }
+
+}
diff --git a/includes/ConstraintCheck/Checker/UniqueValueChecker.php
b/includes/ConstraintCheck/Checker/UniqueValueChecker.php
index 0cc8a00..421cdaa 100644
--- a/includes/ConstraintCheck/Checker/UniqueValueChecker.php
+++ b/includes/ConstraintCheck/Checker/UniqueValueChecker.php
@@ -59,7 +59,7 @@
if ( $this->sparqlHelper !== null ) {
if ( $context->getType() === 'statement' ) {
- $otherEntities =
$this->sparqlHelper->findEntitiesWithSameStatement(
+ $result =
$this->sparqlHelper->findEntitiesWithSameStatement(
$context->getSnakStatement(),
true // ignore deprecated statements
);
@@ -67,7 +67,7 @@
if ( $context->getSnak()->getType() !== 'value'
) {
return new CheckResult( $context,
$constraint, [], CheckResult::STATUS_COMPLIANCE );
}
- $otherEntities =
$this->sparqlHelper->findEntitiesWithSameQualifierOrReference(
+ $result =
$this->sparqlHelper->findEntitiesWithSameQualifierOrReference(
$context->getEntity()->getId(),
$context->getSnak(),
$context->getType(),
@@ -75,6 +75,7 @@
$context->getType() === 'qualifier'
);
}
+ $otherEntities = $result->getArray();
if ( $otherEntities === [] ) {
$status = CheckResult::STATUS_COMPLIANCE;
diff --git a/includes/ConstraintCheck/Helper/SparqlHelper.php
b/includes/ConstraintCheck/Helper/SparqlHelper.php
index dcd18ac..d9da1b0 100644
--- a/includes/ConstraintCheck/Helper/SparqlHelper.php
+++ b/includes/ConstraintCheck/Helper/SparqlHelper.php
@@ -19,6 +19,7 @@
use Wikibase\DataModel\Statement\Statement;
use Wikibase\Rdf\RdfVocabulary;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedBool;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedEntityIds;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedQueryResults;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
@@ -157,7 +158,7 @@
/**
* @param Statement $statement
* @param boolean $ignoreDeprecatedStatements Whether to ignore
deprecated statements or not.
- * @return (EntityId|null)[]
+ * @return CachedEntityIds
* @throws SparqlHelperException if the query times out or some other
error occurs
*/
public function findEntitiesWithSameStatement(
@@ -198,7 +199,7 @@
* @param PropertyValueSnak $snak
* @param string $type Context::TYPE_QUALIFIER or
Context::TYPE_REFERENCE
* @param boolean $ignoreDeprecatedStatements Whether to ignore
deprecated statements or not.
- * @return (EntityId|null)[]
+ * @return CachedEntityIds
* @throws SparqlHelperException if the query times out or some other
error occurs
*/
public function findEntitiesWithSameQualifierOrReference(
@@ -262,10 +263,10 @@
* Extract and parse entity IDs from the ?otherEntity column of a
SPARQL query result.
*
* @param CachedQueryResults $results
- * @return (EntityId|null)[]
+ * @return CachedEntityIds
*/
private function getOtherEntities( CachedQueryResults $results ) {
- return array_map(
+ return new CachedEntityIds( array_map(
function ( $resultBindings ) {
$entityIRI =
$resultBindings['otherEntity']['value'];
$entityPrefixLength = strlen(
$this->entityPrefix );
@@ -282,7 +283,7 @@
return null;
},
$results->getArray()['results']['bindings']
- );
+ ), $results->getCachingMetadata() );
}
// @codingStandardsIgnoreStart cyclomatic complexity of this function
is too high
diff --git a/tests/phpunit/Cache/CachedEntityIdsTest.php
b/tests/phpunit/Cache/CachedEntityIdsTest.php
new file mode 100644
index 0000000..da8fe10
--- /dev/null
+++ b/tests/phpunit/Cache/CachedEntityIdsTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\Test\Cache;
+
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedEntityIds;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata;
+
+/**
+ * @covers
\WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedEntityIds
+ * @uses \WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedArray
+ * @uses
\WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata
+ *
+ * @group WikibaseQualityConstraints
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class CachedEntityIdsTest extends \PHPUnit_Framework_TestCase {
+
+ public function testGetArray() {
+ $array = [ 'boolean' => true ];
+ $cm = CachingMetadata::fresh();
+
+ $ca = new CachedEntityIds( $array, $cm );
+
+ $this->assertSame( $array, $ca->getArray() );
+ }
+
+}
diff --git a/tests/phpunit/Helper/SparqlHelperTest.php
b/tests/phpunit/Helper/SparqlHelperTest.php
index a2af2fb..2ca319b 100644
--- a/tests/phpunit/Helper/SparqlHelperTest.php
+++ b/tests/phpunit/Helper/SparqlHelperTest.php
@@ -132,7 +132,7 @@
->withConsecutive( [ $this->equalTo( $query ) ] );
$this->assertEquals(
- $sparqlHelper->findEntitiesWithSameStatement(
$statement, true ),
+ $sparqlHelper->findEntitiesWithSameStatement(
$statement, true )->getArray(),
[ new ItemId( 'Q100' ), new ItemId( 'Q101' ) ]
);
}
@@ -192,7 +192,7 @@
$snak,
$contextType,
false
- ),
+ )->getArray(),
[ new ItemId( 'Q100' ), new ItemId( 'Q101' ) ]
);
}
diff --git a/tests/phpunit/SparqlHelperMock.php
b/tests/phpunit/SparqlHelperMock.php
index bb64932..7dc974b 100644
--- a/tests/phpunit/SparqlHelperMock.php
+++ b/tests/phpunit/SparqlHelperMock.php
@@ -5,6 +5,8 @@
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Snak\PropertyValueSnak;
use Wikibase\DataModel\Statement\Statement;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedEntityIds;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\SparqlHelper;
/**
@@ -55,7 +57,7 @@
$mock->expects( $this->exactly( 1 ) )
->method( 'findEntitiesWithSameStatement' )
- ->willReturn( $result )
+ ->willReturn( new CachedEntityIds( $result,
CachingMetadata::fresh() ) )
->withConsecutive( [ $this->equalTo( $expectedStatement
), $this->equalTo( true ) ] );
return $mock;
@@ -73,7 +75,7 @@
$mock->expects( $this->exactly( 1 ) )
->method( 'findEntitiesWithSameQualifierOrReference' )
- ->willReturn( $result )
+ ->willReturn( new CachedEntityIds( $result,
CachingMetadata::fresh() ) )
->withConsecutive( [
$this->equalTo( $expectedEntityId ),
$this->equalTo( $expectedSnak ),
--
To view, visit https://gerrit.wikimedia.org/r/391280
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie9c9fb52759f8e0d572fa547d09b94794abd6e0f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits