jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/390025 )
Change subject: Add function to get cache max-age in SparqlHelper
......................................................................
Add function to get cache max-age in SparqlHelper
This function can then later be used to pass that max-age up the chain,
eventually to tell the user how outdated a cached constraint check
result might be.
This also adds tests, including for the new code added in the previous
commit (which this commit moves to the new function).
Change-Id: Ife6e593b10cb78a1b3bc47cd097d22aaf4b48cb5
---
M includes/ConstraintCheck/Helper/SparqlHelper.php
M tests/phpunit/Helper/SparqlHelperTest.php
2 files changed, 65 insertions(+), 1 deletion(-)
Approvals:
Jonas Kress (WMDE): Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/ConstraintCheck/Helper/SparqlHelper.php
b/includes/ConstraintCheck/Helper/SparqlHelper.php
index c4161a0..ba521d1 100644
--- a/includes/ConstraintCheck/Helper/SparqlHelper.php
+++ b/includes/ConstraintCheck/Helper/SparqlHelper.php
@@ -447,6 +447,33 @@
}
/**
+ * Return the max-age of a cached response,
+ * or a boolean indicating whether the response was cached or not.
+ *
+ * @param array $responseHeaders see MWHttpRequest::getResponseHeaders()
+ * @return integer|boolean the max-age (in seconds)
+ * or a plain boolean if no max-age can be determined
+ */
+ public function getCacheMaxAge( $responseHeaders ) {
+ if (
+ array_key_exists( 'x-cache-status', $responseHeaders )
&&
+ preg_match( '/^hit(?:-.*)?$/',
$responseHeaders['x-cache-status'] )
+ ) {
+ $maxage = [];
+ if (
+ array_key_exists( 'cache-control',
$responseHeaders ) &&
+ preg_match( '/\bmax-age=(\d+)\b/',
$responseHeaders['cache-control'], $maxage )
+ ) {
+ return intval( $maxage[1] );
+ } else {
+ return true;
+ }
+ } else {
+ return false;
+ }
+ }
+
+ /**
* Runs a query against the configured endpoint and returns the results.
*
* @param string $query The query, unencoded (plain string).
@@ -484,7 +511,7 @@
( $endTime - $startTime ) * 1000
);
- if ( preg_match( '/^hit(?:-.*)?$/',
$request->getResponseHeader( 'X-Cache-Status' ) ) ) {
+ if ( $this->getCacheMaxAge( $request->getResponseHeaders() ) ) {
$this->dataFactory->increment(
'wikibase.quality.constraints.sparql.cached' );
}
diff --git a/tests/phpunit/Helper/SparqlHelperTest.php
b/tests/phpunit/Helper/SparqlHelperTest.php
index fcb4e51..ec4109e 100644
--- a/tests/phpunit/Helper/SparqlHelperTest.php
+++ b/tests/phpunit/Helper/SparqlHelperTest.php
@@ -407,4 +407,41 @@
];
}
+ /**
+ * @dataProvider getCacheMaxAgeProvider
+ */
+ public function testGetCacheMaxAge( $responseHeaders, $expected ) {
+ $sparqlHelper = new SparqlHelper(
+ $this->getDefaultConfig(),
+ new RdfVocabulary(
+ 'http://www.wikidata.org/entity/',
+
'http://www.wikidata.org/wiki/Special:EntityData/'
+ ),
+ new ItemIdParser(),
+ $this->getMock( PropertyDataTypeLookup::class ),
+ WANObjectCache::newEmpty()
+ );
+
+ $actual = $sparqlHelper->getCacheMaxAge( $responseHeaders );
+
+ $this->assertSame( $expected, $actual );
+ }
+
+ public function getCacheMaxAgeProvider() {
+ return [
+ 'WDQS hit' => [
+ [ 'x-cache-status' => 'hit-front',
'cache-control' => 'public, max-age=300' ],
+ 300
+ ],
+ 'WDQS miss' => [
+ [ 'x-cache-status' => 'miss', 'cache-control'
=> 'public, max-age=300' ],
+ false
+ ],
+ 'generic hit' => [
+ [ 'x-cache-status' => 'hit' ],
+ true
+ ],
+ ];
+ }
+
}
--
To view, visit https://gerrit.wikimedia.org/r/390025
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ife6e593b10cb78a1b3bc47cd097d22aaf4b48cb5
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits