Documentation for this module may be created at Module:Sandbox/trappist the monk/table of cs1 modules at other wikis/doc

require('strict');

local get_args = require ('Module:Arguments').getArgs;
local mw_languages_by_tag_t = mw.language.fetchLanguageNames ('en', 'all');
local interwiki_map = {															-- from Help:Interwiki linking
	['commons'] = 'c:',
	['wiki'] = ':',																-- wikipedia so just the colon
	['wikibooks'] = ':b:',
	['wikinews'] = ':n:',
	['wikiquote'] = ':q:',
	['wikisource'] = ':s:',
	['wikispecies'] = ':species:',
	['wikiversity'] = ':v:',
	['wikivoyage'] = ':voy:',
	['wiktionary'] = ':wikt:',
	}


--[[--------------------------< M A I N >----------------------------------------------------------------------
]]

local function main (frame)
	local args_t = get_args (frame);
	local qid = mw.wikibase.getEntityIdForTitle (args_t[1]);					-- get the qid for this module; nil else
	if not qid then
		return '<span style="color:#d33">no qid for ' .. args_t[1] .. '</span>';	-- no qid then abandon
	end
	
	local sitelinks_t = {};
	local wd_sitelinks_t = mw.wikibase.getEntity (qid)["sitelinks"];
	
	for interwiki, wd_sitelink_t in pairs (wd_sitelinks_t) do
		local site_info_t = {};
		local lang_tag = wd_sitelink_t['site']:match ('([%a_]+)wik'):gsub ('_', '%-');	-- language tag; underscores replaces with hyphens
		local lang_name = 'no language';
			if mw_languages_by_tag_t[lang_tag] then
				lang_name = mw_languages_by_tag_t[lang_tag] .. ' (' .. lang_tag .. ')';	-- <lang_name> (<lang_tag>)
			end
		table.insert (site_info_t, lang_name);

		local project = wd_sitelink_t['site']:match ('[%a_]+(wik.*)');
		project = ('wiki' == project) and 'wikipedia' or project;
		project = project:gsub ('^(%a)', string.upper);							-- first letter uppercase
		table.insert (site_info_t, project);									-- wikipedia, wikisource, wikiversity, etc

		interwiki = interwiki:match ('%a+(wik.*)');								-- get the project
		interwiki = interwiki_map[interwiki];									-- map to shorthand
		table.insert (site_info_t, '[[:' .. lang_tag .. interwiki .. wd_sitelink_t['title'] ..']]');	-- make an interwikilink
		
		sitelinks_t[wd_sitelink_t['site']] = site_info_t;						-- add these data to sitelinks_t
	end
	
	local row_data_t = {};														-- a sequence table of row data extracted from sitelinks_t
	for _, sitelink_t in pairs (sitelinks_t) do
		local row_t = {};														-- initialize
		for _, item in ipairs (sitelink_t) do
			table.insert (row_t, item);
		end
		local row = '| ' .. table.concat (row_t, ' || ') .. ' || || ||';		-- build wikitable row markup
		table.insert (row_data_t, row);											-- and save the row
	end
	table.sort (row_data_t);													-- ascending sort by language name
	local out = table.concat (row_data_t, '\n|-\n');							-- make a big string with more wikitable markup; next assemble caption, headings, and row data into finished table
	out = '{| class="wikitable sortable"\n|+ ' .. args_t[1] .. ' use at other projects\n! Language !! Type !! class="unsortable" | Link !! Date !! Maintainer !! class="unsortable" | Comments\n|-\n' .. out .. '\n|}'
	return out;																	-- done
end


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

return {
	main = main,
	}