11 * Get the current user's groups or rights
14 * @return {jQuery.Promise}
16 function getUserInfo() {
17 if ( !userInfoPromise
) {
18 userInfoPromise
= new mw
.Api().getUserInfo();
20 return userInfoPromise
;
23 // Map from numbers 0-255 to a hex string (with padding)
24 for ( i
= 0; i
< 256; i
++ ) {
25 // Padding: Add a full byte (0x100, 256) and strip the extra character
26 byteToHex
[ i
] = ( i
+ 256 ).toString( 16 ).slice( 1 );
29 // mw.user with the properties options and tokens gets defined in mediawiki.js.
33 * Generate a random user session ID.
35 * This information would potentially be stored in a cookie to identify a user during a
36 * session or series of sessions. Its uniqueness should not be depended on unless the
37 * browser supports the crypto API.
39 * Known problems with Math.random():
40 * Using the Math.random function we have seen sets
41 * with 1% of non uniques among 200,000 values with Safari providing most of these.
42 * Given the prevalence of Safari in mobile the percentage of duplicates in
43 * mobile usages of this code is probably higher.
46 * We need about 64 bits to make sure that probability of collision
47 * on 500 million (5*10^8) is <= 1%
48 * See https://en.wikipedia.org/wiki/Birthday_problem#Probability_table
50 * @return {string} 64 bit integer in hex format, padded
52 generateRandomSessionId: function () {
53 /*jshint bitwise:false */
55 hexRnds
= new Array( 8 ),
57 crypto
= window
.crypto
|| window
.msCrypto
;
59 // Based on https://github.com/broofa/node-uuid/blob/bfd9f96127/uuid.js
60 if ( crypto
&& crypto
.getRandomValues
) {
61 // Fill an array with 8 random values, each of which is 8 bits.
62 // Note that Uint8Array is array-like but does not implement Array.
63 rnds
= new Uint8Array( 8 );
64 crypto
.getRandomValues( rnds
);
66 rnds
= new Array( 8 );
67 for ( i
= 0; i
< 8; i
++ ) {
68 if ( ( i
& 3 ) === 0 ) {
69 r
= Math
.random() * 0x100000000;
71 rnds
[ i
] = r
>>> ( ( i
& 3 ) << 3 ) & 255;
74 // Convert from number to hex
75 for ( i
= 0; i
< 8; i
++ ) {
76 hexRnds
[ i
] = byteToHex
[ rnds
[ i
] ];
79 // Concatenation of two random integers with entrophy n and m
80 // returns a string with entrophy n+m if those strings are independent
81 return hexRnds
.join( '' );
85 * Get the current user's database id
87 * Not to be confused with #id.
89 * @return {number} Current user's id, or 0 if user is anonymous
92 return mw
.config
.get( 'wgUserId', 0 );
96 * Get the current user's name
98 * @return {string|null} User name string or null if user is anonymous
100 getName: function () {
101 return mw
.config
.get( 'wgUserName' );
105 * Get date user registered, if available
107 * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
108 * null when data is not available
110 getRegistration: function () {
111 var registration
= mw
.config
.get( 'wgUserRegistration' );
112 if ( mw
.user
.isAnon() ) {
115 if ( registration
=== null ) {
116 // Information may not be available if they signed up before
117 // MW began storing this.
120 return new Date( registration
);
124 * Whether the current user is anonymous
128 isAnon: function () {
129 return mw
.user
.getName() === null;
133 * Get an automatically generated random ID (stored in a session cookie)
135 * This ID is ephemeral for everyone, staying in their browser only until they close
138 * @return {string} Random session ID
140 sessionId: function () {
141 var sessionId
= mw
.cookie
.get( 'mwuser-sessionId' );
142 if ( sessionId
=== null ) {
143 sessionId
= mw
.user
.generateRandomSessionId();
144 mw
.cookie
.set( 'mwuser-sessionId', sessionId
, { expires
: null } );
150 * Get the current user's name or the session ID
152 * Not to be confused with #getId.
154 * @return {string} User name or random session ID
157 return mw
.user
.getName() || mw
.user
.sessionId();
161 * Get the user's bucket (place them in one if not done already)
163 * mw.user.bucket( 'test', {
164 * buckets: { ignored: 50, control: 25, test: 25 },
169 * @deprecated since 1.23
170 * @param {string} key Name of bucket
171 * @param {Object} options Bucket configuration options
172 * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
173 * must have at least one pair)
174 * @param {number} [options.version=0] Version of bucket test, changing this forces
176 * @param {number} [options.expires=30] Length of time (in days) until the user gets
178 * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
180 bucket: function ( key
, options
) {
181 var cookie
, parts
, version
, bucket
,
182 range
, k
, rand
, total
;
184 options
= $.extend( {
190 cookie
= mw
.cookie
.get( 'mwuser-bucket:' + key
);
192 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
193 if ( typeof cookie
=== 'string' && cookie
.length
> 2 && cookie
.indexOf( ':' ) !== -1 ) {
194 parts
= cookie
.split( ':' );
195 if ( parts
.length
> 1 && Number( parts
[ 0 ] ) === options
.version
) {
196 version
= Number( parts
[ 0 ] );
197 bucket
= String( parts
[ 1 ] );
201 if ( bucket
=== undefined ) {
202 if ( !$.isPlainObject( options
.buckets
) ) {
203 throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
206 version
= Number( options
.version
);
210 for ( k
in options
.buckets
) {
211 range
+= options
.buckets
[ k
];
214 // Select random value within range
215 rand
= Math
.random() * range
;
217 // Determine which bucket the value landed in
219 for ( k
in options
.buckets
) {
221 total
+= options
.buckets
[ k
];
222 if ( total
>= rand
) {
228 'mwuser-bucket:' + key
,
229 version
+ ':' + bucket
,
230 { expires
: Number( options
.expires
) * 86400 }
238 * Get the current user's groups
240 * @param {Function} [callback]
241 * @return {jQuery.Promise}
243 getGroups: function ( callback
) {
244 return getUserInfo().then(
245 function ( userInfo
) { return userInfo
.groups
; },
246 function () { return []; }
251 * Get the current user's rights
253 * @param {Function} [callback]
254 * @return {jQuery.Promise}
256 getRights: function ( callback
) {
257 return getUserInfo().then(
258 function ( userInfo
) { return userInfo
.rights
; },
259 function () { return []; }
264 }( mediaWiki
, jQuery
) );