Module:WikidataIB: Difference between revisions

m
1 revision imported
m (1 revision imported)
 
m (1 revision imported)
 
(3 intermediate revisions by 2 users not shown)
Line 12: Line 12:
local p = {}
local p = {}


local cdate = require("Module:Complex date")._complex_date
local cdate -- initialise as nil and only load _complex_date function if needed
-- [[Module:Complex date]] has the following dependencies:
-- [[Module:Complex date]] is loaded lazily and has the following dependencies:
-- Module:I18n/complex date, Module:ISOdate, Module:DateI18n (alternative for Module:Date),
-- Module:I18n/complex date, Module:ISOdate, Module:DateI18n (alternative for Module:Date),
-- Module:Formatnum, Module:I18n/date, Module:Yesno, Module:Linguistic, Module:Calendar
-- Module:Formatnum, Module:I18n/date, Module:Yesno, Module:Linguistic, Module:Calendar
Line 80: Line 80:
}
}
}
}
-- This allows a internationisation module to override the above table
-- This allows an internationisation module to override the above table
if 'en' ~= mw.getContentLanguage():getCode() then
if 'en' ~= mw.getContentLanguage():getCode() then
require("Module:i18n").loadI18n("Module:WikidataIB/i18n", i18n)
require("Module:i18n").loadI18n("Module:WikidataIB/i18n", i18n)
Line 87: Line 87:
-- This piece of html implements a collapsible container. Check the classes exist on your wiki.
-- This piece of html implements a collapsible container. Check the classes exist on your wiki.
local collapsediv = '<div class="mw-collapsible mw-collapsed" style="width:100%; overflow:auto;" data-expandtext="{{int:show}}" data-collapsetext="{{int:hide}}">'
local collapsediv = '<div class="mw-collapsible mw-collapsed" style="width:100%; overflow:auto;" data-expandtext="{{int:show}}" data-collapsetext="{{int:hide}}">'
-- Some items should not be linked.
-- Each wiki can create a list of those in Module:WikidataIB/nolinks
-- It should return a table called itemsindex, containing true for each item not to be linked
local donotlink = {}
local nolinks_exists, nolinks = pcall(mw.loadData, "Module:WikidataIB/nolinks")
if nolinks_exists then
donotlink = nolinks.itemsindex
end
-- To satisfy Wikipedia:Manual of Style/Titles, certain types of items are italicised, and others are quoted.
-- The submodule [[Module:WikidataIB/titleformats]] lists the entity-ids used in 'instance of' (P31),
-- which allows this module to identify the values that should be formatted.
-- WikidataIB/titleformats exports a table p.formats, which is indexed by entity-id, and contains the value " or ''
local formats = {}
local titleformats_exists, titleformats = pcall(mw.loadData, "Module:WikidataIB/titleformats")
if titleformats_exists then
formats = titleformats.formats
end


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Line 94: Line 113:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- makeOrdinal needs to be internationalised along with the above:
-- makeOrdinal needs to be internationalised along with the above:
-- takes cardinal numer as a numeric and returns the ordinal as a string
-- takes cardinal number as a numeric and returns the ordinal as a string
-- we need three exceptions in English for 1st, 2nd, 3rd, 21st, .. 31st, etc.
-- we need three exceptions in English for 1st, 2nd, 3rd, 21st, .. 31st, etc.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Line 118: Line 137:


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- findLang takes a "langcode" parameter if if supplied and valid
-- findLang takes a "langcode" parameter if supplied and valid
-- otherwise it tries to create it from the user's set language ({{int:lang}})
-- otherwise it tries to create it from the user's set language ({{int:lang}})
-- failing that it uses the wiki's content language.
-- failing that it uses the wiki's content language.
Line 139: Line 158:
end
end
return langobj
return langobj
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: none
-------------------------------------------------------------------------------
local _getItemLangCode = function(qid)
qid = mw.text.trim(qid or ""):upper()
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return end
local prop17 = mw.wikibase.getBestStatements(qid, "P17")[1]
if not prop17 or prop17.mainsnak.snaktype ~= "value" then return end
local qid17 = prop17.mainsnak.datavalue.value.id
local prop37 = mw.wikibase.getBestStatements(qid17, "P37")[1]
if not prop37 or prop37.mainsnak.snaktype ~= "value" then return end
local qid37 = prop37.mainsnak.datavalue.value.id
local prop424 = mw.wikibase.getBestStatements(qid37, "P424")[1]
if not prop424 or prop424.mainsnak.snaktype ~= "value" then return end
return prop424.mainsnak.datavalue.value
end
end


Line 284: Line 328:
-- dateFormat is the handler for properties that are of type "time"
-- dateFormat is the handler for properties that are of type "time"
-- It takes timestamp, precision (6 to 11 per mediawiki), dateformat (y/dmy/mdy), BC format (BC/BCE),
-- It takes timestamp, precision (6 to 11 per mediawiki), dateformat (y/dmy/mdy), BC format (BC/BCE),
-- a plaindate switch (yes/no/adj) to en/disable "sourcing cirumstances"/use adjectival form,
-- a plaindate switch (yes/no/adj) to en/disable "sourcing circumstances"/use adjectival form,
-- any qualifiers for the property, the language, and any adjective to use like 'before'.
-- any qualifiers for the property, the language, and any adjective to use like 'before'.
-- It passes the date through the "complex date" function
-- It passes the date through the "complex date" function
Line 291: Line 335:
-- Dependencies: findLang(); cdate(); dp[]
-- Dependencies: findLang(); cdate(); dp[]
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local dateFormat = function(timestamp, dprec, df, bcf, pd, qualifiers, lang, adj)
local dateFormat = function(timestamp, dprec, df, bcf, pd, qualifiers, lang, adj, model)
-- output formatting according to preferences (y/dmy/mdy/ymd)
df = (df or ""):lower()
-- if ymd is required, return the part of the timestamp in YYYY-MM-DD form
-- but apply Year zero#Astronomers fix: 1 BC = 0000; 2 BC = -0001; etc.
if df == "ymd" then
if timestamp:sub(1,1) == "+" then
return timestamp:sub(2,11)
else
local yr = tonumber(timestamp:sub(2,5)) - 1
yr = ("000" .. yr):sub(-4)
if yr ~= "0000" then yr = "-" .. yr end
return yr .. timestamp:sub(6,11)
end
end
-- A year can be stored like this: "+1872-00-00T00:00:00Z",
-- A year can be stored like this: "+1872-00-00T00:00:00Z",
-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z",
-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z",
Line 297: Line 355:
-- So fix the month 0, day 0 timestamp to become 1 January instead:
-- So fix the month 0, day 0 timestamp to become 1 January instead:
timestamp = timestamp:gsub("%-00%-00T", "-01-01T")
timestamp = timestamp:gsub("%-00%-00T", "-01-01T")
-- output formatting according to preferences (y/dmy/mdy)
df = (df or ""):lower()
-- just in case date precision is missing
-- just in case date precision is missing
dprec = dprec or 11
dprec = dprec or 11
Line 336: Line 392:
end
end
end
end
local fdate = cdate("", adj, tostring(iso), dp[dprec], bc, "", "", "", "", lang, 1)
-- deal with Julian dates:
-- no point in saying that dates before 1582 are Julian - they are by default
-- doesn't make sense for dates less precise than year
-- we can suppress it by setting |plaindate, e.g. for use in constructing categories.
local calendarmodel = ""
if tonumber(year) > 1582
and dprec > 8
and not pd
and model == "http://www.wikidata.org/entity/Q1985786" then
calendarmodel = "julian"
end
if not cdate then
cdate = require("Module:Complex date")._complex_date
end
local fdate = cdate(calendarmodel, adj, tostring(iso), dp[dprec], bc, "", "", "", "", lang, 1)
-- this may have QuickStatements info appended to it in a div, so remove that
-- this may have QuickStatements info appended to it in a div, so remove that
fdate = fdate:gsub(' <div style="display: none;">[^<]*</div>', '')
fdate = fdate:gsub(' <div style="display: none;">[^<]*</div>', '')
-- it may also be returned wrapped in a microformat, so remove that
-- it may also be returned wrapped in a microformat, so remove that
fdate = fdate:gsub("<[^>]*>", "")
fdate = fdate:gsub("<[^>]*>", "")
-- there may be leading zeros that we should remove
fdate = fdate:gsub("^0*", "")
-- if a plain date is required, then remove any links (like BC linked)
if pd then
fdate = fdate:gsub("%[%[.*|", ""):gsub("]]", "")
end
-- if 'circa', use the abbreviated form *** internationalise later ***
-- if 'circa', use the abbreviated form *** internationalise later ***
fdate = fdate:gsub('circa ', '<abbr title="circa">c.</abbr>&nbsp;')
fdate = fdate:gsub('circa ', '<abbr title="circa">c.</abbr>&nbsp;')
Line 364: Line 440:
-- it makes the empty string and nil into the (boolean) value passed as default
-- it makes the empty string and nil into the (boolean) value passed as default
-- allowing the parameter to be true or false by default.
-- allowing the parameter to be true or false by default.
-- It returns a boolean.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local parseParam = function(param, default)
local parseParam = function(param, default)
if type(param) == "boolean" then param = tostring(param) end
if param and param ~= "" then
if param and param ~= "" then
param = param:lower()
param = param:lower()
Line 392: Line 470:
local _getSitelink = function(qid, wiki)
local _getSitelink = function(qid, wiki)
qid = (qid or ""):upper()
qid = (qid or ""):upper()
if qid == "" then return nil end
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return nil end
wiki = wiki or ""
wiki = wiki or ""
local sitelink
local sitelink
if wiki == "" then
if wiki == "" then
sitelink = mw.wikibase.sitelink(qid)
sitelink = mw.wikibase.getSitelink(qid)
else
else
sitelink = mw.wikibase.sitelink(qid, wiki)
sitelink = mw.wikibase.getSitelink(qid, wiki)
end
end
return sitelink
return sitelink
Line 407: Line 486:
-- _getCommonslink takes an optional qid of a Wikidata entity passed as |qid=
-- _getCommonslink takes an optional qid of a Wikidata entity passed as |qid=
-- It returns one of the following in order of preference:
-- It returns one of the following in order of preference:
-- the Commons sitelink of the linked Wikidata item;
-- the Commons sitelink of the Wikidata entity - but not if onlycat=true and it's not a category;
-- the Commons sitelink of the topic's main category of the linked Wikidata item;
-- the Commons sitelink of the topic's main category of the Wikidata entity;
-- the Commons category.
-- the Commons category of the Wikidata entity - unless fallback=false.
-- If the optional parameter onlycat is true/yes/1 then only categories are returned
-- defaults to false.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: _getSitelink(); parseParam()
-- Dependencies: _getSitelink(); parseParam()
Line 420: Line 497:
if not qid then return nil end
if not qid then return nil end
onlycat = parseParam(onlycat, false)
onlycat = parseParam(onlycat, false)
if fallback == "" then fallback = nil end
local sitelink = _getSitelink(qid, "commonswiki")
local sitelink = _getSitelink(qid, "commonswiki")
if onlycat and sitelink and sitelink:sub(1,9) ~= "Category:" then sitelink = nil end
if onlycat and sitelink and sitelink:sub(1,9) ~= "Category:" then sitelink = nil end
Line 426: Line 504:
local prop910 = mw.wikibase.getBestStatements(qid, "P910")[1]
local prop910 = mw.wikibase.getBestStatements(qid, "P910")[1]
if prop910 then
if prop910 then
local tmcid = prop910.mainsnak.datavalue.value.id
local tmcid = prop910.mainsnak.datavalue and prop910.mainsnak.datavalue.value.id
sitelink = _getSitelink(tmcid, "commonswiki")
sitelink = _getSitelink(tmcid, "commonswiki")
end
if not sitelink then
-- check for list's main category
local prop1754 = mw.wikibase.getBestStatements(qid, "P1754")[1]
if prop1754 then
local tmcid = prop1754.mainsnak.datavalue and prop1754.mainsnak.datavalue.value.id
sitelink = _getSitelink(tmcid, "commonswiki")
end
end
end
end
end
Line 434: Line 520:
local prop373 = mw.wikibase.getBestStatements(qid, "P373")[1]
local prop373 = mw.wikibase.getBestStatements(qid, "P373")[1]
if prop373 then
if prop373 then
sitelink = prop373.mainsnak.datavalue.value
sitelink = prop373.mainsnak.datavalue and prop373.mainsnak.datavalue.value
if sitelink then sitelink = "Category:" .. sitelink end
if sitelink then sitelink = "Category:" .. sitelink end
end
end
Line 451: Line 537:
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local labelOrId = function(id)
local labelOrId = function(id, lang)
local label = mw.wikibase.label(id)
if lang == "default" then lang = findLang().code end
local label
if lang then
label = mw.wikibase.getLabelByLang(id, lang)
else
label = mw.wikibase.getLabel(id)
end
if label then
if label then
return mw.text.nowiki(label), true
return mw.text.nowiki(label), true
Line 469: Line 561:
-- 4. Display unlinked label if it exists
-- 4. Display unlinked label if it exists
-- 5. Display entity-id for now to indicate a label could be provided
-- 5. Display entity-id for now to indicate a label could be provided
-- dtxt is text to be used instead of label, or nil.
-- shortname is boolean switch to use P1813 (short name) instead of label if true.
-- lang is the current language code.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: labelOrId()
-- Dependencies: labelOrId(); donotlink[]
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local linkedItem = function(id, lprefix, lpostfix, prefix, postfix)
local linkedItem = function(id, lprefix, lpostfix, prefix, postfix, dtxt, shortname, lang)
lprefix = lprefix or "" -- toughen against nil values passed
lprefix = lprefix or "" -- toughen against nil values passed
lpostfix = lpostfix or ""
lpostfix = lpostfix or ""
prefix = prefix or ""
prefix = prefix or ""
postfix = postfix or ""
postfix = postfix or ""
lang = lang or "en" -- fallback to default if missing
-- see if item might need italics or quotes
local fmt = ""
for k, v in ipairs( mw.wikibase.getBestStatements(id, "P31") ) do
if v.mainsnak.datavalue and formats[v.mainsnak.datavalue.value.id] then
fmt = formats[v.mainsnak.datavalue.value.id]
break -- pick the first match
end
end
local disp
local disp
local sitelink = mw.wikibase.sitelink(id)
local sitelink = mw.wikibase.getSitelink(id)
local label, islabel = labelOrId(id)
local label, islabel
if dtxt then
label, islabel = dtxt, true
elseif shortname then
-- see if there is a shortname in our language, and set label to it
for k, v in ipairs( mw.wikibase.getBestStatements(id, "P1813") ) do
if v.mainsnak.datavalue.value.language == lang then
label, islabel = v.mainsnak.datavalue.value.text, true
break
end -- test for language match
end -- loop through values of short name
-- if we have no label set, then there was no shortname available
if not islabel then
label, islabel = labelOrId(id)
shortname = false
end
else
label, islabel = labelOrId(id)
end
if mw.site.siteName ~= "Wikimedia Commons" then
if mw.site.siteName ~= "Wikimedia Commons" then
if sitelink then
if sitelink then
-- strip any namespace or dab from the sitelink and use that as label
if not (dtxt or shortname) then
local pos = sitelink:find(":") or 0
-- strip any namespace or dab from the sitelink
label = sitelink:sub(pos+1):gsub("%s%(.+%)$", ""):gsub(",.+$", "")
local pos = sitelink:find(":") or 0
disp = "[[" .. lprefix .. sitelink .. lpostfix .. "|" .. prefix .. label .. postfix .. "]]"
local slink = sitelink
if pos > 0 then
local prefix = sitelink:sub(1,pos-1)
if mw.site.namespaces[prefix] then -- that prefix is a valid namespace, so remove it
slink = sitelink:sub(pos+1)
end
end
-- remove stuff after commas or inside parentheses - ie. dabs
slink = slink:gsub("%s%(.+%)$", ""):gsub(",.+$", "")
--  use that as label, preserving label case - find("^%u") is true for 1st char uppercase
if label:find("^%u") then
label = slink:gsub("^(%l)", string.upper)
else
label = slink:gsub("^(%u)", string.lower)
end
end
if donotlink[label] then
disp = prefix .. fmt .. label .. fmt .. postfix
else
disp = "[[" .. lprefix .. sitelink .. lpostfix .. "|" .. prefix .. fmt .. label .. fmt .. postfix .. "]]"
end
elseif islabel then
elseif islabel then
-- no sitelink, label exists, so check if a redirect with that title exists
-- no sitelink, label exists, so check if a redirect with that title exists
local artitle = mw.title.new(label, 0)
local artitle = mw.title.new(label, 0) -- only nil if label has invalid chars
if artitle and artitle.redirectTarget then
if not donotlink[label] and artitle and artitle.redirectTarget then
-- there's a redirect with the same title as the label, so let's link to that
-- there's a redirect with the same title as the label, so let's link to that
disp = "[[".. lprefix .. label .. lpostfix .. "|" .. prefix .. label .. postfix .. "]]"
disp = "[[".. lprefix .. label .. lpostfix .. "|" .. prefix .. fmt .. label .. fmt .. postfix .. "]]"
else
else
-- no sitelink, label exists, not a redirect, so output the plain label
-- either (donotlink is true) or (no sitelink, label exists, not redirect)
disp = prefix .. label .. postfix
-- so output unlinked label with italics or quotes as needed
disp = prefix .. fmt .. label .. fmt .. postfix
end -- test if article title exists as redirect on current Wiki
end -- test if article title exists as redirect on current Wiki
else
else
-- no sitelink and no label, so return whatever was returned from labelOrId for now
-- no sitelink and no label, so return whatever was returned from labelOrId for now
-- add tracking category [[Category:Articles with missing Wikidata information]]
-- add tracking category [[Category:Articles with missing Wikidata information]]
disp = prefix .. label .. postfix .. i18n.missinginfocat
-- for enwiki, just return the tracking category
if mw.wikibase.getGlobalSiteId() == "enwiki" then
disp = i18n.missinginfocat
else
disp = prefix .. label .. postfix .. i18n.missinginfocat
end
end
end
else
else
local ccat = mw.wikibase.getBestStatements(id, "P373")[1]
local ccat = mw.wikibase.getBestStatements(id, "P373")[1]
if ccat then
if ccat and ccat.mainsnak.datavalue then
ccat = ccat.mainsnak.datavalue.value
ccat = ccat.mainsnak.datavalue.value
disp = "[[" .. lprefix .. "Category:" .. ccat .. lpostfix .. "|" .. prefix .. label .. postfix .. "]]"
disp = "[[" .. lprefix .. "Category:" .. ccat .. lpostfix .. "|" .. prefix .. label .. postfix .. "]]"
Line 520: Line 668:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- sourced takes a table representing a statement that may or may not have references
-- sourced takes a table representing a statement that may or may not have references
-- it counts how many references are sourced to something not containing the word "wikipedia"
-- it looks for a reference sourced to something not containing the word "wikipedia"
-- it returns a boolean = true if there are any sourced references.
-- it returns a boolean = true if it finds a sourced reference.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
Line 529: Line 677:
for kr, vr in pairs(claim.references) do
for kr, vr in pairs(claim.references) do
local ref = mw.wikibase.renderSnaks(vr.snaks)
local ref = mw.wikibase.renderSnaks(vr.snaks)
if not ref:find("Wikipedia") then
if not ref:find("Wiki") then
return true
return true
end
end
Line 581: Line 729:
input_parm = mw.text.trim(input_parm or "")
input_parm = mw.text.trim(input_parm or "")
if input_parm == "" then input_parm = nil end
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 = frame.args
local args = frame.args
Line 592: Line 743:


-- The blacklist is passed in named parameter |suppressfields
-- The blacklist is passed in named parameter |suppressfields
local blacklist = args.suppressfields or args.spf
local blacklist = args.suppressfields or args.spf or ""


-- The whitelist is passed in named parameter |fetchwikidata
-- The whitelist is passed in named parameter |fetchwikidata
local whitelist = args.fetchwikidata or args.fwd
local whitelist = args.fetchwikidata or args.fwd or ""
if not whitelist or whitelist == "" then whitelist = "NONE" end
if whitelist == "" then whitelist = "NONE" end


-- The name of the field that this function is called from is passed in named parameter |name
-- The name of the field that this function is called from is passed in named parameter |name
local fieldname = args.name or ""
local fieldname = args.name or ""


if blacklist then
if blacklist ~= "" then
-- The name is compulsory when blacklist is used, so return nil if it is not supplied
-- The name is compulsory when blacklist is used, so return nil if it is not supplied
if not fieldname or fieldname == "" then return false, nil end
if fieldname == "" then return false, nil end
-- If this field is on the blacklist, then return nil
-- If this field is on the blacklist, then return nil
if blacklist:find(fieldname) then return false, nil end
if blacklist:find(fieldname) then return false, nil end
Line 642: Line 793:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- createicon assembles the "Edit at Wikidata" pen icon.
-- createicon assembles the "Edit at Wikidata" pen icon.
-- It returns a wikitext string.
-- It returns a wikitext string inside a span class="penicon"
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: i18n[];
-- Dependencies: i18n[];
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local createicon = function(langcode, entityID, propertyID)
local createicon = function(langcode, entityID, propertyID)
local icon = "&nbsp;[[" .. i18n["filespace"]
local icon = "&nbsp;<span class='penicon'>[["
icon = icon .. ":Blue pencil.svg |frameless |text-top |10px |alt="
-- "&nbsp;<span data-bridge-edit-flow='overwrite' class='penicon'>[[" -> enable Wikidata Bridge
icon = icon .. i18n["editonwikidata"]
.. i18n["filespace"]
icon = icon .. "|link=https://www.wikidata.org/wiki/" .. entityID
.. ":OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt="
icon = icon .. "?uselang=" .. langcode
.. i18n["editonwikidata"]
.. "|link=https://www.wikidata.org/wiki/" .. entityID
.. "?uselang=" .. langcode
if propertyID then icon = icon .. "#" .. propertyID end
if propertyID then icon = icon .. "#" .. propertyID end
icon = icon .. "|" .. i18n["editonwikidata"] .. "]]"
icon = icon .. "|" .. i18n["editonwikidata"] .. "]]</span>"
return icon
return icon
end
end
Line 698: Line 851:
-- Zero or not a number result in no collapsing (default becomes 0).
-- Zero or not a number result in no collapsing (default becomes 0).
local collapse = tonumber(args.collapse) or 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
-- if there's anything to return, then return a list
Line 705: Line 862:
if #out > 0 then
if #out > 0 then
if sorted then table.sort(out) end
if sorted then table.sort(out) end
-- if a pen icon is wanted add it the end of the last value
-- if there's something to display and a pen icon is wanted, add it the end of the last value
if not noic then
local hasdisplay = false
for i, v in ipairs(out) do
if v ~= i18n.missinginfocat then
hasdisplay = true
break
end
end
if not noic and hasdisplay then
out[#out] = out[#out] .. createicon(args.langobj.code, entityID, propertyID)
out[#out] = out[#out] .. createicon(args.langobj.code, entityID, propertyID)
end
end
Line 722: Line 886:
strout = nil -- no items had valid reference
strout = nil -- no items had valid reference
end
end
if replacetext ~= "" and strout then strout = replacetext end
return strout
return strout
end
end
Line 737: Line 902:
-- monolingualtext
-- monolingualtext
-- It also requires linked, the link/pre/postfixes, uabbr, and the arguments passed from frame.
-- It also requires linked, the link/pre/postfixes, uabbr, and the arguments passed from frame.
-- The optional filter parameter allows quantities to be be filtered by unit.
-- The optional filter parameter allows quantities to be be filtered by unit Qid.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: parseParam(); labelOrId(); i18n[]; dateFormat();
-- Dependencies: parseParam(); labelOrId(); i18n[]; dateFormat();
-- roundto(); decimalPrecision(); decimalToDMS();
-- roundto(); decimalPrecision(); decimalToDMS(); linkedItem();
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local rendersnak = function(propval, args, linked, lpre, lpost, pre, post, uabbr, filter)
local rendersnak = function(propval, args, linked, lpre, lpost, pre, post, uabbr, filter)
Line 747: Line 912:
pre = pre or ""
pre = pre or ""
post = post or ""
post = post or ""
args.lang = args.lang or findLang().code
-- allow values to display a fixed text instead of label
local dtxt = args.displaytext or args.dt
if dtxt == "" then dtxt = nil end
-- switch to use display of short name (P1813) instead of label
local shortname = args.shortname or args.sn
shortname = parseParam(shortname, false)
local snak = propval.mainsnak or propval
local snak = propval.mainsnak or propval
local dtype = snak.datatype
local dv = snak.datavalue
local dv = snak.datavalue
local dtype = propval.datatype or propval.mainsnak.datatype
dv = dv and dv.value
dv = dv and dv.value
-- value and monolingual text language code returned
-- value and monolingual text language code returned
Line 766: Line 938:
local qnumber = dv.id
local qnumber = dv.id
if linked then
if linked then
val = linkedItem(qnumber, lpre, lpost, pre, post)
val = linkedItem(qnumber, lpre, lpost, pre, post, dtxt, shortname, args.lang)
else -- no link wanted so test for lang code
else -- no link wanted so check for display-text, otherwise test for lang code
local label, islabel = labelOrId(qnumber)
local label, islabel
local langlabel = mw.wikibase.getLabelByLang(qnumber, args.lang)
if dtxt then
if langlabel then
label = dtxt
label = mw.text.nowiki( langlabel )
else
label, islabel = labelOrId(qnumber)
local langlabel = mw.wikibase.getLabelByLang(qnumber, args.lang)
if langlabel then
label = mw.text.nowiki( langlabel )
end
end
end
val = pre .. label .. post
val = pre .. label .. post
Line 785: Line 962:
-- qualifiers (if any) is a nested table or nil
-- qualifiers (if any) is a nested table or nil
-- lang is given, or user language, or site language
-- lang is given, or user language, or site language
val = dateFormat(dv.time, dv.precision, args.df, args.bc, args.pd, propval.qualifiers, args.lang)
--
-- Here we can check whether args.df has a value
-- If not, use code from Module:Sandbox/RexxS/Getdateformat to set it from templates like {{Use mdy dates}}
val = dateFormat(dv.time, dv.precision, args.df, args.bc, args.pd, propval.qualifiers, args.lang, "", dv.calendarmodel)
------------------------------------
------------------------------------
-- data types which are strings:
-- data types which are strings:
Line 794: Line 974:
-- don't link if no linkpre/postfix or linkprefix is just ":"
-- don't link if no linkpre/postfix or linkprefix is just ":"
val = pre .. dv .. post
val = pre .. dv .. post
elseif dtype == "external-id" then
val = "[" .. lpre .. dv .. lpost .. " " .. pre .. dv .. post .. "]"
else
else
val = "[[" .. lpre .. dv .. lpost .. "|" .. pre .. dv .. post .. "]]"
val = "[[" .. lpre .. dv .. lpost .. "|" .. pre .. dv .. post .. "]]"
Line 810: Line 992:
-- if we have conversions, we won't have formatted numbers or scales
-- if we have conversions, we won't have formatted numbers or scales
if conv then
if conv then
uabbr = true
fnum = false
fnum = false
args.scale = "0"
args.scale = "0"
end
end
--
-- a switch to turn on/off showing units, default is true
local showunits = parseParam(args.su or args.showunits, true)
--
--
-- convert amount to a number
-- convert amount to a number
Line 887: Line 1,073:
local unitqid = string.match( dv.unit, "(Q%d+)" )
local unitqid = string.match( dv.unit, "(Q%d+)" )
if filter and unitqid ~= filter then return nil end
if filter and unitqid ~= filter then return nil end
if unitqid then
if unitqid and showunits then
local uname = mw.wikibase.getLabelByLang(unitqid, args.lang) or ""
local uname = mw.wikibase.getLabelByLang(unitqid, args.lang) or ""
if uname ~= "" then usep, unit = " ", uname end
if uname ~= "" then usep, unit = " ", uname end
if uabbr then
if uabbr then
-- see if there's a unit symbol (P5061)
-- see if there's a unit symbol (P5061)
local unitsymbols = mw.wikibase.getAllStatements(unitqid, "P5061")
local unitsymbols = mw.wikibase.getBestStatements(unitqid, "P5061")
-- construct fallback table
-- construct fallback table, add local lang and multiple languages
local fbtbl = mw.language.getFallbacksFor( args.lang )
local fbtbl = mw.language.getFallbacksFor( args.lang )
table.insert( fbtbl, 1, args.lang )
table.insert( fbtbl, 1, args.lang )
table.insert( fbtbl, 1, "mul" )
local found = false
local found = false
for idx1, us in ipairs(unitsymbols) do
for idx1, us in ipairs(unitsymbols) do
Line 911: Line 1,098:
end
end
-- format display:
-- format display:
if conv and usym ~= "" then
if conv then
if range == "" then
if range == "" then
val = mw.getCurrentFrame():expandTemplate{title = "cvt", args = {amount, usym}}
val = mw.getCurrentFrame():expandTemplate{title = "cvt", args = {amount, unit}}
else
else
val = mw.getCurrentFrame():expandTemplate{title = "cvt", args = {lowb, "to", upb, usym}}
val = mw.getCurrentFrame():expandTemplate{title = "cvt", args = {lowb, "to", upb, unit}}
end
end
elseif unit == "$" or unit == "£" then
elseif unit == "$" or unit == "£" then
Line 932: Line 1,119:
-- default is deg/min/sec -- decimal degrees needs |format = dec
-- default is deg/min/sec -- decimal degrees needs |format = dec
local form = (args.format or ""):lower():sub(1,3)
local form = (args.format or ""):lower():sub(1,3)
if form ~= "dec" then form = "dms" end
if form ~= "dec" then form = "dms" end -- not needed for now
--
--
-- show parameter allows just the latitude or longitude to be shown
-- show parameter allows just the latitude, or just the longitude, or both
-- to be returned as a signed decimal, ignoring the format parameter.
local show = (args.show or ""):lower()
local show = (args.show or ""):lower()
if show ~= "longlat" then show = show:sub(1,3) end
if show ~= "longlat" then show = show:sub(1,3) end
Line 1,058: Line 1,246:
args.pd = pd
args.pd = pd


local lang = args.lang
-- allow qualifiers to have a different date format; default to year
args.qdf = args.qdf or args.qualifierdateformat or args.df or "y"
 
local lang = args.lang or findlang().code
 
    -- qualID is a string list of wanted qualifiers or "ALL"
    qualID = qualID or ""
    -- capitalise list of wanted qualifiers and substitute "DATES"
    qualID = qualID:upper():gsub("DATES", "P580, P582")
    local allflag = (qualID == "ALL")
    -- create table of wanted qualifiers as key
    local qwanted = {}
    -- create sequence of wanted qualifiers
    local qorder = {}
    for q in mw.text.gsplit(qualID, "%p") do -- split at punctuation and iterate
        local qtrim = mw.text.trim(q)
        if qtrim ~= "" then
            qwanted[mw.text.trim(q)] = true
            qorder[#qorder+1] = qtrim
        end
    end
    -- qsep is the output separator for rendering qualifier list
    local qsep = (args.qsep or ""):gsub('"', '')
    -- qargs are the arguments to supply to assembleoutput()
    local qargs = {
        ["osd"]        = "false",
        ["linked"]      = tostring(linked),
        ["prefix"]      = args.qprefix,
        ["postfix"]    = args.qpostfix,
        ["linkprefix"]  = args.qlinkprefix or args.qlp,
        ["linkpostfix"] = args.qlinkpostfix,
        ["wdl"]        = "false",
        ["unitabbr"]    = tostring(uabbr),
        ["maxvals"]    = 0,
        ["sorted"]      = tostring(args.qsorted),
        ["noicon"]      = "true",
        ["list"]        = args.qlist,
        ["sep"]        = qsep,
        ["langobj"]    = args.langobj,
        ["lang"]        = args.langobj.code,
        ["df"]          = args.qdf,
        ["sn"]          = parseParam(args.qsn or args.qshortname, false),
    }
 
-- all proper values of a Wikidata property will be the same type as the first
-- all proper values of a Wikidata property will be the same type as the first
-- qualifiers don't have a mainsnak, properties do
-- qualifiers don't have a mainsnak, properties do
local datatype = objproperty[1].datatype or objproperty[1].mainsnak.datatype
local datatype = objproperty[1].datatype or objproperty[1].mainsnak.datatype
-- out holds the values for this property
 
-- mlt holds the language code if the datatype is monolingual text
-- out[] holds the a list of returned values for this property
-- mlt[] holds the language code if the datatype is monolingual text
local out = {}
local out = {}
local mlt = {}
local mlt = {}
for k, v in pairs(objproperty) do
 
if (onlysrc and not sourced(v)) or (qualsonly and qualID) then
for k, v in ipairs(objproperty) do
local hasvalue = true
if (onlysrc and not sourced(v)) then
-- no value: it isn't sourced when onlysourced=true
-- no value: it isn't sourced when onlysourced=true
-- or only qualifiers are requested
hasvalue = false
else
else
out[#out+1], mlt[#out+1] = rendersnak(v, args, linked, lprefix, lpostfix, prefix, postfix, uabbr)
local val, lcode = rendersnak(v, args, linked, lprefix, lpostfix, prefix, postfix, uabbr)
if not val then
hasvalue = false -- rank doesn't match
elseif qualsonly and qualID then
-- suppress value returned: only qualifiers are requested
else
out[#out+1], mlt[#out+1] = val, lcode
end
end
end


-- See if qualifiers are to be returned:
-- See if qualifiers are to be returned:
local snak = v.mainsnak or v
local snak = v.mainsnak or v
if v.qualifiers and qualID and snak.snaktype=="value" then
if hasvalue and v.qualifiers and qualID ~= "" and snak.snaktype~="novalue" then
local qsep = (args.qsep or ""):gsub('"', '')
            -- collect all wanted qualifier values returned in qlist, indexed by propertyID
local qargs = {
["osd"]        = "false",
["linked"]      = tostring(linked),
["prefix"]      = args.qprefix,
["postfix"]    = args.qpostfix,
["linkprefix"]  = args.qlinkprefix or args.qlp,
["linkpostfix"] = args.qlinkpostfix,
["wdl"]        = "false",
["unitabbr"]    = tostring(uabbr),
["maxvals"]    = 0,
["sorted"]      = args.qsorted,
["noicon"]      = "true",
["list"]        = args.qlist,
["sep"]        = qsep,
["langobj"]    = args.langobj,
["lang"]        = args.langobj.code,
}
local qlist = {}
local qlist = {}
local t1, t2 = "", ""
local timestart, timeend = "", ""
-- see if we want all qualifiers
            -- loop through qualifiers
if qualID == "ALL" then
            for k1, v1 in pairs(v.qualifiers) do
if v["qualifiers-order"] then
                if allflag or qwanted[k1] then
-- the values in the order table are the keys for the qualifiers table:
                    if k1 == "P1326" then
for k1, v1 in ipairs(v["qualifiers-order"]) do
                        local ts = v1[1].datavalue.value.time
if v1 == "P1326" then
                        local dp = v1[1].datavalue.value.precision
local ts = v.qualifiers[v1][1].datavalue.value.time
                        qlist[k1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "before")
local dp = v.qualifiers[v1][1].datavalue.value.precision
                    elseif k1 == "P1319" then
qlist[#qlist + 1] = dateFormat(ts, dp, args.df, args.bc, pd, "", lang, "before")
                        local ts = v1[1].datavalue.value.time
else
                        local dp = v1[1].datavalue.value.precision
qlist[#qlist + 1] = assembleoutput(propertyvalueandquals(v.qualifiers[v1], qargs), qargs)
                        qlist[k1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "after")
end
                    elseif k1 == "P580" then
end
                        timestart = propertyvalueandquals(v1, qargs)[1] or "" -- treat only one start time as valid
else
                    elseif k1 == "P582" then
local ql = propertyvalueandquals(v.qualifiers, qargs)
                        timeend = propertyvalueandquals(v1, qargs)[1] or "" -- treat only one end time as valid
for k1, v1 in ipairs(ql) do
                    else
if k1 == "P1326" then
                        local q = assembleoutput(propertyvalueandquals(v1, qargs), qargs)
local ts = v1[1].datavalue.value.time
                        -- we already deal with circa via 'sourcing circumstances' if the datatype was time
local dp = v1[1].datavalue.value.precision
                        -- circa may be either linked or unlinked *** internationalise later ***
qlist[#qlist + 1] = dateFormat(ts, dp, args.df, args.bc, pd, "", lang, "before")
                        if datatype ~= "time" or q ~= "circa" and not (type(q) == "string" and q:find("circa]]")) then
else
                            qlist[k1] = q
qlist[#qlist + 1] = v1
                        end
end
                    end
end
                end -- of test for wanted
end
            end -- of loop through qualifiers
-- see if we want date/range
            -- set date separator
elseif qualID == "DATES" then
local t = timestart .. timeend
qargs.maxvals = 1
-- *** internationalise date separators later ***
for k1, v1 in pairs(v.qualifiers) do
if k1 == "P580" then -- P580 is "start time"
t1 = propertyvalueandquals(v1, qargs)[1] or ""
elseif k1 == "P582" then -- P582 is "end time"
t2 = propertyvalueandquals(v1, qargs)[1] or ""
end
end
-- otherwise process qualID as a list of qualifiers
else
for q in mw.text.gsplit(qualID, "%p") do -- split at punctuation and iterate
q = mw.text.trim(q):upper() -- remove whitespace and capitalise
if q == "P1326" then
-- latest date, so supply 'before' as well. Assume one date value.
for k1, v1 in pairs(v.qualifiers) do
if k1 == "P1326" then
local ts = v1[1].datavalue.value.time
local dp = v1[1].datavalue.value.precision
qlist[#qlist + 1] = dateFormat(ts, dp, args.df, args.bc, pd, "", lang, "before")
end
end
else
for k1, v1 in pairs(v.qualifiers) do
if k1 == q then
local ql = propertyvalueandquals(v1, qargs)
for k2, v2 in ipairs(ql) do
qlist[#qlist + 1] = v2
end
end
end
end
end -- of loop through list of qualifiers in qualID
end -- of testing for what qualID is
local t = t1 .. t2
-- *** internationalise date separators later ***
local dsep = "&ndash;"
local dsep = "&ndash;"
if t:find("%s") or t:find("&nbsp;") then dsep = " &ndash; " end
if t:find("%s") or t:find("&nbsp;") then dsep = " &ndash; " end
if #qlist > 0 then
            -- set the order for the list of qualifiers returned; start time and end time go last
local qstr = assembleoutput(qlist, qargs)
if next(qlist) then
                local qlistout = {}
                if allflag then
                    for k2, v2 in pairs(qlist) do
                        qlistout[#qlistout+1] = v2
                    end
                else
                    for i2, v2 in ipairs(qorder) do
                        qlistout[#qlistout+1] = qlist[v2]
                    end
                end
                if t ~= "" then
                    qlistout[#qlistout+1] = timestart .. dsep .. timeend
                end
local qstr = assembleoutput(qlistout, qargs)
if qualsonly then
if qualsonly then
out[#out+1] = qstr
out[#out+1] = qstr
Line 1,168: Line 1,372:
out[#out] = out[#out] .. " (" .. qstr .. ")"
out[#out] = out[#out] .. " (" .. qstr .. ")"
end
end
elseif t > "" then
elseif t ~= "" then
if qualsonly then
if qualsonly then
out[#out+1] = t1 .. dsep .. t2
out[#out+1] = timestart .. dsep .. timeend
else
else
out[#out] = out[#out] .. " (" .. t1 .. dsep .. t2 .. ")"
out[#out] = out[#out] .. " (" .. timestart .. dsep .. timeend .. ")"
end
end
end
end
Line 1,184: Line 1,388:
-- otherwise look through the fallback languages for a match
-- otherwise look through the fallback languages for a match
if datatype == "monolingualtext" and #out >1 then
if datatype == "monolingualtext" and #out >1 then
local langcode = args.langobj.code
lang = mw.text.split( lang, '-', true )[1]
langcode = mw.text.split( langcode, '-', true )[1]
local fbtbl = mw.language.getFallbacksFor( lang )
local fbtbl = mw.language.getFallbacksFor( langcode )
table.insert( fbtbl, 1, lang )
table.insert( fbtbl, 1, langcode )
local bestval = ""
local bestval = ""
local found = false
local found = false
Line 1,237: Line 1,440:
frame.args.langobj = findLang(frame.args.lang)
frame.args.langobj = findLang(frame.args.lang)
frame.args.lang = frame.args.langobj.code
frame.args.lang = frame.args.langobj.code
local args = frame.args


-- check for locally supplied parameter in second unnamed parameter
-- check for locally supplied parameter in second unnamed parameter
-- success means no local parameter and the property exists
-- success means no local parameter and the property exists
local qid, props = parseInput(frame, frame.args[2], propertyID)
local qid, props = parseInput(frame, args[2], propertyID)
 
local linked = parseParam(args.linked, true)
local lpre = (args.linkprefix or args.lp or ""):gsub('"', '')
local lpost = (args.linkpostfix or ""):gsub('"', '')
local pre = (args.prefix or ""):gsub('"', '')
local post = (args.postfix or ""):gsub('"', '')
local uabbr = parseParam(args.unitabbr or args.uabbr, false)
local filter = (args.unit or ""):upper()
if filter == "" then filter = nil end


if qid then
if qid then
Line 1,248: Line 1,462:
-- with a qualifier like "language of work or name (P407)" in qualID
-- with a qualifier like "language of work or name (P407)" in qualID
-- whose value has the required ID, like "British English (Q7979)", in qval
-- whose value has the required ID, like "British English (Q7979)", in qval
for k1, v1 in pairs(props) do
for k1, v1 in ipairs(props) do
if v1.mainsnak.snaktype == "value" and v1.mainsnak.datavalue.type == "string" then
if v1.mainsnak.snaktype == "value" then
-- We'll only deal with returning strings for now
-- check if it has the right qualifier
-- so check if it has the right qualifier
local v1q = v1.qualifiers
local v1q = v1.qualifiers
if v1q and v1q[qualID] then
if v1q and v1q[qualID] then
Line 1,257: Line 1,470:
-- if we've got this far, we have a (sourced) claim with qualifiers
-- if we've got this far, we have a (sourced) claim with qualifiers
-- so see if matches the required value
-- so see if matches the required value
-- We'll only deal with wikibase-items for now
-- We'll only deal with wikibase-items and strings for now
if v1q[qualID][1].datatype == "wikibase-item" then
if v1q[qualID][1].datatype == "wikibase-item" then
if checkvalue(v1q[qualID][1].datavalue.value.id) then
if checkvalue(v1q[qualID][1].datavalue.value.id) then
out[#out + 1] = v1.mainsnak.datavalue.value
out[#out + 1] = rendersnak(v1, args, linked, lpre, lpost, pre, post, uabbr, filter)
end
elseif v1q[qualID][1].datatype == "string" then
if checkvalue(v1q[qualID][1].datavalue.value) then
out[#out + 1] = rendersnak(v1, args, linked, lpre, lpost, pre, post, uabbr, filter)
end
end
end
end
Line 1,278: Line 1,495:


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- _location takes Q-id and follows P131 (located in the administrative territorial entity)
-- _location takes Q-id and follows P276 (location)
-- or P276 (location) or P706 (located on terrain feature)
-- or P131 (located in the administrative territorial entity) or P706 (located on terrain feature)
-- from the initial item to higher level territories/locations until it reaches the highest.
-- from the initial item to higher level territories/locations until it reaches the highest.
-- An optional boolean, 'first', determines whether the first item is returned (default: false).
-- An optional boolean, 'first', determines whether the first item is returned (default: false).
Line 1,290: Line 1,507:
first = parseParam(first, false)
first = parseParam(first, false)
skip = parseParam(skip, false)
skip = parseParam(skip, false)
local locs = {"P276", "P131", "P706"}
local out = {}
local out = {}
local langcode = findLang():getCode()
local langcode = findLang():getCode()
local finished = false
local finished = false
local count = 0
local count = 0
local prevqid = "Q0"
repeat
repeat
local prop = mw.wikibase.getBestStatements(qid, "P131")[1] or mw.wikibase.getBestStatements(qid, "P276")[1] or mw.wikibase.getBestStatements(qid, "P706")[1]
local prop
for i1, v1 in ipairs(locs) do
local proptbl = mw.wikibase.getBestStatements(qid, v1)
if #proptbl > 1 then
-- there is more than one higher location
local prevP131, prevP131id
if prevqid ~= "Q0" then
prevP131 = mw.wikibase.getBestStatements(prevqid, "P131")[1]
prevP131id = prevP131
and prevP131.mainsnak.datavalue
and prevP131.mainsnak.datavalue.value.id
end
for i2, v2 in ipairs(proptbl) do
parttbl = v2.qualifiers and v2.qualifiers.P518
if parttbl then
-- this higher location has qualifier 'applies to part' (P518)
for i3, v3 in ipairs(parttbl) do
if v3.snaktype == "value" and v3.datavalue.value.id == prevqid then
-- it has a value equal to the previous location
prop = proptbl[i2]
break
end -- of test for matching last location
end -- of loop through values of 'applies to part'
else
-- there's no qualifier 'applies to part' (P518)
-- so check if the previous location had a P131 that matches this alternate
if qid == prevP131id then
prop = proptbl[i2]
break
end -- of test for matching previous P131
end
end -- of loop through parent locations
-- fallback to second value if match not found
prop = prop or proptbl[2]
elseif #proptbl > 0 then
prop = proptbl[1]
end
if prop then break end
end
 
-- check if it's an instance of (P31) a country (Q6256) and terminate the chain if it is
-- check if it's an instance of (P31) a country (Q6256) and terminate the chain if it is
local inst = mw.wikibase.getAllStatements(qid, "P31")
local inst = mw.wikibase.getAllStatements(qid, "P31")
Line 1,308: Line 1,566:
end
end
end
end
-- get the name of this location and update qid to point to the parent location
if prop and prop.mainsnak.datavalue then
if prop and prop.mainsnak.datavalue then
if not skip or count == 0 then
if not skip or count == 0 then
out[#out+1] = linkedItem(qid, ":", "", "", "") -- get a linked value if we can
out[#out+1] = linkedItem(qid, ":", "", "", "") -- get a linked value if we can
end
end
qid = prop.mainsnak.datavalue.value.id
qid, prevqid = prop.mainsnak.datavalue.value.id, qid
else
else
-- This is top-level location, so get short name except when this is the first item
-- This is top-level location, so get short name except when this is the first item
-- Use full label if there's no short name or this is the first item
-- Use full label if there's no short name or this is the first item
local prop1813 = mw.wikibase.getAllStatements(qid, "P1813")
local prop1813 = mw.wikibase.getAllStatements(qid, "P1813")
Line 1,351: Line 1,611:
count = count + 1
count = count + 1
until finished or count >= 10 -- limit to 10 levels to avoid infinite loops
until finished or count >= 10 -- limit to 10 levels to avoid infinite loops
-- remove the first location if not required
if not first then table.remove(out, 1) end
if not first then table.remove(out, 1) end
-- we might have duplicate text for consecutive locations, so remove them
if #out > 2 then
local plain = {}
for i, v in ipairs(out) do
-- strip any links
plain[i] = v:gsub("^%[%[[^|]*|", ""):gsub("]]$", "")
end
local idx = 2
repeat
if plain[idx] == plain[idx-1] then
-- duplicate found
local removeidx = 0
if (plain[idx] ~= out[idx]) and (plain[idx-1] == out[idx-1]) then
-- only second one is linked, so drop the first
removeidx = idx - 1
elseif (plain[idx] == out[idx]) and (plain[idx-1] ~= out[idx-1]) then
-- only first one is linked, so drop the second
removeidx = idx
else
-- pick one
removeidx = idx - (os.time()%2)
end
table.remove(out, removeidx)
table.remove(plain, removeidx)
else
idx = idx +1
end
until idx >= #out
end
return out
return out
end
-------------------------------------------------------------------------------
-- _getsumofparts scans the property 'has part' (P527) for values matching a list.
-- The list (args.vlist) consists of a string of Qids separated by spaces or any usual punctuation.
-- If the matched values have a qualifer 'quantity' (P1114), those quantites are summed.
-- The sum is returned as a number (i.e. 0 if none)
-- a table of arguments is supplied implementing the usual parameters.
-------------------------------------------------------------------------------
-- Dependencies: setRanks; parseParam; parseInput; sourced; assembleoutput;
-------------------------------------------------------------------------------
local _getsumofparts = function(args)
local vallist = (args.vlist or ""):upper()
if vallist == "" then return end
args.reqranks = setRanks(args.rank)
local f = {}
f.args = args
local qid, props = parseInput(f, "", "P527")
if not qid then return 0 end
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local sum = 0
for k1, v1 in ipairs(props) do
if (onlysrc == false or sourced(v1))
and v1.mainsnak.snaktype == "value"
and v1.mainsnak.datavalue.type == "wikibase-entityid"
and vallist:match( v1.mainsnak.datavalue.value.id )
and v1.qualifiers
then
local quals = v1.qualifiers["P1114"]
if quals then
for k2, v2 in ipairs(quals) do
sum = sum + v2.datavalue.value.amount
end
end
end
end
return sum
end
end


Line 1,361: Line 1,691:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getValue is used to get the value(s) of a property
-- _getValue makes the functionality of getValue available to other modules
-- The property ID is passed as the first unnamed parameter and is required.
-- A locally supplied parameter may optionaly be supplied as the second unnamed parameter.
-- The function will now also return qualifiers if parameter qual is supplied
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: setRanks; parseInput; propertyvalueandquals; assembleoutput; parseParam; sourced;  
-- Dependencies: setRanks; parseInput; propertyvalueandquals; assembleoutput; parseParam; sourced;
-- labelOrId; i18n.latestdatequalifier; format_Date; makeOrdinal; roundto; decimalPrecision; decimalToDMS;
-- labelOrId; i18n.latestdatequalifier; format_Date; makeOrdinal; roundto; decimalPrecision; decimalToDMS;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getValue = function(frame)
p._getValue = function(args)
if not frame.args[1] then
frame.args = frame:getParent().args
if not frame.args[1] then return i18n.errors["No property supplied"] end
end
 
-- parameter sets for commonly used groups of parameters
-- parameter sets for commonly used groups of parameters
local paraset = tonumber(frame.args.ps or frame.args.parameterset or 0)
local paraset = tonumber(args.ps or args.parameterset or 0)
if paraset == 1 then
if paraset == 1 then
frame.args.rank = "best"
-- a common setting
frame.args.fetchwikidata = "ALL"
args.rank = "best"
frame.args.onlysourced = "no"
args.fetchwikidata = "ALL"
frame.args.noicon = "true"
args.onlysourced = "no"
args.noicon = "true"
elseif paraset == 2 then
elseif paraset == 2 then
-- second set goes here
-- equivalent to raw
args.rank = "best"
args.fetchwikidata = "ALL"
args.onlysourced = "no"
args.noicon = "true"
args.linked = "no"
args.pd = "true"
elseif paraset == 3 then
-- third set goes here
end
end


local propertyID = mw.text.trim(frame.args[1] or "")
-- implement eid parameter
local eid = args.eid
if eid == "" then
return nil
elseif eid then
args.qid = eid
end


frame.args.reqranks = setRanks(frame.args.rank)
local propertyID = mw.text.trim(args[1] or "")


local entityid, props = parseInput(frame, frame.args[2], propertyID)
args.reqranks = setRanks(args.rank)
 
-- replacetext (rt) is a string that is returned instead of any non-empty Wikidata value
-- this is useful for tracking and debugging, so we set fetchwikidata=ALL to fill the whitelist
local replacetext = mw.text.trim(args.rt or args.replacetext or "")
if replacetext ~= "" then
args.fetchwikidata = "ALL"
end
 
local f = {}
f.args = args
local entityid, props = parseInput(f, f.args[2], propertyID)


if not entityid then
if not entityid then
return props -- either the input parameter or nothing
return props -- either the input parameter or nothing
end
end
-- qual is a string containing the property ID of the qualifier(s) to be returned
-- qual is a string containing the property ID of the qualifier(s) to be returned
-- if qual == "ALL" then all qualifiers returned
-- if qual == "ALL" then all qualifiers returned
-- if qual == "DATES" then qualifiers P580 (start time) and P582 (end time) returned
-- if qual == "DATES" then qualifiers P580 (start time) and P582 (end time) returned
-- if nothing or an empty string is passed set it nil -> no qualifiers returned
-- if nothing or an empty string is passed set it nil -> no qualifiers returned
local qualID = mw.text.trim(frame.args.qual or ""):upper()
local qualID = mw.text.trim(args.qual or ""):upper()
if qualID == "" then qualID = nil end
if qualID == "" then qualID = nil end


-- set a language object and code in the frame.args table
-- set a language object and code in the args table
frame.args.langobj = findLang(frame.args.lang)
args.langobj = findLang(args.lang)
frame.args.lang = frame.args.langobj.code
args.lang = args.langobj.code


-- table 'out' stores the return value(s):
-- table 'out' stores the return value(s):
local out = propertyvalueandquals(props, frame.args, qualID)
local out = propertyvalueandquals(props, args, qualID)


-- format the table of values and return it as a string:
-- format the table of values and return it as a string:
return assembleoutput(out, frame.args, entityid, propertyID)
return assembleoutput(out, args, entityid, propertyID)
end
 
 
-------------------------------------------------------------------------------
-- getValue is used to get the value(s) of a property
-- The property ID is passed as the first unnamed parameter and is required.
-- A locally supplied parameter may optionaly be supplied as the second unnamed parameter.
-- The function will now also return qualifiers if parameter qual is supplied
-------------------------------------------------------------------------------
-- Dependencies: _getValue; setRanks; parseInput; propertyvalueandquals; assembleoutput; parseParam; sourced;
-- labelOrId; i18n.latestdatequalifier; format_Date; makeOrdinal; roundto; decimalPrecision; decimalToDMS;
-------------------------------------------------------------------------------
p.getValue = function(frame)
local args= frame.args
if not args[1] then
args = frame:getParent().args
if not args[1] then return i18n.errors["No property supplied"] end
end
 
return p._getValue(args)
end
end


Line 1,509: Line 1,878:
local onlysrc = parseParam(frame.args.onlysourced or frame.args.osd, true)
local onlysrc = parseParam(frame.args.onlysourced or frame.args.osd, true)


-- set a language object in the frame.args table
-- set a language object and language code in the frame.args table
frame.args.langobj = findLang(frame.args.lang)
frame.args.langobj = findLang(frame.args.lang)
frame.args.lang = frame.args.langobj.code


-- set the requested ranks flags
-- set the requested ranks flags
Line 1,538: Line 1,908:
qargs.onlysourced = "no"
qargs.onlysourced = "no"
local vals = propertyvalueandquals(quals, qargs, qid)
local vals = propertyvalueandquals(quals, qargs, qid)
for k, v in pairs(vals) do
for k, v in ipairs(vals) do
out[#out + 1] = v
out[#out + 1] = v
end
end
Line 1,551: Line 1,921:
end -- of test for success
end -- of test for success
return nil
return nil
end
-------------------------------------------------------------------------------
-- getSumOfParts scans the property 'has part' (P527) for values matching a list.
-- The list is passed in parameter vlist.
-- It consists of a string of Qids separated by spaces or any usual punctuation.
-- If the matched values have a qualifier 'quantity' (P1114), those quantities are summed.
-- The sum is returned as a number or nothing if zero.
-------------------------------------------------------------------------------
-- Dependencies: _getsumofparts;
-------------------------------------------------------------------------------
p.getSumOfParts = function(frame)
local sum = _getsumofparts(frame.args)
if sum == 0 then return end
return sum
end
end


Line 1,559: Line 1,945:
-- a property ID (the unnamed parameter or 1=Pxxx)
-- a property ID (the unnamed parameter or 1=Pxxx)
-- the ID of a qualifier for that property (qualID=Pyyy)
-- the ID of a qualifier for that property (qualID=Pyyy)
-- the Wikibase-entity ID of a value for that qualifier (qvalue=Qzzz)
-- either the Wikibase-entity ID of a value for that qualifier (qvalue=Qzzz)
-- or a string value for that qualifier (qvalue=abc123)
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Line 1,589: Line 1,976:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getValueByLang = function(frame)
p.getValueByLang = function(frame)
-- The language code for the qualifier we want to match is in named parameter |lang=
-- The language code for the qualifier we want to match is in named parameter |lang=
local langcode = frame.args.lang or ""
local langcode = findLang(frame.args.lang).code
if langcode == "" then
langcode = frame:callParserFunction{ name = "int", args = "lang" }
end
local function checkLanguage(id)
local function checkLanguage(id)
-- id should represent a language like "British English (Q7979)"
-- id should represent a language like "British English (Q7979)"
Line 1,610: Line 1,993:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getValueByRefSource gets the value of a property which has a reference "stated in" (P248)
-- getValueByRefSource gets the value of a property which has a reference "stated in" (P248)
-- whose value has the given entity code.
-- whose value has the given entity-ID.
-- The call needs:
-- The call needs:
-- a property ID (the unnamed parameter or 1=Pxxx)
-- a property ID (the unnamed parameter or 1=Pxxx)
Line 1,649: Line 2,032:
local out = {}
local out = {}
local mlt= {}
local mlt= {}
for k1, v1 in pairs(props) do
for k1, v1 in ipairs(props) do
if onlysrc == false or sourced(v1) then
if onlysrc == false or sourced(v1) then
if v1.references then
if v1.references then
for k2, v2 in pairs(v1.references) do
for k2, v2 in ipairs(v1.references) do
if v2.snaks.P248 then
if v2.snaks.P248 then
for k3, v3 in pairs(v2.snaks.P248) do
for k3, v3 in ipairs(v2.snaks.P248) do
if v3.datavalue.value.id == qval then
if v3.datavalue.value.id == qval then
out[#out+1], mlt[#out+1] = rendersnak(v1, frame.args, linked, "", "", "", "", uabbr, unit)
out[#out+1], mlt[#out+1] = rendersnak(v1, frame.args, linked, "", "", "", "", uabbr, unit)
Line 1,702: Line 2,085:


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getGlobe takes an optional qid of a Wikidata entity passed as |qid=
-- getPropertyIDs takes most of the usual parameters.
-- otherwise it uses the linked item for the current page.
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented.
-- If returns the Qid of the globe used in P625 (coordinate location),
-- It returns the Entity-IDs (Qids) of the values of a property if it is a Wikibase-Entity.
-- or nil if there isn't one.
-- Otherwise it returns nothing.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getGlobe = function(frame)
p.getPropertyIDs = function(frame)
local qid = frame.args.qid or frame.args[1] or ""
local args = frame.args
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
args.reqranks = setRanks(args.rank)
local coords = mw.wikibase.getBestStatements(qid, "P625")[1]
args.langobj = findLang(args.lang)
local globeid
args.lang = args.langobj.code
if coords and coords.mainsnak.snaktype == "value" then
-- change default for noicon to true
globeid = coords.mainsnak.datavalue.value.globe:match("(Q%d+)")
args.noicon = tostring(parseParam(args.noicon or "", true))
local f = {}
f.args = args
local pid = mw.text.trim(args[1] or ""):upper()
 
-- get the qid and table of claims for the property, or nothing and the local value passed
local qid, props = parseInput(f, args[2], pid)
if not qid then return props end
if not props[1] then return nil end
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local maxvals = tonumber(args.maxvals) or 0
 
out = {}
for i, v in ipairs(props) do
local snak = v.mainsnak
if ( snak.datatype == "wikibase-item" )
and ( v.rank and args.reqranks[v.rank:sub(1, 1)] )
and ( snak.snaktype == "value" )
and ( sourced(v) or not onlysrc )
then
out[#out+1] = snak.datavalue.value.id
end
if maxvals > 0 and #out >= maxvals then break end
end
end
return globeid
 
return assembleoutput(out, args, qid, pid)
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getCommonsLink takes an optional qid of a Wikidata entity passed as |qid=
-- getQualifierIDs takes most of the usual parameters.
-- It returns one of the following in order of preference:
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented.
-- the Commons sitelink of the linked Wikidata item;
-- It takes a property-id as the first unnamed parameter, and an optional parameter qlist
-- the Commons sitelink of the topic's main category of the linked Wikidata item;
-- which is a list of qualifier property-ids to search for (default is "ALL")
-- It returns the Entity-IDs (Qids) of the values of a property if it is a Wikibase-Entity.
-- Otherwise it returns nothing.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: _getCommonslink(); _getSitelink(); parseParam()
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getCommonsLink = function(frame)
p.getQualifierIDs = function(frame)
local oc = frame.args.onlycat or frame.args.onlycategories
local args = frame.args
local fb = parseParam(frame.args.fallback or frame.args.fb, true)
args.reqranks = setRanks(args.rank)
return _getCommonslink(frame.args.qid, oc, fb)
args.langobj = findLang(args.lang)
end
args.lang = args.langobj.code
-- change default for noicon to true
args.noicon = tostring(parseParam(args.noicon or "", true))
local f = {}
f.args = args
local pid = mw.text.trim(args[1] or ""):upper()
 
-- get the qid and table of claims for the property, or nothing and the local value passed
local qid, props = parseInput(f, args[2], pid)
if not qid then return props end
if not props[1] then return nil end
-- get the other parameters
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local maxvals = tonumber(args.maxvals) or 0
local qlist = args.qlist or ""
if qlist == "" then qlist = "ALL" end
qlist = qlist:gsub("[%p%s]+", " ") .. " "


out = {}
for i, v in ipairs(props) do
local snak = v.mainsnak
if ( v.rank and args.reqranks[v.rank:sub(1, 1)] )
and ( snak.snaktype == "value" )
and ( sourced(v) or not onlysrc )
then
if v.qualifiers then
for k1, v1 in pairs(v.qualifiers) do
if qlist == "ALL " or qlist:match(k1 .. " ") then
for i2, v2 in ipairs(v1) do
if v2.datatype == "wikibase-item" and v2.snaktype == "value" then
out[#out+1] = v2.datavalue.value.id
end -- of test that id exists
end -- of loop through qualifier values
end -- of test for kq in qlist
end -- of loop through qualifiers
end -- of test for qualifiers
end -- of test for rank value, sourced, and value exists
if maxvals > 0 and #out >= maxvals then break end
end -- of loop through property values


-------------------------------------------------------------------------------
return assembleoutput(out, args, qid, pid)
-- getSitelink takes the qid of a Wikidata entity passed as |qid=
-- It takes an optional parameter |wiki= to determine which wiki is to be checked for a sitelink
-- If the parameter is blank, then it uses the local wiki.
-- If there is a sitelink to an article available, it returns the plain text link to the article
-- If there is no sitelink, it returns nil.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.getSiteLink = function(frame)
return _getSitelink(frame.args.qid, frame.args.wiki or mw.text.trim(frame.args[1] or ""))
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getLink has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- getPropOfProp takes two propertyIDs: prop1 and prop2 (as well as the usual parameters)
-- If there is a sitelink to an article on the local Wiki, it returns a link to the article
-- If the value(s) of prop1 are of type "wikibase-item" then it returns the value(s) of prop2
-- with the Wikidata label as the displayed text.
-- of each of those wikibase-items.
-- If there is no sitelink, it returns the label as plain text.
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-- If there is no label in the local language, it displays the qid instead.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getLink = function(frame)
p.getPropOfProp = function(frame)
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
frame.args.reqranks = setRanks(frame.args.rank)
if itemID == "" then return end
frame.args.langobj = findLang(frame.args.lang)
local sitelink = mw.wikibase.sitelink(itemID)
frame.args.lang = frame.args.langobj.code
local label = labelOrId(itemID)
local args = frame.args
if sitelink then
local pid1 = args.prop1 or args.pid1 or ""
return "[[:" .. sitelink .. "|" .. label .. "]]"
local pid2 = args.prop2 or args.pid2 or ""
else
local localval = mw.text.trim(args[1] or "")
return label
if pid1 == "" or pid2 == "" then return nil end
end
local qid1, statements1 = parseInput(frame, localval, pid1)
if not qid1 then return localval end
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local maxvals = tonumber(args.maxvals) or 0
local qualID = mw.text.trim(args.qual or ""):upper()
if qualID == "" then qualID = nil end
local out = {}
for k, v in ipairs(statements1) do
if not onlysrc or sourced(v) then
local snak = v.mainsnak
if snak.datatype == "wikibase-item" and snak.snaktype == "value" then
local qid2 = snak.datavalue.value.id
local statements2 = {}
if args.reqranks.b then
statements2 = mw.wikibase.getBestStatements(qid2, pid2)
else
statements2 = mw.wikibase.getAllStatements(qid2, pid2)
end
if statements2[1] then
local out2 = propertyvalueandquals(statements2, args, qualID)
out[#out+1] = assembleoutput(out2, args, qid2, pid2)
end
end -- of test for valid property1 value
end -- of test for sourced
if maxvals > 0 and #out >= maxvals then break end
end -- of loop through values of property1
return assembleoutput(out, args, qid1, pid1)
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getLabel has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- getAwardCat takes most of the usual parameters. If the item has values of P166 (award received),
-- It returns the Wikidata label for the local language as plain text.
-- then it examines each of those awards for P2517 (category for recipients of this award).
-- If there is no label in the local language, it displays the qid instead.
-- If it exists, it returns the corresponding category,
-- with the item's P734 (family name) as sort key, or no sort key if there is no family name.
-- The sort key may be overridden by the parameter |sortkey (alias |sk).
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getLabel = function(frame)
p.getAwardCat = function(frame)
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
frame.args.reqranks = setRanks(frame.args.rank)
if itemID == "" then return end
frame.args.langobj = findLang(frame.args.lang)
local label = labelOrId(itemID)
frame.args.lang = frame.args.langobj.code
return label
local args = frame.args
args.sep = " "
local pid1 = args.prop1 or "P166"
local pid2 = args.prop2 or "P2517"
if pid1 == "" or pid2 == "" then return nil end
-- locally supplied value:
local localval = mw.text.trim(args[1] or "")
local qid1, statements1 = parseInput(frame, localval, pid1)
if not qid1 then return localval end
-- linkprefix (strip quotes)
local lp = (args.linkprefix or args.lp or ""):gsub('"', '')
-- sort key (strip quotes, hyphens and periods):
local sk = (args.sortkey or args.sk or ""):gsub('["-.]', '')
-- family name:
local famname = ""
if sk == "" then
local p734 = mw.wikibase.getBestStatements(qid1, "P734")[1]
local p734id = p734 and p734.mainsnak.snaktype == "value" and p734.mainsnak.datavalue.value.id or ""
famname = mw.wikibase.getSitelink(p734id) or ""
-- strip namespace and disambigation
local pos = famname:find(":") or 0
famname = famname:sub(pos+1):gsub("%s%(.+%)$", "")
if famname == "" then
local lbl = mw.wikibase.getLabel(p734id)
famname = lbl and mw.text.nowiki(lbl) or ""
end
end
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local maxvals = tonumber(args.maxvals) or 0
local qualID = mw.text.trim(args.qual or ""):upper()
if qualID == "" then qualID = nil end
local out = {}
for k, v in ipairs(statements1) do
if not onlysrc or sourced(v) then
local snak = v.mainsnak
if snak.datatype == "wikibase-item" and snak.snaktype == "value" then
local qid2 = snak.datavalue.value.id
local statements2 = {}
if args.reqranks.b then
statements2 = mw.wikibase.getBestStatements(qid2, pid2)
else
statements2 = mw.wikibase.getAllStatements(qid2, pid2)
end
if statements2[1] and statements2[1].mainsnak.snaktype == "value" then
local qid3 = statements2[1].mainsnak.datavalue.value.id
local sitelink = mw.wikibase.getSitelink(qid3)
-- if there's no local sitelink, create the sitelink from English label
if not sitelink then
local lbl = mw.wikibase.getLabelByLang(qid3, "en")
if lbl then
if lbl:sub(1,9) == "Category:" then
sitelink = mw.text.nowiki(lbl)
else
sitelink = "Category:" .. mw.text.nowiki(lbl)
end
end
end
if sitelink then
if sk ~= "" then
out[#out+1] = "[[" .. lp .. sitelink .. "|" .. sk .. "]]"
elseif famname ~= "" then
out[#out+1] = "[[" .. lp .. sitelink .. "|" .. famname .. "]]"
else
out[#out+1] = "[[" .. lp .. sitelink .. "]]"
end -- of check for sort keys
end -- of test for sitelink
end -- of test for category
end -- of test for wikibase item has a value
end -- of test for sourced
if maxvals > 0 and #out >= maxvals then break end
end -- of loop through values of property1
return assembleoutput(out, args, qid1, pid1)
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getAT has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- getIntersectCat takes most of the usual parameters.
-- If there is a sitelink to an article on the local Wiki, it returns the sitelink as plain text.
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-- If there is no sitelink, it returns nothing.
-- It takes two properties, |prop1 and |prop2 (e.g. occupation and country of citizenship)
-- Each property's value is a wiki-base entity
-- For each value of the first parameter (ranks implemented) it fetches the value's main category
-- and then each value of the second parameter (possibly substituting a simpler description)
-- then it returns all of the categories representing the intersection of those properties,
-- (e.g. Category:Actors from Canada). A joining term may be supplied (e.g. |join=from).
-- The item's P734 (family name) is the sort key, or no sort key if there is no family name.
-- The sort key may be overridden by the parameter |sortkey (alias |sk).
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getAT = function(frame)
p.getIntersectCat = function(frame)
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
frame.args.reqranks = setRanks(frame.args.rank)
if itemID == "" then return end
frame.args.langobj = findLang(frame.args.lang)
return mw.wikibase.sitelink(itemID)
frame.args.lang = frame.args.langobj.code
end
local args = frame.args
 
args.sep = " "
 
args.linked = "no"
-------------------------------------------------------------------------------
local pid1 = args.prop1 or "P106"
-- getDescription has the qid of a Wikidata entity passed as |qid=
local pid2 = args.prop2 or "P27"
-- (it defaults to the associated qid of the current article if omitted)
if pid1 == "" or pid2 == "" then return nil end
-- and a local parameter passed as the first unnamed parameter.
local qid, statements1 = parseInput(frame, "", pid1)
-- Any local parameter passed (other than "Wikidata" or "none") becomes the return value.
if not qid then return nil end
-- It returns the article description for the Wikidata entity if the local parameter is "Wikidata".
local qid, statements2 = parseInput(frame, "", pid2)
-- Nothing is returned if the description doesn't exist or "none" is passed as the local parameter.
if not qid then return nil end
-------------------------------------------------------------------------------
-- topics like countries may have different names in categories from their label in Wikidata
-- Dependencies: none
local subs_exists, subs = pcall(mw.loadData, "Module:WikidataIB/subs")
-------------------------------------------------------------------------------
local join = args.join or ""
p.getDescription = function(frame)
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local desc = mw.text.trim(frame.args[1] or "")
local maxvals = tonumber(args.maxvals) or 0
local itemID = mw.text.trim(frame.args.qid or "")
-- linkprefix (strip quotes)
if itemID == "" then itemID = nil end
local lp = (args.linkprefix or args.lp or ""):gsub('"', '')
if desc:lower() == 'wikidata' then
-- sort key (strip quotes, hyphens and periods):
return mw.wikibase.description(itemID)
local sk = (args.sortkey or args.sk or ""):gsub('["-.]', '')
elseif desc:lower() == 'none' then
-- family name:
return nil
local famname = ""
else
if sk == "" then
return desc
local p734 = mw.wikibase.getBestStatements(qid, "P734")[1]
local p734id = p734 and p734.mainsnak.snaktype == "value" and p734.mainsnak.datavalue.value.id or ""
famname = mw.wikibase.getSitelink(p734id) or ""
-- strip namespace and disambigation
local pos = famname:find(":") or 0
famname = famname:sub(pos+1):gsub("%s%(.+%)$", "")
if famname == "" then
local lbl = mw.wikibase.getLabel(p734id)
famname = lbl and mw.text.nowiki(lbl) or ""
end
end
local cat1 = {}
for k, v in ipairs(statements1) do
if not onlysrc or sourced(v) then
-- get the ID representing the value of the property
local pvalID = (v.mainsnak.snaktype == "value") and v.mainsnak.datavalue.value.id
if pvalID then
-- get the topic's main category (P910) for that entity
local p910 = mw.wikibase.getBestStatements(pvalID, "P910")[1]
if p910 and p910.mainsnak.snaktype == "value" then
local tmcID = p910.mainsnak.datavalue.value.id
-- use sitelink or the English label for the cat
local cat = mw.wikibase.getSitelink(tmcID)
if not cat then
local lbl = mw.wikibase.getLabelByLang(tmcID, "en")
if lbl then
if lbl:sub(1,9) == "Category:" then
cat = mw.text.nowiki(lbl)
else
cat = "Category:" .. mw.text.nowiki(lbl)
end
end
end
cat1[#cat1+1] = cat
end -- of test for topic's main category exists
end -- of test for property has vaild value
end -- of test for sourced
if maxvals > 0 and #cat1 >= maxvals then break end
end
local cat2 = {}
for k, v in ipairs(statements2) do
if not onlysrc or sourced(v) then
local cat = rendersnak(v, args)
if subs[cat] then cat = subs[cat] end
cat2[#cat2+1] = cat
end
if maxvals > 0 and #cat2 >= maxvals then break end
end
out = {}
for k1, v1 in ipairs(cat1) do
for k2, v2 in ipairs(cat2) do
if sk ~= "" then
out[#out+1] = "[[" .. lp .. v1 .. " " .. join .. " " .. v2 .. "|" .. sk .. "]]"
elseif famname ~= "" then
out[#out+1] = "[[" .. lp .. v1 .. " " .. join .. " " .. v2 .. "|" .. famname .. "]]"
else
out[#out+1] = "[[" .. lp .. v1 .. " " .. join .. " " .. v2 .. "]]"
end -- of check for sort keys
end
end
end
args.noicon = "true"
return assembleoutput(out, args, qid, pid1)
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getAliases has the qid of a Wikidata entity passed as |qid=
-- qualsToTable takes most of the usual parameters.
-- (it defaults to the associated qid of the current article if omitted)
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented.
-- and a local parameter passed as the first unnamed parameter.
-- A qid may be given, and the first unnamed parameter is the property ID, which is of type wikibase item.
-- It implements blacklisting and whitelisting with a field name of "alias" by default.
-- It takes a list of qualifier property IDs as |quals=
-- Any local parameter passed becomes the return value.
-- For a given qid and property, it creates the rows of an html table,
-- Otherwise it returns the aliases for the Wikidata entity with the usual list options.
-- each row being a value of the property (optionally only if the property matches the value in |pval= )
-- Nothing is returned if the aliases do not exist.
-- each cell being the first value of the qualifier corresponding to the list in |quals
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: findLang(); assembleoutput()
-- Dependencies: parseParam; setRanks; parseInput; sourced;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getAliases = function(frame)
p.qualsToTable = function(frame)
local args = frame.args
local args = frame.args


local fieldname = args.name or ""
local quals = args.quals or ""
if fieldname == "" then fieldname = "alias" end
if quals == "" then return "" end


local blacklist = args.suppressfields or args.spf or ""
args.reqranks = setRanks(args.rank)
if blacklist:find(fieldname) then return nil end


local localval = mw.text.trim(args[1] or "")
local propertyID = mw.text.trim(args[1] or "")
if localval ~= "" then return localval end
local f = {}
f.args = args
local entityid, props = parseInput(f, "", propertyID)
if not entityid then return "" end


local whitelist = args.fetchwikidata or args.fwd or ""
args.langobj = findLang(args.lang)
if whitelist == "" then whitelist = "NONE" end
args.lang = args.langobj.code
if not (whitelist == 'ALL' or whitelist:find(fieldname)) then return nil end


local qid = mw.text.trim(args.qid or "")
local pval = args.pval or ""
if qid == "" then qid = nil end


local entity = mw.wikibase.getEntity(qid)
local qplist = mw.text.split(quals, "%p") -- split at punctuation and make a sequential table
if not entity then return nil end
for i, v in ipairs(qplist) do
local aliases = entity.aliases
qplist[i] = mw.text.trim(v):upper() -- remove whitespace and capitalise
if not aliases then return nil end
end
if not qid then qid= mw.wikibase.getEntityIdForCurrentPage() end


args.langobj = findLang(args.lang)
local col1 = args.firstcol or ""
local langcode = args.langobj.code
if col1 ~= "" then
args.lang = langcode
col1 = col1 .. "</td><td>"
end
 
local emptycell = args.emptycell or "&nbsp;"
 
-- construct a 2-D array of qualifier values in qvals
local qvals = {}
for i, v in ipairs(props) do
local skip = false
if pval ~= "" then
local pid = v.mainsnak.datavalue and v.mainsnak.datavalue.value.id
if pid ~= pval then skip = true end
end
if not skip then
local qval = {}
local vqualifiers = v.qualifiers or {}
-- go through list of wanted qualifier properties
for i1, v1 in ipairs(qplist) do
-- check for that property ID in the statement's qualifiers
local qv, qtype
if vqualifiers[v1] then
qtype = vqualifiers[v1][1].datatype
if qtype == "time" then
if vqualifiers[v1][1].snaktype == "value" then
qv = mw.wikibase.renderSnak(vqualifiers[v1][1])
qv = frame:expandTemplate{title="dts", args={qv}}
else
qv = "?"
end
elseif qtype == "url" then
qv = mw.wikibase.renderSnak(vqualifiers[v1][1])
local display = mw.ustring.match( mw.uri.decode(qv, "WIKI"), "([%w ]+)$" )
if display then
qv = "[" .. qv .. " " .. display .. "]"
end
else
qv = mw.wikibase.formatValue(vqualifiers[v1][1])
end
end
-- record either the value or a placeholder
qval[i1] = qv or emptycell
end -- of loop through list of qualifiers
-- add the list of qualifier values as a "row" in the main list
qvals[#qvals+1] = qval
end
end -- of for each value loop


local out = {}
local out = {}
for k1, v1 in pairs(aliases) do
for i, v in ipairs(qvals) do
if v1[1].language == langcode then
out[i] = "<tr><td>" .. col1 .. table.concat(qvals[i], "</td><td>") .. "</td></tr>"
for k1, v2 in ipairs(v1) do
out[#out+1] = v2.value
end
break
end
end
end
 
return table.concat(out, "\n")
return assembleoutput(out, args, qid)
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- pageId returns the page id (entity ID, Qnnn) of the current page
-- getGlobe takes an optional qid of a Wikidata entity passed as |qid=
-- returns nothing if the page is not connected to Wikidata
-- otherwise it uses the linked item for the current page.
-- If returns the Qid of the globe used in P625 (coordinate location),
-- or nil if there isn't one.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.pageId = function(frame)
p.getGlobe = function(frame)
return mw.wikibase.getEntityIdForCurrentPage()
local qid = frame.args.qid or frame.args[1] or ""
end
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
local coords = mw.wikibase.getBestStatements(qid, "P625")[1]
local globeid
if coords and coords.mainsnak.snaktype == "value" then
globeid = coords.mainsnak.datavalue.value.globe:match("(Q%d+)")
end
return globeid
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- formatDate is a wrapper to export the private function format_Date
-- getCommonsLink takes an optional qid of a Wikidata entity passed as |qid=
-- It returns one of the following in order of preference:
-- the Commons sitelink of the linked Wikidata item;
-- the Commons sitelink of the topic's main category of the linked Wikidata item;
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: format_Date();
-- Dependencies: _getCommonslink(); _getSitelink(); parseParam()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.formatDate = function(frame)
p.getCommonsLink = function(frame)
return format_Date(frame.args[1], frame.args.df, frame.args.bc)
local oc = frame.args.onlycat or frame.args.onlycategories
local fb = parseParam(frame.args.fallback or frame.args.fb, true)
return _getCommonslink(frame.args.qid, oc, fb)
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- location is a wrapper to export the private function _location
-- getSitelink takes the qid of a Wikidata entity passed as |qid=
-- it takes the entity-id as qid or the first unnamed parameter
-- It takes an optional parameter |wiki= to determine which wiki is to be checked for a sitelink
-- optional boolean parameter first toggles the display of the first item
-- If the parameter is blank, then it uses the local wiki.
-- optional boolean parameter skip toggles the display to skip to the last item
-- If there is a sitelink to an article available, it returns the plain text link to the article
-- If there is no sitelink, it returns nil.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: _location();
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.location = function(frame)
p.getSiteLink = function(frame)
local qid = mw.text.trim(frame.args.qid or frame.args[1] or ""):upper()
return _getSitelink(frame.args.qid, frame.args.wiki or mw.text.trim(frame.args[1] or ""))
if qid == "" then return "No id supplied" end
local first = mw.text.trim(frame.args.first or "")
local skip = mw.text.trim(frame.args.skip or "")
return table.concat( _location(qid, first, skip), ", " )
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- checkBlacklist implements a test to check whether a named field is allowed
-- getLink has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- returns true if the field is not blacklisted (i.e. allowed)
-- If there is a sitelink to an article on the local Wiki, it returns a link to the article
-- returns false if the field is blacklisted (i.e. disallowed)
-- with the Wikidata label as the displayed text.
-- {{#if:{{#invoke:WikidataIB |checkBlacklist |name=Joe |suppressfields=Dave; Joe; Fred}} | not blacklisted | blacklisted}}
-- If there is no sitelink, it returns the label as plain text.
-- displays "blacklisted"
-- If there is no label in the local language, it displays the qid instead.
-- {{#if:{{#invoke:WikidataIB |checkBlacklist |name=Jim |suppressfields=Dave; Joe; Fred}} | not blacklisted | blacklisted}}
-- displays "not blacklisted"
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.checkBlacklist = function(frame)
p.getLink = function(frame)
local blacklist = frame.args.suppressfields or frame.args.spf or ""
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
local fieldname = frame.args.name or ""
if itemID == "" then return end
if blacklist ~= "" and fieldname ~= "" then
local sitelink = mw.wikibase.getSitelink(itemID)
if blacklist:find(fieldname) then
local label = labelOrId(itemID)
return false
if sitelink then
else
return "[[:" .. sitelink .. "|" .. label .. "]]"
return true
else
end
return label
else
-- one of the fields is missing: let's call that "not on the list"
return true
end
end
end
end
Line 1,945: Line 2,590:


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- emptyor returns nil if its first unnamed argument is just punctuation, whitespace or html tags
-- getLabel has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- otherwise it returns the argument unchanged (including leading/trailing space).
-- It returns the Wikidata label for the local language as plain text.
-- If the argument may contain "=", then it must be called explicitly:
-- If there is no label in the local language, it displays the qid instead.
-- |1=arg
-- (In that case, leading and trailing spaces are trimmed)
-- It finds use in infoboxes where it can replace tests like:
-- {{#if: {{#invoke:WikidatIB |getvalue |P99 |fwd=ALL}} | <span class="xxx">{{#invoke:WikidatIB |getvalue |P99 |fwd=ALL}}</span> | }}
-- with a form that uses just a single call to Wikidata:
-- {{#invoke |WikidataIB |emptyor |1= <span class="xxx">{{#invoke:WikidataIB |getvalue |P99 |fwd=ALL}}</span> }}
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.emptyor = function(frame)
p.getLabel = function(frame)
local s = frame.args[1] or ""
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
if s == "" then return nil end
if itemID == "" then return end
local sx = s:gsub("%s", ""):gsub("<[^>]*>", ""):gsub("%p", "")
local lang = frame.args.lang or ""
if sx == "" then
if lang == "" then lang = nil end
return nil
local label = labelOrId(itemID, lang)
else
return label
return s
end
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- labelorid is a public function to expose the output of labelOrId()
-- label has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- Pass the Q-number as |qid= or as an unnamed parameter.
-- if no qid is supplied, it uses the qid associated with the current page.
-- It returns the Wikidata label for that entity or the qid if no label exists.
-- It returns the Wikidata label for the local language as plain text.
-- If there is no label in the local language, it returns nil.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: labelOrId
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.labelorid = function(frame)
p.label = function(frame)
local label = labelOrId( frame.args.qid or frame.args[1] )
local qid = mw.text.trim(frame.args[1] or frame.args.qid or "")
return label
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return end
local lang = frame.args.lang or ""
if lang == "" then lang = nil end
local label, success = labelOrId(qid, lang)
if success then return label end
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getLang returns the MediaWiki language code of the current content.
-- getAT (Article Title)
-- If optional parameter |style=full, it returns the language name.
-- has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid=
-- If there is a sitelink to an article on the local Wiki, it returns the sitelink as plain text.
-- If there is no sitelink or qid supplied, it returns nothing.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getLang = function(frame)
p.getAT = function(frame)
local style = (frame.args.style or ""):lower()
local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "")
local langcode = mw.language.getContentLanguage().code
if itemID == "" then return end
if style == "full" then
return mw.wikibase.getSitelink(itemID)
return mw.language.fetchLanguageName( langcode )
end
return langcode
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- findLanguage exports the local findLang() function
-- getDescription has the qid of a Wikidata entity passed as |qid=
-- It takes an optional language code and returns, in order of preference:
-- (it defaults to the associated qid of the current article if omitted)
-- the code if a known language;
-- and a local parameter passed as the first unnamed parameter.
-- the user's language, if set;
-- Any local parameter passed (other than "Wikidata" or "none") becomes the return value.
-- the server's content language.
-- 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 "none" is passed as the local parameter.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: findLang
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.findLanguage = function(frame)
p.getDescription = function(frame)
return findLang(frame.args.lang or frame.args[1]).code
local desc = mw.text.trim(frame.args[1] or "")
local itemID = mw.text.trim(frame.args.qid or "")
if itemID == "" then itemID = nil end
if desc:lower() == 'wikidata' then
return mw.wikibase.getDescription(itemID)
elseif desc:lower() == 'none' then
return nil
else
return desc
end
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getQid returns the qid, if supplied
-- getAliases has the qid of a Wikidata entity passed as |qid=
-- failing that, the Wikidata entity ID of the "category's main topic (P301)", if it exists
-- (it defaults to the associated qid of the current article if omitted)
-- failing that, the Wikidata entity ID asociated with the curent page, if it exists
-- and a local parameter passed as the first unnamed parameter.
-- otherwise, nothing
-- 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: none
-- Dependencies: findLang(); assembleoutput()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getQid = function(frame)
p.getAliases = function(frame)
local qid = (frame.args.qid or ""):upper()
local args = frame.args
-- check if a qid was passed; if so, return it:
 
if qid ~= "" then return qid end
local fieldname = args.name or ""
-- check if there's a "category's main topic (P301)":
if fieldname == "" then fieldname = "alias" end
qid = mw.wikibase.getEntityIdForCurrentPage()
 
if qid then
local blacklist = args.suppressfields or args.spf or ""
local prop301 = mw.wikibase.getBestStatements(qid, "P301")
if blacklist:find(fieldname) then return nil end
if prop301[1] then
 
local mctid = prop301[1].mainsnak.datavalue.value.id
local localval = mw.text.trim(args[1] or "")
if mctid then return mctid end
if localval ~= "" then return localval end
end
end
-- otherwise return the page qid (if any)
return qid
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 = mw.text.trim(args.qid or "")
-- followQid takes two optional parameters: qid and props
if qid == "" then qid = nil end
-- If qid is not given, it uses the qid for the connected page
 
-- or returns nil if there isn't one.
local entity = mw.wikibase.getEntity(qid)
-- props is a list of properties, separated by punctuation.
if not entity then return nil end
-- If props is given, the Wikidata item for the qid is examined for each property in turn.
local aliases = entity.aliases
-- If that property contains a value that is another Wikibase-item, that item's qid is returned,
if not aliases then return nil end
-- and the search terminates.
if not qid then qid= mw.wikibase.getEntityIdForCurrentPage() end
-- If props is not given, the qid is returned.
 
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: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.followQid = function(frame)
p.pageId = function(frame)
local qid = (frame.args.qid or ""):upper()
return mw.wikibase.getEntityIdForCurrentPage()
if qid == "" then
end
qid = mw.wikibase.getEntityIdForCurrentPage()
end
if not qid then return nil end


local props = (frame.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
return linkedid
end
end -- loop through values of property to follow
end -- loop through list of properties to follow
end


return qid
-------------------------------------------------------------------------------
-- formatDate is a wrapper to export the private function format_Date
-------------------------------------------------------------------------------
-- Dependencies: format_Date();
-------------------------------------------------------------------------------
p.formatDate = function(frame)
return format_Date(frame.args[1], frame.args.df, frame.args.bc)
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- siteID returns the root of the globalSiteID
-- location is a wrapper to export the private function _location
-- e.g. "en" for "enwiki", "enwikisource", etc.
-- it takes the entity-id as qid or the first unnamed parameter
-- treats "en-gb" as "en", etc.
-- 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: none
-- Dependencies: _location();
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.siteID = function(frame)
p.location = function(frame)
local txtlang = frame:preprocess( "{{int:lang}}" ) or ""
local debug = (frame.args.debug or ""):sub(1, 1):lower()
-- This deals with specific exceptions: be-tarask -> be-x-old
if debug == "" then debug = "n" end
if txtlang == "be-tarask" then
local qid = mw.text.trim(frame.args.qid or frame.args[1] or ""):upper()
return "be_x_old"
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
end
local pos = txtlang:find("-")
local first = mw.text.trim(frame.args.first or "")
local ret = ""
local skip = mw.text.trim(frame.args.skip or "")
if pos then
return table.concat( _location(qid, first, skip), ", " )
ret = txtlang:sub(1, pos-1)
else
ret = txtlang
end
return ret
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- projID returns the code used to link to the reader's language's project
-- checkBlacklist implements a test to check whether a named field is allowed
-- e.g "en" for [[:en:WikidataIB]]
-- returns true if the field is not blacklisted (i.e. allowed)
-- treats "en-gb" as "en", etc.
-- returns false if the field is blacklisted (i.e. disallowed)
-------------------------------------------------------------------------------
-- {{#if:{{#invoke:WikidataIB |checkBlacklist |name=Joe |suppressfields=Dave; Joe; Fred}} | not blacklisted | blacklisted}}
-- displays "blacklisted"
-- {{#if:{{#invoke:WikidataIB |checkBlacklist |name=Jim |suppressfields=Dave; Joe; Fred}} | not blacklisted | blacklisted}}
-- displays "not blacklisted"
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.projID = function(frame)
p.checkBlacklist = function(frame)
local txtlang = frame:preprocess( "{{int:lang}}" ) or ""
local blacklist = frame.args.suppressfields or frame.args.spf or ""
-- This deals with specific exceptions: be-tarask -> be-x-old
local fieldname = frame.args.name or ""
if txtlang == "be-tarask" then
if blacklist ~= "" and fieldname ~= "" then
return "be-x-old"
if blacklist:find(fieldname) then
end
return false
local pos = txtlang:find("-")
else
local ret = ""
return true
if pos then
end
ret = txtlang:sub(1, pos-1)
else
else
ret = txtlang
-- one of the fields is missing: let's call that "not on the list"
return true
end
end
return ret
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- formatNumber formats a number according to the the supplied language code ("|lang=")
-- emptyor returns nil if its first unnamed argument is just punctuation, whitespace or html tags
-- or the default language if not supplied.
-- otherwise it returns the argument unchanged (including leading/trailing space).
-- The number is the first unnamed parameter or "|num="
-- 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:
-- {{#if: {{#invoke:WikidatIB |getvalue |P99 |fwd=ALL}} | <span class="xxx">{{#invoke:WikidatIB |getvalue |P99 |fwd=ALL}}</span> | }}
-- with a form that uses just a single call to Wikidata:
-- {{#invoke |WikidataIB |emptyor |1= <span class="xxx">{{#invoke:WikidataIB |getvalue |P99 |fwd=ALL}}</span> }}
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: findLang()
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.formatNumber = function(frame)
p.emptyor = function(frame)
local lang
local s = frame.args[1] or ""
local num = tonumber(frame.args[1] or frame.args.num) or 0
if s == "" then return nil end
lang = findLang(frame.args.lang)
local sx = s:gsub("%s", ""):gsub("<[^>]*>", ""):gsub("%p", "")
return lang:formatNum( num )
if sx == "" then
return nil
else
return s
end
end
end


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- examine dumps the property (the unnamed parameter or pid)
-- labelorid is a public function to expose the output of labelOrId()
-- from the item given by the parameter 'qid' (or the other unnamed parameter)
-- Pass the Q-number as |qid= or as an unnamed parameter.
-- or from the item corresponding to the current page if qid is not supplied.
-- It returns the Wikidata label for that entity or the qid if no label exists.
-- e.g. {{#invoke:WikidataIB |examine |pid=P26 |qid=Q42}}
-- or {{#invoke:WikidataIB |examine |P26 |Q42}} or any combination of these
-- or {{#invoke:WikidataIB |examine |P26}} for the current page.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: labelOrId
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.examine = function( frame )
p.labelorid = function(frame)
local args
return (labelOrId(frame.args.qid or frame.args[1]))
if frame.args[1] or frame.args.pid or frame.args.qid then
args = frame.args
else
args = frame: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
if pid:sub(1,1) ~= "P" then return "No property supplied" end
if qid:sub(1,1) ~= "Q" 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
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- checkvalue looks for 'val' as a wikibase-item value of a property (the unnamed parameter or pid)
-- getLang returns the MediaWiki language code of the current content.
-- from the item given by the parameter 'qid'
-- If optional parameter |style=full, it returns the language name.
-- or from the Wikidata item associated with the current page if qid is not supplied.
-------------------------------------------------------------------------------
-- If property is not supplied, then P31 (instance of) is assumed.
-- Dependencies: none
-- It returns val if found or nothing if not found.
-------------------------------------------------------------------------------
-- e.g. {{#invoke:WikidataIB |checkvalue |val=Q5 |pid=P31 |qid=Q42}}
p.getLang = function(frame)
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |P31 |qid=Q42}}
local style = (frame.args.style or ""):lower()
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |qid=Q42}}
local langcode = mw.language.getContentLanguage().code
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |P31}} for the current page.
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()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
p.getItemLangCode = function(frame)
return _getItemLangCode(frame.args.qid or frame.args[1])
end
 
 
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.checkvalue = function( frame )
-- findLanguage exports the local findLang() function
local args
-- It takes an optional language code and returns, in order of preference:
if frame.args.val then
-- the code if a known language;
args = frame.args
-- the user's language, if set;
else
-- the server's content language.
args = frame:getParent().args
-------------------------------------------------------------------------------
end
-- Dependencies: findLang
local val = args.val
-------------------------------------------------------------------------------
if not val then return nil end
p.findLanguage = function(frame)
local pid = mw.text.trim(args.pid or args[1] or "P31"):upper()
return findLang(frame.args.lang or frame.args[1]).code
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 stats = mw.wikibase.getAllStatements( qid, pid )
if not stats[1] then return nil end
if stats[1].mainsnak.datatype == "wikibase-item" then
for k, v in pairs( stats ) do
if v.mainsnak.snaktype == "value" and v.mainsnak.datavalue.value.id == val then
return val
end
end
end
return nil
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- url2 takes a parameter url= that is a proper url and formats it for use in an infobox.
-- getQid returns the qid, if supplied
-- If no parameter is supplied, it returns nothing.
-- failing that, the Wikidata entity ID of the "category's main topic (P301)", if it exists
-- This is the equivalent of Template:URL
-- failing that, the Wikidata entity ID associated with the current page, if it exists
-- but it keeps the "edit at Wikidata" pen icon out of the microformat.
-- otherwise, nothing
-- Usually it will take its url parameter directly from a Wikidata call:
-- e.g. {{#invoke:WikidataIB |url2 |url={{wdib |P856 |qid=Q23317 |fwd=ALL |osd=no}}
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.url2 = function(frame)
p.getQid = function(frame)
local txt = frame.args.url or ""
local qid = (frame.args.qid or ""):upper()
if txt == "" then return nil end
-- check if a qid was passed; if so, return it:
local url, icon = txt:match("(.+)&nbsp;(.+)")
if qid ~= "" then return qid end
url = url or txt
-- check if there's a "category's main topic (P301)":
icon = icon or ""
qid = mw.wikibase.getEntityIdForCurrentPage()
local prot, addr = url:match("(http[s]*://)(.+)")
if qid then
prot = prot or url
local prop301 = mw.wikibase.getBestStatements(qid, "P301")
addr = addr or ""
if prop301[1] then
local disp, n = addr:gsub("%.", "<wbr/>%.")
local mctid = prop301[1].mainsnak.datavalue.value.id
return '<span class="url">[' .. prot .. addr .. " " .. disp .. "]</span>&nbsp;" .. icon
if mctid then return mctid end
end
end
-- otherwise return the page qid (if any)
return qid
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getWebsite fetches the Official website (P856) and formats it for use in an infobox.
-- followQid takes three optional parameters: qid, props, and all.
-- This is similar to Template:Official website but with a url displayed,
-- If qid is not given, it uses the qid for the connected page
-- and it adds the "edit at Wikidata" pen icon beyond the microformat if enabled.
-- or returns nil if there isn't one.
-- A local value will override the Wikidata value. "NONE" returns nothing.
-- props is a list of properties, separated by punctuation.
-- e.g. {{#invoke:WikidataIB |getWebsite |qid= |noicon= |lang= |url= }}
-- 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, sparated by spaces.
-- If props is not given, the qid is returned.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: findLang(); parseParam();
-- Dependencies: parseParam()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getWebsite = function(frame)
p.followQid = function(frame)
local url = frame.args.url or ""
local qid = (frame.args.qid or ""):upper()
if url:upper() == "NONE" then return nil end
local all = parseParam(frame.args.all, false)
 
if qid == "" then
local qid = frame.args.qid or ""
qid = mw.wikibase.getEntityIdForCurrentPage()
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
end
if not qid then return nil end
if not qid then return nil end
 
local out = {}
local urls = {}
local props = (frame.args.props or ""):upper()
local quals = {}
if props ~= "" then
if url == "" then
for p in mw.text.gsplit(props, "%p") do -- split at punctuation and iterate
local prop856 = mw.wikibase.getBestStatements(qid, "P856")
p = mw.text.trim(p)
for k, v in pairs(prop856) do
for i, v in ipairs( mw.wikibase.getBestStatements(qid, p) ) do
if v.mainsnak.snaktype == "value" then
local linkedid = v.mainsnak.datavalue and v.mainsnak.datavalue.value.id
urls[#urls+1] = v.mainsnak.datavalue.value
if linkedid then
if v.qualifiers and v.qualifiers["P1065"] then
if all then
-- just take the first archive url (P1065)
out[#out+1] = linkedid
local au = v.qualifiers["P1065"][1]
else
if au.snaktype == "value" then
return linkedid
quals[#urls] = au.datavalue.value
end -- test for all or just the first one found
end -- test for archive url having a value
end -- test for value exists for that property
end -- test for qualifers
end -- loop through values of property to follow
end -- test for website having a value
end -- loop through list of properties to follow
end -- loop through website(s)
end
if #out > 0 then
return table.concat(out, " ")
else
else
urls[1] = url
return qid
end
end
if #urls == 0 then return nil end
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 class="url">[' .. link .. " " .. disp .. "]</span>"
end


local langcode = findLang(frame.args.lang).code
-------------------------------------------------------------------------------
local noicon = parseParam(frame.args.noicon, false)
-- globalSiteID returns the globalSiteID for the current wiki
if url == "" and not noicon then
-- e.g. returns "enwiki" for the English Wikipedia, "enwikisource" for English Wikisource, etc.
out[#out] = out[#out] .. createicon(langcode, qid, "P856")
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.globalSiteID = function(frame)
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: none
-------------------------------------------------------------------------------
p.siteID = function(frame)
local txtlang = frame:preprocess( "{{int:lang}}" ) or ""
-- This deals with specific exceptions: be-tarask -> be-x-old
if txtlang == "be-tarask" then
return "be_x_old"
end
end
 
local pos = txtlang:find("-")
local ret = ""
local ret = ""
if #out > 1 then
if pos then
ret = mw.getCurrentFrame():expandTemplate{title = "ubl", args = out}
ret = txtlang:sub(1, pos-1)
else
else
ret = out[1]
ret = txtlang
end
end
return ret
return ret
end
end
Line 2,298: Line 2,980:


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getAllLabels fetches the set of labels and formats it for display as wikitext.
-- projID returns the code used to link to the reader's language's project
-- It takes a parameter 'qid' for arbitrary access, otherwise it uses the current page.
-- e.g "en" for [[:en:WikidataIB]]
-- treats "en-gb" as "en", etc.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getAllLabels = function(frame)
p.projID = function(frame)
local args = frame.args or frame:getParent().args or {}
local txtlang = frame:preprocess( "{{int:lang}}" ) or ""
 
-- This deals with specific exceptions: be-tarask -> be-x-old
local qid = args.qid
if txtlang == "be-tarask" then
if qid == "" then qid = nil end
return "be-x-old"
 
end
local entity = mw.wikibase.getEntity(qid)
local pos = txtlang:find("-")
if not entity then return i18n["entity-not-found"] end
local ret = ""
 
if pos then
local labels = entity.labels
ret = txtlang:sub(1, pos-1)
if not labels then return i18n["labels-not-found"] end
else
 
ret = txtlang
local out = {}
for k, v in pairs(labels) do
out[#out+1] = v.value .. " (" .. v.language .. ")"
end
end
 
return ret
return table.concat(out, "; ")
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getAllDescriptions fetches the set of descriptions and formats it for display as wikitext.
-- formatNumber formats a number according to the the supplied language code ("|lang=")
-- It takes a parameter 'qid' for arbitrary access, otherwise it uses the current page.
-- or the default language if not supplied.
-- The number is the first unnamed parameter or "|num="
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: findLang()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getAllDescriptions = function(frame)
p.formatNumber = function(frame)
local args = frame.args or frame:getParent().args or {}
local lang
 
local num = tonumber(frame.args[1] or frame.args.num) or 0
local qid = args.qid
lang = findLang(frame.args.lang)
if qid == "" then qid = nil end
return lang:formatNum( num )
 
local entity = mw.wikibase.getEntity(qid)
if not entity then return i18n["entity-not-found"] end
 
local descriptions = entity.descriptions
if not descriptions then return i18n["descriptions-not-found"] end
 
local out = {}
for k, v in pairs(descriptions) do
out[#out+1] = v.value .. " (" .. v.language .. ")"
end
 
return table.concat(out, "; ")
end
end




-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getAllAliases fetches the set of aliases and formats it for display as wikitext.
-- examine dumps the property (the unnamed parameter or pid)
-- It takes a parameter 'qid' for arbitrary access, otherwise it uses the current page.
-- 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. {{#invoke:WikidataIB |examine |pid=P26 |qid=Q42}}
-- or {{#invoke:WikidataIB |examine |P26 |Q42}} or any combination of these
-- or {{#invoke:WikidataIB |examine |P26}} for the current page.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Dependencies: none
-- Dependencies: none
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
p.getAllAliases = function(frame)
p.examine = function( frame )
local args = frame.args or frame:getParent().args or {}
local args
if frame.args[1] or frame.args.pid or frame.args.qid then
args = frame.args
else
args = frame: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


local qid = args.qid
if qid == "" then qid = nil end


local entity = mw.wikibase.getEntity(qid)
-------------------------------------------------------------------------------
if not entity then return i18n["entity-not-found"] 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'
local aliases = entity.aliases
-- or from the Wikidata item associated with the current page if qid is not supplied.
if not aliases then return i18n["aliases-not-found"] end
-- If property is not supplied, then P31 (instance of) is assumed.
 
-- It returns val if found or nothing if not found.
local out = {}
-- e.g. {{#invoke:WikidataIB |checkvalue |val=Q5 |pid=P31 |qid=Q42}}
for k1, v1 in pairs(aliases) do
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |P31 |qid=Q42}}
local lang = v1[1].language
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |qid=Q42}}
local val = {}
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |P31}} for the current page.
for k1, v2 in ipairs(v1) do
-------------------------------------------------------------------------------
val[#val+1] = v2.value
-- Dependencies: none
end
-------------------------------------------------------------------------------
out[#out+1] = table.concat(val, ", ") .. " (" .. lang .. ")"
p.checkvalue = function( frame )
local args
if frame.args.val then
args = frame.args
else
args = frame:getParent().args
end
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 stats = mw.wikibase.getAllStatements( qid, pid )
if not stats[1] then return nil end
if stats[1].mainsnak.datatype == "wikibase-item" then
for k, v in pairs( stats ) do
if v.mainsnak.snaktype == "value" and v.mainsnak.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. {{#invoke:WikidataIB |url2 |url={{wdib |P856 |qid=Q23317 |fwd=ALL |osd=no}}
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.url2 = function(frame)
local txt = frame.args.url or ""
if txt == "" then return nil end
local url, icon = txt:match("(.+)&nbsp;(.+)")
url = url or txt
icon = icon or ""
local prot, addr = url:match("(http[s]*://)(.+)")
prot = prot or url
addr = addr or ""
local disp, n = addr:gsub("%.", "<wbr/>%.")
return '<span class="url">[' .. 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. {{#invoke:WikidataIB |getWebsite |qid= |noicon= |lang= |url= }}
-------------------------------------------------------------------------------
-- Dependencies: findLang(); parseParam();
-------------------------------------------------------------------------------
p.getWebsite = function(frame)
local url = frame.args.url or ""
if url:upper() == "NONE" then return nil end
 
local qid = frame.args.qid or ""
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return nil end
 
local urls = {}
local quals = {}
if url == "" then
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)
else
urls[1] = url
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 class="url">[' .. link .. " " .. disp .. "]</span>"
end
 
local langcode = findLang(frame.args.lang).code
local noicon = parseParam(frame.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.
-- It takes a parameter 'qid' for arbitrary access, otherwise it uses the current page.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.getAllLabels = function(frame)
local args = frame.args or frame:getParent().args or {}
 
local qid = args.qid
if qid == "" then qid = nil end
 
local entity = mw.wikibase.getEntity(qid)
if not entity then return i18n["entity-not-found"] end
 
local labels = entity.labels
if not labels then return i18n["labels-not-found"] end
 
local out = {}
for k, v in pairs(labels) do
out[#out+1] = v.value .. " (" .. v.language .. ")"
end
 
return table.concat(out, "; ")
return table.concat(out, "; ")
end
-------------------------------------------------------------------------------
-- getAllDescriptions fetches the set of descriptions and formats it for display as wikitext.
-- It takes a parameter 'qid' for arbitrary access, otherwise it uses the current page.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.getAllDescriptions = function(frame)
local args = frame.args or frame:getParent().args or {}
local qid = args.qid
if qid == "" then qid = nil end
local entity = mw.wikibase.getEntity(qid)
if not entity then return i18n["entity-not-found"] end
local descriptions = entity.descriptions
if not descriptions then return i18n["descriptions-not-found"] end
local out = {}
for k, v in pairs(descriptions) do
out[#out+1] = v.value .. " (" .. v.language .. ")"
end
return table.concat(out, "; ")
end
-------------------------------------------------------------------------------
-- getAllAliases fetches the set of aliases and formats it for display as wikitext.
-- It takes a parameter 'qid' for arbitrary access, otherwise it uses the current page.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.getAllAliases = function(frame)
local args = frame.args or frame:getParent().args or {}
local qid = args.qid
if qid == "" then qid = nil end
local entity = mw.wikibase.getEntity(qid)
if not entity then return i18n["entity-not-found"] end
local aliases = entity.aliases
if not aliases then return i18n["aliases-not-found"] end
local out = {}
for k1, v1 in pairs(aliases) do
local lang = v1[1].language
local val = {}
for k1, v2 in ipairs(v1) do
val[#val+1] = v2.value
end
out[#out+1] = table.concat(val, ", ") .. " (" .. lang .. ")"
end
return table.concat(out, "; ")
end
-------------------------------------------------------------------------------
-- showNoLinks displays the article titles that should not be linked.
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
p.showNoLinks = function(frame)
local out = {}
for k, v in pairs(donotlink) do
out[#out+1] = k
end
table.sort( out )
return table.concat(out, "; ")
end
-------------------------------------------------------------------------------
-- checkValidity checks whether the first unnamed parameter represents a valid entity-id,
-- that is, something like Q1235 or P123.
-- It returns the strings "true" or "false".
-- Change false to nil to return "true" or "" (easier to test with #if:).
-------------------------------------------------------------------------------
-- Dependencies: none
-------------------------------------------------------------------------------
function p.checkValidity(frame)
local id = mw.text.trim(frame.args[1] or "")
if mw.wikibase.isValidEntityId(id) then
return true
else
return false
end
end
-------------------------------------------------------------------------------
-- getEntityFromTitle returns the Entity-ID (Q-number) for a given title.
-- Modification of Module:ResolveEntityId
-- The title is the first unnamed parameter.
-- The site parameter determines the site/language for the title. Defaults to current wiki.
-- The showdab parameter determines whether dab pages should return the Q-number or nil. Defaults to true.
-- Returns the Q-number or nil if it does not exist.
-------------------------------------------------------------------------------
-- Dependencies: parseParam
-------------------------------------------------------------------------------
function p.getEntityFromTitle(frame)
local args=frame.args
if not args[1] then args=frame:getParent().args end
if not args[1] then return nil end
local title = mw.text.trim(args[1])
local site = args.site or ""
local showdab = parseParam(args.showdab, true)
qid = mw.wikibase.getEntityIdForTitle(title, site)
if qid then
local prop31 = mw.wikibase.getBestStatements(qid, "P31")[1]
if not showdab and prop31 and prop31.mainsnak.datavalue.value.id == "Q4167410" then
return nil
else
return qid
end
end
end
-------------------------------------------------------------------------------
-- getDatePrecision returns the number representing the precision of the first best date value
-- for the given property.
-- It takes the qid and property ID
-- The meanings are given at https://www.mediawiki.org/wiki/Wikibase/DataModel#Dates_and_times
-- 0 = 1 billion years .. 6 = millennium, 7 = century, 8 = decade, 9 = year, 10 = month, 11 = day
-- Returns 0 (or the second unnamed parameter) if the Wikidata does not exist.
-------------------------------------------------------------------------------
-- Dependencies: parseParam
-------------------------------------------------------------------------------
function p.getDatePrecision(frame)
local args=frame.args
if not args[1] then args=frame:getParent().args end
local default = tonumber(args[2] or args.default) or 0
local prop = mw.text.trim(args[1] or "")
if prop == "" then return default end
local qid = args.qid or ""
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return default end
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local stat = mw.wikibase.getBestStatements(qid, prop)
for i, v in ipairs(stat) do
local prec = (onlysrc == false or sourced(v))
and v.mainsnak.datavalue
and v.mainsnak.datavalue.value
and v.mainsnak.datavalue.value.precision
if prec then return prec end
end
return default
end
end


Line 2,389: Line 3,365:
-- List of exported functions
-- List of exported functions
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- getValue
--[[
-- getPreferredValue
_getValue
-- getCoords
getValue
-- getQualifierValue
getPreferredValue
-- getValueByQual
getCoords
-- getValueByLang
getQualifierValue
-- getLink
getSumOfParts
-- getLabel
getValueByQual
-- getAT
getValueByLang
-- getDescription
getValueByRefSource
-- getAliases
getPropertyIDs
-- pageId
getQualifierIDs
-- formatDate
getPropOfProp
-- location
getAwardCat
-- checkBlacklist
getIntersectCat
-- emptyor
getGlobe
-- labelorid
getCommonsLink
-- getLang
getSiteLink
-- findLanguage
getLink
-- getQID
getLabel
-- followQid
label
-- siteID
getAT
-- projID
getDescription
-- formatNumber
getAliases
-- examine
pageId
-- checkvalue
formatDate
-- url2
location
-- getWebsite
checkBlacklist
-- getAllLabels
emptyor
-- getAllDescriptions
labelorid
-- getAllAliases
getLang
getItemLangCode
findLanguage
getQID
followQid
globalSiteID
siteID
projID
formatNumber
examine
checkvalue
url2
getWebsite
getAllLabels
getAllDescriptions
getAllAliases
showNoLinks
checkValidity
getEntityFromTitle
getDatePrecision
--]]
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------