Module:Biota infobox/core
Appearance
![]() | This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
![]() | 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 started out as a quick module for template {{paraphyletic group}}, but has morphed into a module to emulate the {{taxobox/core}}. Handling for templates that call the core has been added for {{taxobox}}, {{Automatic taxobox}} and {{Speciesbox}}, but is incomplete.
Data subpages
Examples:
- User:Jts1882/taxobox/manual (manual taxobox version)
- User:Jts1882/taxobox/auto (automatic taxobox version)
- User:Jts1882/taxobox/species (speciesbox version)
- User:Jts1882/taxobox/infra (subspeciesbox and infraspeciesbox versions)
- User:Jts1882/taxobox/para (paraphyletic group versions)
- User:Jts1882/taxobox/Wikidata (wikidata versions)
Sandbox version at Module:Sandbox/Jts1882/Biota infobox.
Usage
{{#invoke:Biota infobox|function_name}}
require('Module:No globals')
local p = {}
local templateArgs = {}
local info = {}
local paramData = require( 'Module:Biota infobox/data' ) -- contains the taxon ranks in order
local autotaxa = require("Module:Autotaxobox")
local parameters = require( 'Module:Biota infobox/param' )
--[[ ##################### CORE FUNCTIONS ###################################
this core function emulates Template:Taxobox/core
it is followed by functions handling the different type of entry
]]
p.main = function(frame)
-- this function emulates Template:Taxobox/core when called from taxobox feeder templates
-- i.e. Taxobox, Automatic taxobox, Speciesbox, etc
parameters.getArgs(frame)
templateArgs['image_upright'] = templateArgs['upright'] or 1
templateArgs['image2_upright'] = templateArgs['upright2'] or 1
info.headerColor = templateArgs['colour']
return p._core
--TODO check it works
end
-- this is the core function, called either from main() or auto()
p.core = function(frame, args, localinfo)
templateArgs = args
info = localinfo
return p._core(frame)
end
p._core = function(frame)
-- TODO use mw.title.getCurrentTitle().basePageTitle
info.name = templateArgs['name'] or tostring( mw.title.getCurrentTitle() ) --.rootPageTitle )
info.headerColor = p.getTaxoboxColor(frame) -- so only transverse taxobox heirarchy once
--[[ create table (two versions)
1. use mwhtml library to build table in taxoboxTable
2. use wikitest to build table in wikitextTable
]]
local taxoboxTable = mw.html.create('table'):addClass('infobox'):addClass('biota')
:addClass('biota-infobox')
local wikitextTable = '\n{| class="infobox biota biota-infobox" '
info.subheader = ''
if templateArgs['subheader'] and templateArgs['subheader'] ~= '' then
info.subheader='<div style="font-weight:normal;font-size:smaller;">'..templateArgs['subheader']..'</div>'
end
--TODO do we need additional handling to check {{geological range}} templete
-- or handle oldest_fossil and youngest_fossil (these don't seem to be used now)
-- Note: taxobox/core uses temporal_range
local temporalRange = ''
if templateArgs['temporal_range'] then
temporalRange = '<div><small>Temporal range: ' .. templateArgs['temporal_range'] .. '</small></div>' -- use <div> rather than <br/>
end
local rowHeader = taxoboxTable:tag('tr'):tag('th'):attr('colspan', '2'):addClass('section-header')
:cssText('background:' .. info.headerColor .. ';')
:wikitext(info.name .. info.subheader .. temporalRange)
wikitextTable = wikitextTable .. '\n|-\n! colspan="2" class="section-header" style="background:' .. info.headerColor .. ';" '
.. '|' .. info.name .. info.subheader .. temporalRange
-- add images
wikitextTable = wikitextTable .. p.addImageSection(frame, taxoboxTable, 'image')
.. p.addImageSection(frame, taxoboxTable, 'image2')
--add conservation status
wikitextTable = wikitextTable .. p.addStatusSection(frame, taxoboxTable, 'status')
.. p.addStatusSection(frame, taxoboxTable, 'status2')
-- add taxonomy table (uses entered parameters or automatic taxonomy system)
wikitextTable = wikitextTable .. p.addTaxonomySection(frame, taxoboxTable)
-- add sections with binomial, trinomial, type species/genus (with authorities)
wikitextTable = wikitextTable
.. p.addTaxonSection(frame, taxoboxTable, 'binomial', 'Bionomial name')
.. p.addTaxonSection(frame, taxoboxTable, 'trinomial', 'Trionomial name')
.. p.addTaxonSection(frame, taxoboxTable, 'type_genus', 'Type genus')
--TODO type_ichnogenus, type_oogenus
.. p.addTaxonSection(frame, taxoboxTable, 'type_species', 'Type species')
--TODO type_ichnospecies, type_oospecies
.. p.addTaxonSection(frame, taxoboxTable, 'type_strain', 'Type strain')
-- add sections showing subdivisions (i.e. child taxa)
wikitextTable = wikitextTable
.. p.addListSection(frame, taxoboxTable, 'subdivision', templateArgs['subdivision_ranks'] or 'Subdivisions')
.. p.addListSection(frame, taxoboxTable, 'possible_subdivision', templateArgs['possible_subdivision_ranks'] or 'Possible subdivisions')
-- diversity section (TODO consider how best to handle)
wikitextTable = wikitextTable .. p.addTaxonSection(frame, taxoboxTable, 'diversity', 'Diversity')
-- show included groups (accepts 'included' or 'includes') [used for paraphyletic taxa]
wikitextTable = wikitextTable .. p.addListSection(frame, taxoboxTable, 'includes', 'Groups included')
--p.addListSection(frame, taxoboxTable, 'included', 'Groups included') -- use alias
--add range map (should this be below binomial/trinomial?)
wikitextTable = wikitextTable .. p.addImageSection(frame, taxoboxTable, 'range_map')
-- show excluded groups (accepts 'excluded' or 'excludes') [used for paraphyletic taxa]
local excludedHeaderText = '[[Cladistics|Cladistically]] included but traditionally excluded taxa'
wikitextTable = wikitextTable .. p.addListSection(frame, taxoboxTable, 'excludes', excludedHeaderText)
-- add addition binomials, trinomial and range maps
wikitextTable = wikitextTable
.. p.addTaxonSection(frame, taxoboxTable, 'binomial2', 'Bionomial name (2)')
--p.addTaxonSection(frame, taxoboxTable, 'trinomial2', 'Trionomial name (2)')
.. p.addImageSection(frame, taxoboxTable, 'range_map2')
.. p.addTaxonSection(frame, taxoboxTable, 'binomial3', 'Bionomial name (3)')
--p.addTaxonSection(frame, taxoboxTable, 'trinomial3', 'Trionomial name (3)')
.. p.addImageSection(frame, taxoboxTable, 'range_map3')
.. p.addTaxonSection(frame, taxoboxTable, 'binomial4', 'Bionomial name (4)')
--p.addTaxonSection(frame, taxoboxTable, 'trinomial4', 'Trionomial name (4)')
.. p.addImageSection(frame, taxoboxTable, 'range_map4')
-- add synonyms section
wikitextTable = wikitextTable .. p.addListSection(frame, taxoboxTable, 'synonyms', '[[Synonym (taxonomy)|Synonyms]]')
-- add debug/tracking info section
if info.debug then
wikitextTable = wikitextTable ..p.addListSection(frame, taxoboxTable, 'debug', 'Debug/tracking info')
end
------------------add templateSyles and return taxobox table---------------------------
local src = "Template:Biota infobox/styles.css" -- TemplateStyles file
wikitextTable = wikitextTable .. '\n|}'
local output = wikitextTable -- output the wikitext table
--local output = tostring(taxoboxTable) -- output the mw.html table
return p.templateStyle( frame, src ) .. output .. info.parameterCategory
-- .. (info.parameterCategory or "")
end -- End the main function.
--[[ ====================================================================================
function to add conservation sections
uses template {{taxobox/species|{{{status_system|}}}|{{{status|}}}|{{{status_ref|}}}|extinct={{{extinct|}}} }} }}
--]]
function p.addStatusSection(frame, taxoboxTable, status)
-- must use table container to handle template output
-- don't use header or content cells; the 'taxobox/species' template handles it
-- just need to add background colour on the row
local wikiText = ""
if templateArgs[status] and templateArgs[status] ~= "" then
local status = frame:expandTemplate{ title = 'taxobox/species',
args = {templateArgs[status..'_system'] or '',
templateArgs[status] or '',
templateArgs[status..'_ref'] or '',
extinct=templateArgs['extinct'] or ''
}
}
local row = taxoboxTable:tag('tr')
--:cssText('background:' .. p.getTaxoboxColor(frame) .. ';')
local cell = row:tag('td'):attr('colspan', '2')
:wikitext('\n{|\n|- style="background:' .. info.headerColor .. ';"')
:wikitext('\n'..tostring(status))
:wikitext('\n|}')
wikiText = '\n|- colspan="2" style="background:' .. info.headerColor .. ';" '
.. '\n' ..tostring(status)
end
return wikiText
end
--[[ ==============================================================================
function to add sections for taxonomic information with authorities ('_authority' suffix)
e.g. binomial, trinomial, type_species, type_genus;
diversity also handled here
--]]
function p.addTaxonSection(frame, taxoboxTable, target, headerText)
local wikiText = ""
-- return if we don't have value
if not templateArgs[target] or templateArgs[target] == '' then return "" end
local bold = "" -- variable for bolding binomial and trinomial (type genus/species etc are not bolded)
local showHeader = true
local listHeader = (templateArgs[target..'_text'] or headerText)
-- custom processing section
--if target == 'binomial' or target == 'binomial2' or target == 'binomial3' or target == 'binomial4' then
if target == 'binomial' then
headerText = '[[Binomial nomenclature|' .. listHeader .. ']]'
bold ="'''"
--elseif target == 'trinomial' or target == 'trinomial2' or target == 'trinomial3' or target == 'trinomial4' then
elseif target == 'trinomial' then
local trinomenLink = "Trinomen" -- for zoological or default
if info.auto == "infraspeciesbox" then trinomenLink = "Infraspecific name (botany)" end -- regnum contains plant/fung/Archaeplastida"
headerText = '[['.. trinomenLink .. '|' .. headerText .. ']]'
bold ="'''"
elseif target == 'binomial2' or target == 'binomial3' or target == 'binomial4'
or target == 'trinomial2' or target == 'trinomial3' or target == 'trinomial4' then
showHeader = false
bold ="'''"
elseif target == 'diversity' then
headerText = '[[Biodiversity|' .. headerText .. ']]'
else
--TODO is any handling needed for other option
headerText = '[[' .. headerText .. ']]'
end
if templateArgs[target..'_ref'] then
headerText = headerText .. templateArgs[target..'_ref'] .. '\n'
end
local contentString = '' -- content for the content cell
if target == 'diversity' and templateArgs['diversity'] ~= "" then
if templateArgs[target..'_link'] and templateArgs[target..'_link'] ~= "" then
contentString = '\n[[' .. templateArgs[target..'_link'] .. '|' .. templateArgs[target] .. ']]\n'
else
contentString = '\n' .. templateArgs[target] .. '\n'
end
else
-- taxon name and authority (binomial, type species, etc)
local authorityString = ''
if templateArgs[target..'_authority'] then
authorityString = '<br/><small>' .. templateArgs[target..'_authority'] .. '</small>' -- \n'
end
contentString = bold .. templateArgs[target] .. bold .. authorityString .. '\n'
--contentString = '\n' .. bold .. templateArgs[target] .. bold .. authorityString .. '\n' -- extra <p> inserted
end
-- add table rows
if templateArgs[target] and templateArgs[target] ~= '' then
if showHeader then
local rowHeader = taxoboxTable:tag('tr')
rowHeader :tag('th')
:attr('colspan', '2')
:addClass('section-header')
:cssText('background:' .. info.headerColor .. ';')
:wikitext( headerText )
wikiText = wikiText .. '\n|- \n! colspan="2" class="section-header" style="background:' .. info.headerColor .. ';" '
.. '|' .. headerText
end
local rowList = taxoboxTable:tag('tr')
rowList :tag('td')
:attr('colspan', '2')
--:addClass('section-content')
:addClass('taxon-section')
--:cssText('text-align:center;') --font-weight:bold;')
:wikitext( contentString )
wikiText = wikiText .. '\n|- \n| colspan="2" class="taxon-section" |' .. contentString
end
return wikiText
end
--[[ ====================================================================================
function to produce sections for included taxa (subdivisions), synonyms, etc
- each consists of two rows containing a header and the content
- for paraphyletic groups it has include(d|s) and exclude(d|s) groups
- for general taxobox it has subdivision and possible subdivision (disabled here)
- any can be modified for other purposes (e.g. sisters) by changing the header taxt with a -text parameter
]]
function p.addListSection(frame, taxoboxTable, target, headerText)
if not templateArgs[target] or templateArgs[target] == "" then return "" end -- redundant for now
local wikiText = ""
local refString = ''
if templateArgs[target..'_ref'] then
--add '_ref' option for 'synonym_ref' (the rest get it as a bonus) TODO check example
refString = templateArgs[target..'_ref'] --.. '\n'
end
local listHeader = (templateArgs[target..'_text'] or headerText) .. refString
-- add table rows
if templateArgs[target] and templateArgs[target] ~= ''then
local rowHeader = taxoboxTable:tag('tr')
rowHeader :tag('th')
:attr('colspan', '2')
:addClass('section-header')
:cssText('background:' .. info.headerColor .. ';')
:wikitext(listHeader)
wikiText = wikiText .. '\n|- \n! colspan="2" class="section-header" style="background:' .. info.headerColor .. ';" '
.. '|' .. listHeader
local rowList = taxoboxTable:tag('tr')
rowList :tag('td')
:attr('colspan', '2')
:addClass('section-content')
:wikitext('\n' .. templateArgs[target] .. '\n' )
wikiText = wikiText .. '\n|- \n| colspan="2" class="section-content" '
.. '|' .. '\n' .. templateArgs[target] .. '\n'
end
return wikiText
end
-----------------------------------------------------------------------------------------------
--[[ ============================================================================
function to add image sections
- used for illustrative images at top and for range maps
--]]
function p.addImageSection(frame, taxoboxTable, target)
local wikiText = ""
if templateArgs[target] and templateArgs[target] ~= '' then
local imageWidth = ''
local imageCaptionString = ''
if templateArgs[target..'_caption'] then
--imageCaptionString = '<br /><small><div style="text-align:center">' .. templateArgs[target .. '_caption'] .. '</div></small>'
--imageCaptionString = '<br /><div>' .. templateArgs[target .. '_caption'] .. '</div>'
imageCaptionString = templateArgs[target .. '_caption']
end
local imageAltString = ''
if templateArgs[target..'_alt'] then
imageAltString = templateArgs[target..'_alt']
elseif templateArgs[target..'_caption'] then
imageAltString = templateArgs[target..'_caption'] -- use caption for alt text if none provided
end
local upright = templateArgs['image_upright'] or 1
local InfoboxImage = require( 'Module:InfoboxImage' ) --, parent.args[target] )
local params = { args = { image = templateArgs[target],
size = templateArgs[target..'_width'],
sizedefault = 'frameless',
alt = imageAltString,
upright = templateArgs[target..'_upright'] or upright
}
}
local image = InfoboxImage.InfoboxImage( params )
--local rowImage = taxoboxTable:tag('tr')
local rowImage = mw.html.create('tr')
rowImage:tag('td')
:attr('colspan', '2')
:addClass("image-section")
:wikitext(image)
--:wikitext(imageCaptionString)
taxoboxTable:node(rowImage)
wikiText= wikiText .. '\n|- \n|colspan="2" class="image-section" |' .. image
if imageCaptionString ~= "" then -- only insert row if caption string
local rowImageCaption = mw.html.create('tr')
rowImageCaption:tag('td')
:attr('colspan', '2')
:addClass("image-section")
--:wikitext(image)
:wikitext(imageCaptionString)
taxoboxTable:node(rowImageCaption)
wikiText = wikiText .. '\n|- \n|colspan="2" class="image-section" |' .. imageCaptionString
end
end
-- TODO handle upright
return wikiText
end
--[[ ============================ TAXONOMY SECTION =======================================
adds a table showing the taxonomy
- uses either manual parameters or the automatic taxonomy system
- currently adds a table inside a cell (like paraphyletic group) rather than just adding rows (core taxobox system)
--]]
function p.addTaxonomySection(frame, taxoboxTable)
local wikiText = ""
local taxonomyHeader = "[[Taxonomy_(biology)|Scientific classification]]"
if templateArgs['virus_group'] then
taxonomyHeader = "[[Virus classification]]"
elseif templateArgs['ichnos'] then
taxonomyHeader = "[[Trace fossil classification]]"
elseif templateArgs['veterovata'] then
taxonomyHeader = "[[Veterovata|Eggshell classification]]"
else
-- TODO add other options (DONE but not verified ichnos or veterovata)
-- ! colspan=2 style="min-width:15em; text-align: center{{#if:{{{colour|}}}|{{;}} background-color{{COLON}} {{{colour}}} }}" |
-- {{#if:{{{virus_group|{{{virus|}}}}}}|[[Virus classification]]
-- |{{#if:{{{ichnos|}}}|[[Trace fossil classification]]
-- |{{#if:{{{veterovata|}}}|[[Veterovata|Eggshell classification]]
-- |[[Taxonomy (biology)|Scientific classification]]}} }} }}
-- {{#if:{{{edit link|}}}|{{edit taxonomy|{{{parent|}}} | {{{edit link}}} }}
-- {{#if: {{{classification_status|}}} | <br>({{{classification_status}}}) | }} }}
-- handle |classification_status=disputed (add ref)
end
-- add symbol and link to taxonomy editor
local editLink = ''
if info.auto then
local tooltip = templateArgs['edit link'] or "Edit this classification"
local style = 'font-size:smaller; float:right; padding-right:0.4em; margin-left:-3em;'
local link = 'Template:Taxonomy/' .. templateArgs['parent']
if info.db == "wikidata" then
local message = 'Select%20preview%20to%20see%20Wikidata%20hierarchy'
local url='https://en.wikipedia.org/w/index.php?action=edit§ion=new&preloadtitle=' .. message
.. '&title=Template:Biota_infobox/Wikidata/preview&preload=User:Jts1882/Taxonomy/preload2&preloadparams%5b%5d='
link = url .. string.gsub( templateArgs['parent'], " ", "_")
end
editLink = '<span class="plainlinks" style=".. style ..">'
.. '[[File:Red Pencil Icon.png|link=' .. link .. '|' .. tooltip .. ']]</span>'
end
local status = ""
if templateArgs['classification_status'] then
status = '<div style="font-weight:normal;" >(' .. templateArgs['classification_status'] .. ')'
if templateArgs['classification_ref'] then status = status .. templateArgs['classification_ref'] end
status = status .. '</div>'
end
local rowTaxonomyHeader = taxoboxTable:tag('tr')
rowTaxonomyHeader:tag('th')
:attr('colspan', '2')
:addClass('section-header')
:cssText('background:' .. info.headerColor .. ';')
:wikitext(taxonomyHeader)
:wikitext(editLink)
:wikitext(status)
wikiText = wikiText .. '\n|- \n! colspan="2" class="section-header" style="background:' .. info.headerColor .. ';" '
.. '|' .. taxonomyHeader .. editLink .. status
--[[ get taxonomy list, either using automatic taxobox system or manual system
manual: get ranks and taxa from template parameter
auto (default): get ranks and taxa from taxonomy template parameters
module: get ranks and taxa from dataa submodules
wikidata: get ranks and taxa from wikidata
]]
-- shows ranks of genus or above for all auto modes
if info.db == "module" then
wikiText = wikiText .. p.addModuleTaxonomy(frame, taxoboxTable) -- use module copy of template data
elseif info.db == "wikidata" then
--wikiText = wikiText ..'\n|-|\n|' .. p.addWikidataTaxonomy(frame, taxoboxTable) -- use wikidata
wikiText = wikiText .. p.addWikidataTaxonomy(frame, taxoboxTable) -- use wikidata
-- TODO need to handle colour
-- the default is class="section-header" colspan="2" style="background:rgb(250,240,230);"
-- wikiText = wikiText:gsub('style="background:rgb%(250,240,230%);"', 'style="background:red;"')
-- gsub must be applied to wikitextTable
-- use more generally to avoid traversing the hierarchy twice)
elseif info.auto then
--[[ get automatic taxonomy hierarchy (three different variants)
1) direct call to taxoboxList() - preferred if issues with setting frame arguments can be resolved (used in live version)
2) experimental version of taxoboxList()
3) [ET] using Template:Taxobox/taxonomy
]]
wikiText = wikiText ..'\n|-|\n|' .. p.addAutomaticTaxonomy(frame, taxoboxTable) -- use #invoke of module
end
if info.auto then
-- use manual taxobox for subgeneric ranks: subgenus,species, subspecies, variety
-- ranks below genys using manual taxobox code
local taxonRanks = { 'subgenus', 'species', 'hybrid', 'subspecies', 'population', 'variety' , 'infraspecies_rank1' , 'infraspecies_rank2'}
wikiText = wikiText .. p.addManualTaxonomy(frame, taxoboxTable, taxonRanks)
else
wikiText = wikiText .. p.addManualTaxonomy(frame, taxoboxTable, paramData.taxonRanks) -- just add rows manually
end
return wikiText
end
----------------------------------AUTOMATIC TAXONOMY (module and wikidata versions) -----------
function p.addModuleTaxonomy(frame, taxoboxTable) -- use invoke of module function
local moduleData = require("Module:Sandbox/trappist the monk/taxonomy")
local tree_t = {};
local taxon = templateArgs['taxon'] or templateArgs['parent']
tree_t = moduleData._crawl_tree(taxon, tree_t)
--local output = '\n|-\n|' .. table.concat (tree_t, '<br />') -- dump the whole table
local output = ''
for k,v in pairs (tree_t) do
local parts = mw.text.split(v, ": ", true)
if string.sub(parts[1],1,3) == "'''" then
local styled_rank = string.gsub(parts[1], "'''", "")
local linked_taxon = string.gsub(parts[2], " <small>.*", "")
output = output .. '\n|-\n|' .. styled_rank .. '||' .. linked_taxon
end
end
return output
end
--[[ p.WikidataTaxonomy(frame)
- entry point for Template:Biota infobox/Wikidata
]]
function p.WikidataTaxonomy(frame)
-- get taxon name. This must be the Wikipedia page for the taxon (which is linked to the qid), e.g. Helix (gastropod)
-- an alternative is to have a qid parameter
local taxon = frame:getParent().args['taxon'] or frame.args['taxon'] --or "Felis"
local qid = frame:getParent().args['qid'] or frame.args['qid']
if qid and not taxon then
taxon = mw.wikibase.getSitelink(qid) -- only works if taxon name, page title and taxonomy template match
end
if not qid and not taxon then
return "No valid taxon or qid."
end
templateArgs['taxon'] = taxon -- p.addWikidataTaxonomy() gets the taxon from template parameters
templateArgs['qid'] = qid
-- contruct table comparing wikidata taxonomy and automated taxobox taxonomy
info.headerColor = "#ddeeff"
local comment = '<p>Ancestral taxa taken from Wikidata. <br/>Bold taxa show those that will be displayed in taxobox.</p>'
local output = '{|\n|-\n| style="vertical-align:top;" |' .. comment -- outer table
.. '\n{| class="infobox biota" style="text-align: left; font-size:100%;"' -- wikidata ancestry table
output = output .. '\n|- \n! colspan="5" class="section-header" style="text-align:center;background:' .. info.headerColor .. ';" '
.. '|' .. "Ancestral taxa (from Wikidata)"
.. '\n|- style="text-align:center;background:' .. info.headerColor .. ';"'
.. '\n! Rank !! Taxon !! colspan="2" | Wikidata ID !! Parents at Wikdata'
templateArgs['display_parents'] = "all"
templateArgs['show_alt_parents'] = true
templateArgs['select_parent'] = frame:getParent().args['select_parent'] or frame.args['select_parent']
output = output .. p.addWikidataTaxonomy(frame)
taxon = taxon:gsub("(.*)%s%(.*%)", "%1") -- remove disambiguation
local automatedTaxonomyList = frame:callParserFunction ('#invoke', {'Autotaxobox/sandbox', 'taxonomyList', taxon})
automatedTaxonomyList = automatedTaxonomyList:gsub("Ancestral taxa", "Ancestral taxa <br/>(from automated taxonomy templates)")
output = output .. '\n|}' -- close wikidata table
.. '\n| style="vertical-align:top;" |' -- cell for automated taxonomy table
.. automatedTaxonomyList
.. '\n|}'
return output
end
function p.getAnchor(frame)
--return "Felis" --mw.title.getCurrentTitle().fragment
end
function p.addWikidataTaxonomy(frame, taxoboxTable) -- use invoke of module function
local qid
local taxon = templateArgs['taxon'] or templateArgs['parent'] -- parent set for species and subspeciesbox
if templateArgs['qid'] then
qid = templateArgs['qid'] -- get qid from parameter
else
qid = mw.wikibase.getEntityIdForTitle (taxon) -- get qid from Wikipedia page on taxon
end
if not qid then
return "The taxon name must be the Wikipedia page title. Alternatively provide the Wikidata qid (qid=)."
end
local rows = ""
local row = "| rank || taxon"
local count = 0
while qid and count < 100 do
local taxonInfo = p.getTaxonInfo(qid)
if not taxonInfo.rank then taxonInfo.rank = "unranked" end
if not taxonInfo.taxon then taxonInfo.taxon = "no taxon name" end
if p.showTaxonRow(taxonInfo.rank, taxonInfo.taxon, count) then -- display required rows
local link = ""
local italics = ""
if taxonInfo.rank == "genus" then italics = "''" end
if taxonInfo.siteLink and taxonInfo.siteLink ~= taxonInfo.taxon then link = taxonInfo.siteLink .. "|" end
row = "\n|-\n| " .. p.firstToUpper(taxonInfo.rank)
.. "||" .. italics .. "[[" .. link .. taxonInfo.taxon .. "]]" .. italics
if templateArgs['show_alt_parents'] and taxonInfo.altParents then
row = row .. '||[[:d:' .. qid .. '|' .. qid .. ']]'
.. '||[[:d:' .. qid .. '#P171|[edit]]]'
.. "||" .. taxonInfo.altParents
end
rows = row .. rows
end
if taxonInfo.parent then
qid = taxonInfo.parent -- next parent
else
qid = nil -- no more parents so stop
end
count = count + 1
end
return rows
end
local majorRanks = { "superkingdom", "kingdom", "phylum", "division", "class", "order", "family", "genus", "species" }
function p.showTaxonRow(rank, taxon, count)
local majorRank = false
for k, v in pairs(majorRanks) do
if rank == v then
majorRank = true -- flag major ranks for display
majorRanks[k] = nil -- don't show this class again in hierarchy (e.g. for mammals, birds)
end
end
if majorRank then return true end
if count < (tonumber(templateArgs['display_parents']) or 2) then return true end
if templateArgs['display_parents'] == "all" then return true end
if templateArgs['display_parent_taxa'] then
if string.find(templateArgs['display_parent_taxa'], taxon) then return true end
end
if templateArgs['display_parent_ranks'] then
for v in mw.text.gsplit(templateArgs['display_parent_ranks'], ",", true) do
if rank == mw.text.trim(v) then return true end
end
end
return false
end
function p.getTaxonInfo(qid)
--local rank, taxon, parent
local taxonInfo = {}
local item = mw.wikibase.getEntity(qid)
local statements = item:getBestStatements('P105')[1] --taxon rank
if statements and statements.mainsnak and statements.mainsnak.datavalue then
taxonInfo.rank = mw.wikibase.getLabel(statements.mainsnak.datavalue.value.id)
end
local statements = item:getBestStatements('P225')[1] -- taxon name
if statements and statements.mainsnak and statements.mainsnak.datavalue then
taxonInfo.taxon = statements.mainsnak.datavalue.value
end
local statements = item:getBestStatements('P171') -- taxon parent
local statement = statements[1]
if statement and statement.mainsnak and statement.mainsnak.datavalue then
taxonInfo.parent = statement.mainsnak.datavalue.value.id -- use first value
end
for index, statement in pairs( statements ) do
local id
if statement and statement.mainsnak and statement.mainsnak.datavalue then
id = statement.mainsnak.datavalue.value.id
end
if id then
local label = mw.wikibase.getLabel(id)
local parentString = label .. " ([[:d:" .. id .. "#P171|" .. id .. "]])"
if taxonInfo.altParents then
taxonInfo.altParents = taxonInfo.altParents .. "</br/>" .. parentString
else
taxonInfo.altParents = parentString
end
local selectParent = templateArgs['select_parent'] or "1" -- template parameters read as strings
if selectParent == tostring(index) or selectParent == "last" then
taxonInfo.parent = id
end
end
end
local label = mw.wikibase.getLabel(qid)
if label then taxonInfo.label = label end
-- local siteLink = mw.wikibase.getSitelink(qid)
-- if siteLink then taxonInfo.siteLink = siteLink end
return taxonInfo --rank, taxon, parent
end
-------------------------------------------------AUTOMATIC TAXONOMY (using invoke of module function) -----------
function p.addAutomaticTaxonomy(frame, taxoboxTable) -- use invoke of module function
--emulate template: {{taxobox/taxonomy}}
--which uses {{#invoke:Autotaxobox|taxoboxList
local bold_first = 'bold'
if templateArgs['species'] or templateArgs['hybrid'] then bold_first = 'link' end
if templateArgs['link_parent'] then bold_first = 'link' end
local args = { templateArgs['parent'], -- or tostring( mw.title.getCurrentTitle() ),
display_taxa = templateArgs['display_taxa'] or 1,
offset = templateArgs['offset'] or 0,
authority = templateArgs['authority'],
parent_authority = templateArgs['parent_authority'],
gparent_authority = templateArgs['grandparent_authority'],
ggparent_authority = templateArgs['greatgrandparent_authority'],
gggparent_authority = templateArgs['greatgreatgrandparent_authority'],
virus=templateArgs['virus'],
bold_first = bold_first
}
frame.args = args
-- templateArgs['debug'] = mw.dumpObject(frame)
local autoTaxonomy = autotaxa.taxoboxList(frame)
if (1==1) then return autoTaxonomy end
local row = taxoboxTable:tag('tr') -- incompatible with the templates called
:wikitext('\n|rank ||taxon name ')
:wikitext('\n'.. autoTaxonomy ) -- autoTaxonomy ends with a new row (|-)at end
:wikitext('\n|x ||y ') -- so add blank cells to complete extra row
-- this and affects spacing in taxonomy rows
--:wikitext('\n') -- spacing fine, newline for table wikitext
--[[ note: the output of {{taxobox/showtaxon}} [used by invoke:Autotaxobox|taxoboxList] ends with a newline token
this and affects spacing in taxonomy rows
leaving the empty row results in spurious paragraphs above the table
]]
return
end
----------------------------------------MANUAL TAXONOMY--------------------------------
function p.addManualTaxonomy(frame,taxoboxTable, taxonRanks)
--local parent = mw.getCurrentFrame():getParent()
--local taxonRanks = data.taxonRanks
-- following {{Paraphyletic group, a table is add to the cell in the classification row
--local taxonomyTable = mw.html.create('table'):addClass('taxonomy')
-- an alternative is to dispense with the extra table and just add the rows (like taxobox/core),
-- which would need colspan=2 on other rows (DONE)
local taxonomyTable = taxoboxTable
local wikiText = ""
for k,v in pairs(taxonRanks) do
if templateArgs[v] then
local taxonName = templateArgs[v]
local taxonRank = frame:expandTemplate{ title = "anglicise rank", args = {taxonRanks[k]} }
if v == 'infraspecies_rank1' and templateArgs['infraspecies_rank1_name'] then
taxonRank = templateArgs['infraspecies_rank1_name']
elseif v == 'infraspecies_rank2' and templateArgs['infraspecies_rank2_name']then
taxonRank = templateArgs['infraspecies_rank2_name']
end
if taxonRanks[k] == "virus_group" then
taxonName = frame:expandTemplate{ title = "Virus group", args = {templateArgs[v]} }
-- taxonRank = "Group" -- handled by anglicise rank template
end
local authorityString = ''
if templateArgs[taxonRanks[k]..'_authority'] then
authorityString = '<br /><small>'..templateArgs[taxonRanks[k]..'_authority']..'</small>'
end
local taxonString = '<span class="'.. taxonRanks[k] ..'">'..taxonName..'</span>'..authorityString
local row = taxonomyTable:tag('tr')
row:tag('td'):wikitext(taxonRank..':')
row:tag('td'):wikitext(taxonString)
--:wikitext('<br /><small>'..parent.args[taxonRanks[k]..'_authority']..'</small>')
wikiText = wikiText .. '\n|- \n|' .. taxonRank..': \n|' .. taxonString
end
end -- end for loop
--return tostring(taxonomyTable)
return wikiText
end
--[[ ########################### UTILITY FUNCTIONS ###############################################
-----------------------------------------TAXOBOX COLOUR--------------------------------------
-- gets colour for headers using manual or automatic taxobox schemes
]]
function p.getTaxoboxColor(frame)
local colorAs = templateArgs['color_as'] or nil
if info.auto and not templateArgs['virus_group'] then
--[[(1) if color_as|colour_as|color as|colour as set, use template {{Taxobox colour|color_as}}
(2) else use the auto taxonnomy tree to find colour: {{#invoke:Autotaxobox|taxoboxColour| }}
{{#invoke:Autotaxobox|taxoboxColour|{{{parent|{{{genus|{{first word|{{{taxon|{{PAGENAME}} }}
--]]
--[[if (templateArgs['color_as'] and templateArgs['color_as'] ~= "") or
(templateArgs['colour_as'] and templateArgs['colour_as'] ~= "") or
(templateArgs['color as'] and templateArgs['color as'] ~= "") or
(templateArgs['colour as'] and templateArgs['colour as'] ~= "") then
local colorAs = ""
if templateArgs['color_as'] then colorAs = templateArgs['color_as'] end
if templateArgs['colour_as'] then colorAs = templateArgs['colour_as'] end
if templateArgs['color as'] then colorAs = templateArgs['color as'] end
if templateArgs['colour as'] then colorAs = templateArgs['colour as'] end]]
if colorAs then -- templateArgs['color_as'] and templateArgs['color_as'] ~= "" then
return frame:expandTemplate{ title = 'Taxobox colour', args = {colorAs} }
else
-- us #invoke:Autotaxobox|taxoboxColour|{{{parent}}} [parent should be set]
frame.args[1] = templateArgs['parent']
return autotaxa.taxoboxColour(frame)
--return "palegreen"
end
else -- use manual taxobox colours
--{{Taxobox colour|{{{regnum|{{{virus_group|{{{unranked_phylum|{{{phylum|''[[Incertae sedis]]''}}}}}}}}}}}}}}
if not colorAs then --templateArgs['color_as'] then
local group =''
if templateArgs['regnum'] then
group = templateArgs['regnum']
elseif templateArgs['virus_group'] then
group = templateArgs['virus_group']
elseif templateArgs['unranked_phylum'] then
group = templateArgs['unranked_phylum']
elseif templateArgs['phylum'] then
group = templateArgs['phylum']
else
group = "''[[Incertae sedis]]''" -- TODO check if this is what was desired
end
colorAs = group
end
return frame:expandTemplate{ title = 'Taxobox colour', args = {colorAs} }
end
end
-------------------------------------------------------------------
function p.templateStyle( frame, src )
return frame:extensionTag( 'templatestyles', '', { src = src } );
end
function p.firstToUpper(str)
return (str:gsub("^%l", string.upper))
end
function p.test(frame)
return info.auto
end
return p