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