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 a random ID automatically generated and kept in a cookie.
146 * This ID is persistent for anonymous users, staying in their browser up to 1 year. The
147 * expiration time is reset each time the ID is queried, so in most cases this ID will
148 * persist until the browser's cookies are cleared or the user doesn't visit for 1 year.
150 * @return {string} User name or random session ID
152 this.id = function () {
154 name
= user
.getName();
158 id
= $.cookie( 'mediaWiki.user.id' );
159 if ( typeof id
=== 'undefined' || id
=== null ) {
160 id
= user
.generateRandomSessionId();
162 // Set cookie if not set, or renew it if already set
163 $.cookie( 'mediaWiki.user.id', id
, {
171 * Gets the user's bucket, placing them in one at random based on set odds if needed.
173 * @param key String: Name of bucket
174 * @param options Object: Bucket configuration options
175 * @param options.buckets Object: List of bucket-name/relative-probability pairs (required,
176 * must have at least one pair)
177 * @param options.version Number: Version of bucket test, changing this forces rebucketing
178 * (optional, default: 0)
179 * @param options.expires Number: Length of time (in days) until the user gets rebucketed
180 * (optional, default: 30)
181 * @return String: Bucket name - the randomly chosen key of the options.buckets object
184 * mw.user.bucket( 'test', {
185 * 'buckets': { 'ignored': 50, 'control': 25, 'test': 25 },
190 this.bucket = function ( key
, options
) {
191 var cookie
, parts
, version
, bucket
,
192 range
, k
, rand
, total
;
194 options
= $.extend( {
200 cookie
= $.cookie( 'mediaWiki.user.bucket:' + key
);
202 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
203 if ( typeof cookie
=== 'string' && cookie
.length
> 2 && cookie
.indexOf( ':' ) > 0 ) {
204 parts
= cookie
.split( ':' );
205 if ( parts
.length
> 1 && Number( parts
[0] ) === options
.version
) {
206 version
= Number( parts
[0] );
207 bucket
= String( parts
[1] );
210 if ( bucket
=== undefined ) {
211 if ( !$.isPlainObject( options
.buckets
) ) {
212 throw 'Invalid buckets error. Object expected for options.buckets.';
214 version
= Number( options
.version
);
217 for ( k
in options
.buckets
) {
218 range
+= options
.buckets
[k
];
220 // Select random value within range
221 rand
= Math
.random() * range
;
222 // Determine which bucket the value landed in
224 for ( k
in options
.buckets
) {
226 total
+= options
.buckets
[k
];
227 if ( total
>= rand
) {
232 'mediaWiki.user.bucket:' + key
,
233 version
+ ':' + bucket
,
234 { 'path': '/', 'expires': Number( options
.expires
) }
241 * Gets the current user's groups.
243 this.getGroups = function ( callback
) {
244 getUserInfo( 'groups', callback
);
248 * Gets the current user's rights.
250 this.getRights = function ( callback
) {
251 getUserInfo( 'rights', callback
);
255 // Extend the skeleton mw.user from mediawiki.js
256 // This is kind of ugly but we're stuck with this for b/c reasons
257 mw
.user
= new User( mw
.user
.options
, mw
.user
.tokens
);
259 }( mediaWiki
, jQuery
) );