8 // Extend the skeleton mw.user from mediawiki.js
9 // This is kind of ugly but we're stuck with this for b/c reasons
10 options
= mw
.user
.options
|| new mw
.Map(),
11 tokens
= mw
.user
.tokens
|| new mw
.Map();
14 * Get the current user's groups or rights
17 * @param {string} info One of 'groups' or 'rights'
18 * @param {Function} callback
20 function getUserInfo( info
, callback
) {
22 if ( callbacks
[info
] ) {
23 callbacks
[info
].add( callback
);
26 callbacks
.rights
= $.Callbacks('once memory');
27 callbacks
.groups
= $.Callbacks('once memory');
28 callbacks
[info
].add( callback
);
33 uiprop
: 'rights|groups'
34 } ).always( function ( data
) {
36 if ( data
.query
&& data
.query
.userinfo
) {
37 rights
= data
.query
.userinfo
.rights
;
38 groups
= data
.query
.userinfo
.groups
;
40 callbacks
.rights
.fire( rights
|| [] );
41 callbacks
.groups
.fire( groups
|| [] );
50 * Generate a random user session ID (32 alpha-numeric characters)
52 * This information would potentially be stored in a cookie to identify a user during a
53 * session or series of sessions. Its uniqueness should not be depended on.
55 * @return {string} Random set of 32 alpha-numeric characters
57 generateRandomSessionId: function () {
60 seed
= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
61 for ( i
= 0; i
< 32; i
++ ) {
62 r
= Math
.floor( Math
.random() * seed
.length
);
63 id
+= seed
.substring( r
, r
+ 1 );
69 * Get the current user's database id
71 * Not to be confused with #id.
73 * @return {number} Current user's id, or 0 if user is anonymous
76 return mw
.config
.get( 'wgUserId', 0 );
80 * Get the current user's name
82 * @return {string|null} User name string or null if user is anonymous
84 getName: function () {
85 return mw
.config
.get( 'wgUserName' );
89 * @inheritdoc #getName
90 * @deprecated since 1.20 use #getName instead
93 return user
.getName();
97 * Get date user registered, if available
99 * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
100 * null when data is not available
102 getRegistration: function () {
103 var registration
= mw
.config
.get( 'wgUserRegistration' );
104 if ( user
.isAnon() ) {
106 } else if ( registration
=== null ) {
107 // Information may not be available if they signed up before
108 // MW began storing this.
111 return new Date( registration
);
116 * Whether the current user is anonymous
120 isAnon: function () {
121 return user
.getName() === null;
125 * @inheritdoc #isAnon
126 * @deprecated since 1.20 use #isAnon instead
128 anonymous: function () {
129 return user
.isAnon();
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
= $.cookie( 'mediaWiki.user.sessionId' );
142 if ( sessionId
=== undefined || sessionId
=== null ) {
143 sessionId
= user
.generateRandomSessionId();
144 $.cookie( 'mediaWiki.user.sessionId', sessionId
, { expires
: null, path
: '/' } );
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 user
.getName() || 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 * @param {string} key Name of bucket
170 * @param {Object} options Bucket configuration options
171 * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
172 * must have at least one pair)
173 * @param {number} [options.version=0] Version of bucket test, changing this forces
175 * @param {number} [options.expires=30] Length of time (in days) until the user gets
177 * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
179 bucket: function ( key
, options
) {
180 var cookie
, parts
, version
, bucket
,
181 range
, k
, rand
, total
;
183 options
= $.extend( {
189 cookie
= $.cookie( 'mediaWiki.user.bucket:' + key
);
191 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
192 if ( typeof cookie
=== 'string' && cookie
.length
> 2 && cookie
.indexOf( ':' ) !== -1 ) {
193 parts
= cookie
.split( ':' );
194 if ( parts
.length
> 1 && Number( parts
[0] ) === options
.version
) {
195 version
= Number( parts
[0] );
196 bucket
= String( parts
[1] );
200 if ( bucket
=== undefined ) {
201 if ( !$.isPlainObject( options
.buckets
) ) {
202 throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
205 version
= Number( options
.version
);
209 for ( k
in options
.buckets
) {
210 range
+= options
.buckets
[k
];
213 // Select random value within range
214 rand
= Math
.random() * range
;
216 // Determine which bucket the value landed in
218 for ( k
in options
.buckets
) {
220 total
+= options
.buckets
[k
];
221 if ( total
>= rand
) {
227 'mediaWiki.user.bucket:' + key
,
228 version
+ ':' + bucket
,
229 { path
: '/', expires
: Number( options
.expires
) }
237 * Get the current user's groups
239 * @param {Function} callback
241 getGroups: function ( callback
) {
242 getUserInfo( 'groups', callback
);
246 * Get the current user's rights
248 * @param {Function} callback
250 getRights: function ( callback
) {
251 getUserInfo( 'rights', callback
);
255 }( mediaWiki
, jQuery
) );