11 * Get the current user's groups or rights
14 * @param {string} info One of 'groups' or 'rights'
15 * @return {jQuery.Promise}
17 function getUserInfo( info
) {
19 if ( !deferreds
[info
] ) {
21 deferreds
.rights
= $.Deferred();
22 deferreds
.groups
= $.Deferred();
28 uiprop
: 'rights|groups'
29 } ).always( function ( data
) {
31 if ( data
.query
&& data
.query
.userinfo
) {
32 rights
= data
.query
.userinfo
.rights
;
33 groups
= data
.query
.userinfo
.groups
;
35 deferreds
.rights
.resolve( rights
|| [] );
36 deferreds
.groups
.resolve( groups
|| [] );
41 return deferreds
[info
].promise();
44 // Map from numbers 0-255 to a hex string (with padding)
45 for ( i
= 0; i
< 256; i
++ ) {
46 // Padding: Add a full byte (0x100, 256) and strip the extra character
47 byteToHex
[i
] = ( i
+ 256 ).toString( 16 ).slice( 1 );
50 // mw.user with the properties options and tokens gets defined in mediawiki.js.
54 * Generate a random user session ID.
56 * This information would potentially be stored in a cookie to identify a user during a
57 * session or series of sessions. Its uniqueness should not be depended on unless the
58 * browser supports the crypto API.
60 * Known problems with Math.random():
61 * Using the Math.random function we have seen sets
62 * with 1% of non uniques among 200,000 values with Safari providing most of these.
63 * Given the prevalence of Safari in mobile the percentage of duplicates in
64 * mobile usages of this code is probably higher.
67 * We need about 64 bits to make sure that probability of collision
68 * on 500 million (5*10^8) is <= 1%
69 * See https://en.wikipedia.org/wiki/Birthday_problem#Probability_table
71 * @return {string} 64 bit integer in hex format, padded
73 generateRandomSessionId: function () {
74 /*jshint bitwise:false */
76 hexRnds
= new Array( 8 ),
78 crypto
= window
.crypto
|| window
.msCrypto
;
80 // Based on https://github.com/broofa/node-uuid/blob/bfd9f96127/uuid.js
81 if ( crypto
&& crypto
.getRandomValues
) {
82 // Fill an array with 8 random values, each of which is 8 bits.
83 // Note that Uint8Array is array-like but does not implement Array.
84 rnds
= new Uint8Array( 8 );
85 crypto
.getRandomValues( rnds
);
87 rnds
= new Array( 8 );
88 for ( i
= 0; i
< 8; i
++ ) {
89 if ( ( i
& 3 ) === 0 ) {
90 r
= Math
.random() * 0x100000000;
92 rnds
[i
] = r
>>> ( ( i
& 3 ) << 3 ) & 255;
95 // Convert from number to hex
96 for ( i
= 0; i
< 8; i
++ ) {
97 hexRnds
[i
] = byteToHex
[rnds
[i
]];
100 // Concatenation of two random integers with entrophy n and m
101 // returns a string with entrophy n+m if those strings are independent
102 return hexRnds
.join( '' );
106 * Get the current user's database id
108 * Not to be confused with #id.
110 * @return {number} Current user's id, or 0 if user is anonymous
113 return mw
.config
.get( 'wgUserId', 0 );
117 * Get the current user's name
119 * @return {string|null} User name string or null if user is anonymous
121 getName: function () {
122 return mw
.config
.get( 'wgUserName' );
126 * Get date user registered, if available
128 * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
129 * null when data is not available
131 getRegistration: function () {
132 var registration
= mw
.config
.get( 'wgUserRegistration' );
133 if ( mw
.user
.isAnon() ) {
136 if ( registration
=== null ) {
137 // Information may not be available if they signed up before
138 // MW began storing this.
141 return new Date( registration
);
145 * Whether the current user is anonymous
149 isAnon: function () {
150 return mw
.user
.getName() === null;
154 * Get an automatically generated random ID (stored in a session cookie)
156 * This ID is ephemeral for everyone, staying in their browser only until they close
159 * @return {string} Random session ID
161 sessionId: function () {
162 var sessionId
= $.cookie( 'mediaWiki.user.sessionId' );
163 if ( sessionId
=== undefined || sessionId
=== null ) {
164 sessionId
= mw
.user
.generateRandomSessionId();
165 $.cookie( 'mediaWiki.user.sessionId', sessionId
, { expires
: null, path
: '/' } );
171 * Get the current user's name or the session ID
173 * Not to be confused with #getId.
175 * @return {string} User name or random session ID
178 return mw
.user
.getName() || mw
.user
.sessionId();
182 * Get the user's bucket (place them in one if not done already)
184 * mw.user.bucket( 'test', {
185 * buckets: { ignored: 50, control: 25, test: 25 },
190 * @deprecated since 1.23
191 * @param {string} key Name of bucket
192 * @param {Object} options Bucket configuration options
193 * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
194 * must have at least one pair)
195 * @param {number} [options.version=0] Version of bucket test, changing this forces
197 * @param {number} [options.expires=30] Length of time (in days) until the user gets
199 * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
201 bucket: function ( key
, options
) {
202 var cookie
, parts
, version
, bucket
,
203 range
, k
, rand
, total
;
205 options
= $.extend( {
211 cookie
= $.cookie( 'mediaWiki.user.bucket:' + key
);
213 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
214 if ( typeof cookie
=== 'string' && cookie
.length
> 2 && cookie
.indexOf( ':' ) !== -1 ) {
215 parts
= cookie
.split( ':' );
216 if ( parts
.length
> 1 && Number( parts
[0] ) === options
.version
) {
217 version
= Number( parts
[0] );
218 bucket
= String( parts
[1] );
222 if ( bucket
=== undefined ) {
223 if ( !$.isPlainObject( options
.buckets
) ) {
224 throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
227 version
= Number( options
.version
);
231 for ( k
in options
.buckets
) {
232 range
+= options
.buckets
[k
];
235 // Select random value within range
236 rand
= Math
.random() * range
;
238 // Determine which bucket the value landed in
240 for ( k
in options
.buckets
) {
242 total
+= options
.buckets
[k
];
243 if ( total
>= rand
) {
249 'mediaWiki.user.bucket:' + key
,
250 version
+ ':' + bucket
,
251 { path
: '/', expires
: Number( options
.expires
) }
259 * Get the current user's groups
261 * @param {Function} [callback]
262 * @return {jQuery.Promise}
264 getGroups: function ( callback
) {
265 return getUserInfo( 'groups' ).done( callback
);
269 * Get the current user's rights
271 * @param {Function} [callback]
272 * @return {jQuery.Promise}
274 getRights: function ( callback
) {
275 return getUserInfo( 'rights' ).done( callback
);
279 }( mediaWiki
, jQuery
) );