User:BryghtShadow/common.js

// Obtain a list of all language interwikis of a particular wiki. $( function {  var api = new mw.Api;  api.get( { action: 'query', meta: 'siteinfo', siprop: 'interwikimap|languages', sifilteriw: 'local', } ).done( function ( data ) { var languages = {}; var duplicates = {}; data.query.languages.forEach(function(lang) {     var name = lang['*'];      var newCode = lang.code;      var oldCode = languages[name];      // Filter out duplicate language names      // and try to assign it an non-aliased code.      if (oldCode === undefined) {        languages[name] = newCode;      } else {        // assume that aliases are longer and start with the non-aliased code        // @TODO: Ensure that all languages are correct and valid.        // See also: https://meta.wikimedia.org/wiki/Special_language_codes        if (newCode.length < oldCode.length && oldCode.startsWith(newCode)) {          languages[name] = newCode;          duplicates[name] = oldCode;        } else {          duplicates[name] = newCode;        }      }    }); console.log('Included language codes:', languages); console.log('Excluded language codes:', duplicates);

var interwikis = []; data.query.interwikimap.forEach(function(iw) {     var iw_prefix = iw.prefix;      var iw_url = iw.url;      var iw_language = iw.language;      if (languages[iw_language] === iw_prefix && iw_url.indexOf('.wikia.com/') === -1) {        interwikis.push('* '+iw_prefix+': (' + JSON.stringify(iw_language) + '): ' + iw_url.replace('$1', ''));      }    }); console.log('Interwiki languages:\n' + interwikis.join('\n')); }); });