ranklatin, qid, name, vernacular, is_subject, is_extinct)  
params[pf .. "[scientific]"] = sciname
params[pf .. "[is_subject]"] = is_subject
params[pf .. "[taxon]"] = formatted  
return params, is_extinct
end
L.taxonParams = taxonParams  
-- performs the loop up the hierarchy using P_TAXON_PARENT
local function iterateRanks(
qid, count, fetch_detail, child_detailed, child_extinct, params)
local inneritem
local params = params or {["rank[size]"] = 0}
local item = mw.wikibase.getEntity(qid)  
name = targetStr(item, P_TAXON_NAME)
if name == 'nil' then
return params, {}, item
end  
local nextid, references = chooseParent(item)
mw.log('nextid', nextid)
if not code then
codeid = next(targetId(item, P_NOMENCLATURE_CODE))
if codeid and colors[codeid] then
code = codeid
end
end  
if visited[nextid] then -- loop detection
return params, {}, item
elseif nextid then
visited[nextid] = true
end  
-- Monotypic taxon can contain extinct taxa,
-- should not fetch detail in such circumstances
-- for example: Q7105303
local fetch_detail =
fetch_detail or
(child_detailed and not child_extinct and isMonotypic(item))
local is_extinct, is_parent_extinct  
if nextid and (not code or count > 0) then
params, refs, _, is_parent_extinct = iterateRanks(
'Q' .. nextid, count - 1, false,
fetch_detail, isExtinct(item), params)
for ref, _ in pairs(refs) do
references[ref] = true
end
end
if count > 0 then
params, is_extinct = taxonParams(
qid, item, params, fetch_detail, is_parent_extinct)
end
params["code"] = code
params["color"] = colors[code]
return params, references, item, is_extinct
end
L.iterateRanks = iterateRanks  
-- use arguments from second table to override the first table
-- support classical parameters like "species", "unranked_ordo"
local function overrideParams(params, overrides)
overrides = overrides or {}  
for key, val in pairs(overrides) do
params[key] = overrides[key] or params[key]
end  
-- classical taxonomic rank params
local unranked = {}
for i = 1, params["rank[size]"] do
local pf = string.format("rank[%d]", i)
local latin = params[pf .. "[latin]"]
if latin == "clade" then
unranked[#unranked + 1] = i
else
local txarg = pf .. "[taxon]"
local atarg = pf .. "[authority]"
params[txarg] = overrides[latin] or params[txarg]
params[atarg] = overrides[latin .. "_authority"] or params[atarg]
for j = #unranked, 1, -1 do
local txarg = string.format("rank[%d][taxon]", unranked[j])
local atarg = string.format("rank[%d][authority]", unranked[j])
local argname = string.format("unranked_%s", latin)
if j == #unranked then
params[txarg] = overrides[argname] or
override[argname .. "1"] or params[txarg]
params[atarg] = overrides[argname .. "_authority"] or
override[argname .. "1_authority"] or params[atarg]
else
params[txarg] = overrides[string.format('%s%d',
argname, #unranked - j + 1)] or params[txarg]
params[atarg] = overrides[string.format('%s%d_authority',
argname, #unranked - j + 1)] or params[atarg]
end
end
unranked = {}
end
if latin == "species" then
local scarg = pf .. "[scientific]"
params[scarg] = overrides["binomial"] or params[scarg]
elseif ({subspecies=true, varietas=true, forma=true})[latin] then
local scarg = pf .. "[scientific]"
params[scarg] = overrides["trinomial"] or params[scarg]
end
end  
return params
end
L.overrideParams = overrideParams  
-- fetch params should passed to taxobox for the given qid (e.g., qid=Q729412
-- for Heloderma) and count higher levels of the taxon hierarchy.
-- developers: use this method for tests in the debug console, e.g.,
-- p.localFunction("getTaxoboxParams")('Q729412', 5)
local function getTaxoboxParams(qid, count)
visited = {}
local params, references, item = iterateRanks(qid, count, true)
if params["rank[size]"] == 0 then
return {}
end  
local scarg = string.format("rank[%d][scientific]", params["rank[size]"])
local vnarg = string.format("rank[%d][vernacular]", params["rank[size]"])
params["name"] = params[vnarg] or params[scarg]  
capitalize(getItemLabel(item))  
local image = targetStr(item, P_IMAGE)
params["image"] = image  
fossilParams(item, params)  
params["rank[references]"] = references  
local map = targetStr(item, P_SPREAD_MAP)
params["range_map"] = map  
local statusid, statusreferences = next(targetId(item, P_IUCN_STATUS))
params["iucn_status[id]"] = statusid
if statusid then
local status = mw.wikibase.getEntity("Q" .. statusid)
local img, imgqualifiers, imgreferences = targetStr(status, P_IMAGE)
if img then
params["iucn_status[label]"] = getItemLabel(status)
params["iucn_status[references]"] = statusreferences
params["iucn_status[image]"] = img
end
end  
local audio, audioreferences = targetStr(item, P_AUDIO)
params["audio"] = audio  
return params
end
L.getTaxoboxParams = getTaxoboxParams  
local function callbackTaxobox(template, params, overrides, dryrun)
local content = {}
local = mw.getCurrentFrame
params = overrideParams(params, overrides)  
for key, val in pairs(params) do
if type(val) == "boolean" then
val = val and "yes" or "no"
elseif type(val) == "table" and
string.match(key, "%[references%]$") then
local refs = {}
for r, _ in pairs(val) do
table.insert(refs, r)
end
val = table.concat(refs, " ")
end
if dryrun then
content[#content + 1] = string.format(
"|%s = %s", key, val)
else
params[key] = val
end
end  
if dryrun then
table.sort(content, function(a, b)
a = string.gsub(a, "%[(%d+)%]", function(i)
return "[" .. string.rep("0", 3 - #i) .. i .. "]"
end)
b = string.gsub(b, "%[(%d+)%]", function(i)
return "[" .. string.rep("0", 3 - #i) .. i .. "]"
end)
return a < b
end)
content = "\n"
content = :callParserFunction("#tag", "nowiki", content)
return mw.text.tag("pre", {}, content)
else
return :expandTemplate{title=template, args=params}
end
end
L.callbackTaxobox = callbackTaxobox  
-- creates the taxobox from giving params
local function renderTaxobox(params, overrides)
local content = {}
params = overrideParams(params, overrides)  
local color = params.color  
-- title
content[#content + 1] = renderTableHead(params.name, color)  
-- image
if params.image then
content[#content + 1] = renderTableRow(
"")
end  
-- fossil era
if params["era[1][id]"] then
content[#content + 1] = renderFossilEra(params)
end  
-- systematics
local refstr = references(params["rank[references]"])
content[#content + 1] = renderTableHead(
getLink(SYSTEMATICS) .. refstr, color)  
-- ranks
if params["rank[size]"] > 0 then
local taxondetails = {}
for i = 1, params["rank[size]"] do
local row, detailrows = renderRank(i, params)
content[#content + 1] = row
taxondetails[#taxondetails + 1] = table.concat(detailrows)
end
content[#content + 1] = table.concat(taxondetails)
end  
-- range map
if params.range_map then
content[#content + 1] = renderTableHead(i18n('range-map'), color)
content[#content + 1] = renderTableRow(
"")
end  
-- iucn status
if params["iucn_status[id]"] then
content[#content + 1] = renderIUCNStatus(params)
end  
-- audio
if params.audio then
content[#content + 1] = renderTableHead(getLink(P_AUDIO), color)
content[#content + 1] =
renderTableRow("")
end  
return mw.text.tag('table', {
style =
width: ; -width: ; float: ;
-style: solid; background-color: #f9f9f9;  
}, table.concat(content))
end
L.renderTaxobox = renderTaxobox  
if debug then
function p.debugParams(params)
mw.log("Start of logging params")
mw.log(string.rep("=", 20))
for k, v in pairs(params) do
mw.log(k, v)
end
mw.log("End of logging params")
end  
function p.localFunction(name)
return L[name]
end
end  
function p.taxobox
local config = parseConfig(.args)
local qid = .args.qid
local params = getTaxoboxParams(qid, config.count)
return renderTaxobox(params, .args)
end  
function p.callback
local config = parseConfig(.args)
local qid = .args.qid
local template = .args.template or "Taxobox"
local params = getTaxoboxParams(qid, config.count)
return callbackTaxobox(template, params, .args, config.dryrun)
end  
return p