Documentation for this module may be created at ሞድዩል:Wikidata/Country formats/doc

local p={}

local tablesModule = require('Module:Tables')
local	tableElement  = tablesModule.element
local	groupTable    = tablesModule.toGroup
local	orderTable    = tablesModule.toOrder
local wikidataModule  = require('Module:Wikidata')
local   listFormat          = wikidataModule.listFormat
local wikidataModuleFormats = require('Module:Wikidata/Formats')
local	toLink              = wikidataModuleFormats.toLink
local   periodFormat        = wikidataModuleFormats.periodFormat
local getData               = wikidataModuleFormats.getData


local countries = mw.loadData('Module:Wikidata/Country_formats/data')

function p.tests(frame)
	--local entity
	local nationality
	
	for k,v in pairs(countries) do
		if not v.label then
			--entity   = mw.wikibase.getEntityObject(k) or {}
			--v.label = require('Module:Tables').element(entity,'labels', 'ti', 'value')
			v.label = mw.wikibase.getLabel(k)
		end
		if not v.link then
			v.link = mw.wikibase.getSitelink(k)
		end			
		if v.label and not v.nationality then
			nationality = frame:preprocess('{{ዜግነት ህዝቢ|'.. v.label .. '|ms}}')
			
			if nationality and nationality~= '' then
				v.nationality = nationality
			end
		end
    end	
	return require('Module:Tables').tostringordered(countries)
end

function p.nationalityFormat(claim, options)
	if not claim then
		return
	end
	local board_nationalities={}
	local countryId
	local country
	
	--if true then return require('Module:Tables').tostring(claim) end
	for k, v in ipairs(claim) do
		countryId = tableElement(v,'mainsnak','datavalue','value','id')
		if countryId then
			country = countries[countryId] or {}
			-- it is necessary to skip the countries without nationality
			if country.nationality then
				table.insert(
					nationalities_board, 
					{
						['ዜግነት'] = country.nationality,
						['data'] = {
							['countryId']	  = countryId,
							['countryLabel']  = country.label,
							['countryLink']	  = country.link,
							['period']	      = periodFormat(v.qualifiers)				
						},
						['date']	     = tableElement(v,'qualifiers','P585',1,'datavalue','value','time'),
						['creationDate'] = tableElement(v,'qualifiers','P571',1,'datavalue','value','time'),
						['startDate']	 = tableElement(v,'qualifiers','P580',1,'datavalue','value','time'),
						['endDate']	     = tableElement(v,'qualifiers','P582',1,'datavalue','value','time')	
					}
				)
			end
		elseif tableElement(v,'mainsnak','snaktype') == 'novalue' then
			if  tableElement(v,'qualifiers','P805',1,'datavalue','value','id') == 'Q223050' then
				table.insert(
					nationalities_board, 
					{
						['ዜግነት'] = 'ዜግነት ኣልቦ',
						['data'] = {
							['period']	     = periodFormat(v.qualifiers)
						},
						['date']	     = tableElement(v,'qualifiers','P585',1,'datavalue','value','time'),
						['creationDate'] = tableElement(v,'qualifiers','P571',1,'datavalue','value','time'),
						['startDate']    = tableElement(v,'qualifiers','P580',1,'datavalue','value','time'),
						['endDate']	     = tableElement(v,'qualifiers','P582',1,'datavalue','value','time')
					}
				)				
			else
				table.insert(
					nationalities_board, 
					{
						['ዜግነት'] = 'ብዘይ ዜግነት',
						['data'] = {
							['period']	     = periodFormat(v.qualifiers)
						},
						['date']	     = tableElement(v,'qualifiers','P585',1,'datavalue','value','time'),
						['creationDate'] = tableElement(v,'qualifiers','P571',1,'datavalue','value','time'),
						['startDate']	 = tableElement(v,'qualifiers','P580',1,'datavalue','value','time'),
						['endDate']	     = tableElement(v,'qualifiers','P582',1,'datavalue','value','time')
					}
				)
			end
		end
	end
	
	orderTable(nationalities_board, {'ዜግነት', 'startDate', 'endDate'})
	nationalities_board = groupTable(nationalities_board, 'ዜግነት', 'data')
	--if true then return require('Module:Tables').tostring(nationalities_board) end	
	orderTable(nationalities_board, {'startDate', 'endDate'})
	--nationalities_board = groupTable(nationalities_board, 'ዜግነት', 'data')
	--if true then return require('Module:Tables').tostring(nationalities_board) end
	
	local nationalities = {}
	local periods_board
	local nationality
	local periods
	
	for k,v in ipairs(board_nationalities) do
		periods_board = {}
		if v.data[2] then
			nationality = v.nationality
			
			if options['textFormat'] and mw.ustring.find(options['textFormat'],'uppercase', plain ) and k == 1 then
				nationality = mw.language.new('ti'):ucfirst(nationality)			
			end
			
			-- Check if all periods are reported. If not, they will not be placed in parentheses.
			-- For example, William Wordsworth whose country of nationality in Wikidata is the 
			-- United Kingdom of Great Britain and Ireland with no start or end dates and the Kingdom of Great Britain 
			-- with an end date of 1 Jan 1801.
			-- Only "British" will be returned as nationality.
			local wcountryWithoutPeriod
			for k2,v2 in ipairs(v.data) do
				if not v2.period then
					wcountryWithoutPeriod = true
				end
			end
			
			if not wcountryWithoutPeriod then
				for k2,v2 in ipairs(v.data) do
					table.insert(periods_board, toLink(v2.countryLink, v2.period, v2.countryId))
				end
				
				periods = table.concat(periods_board,'፣ ')				
			end	
			--if true then return require('Module:Tables').tostring(periods_board) end
		else
			if options['textFormat'] and mw.ustring.find(options['textFormat'],'uppercase', plain ) and k == 1 then
				options['uppercase'] = 'yes'
			else
				options['uppercase'] = 'no'
			end
			nationality = toLink(v.data[1].countryLink, v.nationality, v.data[1].countryId, options)
			periods = v.data[1].period
		end
		
		if periods and periods ~= '' then
			nationality = nationality .. '&nbsp;<small>(' .. periods .. ')</small>'
		end
		table.insert(nationalities, nationality)
	end
	
	return listFormat(nationalities, options)
	--return require('Module:Tables').tostring(nationalities)
end

function p._nationality(frame)
	local args    = frame.args
	
	--if true then return require('Module:Tables').tostring(frame.args) end
	local claim = mw.wikibase.getAllStatements(args.entityId or args.id,'P27')
	
	if not claim then
		return
	end
	
	options={}
	options.property   = 'P27'
	options.entityId   = args.entityId or args.id,'P27'
	options.linkback   = args.linkback
	options.textFormat = args.textFormat
	options.link       = args.link
	
	return p.nationalityFormat(claim, options)
end

function p.flagFormat(value, options, frame)
	if not value then
		return
	end

	local link, label, id = getData(value)	
    
    if not label then 
    	return
    elseif mw.title.new('Template:Country data '.. label ).exists  then
        return frame:preprocess('{{flagcountry|' .. label ..  '}}')
    else
    	return toLink(link, label, id, options)
    end
end

-- Alias to simplify use from template property
--
p['flag'] 		    = p.flagFormat
p['ዜግነት'] 	= p.nationalityFormat

return p