Module:Taxonbar

From Omniversalis

Documentation for this module may be created at Module:Taxonbar/doc

require('Module:No globals')

local function append(str, c, length)
    while str:len() < length do
        str = c .. str
    end
    return str
end

local function getIdsFromWikidata( item, property )
    local ids = {}
    if not item.claims[property] then
        return ids
    end
    for _, statement in pairs( item.claims[property] ) do
		if statement.mainsnak.datavalue then
			table.insert( ids, statement.mainsnak.datavalue.value )
		end
    end
    return ids
end

local function getLinkFromWikidata( property, val )
	local link = ''
	if mw.ustring.find( val, 'http' ) then
		link = val
		val = mw.ustring.match(val, '.*=(.-)$') or val
	else
		link = mw.wikibase.getEntityObject('P'..property).claims.P1630[1].mainsnak.datavalue.value
		link = mw.ustring.gsub(link, '$1', val)
	end
	link = mw.ustring.gsub(link, '^[Hh][Tt][Tt][Pp]([Ss]?)&#58;//', 'http%1://')
	return '[' .. link .. ' ' .. val .. ']'
end

local function createRow( id, label, rawValue, link, withUid )
    if link then
        if withUid then
            return '*<span style="white-space:nowrap;">' .. label .. ' <span class="uid">' .. link .. '</span></span>\n'
        else
            return '*<span style="white-space:nowrap;">' .. label .. ' ' .. link .. '</span>\n'
        end
    else
        return '* <span class="error">The identifier ' .. id .. ' ' .. rawValue .. ' is not valid.</span>\n'
    end
end

--In this order: name of the parameter, label, propertyId in Wikidata, formatting function
local conf = {
    --Biology
    { 'APDB', 'APDB', 2036 },
    { 'AlgeaBase', '[[AlgaeBase]]', 1348 },
    { 'Araneae', 'Araneae', 3594 },
    { 'ARKive', '[[ARKive]]', 2833 },
    { 'Avibase', 'Avibase', 2026 },
    { 'BacDive', '[[BacDive]]', 2946 },
    { 'BHL', '[[Biodiversity Heritage Library|BHL]]', 687 },
    { 'BioLib', 'BioLib', 838 },
    { 'BugGuide', '[[BugGuide]]', 2464 },
    { 'ButMoth', '[[Natural History Museum#Butterflies and Moths of the World|ButMoth]]', 3060 },
    { 'Conifers', 'Conifers.org', 1940 },
    { 'eBird', '[[eBird]]', 3444 },
    { 'EoL', '[[Encyclopedia of Life|EoL]]', 830 },
    { 'EPPO', '[[European and Mediterranean Plant Protection Organization|EPPO]]', 3031 },
    { 'FaunaEuropaea', '[[Fauna Europaea|FE]]', 1895 },
    { 'FishBase', '[[FishBase]]', 938 },
    { 'FloraBase', '[[FloraBase]]', 3101 },
    { 'Fossilworks', '[[Fossilworks]]', 842 },
    { 'FNA', '[[Flora of North America|FNA]]', 1727 },
    { 'FoAO', 'FoAO', 3100 },
    { 'FoC', '[[Flora of China|FoC]]', 1747 },
    { 'Fungorum', '[[Index Fungorum|Fungorum]]', 1391 },
    { 'GBIF', '[[Global Biodiversity Information Facility|GBIF]]', 846 },
    { 'GrassBase', '[[GrassBase]]', 1832 },
    { 'GRIN', '[[Germplasm Resources Information Network|GRIN]]', 1421 },
    { 'IBC', '[[Handbook of the Birds of the World|IBC]]', 3099 },
    { 'ICTV', '[[International Committee on Taxonomy of Viruses|ICTV]]', 1076 },
    { 'iNaturalist', '[[iNaturalist]]', 3151 },
    { 'IPNI', '[[International Plant Names Index|IPNI]]', 961 },
	{ 'IPNI2', '[[International Plant Names Index|IPNI]] author', 586 },
    { 'ITIS', '[[Integrated Taxonomic Information System|ITIS]]', 815 },
    { 'IUCN', '[[IUCN Red List|IUCN]]', 627 },
    { 'LepIndex', '[[Natural History Museum#The Global Lepidoptera Names Index|LepIndex]]', 3064 },
    { 'LPSN', '[[List of Prokaryotic names with Standing in Nomenclature|LPSN]]', 1991 },
	{ 'MSW', '[[Mammal Species of the World|MSW]]', 959 },
    { 'MycoBank', '[[MycoBank]]', 962 },
    { 'NBN', '[[National Biodiversity Network|NBN]]', 3240 },
    { 'NCBI', '[[National Center for Biotechnology Information|NCBI]]', 685 },
	{ 'Panartic', 'Panartic Flora', 2434 },
    { 'Plantarium', 'Plantarium', 3102 },
    { 'PlantList', '[[The Plant List|Plant List]]', 1070 },
	{ 'PLANTS', '[[Natural Resources Conservation Service#Plants|PLANTS]]', 1772 },
    { 'Plazi', '[[Plazi]]', 1992 },
    { 'Species+', '[[CITES|Species+]]', 2040 },
    { 'SPRAT', '[[Species Profile and Threats Database|SPRAT]]', 2455 },
    { 'Tropicos', '[[Tropicos]]', 960 },
    { 'USDAPLANTS', '[[Natural_Resources_Conservation_Service#Plants|PLANTS]]', 1772 },
    { 'VASCAN', 'VASCAN', 1745 },
	{ 'Watson', 'Watson & Dallwitz', 1761 },
    { 'WCSP', '[[World Checklist of Selected Plant Families|WCSP]]', 3591 },
    { 'WoRMS', '[[World Register of Marine Species|WoRMS]]', 850 },
    { 'WSC', '[[World Spider Catalog|WSC]]', 3288 },
    { 'Xeno-canto', 'Xeno-canto', 2426 },
    { 'ZooBank', '[[ZooBank]]', 1746, }
}

local aliases = {
	{ 'FE', 'FaunaEuropaea' },
	{ 'PaleoDB', 'Fossilworks' },
	{ 'FW', 'Fossilworks' },
	{ 'indexfungorum', 'Fungorum' },
	{ 'species', 'Species+' },
	{ 'WCSPF', 'WCSP' },
	{ 'Xenocanto', 'Xeno-canto' },
	{ 'NameBank', 'uBio' }
}

local p = {}

function p.authorityControlTaxon( frame )
    local parentArgs = frame:getParent().args
    local stringArgs = false
    --Make args case insensitive
    local tempArgs = {unpack(parentArgs)}
    for para, value in pairs( tempArgs ) do
    	if type(para) == string then
    		local lpara = mw.ustring.lower(para)
    		if not parentArgs[lpara] then
    			parentArgs[lpara] = value
    			parentArgs[para] = nil
    		end
    		if value and value ~= '' and lpara ~= 'from' then stringArgs = true end
		end
	end
	--remap aliases
	for _, j in pairs( aliases ) do
		local k, m = mw.ustring.lower(j[1]), mw.ustring.lower(j[2])
		if parentArgs[k] and not parentArgs[m] then
			parentArgs[m] = parentArgs[k]
			parentArgs[k] = nil
		end
	end
    --Create rows
    local elements, title = {}, {}
    local item = mw.wikibase.getEntityObject(parentArgs['from'])
    if parentArgs['from'] ~= nil then title = mw.title.new(item:getLabel()) else title = mw.title.getCurrentTitle() end
	if title.namespace == 0 or stringArgs then --Only in the main namespace or if there are manual overrides
		--redirect PND to GND
		if (parentArgs.GND == nil or parentArgs.GND == '') and parentArgs.PND ~= nil and parentArgs.PND ~= '' then
			parentArgs.GND = parentArgs.PND
		end

		--Wikidata fallback if requested
		if item ~= nil and item.claims ~= nil then
			for _, params in pairs( conf ) do
				if params[3] ~= 0 then
					params[1] = mw.ustring.lower(params[1])
					local val = parentArgs[params[1]]
					if not val or val == '' then
						local wikidataIds = getIdsFromWikidata( item, 'P' .. params[3] )
						if wikidataIds[1] then
							parentArgs[params[1]] = wikidataIds[1]
						end
					end
				end
			end
		end

		--Configured rows
		local rct = 0
		for k, params in pairs( conf ) do
			params[1] = mw.ustring.lower(params[1])
			local val = parentArgs[params[1]]
			if val and val ~= '' and mw.ustring.lower(val) ~= 'no' then
				table.insert( elements, createRow( params[1], params[2] .. ':', val, getLinkFromWikidata( params[3], val ), true ) )
				rct = rct + 1
			end
		end
		local Navbox = require('Module:Navbox')
		--local elementscats = ''
		--if rct > 13 then
		--	elementscats  = '[[Category:AC with ' .. rct .. ' elements]]'
		--end
	
		if #elements ~= 0 then
			return Navbox._navbox( {
				name  = 'Taxonbar',
				bodyclass = 'hlist',
				listclass = '',
				group1 = '[[Help:Taxon identifiers|Taxon Identifiers]]', --.. elementscats,
				list1 = table.concat( elements )
				} )
		else
			return ""
		end
	end
end

return p