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 function generateId() {
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 * Checks if the current user is anonymous.
96 this.isAnon = function () {
97 return user.getName() === null;
101 * @deprecated since 1.20 use mw.user.isAnon() instead
103 this.anonymous = function () {
104 return user.isAnon();
108 * Gets a random session ID automatically generated and kept in a cookie.
110 * This ID is ephemeral for everyone, staying in their browser only until they close
113 * @return String: User name or random session ID
115 this.sessionId = function () {
116 var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
117 if ( typeof sessionId === 'undefined' || sessionId === null ) {
118 sessionId = generateId();
119 $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': null, 'path': '/' } );
125 * Gets the current user's name or a random ID automatically generated and kept in a cookie.
127 * This ID is persistent for anonymous users, staying in their browser up to 1 year. The
128 * expiration time is reset each time the ID is queried, so in most cases this ID will
129 * persist until the browser's cookies are cleared or the user doesn't visit for 1 year.
131 * @return String: User name or random session ID
133 this.id = function() {
135 name = user.getName();
139 id = $.cookie( 'mediaWiki.user.id' );
140 if ( typeof id === 'undefined' || id === null ) {
143 // Set cookie if not set, or renew it if already set
144 $.cookie( 'mediaWiki.user.id', id, {
152 * Gets the user's bucket, placing them in one at random based on set odds if needed.
154 * @param key String: Name of bucket
155 * @param options Object: Bucket configuration options
156 * @param options.buckets Object: List of bucket-name/relative-probability pairs (required,
157 * must have at least one pair)
158 * @param options.version Number: Version of bucket test, changing this forces rebucketing
159 * (optional, default: 0)
160 * @param options.tracked Boolean: Track the event of bucketing through the API module of
161 * the ClickTracking extension (optional, default: false)
162 * @param options.expires Number: Length of time (in days) until the user gets rebucketed
163 * (optional, default: 30)
164 * @return String: Bucket name - the randomly chosen key of the options.buckets object
167 * mw.user.bucket( 'test', {
168 * 'buckets': { 'ignored': 50, 'control': 25, 'test': 25 },
174 this.bucket = function ( key, options ) {
175 var cookie, parts, version, bucket,
176 range, k, rand, total;
178 options = $.extend( {
185 cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
187 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
188 if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) > 0 ) {
189 parts = cookie.split( ':' );
190 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
191 version = Number( parts[0] );
192 bucket = String( parts[1] );
195 if ( bucket === undefined ) {
196 if ( !$.isPlainObject( options.buckets ) ) {
197 throw 'Invalid buckets error. Object expected for options.buckets.';
199 version = Number( options.version );
202 for ( k in options.buckets ) {
203 range += options.buckets[k];
205 // Select random value within range
206 rand = Math.random() * range;
207 // Determine which bucket the value landed in
209 for ( k in options.buckets ) {
211 total += options.buckets[k];
212 if ( total >= rand ) {
216 if ( options.tracked ) {
217 mw.loader.using( 'jquery.clickTracking', function () {
219 'mediaWiki.user.bucket:' + key + '@' + version + ':' + bucket
224 'mediaWiki.user.bucket:' + key,
225 version + ':' + bucket,
226 { 'path': '/', 'expires': Number( options.expires ) }
233 * Gets the current user's groups.
235 this.getGroups = function ( callback ) {
236 getUserInfo( 'groups', callback );
240 * Gets the current user's rights.
242 this.getRights = function ( callback ) {
243 getUserInfo( 'rights', callback );
247 // Extend the skeleton mw.user from mediawiki.js
248 // This is kind of ugly but we're stuck with this for b/c reasons
249 mw.user = new User( mw.user.options, mw.user.tokens );
251 }( mediaWiki, jQuery ) );