Для документации этого модуля может быть создана страница Модуль:Wikidata2/Flags/doc

local p = {}

function p.getFlag( context, countryEntityOrId, actualDate )
	local countryEntity;
	if ( type( countryEntityOrId ) == "string" ) then
		countryEntity = mw.wikibase.getEntity( countryEntityOrId );
	else
		countryEntity = countryEntityOrId;
	end

	if ( not countryEntity or not countryEntity.claims or not countryEntity.claims.P41 ) then
		return nil;
	end

	local countryFlags = {};
	local flagImageStatements = countryEntity.claims.P41;
	for _, flagImageStatement in pairs( countryEntity.claims.P41 ) do
		if ( flagImageStatement.rank ~= 'deprecated' ) then
			local flagImage;
			if ( flagImageStatement and flagImageStatement.mainsnak and flagImageStatement.mainsnak.datavalue and flagImageStatement.mainsnak.datavalue.value ) then
				flagImage = flagImageStatement.mainsnak.datavalue.value;
			end
			local flagStartTime = -9223372036854775808;
			if ( flagImageStatement.qualifiers
					and flagImageStatement.qualifiers.P580
					and flagImageStatement.qualifiers.P580[1] ) then
				 local parsedFlagStartTime = context.parseTimeFromSnak( flagImageStatement.qualifiers.P580[1] );
				 if ( parsedFlagStartTime ) then
				 	flagStartTime = parsedFlagStartTime;
				 end
			end
			if ( flagImage ) then
				countryFlags[ flagStartTime ] = flagImage;
			end
		end
	end

	local goodFlag = nil;
	if ( countryFlags ) then
		local ordered_dates = {}
		for flagBeginDate in pairs(countryFlags) do
			table.insert(ordered_dates, flagBeginDate)
		end
		table.sort(ordered_dates)

		for i = 1, #ordered_dates do
			local flagBeginDate, flag = ordered_dates[i], countryFlags[ ordered_dates[i] ];
			if ( actualDate >= flagBeginDate ) then
				goodFlag = flag;
			end
		end
	end
	if ( goodFlag ) then
		return '[[File:' .. goodFlag .. '|20x15px|border]]';
	end
	return nil;
end

return p;