Module:Sandbox/trappist the monk/interwiki list/data explained

--[=[ This module creates alpha-sorted lists of interwiki prefixes and their language names, and the reverse, language names and their matching interwiki prefixes. These lists are used in [[Wikipedia:Template_index/Redirect_language_codes]]and in .Before this module was developed, Redirect_language_codes_–_code_sort was composed of individual entries thatlooked like this wikitext: abab}} language|Abkhazand Redirect_language_codes similarly composed of individual entries that looked like this wikitext: awa}} language|Awadhiawa

The sequences produced by this module for Redirect_language_codes always link to languagewhere is the name you would get from the magic word.If language is a redlink, create a redirect. If language points to adisambiguation page, use the override_t table to point to the correct article title.

]=]

require ('strict');

--[=[-------------------------< O V E R R I D E _ T >---------------------------------------------------------- override_t is a k/v table that is used to override the default MediaWiki name. This is useful when the assembled wikilink [[<language-name> language]] points to a disambiguation page.

]=]

local override_t =

----------------------------< A D D I T I O N _ T >----------------------------------------------------------

addition_t is a k/v table of tags and names that aren't found in the MediaWiki list of interwiki prefixen but whichmight be useful.

local addition_t =

----------------------------< F O R M A T _ S T R I N G S _ T >----------------------------------------------

local format_strings_t =

----------------------------< I N T E R W I K I M A P T A B L E S >--------------------------------------

two k/v tables of tag/name and name/tag sequences indexed by the first character of the tag/or name.

local inter_wiki_map_indexed_by_tag_t = ; -- maps of interwiki prefixes that are language-code prefixes indexed by language tag; segregated by tag first letterlocal inter_wiki_map_indexed_by_name_t = ; -- maps of interwiki prefixes that are language-code prefixes indexed by language name; segregated by name first letter

----------------------------< M A P _ A D D >----------------------------------------------------------------

add properly formatted tag/name and name/tag items to the appropriate interiki map sequences.

local function map_add (map, fc, tag, name) if map

'tag' then table.insert (inter_wiki_map_indexed_by_tag_t[fc], string.format (format_strings_t.tag, tag, name, name)); else table.insert (inter_wiki_map_indexed_by_name_t[fc], string.format (format_strings_t.name, name, name, tag)); endend

local this_wiki_code = mw.language.getContentLanguage:getCode; -- get this wiki's language codeif string.match (mw.site.server, 'wikidata') then this_wiki_code = mw.getCurrentFrame:callParserFunction ('int',); -- on Wikidata so use interface language setting insteadend

local mw_languages_by_tag_t = mw.language.fetchLanguageNames (this_wiki_code, 'all'); -- get a table of language tag/name pairs known to Wikimedia; indexed by tag

for k, v in pairs (mw.site.interwikiMap ('local')) do -- spin through the base interwiki map (limited to local) local tag = v["prefix"]; -- get the each site's prefix (the 'en' in 'en.wikipedia.org') local lang_name = override_t[tag] or mw_languages_by_tag_t[tag]; -- get the language name that matches the prefix; nil else; try override_t first if lang_name then -- if the prefix matches a known language tag local tag_fc = tag:match ('^%a'):lower; -- get tag and lang_name first-character for indexing local lang_name_fc = mw.ustring.lower(mw.ustring.match (lang_name, '^%a')); -- mw.ustring for i18n

if not inter_wiki_map_indexed_by_tag_t[tag_fc] then -- if the map doesn't have first-character sequence inter_wiki_map_indexed_by_tag_t[tag_fc] = ; -- add an empty sequence end if not inter_wiki_map_indexed_by_name_t[lang_name_fc] then -- if the map doesn't have first-character sequence inter_wiki_map_indexed_by_name_t[lang_name_fc] = ; -- add an empty sequence end

map_add ('tag', tag_fc, tag, lang_name); -- and add tag and name to the maps map_add ('name', lang_name_fc, tag, lang_name); endend

for tag, lang_name in pairs (addition_t) do local tag_fc = tag:match ('^%a'):lower; -- get tag and lang_name first-character for indexing local lang_name_fc = mw.ustring.lower(mw.ustring.match (lang_name, '^%a')); -- mw.ustring for i18n

map_add ('tag', tag_fc, tag, lang_name); -- and add tag and name to the maps map_add ('name', lang_name_fc, tag, lang_name);end

for key, _ in pairs (inter_wiki_map_indexed_by_tag_t) do -- sort the individual alpha sections table.sort (inter_wiki_map_indexed_by_tag_t[key]);end

for key, _ in pairs (inter_wiki_map_indexed_by_name_t) do -- sort the individual alpha sections table.sort (inter_wiki_map_indexed_by_name_t[key], function (a, b) -- case insensitive sort return mw.ustring.lower (a) < mw.ustring.lower (b); -- mw.ustring for i18n end);end

for key, value_t in pairs (inter_wiki_map_indexed_by_tag_t) do --concatenate sequences and then replace sequences with those strings inter_wiki_map_indexed_by_tag_t[key] = table.concat (inter_wiki_map_indexed_by_tag_t[key]);end

for key, value_t in pairs (inter_wiki_map_indexed_by_name_t) do --concatenate sequences and then replace sequences with those strings inter_wiki_map_indexed_by_name_t[key] = table.concat (inter_wiki_map_indexed_by_name_t[key]);end

----------------------------< E X P O R T S >----------------------------------------------------------------

return