disp = i18n.missinginfocat
else
disp = prefix .. label .. postfix .. i18n.missinginfocat
end
end
else
local ccat = mw.wikibase.getBestStatements(id, "P373")[1]
if ccat and ccat.mainsnak.datavalue then
ccat = ccat.mainsnak.datavalue.value
disp = "" .. prefix .. label .. postfix .. ""
elseif sitelink then
-- this asumes that if a sitelink exists, then a label also exists
disp = "" .. prefix .. label .. postfix .. ""
else
-- no sitelink and no Commons cat, so return label from labelOrId for now
disp = prefix .. label .. postfix
end
end
return disp
end  
---
-- sourced takes a table representing a statement that may or may not have references
-- it looks for a reference sourced to something not containing the word "wikipedia"
-- it returns a boolean = true if it finds a sourced reference.
---
-- Dependencies:
---
local sourced = function(claim)
if claim.references then
for kr, vr in pairs(claim.references) do
local ref = mw.wikibase.renderSnaks(vr.snaks)
if not ref:find("Wiki") then
return true
end
end
end
end  
---
-- setRanks takes a flag (parameter passed) that requests the values to return
-- "b[est]" returns preferred if available, otherwise normal
-- "p[referred]" returns preferred
-- "n[ormal]" returns normal
-- "d[eprecated]" returns deprecated
-- multiple values are allowed, e.g. "preferred normal" (which is the default)
-- "best" will override the other flags, and set p and n
---
-- Dependencies:
---
local setRanks = function(rank)
rank = (rank or ""):lower
-- if nothing passed, return preferred and normal
-- if rank == "" then rank = "p n" end
local ranks = {}
for w in string.gmatch(rank, "%a+") do
w = w:sub(1,1)
if w == "b" or w == "p" or w == "n" or w == "d" then
ranks[w] = true
end
end
-- check if "best" is requested or no ranks requested; and if so, set preferred and normal
if ranks.b or not next(ranks) then
ranks.p = true
ranks.n = true
end
return ranks
end  
---
-- parseInput processes the Q-id , the blacklist and the whitelist
-- if an input parameter is supplied, it returns that and ends the call.
-- it returns (1) either the qid or nil indicating whether or not the call should continue
-- and (2) a table containing all of the statements for the propertyID and relevant Qid
-- if "best" ranks are requested, it returns those instead of all non-deprecated ranks
---
-- Dependencies:
---
local parseInput = function(, input_parm, property_id)
-- There may be a local parameter supplied, if it's blank, set it to nil
input_parm = mw.text.trim(input_parm or "")
if input_parm == "" then input_parm = nil end  
-- return nil if Wikidata is not available
if not mw.wikibase then return false, input_parm end  
local args = .args  
-- can take a named parameter |qid which is the Wikidata ID for the article.
-- if it's not supplied, use the id for the current page
local qid = args.qid or ""
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage end
-- if there's no Wikidata item for the current page return nil
if not qid then return false, input_parm end  
-- The blacklist is passed in named parameter |suppressfields
local blacklist = args.suppressfields or args.spf or ""  
-- The whitelist is passed in named parameter |fetchwikidata
local whitelist = args.fetchwikidata or args.fwd or ""
if whitelist == "" then whitelist = "NONE" end  
-- The name of the field that this function is called from is passed in named parameter |name
local fieldname = args.name or ""  
if blacklist ~= "" then
-- The name is compulsory when blacklist is used, so return nil if it is not supplied
if fieldname == "" then return false, nil end
-- If this field is on the blacklist, then return nil
if blacklist:find(fieldname) then return false, nil end
end  
-- If we got this far then we're not on the blacklist
-- The blacklist overrides any locally supplied parameter as well
-- If a non-blank input parameter was supplied return it
if input_parm then return false, input_parm end  
-- We can filter out non-valid properties
if property_id:sub(1,1):upper ~="P" or property_id == "P0" then return false, nil end  
-- Otherwise see if this field is on the whitelist:
-- needs a bit more logic because find will return its second value = 0 if fieldname is ""
-- but nil if fieldname not found on whitelist
local _, found = whitelist:find(fieldname)
found = ((found or 0) > 0)
if whitelist ~= 'ALL' and (whitelist:upper == "NONE" or not found) then
return false, nil
end  
-- See what's on Wikidata (the call always returns a table, but it may be empty):
local props = {}
if args.reqranks.b then
props = mw.wikibase.getBestStatements(qid, property_id)
else
props = mw.wikibase.getAllStatements(qid, property_id)
end
if props[1] then
return qid, props
end
-- no property on Wikidata
return false, nil
end  
---
-- createicon assembles the "Edit at Wikidata" pen icon.
-- It returns a wikitext string inside a span
-- if entityID is nil or empty, the ID associated with current page is used
-- langcode and propertyID may be nil or empty
---
-- Dependencies: i18n[];
---
local createicon = function(langcode, entityID, propertyID)
langcode = langcode or ""
if not entityID or entityID == "" then entityID= mw.wikibase.getEntityIdForCurrentPage end
propertyID = propertyID or ""
local icon = "&nbsp;<span>[["
-- "&nbsp;<span data-bridge-edit-flow='overwrite'>[[" -> enable Wikidata Bridge
.. i18n["filespace"]
.. ":OOjs UI icon edit-ltr-progressive.svg | |text-top | |alt="
.. i18n["editonwikidata"]
.. "|link=https://www.wikidata.org/wiki/" .. entityID
if langcode ~= "" then icon = icon .. "?uselang=" .. langcode end
if propertyID ~= "" then icon = icon .. "#" .. propertyID end
icon = icon .. "|" .. i18n["editonwikidata"] .. "]]</span>"
return icon
end  
---
-- assembleoutput takes the sequence table containing the property values
-- and formats it according to switches given. It returns a string or nil.
-- It uses the entityID (and optionally propertyID) to create a link in the pen icon.
---
-- Dependencies: parseParam;
---
local assembleoutput = function(out, args, entityID, propertyID)  
-- sorted is a boolean passed to enable sorting of the values returned
-- if nothing or an empty string is passed set it false
-- if "false" or "no" or "0" is passed set it false
local sorted = parseParam(args.sorted, false)  
-- noicon is a boolean passed to suppress the trailing "edit at Wikidata" icon
-- for use when the value is processed further by the infobox
-- if nothing or an empty string is passed set it false
-- if "false" or "no" or "0" is passed set it false
local noic = parseParam(args.noicon, false)  
-- list is the name of a template that a list of multiple values is passed through
-- examples include "hlist" and "ubl"
-- setting it to "prose" produces something like "1, 2, 3, and 4"
local list = args.list or ""  
-- sep is a string that is used to separate multiple returned values
-- if nothing or an empty string is passed set it to the default
-- any double-quotes " are stripped out, so that spaces may be passed
-- e.g. |sep=" - "
local sepdefault = i18n["list separator"]
local separator = args.sep or ""
separator = string.gsub(separator, '"', '')
if separator == "" then
separator = sepdefault
end  
-- collapse is a number that determines the maximum number of returned values
-- before the output is collapsed.
-- Zero or not a number result in no collapsing (default becomes 0).
local collapse = tonumber(args.collapse) or 0  
-- replacetext (rt) is a string that is returned instead of any non-empty Wikidata value
-- this is useful for tracking and debugging
local replacetext = mw.text.trim(args.rt or args.replacetext or "")  
-- if there's anything to return, then return a list
-- comma-separated by default, but may be specified by the sep parameter
-- optionally specify a hlist or ubl or a prose list, etc.
local strout
if #out > 0 then
if sorted then table.sort(out) end
-- if there's something to display and a pen icon is wanted, add it the end of the last value
local hasdisplay = false