Sari la conținut

Modul:InLocalLangs

De la Wikipedia, enciclopedia liberă

Documentația acestui modul poate fi creată la Modul:InLocalLangs/doc

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

p.getNativeNamesList = function(q)
	local wdId = q or mw.wikibase.getEntityIdForCurrentPage()
	local nativeNameClaims = Wikidata.findClaimsForProperty(wdId, 'P1705')
	if not nativeNameClaims or #nativeNameClaims == 0 then
		nativeNameClaims = Wikidata.findClaimsForProperty(wdId, 'P1559')
	end
	local roLabel = Wikidata.findLabel(wdId)
	local namesByLangs = {}
	if nativeNameClaims and #nativeNameClaims > 0 then
		for _,eachNativeNameClaim in ipairs(nativeNameClaims) do
			if Wikidata.hasValueSnak(eachNativeNameClaim) then
				local lang = eachNativeNameClaim.mainsnak.datavalue.value.language
				local text = eachNativeNameClaim.mainsnak.datavalue.value.text
				if lang ~= 'ro' and roLabel ~= text then
					if not namesByLangs[lang] or (eachNativeNameClaim.rank ~= namesByLangs[lang].rank and Wikidata.hasBetterRank(eachNativeNameClaim, namesByLangs[lang].rank)) then
						namesByLangs[lang] = {}
						namesByLangs[lang].rank = eachNativeNameClaim.rank
						namesByLangs[lang].texts = {}
					end
					if Wikidata.hasBetterRank(eachNativeNameClaim, namesByLangs[lang].rank) then
						table.insert(namesByLangs[lang].texts, text)
					end
				end
			end
		end
	end
	return namesByLangs
end

p.fromArgs = function(q)
	local namesByLangs = p.getNativeNamesList(q)
	local resList = {}
	for eachLang,eachTextSet in pairs(namesByLangs) do
		table.insert(resList, StringUtils._prependIfMissing({mw.getCurrentFrame():expandTemplate{title = eachLang, args = {mw.text.listToText(eachTextSet.texts)} }, 'în '}))
	end
	return mw.text.listToText(resList)
end

p.fromFrame = function(frame)
	local args = getArgs(frame)
	return p.fromArgs(args.q)
end

return p