Modulo:Demografia: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
refactoring e raffinamento calcolo default
Moroboshi (discussione | contributi)
fix recupero dati da wikidata
Riga 10:
end
 
-- =============================================================
-- Funzioni copiate da Modulo:Wikidata
-- =============================================================
 
-- Messaggi di errore
local i18n = {
["errors"] = {
["entityid-param-not-provided"] = "Parametro ''entityid'' non fornito",
["property-param-not-provided"] = "Parametro ''property'' non fornito",
["qualifier-param-not-provided"] = "Parametro ''qualifier'' non fornito",
["value-param-not-provided"] = "Parametro ''valore'' da ricercare non fornito",
["entity-not-found"] = "Entità non trovata",
["unknown-claim-type"] = "Tipo asserzione sconosciuta",
["unknown-snak-type"] = "Tipo di snak sconosciuto",
["unknown-datavalue-type"] = "Tipo di dato sconosciuto",
["unknown-entity-type"] = "Tipo di entità sconosciuta"
},
["somevalue"] = "''valore sconosciuto''",
["novalue"] = "''nessun valore''"
}
 
-------------------------------------------------------------------------------
-- Formatters
-------------------------------------------------------------------------------
 
local function errhandler(msg)
local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
return string.format('<span class="error">%s</span>%s', msg, cat)
end
 
local function formatExtLink(url)
local protocols = { ftp = true, http = true, https = true }
 
local success, uri = pcall(function() return mw.uri.new(url) end)
if success and uri.protocol and protocols[uri.protocol] then
local dest = tostring(uri)
return string.format('[%s %s]', dest, dest:gsub(uri.protocol .. '://', ''))
else
return url
end
end
 
local function formatEntityId(entityId)
local label = mw.wikibase.label(entityId)
local link = mw.wikibase.sitelink(entityId)
if link then
if label then
return '[[' .. link .. '|' .. label .. ']]'
else
return '[[' .. link .. ']]'
end
else
return label or ''
end
end
 
local function formatTime(value, args)
local year, month, day
local ret = ''
year, month, day = value.time:match('(%d+)%-(%d%d)%-(%d%d).+')
if value.precision == 9 then
ret = tonumber(year)
elseif value.precision == 10 then
ret = mw.getLanguage('it'):formatDate('F Y', tonumber(year) .. '-' .. month)
elseif value.precision == 11 then
ret = mw.getLanguage('it'):formatDate('j F Y', tonumber(year) .. '-' .. month .. '-' .. day)
ret = ret:gsub('^1%s', '1º ')
end
if value.precision >= 9 and value.precision <= 11 then
ret = ret .. (value.time:sub(1, 1) == '-' and ' a.C.' or '')
end
 
return ret
end
 
local function formatGlobecoordinate(value, args)
local ret
if args.formatting == 'latitude' then
ret = value.latitude
elseif args.formatting == 'longitude' then
ret = value.longitude
else
ret = value.latitude .. ', ' .. value.longitude
end
return ret
end
 
local function formatFromPattern(str, args)
local pattern = args.pattern
pattern = mw.ustring.gsub(pattern, '\\{', '{')
pattern = mw.ustring.gsub(pattern, '\\}', '}')
return mw.getCurrentFrame():preprocess(mw.message.newRawMessage(pattern, str):plain())
end
 
local function getEntityIdFromValue(value)
local prefix = ''
if value['entity-type'] == 'item' then
prefix = 'Q'
elseif value['entity-type'] == 'property' then
prefix = 'P'
else
error(i18n.errors['unknown-entity-type'])
end
return prefix .. value['numeric-id']
end
 
local function formatDatavalue(datavalue, args)
local ret
 
--Default formatters
if datavalue.type == 'wikibase-entityid' then
if args.formatting == 'raw' then
ret = getEntityIdFromValue(datavalue.value)
else
ret = formatEntityId(getEntityIdFromValue(datavalue.value))
end
elseif datavalue.type == 'string' then
ret = datavalue.value
if args.formatting == 'extlink' then
ret = formatExtLink(ret)
end
elseif datavalue.type == 'monolingualtext' then
ret = datavalue.value.text
elseif datavalue.type == 'time' then
if args.formatting == 'raw' then
ret = datavalue.value.time
else
ret = formatTime(datavalue.value, args)
end
elseif datavalue.type == 'globecoordinate' then
ret = formatGlobecoordinate(datavalue.value, args)
elseif datavalue.type == 'quantity' then
ret = tonumber(datavalue.value.amount)
else
error(i18n.errors['unknown-datavalue-type'])
end
 
return ret
end
 
local function formatSnak(snak, args)
if snak.snaktype == 'somevalue' then
return i18n['somevalue']
elseif snak.snaktype == 'novalue' then
return i18n['novalue']
elseif snak.snaktype == 'value' then
return formatDatavalue(snak.datavalue, args)
else
error(i18n.errors['unknown-snak-type'])
end
end
 
-- =============================================================
-- Fine Funzioni copiate da Modulo:Wikidata
-- =============================================================
local function max_array(array)
local max = 0
Riga 48 ⟶ 204:
 
local function get_wikidata(from)
local P585 = 'P585'
local populations_raw = mWikidata._getProperty({'P1082', from=from, rank='normal'}, true)
local populations_reverseP1082 = {}'P1082'
local entity = mw.wikibase.getEntityObject(from)
local claims
if entity.claims and entity.claims.P1082 and #entity.claims.P1082 > 0 then
claims = entity.claims.P1082
else
return nil
end
--if true then return mw.text.jsonEncode(claims[1],mw.text.JSON_PRETTY) end
local years = {}
local populations = {}
for i =1, #populations_raw do -- per il momento inserisco come anni una sequenza numerica
for _,claim in ipairs(claims) do
populations_reverse[i] = populations_raw[#populations_raw-i+1]
if claim.qualifiers and claim.qualifiers.P585 and (claim.rank == 'preferred' or claim.rank == 'normal') then
years[i] = i
--if true then return claim end
years[#years+1] = formatSnak(claim.qualifiers.P585[1] , {})
populations[#populations+1] = formatSnak(claim.mainsnak, {})
end
end
for i = 1, math.floor(#populations/2) do
return years,populations_reverse
local j=#populations-i+1
years[i], years[j] = years[j], years[i]
populations[i], populations[j] = populations[j], populations[i]
end
return years, populations
end
 
 
local function default_table(popmax)
local function default_table(popmax, n)
local default = {}
local log_p = math.ceil(math.log10(popmax))
Riga 76 ⟶ 250:
default.passo2 = 2 * 10 ^ (log_p-2)
end
default.dimx = 488
if n > 15 then default.dimx = 488 + (n-15) * 20 end
return default
end
Riga 86 ⟶ 262:
from_wikidata = true
end
--if true then return mw.text.jsonEncode(years) end
if #years == 0 then return '' end
local popmax = max_array(populations)
Riga 97 ⟶ 274:
end
-- calcolo default per la popolazione
local default = default_table(popmax, #populations)
-- generazione del grafico
local graph = {}
dump(graph, 'Colors=\n id:lightgrey value:gray(0.9)\n id:darkgrey value:gray(0.7)\n id:sfondo value:rgb(1,1,1)\n id:barra value:rgb(0.6,0.7,0.8)\n\n')
dump(graph, 'ImageSize = width:', args.dimx or '488'tostring(default.dimx), ' height:', args.dmy or '373', '\n')
dump(graph, 'PlotArea = left:50 bottom:50 top:30 right:30\nDateFormat = x.y\n')
dump(graph, 'Period = from:0 till:', args.popmax or tostring(default.popmax), '\n')