Spring til indhold

Modul:Flag

Fra Wikipedia, den frie encyklopædi

require('Modul:No globals')
local data = mw.loadData("Modul:Flag/data")
local wikibase = mw.wikibase

-- This module uses function from [[Module:Cycling race]]

local p = {}

--[[ Iterator to get all statements for an entity and property which are not deprecated and have a value]]
local function nextStatement(state, i)
	repeat
		i = i + 1
		local s = state[i]
		if s and s.rank ~= 'deprecated' and s.mainsnak.snaktype == 'value' then
			return i, s
		end
	until s == nil
end
local function statements(qid, pid)
	return nextStatement, wikibase.getAllStatements(qid, pid), 0
end

function p.getStatementForTime(ID, property, time)
	for _, s in statements(ID, property) do
		local start, startPrecision, END, endPrecision
		local q = s.qualifiers
		if q then
			if q.P580 and q.P580[1] and q.P580[1].snaktype == 'value' then -- P580 is start time
				start = q.P580[1].datavalue.value.time
				startPrecision = q.P580[1].datavalue.value.precision
				if startPrecision == 9 then -- precision is years
					start = string.sub(start, 1, 5) -- Cut of everything after year
				elseif startPrecision == 10 then -- precision is months
					start = string.sub(start, 1, 8) -- Cut of everything after month
				end
			end
			if q.P582 and q.P582[1] and q.P582[1].snaktype == 'value' then -- P582 is end time
				END = q.P582[1].datavalue.value.time
				endPrecision = q.P582[1].datavalue.value.precision
			end
		end
		if not start or start <= time then
			if not END then
				return s
			end
			if endPrecision == 9 then -- precision 9 is 'years'
				END = string.sub(END, 1, 6) .. '13' -- Set month to 13
			elseif endPrecision == 10 then -- precision 10 is 'months'
				END = string.sub(END, 1, 9) .. '32' -- Set day to 32
			end
			if END > time then
				return s
			end
		end
	end
end

function p.flag(countryID, date)
	local trackingCategory = '[[Category:Missing flag in Module:Cycling race]]'

	local entry = data.flags[countryID]
	local IOC
	local file
	if entry then
		for i, v in ipairs(entry) do
			if i == 1 then
				IOC = v
			else
				if not date then
					file = v[1]
					break
				else
					local from = v[2]
					local to = v[3]
					if (not from or from <= date) and (not to or to > date) then
						file = v[1]
						break
					end
				end
			end
		end
	end
	local flagpxSize = '20px'
	if countryID == 'Q39' then flagpxSize = '16px'end -- Small size for an square flag as Switzerland
	if file then
		return '[[File:' .. file .. '|border|' .. flagpxSize ..'|' .. IOC .. ']]'
	elseif not date then
		local p41 = wikibase.getBestStatements(countryID, "P41") -- P41 is flag image
		if p41[1] and p41[1].mainsnak.snaktype == 'value' then
			return '[[File:' .. p41[1].mainsnak.datavalue.value .. '|border|' .. flagpxSize ..'|(Wikidata:' .. countryID .. ')]]'
				.. trackingCategory
		end
	else
		-- Search flag for specific date
		local p41 = p.getStatementForTime(countryID, "P41", date) -- P41 is flag image
		if p41 then
			return '[[File:' .. p41.mainsnak.datavalue.value .. '|border|' .. flagpxSize ..'|(Wikidata:' .. countryID .. ')]]'
				.. trackingCategory
		end
	end
	return trackingCategory
end

return p