Implement extension registration from an extension.json file
[mediawiki.git] / resources / src / mediawiki / mediawiki.user.js
blob809a65eea66d202ba11f420aec11defb223bea64
1 /**
2  * @class mw.user
3  * @singleton
4  */
5 ( function ( mw, $ ) {
6         var user,
7                 deferreds = {},
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();
13         /**
14          * Get the current user's groups or rights
15          *
16          * @private
17          * @param {string} info One of 'groups' or 'rights'
18          * @return {jQuery.Promise}
19          */
20         function getUserInfo( info ) {
21                 var api;
22                 if ( !deferreds[info] ) {
24                         deferreds.rights = $.Deferred();
25                         deferreds.groups = $.Deferred();
27                         api = new mw.Api();
28                         api.get( {
29                                 action: 'query',
30                                 meta: 'userinfo',
31                                 uiprop: 'rights|groups'
32                         } ).always( function ( data ) {
33                                 var rights, groups;
34                                 if ( data.query && data.query.userinfo ) {
35                                         rights = data.query.userinfo.rights;
36                                         groups = data.query.userinfo.groups;
37                                 }
38                                 deferreds.rights.resolve( rights || [] );
39                                 deferreds.groups.resolve( groups || [] );
40                         } );
42                 }
44                 return deferreds[info].promise();
45         }
47         mw.user = user = {
48                 options: options,
49                 tokens: tokens,
51                 /**
52                  * Generate 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.charAt( r );
66                         }
67                         return id;
68                 },
70                 /**
71                  * Get the current user's database id
72                  *
73                  * Not to be confused with #id.
74                  *
75                  * @return {number} Current user's id, or 0 if user is anonymous
76                  */
77                 getId: function () {
78                         return mw.config.get( 'wgUserId', 0 );
79                 },
81                 /**
82                  * Get the current user's name
83                  *
84                  * @return {string|null} User name string or null if user is anonymous
85                  */
86                 getName: function () {
87                         return mw.config.get( 'wgUserName' );
88                 },
90                 /**
91                  * Get date user registered, if available
92                  *
93                  * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
94                  *  null when data is not available
95                  */
96                 getRegistration: function () {
97                         var registration = mw.config.get( 'wgUserRegistration' );
98                         if ( user.isAnon() ) {
99                                 return false;
100                         } else if ( registration === null ) {
101                                 // Information may not be available if they signed up before
102                                 // MW began storing this.
103                                 return null;
104                         } else {
105                                 return new Date( registration );
106                         }
107                 },
109                 /**
110                  * Whether the current user is anonymous
111                  *
112                  * @return {boolean}
113                  */
114                 isAnon: function () {
115                         return user.getName() === null;
116                 },
118                 /**
119                  * Get an automatically generated random ID (stored in a session cookie)
120                  *
121                  * This ID is ephemeral for everyone, staying in their browser only until they close
122                  * their browser.
123                  *
124                  * @return {string} Random session ID
125                  */
126                 sessionId: function () {
127                         var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
128                         if ( sessionId === undefined || sessionId === null ) {
129                                 sessionId = user.generateRandomSessionId();
130                                 $.cookie( 'mediaWiki.user.sessionId', sessionId, { expires: null, path: '/' } );
131                         }
132                         return sessionId;
133                 },
135                 /**
136                  * Get the current user's name or the session ID
137                  *
138                  * Not to be confused with #getId.
139                  *
140                  * @return {string} User name or random session ID
141                  */
142                 id: function () {
143                         return user.getName() || user.sessionId();
144                 },
146                 /**
147                  * Get the user's bucket (place them in one if not done already)
148                  *
149                  *     mw.user.bucket( 'test', {
150                  *         buckets: { ignored: 50, control: 25, test: 25 },
151                  *         version: 1,
152                  *         expires: 7
153                  *     } );
154                  *
155                  * @deprecated since 1.23
156                  * @param {string} key Name of bucket
157                  * @param {Object} options Bucket configuration options
158                  * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
159                  *  must have at least one pair)
160                  * @param {number} [options.version=0] Version of bucket test, changing this forces
161                  *  rebucketing
162                  * @param {number} [options.expires=30] Length of time (in days) until the user gets
163                  *  rebucketed
164                  * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
165                  */
166                 bucket: function ( key, options ) {
167                         var cookie, parts, version, bucket,
168                                 range, k, rand, total;
170                         options = $.extend( {
171                                 buckets: {},
172                                 version: 0,
173                                 expires: 30
174                         }, options || {} );
176                         cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
178                         // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
179                         if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) !== -1 ) {
180                                 parts = cookie.split( ':' );
181                                 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
182                                         version = Number( parts[0] );
183                                         bucket = String( parts[1] );
184                                 }
185                         }
187                         if ( bucket === undefined ) {
188                                 if ( !$.isPlainObject( options.buckets ) ) {
189                                         throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
190                                 }
192                                 version = Number( options.version );
194                                 // Find range
195                                 range = 0;
196                                 for ( k in options.buckets ) {
197                                         range += options.buckets[k];
198                                 }
200                                 // Select random value within range
201                                 rand = Math.random() * range;
203                                 // Determine which bucket the value landed in
204                                 total = 0;
205                                 for ( k in options.buckets ) {
206                                         bucket = k;
207                                         total += options.buckets[k];
208                                         if ( total >= rand ) {
209                                                 break;
210                                         }
211                                 }
213                                 $.cookie(
214                                         'mediaWiki.user.bucket:' + key,
215                                         version + ':' + bucket,
216                                         { path: '/', expires: Number( options.expires ) }
217                                 );
218                         }
220                         return bucket;
221                 },
223                 /**
224                  * Get the current user's groups
225                  *
226                  * @param {Function} [callback]
227                  * @return {jQuery.Promise}
228                  */
229                 getGroups: function ( callback ) {
230                         return getUserInfo( 'groups' ).done( callback );
231                 },
233                 /**
234                  * Get the current user's rights
235                  *
236                  * @param {Function} [callback]
237                  * @return {jQuery.Promise}
238                  */
239                 getRights: function ( callback ) {
240                         return getUserInfo( 'rights' ).done( callback );
241                 }
242         };
244 }( mediaWiki, jQuery ) );