end
end
return refstr
end
L.references = references  
local function i18nByLatin(ranklatin, str, default)
local suc, format = pcall(i18n, str .. "-" .. ranklatin)
if not suc then
format = default
end
return format
end
L.i18nByLatin = i18nByLatin  
local function formatScientificName(ranklatin, scientific, short)
local pf = "scientific-name"
if short then
pf = "short-" .. pf
end
scipattern = i18nByLatin(
ranklatin, pf .. "-pattern", i18n(pf .. "-pattern"))
scirepl = i18nByLatin(
ranklatin, pf .. "-repl", i18n(pf .. "-repl"))
scientific = string.gsub(scientific, scipattern, scirepl)
for scipattern, scirepl in pairs(
i18nByLatin(ranklatin, pf .. "-replaces", i18n(pf .. "-replaces"))) do
scientific = string.gsub(scientific, scipattern, scirepl)
end
return scientific
end
L.formatScientificName = formatScientificName  
local function renderTableHead(text, color)
local
if color then
bgcolor = bgcolor .. " background-color: " .. color .. ";"
end
return mw.text.tag('tr', {}, mw.text.tag('th', {, .. bgcolor
}, text))
end
L.renderTableHead = renderTableHead  
local function renderTableRow(text, extra_css)
local css = "text-align: ;"
if extra_css then
css = css .. " " .. extra_css
end
return mw.text.tag('tr', {}, mw.text.tag('td',
{, style=css}, text))
end
L.renderTableRow = renderTableRow  
local function renderFossilEra(params)
local eralink = {}
local refstr = references(params["era[references]"])
for i = 1, 2 do
local eraid = params[string.format("era[%d][id]", i)]
if eraid then
eralink[#eralink + 1] = getLink(eraid)
end
end
return renderTableHead(getLink(GEOLOGICAL_ERA) .. refstr, params.color) ..
renderTableRow(table.concat(eralink, '&mdash;'))
end
L.renderFossilEra = renderFossilEra  
local function renderIUCNStatus(params)
local r = {}
refstr = references(params["iucn_status[references]"])
r[#r + 1] = renderTableHead(
getLink(RED_DATA_LIST, getLabel(P_IUCN_STATUS)) .. refstr, params.color)
-- TODO: support SVG systemLanguage here
r[#r + 1] = renderTableRow(
"[[File:" .. params["iucn_status[image]"] ..
"|" .. params["iucn_status[label]"] .. "]]")
return table.concat(r)
end
L.renderIUCNStatus = renderIUCNStatus  
local function formatTaxon(
latin, qid, scientific, vernacular, is_subject, is_extinct)
local nameformat
scientific = scientific or i18n("no-scientific-name")
local scientificshort = scientific
if latin then
scientificshort = formatScientificName(latin, scientific, true)
scientific = formatScientificName(latin, scientific)
end
local nf = "item-format-parent"
if is_subject then
nf = "item-format-current"
end
if vernacular then
nameformat = i18n(nf .. "-with-vernacular-name")
else
nameformat = i18n(nf .. "-without-vernacular-name")
end
local link = qid
if _linkconfig == "sitelink" then
link = mw.wikibase.sitelink(qid)
end
if is_extinct then
nameformat = i18n("extinct-mark") .. nameformat
end
return namedStringFormat{
nameformat, link=link, vernacular=vernacular,
scientific=scientific, scientificshort=scientificshort},
scientific
end
L.formatTaxon = formatTaxon  
local function renderRank(i, params)
local row
local nameformat
local detailrows = {}
local pf = string.format("rank[%d]", i)
local ranklink = i18n("unknown-rank")
local rankid = params[pf .. "[id]"]
local ranklatin = params[pf .. "[latin]"]
local is_subject = params[pf .. "[is_subject]"]
local scientific = params[pf .. "[scientific]"]
local formatted = params[pf .. "[taxon]"]
if rankid then
local linkformat = i18nByLatin(
ranklatin, "rank-format", i18n("rank-format"))
ranklink = getLink(rankid, nil, linkformat, true)
end
row = mw.text.tag(
'tr', {},
mw.text.tag('td', {}, ranklink) ..
mw.text.tag('td', {}, formatted))
if is_subject then
local refstr = references(params[pf .. "[references]"])
local authority = params[pf .. "[authority]"]
detailrows = {
renderTableHead(
string.format(i18n("scientific-name-of-taxon"), getLabel(rankid))
.. refstr, params.color
),
renderTableRow(scientific),
renderTableRow(authority, "font-variant: small-caps;")
}
end
return row, detailrows
end
L.renderRank = renderRank  
-- in case of more than one parent taxa: choose target according to the
-- references selected by usereferences
local function chooseParent(item)
local cand
local nextparent = {}
for id,refs in pairs(targetId(item, P_TAXON_PARENT)) do  
-- some taxon, like Q2382443, the parent taxon is null
local novalue = id == "novalue"  
-- try to find match from usetaxa
if not novalue and usetaxa["Q" .. id] then
table.insert(nextparent, {usetaxa["Q" .. id], id, refs})
end  
-- or according to usereferences
if refs and type(refs) ~= "boolean" then
for r, i in pairs(usereferences) do
if refs[r] then
table.insert(nextparent, {i + usetaxa.size, id, refs})
end
end
end  
if not novalue and not cand then -- if no item had references yet
cand = {nil, id, refs} -- use this
end
end
-- nextparent is not sorted, so sort it
table.sort(nextparent, function(a, b)
return a[1] < b[1]
end)  
if next(nextparent) then
_, cand = next(nextparent)
end  
if cand and cand[1] == nil and cand[3] then
for targetid, _ in pairs(cand[3]) do
usereferences.size = usereferences.size + 1
usereferences[targetid] = usereferences.size
end
end
if cand then
return cand[2], cand[3] or {}
else
return nil, {}
end
end
L.chooseParent = chooseParent  
-- Find out if this taxon is extinct already
local function isExtinct(item)  
-- check IUCN status
local statusid, _ = next(targetId(item, P_IUCN_STATUS))
if statusid == EXTINCT then
return true
end  
-- check temporal range end
local eraend, _ = next(targetId(item, P_ERA_END))
if eraend then
return true
end  
-- check end time
local endtime = targetTime(item, P_END_TIME)
if endtime then
return true
end  
return false
end
L.isExtinct = isExtinct  
-- Find out if the item is a monotypic taxon
local function isMonotypic(item)
return next(targetId(item, P_INSTANCE_OF)) == MONOTYPIC_TAXON
end
L.isMonotypic = isMonotypic  
local function taxonParams(qid, item, params, fetch_detail, is_parent_extinct)
local rankid
local ranklatin
local is_subject
local level = params["rank[size]"] + 1
local pf = string.format("rank[%d]", level)
local name, namequalifiers, namereferences = targetStr(item, P_TAXON_NAME)
local vernacular = vernacularName(item)
local is_extinct = is_parent_extinct or isExtinct(item)  
if rankid == SUBGENUS and
string.match(name, "^%w+$") and
params[string.format("rank[%d][id]", level - 1)] == GENUS then
-- follow ICZN to prepend genus name in front of subgenus name
name = string.format("%s (%s)",
params[string.format("rank[%d][raw_scientific]", level - 1)],
mw.ustring.lower(name))
end  
rankid = next(targetId(item, P_TAXON_RANK))
if rankid == "novalue" then
rankid = CLADE
end  
if rankid then
local rankitem = mw.wikibase.getEntity('Q' .. rankid)
ranklatin = rankitem:getLabel('la')
end
if ranklatin then
ranklatin = mw.ustring.lower(ranklatin)
end  
if (hideranks[rankid] or hideranks[ranklatin]) and not usetaxa[qid] then
-- interrupt since this rank has been hided from display
return params, is_extinct
end  
name = capitalize(name)
params["rank[size]"] = level
params[pf .. "[id]"] = rankid
params[pf .. "[link]"] = qid
params[pf .. "[is_monotypic]"] = isMonotypic(item)
params[pf .. "[vernacular]"] = vernacular
params[pf .. "[raw_scientific]"] = name
params[pf .. "[latin]"] = ranklatin
params[pf .. "[is_extinct]"] = is_extinct  
if fetch_detail then
-- get detail
is_subject = true
params[pf .. "[references]"] = namereferences
year = qualifierTargetTime(namequalifiers, P_TAXON_YEAR) or
targetTime(item, P_TAXON_YEAR)
if year then
-- access year in time representation "+1758-00-00T00:00:00Z"
year = string.sub(year, 2, 5)
else
year = '????'
end
local authorsstr = createAllAuthorsStr(item, namequalifiers, year)
params[pf .. "[authority]"] = authorsstr
else
is_subject = false
end  
local formatted, sciname = formatTaxon(
ranklatin, qid, name, vernacular, is_subject, is_extinct)  
params[pf .. "[scientific]"] = sciname
params[pf .. "[is_subject]"] = is_subject
params[pf .. "[taxon]"] = formatted