jenkins-bot has submitted this change and it was merged.

Change subject: Add dates to banner-hiding cookies (client side)
......................................................................


Add dates to banner-hiding cookies (client side)

This change allows the bannerController to understand both
the old 'hide' value and a new JSON format:
{
        "v" : 1, (cookie version)
        "created" : 12345678,
        "reason" : "close"
}
where "created" is in seconds since the epoch, and "reason" indicates
why the banner should be hidden (i.e., the user has donated or has
clicked the close button on a banner.)

An array of cookie durations is provided in mw.config corresponding to
the possible hide reasons.

This should be thoroughly deployed before updating Special:HideBanners
to write cookies in the new format.

Change-Id: I02988b260d50db2bc61fe32f95490f0339a52e38
---
M CentralNotice.hooks.php
M CentralNotice.php
M modules/ext.centralNotice.bannerController/bannerController.js
M special/SpecialHideBanners.php
4 files changed, 52 insertions(+), 31 deletions(-)

Approvals:
  Awight: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/CentralNotice.hooks.php b/CentralNotice.hooks.php
index 5940b32..bce5a33 100644
--- a/CentralNotice.hooks.php
+++ b/CentralNotice.hooks.php
@@ -285,7 +285,7 @@
        global $wgNoticeFundraisingUrl, $wgCentralPagePath, $wgContLang, 
$wgNoticeXXCountries,
                   $wgNoticeInfrastructure, $wgNoticeCloseButton, 
$wgCentralBannerDispatcher,
                   $wgCentralBannerRecorder, $wgNoticeNumberOfBuckets, 
$wgNoticeBucketExpiry,
-                  $wgNoticeNumberOfControllerBuckets, 
$wgNoticeCookieShortExpiry, $wgScript,
+                  $wgNoticeNumberOfControllerBuckets, 
$wgNoticeCookieDurations, $wgScript,
                   $wgNoticeHideUrls;
 
        // Making these calls too soon will causes issues with the namespace 
localisation cache. This seems
@@ -319,7 +319,7 @@
        $vars[ 'wgNoticeNumberOfBuckets' ] = $wgNoticeNumberOfBuckets;
        $vars[ 'wgNoticeBucketExpiry' ] = $wgNoticeBucketExpiry;
        $vars[ 'wgNoticeNumberOfControllerBuckets' ] = 
$wgNoticeNumberOfControllerBuckets;
-       $vars[ 'wgNoticeCookieShortExpiry' ] = $wgNoticeCookieShortExpiry;
+       $vars[ 'wgNoticeCookieDurations' ] = $wgNoticeCookieDurations;
        $vars[ 'wgNoticeHideUrls' ] = $wgNoticeHideUrls;
 
        if ( $wgNoticeInfrastructure ) {
diff --git a/CentralNotice.php b/CentralNotice.php
index 3a49495..3f420ce 100644
--- a/CentralNotice.php
+++ b/CentralNotice.php
@@ -111,13 +111,20 @@
 // Example: '.wikipedia.org'
 $wgNoticeCookieDomain = '';
 
-// The amount of time banners will be hidden by the close box.
-// Defaults to two weeks.
-$wgNoticeCookieShortExpiry = 1209600;
-
-// Amount of time the banners will hide after a successful donation.
-// Defaults to one year.
-$wgNoticeCookieLongExpiry = 31536000;
+/**
+ * @var string[] $wgNoticeCookieDurations How long to respect different types
+ * of banner hiding cookies, in seconds. bannerController.js selects one of
+ * these entries based on the  cookie's 'reason' element and adds that to the
+ * cookie's 'created' element to determine when to stop hiding the banner.
+ */
+$wgNoticeCookieDurations = array(
+       // The amount of time banners will be hidden by the close box.
+       // Defaults to two weeks.
+       'close' => 1209600,
+       // Amount of time the banners will hide after a successful donation.
+       // Defaults to one year.
+       'donate' => 31536000
+);
 
 /**
  * @var string[] $wgNoticeHideUrls Locations of Special:HideBanner targets to 
hit
diff --git a/modules/ext.centralNotice.bannerController/bannerController.js 
b/modules/ext.centralNotice.bannerController/bannerController.js
index 68cebef..9b4e1b8 100644
--- a/modules/ext.centralNotice.bannerController/bannerController.js
+++ b/modules/ext.centralNotice.bannerController/bannerController.js
@@ -273,7 +273,7 @@
        //
        // TODO: Migrate away from global functions
        window.insertBanner = function ( bannerJson ) {
-               var url, targets;
+               var url, targets, expiry, cookieVal;
 
                var impressionData = {
                        country: mw.centralNotice.data.country,
@@ -304,21 +304,28 @@
                                        result: 'hide',
                                        reason: 'preload'
                                };
-                       } else if (
-                               mw.centralNotice.data.testing === false && /* 
And we want to see what we're testing! :) */
-                               (
-                                       $.cookie( 'centralnotice_hide_' + 
mw.centralNotice.data.category ) === 'hide' ||
-                                       /* Legacy for long duration fundraising 
cookies; remove after Oct 1, 2014 */
-                                       $.cookie( 'centralnotice_' + 
mw.centralNotice.data.category ) === 'hide'
-                               )
-                       ) {
-                               // The banner was hidden by a category hide 
cookie and we're not testing
-                               impressionResultData = {
-                                       result: 'hide',
-                                       reason: 'cookie'
-                               };
-                       } else {
-                               // All conditions fulfilled, inject the banner
+                       } else if ( mw.centralNotice.data.testing === false ) { 
/* And we want to see what we're testing! :) */
+                               cookieVal = $.cookie( 'centralnotice_hide_' + 
mw.centralNotice.data.category );
+                               expiry = mw.config.get( 
'wgNoticeCookieDurations' );
+
+                               if (
+                                       cookieVal === 'hide' ||
+                                       (
+                                               cookieVal !== null &&
+                                               cookieVal.indexOf( '{' ) === 0 
&&
+                                               expiry[$.parseJSON( cookieVal 
).reason] &&
+                                               $.parseJSON( cookieVal 
).created + expiry[$.parseJSON( cookieVal ).reason] > new Date().getTime() / 
1000
+                                       )
+                               ) {
+                                       // The banner was hidden by a category 
hide cookie and we're not testing
+                                       impressionResultData = {
+                                               result: 'hide',
+                                               reason: 'cookie'
+                                       };
+                               }
+                       }
+                       if ( !impressionResultData ) {
+                               // Not hidden yet, inject the banner
                                mw.centralNotice.bannerData.bannerName = 
bannerJson.bannerName;
                                $( 'div#centralNotice' )
                                        .attr( 'class', mw.html.escape( 'cn-' + 
mw.centralNotice.data.category ) )
@@ -377,19 +384,24 @@
        };
 
        // Function for hiding banners when the user clicks the close button
-       // TODO: Make it call up to the special page for cross project hiding
        window.hideBanner = function () {
                var d = new Date(),
-                       expiry = mw.config.get( 'wgNoticeCookieShortExpiry' );
+                       cookieVal = {
+                               v: 1,
+                               created: Math.floor( d.getTime() / 1000 ),
+                               reason: 'close'
+                       },
+                       expiry = mw.config.get( 'wgNoticeCookieDurations' 
).close;
 
                // Immediately hide the banner on the page
                $( '#centralNotice' ).hide();
 
                // Set a local hide cookie for this banner category
                d.setSeconds( d.getSeconds() + expiry );
+
                $.cookie(
                        'centralnotice_hide_' + mw.centralNotice.data.category,
-                       'hide',
+                       $.toJSON( cookieVal ),
                        { expires: d, path: '/' }
                );
 
@@ -398,7 +410,8 @@
                $.each( mw.config.get( 'wgNoticeHideUrls' ), function( idx, 
value ) {
                        (new Image()).src = value + '?' + $.param( {
                                'duration': expiry,
-                               'category': mw.centralNotice.data.category
+                               'category': mw.centralNotice.data.category,
+                               'reason' : 'close'
                        } );
                } );
 
diff --git a/special/SpecialHideBanners.php b/special/SpecialHideBanners.php
index 18e983e..e8f2bb1 100644
--- a/special/SpecialHideBanners.php
+++ b/special/SpecialHideBanners.php
@@ -14,9 +14,10 @@
        }
 
        function execute( $par ) {
-               global $wgNoticeCookieLongExpiry;
+               global $wgNoticeCookieDurations;
 
-               $duration = $this->getRequest()->getInt( 'duration', 
$wgNoticeCookieLongExpiry );
+               $reason = 'donate'; //Once bannerController.js is fully 
deployed, get this from the query string
+               $duration = $this->getRequest()->getInt( 'duration', 
$wgNoticeCookieDurations[$reason] );
                $category = $this->getRequest()->getText( 'category', 
'fundraising' );
                $category = Banner::sanitizeRenderedCategory( $category );
                $this->setHideCookie( $category, $duration );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I02988b260d50db2bc61fe32f95490f0339a52e38
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Mwalker <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to