Vai al contenuto

Modulo:Mapshapes

Da Wikivoyage.

La documentazione per questo modulo può essere creata in Modulo:Mapshapes/man

-- This fetches referenced wikidata entries (P527 - has part, or
-- P2670 - has parts of the class ) and adds mapshapes, with colors acc. to
-- P465 (sRGB color hex triplet) or P462 (color)

-- TODO: Ideally, this could be merged into Module:map (incl. the Template:Mapshapes)

-- test code:
-- frame={["args"]={["default-color"] = "#ff0000"}, ["expandTemplate"] =function(a,b) mw.logObject(b); end}
-- p.showOne(frame, "Q2400801", {})

local p = {}

local function matches_filter(args, id)
	if (args[2] == "") then
		return true
	end

	local idx = 2
	while (args[idx] ~= "") do
		if id == args[idx] then
			return true
		end
		idx = idx + 1
	end
	return false
end

p.showOne = function(frame, subentityID, out)
	local subentity = mw.wikibase.getEntity(subentityID)
	if subentity.claims == nil then
		mw.log("empty mapshape " .. subentityID .. "!")
		return out
	end
	local label = subentity:getLabel()
	if label == nil then label = '' end

	local rgb
	if subentity.claims.P465 ~= nil then
		for k, v in pairs(subentity.claims.P465) do
			if v.rank ~= "deprecated" then
				rgb = subentity.claims.P465[k].mainsnak.datavalue.value
				break
			end
		end
	end
	if rgb == nil or rgb == "" then
		mw.log("no rgb")
		if subentity.claims["P462"] == nil then
			mw.log("no color")
			rgb = frame.args["default-color"]
		else
			if subentity.claims.P462[1].qualifiers ~= nil and subentity.claims.P462[1].qualifiers.P465 ~= nil then
				rgb = subentity.claims.P462[1].qualifiers.P465[1].datavalue.value
				mw.logObject(rgb)
			else
				colorID = subentity.claims.P462[1].mainsnak.datavalue.value.id
				color = mw.wikibase.getEntity(colorID)
				rgb = color.claims.P465[1].mainsnak.datavalue.value
				mw.logObject(rgb)
			end
		end
	end

	arguments = {
		wikidata=subentityID,
		type="geoline",
		stroke="#"..rgb,
	}
	arguments["stroke-width"] = frame.args["stroke-width"]
	arguments["stroke-opacity"] = frame.args["stroke-opacity"]
	arguments["group"] = frame.args["group"]
	arguments["title"] = string.gsub(string.gsub(label, '"', '\\"'), "'", "\'")

	out[#out + 1] = frame:expandTemplate{title='Mapshape', args=arguments}
	return out
end

p.show = function(frame)
	local itemID = mw.text.trim(frame.args[1] or "")
	local entity = mw.wikibase.getEntity(itemID)
	local claims = entity.claims.P527
	if claims == nil then claims = entity.claims.P2670 end
	if claims then
		if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then
			local out = {}
			for k, v in pairs(claims) do
				local subentityID = v.mainsnak.datavalue.value.id
				if matches_filter(frame.args, subentityID) then
					out = p.showOne(frame, subentityID, out)
				end
			end
			return table.concat(out, "")
		end
	else
		return ""
	end
end

return p