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 * @return {jQuery.Promise}
20 function getUserInfo( info ) {
22 if ( !deferreds[info] ) {
24 deferreds.rights = $.Deferred();
25 deferreds.groups = $.Deferred();
31 uiprop: 'rights|groups'
32 } ).always( function ( data ) {
34 if ( data.query && data.query.userinfo ) {
35 rights = data.query.userinfo.rights;
36 groups = data.query.userinfo.groups;
38 deferreds.rights.resolve( rights || [] );
39 deferreds.groups.resolve( groups || [] );
44 return deferreds[info].promise();
52 * Generate 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.charAt( r );
71 * Get the current user's database id
73 * Not to be confused with #id.
75 * @return {number} Current user's id, or 0 if user is anonymous
78 return mw.config.get( 'wgUserId', 0 );
82 * Get the current user's name
84 * @return {string|null} User name string or null if user is anonymous
86 getName: function () {
87 return mw.config.get( 'wgUserName' );
91 * Get date user registered, if available
93 * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
94 * null when data is not available
96 getRegistration: function () {
97 var registration = mw.config.get( 'wgUserRegistration' );
98 if ( user.isAnon() ) {
100 } else if ( registration === null ) {
101 // Information may not be available if they signed up before
102 // MW began storing this.
105 return new Date( registration );
110 * Whether the current user is anonymous
114 isAnon: function () {
115 return user.getName() === null;
119 * Get an automatically generated random ID (stored in a session cookie)
121 * This ID is ephemeral for everyone, staying in their browser only until they close
124 * @return {string} Random session ID
126 sessionId: function () {
127 var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
128 if ( sessionId === undefined || sessionId === null ) {
129 sessionId = user.generateRandomSessionId();
130 $.cookie( 'mediaWiki.user.sessionId', sessionId, { expires: null, path: '/' } );
136 * Get the current user's name or the session ID
138 * Not to be confused with #getId.
140 * @return {string} User name or random session ID
143 return user.getName() || user.sessionId();
147 * Get the user's bucket (place them in one if not done already)
149 * mw.user.bucket( 'test', {
150 * buckets: { ignored: 50, control: 25, test: 25 },
155 * @deprecated since 1.23
156 * @param {string} key Name of bucket
157 * @param {Object} options Bucket configuration options
158 * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
159 * must have at least one pair)
160 * @param {number} [options.version=0] Version of bucket test, changing this forces
162 * @param {number} [options.expires=30] Length of time (in days) until the user gets
164 * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
166 bucket: function ( key, options ) {
167 var cookie, parts, version, bucket,
168 range, k, rand, total;
170 options = $.extend( {
176 cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
178 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
179 if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) !== -1 ) {
180 parts = cookie.split( ':' );
181 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
182 version = Number( parts[0] );
183 bucket = String( parts[1] );
187 if ( bucket === undefined ) {
188 if ( !$.isPlainObject( options.buckets ) ) {
189 throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
192 version = Number( options.version );
196 for ( k in options.buckets ) {
197 range += options.buckets[k];
200 // Select random value within range
201 rand = Math.random() * range;
203 // Determine which bucket the value landed in
205 for ( k in options.buckets ) {
207 total += options.buckets[k];
208 if ( total >= rand ) {
214 'mediaWiki.user.bucket:' + key,
215 version + ':' + bucket,
216 { path: '/', expires: Number( options.expires ) }
224 * Get the current user's groups
226 * @param {Function} [callback]
227 * @return {jQuery.Promise}
229 getGroups: function ( callback ) {
230 return getUserInfo( 'groups' ).done( callback );
234 * Get the current user's rights
236 * @param {Function} [callback]
237 * @return {jQuery.Promise}
239 getRights: function ( callback ) {
240 return getUserInfo( 'rights' ).done( callback );
244 }( mediaWiki, jQuery ) );