2 * Implementation for mediaWiki.user
10 function User( options, tokens ) {
19 * Gets the current user's groups or rights.
20 * @param {String} info: One of 'groups' or 'rights'.
21 * @param {Function} callback
23 function getUserInfo( info, callback ) {
25 if ( callbacks[info] ) {
26 callbacks[info].add( callback );
29 callbacks.rights = $.Callbacks('once memory');
30 callbacks.groups = $.Callbacks('once memory');
31 callbacks[info].add( callback );
36 uiprop: 'rights|groups'
37 } ).always( function ( data ) {
39 if ( data.query && data.query.userinfo ) {
40 rights = data.query.userinfo.rights;
41 groups = data.query.userinfo.groups;
43 callbacks.rights.fire( rights || [] );
44 callbacks.groups.fire( groups || [] );
50 this.options = options || new mw.Map();
52 this.tokens = tokens || new mw.Map();
57 * Generates a random user session ID (32 alpha-numeric characters).
59 * This information would potentially be stored in a cookie to identify a user during a
60 * session or series of sessions. Its uniqueness should not be depended on.
62 * @return String: Random set of 32 alpha-numeric characters
64 this.generateRandomSessionId = function () {
67 seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
68 for ( i = 0; i < 32; i++ ) {
69 r = Math.floor( Math.random() * seed.length );
70 id += seed.substring( r, r + 1 );
76 * Gets the current user's name.
78 * @return Mixed: User name string or null if users is anonymous
80 this.getName = function () {
81 return mw.config.get( 'wgUserName' );
85 * @deprecated since 1.20 use mw.user.getName() instead
87 this.name = function () {
88 return this.getName();
92 * Get date user registered, if available.
94 * @return {Date|false|null} date user registered, or false for anonymous users, or
95 * null when data is not available
97 this.getRegistration = function () {
98 var registration = mw.config.get( 'wgUserRegistration' );
99 if ( this.isAnon() ) {
101 } else if ( registration === null ) {
102 // Information may not be available if they signed up before
103 // MW began storing this.
106 return new Date( registration );
111 * Checks if the current user is anonymous.
115 this.isAnon = function () {
116 return user.getName() === null;
120 * @deprecated since 1.20 use mw.user.isAnon() instead
122 this.anonymous = function () {
123 return user.isAnon();
127 * Gets a random session ID automatically generated and kept in a cookie.
129 * This ID is ephemeral for everyone, staying in their browser only until they close
132 * @return String: User name or random session ID
134 this.sessionId = function () {
135 var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
136 if ( typeof sessionId === 'undefined' || sessionId === null ) {
137 sessionId = user.generateRandomSessionId();
138 $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': null, 'path': '/' } );
144 * Gets the current user's name or the session ID
146 * @return {string} User name or random session ID
148 this.id = function () {
149 var name = user.getName();
153 return user.sessionId();
157 * Gets the user's bucket, placing them in one at random based on set odds if needed.
159 * @param key String: Name of bucket
160 * @param options Object: Bucket configuration options
161 * @param options.buckets Object: List of bucket-name/relative-probability pairs (required,
162 * must have at least one pair)
163 * @param options.version Number: Version of bucket test, changing this forces rebucketing
164 * (optional, default: 0)
165 * @param options.expires Number: Length of time (in days) until the user gets rebucketed
166 * (optional, default: 30)
167 * @return String: Bucket name - the randomly chosen key of the options.buckets object
170 * mw.user.bucket( 'test', {
171 * 'buckets': { 'ignored': 50, 'control': 25, 'test': 25 },
176 this.bucket = function ( key, options ) {
177 var cookie, parts, version, bucket,
178 range, k, rand, total;
180 options = $.extend( {
186 cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
188 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
189 if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) > 0 ) {
190 parts = cookie.split( ':' );
191 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
192 version = Number( parts[0] );
193 bucket = String( parts[1] );
196 if ( bucket === undefined ) {
197 if ( !$.isPlainObject( options.buckets ) ) {
198 throw 'Invalid buckets error. Object expected for options.buckets.';
200 version = Number( options.version );
203 for ( k in options.buckets ) {
204 range += options.buckets[k];
206 // Select random value within range
207 rand = Math.random() * range;
208 // Determine which bucket the value landed in
210 for ( k in options.buckets ) {
212 total += options.buckets[k];
213 if ( total >= rand ) {
218 'mediaWiki.user.bucket:' + key,
219 version + ':' + bucket,
220 { 'path': '/', 'expires': Number( options.expires ) }
227 * Gets the current user's groups.
229 this.getGroups = function ( callback ) {
230 getUserInfo( 'groups', callback );
234 * Gets the current user's rights.
236 this.getRights = function ( callback ) {
237 getUserInfo( 'rights', callback );
241 // Extend the skeleton mw.user from mediawiki.js
242 // This is kind of ugly but we're stuck with this for b/c reasons
243 mw.user = new User( mw.user.options, mw.user.tokens );
245 }( mediaWiki, jQuery ) );