Merge "Remove convertPlural methods already served by CLDR plural system"
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.user.js
1 /**
2 * @class mw.user
3 * @singleton
4 */
5 ( function ( mw, $ ) {
6 var user,
7 callbacks = {},
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();
12
13 /**
14 * Get the current user's groups or rights
15 *
16 * @private
17 * @param {string} info One of 'groups' or 'rights'
18 * @param {Function} callback
19 */
20 function getUserInfo( info, callback ) {
21 var api;
22 if ( callbacks[info] ) {
23 callbacks[info].add( callback );
24 return;
25 }
26 callbacks.rights = $.Callbacks('once memory');
27 callbacks.groups = $.Callbacks('once memory');
28 callbacks[info].add( callback );
29 api = new mw.Api();
30 api.get( {
31 action: 'query',
32 meta: 'userinfo',
33 uiprop: 'rights|groups'
34 } ).always( function ( data ) {
35 var rights, groups;
36 if ( data.query && data.query.userinfo ) {
37 rights = data.query.userinfo.rights;
38 groups = data.query.userinfo.groups;
39 }
40 callbacks.rights.fire( rights || [] );
41 callbacks.groups.fire( groups || [] );
42 } );
43 }
44
45 mw.user = user = {
46 options: options,
47 tokens: tokens,
48
49 /**
50 * Generate a random user session ID (32 alpha-numeric characters)
51 *
52 * This information would potentially be stored in a cookie to identify a user during a
53 * session or series of sessions. Its uniqueness should not be depended on.
54 *
55 * @return {string} Random set of 32 alpha-numeric characters
56 */
57 generateRandomSessionId: function () {
58 var i, r,
59 id = '',
60 seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
61 for ( i = 0; i < 32; i++ ) {
62 r = Math.floor( Math.random() * seed.length );
63 id += seed.substring( r, r + 1 );
64 }
65 return id;
66 },
67
68 /**
69 * Get the current user's database id
70 *
71 * Not to be confused with #id.
72 *
73 * @return {number} Current user's id, or 0 if user is anonymous
74 */
75 getId: function () {
76 return mw.config.get( 'wgUserId', 0 );
77 },
78
79 /**
80 * Get the current user's name
81 *
82 * @return {string|null} User name string or null if user is anonymous
83 */
84 getName: function () {
85 return mw.config.get( 'wgUserName' );
86 },
87
88 /**
89 * @inheritdoc #getName
90 * @deprecated since 1.20 use #getName instead
91 */
92 name: function () {
93 return user.getName();
94 },
95
96 /**
97 * Get date user registered, if available
98 *
99 * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
100 * null when data is not available
101 */
102 getRegistration: function () {
103 var registration = mw.config.get( 'wgUserRegistration' );
104 if ( user.isAnon() ) {
105 return false;
106 } else if ( registration === null ) {
107 // Information may not be available if they signed up before
108 // MW began storing this.
109 return null;
110 } else {
111 return new Date( registration );
112 }
113 },
114
115 /**
116 * Whether the current user is anonymous
117 *
118 * @return {boolean}
119 */
120 isAnon: function () {
121 return user.getName() === null;
122 },
123
124 /**
125 * @inheritdoc #isAnon
126 * @deprecated since 1.20 use #isAnon instead
127 */
128 anonymous: function () {
129 return user.isAnon();
130 },
131
132 /**
133 * Get an automatically generated random ID (stored in a session cookie)
134 *
135 * This ID is ephemeral for everyone, staying in their browser only until they close
136 * their browser.
137 *
138 * @return {string} Random session ID
139 */
140 sessionId: function () {
141 var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
142 if ( sessionId === undefined || sessionId === null ) {
143 sessionId = user.generateRandomSessionId();
144 $.cookie( 'mediaWiki.user.sessionId', sessionId, { expires: null, path: '/' } );
145 }
146 return sessionId;
147 },
148
149 /**
150 * Get the current user's name or the session ID
151 *
152 * Not to be confused with #getId.
153 *
154 * @return {string} User name or random session ID
155 */
156 id: function () {
157 return user.getName() || user.sessionId();
158 },
159
160 /**
161 * Get the user's bucket (place them in one if not done already)
162 *
163 * mw.user.bucket( 'test', {
164 * buckets: { ignored: 50, control: 25, test: 25 },
165 * version: 1,
166 * expires: 7
167 * } );
168 *
169 * @param {string} key Name of bucket
170 * @param {Object} options Bucket configuration options
171 * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
172 * must have at least one pair)
173 * @param {number} [options.version=0] Version of bucket test, changing this forces
174 * rebucketing
175 * @param {number} [options.expires=30] Length of time (in days) until the user gets
176 * rebucketed
177 * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
178 */
179 bucket: function ( key, options ) {
180 var cookie, parts, version, bucket,
181 range, k, rand, total;
182
183 options = $.extend( {
184 buckets: {},
185 version: 0,
186 expires: 30
187 }, options || {} );
188
189 cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
190
191 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
192 if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) !== -1 ) {
193 parts = cookie.split( ':' );
194 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
195 version = Number( parts[0] );
196 bucket = String( parts[1] );
197 }
198 }
199
200 if ( bucket === undefined ) {
201 if ( !$.isPlainObject( options.buckets ) ) {
202 throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
203 }
204
205 version = Number( options.version );
206
207 // Find range
208 range = 0;
209 for ( k in options.buckets ) {
210 range += options.buckets[k];
211 }
212
213 // Select random value within range
214 rand = Math.random() * range;
215
216 // Determine which bucket the value landed in
217 total = 0;
218 for ( k in options.buckets ) {
219 bucket = k;
220 total += options.buckets[k];
221 if ( total >= rand ) {
222 break;
223 }
224 }
225
226 $.cookie(
227 'mediaWiki.user.bucket:' + key,
228 version + ':' + bucket,
229 { path: '/', expires: Number( options.expires ) }
230 );
231 }
232
233 return bucket;
234 },
235
236 /**
237 * Get the current user's groups
238 *
239 * @param {Function} callback
240 */
241 getGroups: function ( callback ) {
242 getUserInfo( 'groups', callback );
243 },
244
245 /**
246 * Get the current user's rights
247 *
248 * @param {Function} callback
249 */
250 getRights: function ( callback ) {
251 getUserInfo( 'rights', callback );
252 }
253 };
254
255 }( mediaWiki, jQuery ) );