Jump to content

User:PleaseStand/userinfo-dev.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by PleaseStand (talk | contribs) at 03:45, 22 April 2012 (Fix < → >=). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// 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];
		}

		console.log( groupsText );

	}

	/**
	 * Asynchronously look up the user's information and displays the result.
	 */
	function main() {
		console.log( 'api' );
		api = new mw.Api();
		console.log( 'loadGroupMemberMessages' );
		loadGroupMemberMessages( function() {
			console.log( 'getInfo' );
			getInfo( title, displayInfo );
		} );
	}

	mw.loader.using( ['mediawiki.Api', 'mediawiki.jqueryMsg'], main );
	
} )( jQuery, mediaWiki );