User:Zppix/Test/MediaWiki:Gadget-CentralAuthInterlinkFixer.js

/** * Fix links like User:Example@somewiki on Special:Log and Special:Recentchanges * @author VasilievVV, 2008-2010 * @author Kalan, 2008 * @author Splarka, 2008 * @author Krinkle, 2012 */ /*global mediaWiki, jQuery */ /*jshint browser:true */ (function (mw, $) {	'use strict';	function host(id) {		var wikimedia, idNoSpaces;		if (id === 'mediawikiwiki') {			return 'mediawiki.org';		}		if (id === 'foundationwiki') {			return 'wikimediafoundation.org';		}		if (id === 'wikidatawiki') {			return 'wikidata.org';		}		wikimedia = /^(advisory|auditcom|board|chair|chapcom|collab|comcom|commons|exec|grants|incubator|internal|login|meta|office|otrs wiki|quality|spcom|species|strategy|steward|wikimaniateam|wikimania20\d\d)wiki$/;		if (wikimedia.test(id)) {			return id.replace(/ /g, '-').replace(/_/g, '-').replace(/wiki$/, '.wikimedia.org');		}		idNoSpaces = id.match(/(arbcom enwiki|labswikimedia|pa uswikimedia|wg enwiki)$/) ? id.replace(/ /g, '.') : id.replace(/ /g, '-');		idNoSpaces = idNoSpaces			.replace(/(wiki([mp]edia|quote|source|books|news|versity)|wiktionary)/, '.$1.org')			.replace(/wiki$/, '.wikipedia.org');		if (idNoSpaces.indexOf('.org') !== -1) { return idNoSpaces; }		return false; }

function fixCrosswikiLinks { $('body li a').each(function {			var bits, wiki, user, hostName,				$el = $(this),				txt = $el.text;			if (txt.indexOf('@') !== -1 && txt.indexOf('/') === -1 && /^User\:/i.test(txt)) {				bits = txt.split('@');				user = bits.shift.replace(/^\s*User:(.+)\s*$/g, '$1');				wiki = bits.join('@');				if (wiki === 'global') {					$el.attr('href', mw.config.get('wgScript') + '?title=Special:CentralAuth&target=' + encodeURIComponent(user));				} else {					hostName = host(wiki);					if (hostName) {						$el.attr('href', '//' + hostName + '/wiki/User:' + encodeURIComponent(user));					}				}				if (wiki !== 'metawiki') {					$el.removeClass('new external').addClass('extiw');				}				$el.attr('title', txt);			}		}); }

if (mw.config.get('wgCanonicalSpecialPageName') === 'Log' || mw.config.get('wgCanonicalSpecialPageName') === 'Recentchanges') { mw.hook('wikipage.content').add(fixCrosswikiLinks); } }(mediaWiki, jQuery));