User:BryghtShadow/common.js

// Obtain a list of all language interwikis of a particular wiki. require(['wikia.window', 'jquery', 'mw'], function (window, $, mw) {	var api = new mw.Api;	api.get( { action: 'query', meta: 'siteinfo', siprop: 'interwikimap|languages', sifilteriw: 'local', } ).done( function ( data ) { var languages = {}, duplicates = {}; var name, curr, prev; $.each(data.query.languages, function(i, language) {			name = language['*'];			curr = language.code; // current code			prev = languages[name]; // previously found code			// Filter out duplicate language names			// and try to assign it an non-aliased code.			// Assumes aliases are longer and start with non-aliased code			// @TODO: Ensure that all languages are correct and valid.			// See: https://meta.wikimedia.org/wiki/Special_language_codes			if (prev === undefined) {				languages[name] = curr;			} else if (curr.length < prev.length && prev.startsWith(curr)) {				languages[name] = curr;				duplicates[name] = prev;			} else {				duplicates[name] = curr;			}		}); console.debug('Included language codes:', languages); console.debug('Excluded language codes:', duplicates); var interwikis = []; var iw_prefix, iw_url, iw_language, hostname; $.each(data.query.interwikimap, function(i, iw) {			iw_prefix = iw.prefix;			iw_url = iw.url.replace('$1', '');			iw_language = iw.language;			// Make use of the anchor element to obtain just the hostname.			hostname = $('').attr('href', iw_url)[0].hostname;			if (languages[iw_language] === iw_prefix && !hostname.endsWith('.wikia.com')) {				var str = '* '+iw_prefix+': ('+iw_language+'): '+iw_url;				interwikis.push(str);			}		}); console.log('Interwiki languages:\n' + interwikis.join('\n')); }); });