local list = args.list or ""
if list == "" then list = nil end
if qid == "" then
qid = mw.wikibase.getEntityIdForCurrentPage
end
if not qid then return nil end
local out = {}
local props = (args.props or ""):upper
if props ~= "" then
for p in mw.text.gsplit(props, "%p") do -- split at punctuation and iterate
p = mw.text.trim(p)
for i, v in ipairs( mw.wikibase.getBestStatements(qid, p) ) do
local linkedid = v.mainsnak.datavalue and v.mainsnak.datavalue.value.id
if linkedid then
if all then
out[#out+1] = linkedid
else
return linkedid
end -- test for all or just the first one found
end -- test for value exists for that property
end -- loop through values of property to follow
end -- loop through list of properties to follow
end
if #out > 0 then
local ret = ""
if list then
ret = mw.getCurrentFrame:expandTemplate{title = list, args = out}
else
ret = table.concat(out, " ")
end
return ret
else
return qid
end
end  
p.followQid = function
return p._followQid(.args)
end  
---
-- globalSiteID returns the globalSiteID for the current wiki
-- e.g. returns "enwiki" for the English Wikipedia, "enwikisource" for English Wikisource, etc.
---
-- Dependencies:
---
p.globalSiteID = function
return mw.wikibase.getGlobalSiteId
end  
---
-- siteID returns the root of the globalSiteID
-- e.g. "en" for "enwiki", "enwikisource", etc.
-- treats "en-gb" as "en", etc.
---
-- Dependencies:
---
p.siteID = function
local txtlang = :preprocess( "" ) or ""
-- This deals with specific exceptions: be-tarask -> be-x-old
if txtlang == "be-tarask" then
return "be_x_old"
end
local pos = txtlang:find("-")
local ret = ""
if pos then
ret = txtlang:sub(1, pos-1)
else
ret = txtlang
end
return ret
end  
---
-- projID returns the code used to link to the reader's language's project
-- e.g "en" for :en:WikidataIB
-- treats "en-gb" as "en", etc.
---
-- Dependencies:
---
p.projID = function
local txtlang = :preprocess( "" ) or ""
-- This deals with specific exceptions: be-tarask -> be-x-old
if txtlang == "be-tarask" then
return "be-x-old"
end
local pos = txtlang:find("-")
local ret = ""
if pos then
ret = txtlang:sub(1, pos-1)
else
ret = txtlang
end
return ret
end  
---
-- formatNumber formats a number according to the the supplied language code ("|lang=")
-- or the default language if not supplied.
-- The number is the first unnamed parameter or "|num="
---
-- Dependencies: findLang
---
p.formatNumber = function
local lang
local num = tonumber(.args[1] or .args.num) or 0
lang = findLang(.args.lang)
return lang:formatNum( num )
end  
---
-- examine dumps the property (the unnamed parameter or pid)
-- from the item given by the parameter 'qid' (or the other unnamed parameter)
-- or from the item corresponding to the current page if qid is not supplied.
-- e.g.
-- or or any combination of these
-- or for the current page.
---
-- Dependencies:
---
p.examine = function
local args
if .args[1] or .args.pid or .args.qid then
args = .args
else
args = :getParent.args
end
local par = {}
local pid = (args.pid or ""):upper
local qid = (args.qid or ""):upper
par[1] = mw.text.trim( args[1] or "" ):upper
par[2] = mw.text.trim( args[2] or "" ):upper
table.sort(par)
if par[2]:sub(1,1) == "P" then par[1], par[2] = par[2], par[1] end
if pid == "" then pid = par[1] end
if qid == "" then qid = par[2] end
local q1 = qid:sub(1,1)
if pid:sub(1,1) ~= "P" then return "No property supplied" end
if q1 ~= "Q" and q1 ~= "M" then qid = mw.wikibase.getEntityIdForCurrentPage end
if not qid then return "No item for this page" end
return "<pre>" .. mw.dumpObject( mw.wikibase.getAllStatements( qid, pid ) ) .. "</pre>"
end  
---
-- checkvalue looks for 'val' as a wikibase-item value of a property (the unnamed parameter or pid)
-- from the item given by the parameter 'qid'
-- or from the Wikidata item associated with the current page if qid is not supplied.
-- It only checks ranks that are requested (preferred and normal by default)
-- If property is not supplied, then P31 (instance of) is assumed.
-- It returns val if found or nothing if not found.
-- e.g.
-- or
-- or
-- or for the current page.
---
-- Dependencies:
---
p.checkvalue = function
local args
if .args.val then
args = .args
else
args = :getParent.args
end
local val = args.val
if not val then return nil end
local pid = mw.text.trim(args.pid or args[1] or "P31"):upper
local qid = (args.qid or ""):upper
if pid:sub(1,1) ~= "P" then return nil end
if qid:sub(1,1) ~= "Q" then qid = mw.wikibase.getEntityIdForCurrentPage end
if not qid then return nil end
local ranks = setRanks(args.rank)
local stats = {}
if ranks.b then
stats = mw.wikibase.getBestStatements(qid, pid)
else
stats = mw.wikibase.getAllStatements( qid, pid )
end
if not stats[1] then return nil end
if stats[1].mainsnak.datatype == "wikibase-item" then
for k, v in pairs( stats ) do
local ms = v.mainsnak
if ranks[v.rank:sub(1,1)] and ms.snaktype == "value" and ms.datavalue.value.id == val then
return val
end
end
end
return nil
end  
---
-- url2 takes a parameter url= that is a proper url and formats it for use in an infobox.
-- If no parameter is supplied, it returns nothing.
-- This is the equivalent of Template:URL
-- but it keeps the "edit at Wikidata" pen icon out of the microformat.
-- Usually it will take its url parameter directly from a Wikidata call:
-- e.g.
---
-- Dependencies:
---
p.url2 = function
local txt = .args.url or ""
if txt == "" then return nil end
-- extract any icon
local url, icon = txt:match("(.+)&nbsp;(.+)")
-- make sure there's at least a space at the end
url = (url or txt) .. " "
icon = icon or ""
-- extract any protocol like https://
local prot = url:match("(https*://).+[ \"\']")
-- extract address
local addr = ""
if prot then
addr = url:match("https*://(.+)[ \"\']") or " "
else
prot = "//"
addr = url:match("[^%p%s]+%.(.+)[ \"\']") or " "
end
-- strip trailing / from end of domain-only url and add <wbr/> before . and /
local disp, n = addr:gsub( "^([^/]+)/$", "%1" ):gsub("%/", "<wbr/>/"):gsub("%.", "<wbr/>.")
return '<span>[' .. prot .. addr .. " " .. disp .. "]</span>&nbsp;" .. icon
end  
---
-- getWebsite fetches the Official website (P856) and formats it for use in an infobox.
-- This is similar to Template:Official website but with a url displayed,
-- and it adds the "edit at Wikidata" pen icon beyond the microformat if enabled.
-- A local value will override the Wikidata value. "NONE" returns nothing.
-- e.g.
---
-- Dependencies: findLang; parseParam;
---
p.getWebsite = function
local url = .args.url or ""
if url:upper == "NONE" then return nil end
local urls = {}
local quals = {}
local qid = .args.qid or ""
if url and url ~= "" then
urls[1] = url
else
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage end
if not qid then return nil end  
local prop856 = mw.wikibase.getBestStatements(qid, "P856")
for k, v in pairs(prop856) do
if v.mainsnak.snaktype == "value" then
urls[#urls+1] = v.mainsnak.datavalue.value
if v.qualifiers and v.qualifiers["P1065"] then
-- just take the first archive url (P1065)
local au = v.qualifiers["P1065"][1]
if au.snaktype == "value" then
quals[#urls] = au.datavalue.value
end -- test for archive url having a value
end -- test for qualifers
end -- test for website having a value
end -- loop through website(s)
end
if #urls == 0 then return nil end  
local out = {}
for i, u in ipairs(urls) do
local link = quals[i] or u
local prot, addr = u:match("(http[s]*://)(.+)")
addr = addr or u
local disp, n = addr:gsub("%.", "<wbr/>%.")
out[#out+1] = '<span>[' .. link .. " " .. disp .. "]</span>"
end  
local langcode = findLang(.args.lang).code
local noicon = parseParam(.args.noicon, false)
if url == "" and not noicon then
out[#out] = out[#out] .. createicon(langcode, qid, "P856")
end  
local ret = ""
if #out > 1 then
ret = mw.getCurrentFrame:expandTemplate{title = "ubl", args = out}
else
ret = out[1]
end  
return ret
end  
---
-- getAllLabels fetches the set of labels and formats it for display as wikitext.