Lucas Werkmeister (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/391278 )

Change subject: Add CachedArray, CachedQueryResults containers
......................................................................

Add CachedArray, CachedQueryResults containers

CachedArray is simply a wrapper around an array and some
CachingMetadata, and CachedQueryResults is a functionally identical
class with some extra documentation.

SparqlHelper::runQuery() is changed to return CachedQueryResults and
attaches the caching information it extracted from the HTTP headers to
them. The other SparqlHelper methods currently throw this information
away again, but will be updated to return another container (e. g.
CachedBoolean) in later commits.

Bug: T179844
Change-Id: I80930ee94a3bef26b07f001ce2d4f346a3b6ff9a
---
A includes/ConstraintCheck/Cache/CachedArray.php
A includes/ConstraintCheck/Cache/CachedQueryResults.php
M includes/ConstraintCheck/Helper/SparqlHelper.php
A tests/phpunit/Cache/CachedArrayTest.php
A tests/phpunit/Cache/CachedQueryResultsTest.php
M tests/phpunit/Helper/SparqlHelperTest.php
6 files changed, 166 insertions(+), 15 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
 refs/changes/78/391278/1

diff --git a/includes/ConstraintCheck/Cache/CachedArray.php 
b/includes/ConstraintCheck/Cache/CachedArray.php
new file mode 100644
index 0000000..99a764b
--- /dev/null
+++ b/includes/ConstraintCheck/Cache/CachedArray.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Cache;
+
+/**
+ * An array (of unspecified nature) along with information whether and how it 
was cached.
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class CachedArray {
+
+       /**
+        * @var array
+        */
+       private $array;
+
+       /**
+        * @var CachingMetadata
+        */
+       private $cachingMetadata;
+
+       public function __construct( array $array, CachingMetadata 
$cachingMetadata ) {
+               $this->array = $array;
+               $this->cachingMetadata = $cachingMetadata;
+       }
+
+       /**
+        * @return array
+        */
+       public function getArray() {
+               return $this->array;
+       }
+
+       /**
+        * @return CachingMetadata
+        */
+       public function getCachingMetadata() {
+               return $this->cachingMetadata;
+       }
+
+}
diff --git a/includes/ConstraintCheck/Cache/CachedQueryResults.php 
b/includes/ConstraintCheck/Cache/CachedQueryResults.php
new file mode 100644
index 0000000..fd4eff3
--- /dev/null
+++ b/includes/ConstraintCheck/Cache/CachedQueryResults.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Cache;
+
+/**
+ * Results of a SPARQL query, along with information whether and how they were 
cached.
+ * The results are represented using the
+ * {@link https://www.w3.org/TR/sparql11-results-json/ SPARQL 1.1 Query 
Results JSON Format}.
+ */
+class CachedQueryResults extends CachedArray {
+
+       /**
+        * @return array The query results.
+        * For SELECT queries, you typically iterate over 
['results']['bindings'],
+        * while for ASK queries, you typically check ['boolean'].
+        */
+       public function getArray() {
+               return parent::getArray();
+       }
+
+}
diff --git a/includes/ConstraintCheck/Helper/SparqlHelper.php 
b/includes/ConstraintCheck/Helper/SparqlHelper.php
index ba521d1..d2593f4 100644
--- a/includes/ConstraintCheck/Helper/SparqlHelper.php
+++ b/includes/ConstraintCheck/Helper/SparqlHelper.php
@@ -18,6 +18,8 @@
 use Wikibase\DataModel\Snak\Snak;
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\Rdf\RdfVocabulary;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedQueryResults;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata;
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
 use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
 use WikibaseQuality\ConstraintReport\Role;
@@ -134,7 +136,7 @@
                        // TODO hint:gearing is a workaround for T168973 and 
can hopefully be removed eventually
 
                        $result = $this->runQuery( $query );
-                       if ( $result['boolean'] ) {
+                       if ( $result->getArray()['boolean'] ) {
                                return true;
                        }
                }
@@ -249,10 +251,10 @@
        /**
         * Extract and parse entity IDs from the ?otherEntity column of a 
SPARQL query result.
         *
-        * @param array $result SPARQL query result
+        * @param CachedQueryResults $results
         * @return (EntityId|null)[]
         */
-       private function getOtherEntities( $result ) {
+       private function getOtherEntities( CachedQueryResults $results ) {
                return array_map(
                        function ( $resultBindings ) {
                                $entityIRI = 
$resultBindings['otherEntity']['value'];
@@ -269,7 +271,7 @@
 
                                return null;
                        },
-                       $result['results']['bindings']
+                       $results->getArray()['results']['bindings']
                );
        }
 
@@ -415,7 +417,7 @@
 
                $result = $this->runQuery( $query );
 
-               $vars = $result['results']['bindings'][0];
+               $vars = $result->getArray()['results']['bindings'][0];
                if ( array_key_exists( 'matches', $vars ) ) {
                        // true or false ⇒ regex okay, text matches or not
                        return $vars['matches']['value'] === 'true';
@@ -478,7 +480,7 @@
         *
         * @param string $query The query, unencoded (plain string).
         *
-        * @return array The returned JSON data (you typically iterate over 
["results"]["bindings"]).
+        * @return CachedQueryResults
         *
         * @throws SparqlHelperException if the query times out or some other 
error occurs
         */
@@ -511,14 +513,18 @@
                        ( $endTime - $startTime ) * 1000
                );
 
-               if ( $this->getCacheMaxAge( $request->getResponseHeaders() ) ) {
+               $maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() 
);
+               if ( $maxAge ) {
                        $this->dataFactory->increment( 
'wikibase.quality.constraints.sparql.cached' );
                }
 
                if ( $status->isOk() ) {
                        $json = $request->getContent();
                        $arr = json_decode( $json, true );
-                       return $arr;
+                       return new CachedQueryResults(
+                               $arr,
+                               $maxAge ? 
CachingMetadata::ofMaximumAgeInSeconds( $maxAge ) : CachingMetadata::fresh()
+                       );
                } else {
                        $this->dataFactory->increment( 
'wikibase.quality.constraints.sparql.error' );
 
diff --git a/tests/phpunit/Cache/CachedArrayTest.php 
b/tests/phpunit/Cache/CachedArrayTest.php
new file mode 100644
index 0000000..d9073eb
--- /dev/null
+++ b/tests/phpunit/Cache/CachedArrayTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\Test\Cache;
+
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedArray;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata;
+
+/**
+ * @covers \WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedArray
+ * @uses 
\WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata
+ *
+ * @group WikibaseQualityConstraints
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class CachedArrayTest extends \PHPUnit_Framework_TestCase {
+
+       public function testGetArray() {
+               $array = [ 'array' => true ];
+               $cm = CachingMetadata::fresh();
+
+               $ca = new CachedArray( $array, $cm );
+
+               $this->assertSame( $array, $ca->getArray() );
+       }
+
+       public function testGetCachingMetadata() {
+               $array = [];
+               $cm = CachingMetadata::ofMaximumAgeInSeconds( 42 );
+
+               $ca = new CachedArray( $array, $cm );
+
+               $this->assertSame( $cm, $ca->getCachingMetadata() );
+       }
+
+}
diff --git a/tests/phpunit/Cache/CachedQueryResultsTest.php 
b/tests/phpunit/Cache/CachedQueryResultsTest.php
new file mode 100644
index 0000000..a639ff7
--- /dev/null
+++ b/tests/phpunit/Cache/CachedQueryResultsTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace WikibaseQuality\ConstraintReport\Test\Cache;
+
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedQueryResults;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata;
+
+/**
+ * @covers 
\WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedQueryResults
+ * @uses \WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedArray
+ * @uses 
\WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata
+ *
+ * @group WikibaseQualityConstraints
+ *
+ * @author Lucas Werkmeister
+ * @license GNU GPL v2+
+ */
+class CachedQueryResultsTest extends \PHPUnit_Framework_TestCase {
+
+       public function testGetArray() {
+               $array = [ 'boolean' => true ];
+               $cm = CachingMetadata::fresh();
+
+               $ca = new CachedQueryResults( $array, $cm );
+
+               $this->assertSame( $array, $ca->getArray() );
+       }
+
+}
diff --git a/tests/phpunit/Helper/SparqlHelperTest.php 
b/tests/phpunit/Helper/SparqlHelperTest.php
index 933cb1a..6068026 100644
--- a/tests/phpunit/Helper/SparqlHelperTest.php
+++ b/tests/phpunit/Helper/SparqlHelperTest.php
@@ -19,6 +19,8 @@
 use Wikibase\DataModel\Statement\Statement;
 use Wikibase\Rdf\RdfVocabulary;
 use WikibaseQuality\ConstraintReport\Constraint;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachedQueryResults;
+use WikibaseQuality\ConstraintReport\ConstraintCheck\Cache\CachingMetadata;
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\Context;
 use 
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterException;
 use WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\SparqlHelper;
@@ -37,6 +39,20 @@
 class SparqlHelperTest extends \PHPUnit_Framework_TestCase {
 
        use DefaultConfig, ResultAssertions;
+
+       private function selectResults( array $bindings ) {
+               return new CachedQueryResults(
+                       [ 'results' => [ 'bindings' => $bindings ] ],
+                       CachingMetadata::fresh()
+               );
+       }
+
+       private function askResult( $boolean ) {
+               return new CachedQueryResults(
+                       [ 'boolean' => $boolean ],
+                       CachingMetadata::fresh()
+               );
+       }
 
        public function testHasType() {
                $sparqlHelper = $this->getMockBuilder( SparqlHelper::class )
@@ -63,7 +79,7 @@
 
                $sparqlHelper->expects( $this->exactly( 1 ) )
                        ->method( 'runQuery' )
-                       ->willReturn( [ 'boolean' => true ] )
+                       ->willReturn( $this->askResult( true ) )
                        ->withConsecutive( [ $this->equalTo( $query ) ] );
 
                $this->assertTrue( $sparqlHelper->hasType( 'Q1', [ 'Q100', 
'Q101' ], true ) );
@@ -109,10 +125,10 @@
 
                $sparqlHelper->expects( $this->exactly( 1 ) )
                        ->method( 'runQuery' )
-                       ->willReturn( [ 'head' => [ 'vars' => [ 'otherEntity' ] 
], 'results' => [ 'bindings' => [
+                       ->willReturn( $this->selectResults( [
                                [ 'otherEntity' => [ 'type' => 'uri', 'value' 
=> 'http://www.wikidata.org/entity/Q100' ] ],
                                [ 'otherEntity' => [ 'type' => 'uri', 'value' 
=> 'http://www.wikidata.org/entity/Q101' ] ],
-                       ] ] ] )
+                       ] ) )
                        ->withConsecutive( [ $this->equalTo( $query ) ] );
 
                $this->assertEquals(
@@ -164,10 +180,10 @@
 
                $sparqlHelper->expects( $this->exactly( 1 ) )
                        ->method( 'runQuery' )
-                       ->willReturn( [ 'head' => [ 'vars' => [ 'otherEntity' ] 
], 'results' => [ 'bindings' => [
+                       ->willReturn( $this->selectResults( [
                                [ 'otherEntity' => [ 'type' => 'uri', 'value' 
=> 'http://www.wikidata.org/entity/Q100' ] ],
                                [ 'otherEntity' => [ 'type' => 'uri', 'value' 
=> 'http://www.wikidata.org/entity/Q101' ] ],
-                       ] ] ] )
+                       ] ) )
                        ->withConsecutive( [ $this->equalTo( $query ) ] );
 
                $this->assertEquals(
@@ -293,7 +309,7 @@
                $sparqlHelper->expects( $this->once() )
                        ->method( 'runQuery' )
                        ->with( $this->equalTo( $query ) )
-                       ->willReturn( [ 'results' => [ 'bindings' => [ [ 
'matches' => [ 'value' => 'false' ] ] ] ] ] );
+                       ->willReturn( $this->selectResults( [ [ 'matches' => [ 
'value' => 'false' ] ] ] ) );
 
                $result = $sparqlHelper->matchesRegularExpressionWithSparql( 
$text, $regex );
 
@@ -313,7 +329,7 @@
                $sparqlHelper->expects( $this->once() )
                        ->method( 'runQuery' )
                        ->with( $this->equalTo( $query ) )
-                       ->willReturn( [ 'results' => [ 'bindings' => [ [] ] ] ] 
);
+                       ->willReturn( $this->selectResults( [ [] ] ) );
 
                try {
                        call_user_func_array( [ $sparqlHelper, 
'matchesRegularExpressionWithSparql' ], [ $text, $regex ] );

-- 
To view, visit https://gerrit.wikimedia.org/r/391278
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I80930ee94a3bef26b07f001ce2d4f346a3b6ff9a
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

Reply via email to