Module:WikidataIB: Difference between revisions
return zero or a default for getDatePrecision
m (1 revision imported) |
(return zero or a default for getDatePrecision) |
||
Line 80: | Line 80: | ||
} | } | ||
} | } | ||
-- This allows | -- 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 95: | Line 95: | ||
if nolinks_exists then | if nolinks_exists then | ||
donotlink = nolinks.itemsindex | 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 | end | ||
Line 103: | Line 113: | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- makeOrdinal needs to be internationalised along with the above: | -- makeOrdinal needs to be internationalised along with the above: | ||
-- takes cardinal | -- 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 318: | 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 | -- 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 326: | Line 336: | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
local dateFormat = function(timestamp, dprec, df, bcf, pd, qualifiers, lang, adj, model) | 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 331: | 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") | ||
-- just in case date precision is missing | -- just in case date precision is missing | ||
dprec = dprec or 11 | dprec = dprec or 11 | ||
Line 373: | Line 395: | ||
-- no point in saying that dates before 1582 are Julian - they are by default | -- no point in saying that dates before 1582 are Julian - they are by default | ||
-- doesn't make sense for dates less precise than year | -- doesn't make sense for dates less precise than year | ||
-- we can | -- we can suppress it by setting |plaindate, e.g. for use in constructing categories. | ||
local calendarmodel = "" | local calendarmodel = "" | ||
if tonumber(year) > 1582 | if tonumber(year) > 1582 | ||
Line 389: | Line 411: | ||
-- 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> ') | fdate = fdate:gsub('circa ', '<abbr title="circa">c.</abbr> ') | ||
Line 412: | 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 445: | Line 475: | ||
local sitelink | local sitelink | ||
if wiki == "" then | if wiki == "" then | ||
sitelink = mw.wikibase. | sitelink = mw.wikibase.getSitelink(qid) | ||
else | else | ||
sitelink = mw.wikibase. | sitelink = mw.wikibase.getSitelink(qid, wiki) | ||
end | end | ||
return sitelink | return sitelink | ||
Line 474: | 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 482: | 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 499: | 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 529: | Line 573: | ||
postfix = postfix or "" | postfix = postfix or "" | ||
lang = lang or "en" -- fallback to default if missing | 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. | local sitelink = mw.wikibase.getSitelink(id) | ||
local label, islabel | local label, islabel | ||
if dtxt then | if dtxt then | ||
Line 553: | Line 605: | ||
if sitelink then | if sitelink then | ||
if not (dtxt or shortname) then | if not (dtxt or shortname) then | ||
-- strip any namespace or dab from the sitelink | -- strip any namespace or dab from the sitelink | ||
local pos = sitelink:find(":") or 0 | local pos = sitelink:find(":") or 0 | ||
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 | end | ||
if donotlink[label] then | if donotlink[label] then | ||
disp = prefix .. label .. postfix | disp = prefix .. fmt .. label .. fmt .. postfix | ||
else | else | ||
disp = "[[" .. lprefix .. sitelink .. lpostfix .. "|" .. prefix .. label .. postfix .. "]]" | disp = "[[" .. lprefix .. sitelink .. lpostfix .. "|" .. prefix .. fmt .. label .. fmt .. postfix .. "]]" | ||
end | 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 | 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 redirect | -- 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 | ||
Line 596: | 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 | -- it looks for a reference sourced to something not containing the word "wikipedia" | ||
-- it returns a boolean = true if | -- it returns a boolean = true if it finds a sourced reference. | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: none | -- Dependencies: none | ||
Line 605: | 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(" | if not ref:find("Wiki") then | ||
return true | return true | ||
end | end | ||
Line 657: | 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 668: | 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 | 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 | 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 718: | 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 = " [[" .. i18n["filespace"] | local icon = " <span class='penicon'>[[" | ||
-- " <span data-bridge-edit-flow='overwrite' class='penicon'>[[" -> enable Wikidata Bridge | |||
.. i18n["filespace"] | |||
.. ":OOjs UI icon edit-ltr-progressive.svg |frameless |text-top |10px |alt=" | |||
.. 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 774: | 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 781: | 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 798: | 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 873: | 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 | ||
-- | |||
-- 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) | val = dateFormat(dv.time, dv.precision, args.df, args.bc, args.pd, propval.qualifiers, args.lang, "", dv.calendarmodel) | ||
------------------------------------ | ------------------------------------ | ||
Line 882: | 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 898: | 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" | ||
Line 983: | Line 1,078: | ||
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. | 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 1,002: | Line 1,098: | ||
end | end | ||
-- format display: | -- format display: | ||
if conv | if conv then | ||
if range == "" then | if range == "" then | ||
val = mw.getCurrentFrame():expandTemplate{title = "cvt", args = {amount, | val = mw.getCurrentFrame():expandTemplate{title = "cvt", args = {amount, unit}} | ||
else | else | ||
val = mw.getCurrentFrame():expandTemplate{title = "cvt", args = {lowb, "to", upb, | val = mw.getCurrentFrame():expandTemplate{title = "cvt", args = {lowb, "to", upb, unit}} | ||
end | end | ||
elseif unit == "$" or unit == "£" then | elseif unit == "$" or unit == "£" then | ||
Line 1,023: | 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 | -- 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,148: | Line 1,245: | ||
local pd = args.plaindate or args.pd or "no" | local pd = args.plaindate or args.pd or "no" | ||
args.pd = pd | args.pd = pd | ||
-- 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 | 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 | |||
-- out[] holds the a list of returned values for this property | |||
-- mlt[] holds the language code if the datatype is monolingual text | -- mlt[] holds the language code if the datatype is monolingual text | ||
local out = {} | local out = {} | ||
local mlt = {} | local mlt = {} | ||
for k, v in ipairs(objproperty) do | for k, v in ipairs(objproperty) do | ||
local hasvalue = true | local hasvalue = true | ||
Line 1,175: | Line 1,317: | ||
-- 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 hasvalue and v.qualifiers and qualID and snak.snaktype | if hasvalue and v.qualifiers and qualID ~= "" and snak.snaktype~="novalue" then | ||
-- collect all wanted qualifier values returned in qlist, indexed by propertyID | |||
local qlist = {} | local qlist = {} | ||
local | local timestart, timeend = "", "" | ||
-- loop through qualifiers | |||
for k1, v1 in pairs(v.qualifiers) do | |||
if allflag or qwanted[k1] then | |||
if k1 == "P1326" then | |||
local ts = v1[1].datavalue.value.time | |||
local dp = v1[1].datavalue.value.precision | |||
qlist[k1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "before") | |||
elseif k1 == "P1319" then | |||
local ts = v1[1].datavalue.value.time | |||
local dp = v1[1].datavalue.value.precision | |||
qlist[k1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "after") | |||
elseif k1 == "P580" then | |||
timestart = propertyvalueandquals(v1, qargs)[1] or "" -- treat only one start time as valid | |||
elseif k1 == "P582" then | |||
timeend = propertyvalueandquals(v1, qargs)[1] or "" -- treat only one end time as valid | |||
else | |||
local q = assembleoutput(propertyvalueandquals(v1, qargs), qargs) | |||
-- we already deal with circa via 'sourcing circumstances' if the datatype was time | |||
-- circa may be either linked or unlinked *** internationalise later *** | |||
if datatype ~= "time" or q ~= "circa" and not (type(q) == "string" and q:find("circa]]")) then | |||
qlist[k1] = q | |||
end | |||
end | |||
end -- of test for wanted | |||
end -- of loop through qualifiers | |||
-- set date separator | |||
local t = timestart .. timeend | |||
local t = | |||
-- *** internationalise date separators later *** | -- *** internationalise date separators later *** | ||
local dsep = "–" | local dsep = "–" | ||
if t:find("%s") or t:find(" ") then dsep = " – " end | if t:find("%s") or t:find(" ") then dsep = " – " end | ||
if #qlist | -- set the order for the list of qualifiers returned; start time and end time go last | ||
local qstr = assembleoutput( | 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,275: | Line 1,372: | ||
out[#out] = out[#out] .. " (" .. qstr .. ")" | out[#out] = out[#out] .. " (" .. qstr .. ")" | ||
end | end | ||
elseif t | elseif t ~= "" then | ||
if qualsonly then | if qualsonly then | ||
out[#out+1] = | out[#out+1] = timestart .. dsep .. timeend | ||
else | else | ||
out[#out] = out[#out] .. " (" .. | out[#out] = out[#out] .. " (" .. timestart .. dsep .. timeend .. ")" | ||
end | end | ||
end | end | ||
Line 1,373: | 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] = 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) | out[#out + 1] = rendersnak(v1, args, linked, lpre, lpost, pre, post, uabbr, filter) | ||
end | end | ||
Line 1,411: | Line 1,512: | ||
local finished = false | local finished = false | ||
local count = 0 | local count = 0 | ||
local prevqid = "" | local prevqid = "Q0" | ||
repeat | repeat | ||
local prop | local prop | ||
Line 1,417: | Line 1,518: | ||
local proptbl = mw.wikibase.getBestStatements(qid, v1) | local proptbl = mw.wikibase.getBestStatements(qid, v1) | ||
if #proptbl > 1 then | if #proptbl > 1 then | ||
-- there is more than one | -- 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 | for i2, v2 in ipairs(proptbl) do | ||
parttbl = v2.qualifiers and v2.qualifiers.P518 | parttbl = v2.qualifiers and v2.qualifiers.P518 | ||
if parttbl then | if parttbl then | ||
-- this | -- this higher location has qualifier 'applies to part' (P518) | ||
for i3, v3 in ipairs(parttbl) do | for i3, v3 in ipairs(parttbl) do | ||
if v3.snaktype == "value" and v3.datavalue.value.id == prevqid then | if v3.snaktype == "value" and v3.datavalue.value.id == prevqid then | ||
Line 1,429: | Line 1,537: | ||
end -- of test for matching last location | end -- of test for matching last location | ||
end -- of loop through values of 'applies to part' | 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 | ||
end -- of loop through parent locations | end -- of loop through parent locations | ||
Line 1,497: | Line 1,612: | ||
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 | -- 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 | ||
Line 1,576: | Line 1,691: | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- getValue | -- _getValue makes the functionality of getValue available to other modules | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- 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. | p._getValue = function(args) | ||
-- parameter sets for commonly used groups of parameters | -- parameter sets for commonly used groups of parameters | ||
local paraset = tonumber( | local paraset = tonumber(args.ps or args.parameterset or 0) | ||
if paraset == 1 then | if paraset == 1 then | ||
-- a common setting | |||
args.rank = "best" | |||
args.fetchwikidata = "ALL" | |||
args.onlysourced = "no" | |||
args.noicon = "true" | |||
elseif paraset == 2 then | elseif paraset == 2 then | ||
-- | -- 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 | -- implement eid parameter | ||
local eid = args.eid | |||
if eid == "" then | |||
return nil | |||
elseif eid then | |||
args.qid = eid | |||
end | |||
local propertyID = mw.text.trim(args[1] or "") | |||
local entityid, props = parseInput( | 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( | 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 | -- set a language object and code in the args table | ||
args.langobj = findLang(args.lang) | |||
args.lang = args.langobj.code | |||
-- table 'out' stores the return value(s): | -- table 'out' stores the return value(s): | ||
local out = propertyvalueandquals(props, | 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, | 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,791: | 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,821: | 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 | local langcode = findLang(frame.args.lang).code | ||
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,842: | 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 | -- 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,933: | Line 2,084: | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getPropertyIDs takes most of the usual parameters. | ||
-- | -- The usual whitelisting, blacklisting, onlysourced, etc. are implemented. | ||
-- It returns the Entity-IDs (Qids) of the values of a property if it is a Wikibase-Entity. | |||
-- | -- Otherwise it returns nothing. | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput; | -- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput; | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getPropertyIDs = function(frame) | ||
local args = frame.args | local args = frame.args | ||
args.reqranks = setRanks(args.rank) | |||
args.langobj = findLang(args.lang) | |||
local | args.lang = args.langobj.code | ||
-- change default for noicon to true | |||
local | args.noicon = tostring(parseParam(args.noicon or "", true)) | ||
if not | 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 onlysrc = parseParam(args.onlysourced or args.osd, true) | ||
local maxvals = tonumber(args.maxvals) or 0 | local maxvals = tonumber(args.maxvals) or 0 | ||
out = {} | |||
for i, v in ipairs(props) do | |||
for | 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 | |||
return assembleoutput(out, args, qid, pid) | |||
end | |||
end | |||
if maxvals > 0 and #out >= maxvals then break end | |||
end | |||
return assembleoutput(out, args, | |||
end | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getQualifierIDs takes most of the usual parameters. | ||
-- | -- The usual whitelisting, blacklisting, onlysourced, etc. are implemented. | ||
-- | -- It takes a property-id as the first unnamed parameter, and an optional parameter qlist | ||
- | -- 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: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput; | -- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput; | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getQualifierIDs = function(frame) | ||
local args = frame.args | local args = frame.args | ||
args. | args.reqranks = setRanks(args.rank) | ||
local pid1 = args.prop1 or " | args.langobj = findLang(args.lang) | ||
local pid2 = args.prop2 or " | 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) | |||
end | |||
------------------------------------------------------------------------------- | |||
-- getPropOfProp takes two propertyIDs: prop1 and prop2 (as well as the usual parameters) | |||
-- If the value(s) of prop1 are of type "wikibase-item" then it returns the value(s) of prop2 | |||
-- of each of those wikibase-items. | |||
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented | |||
------------------------------------------------------------------------------- | |||
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput; | |||
------------------------------------------------------------------------------- | |||
p.getPropOfProp = function(frame) | |||
frame.args.reqranks = setRanks(frame.args.rank) | |||
frame.args.langobj = findLang(frame.args.lang) | |||
frame.args.lang = frame.args.langobj.code | |||
local args = frame.args | |||
local pid1 = args.prop1 or args.pid1 or "" | |||
local pid2 = args.prop2 or args.pid2 or "" | |||
local localval = mw.text.trim(args[1] or "") | |||
if pid1 == "" or pid2 == "" then return nil end | if pid1 == "" or pid2 == "" then return nil end | ||
local qid1, statements1 = parseInput(frame, localval, pid1) | local qid1, statements1 = parseInput(frame, localval, pid1) | ||
if not qid1 then return localval end | 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() | |||
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 | if qualID == "" then qualID = nil end | ||
local out = {} | local out = {} | ||
Line 2,037: | Line 2,220: | ||
statements2 = mw.wikibase.getAllStatements(qid2, pid2) | statements2 = mw.wikibase.getAllStatements(qid2, pid2) | ||
end | end | ||
if statements2[1] | if statements2[1] then | ||
local | 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 -- of test for | |||
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 | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getAwardCat takes most of the usual parameters. If the item has values of P166 (award received), | ||
-- then it examines each of those awards for P2517 (category for recipients of this award). | |||
-- 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 | -- The usual whitelisting, blacklisting, onlysourced, etc. are implemented | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput; | -- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput; | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getAwardCat = function(frame) | ||
frame.args.reqranks = setRanks(frame.args.rank) | frame.args.reqranks = setRanks(frame.args.rank) | ||
frame.args.langobj = findLang(frame.args.lang) | frame.args.langobj = findLang(frame.args.lang) | ||
Line 2,089: | Line 2,248: | ||
local args = frame.args | local args = frame.args | ||
args.sep = " " | args.sep = " " | ||
local pid1 = args.prop1 or "P166" | |||
local pid1 = args.prop1 or " | local pid2 = args.prop2 or "P2517" | ||
local pid2 = args.prop2 or " | |||
if pid1 == "" or pid2 == "" then return nil end | if pid1 == "" or pid2 == "" then return nil end | ||
local | -- locally supplied value: | ||
local localval = mw.text.trim(args[1] or "") | |||
local | local qid1, statements1 = parseInput(frame, localval, pid1) | ||
if not | if not qid1 then return localval end | ||
-- linkprefix (strip quotes) | -- linkprefix (strip quotes) | ||
local lp = (args.linkprefix or args.lp or ""):gsub('"', '') | local lp = (args.linkprefix or args.lp or ""):gsub('"', '') | ||
Line 2,109: | Line 2,262: | ||
local famname = "" | local famname = "" | ||
if sk == "" then | if sk == "" then | ||
local p734 = mw.wikibase.getBestStatements( | local p734 = mw.wikibase.getBestStatements(qid1, "P734")[1] | ||
local p734id = p734 and p734.mainsnak.snaktype == "value" and p734.mainsnak.datavalue.value.id or "" | local p734id = p734 and p734.mainsnak.snaktype == "value" and p734.mainsnak.datavalue.value.id or "" | ||
famname = mw.wikibase. | famname = mw.wikibase.getSitelink(p734id) or "" | ||
-- strip namespace and disambigation | -- strip namespace and disambigation | ||
local pos = famname:find(":") or 0 | local pos = famname:find(":") or 0 | ||
famname = famname:sub(pos+1):gsub("%s%(.+%)$", "") | famname = famname:sub(pos+1):gsub("%s%(.+%)$", "") | ||
if famname == "" then | if famname == "" then | ||
local lbl = mw.wikibase. | local lbl = mw.wikibase.getLabel(p734id) | ||
famname = lbl and mw.text.nowiki(lbl) or "" | famname = lbl and mw.text.nowiki(lbl) or "" | ||
end | end | ||
end | end | ||
local | 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 | for k, v in ipairs(statements1) do | ||
if not onlysrc or sourced(v) then | if not onlysrc or sourced(v) then | ||
local snak = v.mainsnak | |||
local | 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) | |||
local | else | ||
statements2 = mw.wikibase.getAllStatements(qid2, pid2) | |||
end | |||
if not | if statements2[1] and statements2[1].mainsnak.snaktype == "value" then | ||
local lbl = mw.wikibase.getLabelByLang( | 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 then | ||
if lbl:sub(1,9) == "Category:" then | if lbl:sub(1,9) == "Category:" then | ||
sitelink = mw.text.nowiki(lbl) | |||
else | else | ||
sitelink = "Category:" .. mw.text.nowiki(lbl) | |||
end | end | ||
end | end | ||
end | end | ||
if sitelink then | |||
end -- of test for | if sk ~= "" then | ||
end -- of test for | 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 | end -- of test for sourced | ||
if maxvals > 0 and # | if maxvals > 0 and #out >= maxvals then break end | ||
end -- of loop through values of property1 | |||
return assembleoutput(out, args, qid1, pid1) | |||
return assembleoutput(out, args, | |||
end | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getIntersectCat takes most of the usual parameters. | ||
-- | -- The usual whitelisting, blacklisting, onlysourced, etc. are implemented | ||
-- | -- It takes two properties, |prop1 and |prop2 (e.g. occupation and country of citizenship) | ||
-- or | -- 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: | -- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput; | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getIntersectCat = function(frame) | ||
frame.args.reqranks = setRanks(frame.args.rank) | |||
frame.args.langobj = findLang(frame.args.lang) | |||
local | frame.args.lang = frame.args.langobj.code | ||
local | local args = frame.args | ||
if | args.sep = " " | ||
args.linked = "no" | |||
end | local pid1 = args.prop1 or "P106" | ||
return | local pid2 = args.prop2 or "P27" | ||
end | if pid1 == "" or pid2 == "" then return nil end | ||
local qid, statements1 = parseInput(frame, "", pid1) | |||
if not qid then return nil end | |||
-- | local qid, statements2 = parseInput(frame, "", pid2) | ||
if not qid then return nil end | |||
-- topics like countries may have different names in categories from their label in Wikidata | |||
local subs_exists, subs = pcall(mw.loadData, "Module:WikidataIB/subs") | |||
local join = args.join or "" | |||
local onlysrc = parseParam(args.onlysourced or args.osd, true) | |||
local maxvals = tonumber(args.maxvals) or 0 | |||
-- linkprefix (strip quotes) | |||
local lp = (args.linkprefix or args.lp or ""):gsub('"', '') | |||
local | -- sort key (strip quotes, hyphens and periods): | ||
local | local sk = (args.sortkey or args.sk or ""):gsub('["-.]', '') | ||
-- family name: | |||
local famname = "" | |||
if sk == "" then | |||
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 | 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 | |||
end | 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 | |||
args.noicon = "true" | |||
return assembleoutput(out, args, qid, pid1) | |||
end | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- qualsToTable takes most of the usual parameters. | ||
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented. | |||
-- A qid may be given, and the first unnamed parameter is the property ID, which is of type wikibase item. | |||
-- It takes a list of qualifier property IDs as |quals= | |||
-- For a given qid and property, it creates the rows of an html table, | |||
-- each row being a value of the property (optionally only if the property matches the value in |pval= ) | |||
-- each cell being the first value of the qualifier corresponding to the list in |quals | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | -- Dependencies: parseParam; setRanks; parseInput; sourced; | ||
local | ------------------------------------------------------------------------------- | ||
p.qualsToTable = function(frame) | |||
local args = frame.args | |||
local quals = args.quals or "" | |||
if quals == "" then return "" end | |||
args.reqranks = setRanks(args.rank) | |||
local propertyID = mw.text.trim(args[1] or "") | |||
local f = {} | |||
f.args = args | |||
local entityid, props = parseInput(f, "", propertyID) | |||
if not entityid then return "" end | |||
args.langobj = findLang(args.lang) | |||
args.lang = args.langobj.code | |||
local pval = args.pval or "" | |||
local qplist = mw.text.split(quals, "%p") -- split at punctuation and make a sequential table | |||
for i, v in ipairs(qplist) do | |||
qplist[i] = mw.text.trim(v):upper() -- remove whitespace and capitalise | |||
-- and a | |||
end | end | ||
local col1 = args.firstcol or "" | |||
if col1 ~= "" then | |||
col1 = col1 .. "</td><td>" | |||
end | |||
local emptycell = args.emptycell or " " | |||
local | -- 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 | local out = {} | ||
for i, v in ipairs(qvals) do | |||
out[i] = "<tr><td>" .. col1 .. table.concat(qvals[i], "</td><td>") .. "</td></tr>" | |||
end | |||
return table.concat(out, "\n") | |||
end | |||
------------------------------------------------------------------------------- | |||
-- getGlobe takes an optional qid of a Wikidata entity passed as |qid= | |||
-- 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. | |||
------------------------------------------------------------------------------- | |||
-- | |||
-- returns | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: none | -- Dependencies: none | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getGlobe = function(frame) | ||
local qid = frame.args.qid or frame.args[1] or "" | |||
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 | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- 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: | -- Dependencies: _getCommonslink(); _getSitelink(); parseParam() | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getCommonsLink = function(frame) | ||
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 | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- 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 | |||
-- optional | -- 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: | -- Dependencies: none | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | 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= | ||
-- | -- If there is a sitelink to an article on the local Wiki, it returns a link to the article | ||
-- returns | -- with the Wikidata label as the displayed text. | ||
-- If there is no sitelink, it returns the label as plain text. | |||
-- displays | -- If there is no label in the local language, it displays the qid instead. | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: none | -- Dependencies: none | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getLink = function(frame) | ||
local | local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "") | ||
if itemID == "" then return end | |||
if | local sitelink = mw.wikibase.getSitelink(itemID) | ||
local label = labelOrId(itemID) | |||
if sitelink then | |||
return "[[:" .. sitelink .. "|" .. label .. "]]" | |||
else | else | ||
return label | |||
return | |||
end | end | ||
end | end | ||
Line 2,429: | Line 2,590: | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getLabel has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid= | ||
-- | -- It returns the Wikidata label for the local language as plain text. | ||
-- If the | -- If there is no label in the local language, it displays the qid instead. | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: none | -- Dependencies: none | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getLabel = function(frame) | ||
local | local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "") | ||
if | if itemID == "" then return end | ||
local | local lang = frame.args.lang or "" | ||
if | if lang == "" then lang = nil end | ||
local label = labelOrId(itemID, lang) | |||
return label | |||
end | |||
end | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- label has the qid of a Wikidata entity passed as the first unnamed parameter or as |qid= | ||
-- | -- if no qid is supplied, it uses the qid associated with the current page. | ||
-- It returns the Wikidata label for | -- 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: | -- Dependencies: none | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.label = function(frame) | ||
local qid = mw.text.trim(frame.args[1] or frame.args.qid or "") | |||
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 | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getAT (Article Title) | ||
-- If | -- 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. | p.getAT = function(frame) | ||
local | local itemID = mw.text.trim(frame.args[1] or frame.args.qid or "") | ||
if itemID == "" then return end | |||
if | return mw.wikibase.getSitelink(itemID) | ||
end | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getDescription has the qid of a Wikidata entity passed as |qid= | ||
-- | -- (it defaults to the associated qid of the current article if omitted) | ||
-- | -- and a local parameter passed as the first unnamed parameter. | ||
-- | -- Any local parameter passed (other than "Wikidata" or "none") becomes the return value. | ||
-- | -- 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: | -- Dependencies: none | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getDescription = function(frame) | ||
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 | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getAliases has the qid of a Wikidata entity passed as |qid= | ||
-- It | -- (it defaults to the associated qid of the current article if omitted) | ||
-- the | -- and a local parameter passed as the first unnamed parameter. | ||
-- the | -- It implements blacklisting and whitelisting with a field name of "alias" by default. | ||
-- the | -- Any local parameter passed becomes the return value. | ||
-- Otherwise it returns the aliases for the Wikidata entity with the usual list options. | |||
-- Nothing is returned if the aliases do not exist. | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: findLang | -- Dependencies: findLang(); assembleoutput() | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getAliases = function(frame) | ||
local args = frame.args | |||
local fieldname = args.name or "" | |||
if fieldname == "" then fieldname = "alias" end | |||
local blacklist = args.suppressfields or args.spf or "" | |||
if blacklist:find(fieldname) then return nil end | |||
local | |||
end | |||
local localval = mw.text.trim(args[1] or "") | |||
if localval ~= "" then return localval end | |||
local whitelist = args.fetchwikidata or args.fwd or "" | |||
if whitelist == "" then whitelist = "NONE" end | |||
if not (whitelist == 'ALL' or whitelist:find(fieldname)) then return nil end | |||
local qid = mw.text.trim(args.qid or "") | |||
if qid == "" then qid = nil end | |||
local entity = mw.wikibase.getEntity(qid) | |||
if not entity then return nil end | |||
local aliases = entity.aliases | |||
if not aliases then return nil end | |||
if not qid then qid= mw.wikibase.getEntityIdForCurrentPage() end | |||
args.langobj = findLang(args.lang) | |||
local langcode = args.langobj.code | |||
args.lang = langcode | |||
local out = {} | 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 | |||
for | end | ||
break | |||
end | |||
end | |||
end | end | ||
return assembleoutput(out, args, qid) | |||
end | 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. | p.pageId = function(frame) | ||
return mw.wikibase.getEntityIdForCurrentPage() | |||
end | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- formatDate is a wrapper to export the private function format_Date | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: | -- Dependencies: format_Date(); | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.formatDate = function(frame) | ||
return format_Date(frame.args[1], frame.args.df, frame.args.bc) | |||
end | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- location is a wrapper to export the private function _location | ||
-- or the | -- it takes the entity-id as qid or the first unnamed parameter | ||
-- | -- optional boolean parameter first toggles the display of the first item | ||
-- optional boolean parameter skip toggles the display to skip to the last item | |||
-- parameter debug=<y/n> (default 'n') adds error msg if not a location | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: | -- Dependencies: _location(); | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.location = function(frame) | ||
local | local debug = (frame.args.debug or ""):sub(1, 1):lower() | ||
local | if debug == "" then debug = "n" end | ||
local qid = mw.text.trim(frame.args.qid or frame.args[1] or ""):upper() | |||
return | if qid == "" then qid=mw.wikibase.getEntityIdForCurrentPage() end | ||
if not qid then | |||
if debug ~= "n" then | |||
return i18n.errors["entity-not-found"] | |||
else | |||
return nil | |||
end | |||
end | |||
local first = mw.text.trim(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 | ||
-- | -- returns true if the field is not blacklisted (i.e. allowed) | ||
-- | -- 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. | p.checkBlacklist = function(frame) | ||
local | local blacklist = frame.args.suppressfields or frame.args.spf or "" | ||
local fieldname = frame.args.name or "" | |||
if blacklist ~= "" and fieldname ~= "" then | |||
if blacklist:find(fieldname) then | |||
return false | |||
else | |||
return true | |||
end | |||
else | else | ||
-- one of the fields is missing: let's call that "not on the list" | |||
return true | |||
end | |||
end | |||
------------------------------------------------------------------------------- | |||
-- emptyor returns nil if its first unnamed argument is just punctuation, whitespace or html tags | |||
-- otherwise it returns the argument unchanged (including leading/trailing space). | |||
-- If the argument may contain "=", then it must be called explicitly: | |||
-- |1=arg | |||
-- (In that case, leading and trailing spaces are trimmed) | |||
-- It finds use in infoboxes where it can replace tests like: | |||
-- {{#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 | |||
------------------------------------------------------------------------------- | |||
p.emptyor = function(frame) | |||
local s = frame.args[1] or "" | |||
if s == "" then return nil end | |||
local sx = s:gsub("%s", ""):gsub("<[^>]*>", ""):gsub("%p", "") | |||
if sx == "" then | |||
return nil | |||
else | |||
return s | |||
end | end | ||
end | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- labelorid is a public function to expose the output of labelOrId() | ||
-- | -- Pass the Q-number as |qid= or as an unnamed parameter. | ||
-- | -- It returns the Wikidata label for that entity or the qid if no label exists. | ||
-- | ------------------------------------------------------------------------------- | ||
-- | -- Dependencies: labelOrId | ||
-- | |||
-- | |||
-- | |||
-- | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p.labelorid = function(frame) | |||
return (labelOrId(frame.args.qid or frame.args[1])) | |||
p. | |||
end | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- getLang returns the MediaWiki language code of the current content. | ||
-- If | -- If optional parameter |style=full, it returns the language name. | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: none | -- Dependencies: none | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getLang = function(frame) | ||
local | local style = (frame.args.style or ""):lower() | ||
local langcode = mw.language.getContentLanguage().code | |||
if style == "full" then | |||
return mw.language.fetchLanguageName( langcode ) | |||
end | |||
local | return langcode | ||
return | |||
end | 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: | -- Dependencies: _getItemLangCode() | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.getItemLangCode = function(frame) | ||
return _getItemLangCode(frame.args.qid or frame.args[1]) | |||
end | |||
------------------------------------------------------------------------------- | |||
-- findLanguage exports the local findLang() function | |||
-- It takes an optional language code and returns, in order of preference: | |||
-- the code if a known language; | |||
-- the user's language, if set; | |||
-- the server's content language. | |||
------------------------------------------------------------------------------- | |||
-- Dependencies: findLang | |||
------------------------------------------------------------------------------- | |||
p.findLanguage = function(frame) | |||
return findLang(frame.args.lang or frame.args[1]).code | |||
end | |||
------------------------------------------------------------------------------- | |||
-- getQid returns the qid, if supplied | |||
-- failing that, the Wikidata entity ID of the "category's main topic (P301)", if it exists | |||
-- failing that, the Wikidata entity ID associated with the current page, if it exists | |||
-- otherwise, nothing | |||
------------------------------------------------------------------------------- | |||
-- Dependencies: none | |||
------------------------------------------------------------------------------- | |||
p.getQid = function(frame) | |||
local qid = (frame.args.qid or ""):upper() | |||
-- check if a qid was passed; if so, return it: | |||
if qid ~= "" then return qid end | |||
-- check if there's a "category's main topic (P301)": | |||
end | qid = mw.wikibase.getEntityIdForCurrentPage() | ||
end | if qid then | ||
local prop301 = mw.wikibase.getBestStatements(qid, "P301") | |||
if prop301[1] then | |||
local mctid = prop301[1].mainsnak.datavalue.value.id | |||
if mctid then return mctid end | |||
end | |||
end | end | ||
-- otherwise return the page qid (if any) | |||
return qid | |||
end | |||
return | |||
end | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- followQid takes three optional parameters: qid, props, and all. | ||
-- | -- If qid is not given, it uses the qid for the connected page | ||
-- or returns nil if there isn't one. | |||
-- props is a list of properties, separated by punctuation. | |||
-- If props is given, the Wikidata item for the qid is examined for each property in turn. | |||
-- If that property contains a value that is another Wikibase-item, that item's qid is returned, | |||
-- and the search terminates, unless |all=y when all of the qids are returned, sparated by spaces. | |||
-- If props is not given, the qid is returned. | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: | -- Dependencies: parseParam() | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.followQid = function(frame) | ||
local | local qid = (frame.args.qid or ""):upper() | ||
local all = parseParam(frame.args.all, false) | |||
if qid == "" then | |||
qid = mw.wikibase.getEntityIdForCurrentPage() | |||
end | |||
if not qid then return nil end | |||
local out = {} | |||
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 | |||
if all then | |||
out[#out+1] = linkedid | |||
else | |||
return linkedid | |||
end -- test for all or just the first one found | |||
end -- test for value exists for that property | |||
end -- loop through values of property to follow | |||
end -- loop through list of properties to follow | |||
end | |||
if #out > 0 then | |||
return table.concat(out, " ") | |||
else | |||
return qid | |||
end | |||
end | |||
------------------------------------------------------------------------------- | |||
-- globalSiteID returns the globalSiteID for the current wiki | |||
-- e.g. returns "enwiki" for the English Wikipedia, "enwikisource" for English Wikisource, etc. | |||
------------------------------------------------------------------------------- | |||
-- Dependencies: none | |||
------------------------------------------------------------------------------- | |||
p.globalSiteID = function(frame) | |||
return mw.wikibase.getGlobalSiteId() | |||
return | |||
end | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- siteID returns the root of the globalSiteID | ||
-- | -- e.g. "en" for "enwiki", "enwikisource", etc. | ||
-- treats "en-gb" as "en", etc. | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: none | -- Dependencies: none | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.siteID = function(frame) | ||
local | 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 | |||
local pos = txtlang:find("-") | |||
local ret = "" | |||
if pos then | |||
ret = txtlang:sub(1, pos-1) | |||
else | |||
ret = txtlang | |||
end | |||
return ret | |||
end | |||
local | ------------------------------------------------------------------------------- | ||
if | -- projID returns the code used to link to the reader's language's project | ||
-- e.g "en" for [[:en:WikidataIB]] | |||
local | -- treats "en-gb" as "en", etc. | ||
------------------------------------------------------------------------------- | |||
-- Dependencies: none | |||
local | ------------------------------------------------------------------------------- | ||
p.projID = 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 | |||
local pos = txtlang:find("-") | |||
local ret = "" | |||
if pos then | |||
ret = txtlang:sub(1, pos-1) | |||
else | |||
ret = txtlang | |||
end | end | ||
return ret | |||
return | |||
end | end | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- formatNumber formats a number according to the the supplied language code ("|lang=") | ||
-- | -- or the default language if not supplied. | ||
-- The number is the first unnamed parameter or "|num=" | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- Dependencies: | -- Dependencies: findLang() | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
p. | p.formatNumber = function(frame) | ||
local | local lang | ||
local num = tonumber(frame.args[1] or frame.args.num) or 0 | |||
lang = findLang(frame.args.lang) | |||
return lang:formatNum( num ) | |||
end | |||
------------------------------------------------------------------------------- | |||
-- examine dumps the property (the unnamed parameter or pid) | |||
-- from the item given by the parameter 'qid' (or the other unnamed parameter) | |||
-- or from the item corresponding to the current page if qid is not supplied. | |||
-- e.g. {{#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 | |||
------------------------------------------------------------------------------- | |||
p.examine = function( frame ) | |||
local args | |||
if frame.args[1] or frame.args.pid or frame.args.qid then | |||
args = frame.args | |||
else | |||
args = frame:getParent().args | |||
end | end | ||
local par = {} | |||
local pid = (args.pid or ""):upper() | |||
local qid = (args.qid or ""):upper() | |||
par[1] = mw.text.trim( args[1] or "" ):upper() | |||
par[2] = mw.text.trim( args[2] or "" ):upper() | |||
------------------------------------------------------------------------------- | table.sort(par) | ||
if par[2]:sub(1,1) == "P" then par[1], par[2] = par[2], par[1] end | |||
if pid == "" then pid = par[1] end | |||
if qid == "" then qid = par[2] end | |||
local q1 = qid:sub(1,1) | |||
if pid:sub(1,1) ~= "P" then return "No property supplied" end | |||
if q1 ~= "Q" and q1 ~= "M" then qid = mw.wikibase.getEntityIdForCurrentPage() end | |||
if not qid then return "No item for this page" end | |||
return "<pre>" .. mw.dumpObject( mw.wikibase.getAllStatements( qid, pid ) ) .. "</pre>" | |||
end | |||
------------------------------------------------------------------------------- | |||
-- checkvalue looks for 'val' as a wikibase-item value of a property (the unnamed parameter or pid) | |||
-- from the item given by the parameter 'qid' | |||
------------------------------------------------------- | -- or from the Wikidata item associated with the current page if qid is not supplied. | ||
-- If property is not supplied, then P31 (instance of) is assumed. | |||
-- It returns val if found or nothing if not found. | |||
-- e.g. {{#invoke:WikidataIB |checkvalue |val=Q5 |pid=P31 |qid=Q42}} | |||
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |P31 |qid=Q42}} | |||
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |qid=Q42}} | |||
-- or {{#invoke:WikidataIB |checkvalue |val=Q5 |P31}} for the current page. | |||
------------------------------------------------------------------------------- | |||
-- Dependencies: none | |||
------------------------------------------------------------------------------- | |||
p.checkvalue = function( frame ) | |||
local args | |||
if frame.args.val then | |||
args = frame.args | |||
else | |||
args = frame:getParent().args | |||
end | |||
local val = args.val | |||
if not val then return nil end | |||
local pid = mw.text.trim(args.pid or args[1] or "P31"):upper() | |||
local qid = (args.qid or ""):upper() | |||
if pid:sub(1,1) ~= "P" then return nil end | |||
if qid:sub(1,1) ~= "Q" then qid = mw.wikibase.getEntityIdForCurrentPage() end | |||
if not qid then return nil end | |||
local 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("(.+) (.+)") | |||
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> " .. 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, "; ") | |||
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 | end | ||
return default | |||
end | end | ||
Line 2,928: | Line 3,365: | ||
-- List of exported functions | -- List of exported functions | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- getValue | --[[ | ||
_getValue | |||
getValue | |||
getPreferredValue | |||
getCoords | |||
getQualifierValue | |||
getSumOfParts | |||
getValueByQual | |||
getValueByLang | |||
getValueByRefSource | |||
getPropertyIDs | |||
getQualifierIDs | |||
getPropOfProp | |||
getAwardCat | |||
getIntersectCat | |||
getGlobe | |||
getCommonsLink | |||
getSiteLink | |||
getLink | |||
getLabel | |||
label | |||
getAT | |||
getDescription | |||
getAliases | |||
pageId | |||
formatDate | |||
location | |||
checkBlacklist | |||
emptyor | |||
labelorid | |||
getLang | |||
getItemLangCode | |||
findLanguage | |||
getQID | |||
followQid | |||
globalSiteID | |||
siteID | |||
projID | |||
formatNumber | |||
examine | |||
checkvalue | |||
-- | url2 | ||
getWebsite | |||
getAllLabels | |||
getAllDescriptions | |||
getAllAliases | |||
showNoLinks | |||
checkValidity | |||
getEntityFromTitle | |||
getDatePrecision | |||
--]] | |||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |