Cebur nuju daging

Modul:Taxontree

Saking Wikipédia

Dokuméntasi antuk modul puniki prasida kakardi ring Modul:Taxontree/doc

--[[ saking https://commons.wikimedia.org/wiki/Module:Taxontree
Module:Taxontree
Module to display chain of taxon informatiom in table format.
This only returns table rows, so needs to be invoked inside a table.
Each row displays Taxon-Rank as row label and Taxon-Name linked to Commons Category as value
If parameter first=true, it displays the row for the item invoked
{default is false)
It finishes when it reaches an entity that has no parent taxon (P171)
or when it reaches an entity whose taxon rank (P105) with value "kingdom" (Q36732)
For stand-alone use:
{|
{{#invoke:Taxontree |show |qid=<Qnnn> |first=yes/no |lang=<langcode>}}
|}
--]]


local p = {}


-- These are the qnumbers of taxon ranks that should have italicised values
local italicvalues = {
	"Q34740", -- genus
	"Q3238261", --subgenus
	"Q3181348", -- section
	"Q5998839", -- subsection
	"Q3025161", -- series
	"Q13198444", -- subseries 
	"Q7432", -- species
	"Q68947", -- subspecies
	"Q767728", -- variety
	"Q630771", -- subvariety
	"Q279749", -- form
	"Q12774043", -- subform
}


--[[
_findlang takes a "langcode" parameter if supplied and valid
Otherwise it tries to create it from the user's set language ({{int:lang}})
Failing that it uses the wiki's content language.
It returns a language object
Dependencies: none
--]]
local _findlang = function(langcode)
	local langobj
	langcode = mw.text.trim(langcode or "")
	if mw.language.isKnownLanguageTag(langcode) then
		langobj = mw.language.new( langcode )
	else
		langobj = mw.language.new( mw.getCurrentFrame():preprocess( '{{int:lang}}' ) )
		if not langobj then langobj = mw.language.getContentLanguage() end
	end
	return langobj
end


--[[
_parseparam takes a (string) parameter, e.g. from the list of frame arguments,
and makes "false", "no", and "0" into the (boolean) false
It makes the empty string and nil into the (boolean) value passed as default
allowing the parameter to be true or false by default.
Dependencies: none
--]]
local _parseparam = function(param, default)
	param = mw.text.trim(param or ""):lower()
	if param ~= "" then
		if (param == "false") or (param:sub(1,1) == "n") or (param == "0") then
			return false
		else
			return true
		end
	else
		return default
	end
end


--[[
_getqid returns the qid if supplied (uppercased and trimmed)
failing that, the Wikidata entity ID of the "category's main topic (P301)", if it exists
failing that, the Wikidata entity ID asociated with the curent page, if it exists
otherwise, nothing
Dependencies: none
--]]
local _getqid = function(qid)
	qid = mw.text.trim(qid or ""):upper()
	if qid ~= "" then return qid end
	-- check if there's a "category's main topic (P301)":
	qid = mw.wikibase.getEntityIdForCurrentPage()
	if qid then
		local prop301 = mw.wikibase.getBestStatements(qid, "P301")
		if prop301[1] then
			local mctid = prop301[1].mainsnak.datavalue.value.id
			if mctid then return mctid end
		end
	end
	-- otherwise return the page qid (if any)
	return qid
end


--[[
_show returns a Lua table containing the rows of an html table
Each row is one step in the chain of P171 (parent taxon)
qid is an entity-id <Qnnnn>
lang is an ISO-style language code
first and authorcite are boolean
Dependencies: _findlang()
--]]
local _show = function(qid, first, lang, authorcite)
	local langcode = _findlang(lang):getCode()
	-- see if we want and can get a taxon author citation (P6507, string value)
	local tac
	if authorcite then
		tac = mw.wikibase.getBestStatements(qid, "P6507")[1]
		tac = tac and tac.mainsnak.datavalue and tac.mainsnak.datavalue.value
	end
	local rows = {}
	local finished = false
	local count = 0
	repeat
		-- get the taxon name, rank and rank-id for this entity-id
		local taxonName = mw.wikibase.getBestStatements(qid, "P225")[1]
		taxonName = taxonName and taxonName.mainsnak.datavalue and taxonName.mainsnak.datavalue.value
		local taxonRank = mw.wikibase.getBestStatements(qid, "P105")[1]
		taxonRankID = taxonRank and taxonRank.mainsnak.datavalue and taxonRank.mainsnak.datavalue.value.id
		taxonRank = taxonRankID and mw.wikibase.label(taxonRankID)
		-- if the rank is kingdom, finish looping
		if taxonRankID == "Q36732" then finished = true end
		-- see if this entity-id has a Commons category
		local commonsCat = mw.wikibase.getBestStatements(qid, "P373")[1]
		commonsCat = commonsCat and commonsCat.mainsnak.datavalue and commonsCat.mainsnak.datavalue.value
		if taxonRank and taxonName then
			taxonRank = taxonRank:gsub("^(%l)", string.upper) -- ucfirst
			-- test for ranks that should have italicised values
			for i, v in ipairs(italicvalues) do
				if v == taxonRankID then
					taxonName = "<i>" .. taxonName .. "</i>"
					break
				end
			end
			-- if there's a Commons category, make taxonName link to it
			if commonsCat then
				taxonName = "[[:Category:" .. commonsCat .. "|" .. taxonName .. "]]"
			end
			local row
			-- if this is the first one and taxon author citation is set, span the 2 columns
			if count == 0 and tac then
				row = "<th colspan='2' class='taxontree-hdrcell'>" .. taxonRank .. "</th></tr>"
				row = row .. "<tr class='taxontree-row'><td colspan='2' class='taxontree-fullcell'>"
				row = row .. taxonName .. "<br>" .. tac
			else
				row = "<th class='taxontree-lcell'>" .. taxonRank .. "</th>"
				row = row .. "<td class='taxontree-rcell'>" .. taxonName
			end
			rows[#rows+1] = row .. "</td>"
		end
		local parent = mw.wikibase.getBestStatements(qid, "P171")[1]
		if parent and parent.qualifiers and parent.qualifiers.P678 and parent.qualifiers.P678[1].datavalue then
			local rank = mw.wikibase.label(parent.qualifiers.P678[1].datavalue.value.id)
			if rank then
				rank = rank:sub(1, 1):upper() .. rank:sub(2)
				local hdr = "<th class='taxontree-lcell'>" .. rank .. "</th>"
				rows[#rows+1] = hdr .. "<td class='taxontree-rcell'>incertae sedis</td>"
			end
		end
		if parent and parent.mainsnak.datavalue then
			qid = parent.mainsnak.datavalue.value.id
		else
			-- This is top-level location
			finished = true
		end
		count = count + 1
	until finished or count >= 30 -- limit to 30 levels to avoid infinite loops
	if not first and not tac then table.remove(rows, 1) end
	local reversed = {} -- reverse the table order
	if #rows > 0 then
		for i, v in ipairs(rows) do
			reversed[#rows + 1 - i] = v
		end
	end
	return reversed
end


--[[
This is the main entry point:
{{#invoke:Taxontree |show |qid=<Qnnn> |first=yes/no |lang=<langcode> |authorcite=yes/no}}
Dependencies: _parseparam(); _getqid(); _show(); _findlang()
]]
p.show = function(frame)
	local qid = _getqid(frame.args.qid or frame.args[1]) or ""
	if qid == "" then return "No id supplied" end
	local first = _parseparam(frame.args.first, false)
	local authorcite = _parseparam(frame.args.authorcite or frame.args.ac, false)
	local lang = mw.text.trim(frame.args.lang or "")
	local rows = table.concat( _show(qid, first, lang, authorcite), "</tr><tr class='taxontree-row'>")
	if rows then
		return "<tr class='taxontree-row'>" .. rows .. "</tr>"
	end
end


--[[
findLang returns the code property of the object returned from the _findlang() function:
{{#invoke:Taxontree |findLang |lang=<langcode>}}
Dependencies: _findlang()
]]
p.findLang = function(frame)
	return _findlang( frame.args.lang or frame.args[1] ).code
end


return p