Módulo:CountryData

De Wikiviajes, la guía libre de viajes
Ir a la navegación Ir a la búsqueda
Icono de documentación de módulo Documentación del módulo[ver] [editar] [historial] [purgar]

Version check

Designación de la versión en Wikidata: 2021-04-09 Ok!

Uso

This module provides functions to get information about countries like, currency, language... It partly uses CountryData/Geography to avoid Wikidata access and save time.
Esta documentación está transcluida desde Módulo:CountryData/doc.
Los editores pueden experimentar en la zona de pruebas (crear) y en los casos de prueba (crear) del módulo.
Por favor, añade las categorías en la subpágina de documentación. Subpáginas de este módulo.

-- documentation
local CountryData = {
	suite  = 'CountryData',
	serial = '2021-04-09',
	item   = 65431301
}

-- CountryData module

require( 'Module:No globals' )
local cg = require( 'Module:CountryData/Geography' )
local cu = mw.loadData( 'Module:CountryData/Currencies' ) -- additional currency symbols
local wu = require( 'Module:Wikidata utilities' )

-- module variable and administration
local cm = {
	moduleInterface = CountryData
}

local function getCountry( id )
	local c = cg.countries[ id ]
	if c and not c.id then
		c.id = id
	end
	return c
end

local function getAdminEntity( id )
	local c = cg.adminEntities[ id ]
	if c and not c.id then
		c.id = id
	end
	return c
end

local function checkTable( id )
	if id == nil or id == '' then
		return nil
	end

	return getAdminEntity( id ) or getCountry( id ) -- nil or table
end

local function checkTableFromEntity( anEntity )
	local id = nil
	if not anEntity then
		return id, nil
	end

	local t
	-- located in the (1) administrative territorial entity, (2) country
	for _, prop in ipairs( { 'P131', 'P17' } ) do
		for _, val in ipairs( wu.getValues( anEntity, prop ) ) do
			id = val.id
			t = checkTable( id )
			if t then
				return id, t
			end
		end
	end

	return id, nil
end

function cm.getDataFromTables( vcEntity )
	-- article lemma
	local t = cg.articles[ mw.title.getCurrentTitle().text ]
	if t then
		return getCountry( t )
	end

	-- article id
	local articleId = mw.wikibase.getEntityIdForCurrentPage()
	if not articleId and not vcEntity then
		return nil
	end

	local arId = nil
	if articleId then
		-- article in tables
		t = checkTable( articleId )
		if t then
			return t
		end

		-- country or admin entity in table
		arId, t = checkTableFromEntity( articleId )
		if t then
			return t
		end
	end

	-- vCard entity
	local vcId = nil
	if vcEntity then
		vcId, t = checkTableFromEntity( vcEntity )
		if t then
			return t
		end
	end

	-- not found in tables, get it now all from Wikidata
	return vcId or arId
end

-- getting data for vCard

local function _getCountryData( vcEntity )
	local t = cm.getDataFromTables( vcEntity )
	if type( t ) == 'table' then
		return t
	end

	local country = {
		id = '',
		cont = '',
		iso_3166 = '',
		cc = '',
		lang = '',
		currency = '',
		country = '',
		show = ''
	}
	local ns = mw.title.getCurrentTitle().namespace
	local defaultCountry = nil
	if ns ~= 0 then
		defaultCountry = cg.articles[ '_default' ]
		if defaultCountry then 
			defaultCountry = getCountry( defaultCountry )
		end
	end

	if t == nil or t == '' then
		return defaultCountry or country
	end

	-- getting data from country entity
	country.id = t
	local coEntity = mw.wikibase.getEntity( country.id )
	if coEntity then
		country.fromWD = true
		country.iso_3166 = wu.getValue( coEntity, 'P297' )
		country.iso_3166 = country.iso_3166:upper()
		country.cont = wu.getId( coEntity, 'P30' )
		country.cont = cg.continents[ country.cont ] or ''
		country.cc = wu.getValue( coEntity, 'P474' ) -- country calling code
		t = wu.getId( coEntity, 'P37' ) -- official language id
		if t ~= '' then
			country.lang = wu.getValue( t, 'P218' ) -- ISO 639-1
			country.lang = country.lang:lower()
		end
		country.currency = wu.getId( coEntity, 'P38' ) -- currency id
	else
		country = defaultCountry or country
	end

	return country
end

-- getting county data from country calling code

function cm.getCountryFromPhones( tab )
	local country = {
		id = '',
		cont = '',
		iso_3166 = '',
		cc = '',
		lang = '',
		currency = '',
		country = '',
		show = ''
	}

	-- prepare phone numbers
	for i = #tab, 1, -1 do
		tab[ i ] = tab[ i ]:gsub( '^00', '+' )
			:gsub( '^%+%+', '+' )
			:gsub( '[^%+0-9A-Z]', '' )
		if not tab[ i ]:match( '^%+%d%d%d%d' ) then
			table.remove( tab, i )
		end
	end
	if #tab == 0 then
		return country
	end

	-- make county codes table
	local ccodes = {}
	for key, country in pairs( cg.countries ) do
		if country.cc ~= '' then
			ccodes[ country.cc:gsub( '-', '' ) ] = key
		end
	end
	for key, country in pairs( cg.adminEntities ) do
		if country.cc ~= '' then
			ccodes[ country.cc:gsub( '-', '' ) ] = key
		end
	end

	-- look for country code in phone numbers
	local q
	for i, phone in ipairs( tab ) do
		phone = phone:sub( 1, 5 )
		repeat
			q = ccodes[ phone ]
			phone = phone:sub( 1, -2 )
		until phone == '' or q
		if q then
			break
		end
	end
	if q then
		country = getCountry( q ) or getAdminEntity( q )
	end
	
	return country
end

function cm.getCountryData( entity, phones )
	local country = _getCountryData( entity )
	if country.id == '' and phones and #phones > 0 then
		country = cm.getCountryFromPhones( phones )
	end

	local c = cu[ country.currency ]
	country.currency = c and c.iso or ''
	country.addCurrency = country.currency
	if c and c.add and c.add ~= '' then
		country.addCurrency = country.addCurrency
			.. ( country.addCurrency ~= '' and ', ' or '' ) .. c.add
	end
	country.unknownLanguage = true
	if country.lang ~= '' then
		country.langName = mw.language.fetchLanguageName( country.lang,
			mw.getContentLanguage():getCode() ) or ''
		if country.langName ~= '' then
			country.unknownLanguage = false
			country.isRTL = mw.getLanguage( country.lang ):isRTL()
		end
	end

	return country
end

-- getting first-order administrative-territorial entity code
function cm.getAdm1st( countryId )
	local entityId = mw.wikibase.getEntityIdForCurrentPage()
	if not entityId or not countryId or countryId == '' then
		return nil
	end
	local i = 0
	local iso3166_2 = ''
	while entityId ~= '' and not getAdminEntity( entityId )
		and not getCountry( entityId ) and i < 5 do
		-- getting ISO 3166-2 code
		iso3166_2 = wu.getValue( entityId, 'P300' )
		if iso3166_2 ~= '' then
			return iso3166_2
		end
		-- getting next administrative territorial entity
		entityId = wu.getId( entityId, 'P131' )
		i = i + 1
	end
end

function cm.getCategories( formatStr )
	return wu.getCategories( formatStr )
end

-- getting data for LinkPhone

function cm.getCountryCode()
	local t = cm.getDataFromTables( nil )
	if type( t ) == 'table' then
		return t.cc, t.phoneDigits or 2
	end
	if t == nil or t == '' then
		return '', 2
	end
	-- t is country id
	return wu.getValue( t, 'P474' ), 2
end

-- returns a single data set from Module:CountryData/Currencies
function cm.getCurrency( key )
	return cu[ key ]
end

return cm