New hook for readonly editpages
[mediawiki.git] / resources / mediawiki / mediawiki.user.js
blob83441119942eae4d11fe6ec728ce7a6945de7b9b
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          * @param {Function} [callback]
19          * @return {jQuery.Promise}
20          */
21         function getUserInfo( info, callback ) {
22                 var api;
23                 if ( !deferreds[info] ) {
25                         deferreds.rights = $.Deferred();
26                         deferreds.groups = $.Deferred();
28                         api = new mw.Api();
29                         api.get( {
30                                 action: 'query',
31                                 meta: 'userinfo',
32                                 uiprop: 'rights|groups'
33                         } ).always( function ( data ) {
34                                 var rights, groups;
35                                 if ( data.query && data.query.userinfo ) {
36                                         rights = data.query.userinfo.rights;
37                                         groups = data.query.userinfo.groups;
38                                 }
39                                 deferreds.rights.resolve( rights || [] );
40                                 deferreds.groups.resolve( groups || [] );
41                         } );
43                 }
45                 return deferreds[info].done( callback ).promise();
46         }
48         mw.user = user = {
49                 options: options,
50                 tokens: tokens,
52                 /**
53                  * Generate a random user session ID (32 alpha-numeric characters)
54                  *
55                  * This information would potentially be stored in a cookie to identify a user during a
56                  * session or series of sessions. Its uniqueness should not be depended on.
57                  *
58                  * @return {string} Random set of 32 alpha-numeric characters
59                  */
60                 generateRandomSessionId: function () {
61                         var i, r,
62                                 id = '',
63                                 seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
64                         for ( i = 0; i < 32; i++ ) {
65                                 r = Math.floor( Math.random() * seed.length );
66                                 id += seed.substring( r, r + 1 );
67                         }
68                         return id;
69                 },
71                 /**
72                  * Get the current user's database id
73                  *
74                  * Not to be confused with #id.
75                  *
76                  * @return {number} Current user's id, or 0 if user is anonymous
77                  */
78                 getId: function () {
79                         return mw.config.get( 'wgUserId', 0 );
80                 },
82                 /**
83                  * Get the current user's name
84                  *
85                  * @return {string|null} User name string or null if user is anonymous
86                  */
87                 getName: function () {
88                         return mw.config.get( 'wgUserName' );
89                 },
91                 /**
92                  * @inheritdoc #getName
93                  * @deprecated since 1.20 use #getName instead
94                  */
95                 name: function () {
96                         return user.getName();
97                 },
99                 /**
100                  * Get date user registered, if available
101                  *
102                  * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
103                  *  null when data is not available
104                  */
105                 getRegistration: function () {
106                         var registration = mw.config.get( 'wgUserRegistration' );
107                         if ( user.isAnon() ) {
108                                 return false;
109                         } else if ( registration === null ) {
110                                 // Information may not be available if they signed up before
111                                 // MW began storing this.
112                                 return null;
113                         } else {
114                                 return new Date( registration );
115                         }
116                 },
118                 /**
119                  * Whether the current user is anonymous
120                  *
121                  * @return {boolean}
122                  */
123                 isAnon: function () {
124                         return user.getName() === null;
125                 },
127                 /**
128                  * @inheritdoc #isAnon
129                  * @deprecated since 1.20 use #isAnon instead
130                  */
131                 anonymous: function () {
132                         return user.isAnon();
133                 },
135                 /**
136                  * Get an automatically generated random ID (stored in a session cookie)
137                  *
138                  * This ID is ephemeral for everyone, staying in their browser only until they close
139                  * their browser.
140                  *
141                  * @return {string} Random session ID
142                  */
143                 sessionId: function () {
144                         var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
145                         if ( sessionId === undefined || sessionId === null ) {
146                                 sessionId = user.generateRandomSessionId();
147                                 $.cookie( 'mediaWiki.user.sessionId', sessionId, { expires: null, path: '/' } );
148                         }
149                         return sessionId;
150                 },
152                 /**
153                  * Get the current user's name or the session ID
154                  *
155                  * Not to be confused with #getId.
156                  *
157                  * @return {string} User name or random session ID
158                  */
159                 id: function () {
160                         return user.getName() || user.sessionId();
161                 },
163                 /**
164                  * Get the user's bucket (place them in one if not done already)
165                  *
166                  *     mw.user.bucket( 'test', {
167                  *         buckets: { ignored: 50, control: 25, test: 25 },
168                  *         version: 1,
169                  *         expires: 7
170                  *     } );
171                  *
172                  * @deprecated since 1.23
173                  * @param {string} key Name of bucket
174                  * @param {Object} options Bucket configuration options
175                  * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
176                  *  must have at least one pair)
177                  * @param {number} [options.version=0] Version of bucket test, changing this forces
178                  *  rebucketing
179                  * @param {number} [options.expires=30] Length of time (in days) until the user gets
180                  *  rebucketed
181                  * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
182                  */
183                 bucket: function ( key, options ) {
184                         var cookie, parts, version, bucket,
185                                 range, k, rand, total;
187                         options = $.extend( {
188                                 buckets: {},
189                                 version: 0,
190                                 expires: 30
191                         }, options || {} );
193                         cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
195                         // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
196                         if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) !== -1 ) {
197                                 parts = cookie.split( ':' );
198                                 if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
199                                         version = Number( parts[0] );
200                                         bucket = String( parts[1] );
201                                 }
202                         }
204                         if ( bucket === undefined ) {
205                                 if ( !$.isPlainObject( options.buckets ) ) {
206                                         throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
207                                 }
209                                 version = Number( options.version );
211                                 // Find range
212                                 range = 0;
213                                 for ( k in options.buckets ) {
214                                         range += options.buckets[k];
215                                 }
217                                 // Select random value within range
218                                 rand = Math.random() * range;
220                                 // Determine which bucket the value landed in
221                                 total = 0;
222                                 for ( k in options.buckets ) {
223                                         bucket = k;
224                                         total += options.buckets[k];
225                                         if ( total >= rand ) {
226                                                 break;
227                                         }
228                                 }
230                                 $.cookie(
231                                         'mediaWiki.user.bucket:' + key,
232                                         version + ':' + bucket,
233                                         { path: '/', expires: Number( options.expires ) }
234                                 );
235                         }
237                         return bucket;
238                 },
240                 /**
241                  * Get the current user's groups
242                  *
243                  * @param {Function} [callback]
244                  * @return {jQuery.Promise}
245                  */
246                 getGroups: function ( callback ) {
247                         return getUserInfo( 'groups', callback );
248                 },
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( 'rights', callback );
258                 }
259         };
261 }( mediaWiki, jQuery ) );