thumb_handler.php doesn't seem to extract path_info correctly
[mediawiki.git] / resources / mediawiki / mediawiki.user.js
blobe8366a554a08fa68c4dec1f0ea726eaf63e57639
1 /**
2  * @class mw.user
3  * @singleton
4  */
5 ( function ( mw, $ ) {
6         var callbacks, options, tokens, user;
8         /**
9          * Gets the current user's groups or rights.
10          *
11          * @private
12          * @param {string} info One of 'groups' or 'rights'
13          * @param {Function} callback
14          */
15         function getUserInfo( info, callback ) {
16                 var api;
17                 if ( callbacks[info] ) {
18                         callbacks[info].add( callback );
19                         return;
20                 }
21                 callbacks.rights = $.Callbacks('once memory');
22                 callbacks.groups = $.Callbacks('once memory');
23                 callbacks[info].add( callback );
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                         callbacks.rights.fire( rights || [] );
36                         callbacks.groups.fire( groups || [] );
37                 } );
38         }
40         callbacks = {};
42         // Extend the skeleton mw.user from mediawiki.js
43         // This is kind of ugly but we're stuck with this for b/c reasons
44         options = mw.user.options || new mw.Map();
45         tokens = mw.user.tokens || new mw.Map();
47         mw.user = user = {
48                 options: options,
49                 tokens: tokens,
51                 /**
52                  * Generates a random user session ID (32 alpha-numeric characters).
53                  *
54                  * This information would potentially be stored in a cookie to identify a user during a
55                  * session or series of sessions. Its uniqueness should not be depended on.
56                  *
57                  * @return {string} Random set of 32 alpha-numeric characters
58                  */
59                 generateRandomSessionId: function () {
60                         var i, r,
61                                 id = '',
62                                 seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
63                         for ( i = 0; i < 32; i++ ) {
64                                 r = Math.floor( Math.random() * seed.length );
65                                 id += seed.substring( r, r + 1 );
66                         }
67                         return id;
68                 },
70                 /**
71                  * Gets the current user's name.
72                  *
73                  * @return {string|null} User name string or null if users is anonymous
74                  */
75                 getName: function () {
76                         return mw.config.get( 'wgUserName' );
77                 },
79                 /**
80                  * @inheritdoc #getName
81                  * @deprecated since 1.20 use #getName instead
82                  */
83                 name: function () {
84                         return user.getName();
85                 },
87                 /**
88                  * Get date user registered, if available.
89                  *
90                  * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
91                  *  null when data is not available
92                  */
93                 getRegistration: function () {
94                         var registration = mw.config.get( 'wgUserRegistration' );
95                         if ( user.isAnon() ) {
96                                 return false;
97                         } else if ( registration === null ) {
98                                 // Information may not be available if they signed up before
99                                 // MW began storing this.
100                                 return null;
101                         } else {
102                                 return new Date( registration );
103                         }
104                 },
106                 /**
107                  * Checks if the current user is anonymous.
108                  *
109                  * @return {boolean}
110                  */
111                 isAnon: function () {
112                         return user.getName() === null;
113                 },
115                 /**
116                  * @inheritdoc #isAnon
117                  * @deprecated since 1.20 use #isAnon instead
118                  */
119                 anonymous: function () {
120                         return user.isAnon();
121                 },
123                 /**
124                  * Gets a random ID automatically generated and stored in a session cookie.
125                  *
126                  * This ID is ephemeral for everyone, staying in their browser only until they close
127                  * their browser.
128                  *
129                  * @return {string} Random session ID
130                  */
131                 sessionId: function () {
132                         var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
133                         if ( typeof sessionId === 'undefined' || sessionId === null ) {
134                                 sessionId = user.generateRandomSessionId();
135                                 $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': null, 'path': '/' } );
136                         }
137                         return sessionId;
138                 },
140                 /**
141                  * Gets the current user's name or the session ID
142                  *
143                  * @return {string} User name or random session ID
144                  */
145                 id: function () {
146                         var name = user.getName();
147                         if ( name ) {
148                                 return name;
149                         }
150                         return user.sessionId();
151                 },
153                 /**
154                  * Gets the user's bucket, placing them in one at random based on set odds if needed.
155                  *
156                  *     mw.user.bucket( 'test', {
157                  *         'buckets': { 'ignored': 50, 'control': 25, 'test': 25 },
158                  *         'version': 1,
159                  *         'expires': 7
160                  *     } );
161                  *
162                  * @param {string} key Name of bucket
163                  * @param {Object} options Bucket configuration options
164                  * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
165                  *  must have at least one pair)
166                  * @param {number} options.version Version of bucket test, changing this forces rebucketing
167                  *  (optional, default: 0)
168                  * @param {number} options.expires Length of time (in days) until the user gets rebucketed
169                  *  (optional, default: 30)
170                  * @return {string} Bucket name - the randomly chosen key of the options.buckets object
171                  */
172                 bucket: function ( key, options ) {
173                         var cookie, parts, version, bucket,
174                                 range, k, rand, total;
176                         options = $.extend( {
177                                 buckets: {},
178                                 version: 0,
179                                 expires: 30
180                         }, options || {} );
182                         cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
184                         // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
185                         if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) > 0 ) {
186                                 parts = cookie.split( ':' );
187                                 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
188                                         version = Number( parts[0] );
189                                         bucket = String( parts[1] );
190                                 }
191                         }
192                         if ( bucket === undefined ) {
193                                 if ( !$.isPlainObject( options.buckets ) ) {
194                                         throw 'Invalid buckets error. Object expected for options.buckets.';
195                                 }
196                                 version = Number( options.version );
197                                 // Find range
198                                 range = 0;
199                                 for ( k in options.buckets ) {
200                                         range += options.buckets[k];
201                                 }
202                                 // Select random value within range
203                                 rand = Math.random() * range;
204                                 // Determine which bucket the value landed in
205                                 total = 0;
206                                 for ( k in options.buckets ) {
207                                         bucket = k;
208                                         total += options.buckets[k];
209                                         if ( total >= rand ) {
210                                                 break;
211                                         }
212                                 }
213                                 $.cookie(
214                                         'mediaWiki.user.bucket:' + key,
215                                         version + ':' + bucket,
216                                         { 'path': '/', 'expires': Number( options.expires ) }
217                                 );
218                         }
219                         return bucket;
220                 },
222                 /**
223                  * Gets the current user's groups.
224                  *
225                  * @param {Function} callback
226                  */
227                 getGroups: function ( callback ) {
228                         getUserInfo( 'groups', callback );
229                 },
231                 /**
232                  * Gets the current user's rights.
233                  *
234                  * @param {Function} callback
235                  */
236                 getRights: function ( callback ) {
237                         getUserInfo( 'rights', callback );
238                 }
239         };
241 }( mediaWiki, jQuery ) );