summaryrefslogtreecommitdiff
path: root/resources/mediawiki/mediawiki.user.js
diff options
context:
space:
mode:
Diffstat (limited to 'resources/mediawiki/mediawiki.user.js')
-rw-r--r--resources/mediawiki/mediawiki.user.js253
1 files changed, 126 insertions, 127 deletions
diff --git a/resources/mediawiki/mediawiki.user.js b/resources/mediawiki/mediawiki.user.js
index e0329597..3e375fb6 100644
--- a/resources/mediawiki/mediawiki.user.js
+++ b/resources/mediawiki/mediawiki.user.js
@@ -1,67 +1,60 @@
-/*
- * Implementation for mediaWiki.user
+/**
+ * @class mw.user
+ * @singleton
*/
-
( function ( mw, $ ) {
+ var user,
+ callbacks = {},
+ // Extend the skeleton mw.user from mediawiki.js
+ // This is kind of ugly but we're stuck with this for b/c reasons
+ options = mw.user.options || new mw.Map(),
+ tokens = mw.user.tokens || new mw.Map();
/**
- * User object
+ * Get the current user's groups or rights
+ *
+ * @private
+ * @param {string} info One of 'groups' or 'rights'
+ * @param {Function} callback
*/
- function User( options, tokens ) {
- var user, callbacks;
-
- /* Private Members */
-
- user = this;
- 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');
+ function getUserInfo( info, callback ) {
+ var api;
+ if ( callbacks[info] ) {
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 || [] );
- } );
+ 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 */
-
- this.options = options || new mw.Map();
-
- this.tokens = tokens || new mw.Map();
-
- /* Public Methods */
+ mw.user = user = {
+ options: options,
+ tokens: tokens,
/**
- * Generates a random user session ID (32 alpha-numeric characters).
+ * Generate a random user session ID (32 alpha-numeric characters)
*
* This information would potentially be stored in a cookie to identify a user during a
* session or series of sessions. Its uniqueness should not be depended on.
*
- * @return String: Random set of 32 alpha-numeric characters
+ * @return {string} Random set of 32 alpha-numeric characters
*/
- this.generateRandomSessionId = function () {
+ generateRandomSessionId: function () {
var i, r,
id = '',
seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
@@ -70,33 +63,45 @@
id += seed.substring( r, r + 1 );
}
return id;
- };
+ },
+
+ /**
+ * Get the current user's database id
+ *
+ * Not to be confused with #id.
+ *
+ * @return {number} Current user's id, or 0 if user is anonymous
+ */
+ getId: function () {
+ return mw.config.get( 'wgUserId', 0 );
+ },
/**
- * Gets the current user's name.
+ * Get the current user's name
*
- * @return Mixed: User name string or null if users is anonymous
+ * @return {string|null} User name string or null if user is anonymous
*/
- this.getName = function () {
+ getName: function () {
return mw.config.get( 'wgUserName' );
- };
+ },
/**
- * @deprecated since 1.20 use mw.user.getName() instead
+ * @inheritdoc #getName
+ * @deprecated since 1.20 use #getName instead
*/
- this.name = function () {
- return this.getName();
- };
+ name: function () {
+ return user.getName();
+ },
/**
- * Get date user registered, if available.
+ * Get date user registered, if available
*
- * @return {Date|false|null} date user registered, or false for anonymous users, or
+ * @return {Date|boolean|null} Date user registered, or false for anonymous users, or
* null when data is not available
*/
- this.getRegistration = function () {
+ getRegistration: function () {
var registration = mw.config.get( 'wgUserRegistration' );
- if ( this.isAnon() ) {
+ if ( user.isAnon() ) {
return false;
} else if ( registration === null ) {
// Information may not be available if they signed up before
@@ -105,110 +110,109 @@
} else {
return new Date( registration );
}
- };
+ },
/**
- * Checks if the current user is anonymous.
+ * Whether the current user is anonymous
*
- * @return Boolean
+ * @return {boolean}
*/
- this.isAnon = function () {
+ isAnon: function () {
return user.getName() === null;
- };
+ },
/**
- * @deprecated since 1.20 use mw.user.isAnon() instead
+ * @inheritdoc #isAnon
+ * @deprecated since 1.20 use #isAnon instead
*/
- this.anonymous = function () {
+ anonymous: function () {
return user.isAnon();
- };
+ },
/**
- * Gets a random session ID automatically generated and kept in a cookie.
+ * Get an automatically generated random ID (stored in a session cookie)
*
* This ID is ephemeral for everyone, staying in their browser only until they close
* their browser.
*
- * @return String: User name or random session ID
+ * @return {string} Random session ID
*/
- this.sessionId = function () {
+ sessionId: function () {
var sessionId = $.cookie( 'mediaWiki.user.sessionId' );
- if ( typeof sessionId === 'undefined' || sessionId === null ) {
+ if ( sessionId === undefined || sessionId === null ) {
sessionId = user.generateRandomSessionId();
- $.cookie( 'mediaWiki.user.sessionId', sessionId, { 'expires': null, 'path': '/' } );
+ $.cookie( 'mediaWiki.user.sessionId', sessionId, { expires: null, path: '/' } );
}
return sessionId;
- };
+ },
/**
- * Gets the current user's name or the session ID
+ * Get the current user's name or the session ID
*
- * @return String: User name or random session ID
+ * Not to be confused with #getId.
+ *
+ * @return {string} User name or random session ID
*/
- this.id = function() {
- var name = user.getName();
- if ( name ) {
- return name;
- }
- return user.sessionId();
- };
+ id: function () {
+ return user.getName() || user.sessionId();
+ },
/**
- * Gets the user's bucket, placing them in one at random based on set odds if needed.
- *
- * @param key String: Name of bucket
- * @param options Object: Bucket configuration options
- * @param options.buckets Object: List of bucket-name/relative-probability pairs (required,
- * must have at least one pair)
- * @param options.version Number: Version of bucket test, changing this forces rebucketing
- * (optional, default: 0)
- * @param options.tracked Boolean: Track the event of bucketing through the API module of
- * the ClickTracking extension (optional, default: false)
- * @param options.expires Number: Length of time (in days) until the user gets rebucketed
- * (optional, default: 30)
- * @return String: Bucket name - the randomly chosen key of the options.buckets object
+ * Get the user's bucket (place them in one if not done already)
*
- * @example
* mw.user.bucket( 'test', {
- * 'buckets': { 'ignored': 50, 'control': 25, 'test': 25 },
- * 'version': 1,
- * 'tracked': true,
- * 'expires': 7
+ * buckets: { ignored: 50, control: 25, test: 25 },
+ * version: 1,
+ * expires: 7
* } );
+ *
+ * @param {string} key Name of bucket
+ * @param {Object} options Bucket configuration options
+ * @param {Object} options.buckets List of bucket-name/relative-probability pairs (required,
+ * must have at least one pair)
+ * @param {number} [options.version=0] Version of bucket test, changing this forces
+ * rebucketing
+ * @param {number} [options.expires=30] Length of time (in days) until the user gets
+ * rebucketed
+ * @return {string} Bucket name - the randomly chosen key of the `options.buckets` object
*/
- this.bucket = function ( key, options ) {
+ bucket: function ( key, options ) {
var cookie, parts, version, bucket,
range, k, rand, total;
options = $.extend( {
buckets: {},
version: 0,
- tracked: false,
expires: 30
}, options || {} );
cookie = $.cookie( 'mediaWiki.user.bucket:' + key );
// Bucket information is stored as 2 integers, together as version:bucket like: "1:2"
- if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) > 0 ) {
+ if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) !== -1 ) {
parts = cookie.split( ':' );
if ( parts.length > 1 && Number( parts[0] ) === options.version ) {
version = Number( parts[0] );
bucket = String( parts[1] );
}
}
+
if ( bucket === undefined ) {
if ( !$.isPlainObject( options.buckets ) ) {
- throw 'Invalid buckets error. Object expected for options.buckets.';
+ throw new Error( 'Invalid bucket. Object expected for options.buckets.' );
}
+
version = Number( options.version );
+
// Find range
range = 0;
for ( k in options.buckets ) {
range += options.buckets[k];
}
+
// Select random value within range
rand = Math.random() * range;
+
// Determine which bucket the value landed in
total = 0;
for ( k in options.buckets ) {
@@ -218,39 +222,34 @@
break;
}
}
- if ( options.tracked ) {
- mw.loader.using( 'jquery.clickTracking', function () {
- $.trackAction(
- 'mediaWiki.user.bucket:' + key + '@' + version + ':' + bucket
- );
- } );
- }
+
$.cookie(
'mediaWiki.user.bucket:' + key,
version + ':' + bucket,
- { 'path': '/', 'expires': Number( options.expires ) }
+ { path: '/', expires: Number( options.expires ) }
);
}
+
return bucket;
- };
+ },
/**
- * Gets the current user's groups.
+ * Get the current user's groups
+ *
+ * @param {Function} callback
*/
- this.getGroups = function ( callback ) {
+ getGroups: function ( callback ) {
getUserInfo( 'groups', callback );
- };
+ },
/**
- * Gets the current user's rights.
+ * Get the current user's rights
+ *
+ * @param {Function} callback
*/
- this.getRights = function ( callback ) {
+ 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 );
+ }
+ };
}( mediaWiki, jQuery ) );