6 var callbacks, options, tokens, user;
9 * Gets the current user's groups or rights.
12 * @param {string} info One of 'groups' or 'rights'
13 * @param {Function} callback
15 function getUserInfo( info, callback ) {
17 if ( callbacks[info] ) {
18 callbacks[info].add( callback );
21 callbacks.rights = $.Callbacks('once memory');
22 callbacks.groups = $.Callbacks('once memory');
23 callbacks[info].add( callback );
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 callbacks.rights.fire( rights || [] );
36 callbacks.groups.fire( groups || [] );
42 // Extend the skeleton mw.user from mediawiki.js
43 // This is kind of ugly but we're stuck with this for b/c reasons
44 options = mw.user.options || new mw.Map();
45 tokens = mw.user.tokens || new mw.Map();
52 * Generates a random user session ID (32 alpha-numeric characters).
54 * This information would potentially be stored in a cookie to identify a user during a
55 * session or series of sessions. Its uniqueness should not be depended on.
57 * @return {string} Random set of 32 alpha-numeric characters
59 generateRandomSessionId: function () {
62 seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
63 for ( i = 0; i < 32; i++ ) {
64 r = Math.floor( Math.random() * seed.length );
65 id += seed.substring( r, r + 1 );
71 * Gets the current user's name.
73 * @return {string|null} User name string or null if users is anonymous
75 getName: function () {
76 return mw.config.get( 'wgUserName' );
80 * @inheritdoc #getName
81 * @deprecated since 1.20 use #getName instead
84 return user.getName();
88 * Get date user registered, if available.
90 * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
91 * null when data is not available
93 getRegistration: function () {
94 var registration = mw.config.get( 'wgUserRegistration' );
95 if ( user.isAnon() ) {
97 } else if ( registration === null ) {
98 // Information may not be available if they signed up before
99 // MW began storing this.
102 return new Date( registration );
107 * Checks if the current user is anonymous.
111 isAnon: function () {
112 return user.getName() === null;
116 * @inheritdoc #isAnon
117 * @deprecated since 1.20 use #isAnon instead
119 anonymous: function () {
120 return user.isAnon();
124 * Gets a random ID automatically generated and stored in a session cookie.
126 * This ID is ephemeral for everyone, staying in their browser only until they close
129 * @return {string} Random session ID
131 sessionId: function () {
132 var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
133 if ( typeof sessionId === 'undefined' || sessionId === null ) {
134 sessionId = user.generateRandomSessionId();
135 $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': null, 'path': '/' } );
141 * Gets the current user's name or the session ID
143 * @return {string} User name or random session ID
146 var name = user.getName();
150 return user.sessionId();
154 * Gets the user's bucket, placing them in one at random based on set odds if needed.
156 * mw.user.bucket( 'test', {
157 * 'buckets': { 'ignored': 50, 'control': 25, 'test': 25 },
162 * @param {string} key Name of bucket
163 * @param {Object} options Bucket configuration options
164 * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
165 * must have at least one pair)
166 * @param {number} options.version Version of bucket test, changing this forces rebucketing
167 * (optional, default: 0)
168 * @param {number} options.expires Length of time (in days) until the user gets rebucketed
169 * (optional, default: 30)
170 * @return {string} Bucket name - the randomly chosen key of the options.buckets object
172 bucket: function ( key, options ) {
173 var cookie, parts, version, bucket,
174 range, k, rand, total;
176 options = $.extend( {
182 cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
184 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
185 if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) > 0 ) {
186 parts = cookie.split( ':' );
187 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
188 version = Number( parts[0] );
189 bucket = String( parts[1] );
192 if ( bucket === undefined ) {
193 if ( !$.isPlainObject( options.buckets ) ) {
194 throw 'Invalid buckets error. Object expected for options.buckets.';
196 version = Number( options.version );
199 for ( k in options.buckets ) {
200 range += options.buckets[k];
202 // Select random value within range
203 rand = Math.random() * range;
204 // Determine which bucket the value landed in
206 for ( k in options.buckets ) {
208 total += options.buckets[k];
209 if ( total >= rand ) {
214 'mediaWiki.user.bucket:' + key,
215 version + ':' + bucket,
216 { 'path': '/', 'expires': Number( options.expires ) }
223 * Gets the current user's groups.
225 * @param {Function} callback
227 getGroups: function ( callback ) {
228 getUserInfo( 'groups', callback );
232 * Gets the current user's rights.
234 * @param {Function} callback
236 getRights: function ( callback ) {
237 getUserInfo( 'rights', callback );
241 }( mediaWiki, jQuery ) );