local label
if v.mainsnak.snaktype == "value" then
if v.mainsnak.datatype == "wikibase-item" then
local qnumber = v.mainsnak.datavalue.value.id
local sitelink = mw.wikibase.getSitelink(qnumber)
if qnumber == "Q2818964" then sitelink = nil end -- suppress link to "Various authors"
if v.qualifiers and v.qualifiers.P1932 then
label = v.qualifiers.P1932[1].datavalue.value
else
label = mw.wikibase.getLabel(qnumber)
if label then
label = mw.text.nowiki(label)
else
label = qnumber -- should add tracking category
end
end
local position = maxpos + 1 -- Default to 'next' author.
-- use P1545 (series ordinal) instead of default position.
if v["qualifiers"] and v.qualifiers["P1545"] and v.qualifiers["P1545"][1] then
position = tonumber(v.qualifiers["P1545"][1].datavalue.value)
end
maxpos = math.max(maxpos, position)
if sitelink then
-- just the plain name,
-- but keep a record of the links, using the same index
out[position] = label
link[position] = sitelink
else
if wdl then
-- show that there's a Wikidata entry available
out[position] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]&nbsp;<span title='" .. i18n["errors"]["local-article-not-found"] .. "'></span>"
else
-- no Wikidata links wanted, so just give the plain label
out[position] = label
end
end
elseif v.mainsnak.datatype == "string" then
local position = maxpos + 1 -- Default to 'next' author.
-- use P1545 (series ordinal) instead of default position.
if v["qualifiers"] and v.qualifiers["P1545"] and v.qualifiers["P1545"][1] then
position = tonumber(v.qualifiers["P1545"][1].datavalue.value)
end
maxpos = math.max(maxpos, position)
out[position] = v.mainsnak.datavalue.value
else
-- not a wikibase-item or a string!
end
else
-- code here if we want to return something when author is "unknown"
if v.qualifiers and v.qualifiers.P1932 then
label = v.qualifiers.P1932[1].datavalue.value
else
label = i18n["unknown-author"] .. (i18n["unknown-author-trackingcat"] or "")
end
maxpos = maxpos + 1
out[maxpos] = label
end
return maxpos
end  
--[=[-------------------------< G E T _ N A M E _ L I S T >----------------------------------------------------
get_name_list -- adapted from getAuthors code taken from Module:RexxS
arguments:
nl_type - type of name list to fetch: nl_type = 'author' for authors; 'editor' for editors; 'translator' for translators
args - pointer to the parameter arguments table from the template call
qid - value from |qid= parameter; the Q-id of the source (book, etc.) in qid
wdl - value from the |wdl= parameter; a Boolean passed to enable links to Wikidata when no article exists
returns nothing; modifies the args table
]=]  
local function get_name_list (nl_type, args, qid, wdl)
local propertyID = "P50"
local fallbackID = "P2093" -- author name string  
if nl_type =="author" then
propertyID = 'P50' -- for authors
fallbackID = 'P2093' -- author-string
elseif nl_type =="editor" then
propertyID = 'P5769' -- "editor-in-chief"
fallbackID = 'P98' -- for editors - So-called "fallbacks" are actually a second set of properties processed
-- TBD. Take book series editors into account as well (if they have a separate P code as well)?
elseif nl_type == "translator" then
propertyID = 'P655' -- for translators
fallbackID = nil
-- elseif 'contributor' == nl_type then
-- f.e. author of forewords (P2679) and afterwords (P2680); requires |contribution=, |title= and |author=
-- propertyID = 'P' -- for contributors
-- fallbackID = nil
else
return -- not specified so return
end  
-- wdl is a Boolean passed to enable links to Wikidata when no article exists
-- if "false" or "no" or "0" is passed set it false
-- if nothing or an empty string is passed set it false
if wdl and (#wdl > 0) then
wdl = wdl:lower
wdl = in_array (wdl, {"false", "no", "0"})
else
-- wdl is empty, so
wdl = false
end  
local props = nil
local fallback = nil
if mw.wikibase.entityExists(qid) then
props = mw.wikibase.getAllStatements(qid, propertyID)
if props and fallbackID then
fallback = mw.wikibase.getAllStatements(qid, fallbackID)
end
end  
-- Make sure it actually has at least one of the properties requested
if not (props and props[1]) and not (fallback and fallback[1]) then
return nil
end  
-- So now we have something to return:
-- table 'out' is going to store the names(s):
-- and table 'link' will store any links to the name's article
local out = {}
local link = {}
local maxpos = 0
if props and props[1] then
for k, v in pairs(props) do
maxpos = makelink(v, out, link, maxpos, wdl)
end
end
if fallback and fallback[1] then
-- second properties
for k, v in pairs(fallback) do
maxpos = makelink(v, out, link, maxpos, wdl)
end
end  
-- if there's anything to return, then insert the additions in the template arguments table
-- in the form |author1=firstname secondname |author2= ...
-- Renumber, in case we have inconsistent numbering
local keys = {}
for k, v in pairs(out) do
keys[#keys + 1] = k
end
table.sort(keys) -- as they might be out of order
for i, k in ipairs(keys) do
out[k] = out[k]:gsub ('&#39;', '\''); -- prevent cs1|2 multiple names categorization; replace html entity with the actual character
mw.log(i .. " " .. k .. " " .. (out[k]))
if args[nl_type .. i] then -- name gets overwritten
-- pull corresponding -link only if overwritten name is same as WD name
if link[k] and (args[nl_type .. i] == out[k]) then
args[nl_type .. '-link' .. i] = args[nl_type .. '-link' .. i] or link[k] -- author-linkn or editor-linkn
end
else -- name does not get overwritten, so pull name from WD
args[nl_type .. i] = out[k]
if link[k] then
args[nl_type .. '-link' .. i] = args[nl_type .. '-link' .. i] or link[k] -- author-linkn or editor-linkn
end
end
end
end  
-- gets language codes used for a monolingual text property as a table
function p._getLangOfProp(qid, pid)
if not pid then return {} end
local out = {}
local props = mw.wikibase.getAllStatements(qid, pid)
for i, v in ipairs(props) do
if v.mainsnak.datatype == "monolingualtext" and v.mainsnak.datavalue then
out[#out + 1] = v.mainsnak.datavalue.value.language
end
end
return out
end
function p.getLangOfProp
local pid = .args.pid or mw.text.trim(.args[1] or "")
if pid == "" then return end
local qid = .args.qid
if qid == "" then qid = nil end
return table.concat(p._getLangOfProp(qid, pid), ", ")
end  
-- gets the language codes of a Wikidata entry as a table
local function _lang_code(qid)
local lc = getPropOfProp( {qid = qid, prop1 = "P407", prop2 = "P424", ps = 1} )
if lc then return mw.text.split( lc, "[, ]+" ) end
lc = getPropOfProp( {qid = qid, prop1 = "P407", prop2 = "P218", ps = 1} )
if lc then return mw.text.split( lc, "[, ]+" ) end
return p._getLangOfProp(qid, "P1476")
end
function p.lang_code
return table.concat(_lang_code(.args.qid or mw.text.trim(.args[1] or "")), ", ")
end  
-- export for debug
function p.getPropOfProp
return getPropOfProp(.args)
end  
-- wraps a string in nowiki unless disable flag is set
local function wrap_nowiki(str, disable)
if disable then return str or '' end
return mw.text.nowiki(str or '')
end  
-- sort sequence table whose values are key-value pairs by key
local function comp_key(a, b)
return a[1] < b[1]
end  
-- sort sequence table whose values are key-value pairs by value
local function comp_val(a, b)
return a[2] < b[2]
end  
--2 template parameters and passes all to . If neither of |author= and |author1=
are set, calls get_authors to try to get an author name-list from Wikidata. The result is passed to
for rendering.
---
function p._cite_q (citeq_args)
local = mw.getCurrentFrame  
-- parameters that don't get passed to Citation
local expand = citeq_args.expand -- when set to anything, causes to render <code></code>
local qid = citeq_args.qid or citeq_args[1]
local wdl = citeq_args.wdl
local template = citeq_args.template
citeq_args.expand = nil
citeq_args[1] = nil
citeq_args.qid = nil
citeq_args.wdl = nil
citeq_args.template = nil  
-- if title supplied, flag to not read html title
local titleforced = (citeq_args.title ~= nil)  
local oth = {}