Module:Taxonbar

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 '* ' .. label .. ' ' .. link .. ' \n' else return '* ' .. label .. ' ' .. link .. ' \n' end else return '* The identifier ' .. id .. ' ' .. rawValue .. ' is not valid. \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', 'BHL', 687 }, { 'BioLib', 'BioLib', 838 }, { 'BugGuide', 'BugGuide', 2464 }, { 'ButMoth', 'ButMoth', 3060 }, { 'Conifers', 'Conifers.org', 1940 }, { 'eBird', 'eBird', 3444 }, { 'EoL', 'EoL', 830 }, { 'EPPO', 'EPPO', 3031 }, { 'FaunaEuropaea', 'FE', 1895 }, { 'FishBase', 'FishBase', 938 }, { 'FloraBase', 'FloraBase', 3101 }, { 'Fossilworks', 'Fossilworks', 842 }, { 'FNA', 'FNA', 1727 }, { 'FoAO', 'FoAO', 3100 }, { 'FoC', 'FoC', 1747 }, { 'Fungorum', 'Fungorum', 1391 }, { 'GBIF', 'GBIF', 846 }, { 'GrassBase', 'GrassBase', 1832 }, { 'GRIN', 'GRIN', 1421 }, { 'IBC', 'IBC', 3099 }, { 'ICTV', 'ICTV', 1076 }, { 'iNaturalist', 'iNaturalist', 3151 }, { 'IPNI', 'IPNI', 961 }, { 'IPNI2', 'IPNI author', 586 }, { 'ITIS', 'ITIS', 815 }, { 'IUCN', 'IUCN', 627 }, { 'LepIndex', 'LepIndex', 3064 }, { 'LPSN', 'LPSN', 1991 }, { 'MSW', 'MSW', 959 }, { 'MycoBank', 'MycoBank', 962 }, { 'NBN', 'NBN', 3240 }, { 'NCBI', 'NCBI', 685 }, { 'Panartic', 'Panartic Flora', 2434 }, { 'Plantarium', 'Plantarium', 3102 }, { 'PlantList', 'Plant List', 1070 }, { 'PLANTS', 'PLANTS', 1772 }, { 'Plazi', 'Plazi', 1992 }, { 'Species+', 'Species+', 2040 }, { 'SPRAT', 'SPRAT', 2455 }, { 'Tropicos', 'Tropicos', 960 }, { 'USDAPLANTS', 'PLANTS', 1772 }, { 'VASCAN', 'VASCAN', 1745 }, { 'Watson', 'Watson & Dallwitz', 1761 }, { 'WCSP', 'WCSP', 3591 }, { 'WoRMS', 'WoRMS', 850 }, { 'WSC', '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 = '' --end if #elements ~= 0 then return Navbox._navbox( {				name = 'Taxonbar',				bodyclass = 'hlist',				listclass = '',				group1 = 'Taxon Identifiers', --.. elementscats,				list1 = table.concat( elements )				} ) else return "" end end end

return p