b5f124ba60bd6b320135e3f56ca980dee86a91bc
2 * Implementation for mediaWiki.user
10 function User( options
, tokens
) {
18 * Gets the current user's groups or rights.
19 * @param {String} info: One of 'groups' or 'rights'.
20 * @param {Function} callback
22 function getUserInfo( info
, callback
) {
24 if ( callbacks
[info
] ) {
25 callbacks
[info
].add( callback
);
28 callbacks
.rights
= $.Callbacks('once memory');
29 callbacks
.groups
= $.Callbacks('once memory');
30 callbacks
[info
].add( callback
);
35 uiprop
: 'rights|groups'
36 } ).always( function ( data
) {
38 if ( data
.query
&& data
.query
.userinfo
) {
39 rights
= data
.query
.userinfo
.rights
;
40 groups
= data
.query
.userinfo
.groups
;
42 callbacks
.rights
.fire( rights
|| [] );
43 callbacks
.groups
.fire( groups
|| [] );
49 this.options
= options
|| new mw
.Map();
51 this.tokens
= tokens
|| new mw
.Map();
56 * Generates a random user session ID (32 alpha-numeric characters).
58 * This information would potentially be stored in a cookie to identify a user during a
59 * session or series of sessions. Its uniqueness should not be depended on.
61 * @return String: Random set of 32 alpha-numeric characters
63 function generateId() {
65 var seed
= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
66 for ( var i
= 0, r
; i
< 32; i
++ ) {
67 r
= Math
.floor( Math
.random() * seed
.length
);
68 id
+= seed
.substring( r
, r
+ 1 );
74 * Gets the current user's name.
76 * @return Mixed: User name string or null if users is anonymous
78 this.getName = function () {
79 return mw
.config
.get( 'wgUserName' );
83 * @deprecated since 1.20 use mw.user.getName() instead
85 this.name = function () {
86 return this.getName();
90 * Checks if the current user is anonymous.
94 this.isAnon = function () {
95 return that
.getName() === null;
99 * @deprecated since 1.20 use mw.user.isAnon() instead
101 this.anonymous = function () {
102 return that
.isAnon();
106 * Gets a random session ID automatically generated and kept in a cookie.
108 * This ID is ephemeral for everyone, staying in their browser only until they close
111 * @return String: User name or random session ID
113 this.sessionId = function () {
114 var sessionId
= $.cookie( 'mediaWiki.user.sessionId' );
115 if ( typeof sessionId
=== 'undefined' || sessionId
=== null ) {
116 sessionId
= generateId();
117 $.cookie( 'mediaWiki.user.sessionId', sessionId
, { 'expires': null, 'path': '/' } );
123 * Gets the current user's name or a random ID automatically generated and kept in a cookie.
125 * This ID is persistent for anonymous users, staying in their browser up to 1 year. The
126 * expiration time is reset each time the ID is queried, so in most cases this ID will
127 * persist until the browser's cookies are cleared or the user doesn't visit for 1 year.
129 * @return String: User name or random session ID
131 this.id = function() {
132 var name
= that
.getName();
136 var id
= $.cookie( 'mediaWiki.user.id' );
137 if ( typeof id
=== 'undefined' || id
=== null ) {
140 // Set cookie if not set, or renew it if already set
141 $.cookie( 'mediaWiki.user.id', id
, {
149 * Gets the user's bucket, placing them in one at random based on set odds if needed.
151 * @param key String: Name of bucket
152 * @param options Object: Bucket configuration options
153 * @param options.buckets Object: List of bucket-name/relative-probability pairs (required,
154 * must have at least one pair)
155 * @param options.version Number: Version of bucket test, changing this forces rebucketing
156 * (optional, default: 0)
157 * @param options.tracked Boolean: Track the event of bucketing through the API module of
158 * the ClickTracking extension (optional, default: false)
159 * @param options.expires Number: Length of time (in days) until the user gets rebucketed
160 * (optional, default: 30)
161 * @return String: Bucket name - the randomly chosen key of the options.buckets object
164 * mw.user.bucket( 'test', {
165 * 'buckets': { 'ignored': 50, 'control': 25, 'test': 25 },
171 this.bucket = function ( key
, options
) {
172 options
= $.extend( {
178 var cookie
= $.cookie( 'mediaWiki.user.bucket:' + key
);
181 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
182 if ( typeof cookie
=== 'string' && cookie
.length
> 2 && cookie
.indexOf( ':' ) > 0 ) {
183 var parts
= cookie
.split( ':' );
184 if ( parts
.length
> 1 && Number( parts
[0] ) === options
.version
) {
185 version
= Number( parts
[0] );
186 bucket
= String( parts
[1] );
189 if ( bucket
=== null ) {
190 if ( !$.isPlainObject( options
.buckets
) ) {
191 throw 'Invalid buckets error. Object expected for options.buckets.';
193 version
= Number( options
.version
);
196 for ( k
in options
.buckets
) {
197 range
+= options
.buckets
[k
];
199 // Select random value within range
200 var rand
= Math
.random() * range
;
201 // Determine which bucket the value landed in
203 for ( k
in options
.buckets
) {
205 total
+= options
.buckets
[k
];
206 if ( total
>= rand
) {
210 if ( options
.tracked
) {
211 mw
.loader
.using( 'jquery.clickTracking', function () {
213 'mediaWiki.user.bucket:' + key
+ '@' + version
+ ':' + bucket
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
) );