User:PleaseStand/userinfo-dev.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:PleaseStand/userinfo-dev. |
// based on http://en.wikipedia.org/wiki/User:Fran_McCrory/dimorphism.js
// and on http://en.wikipedia.org/wiki/User:Splarka/sysopdectector.js
( function( $, mw, undefined ) {
var ns = mw.config.get( 'wgNamespaceNumber' ), title = mw.config.get( 'wgTitle' ), api;
// Bail out unless on a user or user talk page, and not a subpage.
if ( ( ns !== 2 && ns !== 3 ) || title.indexOf( '/' ) < 0 ) {
return;
}
/**
* Parse a UTC date in the subset of ISO 8601 format used by MediaWiki.
* That format is YYYY-MM-DDTHH:MM:SSZ.
* @param str The UTC date returned by the MediaWiki API
* @return Date object
*/
function parseDate( str ) {
var m = str.match( /^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/ );
if ( !m ) {
return null;
}
var d = new Date();
d.setUTCFullYear( m[1], m[2] - 1, m[3] );
d.setUTCHours( m[4], m[5], m[6] );
return d;
}
/**
* Load messages corresponding to MediaWiki user groups into mw.messages.
* @param callback Called when messages have loaded
*/
function loadGroupMemberMessages( callback ) {
api.get( {
action: 'query',
amfilter: '-member',
amlang: /*mw.config.get( 'wgUserLanguage' )*/ 'en',
amprefix: 'group-',
maxage: 86400,
meta: 'allmessages'
}, {
ok: function( data ) {
$.each( data.query.allmessages, function( i, message ) {
mw.messages.set( message.name, message['*'] );
} );
callback();
}
} );
}
/**
* Get the user's information by performing an AJAX request and processing the response.
* @param userName The user to retrieve information for
* @param callback Function to receive the processed response
*/
function getInfo( userName, callback ) {
api.get( {
action: 'query',
list: 'users|usercontribs',
maxage: 300,
uclimit: 1,
ucprop: 'timestamp',
ucuser: userName,
usprop: 'blockinfo|editcount|gender|registration|groups',
ususers: userName
}, {
ok: function( data ) {
callback( processResponse( data ) );
}
} );
}
/**
* Extract relevant information from the MediaWiki API output.
* @param data The server's response to the AJAX request
* @return Information about the user
*/
function processResponse( data ) {
var query = data.query, user = query.users[0], contribs = query.usercontribs;
return {
isInvalid: user.invalid !== undefined,
isMissing: user.missing !== undefined,
groups: user.groups || [],
editCount: ( user.editcount !== undefined ) ? user.editcount : null,
registration: ( user.registration !== undefined ) ? parseDate( user.registration ) : null,
isBlocked: user.blockexpiry !== undefined,
gender: ( user.gender !== undefined && user.gender !== 'unknown' ) ? user.gender : null,
lastEdited: ( contribs && contribs[0] && contribs[0].timestamp )
? parseDate( contribs[0].timestamp )
: null
};
}
/**
* Display the user's information on screen.
* @param info Information about the user
*/
function displayInfo( info ) {
var groupCount = info.groups.length, groupList = [];
for ( var i = 0; i < groupCount; ++i ) {
var group = info.groups[i];
if ( groupCount > 1 && group === 'user' ) {
continue;
}
groupList.push( mw.msg( 'group-' + group + '-member', info.gender ) );
}
var groupsText;
if ( groupList.length === 0 ) {
groupsText = '';
} else if ( groupList.length === 1 ) {
groupsText = groupList[0];
} else if ( groupList.length === 2 ) {
groupsText = groupList[0] + ' and ' + groupList[1];
} else {
groupsText = groupList.slice( 0, -1 ).join(', ') + ', and ' + groupList[groupList.length - 1];
}
if ( console ) {
console.log( groupsText );
}
}
/**
* Asynchronously look up the user's information and displays the result.
*/
function main() {
api = new mw.Api();
loadGroupMemberMessages( function() {
getInfo( title, displayInfo );
} );
}
mw.loader.using( ['mediawiki.api', 'mediawiki.jqueryMsg'], main );
} )( jQuery, mediaWiki );