User:Jack who built the house/getUrlFromInterwikiLink

getUrlFromInterwikiLink(interwikiLink[, originHostname]) (source) returns a promise for a URL (absolute or relative) with interwiki prefixes expanded into URL schemas.

mw.loader.getScript('https://en.wikipedia.org/w/index.php?title=User:Jack_who_built_the_house/getUrlFromInterwikiLink.js&action=raw&ctype=text/javascript').then(() => {
  getUrlFromInterwikiLink('de:wikt:test').then(console.log);
  // https://de.wiktionary.org/wiki/test

  getUrlFromInterwikiLink('w:en:test', 'de.wiktionary.org').then(console.log);
  // https://en.wikipedia.org/wiki/test

  getUrlFromInterwikiLink('test').then(console.log);
  // /wiki/test
  
  getUrlFromInterwikiLink('translatewiki:test').then(console.log);
  // https://translatewiki.net/wiki/test
});
  • Works on all WMF wikis, understanding the relativity of interwiki links (e.g., :en: on Wiktionaries means en.wiktionary.org, :en: on Wikipedias means en.wikipedia.org).
  • As compared to using the site's interwiki map, handles all WMF-internal[1] prefixes locally, requesting the interwiki map only for WMF-external prefixes (e.g. translatewiki:).
  • Handles most of the tricky cases the same way MediaWiki does.
  • The exception is namespace names coinciding with prefixes: due to local processing, the script has access only to local namespaces. As a result, it will handle such collisions correctly only for the current domain (e.g. wikipedia: for en.wikipedia.org, like wikipedia:NPOV). For the rest of domains, it will assume that strings in lowercase that are present in the interwiki map are prefixes (unless the string concides with the current wiki prefix, like commons: on Commons – there is a namespace with that name!), while strings in other cases are namespaces (e.g. if something: were an interwiki prefix, something: would be treated as a prefix while Something: – as a namespace).
    • Due to this being a marginal case, I didn't implement the correct differentiation between namespaces and prefixes, but if there is a use case for it, it is possible to implement it.
  • Another exception is highly strange cases like [[w::test]] (doesn't turn into a link on en.wikipedia.org) vs [[wikt::test]] (does turn into a link on en.wikipedia.org).
  • If some prefix is added to the interwiki map and the script is not yet updated, the function will request the interwiki map. If there is some other change to the interwiki map for WMF-internal prefixes, the script will return the outdated data until it is updated. (The interwiki data can be updated by executing User:Jack who built the house/getInterwikiData.js in the browser console and copypasting the resulting value into the script.)
  • You can run the tests on the function's return value by executing User:Jack who built the house/getUrlFromInterwikiLink-tests.js in the browser console (make sure you're on en.wikipedia.org).

If you report a bug, ping JWBTH.

Get an interwiki prefix for a hostname

getInterwikiPrefixForHostname(targetHostname[, originHostname]) returns a promise for an interwiki prefix for targetHostname, provided that the user is on originHostname.

Caveats:

  • It only supports WMF wikis currently (e.g., it won't return a prefix for translatewiki.net).
  • There are hostnames with multiple prefixes for different URLs: for example, Wikidata has not only d: for entities, but also lexemes: for lexemes at https://www.wikidata.org/w/index.php?search=$1&ns146=1. This function is intended to be used for wiki pages or whatever is in under the hostname/wiki/ path.
  • Be sure to specify the canonical hostname, with www. where needed and without m. on mobile.
mw.loader.getScript('https://en.wikipedia.org/w/index.php?title=User:Jack_who_built_the_house/getUrlFromInterwikiLink.js&action=raw&ctype=text/javascript').then(() => {
  getInterwikiPrefixForHostname('de.wiktionary.org', 'en.wikipedia.org').then(console.log);
  // wikt:de:

  getInterwikiPrefixForHostname('en.wikipedia.org', 'de.wiktionary.org').then(console.log);
  // w:en:
  
  getInterwikiPrefixForHostname('de.wikipedia.org', 'en.wikipedia.org').then(console.log);
  // de:

  getInterwikiPrefixForHostname('en.wikipedia.org', 'en.wikipedia.org').then(console.log);
  // (Empty string)

  getInterwikiPrefixForHostname('www.wikidata.org').then(console.log);
  // d:
});

You can also use a synchronous version of this function after accepting it from a hook:

mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Jack_who_built_the_house/getUrlFromInterwikiLink.js&action=raw&ctype=text/javascript');
mw.hook('userjs.getUrlFromInterwikiLink.ready').add(({ getInterwikiPrefixForHostnameSync }) => {
  console.log(getInterwikiPrefixForHostnameSync('de.wiktionary.org', 'en.wikipedia.org'));
  // wikt:de:

  // ...
});

References

  1. ^ More precisely, all domains that can access each other through cross-site AJAX requests. For instance, this includes most of wikimedia.org subdomains, but doesn't include wmflabs.org. See $wgCrossSiteAJAXdomains at https://github.com/wikimedia/operations-mediawiki-config/blob/master/wmf-config/CommonSettings.php for a complete list.
Retrieved from "https://en.wikipedia.org/w/index.php?title=User:Jack_who_built_the_house/getUrlFromInterwikiLink&oldid=1221754645"