Module:Taxonbar
Taxonbar (edit talk history links # /subpages /doc /doc edit /sbox /sbox diff /test)
![]() | This Lua module is used on approximately 378,000 pages, or roughly 43% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |

![]() | This module depends on the following other modules: |
Related pages |
---|
This module contains the code of {{Taxonbar}}. To use Taxonbar, follow instructions at Template:Taxonbar/doc.
Configuration
Parameters and databases are set by Module:Taxonbar/conf.
Taxon identifiers
Testcases
For testcases use: {{Taxonbar | from=QID}}
Rhododendron maximum Lua error in package.lua at line 80: module 'Module:No globals' not found. Asclepias syriaca Lua error in package.lua at line 80: module 'Module:No globals' not found. Peach (Prunus persica) Lua error in package.lua at line 80: module 'Module:No globals' not found. Puma (genus) Lua error in package.lua at line 80: module 'Module:No globals' not found. Dog (Canis lupus familiaris) Lua error in package.lua at line 80: module 'Module:No globals' not found. Eastern bluebird (Sialia sialis) Lua error in package.lua at line 80: module 'Module:No globals' not found. Honey bee (Apis) Lua error in package.lua at line 80: module 'Module:No globals' not found. Western honey bee (Apis mellifera) Lua error in package.lua at line 80: module 'Module:No globals' not found. Clipper butterfly (Parthenos sylvia) Lua error in package.lua at line 80: module 'Module:No globals' not found. Turkey tail (Trametes versicolor) Lua error in package.lua at line 80: module 'Module:No globals' not found. Button/portobello mushroom (Agaricus bisporus) Lua error in package.lua at line 80: module 'Module:No globals' not found.
Module editing — to-do list
See also
- {{Taxonbar}}, the original template
- Module:Taxonbar/conf, Configuration of this module (add or edit sources)
- Template talk:Taxonbar, Taxonbar discussion
- Template:Taxonbar/testcases, Taxonbar testcases
- Module:Taxonbar/sandbox, Taxonbar sandbox
- Module:Authority control
- gl:Módulo:Taxonbar, original code source for this template
require('Module:No globals')
local conf = require( "Module:Taxonbar/conf" )
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 getLink( property, val )
local link = ""
if mw.ustring.find( val, '//' ) then
link = val
else
if type(property) == 'number' then
local entityObject = mw.wikibase.getEntityObject('P'..property)
local dataType = entityObject.datatype
if dataType == "external-id" then
local formatterURL = entityObject:getBestStatements('P1630')[1]
if formatterURL then link = formatterURL.mainsnak.datavalue.value end
elseif dataType == "url" then
local subjectItem = entityObject:getBestStatements('P1629')[1]
if subjectItem then
local officialWebsite = mw.wikibase.getEntityObject(subjectItem.mainsnak.datavalue.value.id):getBestStatements('P856')[1]
if officialWebsite then link = officialWebsite.mainsnak.datavalue.value end
end
elseif dataType == "string" then
local formatterURL = entityObject:getBestStatements('P1630')[1]
if formatterURL then
link = formatterURL.mainsnak.datavalue.value
else
local subjectItem = entityObject:getBestStatements('P1629')[1]
if subjectItem then
local officialWebsite = mw.wikibase.getEntityObject(subjectItem.mainsnak.datavalue.value.id):getBestStatements('P856')[1]
if officialWebsite then link = officialWebsite.mainsnak.datavalue.value end
end
end
end
elseif type(property) == 'string' then
link = property
end
link = mw.ustring.gsub(link, '$1', val)
end
link = mw.ustring.gsub(link, '^[Hh][Tt][Tt][Pp]([Ss]?)://', 'http%1://') -- fix wikidata URL
val = mw.ustring.match(val, '([^=/]*)/?$') -- get display name from end of URL
if mw.ustring.find( link, '//' ) then
return '['..link..' '..val..']'
elseif link == "" then
return val
else
return '<span class="external">[['..link..'|'..val..']]</span>'
end
end
local function createRow( id, label, rawValue, link, withUid )
if link then
local outStr = '*<span style="white-space:nowrap;">' .. label .. ' <span'
if withUid then outStr = outStr..' class="uid"' end
return outStr..'>' .. link .. '</span></span>\n'
else
return '* <span class="error">The identifier ' .. id .. ' ' .. rawValue .. ' is not valid.</span>\n'
end
end
local function copyTable(inTable)
if type(inTable) ~= 'table' then return inTable end
local outTable = setmetatable({}, getmetatable(inTable))
for key, value in pairs (inTable) do outTable[copyTable(key)] = copyTable(value) end
return outTable
end
local p = {}
function p.authorityControlTaxon( frame )
local parentArgs = copyTable(frame:getParent().args)
local stringArgs = false
local fromForCount, firstRow, rowCount = 1, 0, 0
--Cleanup args
for k, v in pairs( frame:getParent().args ) do
if type(k) == 'string' then
--make args case insensitive
local lowerk = mw.ustring.lower(k)
if not parentArgs[lowerk] or parentArgs[lowerk] == '' then
parentArgs[lowerk] = v
parentArgs[k] = nil
end
--remap abc to abc1
if not mw.ustring.find(lowerk,"%d$") then --if no number at end of param
if not parentArgs[lowerk..'1'] or parentArgs[lowerk..'1'] == '' then
parentArgs[lowerk..'1'] = v
parentArgs[lowerk] = nil
end
end
if v and v ~= '' then
--find highest from param
if mw.ustring.sub(lowerk,1,4) == "from" then
local fromNumber = tonumber(mw.ustring.sub(lowerk,5,-1))
if fromNumber and fromNumber >= fromForCount then fromForCount = fromNumber end
elseif mw.ustring.sub(lowerk,1,3) == "for" then
local forNumber = tonumber(mw.ustring.sub(lowerk,4,-1))
if forNumber and forNumber >= fromForCount then fromForCount = forNumber end
elseif mw.ustring.lower(v) ~= 'no' and lowerk ~= 'for' then
stringArgs = true
end
end
end
end
--Setup navbox
local navboxParams = {
name = 'Taxonbar',
bodyclass = 'hlist',
listclass = '',
groupstyle = 'text-align: left;',
}
for f = 1,fromForCount,1
do
local elements, title = {}, {}
--cleanup parameters
if parentArgs['from'..f] == '' then parentArgs['from'..f] = nil end
if parentArgs['for'..f] == '' then parentArgs['for'..f] = nil end
--remap aliases
for _, a in pairs( conf.aliases ) do
local alias, name = mw.ustring.lower(a[1]), mw.ustring.lower(a[2])
if parentArgs[alias..f] and not parentArgs[name..f] then
parentArgs[name..f] = parentArgs[alias..f]
parentArgs[alias..f] = nil
end
end
--Fetch Wikidata item
local item = mw.wikibase.getEntity(parentArgs['from'..f])
local label = item and item:getLabel()
if label and label ~= '' then
title = mw.title.new(label)
if not title then title = mw.title.getCurrentTitle() end
else
title = mw.title.getCurrentTitle()
end
if (not parentArgs['wikidata'..f] or parentArgs['wikidata'..f] == '') and (title.namespace == 0) then
if parentArgs['from'..f] then parentArgs['wikidata'..f] = parentArgs['from'..f] elseif item then parentArgs['wikidata'..f] = item.id end
end
if title.namespace == 0 or stringArgs then --Only in the main namespace or if there are manual overrides
local sourcesFound = false
for _, params in pairs( conf.databases ) do
params[1] = mw.ustring.lower(params[1])
--Wikidata fallback if requested
if (item ~= nil and item.claims ~= nil) and params[3] ~= 0 and (not parentArgs[params[1]..f] or parentArgs[params[1]..f] == '') then
local wikidataIds = getIdsFromWikidata( item, 'P' .. params[3] )
if wikidataIds[1] then
parentArgs[params[1]..f] = wikidataIds[1]
end
end
local val = parentArgs[params[1]..f]
if val and val ~= '' and mw.ustring.lower(val) ~= 'no' and params[3] ~= 0 then
table.insert( elements, createRow( params[1], params[2] .. ':', val, getLink( params[3], val ), true ) )
if params[1] ~= 'wikidata' then sourcesFound = true end
end
end
--Generate navbox title
if sourcesFound then
rowCount = rowCount + 1
if firstRow == 0 then firstRow = f end
if parentArgs['for'..f] and parentArgs['for'..f] ~= '' then
navboxParams['group'..f] = "''" .. parentArgs['for'..f] .. "''"
else
navboxParams['group'..f] = ""
end
navboxParams['list'..f] = table.concat( elements )
end
end
end
if rowCount > 0 then
local Navbox = require('Module:Navbox')
if rowCount > 1 then
navboxParams['title'] = "[[Help:Taxon identifiers|Taxon identifiers]]"
--add missing names
for f = 1,fromForCount,1
do
if navboxParams['group'..f] == "" then
navboxParams['group'..f] = "''" .. mw.wikibase.getEntity(parentArgs['wikidata'..f]):getLabel().."''"
end
end
if rowCount > 2 then
navboxParams['navbar'] = 'plain'
else
navboxParams['state'] = 'off'
navboxParams['navbar'] = 'off'
end
else
navboxParams['group'..firstRow] = '[[Help:Taxon identifiers|Taxon identifiers]]<br />' .. navboxParams['group'..firstRow]
end
return Navbox._navbox(navboxParams)
else
return ""
end
end
return p