end
end
end
if claim.mainsnak.datatype == 'monolingualtext' then
choosenclaim[claim.rank][index] = tostring(claim.mainsnak.datavalue.value)
else
choosenclaim[claim.rank][index] = tostring(claim.mainsnak.datavalue.value)
end
choosenqualifiers[claim.rank][index] = claim.qualifiers
choosenreferences[claim.rank][index] = refids
end
end
end  
return choosenclaim, choosenqualifiers, choosenreferences
end
L.targetStrs = targetStrs  
-- Gives the first highest ranked claim and its qualifiers and references.
-- Use only if the data type of the property is string
local function targetStr(item, property)
choosenclaim, choosenqualifiers, choosenreferences = targetStrs(item, property)  
for _, priority in pairs({"preferred", "normal", "deprecated"}) do
index = next(choosenclaim[priority])
if index then
return choosenclaim[priority][index],
choosenqualifiers[priority][index],
choosenreferences[priority][index]
end
end
return
end
L.targetStr = targetStr  
-- helper function to merge all claims, regardless of rank
local function mergeClaims(claims, qualifiers, references)
local c = {}
local q = {}
local r = {}
for _, priority in pairs({"preferred", "normal", "deprecated"}) do
mergeTable(c, claims[priority] or {})
mergeTable(q, qualifiers[priority] or {})
mergeTable(r, references[priority] or {})
end
return c, q, r
end
L.mergeClaims = mergeClaims  
-- Use only if the data type of the property is time
local function targetTime(item, property)
if item and item.claims and item.claims[property] then
for _,claim in pairs(item.claims[property]) do
if claim.mainsnak and claim.mainsnak.datavalue then
return claim.mainsnak.datavalue.value.time
end
end
end
end
L.targetTime = targetTime  
-- same as targetId but for qualifiers
-- TODO merge
local function qualifierTargetId(qualifiers, property)
local claims = {}
if qualifiers and qualifiers[property] then
for _,claim in pairs(qualifiers[property]) do
local valueid = claim.datavalue.value['numeric-id']
table.insert(claims, valueid)
end
end
return claims
end
L.qualifierTargetId = qualifierTargetId  
-- same as targetTime but for qualifiers
-- TODO merge
local function qualifierTargetTime(qualifiers, property)
local claims = {}
if qualifiers and qualifiers[property] then
for _,claim in pairs(qualifiers[property]) do
if claim.datavalue then
mw.log('time qualifier', property, claim.datavalue.value.time)
return claim.datavalue.value.time
end
end
end
end
L.qualifierTargetTime = qualifierTargetTime  
-- takes a list of item ids (the values of the given table) and creates wikilinks based on their labels
local function createLinks(list, authorAbbreviation)
local authors = {}
for _,authorid in pairs(list) do
if authorid then
local author = mw.wikibase.getEntity('Q' .. authorid)
if author then
local label
if authorAbbreviation then
if icnafpApplies then
-- get author abbrieviation per IPNI set
if targetStr(author, P_AUTHOR_ABBR_IPNI) then
label = targetStr(author, P_AUTHOR_ABBR_IPNI)
end
elseif targetStr(author, P_AUTHOR_ABBR_ZOOLOGY) then
-- get zoologist author citation set
label = targetStr(author, P_AUTHOR_ABBR_ZOOLOGY)
end
if not label then
-- use the "last" name if no abbreviation found
-- also don't use the translated name
label = getItemLabel(author, "en")
if label ~= i18n('no-label') then
_, _, label = mw.ustring.find(label, "(%w+)$")
end
end
end
table.insert(authors, getLink(authorid, label))
end
end
end
return authors
end
L.createLinks = createLinks  
local function vernacularName(item)
local vernacularname
-- select vernacular name for current language
if item.claims and item.claims[P_COMMON_NAME] then
for _, claim in pairs(item.claims[P_COMMON_NAME]) do
if claim.mainsnak and claim.mainsnak.datavalue and
claim.mainsnak.datavalue.type == "monolingualtext" and
claim.mainsnak.datavalue.value.language == getLang then
vernacularname = claim.mainsnak.datavalue.value.text
break
end
end
if vernacularname == '' then
vernacularname = nil
end
end  
if not vernacularname then
-- test if item label is not one of the scientific names
vernacularname = getItemLabel(item)
scnames = mergeClaims(targetStrs(item, P_TAXON_NAME))
for _, n in pairs(scnames) do
if vernacularname == n then
return
end
end
end
if vernacularname == i18n("no-label") then
return
end
return capitalize(vernacularname)
end
L.vernacularName = vernacularName  
local function authorString(item, namequalifiers, pid)
pid = pid or P_AUTHOR -- set default property
local concatstr = ', '
local authorids = qualifierTargetId(namequalifiers, pid) -- get qualifiers
if not next(authorids) then -- no qualifiers found, check properties
local authorset = targetId(item, pid)
local authors = {}
if authorset then -- create list from set
authorids = {}
for author,_ in pairs(authorset) do
table.insert(authorids, author)
end
end
end
local authors = createLinks(authorids, true)
if next(authors) then
local last = table.remove(authors)
local rest = ''
if #authors > 0 then
rest = table.concat(authors, concatstr) .. ' & '
end
return rest .. last
end
end
L.authorString = authorString  
-- create the taxon authors string, including year, ex authors and authors of the basionym
local function createAllAuthorsStr(item, namequalifiers, year)
local authors = authorString(item, namequalifiers)
local authorsstr = ''
if authors or not year == '????' then
if icnafpApplies then
-- check for basionym
local basionymids = targetId(item, P_BASINYM)
local basionymstr = ''
if next(basionymids) then
local basionym = mw.wikibase.getEntity('Q' .. next(basionymids))
local _,basionymnamequalifiers = targetStr(basionym, P_TAXON_NAME)
basionymstr = createAllAuthorsStr(basionym, basionymnamequalifiers)
if basionymstr ~= '' then
basionymstr = '(' .. basionymstr .. ') '
else
-- indicate missing basionym author
basionymstr = '(????) '
end
end  
-- check ex-authors
local exauthors = authorString(nil, namequalifiers, P_EX_AUTHOR)
exauthorsstr = ''
mw.log(exauthors)
if exauthors then
exauthorsstr = exauthors .. ' ex '
end
authorsstr = basionymstr .. exauthorsstr .. authors
if year then
authorsstr = authorsstr .. ' (' .. year .. ')'
end
else
authorsstr = authors .. ', ' .. year  
-- parentheses needed if instance of recombination
local recombination = false
for _,tid in pairs(qualifierTargetId(namequalifiers, P_INSTANCE_OF)) do
if tid == RECOMBINATION then
recombination = true
end
end  
if recombination then
authorsstr = '(' .. authorsstr .. ')'
end
end
end
return authorsstr
end
L.createAllAuthorsStr = createAllAuthorsStr  
-- show the stratigraphic range in which an extinct fossil existed
local function fossilParams(item, params)
local era1, era1references = next(targetId(item, P_ERA_START))
local era2, era2references = next(targetId(item, P_ERA_END))
if era1 and era2 and not (era1 == 'novalue' or era2 == 'novalue') then
local era1item = mw.wikibase.getEntity("Q" .. era1)
if era1item then
params["era[1][id]"] = era1
params["era[1][label]"] = getItemLabel(era1item)
if not (era1 == era2) then
local era2item = mw.wikibase.getEntity("Q" .. era2)
if era2item then
params["era[2][id]"] = era2
params["era[2][label]"] = getItemLabel(era2item)
end
end  
-- merge references from era2 to era1, only show once
for a, b in pairs(era2references) do
era1references[a] = b
end  
-- TODO: return data structure instead of pure str here
params["era[references]"] = era1references
end
end
end
L.fossilParams = fossilParams  
-- returns html for the given refids set
-- parameters:
-- refids: list of integer ID to create a list of <ref>-references
local function references(refids)
local = mw.getCurrentFrame
local refstr = ''
if refids then
for id,_ in pairs(refids) do
local ref = Cite.citeitem(id, getLang) or 'Error during creation of citation. Please report ' .. id .. ' at Module_talk:Cite'
mw.log('refstr for ', id, ref)
refstr = refstr .. :extensionTag('ref', ref, {name=id})
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