Merge "Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups()."
[lhc/web/wiklou.git] / resources / mediawiki / mediawiki.user.js
index 7a15e29..b5f124b 100644 (file)
@@ -2,7 +2,7 @@
  * Implementation for mediaWiki.user
  */
 
-(function( $ ) {
+( function ( mw, $ ) {
 
        /**
         * User object
                /* Private Members */
 
                var that = this;
+               var callbacks = {};
+
+               /**
+                * Gets the current user's groups or rights.
+                * @param {String} info: One of 'groups' or 'rights'.
+                * @param {Function} callback
+                */
+               function getUserInfo( info, callback ) {
+                       var api;
+                       if ( callbacks[info] ) {
+                               callbacks[info].add( callback );
+                               return;
+                       }
+                       callbacks.rights = $.Callbacks('once memory');
+                       callbacks.groups = $.Callbacks('once memory');
+                       callbacks[info].add( callback );
+                       api = new mw.Api();
+                       api.get( {
+                               action: 'query',
+                               meta: 'userinfo',
+                               uiprop: 'rights|groups'
+                       } ).always( function ( data ) {
+                               var rights, groups;
+                               if ( data.query && data.query.userinfo ) {
+                                       rights = data.query.userinfo.rights;
+                                       groups = data.query.userinfo.groups;
+                               }
+                               callbacks.rights.fire( rights || [] );
+                               callbacks.groups.fire( groups || [] );
+                       } );
+               }
 
                /* Public Members */
 
                 *
                 * @return Mixed: User name string or null if users is anonymous
                 */
-               this.name = function() {
+               this.getName = function () {
                        return mw.config.get( 'wgUserName' );
                };
 
+               /**
+                * @deprecated since 1.20 use mw.user.getName() instead
+                */
+               this.name = function () {
+                       return this.getName();
+               };
+
                /**
                 * Checks if the current user is anonymous.
                 *
                 * @return Boolean
                 */
-               this.anonymous = function() {
-                       return that.name() ? false : true;
+               this.isAnon = function () {
+                       return that.getName() === null;
+               };
+
+               /**
+                * @deprecated since 1.20 use mw.user.isAnon() instead
+                */
+               this.anonymous = function () {
+                       return that.isAnon();
                };
 
                /**
                 */
                this.sessionId = function () {
                        var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
-                       if ( typeof sessionId == 'undefined' || sessionId === null ) {
+                       if ( typeof sessionId === 'undefined' || sessionId === null ) {
                                sessionId = generateId();
                                $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': null, 'path': '/' } );
                        }
                 * @return String: User name or random session ID
                 */
                this.id = function() {
-                       var name = that.name();
+                       var name = that.getName();
                        if ( name ) {
                                return name;
                        }
                        var id = $.cookie( 'mediaWiki.user.id' );
-                       if ( typeof id == 'undefined' || id === null ) {
+                       if ( typeof id === 'undefined' || id === null ) {
                                id = generateId();
                        }
                        // Set cookie if not set, or renew it if already set
-                       $.cookie( 'mediaWiki.user.id', id, { 'expires': 365, 'path': '/' } );
+                       $.cookie( 'mediaWiki.user.id', id, {
+                               expires: 365,
+                               path: '/'
+                       } );
                        return id;
                };
 
                 *         'expires': 7
                 *     } );
                 */
-               this.bucket = function( key, options ) {
+               this.bucket = function ( key, options ) {
                        options = $.extend( {
                                'buckets': {},
                                'version': 0,
                        // Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
                        if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) > 0 ) {
                                var parts = cookie.split( ':' );
-                               if ( parts.length > 1 && parts[0] == options.version ) {
+                               if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
                                        version = Number( parts[0] );
                                        bucket = String( parts[1] );
                                }
                                        }
                                }
                                if ( options.tracked ) {
-                                       mw.loader.using( 'jquery.clickTracking', function() {
+                                       mw.loader.using( 'jquery.clickTracking', function () {
                                                $.trackAction(
                                                        'mediaWiki.user.bucket:' + key + '@' + version + ':' + bucket
                                                );
                        }
                        return bucket;
                };
+
+               /**
+                * Gets the current user's groups.
+                */
+               this.getGroups = function ( callback ) {
+                       getUserInfo( 'groups', callback );
+               };
+
+               /**
+                * Gets the current user's rights.
+                */
+               this.getRights = function ( callback ) {
+                       getUserInfo( 'rights', callback );
+               };
        }
 
        // Extend the skeleton mw.user from mediawiki.js
        // This is kind of ugly but we're stuck with this for b/c reasons
        mw.user = new User( mw.user.options, mw.user.tokens );
 
-})(jQuery);
+}( mediaWiki, jQuery ) );