Mine sisu juurde

Moodul:Mall:Provints

Allikas: Vikipeedia

Selle mooduli dokumentatsiooni saab kirjutada asukohta Moodul:Mall:Provints/doc.

local wdLoadSuccess, mdlWikidata = pcall(require, "Moodul:Wikidata")
assert (wdLoadSuccess, "Wikidata mooduli laadimine ebaõnnestus.")

local p = {}

local function precision_format(num)
	local prFormat = require("Moodul:Math")._precision_format
	precision = num > 100 and 0 or num < 1 and 2 or 1
	return prFormat(num, precision)
end

local function population(frame)
	local wdParameters = {}
	numPop = ""

	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = "P1082"
	numPop = mdlWikidata.claim(wdParameters) or ""
    
	return numPop
end

function p.area(frame)
	local header = frame.args["header"]
	local getfullstr = frame.args["getfullstr"]
	local wdParameters = {}
	strOutput = ""
	numArea = ""
	strDate = ""
	strRefs = ""
	areaPID = "P2046"

	wdParameters["args"] = nil
	wdParameters["args"] = {}
	wdParameters["args"][1] = areaPID
	numArea = mdlWikidata.claim(wdParameters) or ""

    -- kehtivusosa
    wdParameters["args"]["qualifier"] = "P518"
    wdParameters["args"]["parameter"] = "numeric-id"
    strAppliesTo = mdlWikidata.claim(wdParameters) or nil
    wdParameters["args"]["qualifier"] = nil
    wdParameters["args"]["parameter"] = nil
    if strAppliesTo and tonumber(strAppliesTo) ~= 16868672 then
    	wdParameters["args"][2] = "P518"
    	wdParameters["args"][3] = "Q16868672"
    	numArea = mdlWikidata.claim(wdParameters) or ""
    end
    
    -- unit ha or km2
    wdParameters["args"]["parameter"] = "unit"
    strUnit = mdlWikidata.claim(wdParameters) or ""
    wdParameters["args"]["parameter"] = nil
    if numArea and strUnit == "Q35852" then
    	numArea = numArea / 100
    elseif strUnit ~= "Q712226" then
      	return ""
    end
    
    if header or getfullstr then
    	if header then header = header .. " " else header = "" end

	    -- date
	    wdParameters["args"]["qualifier"] = "P585"
	    wdParameters["args"]["parameter"] = "time:Y"
	    strDate = mdlWikidata.claim(wdParameters) or nil
	    wdParameters["args"]["qualifier"] = nil
	    wdParameters["args"]["parameter"] = nil
	    if strDate then
	    	strDate = " <span style=\"font-size:88%\">(" .. strDate .. ")</span>"
	    else
	    	strDate = ""
	    end
	    
	    -- refs
	    wdParameters["args"]["references"] = "only"
	    strRefs = mdlWikidata.claim(wdParameters) or ""
	    local showIcon = require("Moodul:EditAtWikidata").showMessage
	    local iconParameters = {}
	    iconParameters["args"] = nil
		iconParameters["args"] = {}
		iconParameters["args"]["pid"] = areaPID
	    strOutput = header .. precision_format(numArea) .. " km²" .. strDate .. strRefs .. showIcon(iconParameters)
    else
    	strOutput = numArea
    end

	return strOutput
end

function p.popDensity( frame )
	local pop = tonumber(frame.args["pop"])
	local area = mw.language.getContentLanguage():parseFormattedNumber( frame.args["area"] )
	local header = frame.args["header"]
	popDensity = ""
	
	if ( (string.len(frame.args["pop"]) > 0 and not pop)
		or (string.len(frame.args["area"]) > 0 and not area) ) then
		    errorStr = "<span style=\"color:red\">Kontrolli pindala ja rahvaarvu väärtust ([[Mall:Provints#cite note-tihedus-2|?]])</span>[[Kategooria:Parameetrite vead]]"
		    return errorStr
	end
	frame.args = {}
	
	if not pop then
		pop = tonumber( population(frame) )
	end
	
	if not area then
		area = tonumber( p.area(frame) )
	end
	if pop and area then
		popDensity = pop / area > 0.01 and precision_format(pop / area) or '< 1'
		if header then
			popDensity = header .. " " .. popDensity .. " in/km²"
		end
	end
	
	return popDensity
end

return p