Modul:SisterProjectLink

Tato stránka je zamčena
Z Wikipedie, otevřené encyklopedie

Tento modul se používá pro vytváření odkazů na sesterské projekty. Jak přesně funguje, se můžete ptát třeba jeho tvůrce Danny B.


-- @brief
--  Backend for sister projects linking templates.
-- 
-- @author
--  [[meta:User:Danny B.]]
local _module = {}
----------------------------------------


local Error = require( "Module:Error" )
local WikimediaProjects = require( "Module:WikimediaProjects" )


function inTable ( val, tbl )
	for _, value in pairs( tbl ) do
		if ( value == val ) then
			return true
		end
	end
	return false
end


local Params = {
	["autor"] = { textPrefix = "Autor ", linkPrefix = "Autor:" },
	["článek"] = { textPrefix = "Encyklopedický článek " },
	["dílo"] = { textPrefix = "Dílo " },
	["edice"] = { textPrefix = "Edice ", linkPrefix = "Edice:" },
	["galerie"] = { textPrefix = "Galerie " },
	["heslo"] = { textPrefix = "Slovníkové heslo " },
	["kategorie"] = { textPrefix = "Kategorie ", linkPrefix = "Category:" },
	["kniha"] = { textPrefix = "Kniha " },
	["kurs"] = { textPrefix = "Výukový kurs " },
	["osoba"] = { textPrefix = "Osoba " },
	["portál"] = { textPrefix = "Portál ", linkPrefix = "Portál:" },
	["průvodce"] = { textPrefix = "Průvodce " },
	["příloha"] = { textPrefix = "Příloha ", linkPrefix = "Příloha:" },
	["rozcestník"] = { textPrefix = "Rozcestník " },
	["taxon"] = { textPrefix = "Taxon " },
	["téma"] = { textPrefix = "Téma " },
	["úložiště"] = { textPrefix = "Úložiště vzdělávacích materiálů " },
	["zpráva"] = { textPrefix = "Zpráva " }
}

local ParamLists = {
	["commons"] = { "galerie", "kategorie" },
	["wikibooks"] = { "kategorie", "kniha" },
	["wikinews"] = { "kategorie", "zpráva" },
	["wikipedia"] = { "článek", "kategorie", "portál", "rozcestník" },
	["wikiquote"] = { "dílo", "kategorie", "osoba", "téma" },
	["wikisource"] = { "autor", "dílo", "kategorie", "rozcestník", "edice" },
	["wikispecies"] = { "kategorie", "taxon" },
	["wikiversity"] = { "kategorie", "kurs", "úložiště" },
	["wikivoyage"] = { "kategorie", "průvodce" },
	["wiktionary"] = { "heslo", "kategorie", "příloha" }
}


function _module.print( frame )
	
	local output = ""
	local parentFrame = frame:getParent()
	local moduleArgs = frame.args
	local templateArgs = {}
	
	local project = mw.text.trim( moduleArgs["1"] or "" )
	-- TODO: tohle je dost ošklivý side-effect
	if project == "wikiquote" then
		Params["dílo"].linkPrefix = "Dílo:"
	else
		Params["dílo"].linkPrefix = nil
	end
	
	if project == "" then
		
		error( "Missing 1. parameter (project name)" )
		
	elseif not WikimediaProjects[project] then
		
		error( "Unknown value „" .. project .. "“ of 1. parameter (project name)" )
		
	else
		
		local template = mw.text.trim( moduleArgs["2"] or "" )
		local errorData = { template = template == "" and WikimediaProjects[project].i18n.cs.name or template }
		local targetType
		local target
		
		local paramCount = 0
		local paramList = table.concat( ParamLists[project], "“, „" )
		
		
		for param, value in pairs( parentFrame.args ) do
			paramCount = paramCount + 1
			templateArgs[param] = mw.text.trim( value or "" )
		end
		
		if paramCount == 0 then
			errorData.text = "Chybí některý z parametrů „" .. paramList .. "“."
			errorData.category = "Přidat chybějící parametr"
			output = output .. Error.getText( errorData )
		elseif paramCount > 1 then
			errorData.text = "Příliš mnoho parametrů."
			errorData.category = "Upravit počet parametrů"
			output = output .. Error.getText( errorData )
		else
			targetType, target = next( templateArgs )
			if targetType == 1 then
				errorData.text = "Nespecifikovaný typ odkazu. Použijte některý z parametrů „" .. paramList .. "“."
				errorData.category = "Specifikovat cíl odkazu"
				output = output .. Error.getText( errorData )
			elseif not inTable( targetType, ParamLists[project] ) then
				errorData.text = "Neznámý parametr „" .. targetType .. "“ (možné parametry: „" .. paramList .. "“)."
				errorData.category = "Opravit parametr"
				output = output .. Error.getText( errorData )
			end
		end
		
		if output == "" then
			output = output .. "<span class=\"sisterproject sisterproject-" .. project .."\">"
			output = output .. "<span class=\"sisterproject_image\">[[Soubor:" .. WikimediaProjects[project].common.logo .. "|16x16px|alt=|link=]]</span> "
			output = output .. "<span class=\"sisterproject_text\">"
			output = Params[targetType].textPrefix and output .. "<span class=\"sisterproject_text_prefix\">" .. Params[targetType].textPrefix .. "</span>" or output
			output = output .. "<span class=\"sisterproject_text_target\">[[" .. WikimediaProjects[project].common.iwPrefix .. ":" .. ( Params[targetType].linkPrefix or "" ) .. target .. "|" .. target .. "]]</span>"
			output = output .. "<span class=\"sisterproject_text_suffix\">" .. ( Params[targetType].textSuffix or "" ) .. " " .. WikimediaProjects[project].i18n.cs.where .. "</span>"
			output = output .. "</span>"
			output = output .. "</span>"
		end
		
	end
	
	output = frame:preprocess( output )
	
	return output
	
end


----------------------------------------
return _module