Sari la conținut

Modul:Ill-wd

Documentația acestui modul poate fi creată la Modul:Ill-wd/doc

local getArgs = require('Modul:Arguments').getArgs
local wikidata = require('Modul:Wikidata')
local StringUtils = require('Modul:StringUtils')
local p = {}

p.fromArgs = function(wdId, wdLabel, wdArticleName, capitalize)
	local out = ''
	if mw.wikibase.sitelink(wdId) then
		if wdLabel then
			out = wikidata.findLinkToItemWithLabel(wdId, wdLabel)
		else
			local label = mw.wikibase.label(StringUtils._prependIfMissing({tostring(wdId), 'Q'}))
			if label and capitalize then label = StringUtils._capitalize({ label }) end
			out = label and wikidata.findLinkToItemWithLabel(wdId, label) or wikidata.findLinkToItem(wdId, capitalize, false, false)
		end
	else
		wdLabel = wdLabel or mw.wikibase.label(wdId) or wdArticleName or wikidata.findLabel(wdId)
		if wdLabel and capitalize then wdLabel = StringUtils._capitalize({ wdLabel }) end
		
		if not wdArticleName then
			local wdLangLabel, wdLangName = mw.wikibase.getLabelWithLang(wdId)
			if wdLangName == 'ro' then wdArticleName = wdLangLabel end
		end
		
		wdArticleName = wdArticleName or wdLabel

		if not wdArticleName then 
			mw.logObject(wdId, 'Missing label for item')
			return wikidata.findLinkToItem(wdId, capitalize, false, false)
		end
		out = out .. '[[:' .. wdArticleName
		if wdArticleName ~= wdLabel then 
			out = out .. '|' .. (wdLabel or wdArticleName)
		end
		out = out .. ']]'
	
		local wikidataLinkSpan = mw.html.create('span'):attr('title', wdArticleName .. ' la Wikidata'):wikitext('d')
		local supTag = mw.html.create('sup'):tag('small'):wikitext(mw.ustring.char(0x2060), '([[:d:', wdId, '|', tostring(wikidataLinkSpan), ']])'):allDone()
		out = out .. tostring(supTag)
	end
	return out
end

p.fromArray = function(array)
	local wdId = StringUtils.prependIfMissing({array[1] or wikidata.getEntityId(), 'Q'})
	local inLabel = array[3]
	local labelLang = 'ro'
	if not inLabel then
		wdLabel, labelLang = mw.wikibase.getLabelWithLang(wdId)
	end
	local wdArticleName = array[2] or wdLabel
	
	if not wdLabel or labelLang ~= 'ro' then
		wdLabel = wdArticleName or wdLabel or wikidata.findLabel(wdId, 'ro')
	end
	
	local capitalize = array['capitalize']

	return p.fromArgs(wdId, inLabel or wdLabel, wdArticleName, capitalize ~= nil)
end
p.fromFrame = function(frame)
	local args = getArgs(frame)
	return p.fromArray(args)
end

return p