Nothing Special   »   [go: up one dir, main page]

Sari la conținut

Modul:UATList

De la Wikipedia, enciclopedia liberă

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

local getArgs = require('Modul:Arguments').getArgs
local illWd = require('Modul:Ill-wd').fromArgs
local Wikidata = require('Modul:Wikidata')
local StringUtils = require('Modul:StringUtils')
local TableTools = require('Modul:TableTools')
local formatNum = require('Modul:Formatnum')._formatNum
local encloseString = StringUtils._encloseString
local prependToString = StringUtils._prependToString

local br = tostring(mw.html.create('br'))

local p = {}

local function displayImageFromName(name)
	if name == nil then return '' end
	if string.find(name, ':') == nil then
		return encloseString({name, '[[Fișier:', '|100x100px]]'})
	else
		return encloseString({name, '[[', '|100x100px]]'})
	end
end

p.getUATLine = function(uatData)
	local types = uatData.types and mw.text.listToText(uatData.types, br, br) or ''
	if uatData.link == nil then 
		uatData.link = "[[" .. uatData.article .. "|" .. uatData.name .. "]]"
	end
	--TODO remove this when we have proper params
	uatData.name = uatData.name or uatData.link
	
	local htmlRow = mw.html.create('tr')
		:attr('class', 'uat-list uat')
		:tag('td')
			:attr('class', 'uat-list coa')
			:wikitext(displayImageFromName(uatData.coa))
			:done()
		:tag('td')
			:attr('class', 'uat-list link')
			:attr('data-sort-value', "8_" .. uatData.name)
			:wikitext(encloseString({uatData.link, "'''", "'''"}))
			:done()
		:tag('td')
			:wikitext(types)
			:done()
		:tag('td')
			:attr('class', 'uat-list pop')
			:attr('data-sort-type', 'number')
			:attr('data-sort-value', uatData.population)
			:wikitext(uatData.population and formatNum(uatData.population) or '')
			:done()
		:tag('td')
			:attr('class', 'uat-list image')
			:wikitext(displayImageFromName(uatData.image))
			:allDone()

	return htmlRow
end

p.getSettlementLine = function(uatData)
	local types = mw.text.listToText(uatData.types, br, br)
	if uatData.link == nil then 
		uatData.link = "[[" .. uatData.article .. "|" .. uatData.name .. "]]"
	end
	--TODO remove this when we have proper params
	uatData.name = uatData.name or uatData.link
	
	local htmlRow = mw.html.create('tr')
		:attr('class', 'uat-list settlement')
		:tag('td')
			:attr('class', 'uat-list coa')
			:done() --no CoA for settlements
		:tag('td')
			:attr('class', 'uat-list link')
			:attr('data-sort-value', "9_" .. uatData.name)
			:wikitext(uatData.link)
			:done()
		:tag('td')
			:wikitext(types)
			:done()
		:tag('td')
			:attr('class', 'uat-list pop')
			:attr('data-sort-type', 'number')
			:attr('data-sort-value', uatData.population)
			:wikitext(uatData.population and formatNum(uatData.population) or '')
			:done()
		:tag('td')
			:attr('class', 'uat-list image')
			:wikitext(displayImageFromName(uatData.image))
			:allDone()

	return htmlRow
end

p.displayUATListHeader = function()
	local tr = mw.html.create('tr')
				:tag('th'):attr('class', 'unsortable'):wikitext('Stemă'):done()
				:tag('th'):wikitext('Nume'):done()
				:tag('th'):wikitext('Tip de localitate'):done()
				:tag('th'):wikitext('Populație'):done()
				:tag('th'):attr('class', 'unsortable'):wikitext('Imagine'):allDone()
	return tostring(tr)
end

p.displayUATListFromArgs = function(uats, header)
	local tbl = ""
	local content = {}
	
	for _,eachUat in ipairs(uats) do
		table.insert(content, tostring(p.getUATLine(eachUat)))
		if eachUat.settlements then for _,eachSettlement in ipairs(eachUat.settlements) do
			table.insert(content, tostring(p.getSettlementLine(eachSettlement)))
		end end
	end

	if header then
		tbl = mw.html.create('table'):addClass('wikitable'):addClass('sortable'):css('width', '100%')
		tbl:node(p.displayUATListHeader())
		for _, line in ipairs(content) do tbl:node(line) end
		return tostring(tbl)
	else
		return table.concat(content, "\n", 1, #content) 
	end
end

p.displayUATFromFrame = function(frame)
	local uats = p.parseArgsFromFrame(frame)
	if #uats > 1 then 
		return error("Prea multe localități în listă") 
	end
	return p.displayUATListFromArgs(uats, false)
end

p.displayUATListFromFrame = function(frame)
	local uats = p.parseArgsFromFrame(frame)
	return p.displayUATListFromArgs(uats, true)
end

p.parseArgsFromFrame = function(frame)
	local args = getArgs(frame)
	local uats = {}
	for argName,argValue in pairs(args) do
		local uatNStr, uatProp, uatPropIndexStr, uatPropQual, uatPropQualIndexStr = mw.ustring.match(argName, 'uat(%d*)(%a*)(%d*)(%a*)(%d*)')
		if uatNStr then
			local uatN = tonumber(uatNStr)
			mw.logObject(uatN, limit)
			
			local uatPropIndex = type(uatPropIndexStr) == 'string' and tonumber(uatPropIndexStr) or -1
			local uatPropQualIndex = type(uatPropQualIndexStr) == 'string' and tonumber(uatPropQualIndexStr) or -1
			uats[uatN] = uats[uatN] or {}
			local uat = uats[uatN]
			
 			if uatPropIndex > 0 then
 				uatProp = uatProp .. 's'
 				uat[uatProp] = uat[uatProp] or {}
 				if uatPropQual and uatPropQual ~= '' then
 					uat[uatProp][uatPropIndex] = uat[uatProp][uatPropIndex] or {}
 					if uatPropQualIndex > 0 then
 						uatPropQual = uatPropQual .. 's'
 						uat[uatProp][uatPropIndex][uatPropQual] = uat[uatProp][uatPropIndex][uatPropQual] or {}
 						uat[uatProp][uatPropIndex][uatPropQual][uatPropQualIndex] = argValue
 					else
 						uat[uatProp][uatPropIndex][uatPropQual] = argValue
 					end
 				else
 					uat[uatProp][uatPropIndex] = argValue
 				end
 			else
 				uat[uatProp] = argValue
 			end
		end
	end
	return TableTools.compressSparseArray(uats)
end

return p