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

--[=[

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 Template_index/Redirect_language_codes and in Template_index/Redirect_language_codes_–_code_sort. Before this module was developed, Redirect_language_codes_–_code_sort was composed of individual entries that looked like this wikitext: ab – Abkhaz and Redirect_language_codes similarly composed of individual entries that looked like this wikitext: Awadhi – awa

The sequences produced by this module for Redirect_language_codes always link to  language where  is the name you would get from the magic word. If  language is a redlink, create a redirect. If  language points to a disambiguation 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 points to a disambiguation page.

]=]

local override_t = { ['bo'] = 'Lhasa Tibetan',													-- the language of bo.wiki; see List of Wikipedias }

--[[--< 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 which might be useful.

]]

local addition_t = { ['und'] = 'undetermined',													-- a language tag but not a prefix; included in the original wikitext list pages }

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

local format_strings_t = { tag = '\n*%s – %s',											-- new line must precede * for unordered markup to work name = '\n*%s – %s' }

--[[--< 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 letter local 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)); end end

----< B U I L D  T A B L E S >--

local this_wiki_code = mw.language.getContentLanguage:getCode;				-- get this wiki's language code if string.match (mw.site.server, 'wikidata') then this_wiki_code = mw.getCurrentFrame:callParserFunction ('int', {'lang'});	-- on Wikidata so use interface language setting instead end

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); end end

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 { inter_wiki_map_indexed_by_tag_t = inter_wiki_map_indexed_by_tag_t, inter_wiki_map_indexed_by_name_t = inter_wiki_map_indexed_by_name_t, }