Moduł:Brudnopis/Paweł Ziemian/Taxonbar

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Brudnopis/Paweł Ziemian/Taxonbar/opis

local resources = {
	title = "[[:d:Wikidata:WikiProject_Taxonomy|Taksonomia]]",
}

local sources = {
	{
		name     = "BHL",
		property = "P687",
	},
	{
		name     = "BioLib",
		property = "P838",
	},
	{
		name     = "EoL",
		property = "P830",
	},
	{
		name     = "GBIF",
		property = "P846",
	},
	{
		name     = "PlantList",
		property = "P1070",
	},
	{
		name     = "Tropicos",
		property = "P960",
	},
	{
		name     = "ITIS",
		property = "P815",
	},
	{
		name     = "IUCN",
		property = "P627",
	},
	{
		name     = "NCBI",
		property = "P685",
	},
	{
		name     = "GRIN",
		property = "P1421",
		getid    = function(value) return mw.ustring.sub(value, 4) end,
	},
	{
		name     = "Fossilworks",
		property = "P842",
	},
	{
		name     = "WoRMS",
		property = "P850",
	},
	{
		name     = "FishBase",
		property = "P938",
	},
	{
		name     = "MSW",
		property = "P959",
	},
	{
		name     = "IPNI",
		property = "P961",
	},
	{
		name     = "MycoBank",
		property = "P962",
	},
	{
		name     = "AlgaeBase",
		property = "P1348",
	},
	{
		name     = "Index Fungorum",
		property = "P1391",
	},
	{
		name     = "FNA",
		property = "P1727",
	},
	{
		name     = "FOC",
		property = "P1747",
	},
	{
		name     = "ZooBank",
		property = "P1746",
	},
	{
		name     = "Watson & Dallwitz",
		property = "P1761",
	},
	{
		name     = "PLANTS",
		property = "P1772",
	},
	{
		name     = "GrassBase",
		property = "P1832",
	},
	{
		name     = "Fauna Europaea",
		property = "P1895",
	},
	{
		name     = "Conifers.org",
		property = "P1940",
	},
	{
		name     = "LPSN",
		property = "P1991",
	},
	{
		name     = "Plazi",
		property = "P1992",
	},
	{
		name     = "Avibase",
		property = "P2026",
	},
	{
		name     = "AFPD",
		property = "P2036",
	},
	{
		name     = "Species+",
		property = "P2040",
	},
	{
		name     = "Xeno-canto",
		property = "P2426",
	},
	{
		name     = "Panarctic Flora",
		property = "P2434",
	},
}

local function get(entity, index)
	local cursor = entity
	for i, v in ipairs(index) do
		if cursor == nil then
			return
		end
		
		if (type(v) == "string") or (type(v) == "number") then
			cursor = cursor[v]
		elseif type(v) == "table" then
			if cursor[v[1]] ~= v[2] then
				cursor = nil
			end
		else
			cursor = nil
		end
	end

	return cursor
end

local function getSubjectItem(entity, name)
	local p1629id = get(entity, {"claims", "P1629", 1, { "type", "statement" }, "mainsnak", { "snaktype", "value" }, { "datatype", "wikibase-item" }, "datavalue", { "type", "wikibase-entityid" }, "value", { "entity-type", "item" }, "numeric-id" })
	if p1629id then
		local q = "Q"..tostring(p1629id)
		local sitelink = mw.wikibase.sitelink(q) -- Wikipedia article
			or (":d:"..q)                        -- or Wikidata
		local n = name or mw.wikibase.label(q) or q
		return "[["..sitelink.."|"..n.."]]"
	end
end

local function getFormatterURL(entity)
	return get(entity, {"claims", "P1630", 1, { "type", "statement" }, "mainsnak", { "snaktype", "value" }, { "datatype", "string" }, "datavalue", { "type", "string" }, "value" })
end

local function getRootPage(entity)
	return get(entity, {"claims", "P1896", 1, { "type", "statement" }, "mainsnak", { "snaktype", "value" }, { "datatype", "url" }, "datavalue", { "type", "string" }, "value" })
end

local show = function(args, parent, foreign, entity)
	local claims = (entity and entity.type == "item") and entity.claims or {}
	local namespace = foreign and -1 or mw.title.getCurrentTitle().namespace --TODO remove foreign constrait when entities API is available for them
	
	local function identifiers(info)
		local result = {}
		local data = claims[info.property]
		if data then
			for _, v in ipairs(data) do
				if v.mainsnak.snaktype == "value" then
					local id = v.mainsnak.datavalue.value
					table.insert(result, id)
				end
			end
		end
	
		return result
	end
	
	local function formatLabel(info)
		local pentity = mw.wikibase.getEntity(info.property)
		if not pentity then
			return
		end
		
		return getSubjectItem(pentity, info.name) or info.name
	end
	
	local function formatLink(info, identifier, title)
		local pentity = mw.wikibase.getEntity(info.property)
		if not pentity then
			return
		end
		
		local id = false
		if pentity.datatype == "external-id" then
			local formatterURL = getFormatterURL(pentity)
			if formatterURL then
				id = mw.ustring.gsub(formatterURL, "$1", identifier)
			end
		elseif pentity.datatype == "string" then
			local formatterURL = getFormatterURL(pentity)
			if formatterURL then
				id = mw.ustring.gsub(formatterURL, "$1", identifier)
			end
		elseif pentity.datatype == "url" then
			id = identifier
			-- assume id is in query string
			local s, e = mw.ustring.find(identifier, "?", 1, true)
			if not e then
				-- try exclude root page name if defined
				local root = getRootPage(pentity)
				if root then
					s, e = mw.ustring.find(identifier, root, 1, true)
				end
			end
			
			if not e or (s > 1) then
				-- exclude host name
				s, e = mw.ustring.find(identifier, "//[^/]+/", 1, false)
			end
			
			if e then
				-- the remain part is an identifier
				local query = mw.ustring.sub(identifier, e + 1)
				identifier = info.getid and info.getid(query) or query
			end
		end
		
		local description = title and string.format("<span title=\"%s\">%s</span>", identifier, title) or identifier
		local result = id and ("["..id.." "..description.."]") or description
		return result
	end
	
	local formats = {
		classic = function(info, identifiers)
			local result = {}
			table.insert(result, formatLabel(info))
			for i, id in ipairs(identifiers) do
				table.insert(result, (i == 1) and ":" or ",")
				table.insert(result, "&thinsp;<span class=\"uid\">")
				table.insert(result, formatLink(info, id))
				table.insert(result, "</span>")
			end
			
			return table.concat(result, "")
		end,
		
		mini = function(info, identifiers)
			local result = {}
			if #identifiers == 1 then
				-- simple link with authority name
				local id = identifiers[1]
				table.insert(result, formatLink(info, id, info.name))
				return table.concat(result, "")
			end

			table.insert(result, formatLabel(info))
			for i, id in ipairs(identifiers) do
				table.insert(result, (i == 1) and ":" or ",")
				table.insert(result, "&thinsp;[")
				table.insert(result, formatLink(info, id, tostring(i)))
			end
			
			return table.concat(result, "")
		end,
	}
	
	local result = {}
	local formatItems = formats[args.style] or formats.classic
	for _, info in ipairs(sources) do
		local list, cat = identifiers(info)
		if #list > 0 then
			table.insert(result, "<li>"..formatItems(info, list).."</li>")
		end
	end

	if #result > 0 then
		table.insert(result, 1, "<div id=\"taxonbar\" class=\"catlinks taxonbar\">"..resources.title)
		table.insert(result, 2, ":<ul>")
		table.insert(result,"</ul></div>")
		return table.concat(result, "");
	end
end

return {
	
["Dokumentacja"] = function(frame)
	local result = {}
	table.insert(result, "{| class=wikitable\n!nazwa\n!cecha\n!temat\n!typ\n!format\n")
	for i, v in ipairs(sources) do
		local pentity = mw.wikibase.getEntity(v.property)
		
		if i > 0 then
			table.insert(result, "|-\n")
		end
		
		table.insert(result, "|")
		table.insert(result, v.name)
		table.insert(result, "\n")
		
		table.insert(result, "|[[:d:Property:")
		table.insert(result, v.property)
		table.insert(result, "|")
		table.insert(result, v.property)
		table.insert(result, "]]\n")
		
		table.insert(result, "|")
		table.insert(result, getSubjectItem(pentity) or "—")
		table.insert(result, "\n")
		
		table.insert(result, "|")
		table.insert(result, pentity and pentity.datatype or "—")
		table.insert(result, "\n")
		
		table.insert(result, "|")
		table.insert(result, mw.text.nowiki(getFormatterURL(pentity) or getRootPage(pentity) or "—"))
		table.insert(result, "\n")
		
	end
	
	table.insert(result, "|}")
	
	return table.concat(result, "")
end,

["Pokaż"] = function(frame)
	local args = frame.args
	local parent = frame:getParent().args
	local foreign = args[1] or parent[1]
	local entity = mw.wikibase.getEntityObject(foreign)
	return show(args, parent, foreign, entity)	
end,

Wikikod = function(frame)
	local result = m["Pokaż"](frame)
	return mw.text.nowiki(result)
end,

Wikidok = function(frame)
	local result, _ = string.gsub(mw.text.nowiki(m["Dokumentacja"](frame)), "\n", "</br>")
	return result
end,

}