From: Gilles Dubuc Date: Thu, 22 Mar 2018 13:06:56 +0000 (+0100) Subject: mediawiki.user: Implement mw.user.stickyRandomId X-Git-Tag: 1.31.0-rc.0~267^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/categories/modifier.php?a=commitdiff_plain;h=6123a7a071c522b012a97ab3d5bc9b0bfa045651;p=lhc%2Fweb%2Fwiklou.git mediawiki.user: Implement mw.user.stickyRandomId This is a sticky version of generateRandomSessionId, useful to keep track of the pageview between extensions. Bug: T187299 Change-Id: I0877c399c60d3fb2fdf8e844cad6acecf6f704c9 --- diff --git a/resources/src/mediawiki/mediawiki.user.js b/resources/src/mediawiki/mediawiki.user.js index 65e9e4168d..5fc1990758 100644 --- a/resources/src/mediawiki/mediawiki.user.js +++ b/resources/src/mediawiki/mediawiki.user.js @@ -4,7 +4,7 @@ */ /* global Uint32Array */ ( function ( mw, $ ) { - var userInfoPromise; + var userInfoPromise, stickyRandomSessionId; /** * Get the current user's groups or rights @@ -48,7 +48,7 @@ // Support: IE 11 crypto = window.crypto || window.msCrypto; - if ( crypto && crypto.getRandomValues ) { + if ( crypto && crypto.getRandomValues && typeof Uint32Array === 'function' ) { // Fill an array with 2 random values, each of which is 32 bits. // Note that Uint32Array is array-like but does not implement Array. rnds = new Uint32Array( 2 ); @@ -71,6 +71,20 @@ return hexRnds.join( '' ); }, + /** + * A sticky generateRandomSessionId for the current JS execution context, + * cached within this class. + * + * @return {string} 64 bit integer in hex format, padded + */ + stickyRandomId: function () { + if ( !stickyRandomSessionId ) { + stickyRandomSessionId = mw.user.generateRandomSessionId(); + } + + return stickyRandomSessionId; + }, + /** * Get the current user's database id * diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js index bc12642983..814a20753e 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js @@ -97,6 +97,14 @@ assert.notEqual( result, result2, 'different when called multiple times' ); } ); + QUnit.test( 'stickyRandomId', function ( assert ) { + var result = mw.user.stickyRandomId(), + result2 = mw.user.stickyRandomId(); + assert.equal( typeof result, 'string', 'type' ); + assert.strictEqual( /^[a-f0-9]{16}$/.test( result ), true, '16 HEX symbols string' ); + assert.equal( result2, result, 'sticky' ); + } ); + QUnit.test( 'sessionId', function ( assert ) { var result = mw.user.sessionId(), result2 = mw.user.sessionId();