The official discord link if you wish to join the discord: https://discord.gg/j5RKwCvAFu

Support the wiki on our official Ko-Fi page or Patreon page!

MediaWiki:Gadget-userProfile.js

From The Codex
Revision as of 16:37, 30 January 2024 by GiverOfThePeace (talk | contribs) (Protected "MediaWiki:Gadget-userProfile.js" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:MarkusRost/profile.js&action=raw&ctype=text/javascript' );
mw.loader.using( [ 'mediawiki.util', 'mediawiki.api' ] ).done( function() {
	var config = mw.config.get( [
		'wgRelevantUserName',
		'wgNamespaceNumber',
		'wgUserLanguage',
		'wgArticleId',
		'wgAction',
		'wgTitle'
	] );
	var username = config.wgRelevantUserName;
	if ( !username || window.loadedHorseUserProfile ) return;
	window.loadedHorseUserProfile = true;
	if ( config.wgNamespaceNumber !== -1 && config.wgTitle.includes('/') ) return;
	
	var lang = mw.language.bcp47(config.wgUserLanguage);
	var dateOptions = {
		year: "numeric",
		month: "short",
		day: "numeric",
		hour: "2-digit",
		minute: "2-digit",
		timeZone: "UTC",
		timeZoneName: "short"
	};
	var api = new mw.Api();
	if ( mw.util.isIPAddress(username) ) api.get( {
		action: 'query',
		list: ['usercontribs', 'blocks'],
		ucuser: username,
		ucprop: [],
		uclimit: 'max',
		bkip: username,
		bkprop: ['user', 'by', 'timestamp', 'expiry', 'reason'],
		bklimit: 1,
		formatversion: 2
	} ).done( function(data) {
		var editcount = data.query.usercontribs.length;
		if ( data.continue ) editcount = data.limits.usercontribs+'+'
		var user = {
			groups: ['*'],
			editcount: editcount
		};
		formatProfile(user, data.query.blocks[0]);
	} );
	else api.get( {
		action: 'query',
		list: 'users',
		ususers: username,
		usprop: ['groups', 'editcount', 'registration', 'blockinfo', 'emailable'],
		formatversion: 2
	} ).done( function(data) {
		var user = data.query.users[0];
		var block;
		if ( user.blockid ) block = {
			user: username,
			by: user.blockedby,
			timestamp: user.blockedtimestamp,
			expiry: user.blockexpiry,
			reason: user.blockreason
		};
		formatProfile(user, block);
	} );
	
	if ( window.loadedHorseUserProfileContribs ) return;
	window.loadedHorseUserProfileContribs = true;
	if ( config.wgNamespaceNumber === 2 && !config.wgArticleId && config.wgAction === 'view' ) {
		mw.loader.using( [ 'mediawiki.pager.styles', 'mediawiki.special.changeslist' ] ).then( function() {
			var text = '== [[Special:Contributions/' + username + '|{{int:contributions-title|' + username + '}}]] ==\n{{Special:Contributions/' + username + '|limit=20}}';
			new mw.Api().parse( text, {
				title: 'User:' + config.wgTitle,
				wrapoutputclass: 'horse-userprofile-contribs',
				disableeditsection: true,
				disablelimitreport: true,
				uselang: config.wgUserLanguage
			} ).done( function(parsedText) {
				mw.hook( 'wikipage.content' ).fire(
					$( parsedText ).appendTo('#mw-content-text')
				);
			} );
		} );
	}
	
	function formatProfile(user, block) {
		user.groups.splice(user.groups.indexOf('*'), 1);
		var text = '<div class="horse-userprofile-info">';
		if ( user.groups.length ) {
			var groupList = user.groups.map( function(group) {
				var grouppage = '{{MediaWiki:grouppage-'+group+'}}';
				return [
					'{{#ifexist: '+grouppage+' | [['+grouppage+'|{{int:group-'+group+'}}]] | {{int:group-'+group+'}} }}',
					'{{#ifexist: '+grouppage+' | [['+grouppage+'|{{int:group-'+group+'-member|'+username+'}}]] | {{int:group-'+group+'-member|'+username+'}} }}'
				];
			} ).reduce( function(prev, cur) {
				prev[0].push(cur[0]);
				prev[1].push(cur[1]);
				return prev;
			}, [[], []] );
			text += '\n\n<span>{{int:prefs-memberingroups|'+user.groups.length+'|'+username+'}}</span>\t<span>{{int:prefs-memberingroups-type|'+mw.language.listToText(groupList[0])+'|'+mw.language.listToText(groupList[1])+'}}</span>';
		}
		text += '\n\n<span>{{int:prefs-edits}}</span>\t<span>[[Special:Contribs/'+username+'|'+mw.language.convertNumber(user.editcount)+']]</span>';
		if ( user.registration ) {
			text += '\n\n<span>{{int:prefs-registration}}</span>\t<span>'+mw.language.convertNumber(new Date(user.registration).toLocaleString(lang, dateOptions))+'</span>';
			if ( !user.emailable ) text += '\n\n<span>{{int:nowikiemailtext|'+username+'}}</span>';
		}
		text += '\n\n</div>';
		if ( block && !$('.mw-warning-with-logexcerpt .mw-logevent-loglines > .mw-logline-block').length ) {
			var blockDate = new Date(block.timestamp);
			var blockexpiry = '{{int:infiniteblock}}';
			if ( !['infinite', 'indefinite', 'infinity', 'never'].includes(block.expiry) ) {
				var expiry = new Date(block.expiry.replace( /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2,3})/, '$1-$2-$3T$4:$5:$6Z' ));
				var duration = expiry - blockDate;
				var blockDuration = [];
				var factor = 1000;
				if ( duration % factor === 0 ) blockDuration = ['duration-seconds', Math.floor(duration / factor)];
				factor *= 60;
				if ( duration % factor === 0 ) blockDuration = ['duration-minutes', Math.floor(duration / factor)];
				factor *= 60;
				if ( duration % factor === 0 ) blockDuration = ['duration-hours', Math.floor(duration / factor)];
				factor *= 24;
				if ( duration % factor === 0 ) blockDuration = ['duration-days', Math.floor(duration / factor)];
				if ( duration % (factor*7) === 0 ) blockDuration = ['duration-weeks', Math.floor(duration / (factor*7))];
				if ( duration % (factor*30) === 0 ) blockDuration = ['months', Math.floor(duration / (factor*30))];
				factor *= 365;
				if ( duration % factor === 0 ) blockDuration = ['duration-years', Math.floor(duration / factor)];
				if ( duration % (factor*10) === 0 ) blockDuration = ['duration-decades', Math.floor(duration / (factor*10))];
				if ( duration % (factor*100) === 0 ) blockDuration = ['duration-centuries', Math.floor(duration / (factor*100))];
				if ( duration % (factor*1000) === 0 ) blockDuration = ['duration-millennia', Math.floor(duration / (factor*1000))];
				if ( blockDuration.length < 2 || blockDuration[1] > 1000 ) blockexpiry = mw.language.convertNumber(expiry.toLocaleString(lang, dateOptions));
				else blockexpiry = '{{int:'+blockDuration.join('|')+'}}';
			}
			text += '<div class="horse-userprofile-block mw-warning-with-logexcerpt"><div>\n{{int:blocked-notice-logextract|'+username+'}}\n';
			text += '<ul class="mw-logevent-loglines"><li class="mw-logline-block">'+mw.language.convertNumber(blockDate.toLocaleString(lang, dateOptions))+' {{int:logentry-block-block|[[User:'+block.by+'|'+block.by+']] <span class="mw-usertoollinks mw-changeslist-links"><span>[[User talk:'+block.by+'|{{int:talkpagelinktext}}]]</span> <span>[[Special:Contribs/'+block.by+'|{{int:contribslink}}]]</span></span>|'+block.by;
			if ( mw.util.isIPAddress(block.user, true) ) text += '|[[Special:Contribs/'+block.user+'|'+block.user+']] <span class="mw-usertoollinks mw-changeslist-links"><span>[[User talk:'+block.user+'|{{int:talkpagelinktext}}]]</span></span>';
			else text += '|[[User:'+block.user+'|'+block.user+']] <span class="mw-usertoollinks mw-changeslist-links"><span>[[User talk:'+block.user+'|{{int:talkpagelinktext}}]]</span> <span>[[Special:Contribs/'+block.user+'|{{int:contribslink}}]]</span></span>';
			text += '|'+block.user+'|'+blockexpiry+'|}} <span class="comment">{{int:parentheses|'+block.reason+'}}</span></li></ul></div></div>';
		}
		api.parse( text, {
			disablelimitreport: true,
			wrapoutputclass: 'horse-userprofile mw-message-box mw-content-'+($('#contentSub').attr('dir')||'ltr'),
			uselang: config.wgUserLanguage
		} ).done( function(parsedText) {
			parsedText = parsedText.replace( /\u29FCgroup-([^\u29FC\u29FD]+?)(?:-member)?\u29FD/g, '$1' );
			mw.hook( 'wikipage.content' ).fire(
				$( parsedText ).appendTo('#contentSub')
			);
		} );
	}
} );