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