Jump to content

User:Rail01/UserInfo.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * @desc    Get basic info about other users on their contributions
 * @author  Rail01
 * @version 0.4-b
 */
;( function( mw, $ ) {
	'use strict';

	var self = {};
	self.conf = mw.config.get( [
		'wgCanonicalSpecialPageName',
		'wgUserLanguage',
		'wgServer'
	] );

	if (
		self.conf.wgCanonicalSpecialPageName !== 'Contributions' ||
		$( '.mw-userpage-userdoesnotexist' ).length ||
		self.ready
	) return;

	var user = $( '.mw-contributions-user-tools a' )[0].innerText;
	var dataset = {};

	self.i18n = function( msg ) {
		if ( !msg ) return;

		var i18n = {
			en: {
				'info-contribs': 'info',
				'info-contribs-title': 'Get basic information about this user',
				'userinfo': 'User information',
				'userinfo-intro': 'This modal shows you some basic info about "<a href="$1">$2</a>".',
				'label-id': 'User ID',
				'label-editcount': 'Editcount',
				'label-register': 'Registration',
				'label-gender': 'Gender',
				'label-groups': 'Groups',
				'gender-unknown': 'unknown',
				'gender-male': 'male',
				'gender-female': 'female',
				'cancel': 'Cancel',
				'error-api': 'API error occcured! "$1": $2',
				'error-http': 'Unknown HTTP error occured! Please chceck your connection.',
				// Fandom-specific i18n
				'fandom-links': 'Addidiotnal Fandom-specific links for "$1"',
				'fandom-global-js': 'Global JS',
				'fandom-global-css': 'Global CSS',
				'fandom-services': 'Services API',
				'fandom-global-log': 'CC Rights Log'
			},
			pl: {
				'info-contribs': 'informacje',
				'info-contribs-title': 'Uzyskaj podstawowe informacje o użytkowniku',
				'userinfo': 'Informacje o użytkowniku',
				'userinfo-intro': 'To okno przedstawia niektóre podstawowe informacje na temat użytkownika „<a href="$1">$2</a>”.',
				'label-id': 'ID',
				'label-editcount': 'Licznik edycji',
				'label-register': 'Rejestracja',
				'label-gender': 'Płeć',
				'label-groups': 'Grupy',
				'gender-unknown': 'nieznana',
				'gender-male': 'mężczyzna',
				'gender-female': 'kobieta',
				'cancel': 'Anuluj',
				'error-api': 'Wystąpił błąd API! „$1”: $2',
				'error-http': 'Wystąpił nieznany błąd HTTP! Sprawdź swoje połączenie.',
				// Fandom-specific i18n
				'fandom-links': 'Dodatkowe linki dla „$1”',
				'fandom-global-js': 'Globalny JS',
				'fandom-global-css': 'Globalny CSS',
				'fandom-services': 'Services API',
				'fandom-global-log': 'Rejestr uprawnień na CC'
			}
		};

		i18n = $.extend(
			i18n.en,
			i18n[self.conf.wgUserLanguage]
		);

		return i18n[msg];
	};

	self.loadOOUI = function() {
		var module = 'oojs-ui';

		if ( mw.loader.getState( module ) !== 'ready' ) {
			mw.loader.load( module );
		}
	};

	self.error = function( type ) {
		if ( !msg ) return;

		var error = this.i18n( 'error-' + ( type === 'api' ? 'api' : 'http' ) );
		OO.ui.alert( error );
	};

	self.prompt = function( data ) {
		mw.loader.using( [
			'oojs',
			'oojs-ui',
			'mediawiki.util'
		], function() {
			function UserInfoDialog( config ) {
				UserInfoDialog.parent.call( this, config );
			}

			OO.inheritClass( UserInfoDialog, OO.ui.ProcessDialog );

			UserInfoDialog.static.name = 'userInfoDialog';
			UserInfoDialog.static.title = self.i18n( 'userinfo' );
			UserInfoDialog.static.actions = [ {
				label: self.i18n( 'cancel' ),
				flags: 'safe'
			} ];

			var gender;
			if ( data.gender === 'male' ) {
				gender = self.i18n( 'gender-male' );
			} else if ( data.gender === 'female' ) {
				gender = self.i18n( 'gender-female' );
			} else {
				gender = self.i18n( 'gender-unknown' );
			}
			
			var $modal_content = $( '<div>', { id: 'userinfo-modal-content' } ).append(
				$( '<p>', {
					id: 'userinfo-modal-intro',
					html: self.i18n( 'userinfo-intro' )
						.replace( '$1', mw.util.getUrl( 'User:' + user ) )
						.replace( '$2', user )
				} ),
				$( '<ul>', { id: 'userinfo-modal-list' } ).append(
					$( '<li>', { class: 'userinfo-row', id: 'userinfo-id' } ).append(
						$( '<span>', { class: 'userinfo-label', text: self.i18n( 'label-id' ) } ), ': ',
						$( '<code>', { class: 'userinfo-value', text: data.userid } )
					),
					$( '<li>', { class: 'userinfo-row', id: 'userinfo-editcount' } ).append(
						$( '<span>', { class: 'userinfo-label', text: self.i18n( 'label-editcount' ) } ), ': ',
						$( '<code>', { class: 'userinfo-value', text: data.editcount } )
					),
					$( '<li>', { class: 'userinfo-row', id: 'userinfo-register' } ).append(
						$( '<span>', { class: 'userinfo-label', text: self.i18n( 'label-register' ) } ), ': ',
						$( '<code>', { class: 'userinfo-value', text: data.registration } )
					),
					$( '<li>', { class: 'userinfo-row', id: 'userinfo-gender' } ).append(
						$( '<span>', { class: 'userinfo-label', text: self.i18n( 'label-gender' ) } ), ': ',
						$( '<code>', { class: 'userinfo-value', text: gender } )
					),
					$( '<li>', { class: 'userinfo-row', id: 'userinfo-groups' } ).append(
						$( '<span>', { class: 'userinfo-label', text: self.i18n( 'label-groups' ) } ), ': ',
						$( '<code>', { class: 'userinfo-value', text: data.groups.join(', ') } )
					)
				)
			);

			// Fandom-specific section (this script is loaded on external wikis)
			if ( /\.fandom\.com|\.wikia\.org/.test( self.conf.wgServer ) ) {
				var fandomURL = {
					cc: 'https://community.fandom.com/',
					ucp: 'https://ucp.fandom.com/',
					services: 'https://services.fandom.com/'
				};
				
				$modal_content.append(
					'<br />',
					$( '<h3>', {
						id: 'userinfo-fandom-links',
						html: self.i18n( 'fandom-links' )
							.replace( '$1', user )
					} ),
					$( '<ul>', { id: 'userinfo-fandom-links-list' } ).append(
						$( '<li>', { class: 'userinfo-row userinfo-fandom-link', id: 'userinfo-fandom-global-js' } ).append(
							$( '<a>', {
								href: fandomURL.ucp + 'User:' + user + '/global.js',
								target: '_blank',
								text: self.i18n( 'fandom-global-js' )
							} )
						),
						$( '<li>', { class: 'userinfo-row userinfo-fandom-link', id: 'userinfo-fandom-global-css' } ).append(
							$( '<a>', {
								href: fandomURL.ucp + 'User:' + user + '/global.css',
								target: '_blank',
								text: self.i18n( 'fandom-global-css' )
							} )
						),
						$( '<li>', { class: 'userinfo-row userinfo-fandom-link', id: 'userinfo-fandom-services' } ).append(
							$( '<a>', {
								href: fandomURL.services + 'user-attribute/user/' + data.userid,
								target: '_blank',
								text: self.i18n( 'fandom-services' )
							} )
						),
						$( '<li>', { class: 'userinfo-row userinfo-fandom-link', id: 'userinfo-fandom-global-log' } ).append(
							$( '<a>', {
								href: fandomURL.cc + 'Special:Log/rights?page=' + user,
								target: '_blank',
								text: self.i18n( 'fandom-global-log' )
							} )
						)
					)
				);
			}
			// End Fandom-specific section
			
			UserInfoDialog.prototype.initialize = function () {
				UserInfoDialog.parent.prototype.initialize.apply( this, arguments );

				this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
				this.content.$element.append( $modal_content );
				this.$body.append( this.content.$element );
			};

			UserInfoDialog.prototype.getActionProcess = function ( action ) {
				var dialog = this;

				if ( action ) {
					return new OO.ui.Process( function () {
						dialog.close( { action: action } );
					} );
				}
				return UserInfoDialog.parent.prototype.getActionProcess.call( dialog, action );
			};

			var windowManager = new OO.ui.WindowManager();
			var dialog = new UserInfoDialog();

			$( 'body' ).append( windowManager.$element );

			windowManager.addWindows( [ dialog ] );
			windowManager.openWindow( dialog );
		} );
	};

	self.ajax = function() {
		if ( !dataset.userid ) {
			mw.loader.using( 'mediawiki.api', function() {
				new mw.Api().get( {
					action: 'query',
					list: 'users',
					ususers: user,
					usprop: [
						'gender',
						'registration',
						'editcount',
						'groups'
					].join('|')
				} ).done( function( response ) {
					if ( response.error ) {
						self.error( 'api' )
							.replace( '$1', responde.error.code )
							.replace( '$2', responde.error.info );
						return;
					}

					dataset = response.query.users[0];
					self.prompt( dataset );
				} ).fail( function() {
					self.error( 'http' );
					return;
				} );
			} );
		} else self.prompt( dataset );
	};

	self.init = function() {
		self.loadOOUI();

		var $el = $( '<span>', {
			text: self.i18n( 'info-contribs' ),
			title: self.i18n( 'info-contribs-title' ),
			id: 'get-info',
		} ).click( self.ajax );

		$el.appendTo( '.mw-contributions-user-tools .mw-changeslist-links' );
		self.ready = true;
	};

	self.init();
} )( mediaWiki, jQuery );