Modul:Folketall

Fra Wikipedia, den frie encyklopedi
Moduldokumentasjon
local p = {}

function dump( out )
    if type( out ) == 'table' then
        local s = '{ '
        for k,v in pairs( out ) do
                if type( k ) ~= 'number' then k = '"'..k..'"' end
                s = s .. '['..k..'] = ' .. dump( v ) .. ','
        end
        return s .. '} '
    else
        return tostring( out )
    end
end
local lang = mw.language.getContentLanguage()

local function normalizeDate(timestamp)
	local success, date = pcall(lang.formatDate, lang, 'Y-m-d', timestamp)
	return success and date
end

local function norskDato(timestamp)
	local success, date = pcall(lang.formatDate, lang, 'j. F Y', timestamp)
	return success and date
end
local function year(timestamp)
    local success, date = pcall(lang.formatDate, lang, 'Y', timestamp)
	return success and date
end
local function getfolketall(frame)
	if not mw.wikibase then
        return ""
    end
    local artikkel = mw.wikibase.getEntityObject()
    if not artikkel then
        return ""
    end
    local claims = artikkel.claims
    if not claims then
        return ""
    end
    return claims.P1082
end

local function aar(pv)
	if not pv.qualifiers then
		return "0"
	end
	local date =	year(pv.qualifiers["P585"][1]["datavalue"]["value"]["time"])
	if not date then
		date = "0"
	end
	return date
end
local function aar2(pv)
	if not pv.qualifiers then
		return "0"
	end
	local date = year(pv.qualifiers["P585"][1]["datavalue"]["value"]["time"])
	if not date then
		date = "0"
	end
	return date
end

local function sortdate(pv)
	if not pv.qualifiers then
		return "0"
	end
	local date = normalizeDate(pv.qualifiers["P585"][1]["datavalue"]["value"]["time"])
	if not date then
		date = "0"
	end
	return date
end
local function sortdate2(pv)
	if not pv.qualifiers then
		return "0"
	end
	local date = normalizeDate(pv.qualifiers["P585"][1]["datavalue"]["value"]["time"])
	if not date then
		date = "0"
	end
	return date
end

local function dato(pv)
	if not pv.qualifiers then
		return "0"
	end
	local date = 	norskDato(pv.qualifiers["P585"][1]["datavalue"]["value"]["time"])
	if not date then
		date = "0"
	end
	return date
end

local function single(pv)
	local aaret = aar(pv)
	if aaret == "0" then
		return ""
	end
	local folketall = tonumber(pv.mainsnak.datavalue.value["amount"])
	return '{"x": ' .. aaret .. ',  "y": ' .. folketall .. '}'
end

local function single2(pv)
	local aaret = sortdate2(pv)
	if aaret == "0" then
		return ""
	end
	local folketall = tonumber(pv.mainsnak.datavalue.value["amount"])
	return '{"x": ' .. aaret .. ',  "y": ' .. folketall .. '}'
end



local function less(a,b)
	return sortdate(a) < sortdate(b)
end

local function hentSortertFolketall(frame)
    local pv = getfolketall(frame)
    if not pv then
        return pv
    end
    table.sort(pv,less)
    return pv
end


local function gettallogdato(frame)
    local pv = hentSortertFolketall(frame)
    if not pv then
        return ""
    end
    
    local i = 1
    local resultat = single(pv[i])
    local first = string.len(resultat)==0
    while i < #pv do
    	i = i+1
    	local lres = single(pv[i])
    	if string.len(lres) > 0 then
    		if not first then
    			resultat = resultat .. ", "
    		end
    	    resultat = resultat .. lres
    	    first = false;
    	end
	end
    return resultat
end

function p.folketallgraph(frame)
    return '{' ..
 ' "width": 800, ' ..
 '  "height": 200, ' .. 
 ' "padding": {"top": 20, "left": 100, "bottom": 30, "right": 10}, ' .. 
 ' "data": [ ' .. 
 '   { ' .. 
 '     "name": "table", ' .. 
 '     "values": [' ..
       gettallogdato(frame) ..
 '     	    ]' .. 
 '   }' .. 
 ' ],' .. 
 ' "scales": [' .. 
 '   {' .. 
 '     "name": "x",' .. 
 '     "type": "ordinal",' .. 
 '     "range": "width",' .. 
 '     "domain": {"data": "table", "field": "data.x"}' .. 
 '   },' .. 
 '   {' .. 
 '     "name": "y",' .. 
 '     "range": "height",' .. 
 '     "nice": true,' .. 
 '     "domain": {"data": "table", "field": "data.y"}' .. 
 '   }' .. 
 ' ],' .. 
 ' "axes": [' .. 
 '   {"type": "x", "scale": "x", ' ..
 '      "properties": { ' ..
 '         "labels": { "angle": {"value": 45}} ' ..
 '      } ' ..
 '    },' .. 
 '   {"type": "y", "scale": "y"}' .. 
 ' ],' .. 
 ' "marks": [' .. 
 '   { ' .. 
 '     "type": "rect", ' .. 
 '     "from": {"data": "table"}, ' .. 
 '     "properties": { ' .. 
 '       "enter": { ' .. 
 '         "x": {"scale": "x", "field": "data.x"}, ' .. 
 '         "width": {"scale": "x", "band": true, "offset": -5}, ' .. 
 '         "y": {"scale": "y", "field": "data.y"}, ' .. 
 '         "y2": {"scale": "y", "value": 5} ' .. 
 '       }, ' .. 
 '       "update": { ' .. 
 '         "fill": {"value": "steelblue"} ' .. 
 '       }, ' .. 
 '       "hover": { ' .. 
 '         "fill": {"value": "red"} ' .. 
 '       } ' .. 
 '     } ' .. 
 '   } ' .. 
 ' ] ' .. 
'} '

end

function p.nyeste_folketall_og_dato(frame)
    local pv = hentSortertFolketall(frame)
    if not pv then
        return ""
    end
	local datoen = dato(pv[#pv])
	if datoen == "0" then
		return ""
	end
	local folketall = tonumber(pv[#pv].mainsnak.datavalue.value["amount"])
	return folketall .. ' (' .. datoen .. ')'

end

function p.nyeste_folketall(frame)
    local pv = hentSortertFolketall(frame)
    if not pv then
        return ""
    end
	local folketall = tonumber(pv[#pv].mainsnak.datavalue.value["amount"])
		return folketall

end

function p.alleTall(frame)
	local pv = getfolketall(frame)
    if not pv then
        return ""
    end
    local i = 1
    local resultat = single2(pv[i])
    local first = string.len(resultat)==0
    while i < #pv do
    	i = i+1
    	local lres = single2(pv[i])
    	if string.len(lres) > 0 then
    		if not first then
    			resultat = resultat .. ", "
    		end
    	    resultat = resultat .. lres
    	    first = false;
    	end
	end
    return resultat
end

return p