AndyRussG has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/169721

Change subject: WIP Temporary fix for bucket stickiness and expiry
......................................................................

WIP Temporary fix for bucket stickiness and expiry

This is a temporary measure to ensure that buckets do not cycle
haphasardly as described here:
https://www.mediawiki.org/wiki/Extension:CentralNotice/Buckets#Cycling_bucket_cookie_expiries.3F

With this patch, bucket cookies work like this:
- When requesting a banner, get a bucket from the bucket cookie, or
  if none is available, choose one randomly (same functionality as
  before).
- Only store the bucket in a cookie if a banner is received.
- Every time a banner is received, extend the bucket cookie's validity
  to last into the future the time indicated by wgNoticeBucketExpiry.
- If a banner is not received and there is a bucket cookie, remove/
  invalidate it to make sure a new bucet is chosen next time.

Change-Id: If520a709856d835ca37d41630329aeefeea466e7
---
M modules/ext.centralNotice.bannerController/bannerController.js
1 file changed, 37 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralNotice 
refs/changes/21/169721/1

diff --git a/modules/ext.centralNotice.bannerController/bannerController.js 
b/modules/ext.centralNotice.bannerController/bannerController.js
index b1674bc..33bb2de 100644
--- a/modules/ext.centralNotice.bannerController/bannerController.js
+++ b/modules/ext.centralNotice.bannerController/bannerController.js
@@ -26,7 +26,10 @@
  */
 ( function ( $, mw ) {
 
-       var rPlus = /\+/g;
+       var rPlus = /\+/g,
+               bucketValidityFromServer = mw.config.get( 
'wgNoticeNumberOfBuckets' )
+                       + '.' + mw.config.get( 
'wgNoticeNumberOfControllerBuckets' );
+
        function decode( s ) {
                try {
                        // decodeURIComponent can throw an exception for 
unknown char encodings.
@@ -183,20 +186,36 @@
                getBucket: function() {
                        var dataString = $.cookie( 'centralnotice_bucket' ) || 
'',
                                bucket = dataString.split('-')[0],
-                               validity = dataString.split('-')[1],
-                               expValidity = mw.config.get( 
'wgNoticeNumberOfBuckets' ) + '.' + mw.config.get( 
'wgNoticeNumberOfControllerBuckets' );
+                               validity = dataString.split('-')[1];
 
-                       if ( ( bucket === null ) || ( validity !== expValidity 
) ) {
+                       if ( ( bucket === null ) || ( validity !== 
bucketValidityFromServer ) ) {
                                bucket = Math.floor(
                                        Math.random() * mw.config.get( 
'wgNoticeNumberOfControllerBuckets' )
-                               );
-                               $.cookie(
-                                       'centralnotice_bucket', bucket + '-' + 
expValidity,
-                                       { expires: mw.config.get( 
'wgNoticeBucketExpiry' ), path: '/' }
                                );
                        }
 
                        return bucket;
+               },
+               storeBucket: function() {
+                       $.cookie(
+                               'centralnotice_bucket',
+                               mw.centralNotice.data.bucket + '-' + 
bucketValidityFromServer,
+                               { expires: mw.config.get( 
'wgNoticeBucketExpiry' ), path: '/' }
+                       );
+               },
+               clearStoredBucket: function() {
+
+                       if ( $.cookie( 'centralnotice_bucket' ) ) {
+
+                               // We can't reliably remove the cookie, so just 
set its
+                               // expiry to now, and make sure it contains 
invalid data to
+                               // be sure a new one will be chosen in case the 
cookie is still
+                               // around next time.
+                               $.cookie(
+                                       'centralnotice_bucket', 'invalid',
+                                       { expires: 0, path: '/' }
+                               );
+                       }
                },
                initialize: function () {
                        // === Do not allow CentralNotice to be re-initialized. 
===
@@ -291,11 +310,21 @@
                        // There was no banner returned from the server
                        hideBanner = true;
                        impressionData.reason = 'empty';
+
+                       // Clear local bucket storage. This should help keep 
buckets
+                       // reliably sticky when users _do_ get a banner.
+                       mw.centralNotice.clearStoredBucket();
+
                } else {
                        // Ok, we have a banner!
                        impressionData.banner = bannerJson.bannerName;
                        impressionData.campaign = bannerJson.campaign;
 
+                       // Store the bucket we used in a cookie. If it's 
already there, this
+                       // should extend the bucket cookie's expiry the duration
+                       // indicated by wgNoticeBucketExpiry.
+                       mw.centralNotice.storeBucket();
+
                        // Get the banner type for more queryness
                        mw.centralNotice.data.category = encodeURIComponent( 
bannerJson.category );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If520a709856d835ca37d41630329aeefeea466e7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: AndyRussG <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to