وحدة:Ill-wd

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث
local getArgs = require('وحدة:Arguments').getArgs
local wikidata = require('وحدة:Wikidata')
local StringUtils = require('وحدة: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 wikidata.findLabel(wdId)
		if wdLabel and capitalize then wdLabel = StringUtils._capitalize({ wdLabel }) end
		wdArticleName = wdArticleName or wdLabel
		if not wdArticleName then error('Missing label for item ' .. wdId) 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 wdLabel = array[3] or mw.wikibase.label(wdId) or wikidata.findLabel(wdId, 'ro')
	local wdArticleName = array[2] or mw.wikibase.label(wdId) or wikidata.findLabel(wdId, 'ro')
	local capitalize = array['capitalize']

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

return p