-- It returns the article description for the Wikidata entity if the local parameter is "Wikidata".
-- Nothing is returned if the description doesn't exist or "" is passed as the local parameter.
---
-- Dependencies:
---
p.getDescription = function
local desc = mw.text.trim(.args[1] or "")
local itemID = mw.text.trim(.args.qid or "")
if itemID == "" then itemID = nil end
if desc:lower == 'wikidata' then
return mw.wikibase.getDescription(itemID)
elseif desc:lower == '' then
return nil
else
return desc
end
end  
---
-- getAliases has the qid of a Wikidata entity passed as |qid=
-- (it defaults to the associated qid of the current article if omitted)
-- and a local parameter passed as the first unnamed parameter.
-- It implements blacklisting and whitelisting with a field name of "alias" by default.
-- Any local parameter passed becomes the return value.
-- Otherwise it returns the aliases for the Wikidata entity with the usual list options.
-- Nothing is returned if the aliases do not exist.
---
-- Dependencies: findLang; assembleoutput
---
p.getAliases = function
local args = .args  
local fieldname = args.name or ""
if fieldname == "" then fieldname = "alias" end  
local blacklist = args.suppressfields or args.spf or ""
if blacklist:find(fieldname) then return nil end  
local localval = mw.text.trim(args[1] or "")
if localval ~= "" then return localval end  
local whitelist = args.fetchwikidata or args.fwd or ""
if whitelist == "" then whitelist = "NONE" end
if not (whitelist == 'ALL' or whitelist:find(fieldname)) then return nil end  
local qid = args.qid or ""
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage end
if not qid or not mw.wikibase.entityExists(qid) then return nil end  
local aliases = mw.wikibase.getEntity(qid).aliases
if not aliases then return nil end  
args.langobj = findLang(args.lang)
local langcode = args.langobj.code
args.lang = langcode  
local out = {}
for k1, v1 in pairs(aliases) do
if v1[1].language == langcode then
for k1, v2 in ipairs(v1) do
out[#out+1] = v2.value
end
break
end
end  
return assembleoutput(out, args, qid)
end  
---
-- pageId returns the page id (entity ID, Qnnn) of the current page
-- returns nothing if the page is not connected to Wikidata
---
-- Dependencies:
---
p.pageId = function
return mw.wikibase.getEntityIdForCurrentPage
end  
---
-- formatDate is a wrapper to export the private function format_Date
---
-- Dependencies: format_Date;
---
p.formatDate = function
return format_Date(.args[1], .args.df, .args.bc)
end  
---
-- location is a wrapper to export the private function _location
-- it takes the entity-id as qid or the first unnamed parameter
-- optional boolean parameter first toggles the display of the first item
-- optional boolean parameter skip toggles the display to skip to the last item
-- parameter debug=<y/n> (default 'n') adds error msg if not a location
---
-- Dependencies: _location;
---
p.location = function
local debug = (.args.debug or ""):sub(1, 1):lower
if debug == "" then debug = "n" end
local qid = mw.text.trim(.args.qid or .args[1] or ""):upper
if qid == "" then qid=mw.wikibase.getEntityIdForCurrentPage end
if not qid then
if debug ~= "n" then
return i18n.errors["entity-not-found"]
else
return nil
end
end
local first = mw.text.trim(.args.first or "")
local skip = mw.text.trim(.args.skip or "")
return table.concat( _location(qid, first, skip), ", " )
end  
---
-- checkBlacklist implements a test to check whether a named field is allowed
-- returns true if the field is not blacklisted (i.e. allowed)
-- returns false if the field is blacklisted (i.e. disallowed)
---
-- displays "blacklisted"
---
-- displays "not blacklisted"
---
-- Dependencies:
---
p.checkBlacklist = function
local blacklist = .args.suppressfields or .args.spf or ""
local fieldname = .args.name or ""
if blacklist ~= "" and fieldname ~= "" then
if blacklist:find(fieldname) then
return false
else
return true
end
else
-- one of the fields is missing: let's call that "not on the list"
return true
end
end  
---
-- emptyor returns nil if its first unnamed argument is just punctuation, whitespace or html tags
-- otherwise it returns the argument unchanged (including leading/trailing space).
-- If the argument may contain "=", then it must be called explicitly:
-- |1=arg
-- (In that case, leading and trailing spaces are trimmed)
-- It finds use in infoboxes where it can replace tests like:
---
-- with a form that uses just a single call to Wikidata:
---
---
-- Dependencies:
---
p.emptyor = function
local s = .args[1] or ""
if s == "" then return nil end
local sx = s:gsub("%s", ""):gsub("<[^>]*>", ""):gsub("%p", "")
if sx == "" then
return nil
else
return s
end
end  
---
-- labelorid is a public function to expose the output of labelOrId
-- Pass the Q-number as |qid= or as an unnamed parameter.
-- It returns the Wikidata label for that entity or the qid if no label exists.
---
-- Dependencies: labelOrId
---
p.labelorid = function
return (labelOrId(.args.qid or .args[1]))
end  
---
-- getLang returns the MediaWiki language code of the current content.
-- If optional parameter |style=full, it returns the language name.
---
-- Dependencies:
---
p.getLang = function
local style = (.args.style or ""):lower
local langcode = mw.language.getContentLanguage.code
if style == "full" then
return mw.language.fetchLanguageName( langcode )
end
return langcode
end  
---
-- getItemLangCode takes a qid parameter (using the current page's qid if blank)
-- If the item for that qid has property country (P17) it looks at the first preferred value
-- If the country has an official language (P37), it looks at the first preferred value
-- If that official language has a language code (P424), it returns the first preferred value
-- Otherwise it returns nothing.
---
-- Dependencies: _getItemLangCode
---
p.getItemLangCode = function
return _getItemLangCode(.args.qid or .args[1])
end  
---
-- findLanguage exports the local findLang function
-- It takes an optional language code and returns, in order of preference:
-- the code if a known language;
-- the user's language, if set;
-- the server's content language.
---
-- Dependencies: findLang
---
p.findLanguage = function
return findLang(.args.lang or .args[1]).code
end  
---
-- getQid returns the qid, if supplied
-- failing that, the Wikidata entity ID of the "category's main topic (P301)", if it exists
-- failing that, the Wikidata entity ID associated with the current page, if it exists
-- otherwise, nothing
---
-- Dependencies:
---
p.getQid = function
local qid = (.args.qid or ""):upper
-- check if a qid was passed; if so, return it:
if qid ~= "" then return qid end
-- check if there's a "category's main topic (P301)":
qid = mw.wikibase.getEntityIdForCurrentPage
if qid then
local prop301 = mw.wikibase.getBestStatements(qid, "P301")
if prop301[1] then
local mctid = prop301[1].mainsnak.datavalue.value.id
if mctid then return mctid end
end
end
-- otherwise return the page qid (if any)
return qid
end  
---
-- followQid takes four optional parameters: qid, props, list and all.
-- If qid is not given, it uses the qid for the connected page
-- or returns nil if there isn't one.
-- props is a list of properties, separated by punctuation.
-- If props is given, the Wikidata item for the qid is examined for each property in turn.
-- If that property contains a value that is another Wikibase-item, that item's qid is returned,
-- and the search terminates, unless |all=y when all of the qids are returned, separated by spaces.
-- If |list= is set to a template, the qids are passed as arguments to the template.
-- If props is not given, the qid is returned.
---
-- Dependencies: parseParam
---
p._followQid = function(args)
local qid = (args.qid or ""):upper
local all = parseParam(args.all, false)
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 = {}