Samwilson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/332737 )

Change subject: [WIP] Add an expiry to the localStorage replication of the 
block cookie
......................................................................

[WIP] Add an expiry to the localStorage replication of the block cookie

The block cookie is replicated to localStorage in order that the cookie
can be recreated if it's removed. However, this would have recreated
the cookie even years after the expiry of the original cookie because
localStorage never expires.

This change adds an expiry time to the localStorage data (by switching
it to be a JSON string with 'data' and 'expiry' keys) and only recreates
the cookie if the expiry time is greater than the current time.

Bug: T152952
Change-Id: Ifb06dc2390f4d648d7fcb39e30267de5eddc6941
---
M includes/EditPage.php
M resources/src/mediawiki/mediawiki.user.blockcookie.js
2 files changed, 52 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/37/332737/1

diff --git a/includes/EditPage.php b/includes/EditPage.php
index 05fa366..102a1c9 100644
--- a/includes/EditPage.php
+++ b/includes/EditPage.php
@@ -2331,12 +2331,17 @@
        }
 
        function setHeaders() {
-               global $wgOut, $wgUser, $wgAjaxEditStash, 
$wgCookieSetOnAutoblock;
+               global $wgOut, $wgUser, $wgAjaxEditStash;
 
                $wgOut->addModules( 'mediawiki.action.edit' );
-               if ( $wgCookieSetOnAutoblock === true ) {
+
+               // Add JS for tracking block ID cookies in localStorage.
+               $config = RequestContext::getMain()->getConfig();
+               if ( $config->get( 'CookieSetOnAutoblock' ) === true ) {
                        $wgOut->addModules( 'mediawiki.user.blockcookie' );
+                       $wgOut->addJsConfigVars( 'wgAutoblockExpiry', 
$config->get( 'AutoblockExpiry' ) );
                }
+
                $wgOut->addModuleStyles( 'mediawiki.action.edit.styles' );
 
                if ( $wgUser->getOption( 'showtoolbar' ) ) {
diff --git a/resources/src/mediawiki/mediawiki.user.blockcookie.js 
b/resources/src/mediawiki/mediawiki.user.blockcookie.js
index ffff039..47f97c5 100644
--- a/resources/src/mediawiki/mediawiki.user.blockcookie.js
+++ b/resources/src/mediawiki/mediawiki.user.blockcookie.js
@@ -1,18 +1,50 @@
-( function ( mw ) {
+(
 
-       // If a user has been autoblocked, a cookie is set.
-       // Its value is replicated here in localStorage to guard against 
cookie-removal.
-       // This module will only be loaded when $wgCookieSetOnAutoblock is true.
-       // Ref: https://phabricator.wikimedia.org/T5233
+/**
+ * If a user has been autoblocked, a cookie is set.
+ * Its value is replicated here in localStorage to guard against 
cookie-removal.
+ * The localStorage item will be removed after $wgAutoblockExpiry (default 
86400 seconds).
+ * This module will only be loaded when $wgCookieSetOnAutoblock is true.
+ * Ref: T5233
+ *
+ * This used to just store the block ID, but for T152952 was changed to also 
store an expiry time in order that the
+ * localStorage data be removed after $wgAutoblockExpiry (default 86400 
seconds).
+ */
+function ( mw ) {
+       var expiry, cookieVal, storageValue, storedObject, storedExpiry;
+
+       // Calculate the expiry time (add milliseconds to current time).
+       expiry = Date.now() + ( mw.config.get( 'wgAutoblockExpiry' ) * 1000 );
+
+       //cookieVal = mw.cookie.get( 'BlockID' );
+       console.log( document.cookie );
+
+       // There are three parts to this next bit:
+       // 1. cookie doesn't exist and there's nothing in localStorage;
+       // 2. cookie does exist and there's nothing in localStorage; and
+       // 3. cookie is blank and there is something in localStorage.
 
        if ( !mw.cookie.get( 'BlockID' ) && mw.storage.get( 'blockID' ) ) {
-               // The block ID exists in storage, but not in the cookie.
-               mw.cookie.set( 'BlockID', mw.storage.get( 'blockID' ) );
+               // The block ID exists in storage, but the cookie doesn't 
exist, so we re-create the cookie.
+               // The cookie value is either just the whole stored value, or 
just the 'data' element if it exists.
+               storageValue = mw.storage.get( 'BlockID' );
+               storedObject = JSON.parse( storageValue );
+               if ( ! "data" in storedObject ) {
+                       // If there's no 'data' property, this must be an 
old-style localStorage item, so we give it an expiry date.
+                       storedObject = { data: storageValue, expiry: expiry }
+               }
+               storedExpiry = new Date( storedObject.expiry );
+               if ( storedExpiry >= new Date() ) {
+                       // localStorage data hasn't expired, so use it to 
recreate the cookie.
+                       mw.cookie.set( 'BlockID', storedObject.data );
+               }
 
-       } else if ( parseInt( mw.cookie.get( 'BlockID' ), 10 ) > 0 && 
!mw.storage.get( 'blockID' ) ) {
+       } else if ( mw.cookie.get( 'BlockID' ) !== null && !mw.storage.get( 
'blockID' ) ) {
                // The block ID exists in the cookie, but not in storage.
-               // (When a block expires the cookie remains but its value is 
'', hence the integer check above.)
-               mw.storage.set( 'blockID', mw.cookie.get( 'BlockID' ) );
+               // (When a block expires the cookie remains but its value is 
''.)
+               cookieVal = mw.cookie.get( 'BlockID' );
+               storedObject = { data: cookieVal, expiry: expiry };
+               mw.storage.set( 'blockID', JSON.stringify( storedObject ) );
 
        } else if ( mw.cookie.get( 'BlockID' ) === '' && mw.storage.get( 
'blockID' ) ) {
                // If only the empty string is in the cookie, remove the 
storage value. The block is no longer valid.
@@ -20,4 +52,6 @@
 
        }
 
-}( mediaWiki ) );
+}( mediaWiki )
+
+);

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifb06dc2390f4d648d7fcb39e30267de5eddc6941
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Samwilson <s...@samwilson.id.au>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to