content: Use Language::factory( 'en' ) instead of wfGetLangObj( 'en' )
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.user.js
1 /**
2 * @class mw.user
3 * @singleton
4 */
5 ( function ( mw, $ ) {
6 var i,
7 userInfoPromise,
8 byteToHex = [];
9
10 /**
11 * Get the current user's groups or rights
12 *
13 * @private
14 * @return {jQuery.Promise}
15 */
16 function getUserInfo() {
17 if ( !userInfoPromise ) {
18 userInfoPromise = new mw.Api().getUserInfo();
19 }
20 return userInfoPromise;
21 }
22
23 // Map from numbers 0-255 to a hex string (with padding)
24 for ( i = 0; i < 256; i++ ) {
25 // Padding: Add a full byte (0x100, 256) and strip the extra character
26 byteToHex[ i ] = ( i + 256 ).toString( 16 ).slice( 1 );
27 }
28
29 // mw.user with the properties options and tokens gets defined in mediawiki.js.
30 $.extend( mw.user, {
31
32 /**
33 * Generate a random user session ID.
34 *
35 * This information would potentially be stored in a cookie to identify a user during a
36 * session or series of sessions. Its uniqueness should not be depended on unless the
37 * browser supports the crypto API.
38 *
39 * Known problems with Math.random():
40 * Using the Math.random function we have seen sets
41 * with 1% of non uniques among 200,000 values with Safari providing most of these.
42 * Given the prevalence of Safari in mobile the percentage of duplicates in
43 * mobile usages of this code is probably higher.
44 *
45 * Rationale:
46 * We need about 64 bits to make sure that probability of collision
47 * on 500 million (5*10^8) is <= 1%
48 * See https://en.wikipedia.org/wiki/Birthday_problem#Probability_table
49 *
50 * @return {string} 64 bit integer in hex format, padded
51 */
52 generateRandomSessionId: function () {
53 /*jshint bitwise:false */
54 var rnds, i, r,
55 hexRnds = new Array( 8 ),
56 // Support: IE 11
57 crypto = window.crypto || window.msCrypto;
58
59 // Based on https://github.com/broofa/node-uuid/blob/bfd9f96127/uuid.js
60 if ( crypto && crypto.getRandomValues ) {
61 // Fill an array with 8 random values, each of which is 8 bits.
62 // Note that Uint8Array is array-like but does not implement Array.
63 rnds = new Uint8Array( 8 );
64 crypto.getRandomValues( rnds );
65 } else {
66 rnds = new Array( 8 );
67 for ( i = 0; i < 8; i++ ) {
68 if ( ( i & 3 ) === 0 ) {
69 r = Math.random() * 0x100000000;
70 }
71 rnds[ i ] = r >>> ( ( i & 3 ) << 3 ) & 255;
72 }
73 }
74 // Convert from number to hex
75 for ( i = 0; i < 8; i++ ) {
76 hexRnds[ i ] = byteToHex[ rnds[ i ] ];
77 }
78
79 // Concatenation of two random integers with entropy n and m
80 // returns a string with entropy n+m if those strings are independent
81 return hexRnds.join( '' );
82 },
83
84 /**
85 * Get the current user's database id
86 *
87 * Not to be confused with #id.
88 *
89 * @return {number} Current user's id, or 0 if user is anonymous
90 */
91 getId: function () {
92 return mw.config.get( 'wgUserId', 0 );
93 },
94
95 /**
96 * Get the current user's name
97 *
98 * @return {string|null} User name string or null if user is anonymous
99 */
100 getName: function () {
101 return mw.config.get( 'wgUserName' );
102 },
103
104 /**
105 * Get date user registered, if available
106 *
107 * @return {boolean|null|Date} False for anonymous users, null if data is
108 * unavailable, or Date for when the user registered.
109 */
110 getRegistration: function () {
111 if ( mw.user.isAnon() ) {
112 return false;
113 }
114 var registration = mw.config.get( 'wgUserRegistration' );
115 // Registration may be unavailable if the user signed up before MediaWiki
116 // began tracking this.
117 return !registration ? null : new Date( registration );
118 },
119
120 /**
121 * Whether the current user is anonymous
122 *
123 * @return {boolean}
124 */
125 isAnon: function () {
126 return mw.user.getName() === null;
127 },
128
129 /**
130 * Get an automatically generated random ID (stored in a session cookie)
131 *
132 * This ID is ephemeral for everyone, staying in their browser only until they close
133 * their browser.
134 *
135 * @return {string} Random session ID
136 */
137 sessionId: function () {
138 var sessionId = mw.cookie.get( 'mwuser-sessionId' );
139 if ( sessionId === null ) {
140 sessionId = mw.user.generateRandomSessionId();
141 mw.cookie.set( 'mwuser-sessionId', sessionId, { expires: null } );
142 }
143 return sessionId;
144 },
145
146 /**
147 * Get the current user's name or the session ID
148 *
149 * Not to be confused with #getId.
150 *
151 * @return {string} User name or random session ID
152 */
153 id: function () {
154 return mw.user.getName() || mw.user.sessionId();
155 },
156
157 /**
158 * Get the user's bucket (place them in one if not done already)
159 *
160 * mw.user.bucket( 'test', {
161 * buckets: { ignored: 50, control: 25, test: 25 },
162 * version: 1,
163 * expires: 7
164 * } );
165 *
166 * @deprecated since 1.23
167 * @param {string} key Name of bucket
168 * @param {Object} options Bucket configuration options
169 * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
170 * must have at least one pair)
171 * @param {number} [options.version=0] Version of bucket test, changing this forces
172 * rebucketing
173 * @param {number} [options.expires=30] Length of time (in days) until the user gets
174 * rebucketed
175 * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
176 */
177 bucket: function ( key, options ) {
178 var cookie, parts, version, bucket,
179 range, k, rand, total;
180
181 options = $.extend( {
182 buckets: {},
183 version: 0,
184 expires: 30
185 }, options || {} );
186
187 cookie = mw.cookie.get( 'mwuser-bucket:' + key );
188
189 // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
190 if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) !== -1 ) {
191 parts = cookie.split( ':' );
192 if ( parts.length > 1 && Number( parts[ 0 ] ) === options.version ) {
193 version = Number( parts[ 0 ] );
194 bucket = String( parts[ 1 ] );
195 }
196 }
197
198 if ( bucket === undefined ) {
199 if ( !$.isPlainObject( options.buckets ) ) {
200 throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
201 }
202
203 version = Number( options.version );
204
205 // Find range
206 range = 0;
207 for ( k in options.buckets ) {
208 range += options.buckets[ k ];
209 }
210
211 // Select random value within range
212 rand = Math.random() * range;
213
214 // Determine which bucket the value landed in
215 total = 0;
216 for ( k in options.buckets ) {
217 bucket = k;
218 total += options.buckets[ k ];
219 if ( total >= rand ) {
220 break;
221 }
222 }
223
224 mw.cookie.set(
225 'mwuser-bucket:' + key,
226 version + ':' + bucket,
227 { expires: Number( options.expires ) * 86400 }
228 );
229 }
230
231 return bucket;
232 },
233
234 /**
235 * Get the current user's groups
236 *
237 * @param {Function} [callback]
238 * @return {jQuery.Promise}
239 */
240 getGroups: function ( callback ) {
241 var userGroups = mw.config.get( 'wgUserGroups', [] );
242
243 // Uses promise for backwards compatibility
244 return $.Deferred().resolve( userGroups ).done( callback );
245 },
246
247 /**
248 * Get the current user's rights
249 *
250 * @param {Function} [callback]
251 * @return {jQuery.Promise}
252 */
253 getRights: function ( callback ) {
254 return getUserInfo().then(
255 function ( userInfo ) { return userInfo.rights; },
256 function () { return []; }
257 ).done( callback );
258 }
259 } );
260
261 }( mediaWiki, jQuery ) );