5 /* global Uint32Array */
7 var userInfoPromise
, stickyRandomSessionId
;
10 * Get the current user's groups or rights
13 * @return {jQuery.Promise}
15 function getUserInfo() {
16 if ( !userInfoPromise
) {
17 userInfoPromise
= new mw
.Api().getUserInfo();
19 return userInfoPromise
;
22 // mw.user with the properties options and tokens gets defined in mediawiki.js.
26 * Generate a random user session ID.
28 * This information would potentially be stored in a cookie to identify a user during a
29 * session or series of sessions. Its uniqueness should not be depended on unless the
30 * browser supports the crypto API.
32 * Known problems with Math.random():
33 * Using the Math.random function we have seen sets
34 * with 1% of non uniques among 200,000 values with Safari providing most of these.
35 * Given the prevalence of Safari in mobile the percentage of duplicates in
36 * mobile usages of this code is probably higher.
39 * We need about 64 bits to make sure that probability of collision
40 * on 500 million (5*10^8) is <= 1%
41 * See https://en.wikipedia.org/wiki/Birthday_problem#Probability_table
43 * @return {string} 64 bit integer in hex format, padded
45 generateRandomSessionId: function () {
47 hexRnds
= new Array( 2 ),
49 crypto
= window
.crypto
|| window
.msCrypto
;
51 if ( crypto
&& crypto
.getRandomValues
&& typeof Uint32Array
=== 'function' ) {
52 // Fill an array with 2 random values, each of which is 32 bits.
53 // Note that Uint32Array is array-like but does not implement Array.
54 rnds
= new Uint32Array( 2 );
55 crypto
.getRandomValues( rnds
);
58 Math
.floor( Math
.random() * 0x100000000 ),
59 Math
.floor( Math
.random() * 0x100000000 )
62 // Convert number to a string with 16 hex characters
63 for ( i
= 0; i
< 2; i
++ ) {
64 // Add 0x100000000 before converting to hex and strip the extra character
65 // after converting to keep the leading zeros.
66 hexRnds
[ i
] = ( rnds
[ i
] + 0x100000000 ).toString( 16 ).slice( 1 );
69 // Concatenation of two random integers with entropy n and m
70 // returns a string with entropy n+m if those strings are independent
71 return hexRnds
.join( '' );
75 * A sticky generateRandomSessionId for the current JS execution context,
76 * cached within this class.
78 * @return {string} 64 bit integer in hex format, padded
80 stickyRandomId: function () {
81 if ( !stickyRandomSessionId
) {
82 stickyRandomSessionId
= mw
.user
.generateRandomSessionId();
85 return stickyRandomSessionId
;
89 * Get the current user's database id
91 * Not to be confused with #id.
93 * @return {number} Current user's id, or 0 if user is anonymous
96 return mw
.config
.get( 'wgUserId' ) || 0;
100 * Get the current user's name
102 * @return {string|null} User name string or null if user is anonymous
104 getName: function () {
105 return mw
.config
.get( 'wgUserName' );
109 * Get date user registered, if available
111 * @return {boolean|null|Date} False for anonymous users, null if data is
112 * unavailable, or Date for when the user registered.
114 getRegistration: function () {
116 if ( mw
.user
.isAnon() ) {
119 registration
= mw
.config
.get( 'wgUserRegistration' );
120 // Registration may be unavailable if the user signed up before MediaWiki
121 // began tracking this.
122 return !registration
? null : new Date( registration
);
126 * Whether the current user is anonymous
130 isAnon: function () {
131 return mw
.user
.getName() === null;
135 * Get an automatically generated random ID (persisted in sessionStorage)
137 * This ID is ephemeral for everyone, staying in their browser only until they
138 * close their browsing session.
140 * @return {string} Random session ID
142 sessionId: function () {
143 var sessionId
= mw
.storage
.session
.get( 'mwuser-sessionId' );
145 sessionId
= mw
.user
.generateRandomSessionId();
146 mw
.storage
.session
.set( 'mwuser-sessionId', sessionId
);
152 * Get the current user's name or the session ID
154 * Not to be confused with #getId.
156 * @return {string} User name or random session ID
159 return mw
.user
.getName() || mw
.user
.sessionId();
163 * Get the current user's groups
165 * @param {Function} [callback]
166 * @return {jQuery.Promise}
168 getGroups: function ( callback
) {
169 var userGroups
= mw
.config
.get( 'wgUserGroups', [] );
171 // Uses promise for backwards compatibility
172 return $.Deferred().resolve( userGroups
).done( callback
);
176 * Get the current user's rights
178 * @param {Function} [callback]
179 * @return {jQuery.Promise}
181 getRights: function ( callback
) {
182 return getUserInfo().then(
183 function ( userInfo
) { return userInfo
.rights
; },
184 function () { return []; }
189 }( mediaWiki
, jQuery
) );