Jump to content

Module:Automated taxobox

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Peter coxhead (talk | contribs) at 17:32, 31 January 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[ THE FOLLOWING PARAMETERS COULD BE IMPLEMENTED:
| binomial2 = {{{binomial2|}}}
| binomial2_authority = {{{binomial2 authority|{{{binomial2_authority|}}} }}}
| range_map2 = {{{range map2|{{{range_map2|}}} }}}
| range_map2_width = {{{range map2 width| {{{range_map2_width|}}}}}}
| range_map2_alt = {{{range map2 alt|{{{range_map2_alt|}}} }}}
| range_map2_caption = {{{range map2 caption|{{{range_map2_caption|}}} }}}
| binomial3 = {{{binomial3|}}}
| binomial3_authority = {{{binomial3 authority|{{{binomial3_authority|}}} }}}
| range_map3 = {{{range map3|{{{range_map3|}}} }}}
| range_map3_width = {{{range map3 width|{{{range_map3_width|}}} }}}
| range_map3_alt = {{{range map3 alt|{{{range_map3_alt|}}} }}}
| range_map3_caption = {{{range map3 caption|{{{range_map3_caption|}}} }}}
| binomial4 = {{{binomial4|}}}
| binomial4_authority = {{{binomial4 authority|{{{binomial4_authority|}}} }}}
| range_map4 = {{{range map4|{{{range_map4|}}} }}}
| range_map4_width = {{{range map4 width|{{{range_map4_width|}}} }}}
| range_map4_alt = {{{range map4 alt|{{{range_map4_alt|}}} }}}
| range_map4_caption = {{{range map4 caption|{{{range_map4_caption|}}} }}}
]]

require('Module:No globals')
local TaxonItalics = require('Module:TaxonItalics')
local Autotaxobox = require('Module:Autotaxobox/sandbox')
local p = {} -- functions made public
local l = {} -- internal functions, kept separate

-- =============================================================================
-- main implements Template:Automatic taxobox; see the documentation of that template
-- for details.
-- =============================================================================

function p.main(frame)
	local args
	if frame.args['direct'] == 'yes' then args = frame.args
	else args = frame:getParent().args end
	-- ---------------------------------------------------------------------
	-- pick up taxobox parameters from the caller that need to be processed;
	-- most will be passed on unchanged
	-- ---------------------------------------------------------------------
	local fossilRange = args['fossil_range'] or args['fossil range'] or args['temporal_range'] or args['temporal range'] or ''
    local oldestFossil = args['oldest_fossil'] or args['oldest fossil'] or ''
    local youngestFossil =  args['youngest_fossil'] or args['youngest fossil'] or ''
	local taxon = args['taxon'] or ''
	local name = args['name'] or ''
	local colourAs = args['color_as'] or args['color as'] or args['colour_as']  or args['colour as'] or ''
	local subdivision = args['subdivision'] or ''
	local subdivisionRanks = args['subdivision_ranks'] or ''
	local binomial = args['binomial'] or ''
	local pagename = args['pagename'] or '' -- for testing and debugging only
	-- ------------------------------------------------------
	-- set the taxobox parameters determined by this function
	-- ------------------------------------------------------
    fossilRange = l.setfossilRange(frame, fossilRange, oldestFossil, youngestFossil)
	-- use the base page name if the taxon parameter is missing
	local basePagename
	if pagename == '' then
		basePagename = frame:expandTemplate{ title = 'PAGENAMEBASE', args = {} }
	else
		basePagename = frame:expandTemplate{ title = 'PAGENAMEBASE', args = {pagename} }
	end
	local taxonParaMissingError = false
	if taxon == '' then
		taxonParaMissingError = true
		taxon = basePagename
	end
	local ok, taxonRank = Autotaxobox.getTaxonInfoItem(frame, taxon, 'rank')
	if not ok then taxonRank = '' end
	-- set the taxobox name, italicized if appropriate
	if name == '' then
		if pagename == '' then name = basePagename
		else name = pagename end
	end
	local nameRank
	ok, nameRank = Autotaxobox.getTaxonInfoItem(frame, name, 'rank')
	if ok and nameRank ~= '' and frame:expandTemplate{ title = 'Is italic taxon', args = {nameRank} } == 'yes' then
		name = TaxonItalics.italicizeTaxonName(name, false, false)
	end
	-- determine taxobox colour
	local colour = ''
	if colourAs ~= '' then
		colour = frame:expandTemplate{ title = 'Taxobox colour', args = {colourAs} }
	else
		colour = Autotaxobox.getTaxoboxColour(frame, taxon)
	end
	-- fill in a missing subdivision_ranks parameter
	if subdivision ~= '' and subdivisionRanks == '' and taxonRank ~= '' then
		subdivisionRanks =  frame:expandTemplate{ title = 'Children rank', args = {taxonRank} }
	end
	-- set binomial parameter if the target taxon is (unusually) a species
	if binomial == '' then
		if ok and taxonRank == 'species' then binomial = taxon end
	end
	-- ------------------------------------------------
	-- now call Taxobox/core with all of its parameters
	-- ------------------------------------------------
	local res = frame:expandTemplate{ title = 'Taxobox/core', args =
		{ ['edit link'] = 'e',
		  temporal_range = fossilRange,
		  display_parents = args['display_parents'] or args['display parents'] or '1',
		  parent = taxon,
		  authority = args['authority'] or '',
          parent_authority = args['parent_authority'] or args['parent authority'] or '',
		  grandparent_authority = args['grandparent_authority'] or args['grandparent authority'] or '',
		  greatgrandparent_authority = args['greatgrandparent_authority'] or args['greatgrandparent authority'] or '',
		  greatgreatgrandparent_authority = args['greatgreatgrandparent_authority'] or args['greatgreatgrandparent authority'] or '',
		  name = name,
		  colour = colour,
		  status = args['status'] or '',
		  status_system = args['status_system'] or args['status system'] or '',
		  status_ref = args['status_ref'] or args['status ref'] or '',
		  status2 = args['status2'] or '',
		  status2_system = args['status2_system'] or args['status2 system'] or '',
		  status2_ref = args['status2_ref'] or args['status2 ref'] or '',
		  trend = args['trend'] or '',
		  extinct = args['extinct'] or '',
		  image = args['image'] or '',
		  upright = args['image_upright'] or args['image upright'] or '',
		  image_alt = args['image_alt'] or args['image alt'] or '',
		  image_caption = args['image_caption'] or args['image caption'] or '',
		  image_caption_align = args['image_caption_align'] or args['image caption align'] or 'center',
		  image2 = args['image2'] or '',
		  upright2 = args['image2_upright'] or args['image2 upright'] or '',
		  image2_alt = args['image2_alt'] or args['image2 alt'] or '',
		  image2_caption = args['image2_caption'] or args['image2 caption'] or '',
		  image2_caption_align = args['image2_caption_align'] or args['image2 caption align'] or 'center',
		  classification_status = args['{classification_status'] or args['{classification status'] or '',
		  diversity = args['diversity'] or '',
		  diversity_ref = args['diversity_ref'] or args['diversity ref'] or '',
		  diversity_link = args['diversity_link'] or args['diversity link'] or '',
		  binomial = binomial,
		  binomial_authority = args['binomial_authority'] or args['binomial authority'] or '',
		  trinomial = args['trinomial'] or '',
		  trinomial_authority = args['trinomial_authority'] or args['trinomial authority'] or '',
		  type_genus = args['type_genus'] or args['type genus'] or '',
		  type_genus_authority = args['type_genus_authority'] or args['type genus authority'] or '',
		  type_species = args['type_species'] or args['type species'] or '',
		  type_species_authority = args['type_species_authority'] or args['type species authority'] or '',
		  subdivision = subdivision,
		  subdivision_ranks = subdivisionRanks,		  
		  type_strain = args['type_strain'] or args['type strain'] or '',
		  range_map = args['range_map'] or args['range map'] or '',
		  range_map_upright = args['range_map_upright'] or args['range map upright'] or '',
		  range_map_alt = args['range_map_alt'] or args['range map alt'] or '',
		  range_map_caption = args['range_map_caption'] or args['range map caption'] or '',
		  synonyms_ref = args['synonyms_ref'] or args['synonyms ref'] or '',
		  synonyms = args['synonyms'] or ''
		} }
	-- put page in error-tracking categories if required
	local errCat1 = ''
	if binomial ~= '' then errCat1 = '[[Category:Automatic taxoboxes using binomial parameter]]' end
	res = res .. frame:expandTemplate{ title = 'Main other', args = {errCat1} }
	return res
end

-- =============================================================================
-- l.setfossilRange(frame, fossilRange, oldestFossil, youngestFossil) checks
-- the parameters that determine the fossil range, returning an appropriate
-- range.
-- =============================================================================
-- temporary public function for debugging
function p.chkFossilRange(frame)
	local args = frame.args
	local fossilRange = args['temporal_range'] or args['temporal range'] or args['fossil_range'] or args['fossil range'] or ''
    local oldestFossil = args['oldest_fossil'] or args['oldest fossil'] or ''
    local youngestFossil =  args['youngest_fossil'] or args['youngest fossil'] or ''
    local fossilRange = l.setfossilRange(frame, fossilRange, oldestFossil, youngestFossil)
	return fossilRange
end

function l.setfossilRange(frame, fossilRange, oldestFossil, youngestFossil)
	local res = ''
	if fossilRange ~= '' then
		if mw.ustring.find(frame:expandTemplate{ title = 'Period start', args = { fossilRange } }, '[Ee]rror') then
			res = fossilRange
		else 
			res = frame:expandTemplate{ title = 'Geological range', args = { fossilRange } }
		end
	elseif oldestFossil ~= '' then
		if youngestFossil == '' then youngestFossil = 'Recent' end
		if mw.ustring.find(frame:expandTemplate{ title = 'Period start', args = { oldestFossil } }, '[Ee]rror') or
		   mw.ustring.find(frame:expandTemplate{ title = 'Period start', args = { youngestFossil } }, '[Ee]rror') then
			res = oldestFossil..'–'..youngestFossil
		else
		res = frame:expandTemplate{ title = 'Geological range', args = { oldestFossil, youngestFossil } }
		end
	end
	return res
end

return p