模組:沙盒/PhiLiP/Taxobox-value-renderer

维基百科,自由的百科全书
文档图示 模块文档[创建]
-- vim: set noexpandtab ft=lua ts=4 sw=4:

local r = {}

function r.entityRenderer(entity, propertyID, qualifierID)
	local claims
	local datatype
	local datavalue

	if not entity or not entity.claims then
		return ""
	end

	claims = entity.claims[propertyID]
	if not claims or not claims[1] then
		return ""
	end

	datavalue = claims[1].mainsnak.datavalue
	if not datavalue then
		return ""
	end

	datatype = datavalue.type

	if propertyID == "P225" and datatype == "string" then
		-- get taxon name
		return datavalue.value
	elseif propertyID == "P171" and datatype == "wikibase-entityid" then
		-- get parent link
		local entity_id = 'Q' .. datavalue.value["numeric-id"]
		local sitelink = mw.wikibase.sitelink(entity_id)
		local label = mw.wikibase.label(entity_id)
		if label == nil then
			label = entity_id
		end

		if not sitelink then
			-- link-d?
			sitelink = label
		end
		if sitelink ~= label then
			return "[[" .. sitelink .. "|" .. label .. "]]"
		else
			return "[[" .. sitelink .. "]]"
		end
	elseif propertyID == "P105" and datatype == "wikibase-entityid" then
		-- get taxon rank
		local entity_id = 'Q' .. datavalue.value["numeric-id"]
		local ranks = {
			["Q7432"] = "species",
			["Q34740"] = "genus",
			["Q35409"] = "familia",
			["Q36602"] = "ordo",
			["Q37517"] = "classis",
			["Q38348"] = "phylum",
			["Q334460"] = "phylum",
			["Q2752679"] = "subregnum",
			["Q36732"] = "regnum",
			["Q146481"] = "domain"
		}
		return ranks[entity_id] or "unrank"
	end
	return ""
end

return r