ናብ ትሕዝቶ ኪድ

ሞድዩል:Wikidata/Place format

ካብ ዊኪፐድያ፣ ናጻ ኢንሳይክሎፐድያ

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

local p = {}

local separatedByComma           = require('Module:Text format').separatedByComma
local tablesModule               = require('Module:Tables')
	local tableElement           = tablesModule.element
local wikidataModuleFormats      = require('Module:Wikidata/Formats')
	local toLink                 = wikidataModuleFormats.toLink
	local getData                = wikidataModuleFormats.getData
local Date                       = require('Module:Date')._Date

function p.placeFormat(value, options, frame, qualifyings)
	-- Function that returns something of the form Place, administrative territorial entity, country
	
	function getOccurrenceToDate(board, Date)
		if not board or not Date then
			return
		end
		
		local startDate, endDate
		Date = Date(Date)
		
		--if true then return require('Module:Tables').tostring(board) end
		--
		for k,v in pairs(board) do
			startDate = tableElement(v,'qualifiers','P580',1,'datavalue','value','time')
			endDate   = tableElement(v,'qualifiers','P582',1,'datavalue','value','time')
			startDate = Date(startDate)
			endDate   = Date(endDate)
			
			if startDate and endDate then
				if  Date >= startDate and
					Date <= endDate then
						return k
				end
			elseif startDate then
				if  Date >= startDate then
						return k
				end
			elseif endDate then
				if  Date <= endDate then
						return k
				end
			end
		end
			
	end
	
	function getOccurrencePreferentialRank(board)
		if not board then
			return
		end
		for k,v in pairs(board) do
			if v.rank == 'preferred' then
				return k
			end
		end
	end
	
	local placeId, placeLink, placeTag, linkedPlace
	local linkedAdministrativeTerritorialEntity	
	local countryId, countryLink, countryTag, linkedCountry
	local Date
	
	-- Validate that the place id is informed.
	if not value or not value['numeric-id'] then 
		return
	end
	
	if options then
		Date = options.Date
	end
	
	-- Obtain first the administrative territorial entity and the country of the qualifyings if possible
	-- The place is not obtained first, which would be the most logical, because it will be obtained in a different way 
	-- depending on whether or not the qualifyings are informed.
	
	if qualifyings then
		linkedAdministrativeTerritorialEntity = toLink(getData(tableElement(qualifyings,'P131',1,'datavalue','value')))
		countryLink, countryTag, countryId    = getData(tableElement(qualifyings,'P17',1,'datavalue','value'))
		linkedCountry                         = toLink(countryLink, countryTag, countryId)
	end

	if linkedCountry then
		placeLink, placeTag, placeId = getData(value)
		linkedPlace = toLink(placeLink, placeTag, placeId)
	else
	 -- If the place does not have the country qualifier, obtain it from the country property of the 
	 -- Wikidata entity of the place.
	 
	 -- At the moment the administrative entity is not obtained (it has atrocities...)
		local entity
		
		placeId = 'Q' .. value['numeric-id']
		entity  = mw.wikibase.getEntityObject(placeId)
		
		if not entity then
			return
		end
		
		placeLink   = tableElement(entity,'sitelinks','tiwiki','title')
		placeTag    = tableElement(entity,'labels','ti','value')
		linkedPlace = toLink(placeLink, placeTag, placeId, options)
		--
		-- Get the country of the place.
		--
		local countriesPlace = tableElement(entity,'claims','P17')
		
		if countriesPlace then
			-- First the country is tried on the date of the options, then 
			-- the country with the maximum rank (the current one) and then the first country.
			local countryOccurrence = getOccurrenceToDate(countriesPlace, Date) or
			getOccurrencePreferentialRank(countriesPlace) or 
			1

			countryLink, countryTag, countryId = getData(tableElement(countriesPlace,countryOccurrence,'mainsnak','datavalue','value'))
			linkedCountry  = toLink(countryLink, countryTag, countryId)
		end
	end
	
	if placeId == countryId then -- If the place is a country, for example, the United States, in Wikidata it has the country property reported with the United States.
		return linkedPlace
	elseif linkedPlace and linkedAdministrativeTerritorialEntity and linkedCountry then
		return linkedPlace .. ' (' .. linkedAdministrativeTerritorialEntity ..  '፣ ' .. linkedCountry .. ')'
	elseif linkedPlace and linkedAdministrativeTerritorialEntity then
		return linkedPlace .. ' (' .. linkedAdministrativeTerritorialEntity .. ')'
	elseif linkedPlace and linkedCountry then
		return linkedPlace .. ' (' .. linkedCountry .. ')'		
	elseif linkedAdministrativeTerritorialEntity and linkedCountry then
		return linkedAdministrativeTerritorialEntity .. ' (' .. linkedCountry .. ')'	
	else
		return linkedPlace or linkedAdministrativeTerritorialEntity or linkedCountry	
	end
end

--*****************************************************************************
-- Format aliases to use in template property
--*****************************************************************************
p['place']     = p['placeFormat']
	
return p