Module:CountryData
Jump to navigation
Jump to search
Documentation for this module may be created at Module:CountryData/doc
--[[
Source script: https://it.wikivoyage.org/wiki/Modulo:CountryData
Maintainer: Andyrom75
]]
local fw = require( 'Module:FastWikidata' )
local getArgs = require('Module:Arguments').getArgs
local debugMode = ''
local cents = { -- known cents symbols
['ALL'] = 'q',
['AUD'] = 'c',
['BTN'] = 'Ch.',
['CAD'] = '¢',
['CZK'] = 'h',
['EGP'] = 'pt.',
['EUR'] = 'c',
['GBP'] = 'p',
['HRK'] = 'lp',
['MXN'] = '¢',
['NZD'] = 'c',
['PLN'] = 'gr',
['TOP'] = '¢',
['USD'] = '¢',
['XOF'] = 'c',
['ZAR'] = 'c'
}
local function _is_defined(s)
if s and s ~= '' then return s end
return nil
end
local function _getAdministrativeEntity( entity )
local id = nil
local i, w
if entity then
-- located in the administrative territorial entity
-- IMPORTANT NOTE: evaulate a recursive function to find the exact territorial entity (maybe with a deep limit)
w = fw.getBestStatements( entity, 'P131' )
if #w > 0 then
for i = 1, #w, 1 do
if w[ i ].mainsnak.snaktype == 'value' then
id = w[ i ].mainsnak.datavalue.value.id
end
end
end
end
return id
end
local function _getCountryEntity( entity )
local id = nil
local i, w
if entity then
w = fw.getValues( entity, 'P17', nil ) -- country
if #w > 0 then
for i = 1, #w, 1 do
id = w[ i ].id
end
end
end
return id
end
function _getEntityData( currentEntity )
local entityData = {
id = '',
iso_3166 = '',
cc = '',
lc = '',
currency = '',
currency_symbol = ''
}
-- getting data from country entity
if currentEntity then
entityData.cc = fw.getValue( currentEntity, 'P474' ) -- country calling code
local currencyEntity = fw.getId( currentEntity, 'P38' ) -- currency id
if not _is_defined(entityData.cc) or not _is_defined(currencyEntity) then
return entityData
end
if currencyEntity ~= '' then
entityData.currency = fw.getValue( currencyEntity, 'P498' ):upper() -- ISO 4217
entityData.currency_symbol = fw.getValuesWithLanguages( currencyEntity, 'P5061' ) --
entityData.currency_symbol = entityData.currency_symbol['mul']
end
entityData.iso_3166 = fw.getValue( currentEntity, 'P297' ):upper() --three-letter country identifier
else
return entityData
end
entityData.id = currentEntity
return entityData
end
function _getCountryData( entity )
local entityData = {}
entityData = _getEntityData(entity) --current or requested EntityData
if entityData.id == '' then
entityData = _getEntityData(_getAdministrativeEntity(entity)) --administrativeEntityData
if entityData.id == '' then
entityData = _getEntityData(_getCountryEntity(entity)) --countryEntityData
end
end
entityData.lc = fw.getValue( entity, 'P473' ) -- local dialing code
return entityData
end
local p = {}
function p._countryData2HTML( entity )
if entity == nil then --check externally requested entity
entity = mw.wikibase.getEntityIdForCurrentPage() --current entity
if entity == nil then
return nil
end
end
mw.log('entity: ' .. entity)
local countryData = _getCountryData( entity )
local currency = countryData.currency
if( countryData.currency_symbol ) then
if( currency and #currency>0 ) then
currency = currency .. ', '
end
currency = currency .. countryData.currency_symbol
end
if( cents[countryData.currency] ) then
if( currency and #currency>0 ) then
currency = currency .. ', '
end
currency = currency .. cents[countryData.currency]
end
return (debugMode == "YES" and "<" or "<") .. 'span class="countryData" data-country-calling-code="'.. countryData.cc ..'" data-local-dialing-code="'.. countryData.lc ..'" data-currency="'.. currency ..'" style="display:none;"></span>'
end
function p.countryData2HTML( frame )
local args = getArgs(frame)
local entity = args[1]
debugMode = args['debug']
return p._countryData2HTML(entity)
end
return p