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]
19 * @return {jQuery.Promise}
21 function getUserInfo( info, callback ) {
23 if ( !deferreds[info] ) {
25 deferreds.rights = $.Deferred();
26 deferreds.groups = $.Deferred();
32 uiprop: 'rights|groups'
33 } ).always( function ( data ) {
35 if ( data.query && data.query.userinfo ) {
36 rights = data.query.userinfo.rights;
37 groups = data.query.userinfo.groups;
39 deferreds.rights.resolve( rights || [] );
40 deferreds.groups.resolve( groups || [] );
45 return deferreds[info].done( callback ).promise();
53 * Generate a random user session ID (32 alpha-numeric characters)
55 * This information would potentially be stored in a cookie to identify a user during a
56 * session or series of sessions. Its uniqueness should not be depended on.
58 * @return {string} Random set of 32 alpha-numeric characters
60 generateRandomSessionId: function () {
63 seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
64 for ( i = 0; i < 32; i++ ) {
65 r = Math.floor( Math.random() * seed.length );
66 id += seed.substring( r, r + 1 );
72 * Get the current user's database id
74 * Not to be confused with #id.
76 * @return {number} Current user's id, or 0 if user is anonymous
79 return mw.config.get( 'wgUserId', 0 );
83 * Get the current user's name
85 * @return {string|null} User name string or null if user is anonymous
87 getName: function () {
88 return mw.config.get( 'wgUserName' );
92 * @inheritdoc #getName
93 * @deprecated since 1.20 use #getName instead
96 return user.getName();
100 * Get date user registered, if available
102 * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
103 * null when data is not available
105 getRegistration: function () {
106 var registration = mw.config.get( 'wgUserRegistration' );
107 if ( user.isAnon() ) {
109 } else if ( registration === null ) {
110 // Information may not be available if they signed up before
111 // MW began storing this.
114 return new Date( registration );
119 * Whether the current user is anonymous
123 isAnon: function () {
124 return user.getName() === null;
128 * @inheritdoc #isAnon
129 * @deprecated since 1.20 use #isAnon instead
131 anonymous: function () {
132 return user.isAnon();
136 * Get an automatically generated random ID (stored in a session cookie)
138 * This ID is ephemeral for everyone, staying in their browser only until they close
141 * @return {string} Random session ID
143 sessionId: function () {
144 var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
145 if ( sessionId === undefined || sessionId === null ) {
146 sessionId = user.generateRandomSessionId();
147 $.cookie( 'mediaWiki.user.sessionId', sessionId, { expires: null, path: '/' } );
153 * Get the current user's name or the session ID
155 * Not to be confused with #getId.
157 * @return {string} User name or random session ID
160 return user.getName() || user.sessionId();
164 * Get the user's bucket (place them in one if not done already)
166 * mw.user.bucket( 'test', {
167 * buckets: { ignored: 50, control: 25, test: 25 },
172 * @param {string} key Name of bucket
173 * @param {Object} options Bucket configuration options
174 * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
175 * must have at least one pair)
176 * @param {number} [options.version=0] Version of bucket test, changing this forces
178 * @param {number} [options.expires=30] Length of time (in days) until the user gets
180 * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
182 bucket: function ( key, options ) {
183 var cookie, parts, version, bucket,
184 range, k, rand, total;
186 options = $.extend( {
192 cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
194 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
195 if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) !== -1 ) {
196 parts = cookie.split( ':' );
197 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
198 version = Number( parts[0] );
199 bucket = String( parts[1] );
203 if ( bucket === undefined ) {
204 if ( !$.isPlainObject( options.buckets ) ) {
205 throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
208 version = Number( options.version );
212 for ( k in options.buckets ) {
213 range += options.buckets[k];
216 // Select random value within range
217 rand = Math.random() * range;
219 // Determine which bucket the value landed in
221 for ( k in options.buckets ) {
223 total += options.buckets[k];
224 if ( total >= rand ) {
230 'mediaWiki.user.bucket:' + key,
231 version + ':' + bucket,
232 { path: '/', expires: Number( options.expires ) }
240 * Get the current user's groups
242 * @param {Function} [callback]
243 * @return {jQuery.Promise}
245 getGroups: function ( callback ) {
246 return getUserInfo( 'groups', callback );
250 * Get the current user's rights
252 * @param {Function} [callback]
253 * @return {jQuery.Promise}
255 getRights: function ( callback ) {
256 return getUserInfo( 'rights', callback );
260 }( mediaWiki, jQuery ) );