Indicacións de uso do módulo

Aínda non se redactaron as instrucións sobre como usar este módulo.
Engadir a explicación sobre o seu uso.

Os editores poden probar cambios no mesmo en Módulo:Concello/probas.
Por favor, engade as categorías na subpáxina de documentación e os interwikis no Wikidata. Ver as subpáxinas deste módulo.
--[[*********************************************************************************
    * Módulo para traballar con datos de Wikidata sobre concellos.
    *********************************************************************************`-- ]]

local module = {}
local wikidata = require('Módulo:Wikidata')
local wikidata_data = require('Módulo:Wikidata/Data')
local gl = mw.language.new('gl')


-- Funcións de depuración (de http://lua-users.org/wiki/TableUtils)

-- function table.val_to_str ( v )
--   if "string" == type( v ) then
--     v = string.gsub( v, "\n", "\\n" )
--     if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
--       return "'" .. v .. "'"
--     end
--     return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
--   else
--     return "table" == type( v ) and table.tostring( v ) or
--       tostring( v )
--   end
-- end
--
-- function table.key_to_str ( k )
--   if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
--     return k
--   else
--     return "[" .. table.val_to_str( k ) .. "]"
--   end
-- end
--
-- function table.tostring( tbl )
--   local result, done = {}, {}
--   for k, v in ipairs( tbl ) do
--     table.insert( result, table.val_to_str( v ) )
--     done[ k ] = true
--   end
--   for k, v in pairs( tbl ) do
--     if not done[ k ] then
--       table.insert( result,
--         table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
--     end
--   end
--   return "{" .. table.concat( result, "," ) .. "}"
-- end


-- Funcións internas

function entity_from_id(id)
    if id and id ~= '' then
        return mw.wikibase.getEntityObject(id)
    else
        return mw.wikibase.getEntityObject()
    end
end

function population_claim(entity)
    local claims = entity.claims["P1082"]
    if not claims then
        return nil
    end
    local highest_rank = wikidata.getRango(claims)
    local claim
    for index, candidate_claim in pairs(claims) do
        if candidate_claim.rank == highest_rank then
            claim = candidate_claim
            break
        end
    end
    return claim
end


-- Funcións expostas

-- Devolve o número total de habitantes da entidade asociada coa páxina desde a que se executa.
function module.habitantes(frame)
    local entity = entity_from_id(frame.args.entity)
    if not entity then
        return
    end
    local claim = population_claim(entity)
    if not claim then
        return ''
    end
    return gl:formatNum(tonumber(claim.mainsnak.datavalue.value.amount))
end

-- Devolve o ano no que se tomou o valor que devolve habitantes().
function module.ano_padron(frame)
    local entity = entity_from_id(frame.args.entity)
    if not entity then
        return
    end
    local claim = population_claim(entity)
    if not claim then
        return ''
    end
    return wikidata_data.FormateaFechaHora(claim.qualifiers["P585"][1].datavalue.value, {enlace="no"})
end


return module