Модуль:Wikidata/Biology

Материал из Буквицы
Перейти к навигации Перейти к поиску

Содержит функции форматирования для биологических свойств и квалификаторов.


local p = {}

local function idFromStatement(s)
	if s.id then
		return mw.ustring.sub(s.id, 1, mw.ustring.find(s.id, '%$')-1)
	end
	return nil
end

function p._format_authors_and_ex(context, options, qualifiers)
	local options405 = options:extends({
				["value-module"] = "Wikidata/Biology",
				["value-function"] = "formatTaxonAuthorSnak",
				["conjunction"] = " & ",
			})
	local authors = ''
	if ( qualifiers.P405 ) then
		local options405 = options:extends({
			["value-module"] = "Wikidata/Biology",
			["value-function"] = "formatTaxonAuthorSnak",
			["conjunction"] = " & ",
		})

		authors = {}
		for _, qualifier in pairs( qualifiers.P405 ) do
			table.insert(authors, context.formatSnak( options405, qualifier ) )
		end
		authors = mw.text.listToText( authors, ", ", " & " )
	end
	if ( qualifiers.P697 ) then
		ex_authors = {}
		local options697 = options:extends({
			["value-module"] = "Wikidata/Biology",
			["value-function"] = "formatTaxonAuthorSnak",
			["conjunction"] = " & ",
		})
		for _, qualifier in pairs( qualifiers.P697 ) do
			table.insert(ex_authors, context.formatSnak( options697, qualifier ) )
		end
		authors = mw.text.listToText( ex_authors, ", ", " & " ) .. ' [[Ex (таксономия)|ex]] ' .. authors
	end
	return authors
end

function p._get428from566(context, options, statement)
	local id = idFromStatement(statement)
	if id == nil then return nil end
	local basionym = mw.wikibase.getBestStatements(id, 'P566')
	if basionym == nil or #basionym ~= 1 then return nil end
	local b_id = basionym[1].mainsnak.datavalue.value.id
	if b_id == nil then return nil end
	local b_name = mw.wikibase.getBestStatements(b_id, 'P225')
	if b_name == nil or #b_name ~= 1 or b_name[1].qualifiers == nil then return nil end
	local b405 = b_name[1].qualifiers.P405
	if b405 == nil then return nil end
	return p._format_authors_and_ex(context, options, b_name[1].qualifiers)
end

--Property:P225
function p.formatTaxonNameClaim( context, options, statement )
	local title = context.formatSnak( options, statement.mainsnak )

	local authors = ''
	local dates = ''
	--taxon author
	if ( statement.qualifiers ) then
		authors = p._format_authors_and_ex(context, options, statement.qualifiers)
		--Date of release
		if ( statement.qualifiers.P574 ) then
			dates = ', ' .. context.formatSnak( {}, statement.qualifiers.P574[1] )
		end
	end
	local basionym_author = p._get428from566(context, options, statement)
	if basionym_author then
		authors = '(' .. basionym_author .. ') ' .. authors
	end

	local result = options.frame:expandTemplate{ title = 'btname', args = { title, authors } }
		.. dates
		.. context.formatRefs( options, statement )
	return result
end

-- Qualifier P405
function p.formatTaxonAuthorSnak( context, options, value )
	local entityId = value.id

	local abbr = nil
	local statements = mw.wikibase.getBestStatements( entityId, 'P428' )
					or mw.wikibase.getBestStatements( entityId, 'P835' )
					or {}
	for _, statement in pairs( statements ) do
		if statement.mainsnak.snaktype == 'value' then
			abbr = statement.mainsnak.datavalue.value
			break
		end
	end

	local link = mw.wikibase.getSitelink( entityId ) or ( ":d:" .. entityId )
	local label = abbr or mw.wikibase.getLabel( entityId ) or entityId

	return "[[" .. link .. "|" .. label .. "]]"
end

return p