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

Change subject: Add maximum age to CachedResult
......................................................................

Add maximum age to CachedResult

A CachedResult can now also hold the information for how old the cached
value may be, in addition to just whether it was cached or not.
SparqlHelper extracts this information from the Cache-Control header
(see commit ca11b298c5, change Ife6e593b10), and the gadget uses it to
display a more helpful message.

Bug: T179844
Change-Id: I3ea1eb3a966394068930e74fe6a234b6bae2d9a3
---
M api/CheckConstraints.php
M extension.json
M i18n/en.json
M i18n/qqq.json
M includes/ConstraintCheck/Helper/SparqlHelper.php
M includes/ConstraintCheck/Result/CachedResult.php
M modules/gadget.js
M tests/phpunit/Result/CachedResultTest.php
8 files changed, 70 insertions(+), 11 deletions(-)


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

diff --git a/api/CheckConstraints.php b/api/CheckConstraints.php
index e5bb549..81aecb8 100644
--- a/api/CheckConstraints.php
+++ b/api/CheckConstraints.php
@@ -373,7 +373,11 @@
                                        $result['claim'] = 
$checkResult->getContext()->getSnakStatement()->getGuid();
                                }
                                if ( $checkResult->isCached() ) {
-                                       $result['cached'] = true;
+                                       if ( $checkResult->getMaxAge() !== 
false ) {
+                                               $result['cached'] = [ 'max-age' 
=> $checkResult->getMaxAge() ];
+                                       } else {
+                                               $result['cached'] = true;
+                                       }
                                }
                        }
 
diff --git a/extension.json b/extension.json
index 26a31e9..c3e77af 100644
--- a/extension.json
+++ b/extension.json
@@ -74,7 +74,8 @@
                                "wbqc-parameterissues-long",
                                "wbqc-constrainttypehelp-short",
                                "wbqc-constrainttypehelp-long",
-                               "wbqc-cached-generic"
+                               "wbqc-cached-generic",
+                               "wbqc-cached-minutes"
                        ],
                        "scripts": [
                                "modules/ui/module.js",
diff --git a/i18n/en.json b/i18n/en.json
index 6aef96d..b569f3b 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -43,6 +43,7 @@
        "wbqc-constrainttypehelp-short": "Help",
        "wbqc-constrainttypehelp-long": "Help page for this constraint type",
        "wbqc-cached-generic": "This result is cached and might be out of 
date.",
+       "wbqc-cached-minutes": "This result is cached and might be out of date 
by up to {{PLURAL:$1|1=one minute|$1 minutes}}.",
 
        "apihelp-wbcheckconstraints-description": "Performs constraint checks 
on any entity you want and returns the result.",
        "apihelp-wbcheckconstraints-summary": "Performs constraint checks on 
any entity you want and returns the result.",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index c6bf83d..72576d4 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -45,6 +45,7 @@
        "wbqc-constrainttypehelp-short": "Text for the help link shown next to 
the constraint type in a constraint violation report. The link leads to a help 
page on Wikidata about the constraint type.\n{{Identical|Help}}",
        "wbqc-constrainttypehelp-long": "Title for the help link shown next to 
the constraint type in a constraint violation report. The link leads to a help 
page on Wikidata about the constraint 
type.\n{{Related|wbqc-constrainttypehelp-short}}",
        "wbqc-cached-generic": "Generic message informing the user that a 
constraint check result might be outdated. There is no information on how old 
the cached result is; if more information is available, a more specific message 
is used.",
+       "wbqc-cached-minutes": "Message informing the user that a constraint 
check result might be updated by up to ''n'' 
minutes.\n{{Related|wbqc-cached-generic}}",
        "apihelp-wbcheckconstraints-description": 
"{{doc-apihelp-description|wbcheckconstraints}}",
        "apihelp-wbcheckconstraints-summary": 
"{{doc-apihelp-summary|wbcheckconstraints}}",
        "apihelp-wbcheckconstraints-param-id": 
"{{doc-apihelp-param|wbcheckconstraints|id}}",
diff --git a/includes/ConstraintCheck/Helper/SparqlHelper.php 
b/includes/ConstraintCheck/Helper/SparqlHelper.php
index 1089cd8..8878bb5 100644
--- a/includes/ConstraintCheck/Helper/SparqlHelper.php
+++ b/includes/ConstraintCheck/Helper/SparqlHelper.php
@@ -522,18 +522,16 @@
                        ( $endTime - $startTime ) * 1000
                );
 
-               if ( $this->getCacheMaxAge( $request->getResponseHeaders() ) ) {
-                       $cached = true;
+               $maxAge = $this->getCacheMaxAge( $request->getResponseHeaders() 
);
+               if ( $maxAge ) {
                        $this->dataFactory->increment( 
'wikibase.quality.constraints.sparql.cached' );
-               } else {
-                       $cached = false;
                }
 
                if ( $status->isOk() ) {
                        $json = $request->getContent();
                        $arr = json_decode( $json, true );
                        return ( new CachedResult( $arr ) )
-                               ->mergeCachedStatus( $cached );
+                               ->mergeCachedStatus( $maxAge );
                } else {
                        $this->dataFactory->increment( 
'wikibase.quality.constraints.sparql.error' );
 
diff --git a/includes/ConstraintCheck/Result/CachedResult.php 
b/includes/ConstraintCheck/Result/CachedResult.php
index 8d1c735..a3bc698 100644
--- a/includes/ConstraintCheck/Result/CachedResult.php
+++ b/includes/ConstraintCheck/Result/CachedResult.php
@@ -2,6 +2,8 @@
 
 namespace WikibaseQuality\ConstraintReport\ConstraintCheck\Result;
 
+use RangeException;
+
 /**
  * A generic container for any kind of value which may be cached,
  * holding the value as well as information about whether and how it was 
cached.
@@ -20,6 +22,11 @@
         * @var boolean Whether the result is cached or not.
         */
        private $isCached = false;
+
+       /**
+        * @var integer|boolean The maximum age of the cached value in seconds, 
if known.
+        */
+       private $maxAge = false;
 
        /**
         * @param mixed $value The wrapped value.
@@ -43,22 +50,49 @@
        }
 
        /**
+        * @return integer|boolean The maximum age of the cached result in 
seconds
+        * if the result is cached and the maximum age is known,
+        * or false to indicate that the maximum age is unknown or the result 
is not cached.
+        */
+       public function getMaxAge() {
+               if ( is_integer( $this->maxAge ) ) {
+                       return $this->maxAge;
+               } else {
+                       return false;
+               }
+       }
+
+       /**
         * Merge the cached status of the other result into this result.
         * Use this if the value wrapped in this result
         * was derived from the value wrapped in the other result.
-        * The other result may also just be a boolean
-        * indicating whether the value is cached or not.
         *
-        * @param CachedResult|boolean $other
+        * The other result may also be an integer
+        * indicating the maximum age of the cached value
+        * (implying that it was cached),
+        * or a boolean indicating whether the value was cached or not.
+        *
+        * @param CachedResult|integer|boolean $other
         * @return self
         */
        public function mergeCachedStatus( $other ) {
                if ( $other instanceof CachedResult ) {
                        $otherIsCached = $other->isCached;
+                       $otherMaxAge = $other->maxAge;
+               } elseif ( is_integer( $other ) ) {
+                       if ( $other <= 0 ) {
+                               throw new RangeException( 'max age must be 
strictly positive' );
+                       }
+                       $otherIsCached = true;
+                       $otherMaxAge = $other;
                } else {
                        $otherIsCached = $other;
+                       $otherMaxAge = false;
                }
+
                $this->isCached = $this->isCached || $otherIsCached;
+               $this->maxAge = max( $this->maxAge, $otherMaxAge );
+
                return $this;
        }
 
diff --git a/modules/gadget.js b/modules/gadget.js
index 71964d2..f9654db 100644
--- a/modules/gadget.js
+++ b/modules/gadget.js
@@ -31,7 +31,14 @@
                        message: result[ 'message-html' ]
                };
                if ( result.cached ) {
-                       config.ancillaryMessages = [ mw.message( 
'wbqc-cached-generic' ).escaped() ];
+                       if ( typeof result.cached === 'object' && typeof 
result.cached['max-age'] === 'number' ) {
+                               var maxAge = result.cached['max-age'];
+                               config.ancillaryMessages = [
+                                       
mw.message('wbqc-cached-minutes').params( [ Math.ceil(maxAge / 60) ] ).escaped()
+                               ];
+                       } else {
+                               config.ancillaryMessages = [ 
mw.message('wbqc-cached-generic').escaped() ];
+                       }
                }
                return new wb.quality.constraints.ui.ConstraintReportPanel( 
config );
        }
diff --git a/tests/phpunit/Result/CachedResultTest.php 
b/tests/phpunit/Result/CachedResultTest.php
index fef5d26..976b69c 100644
--- a/tests/phpunit/Result/CachedResultTest.php
+++ b/tests/phpunit/Result/CachedResultTest.php
@@ -24,9 +24,22 @@
                $result->mergeCachedStatus( false );
                $this->assertTrue( $result->isCached(), "should still be 
cached" );
 
+               $result->mergeCachedStatus( 10 );
+               $this->assertTrue( $result->isCached() );
+               $this->assertSame( 10, $result->getMaxAge(), "set max age" );
+
+               $result->mergeCachedStatus( 20 );
+               $this->assertTrue( $result->isCached() );
+               $this->assertSame( 20, $result->getMaxAge(), "increase max age" 
);
+
+               $result->mergeCachedStatus( 5 );
+               $this->assertTrue( $result->isCached() );
+               $this->assertSame( 20, $result->getMaxAge(), "max age should 
not decrease" );
+
                $result2 = new CachedResult( null );
                $result2->mergeCachedStatus( $result );
                $this->assertTrue( $result2->isCached(), "cached status should 
propagate" );
+               $this->assertSame( 20, $result2->getMaxAge(), "cached status 
should propagate" );
        }
 
 }

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

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