跳转到内容

模組:Taxobox wikidata

本页使用了标题或全文手工转换
维基百科,自由的百科全书

这是本页的一个历史版本,由Taeas留言 | 贡献2023年1月10日 (二) 17:12 撤销Taeas讨论)的版本75460219)编辑。这可能和当前版本存在着巨大的差异。

require('Module:No globals')
local p = {} -- functions made public
local l = {}
local gFrame

local rankList = {
	regnum = '',
	phylum = '',
	classis = '',
	ordo = '',
	familia = '',
	genus = '',
	subgenus = '',
	sectio = '',
	species = '',
}
local binomial = ''
local binomialAuthority = ''

function p.main(frame)
	gFrame = frame
	local args
	if frame.args['direct'] == 'yes' then args = frame.args
	else args = frame:getParent().args end
	
	local from = args['from'] or ''
	
	local currName =  l.getData({'label', from})
	local currTaxonName = l.getData({'property', from, 'P225'})
	local currRank = l.getData({'property', 'raw', from, 'P105'})
	local colour = ''
	
	l.setRank(currRank, currName, currTaxonName, true, args['authority'] or '')
	
	local parent = l.getData({'property', 'raw', from, 'P171'})
	local parentRank = l.getData({'property', 'raw', parent, 'P105'})
	local preRank = currRank
	local maxStep = 30
	local step = 0
	while true
	do
		step = step + 1
		local gen = true
		
		if parentRank == (currRank or preRank)
		then
			break
		end
		preRank = parentRank
		
		if parentRank == ''
		then
			gen = false
		end
		
		-- 种
		if currRank == 'Q7432'
		then
			-- 总科(Q2136103)、亚目(Q5867959)、系(Q21061732)、亚类(Q30093105)、类(Q30093070)、亚派(Q10861375)、派(Q10861426)、下群(Q4226087)、亚群(Q6541077)、群(Q2981883)、总群(Q6054425)、高群(Q60445775)、下纲(Q2007442)、亚纲(Q5867051)、总纲(Q3504061)、高纲(Q60922428)、下门(Q2361851)、亚门(Q1153785)、下界(Q3150876)、亚界(Q2752679)
			if parentRank == ('Q2136103' or 'Q5867959' or 'Q21061732' or 'Q30093105' or 'Q30093070' or 'Q10861375' or 'Q10861426' or 'Q4226087' or 'Q6541077' or 'Q2981883' or 'Q6054425' or 'Q60445775' or 'Q2007442' or 'Q5867051' or 'Q3504061' or 'Q60922428' or 'Q2361851' or 'Q1153785' or 'Q3150876' or 'Q2752679')
			then
				gen = false
			end
		end
		
		if gen == true
		then
			local parentTaxonName = l.getData({'property', parent, 'P225'})
			local parentName = l.getData({'label', parent})
			l.setRank(parentRank, parentName, parentTaxonName)
			
			if parentTaxonName == 'Animalia'
			then
				colour = 'rgb(235,235,210)'
			end
		end
		
		-- 界
		if parentRank == 'Q36732' or step > maxStep
		then
			break
		end
		
		parent = l.getData({'property', 'raw', parent, 'P171'})
		parentRank = l.getData({'property', 'raw', parent, 'P105'})
	end
	
	local res = frame:expandTemplate{ title = 'Taxobox/core', args = {
		name = currName,
		image = l.getData({'property', 'raw', from, 'P18'}),
		image_caption = l.getData({'qualifier', from, 'P18', 'P2096'}),
		colour = colour,
		regnum = rankList.regnum,
		phylum = rankList.phylum,
		classis = rankList.classis,
		ordo = rankList.ordo,
		familia = rankList.familia,
		genus = rankList.genus,
		subgenus = rankList.subgenus,
		sectio = rankList.sectio,
		species = rankList.species,
		binomial = binomial,
		binomial_authority = binomialAuthority,
		status = l.transIUCN(l.getData({'property', 'raw', from, 'P141'})) or '',
		status_system = 'iucn3.1',
		range_map = l.getData({'property', 'raw', from, 'P181'}),
		range_map_caption = l.getData({'qualifier', from, 'P181', 'P2096'})
		}
	}
	return res
end

function l.getData(args)
	return gFrame:expandTemplate{ title = 'Wikidata', args = args }
end

function l.setRank(qid, article, taxonName, curr, authority)
	local link = "[[" .. article .. "]]"
	local partialItalics
	local normal
	
	if curr == true
	then
		partialItalics = l.bold(link .. " " .. l.ltalic(taxonName))
		normal = l.bold(link .. " " .. taxonName)
	else
		partialItalics = link .. " " .. l.ltalic(taxonName)
		normal = link .. " " .. taxonName
	end
	
	if qid == 'Q35409'
	then
		rankList['familia'] = normal
	elseif qid == 'Q7432'
	then
		local displayTaxonName = l.ltalic(string.gsub(taxonName, '([%u])[%l]+ ', '%1. '))
		if curr == true
		then
			rankList['species'] = l.bold(link .. ' ' .. displayTaxonName)
			binomial = l.ltalic(taxonName)
			binomialAuthority = authority
		else
			rankList['species'] = link .. ' ' .. displayTaxonName
		end
	elseif qid == 'Q3181348'
	then
		rankList['sectio'] = partialItalics
	elseif qid == 'Q3238261'
	then
		rankList['subgenus'] = partialItalics
	elseif qid == 'Q34740'
	then
		rankList['genus'] = partialItalics
	elseif qid == 'Q36602'
	then
		rankList['ordo'] = normal
	elseif qid == 'Q37517'
	then
		rankList['classis'] = normal
	elseif qid == 'Q38348'
	then
		rankList['phylum'] = normal
	elseif qid == 'Q36732'
	then
		rankList['regnum'] = normal
	end
end

function l.bold(str)
	return "'''" .. str .. "'''"
end

function l.ltalic(str)
	return "''" .. str .. "''"
end

function l.transIUCN(qid)
	if qid == 'Q3350324'
	then
		return 'NE'
	elseif qid == 'Q3245245'
	then
		return 'DD'
	elseif qid == 'Q211005'
	then
		return 'LC'
	elseif qid == 'Q719675'
	then
		return 'NT'
	elseif qid == 'Q278113'
	then
		return 'VU'
	elseif qid == 'Q11394'
	then
		return 'EN'
	elseif qid == 'Q219127'
	then
		return 'CR'
	elseif qid == 'Q239509'
	then
		return 'EW'
	elseif qid == 'Q123509'
	then
		return 'EX'
	end
end

return p