Mòdul:ILinkFromWDItem

Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul ILinkFromWDItem (codi · ús · discussió · proves · tests · casos prova | subpàgines · enllaços)

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Conforma {{ILinkFromWDItem}}

local p = {}
local SA = require "Module:SimpleArgs"

local function uc_first(word)
	return mw.ustring.upper(mw.ustring.sub(word, 1, 1)) .. mw.ustring.sub(word, 2)
end

function p.PLink (PId, UserLang)
	return require("Module:Wikidades").getLabel({PId, ['lang']=UserLang}) or ''
end

function p.Link (QId, Upper, UserLang, InOtherLang)
	
	function PerhapsUpper (S)
		if Upper == true then
			return uc_first(S)
		else	
			return S
		end	
	end	
	
	function QLink()
		return '[[:wikidata:'..QId..'|'..QId..']]'
	end	
	
	if string.sub (QId, 1, 1) == 'P' then
		 return p.PLink (QId, UserLang)
	end	
	if UserLang == InOtherLang then
		InOtherLang = ''
	end	
	local SiteLang = mw.language.getContentLanguage().code or ''
	local UserLangIsSiteLang = UserLang == SiteLang
	local UserLangInOtherLang = UserLang == InOtherLang
	local label = mw.wikibase.getLabelByLang (QId, SiteLang) or ''
	local labelX = ''
	local DiffLabels = false
	if not UserLangIsSiteLang then
		labelX = mw.wikibase.getLabelByLang (QId, UserLang) or ''
		if (labelX == '') and (InOtherLang ~= '') then 
			error ('A')
			labelX = mw.wikibase.getLabelByLang (QId, InOtherLang) or ''
			DiffLabels = labelX ~= label 
		end	
		label = labelX
	elseif (label == '') and (not UserLangInOtherLang) and (InOtherLang ~= '') then
		labelX = mw.wikibase.getLabelByLang (QId, InOtherLang) or ''
		DiffLabels = labelX ~= label 
		label = labelX
	end	
	if label == '' then
		return QLink()
	else
		local link = mw.wikibase.getSitelink (QId) or ''
		local DiffLinks = false
		if (link == '') and (not UserLangIsSiteLang) then
			link = mw.wikibase.getSitelink (QId, UserLang..'wiki') or ''
			if link == '' then
				if (not UserLangInOtherLang) and (InOtherLang ~= '') then
					DiffLinks = true
					link = mw.wikibase.getSitelink (QId, InOtherLang..'wiki') or ''
					if link ~= '' then
						link = ":"..InOtherLang..":"..link
					else	
						return QLink()
					end
				else
					return QLink()
				end	
			else
				link = ":"..UserLang..":"..link	
			end
		elseif (link == '') and (not UserLangInOtherLang) and (InOtherLang ~= '') then
			DiffLinks = true
			link = mw.wikibase.getSitelink (QId, InOtherLang..'wiki') or ''
			if link ~= '' then
				link = ":"..InOtherLang..":"..link
			end
		end
		if link == '' then
			if DiffLabels then
				return "''"..PerhapsUpper(label).."''"
			else
				return PerhapsUpper(label)
			end	
		else
			label = PerhapsUpper(label)
			if DiffLabels or DiffLinks then
				label = "''"..label.."''"
			end	
			if link ~= label then
				link = link..'|'..label
			end	
			return '[['..link..']]'
		end
	end
end	--ILinkFromWDItem

function p.ILinkFromWDItem (frame)
	local args = SA.GetArgs (frame)
	local S = SA.Str_Par (args, 1, '') 
	local InOtherLang = SA.Str_Par (args, 2, '') 
	local UpperFirst = SA.Bool_Par (args, 3, true)
	local UserLang = frame:callParserFunction('Int', 'Lang')
	if S == '' then
		error ('What?', 0)
	else	
		return p.Link (S, UpperFirst, UserLang, InOtherLang)
	end	
end --ILinkFromWDItem

return p