// 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 text = '';
var groupCount = info.groups.length, groupList = [];
for ( var i = 0; i < groupCount; ++i ) {
var group = info.groups[i];
if ( group === '*' || ( groupCount > 2 && group === 'user' ) ) {
continue;
}
groupList.push( mw.msg( 'group-' + group + '-member', info.gender ) );
}
if ( groupList.length === 0 ) {
text = 'anonymous user';
} else if ( groupList.length === 1 ) {
text = groupList[0];
} else if ( groupList.length === 2 ) {
text = groupList[0] + ' and ' + groupList[1];
} else {
text = groupList.slice( 0, -1 ).join(', ') + ', and ' + groupList[groupList.length - 1];
}
if ( info.isBlocked ) {
text = 'blocked ' + text;
}
if ( 'AEIOUaeiou'.indexOf( text.charAt(0) ) < 0 ) {
text = 'A ' + text;
} else {
text = 'An ' + text;
}
if ( info.registration ) {
text += ', since ' + info.registration;
}
if ( info.editCount !== null ) {
text += ', with ' + info.editCount + 'edits';
}
text += '.';
if ( info.lastEdited ) {
text += ' Last edited ' + info.lastEdited + '.';
console.log( text );
}
/**
* 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 );