Changes

Jump to navigation Jump to search
1,756 bytes added ,  10:23, 29 September 2020
restore oen icon code and update stripping of trailing / per talk
-- Version: 2020-09-24
-- Module to implement use of a blacklist and whitelist for infobox fields
-- Can take a named parameter |qid which is the Wikidata ID for the article
-- shortname is boolean switch to use P1813 (short name) instead of label if true.
-- lang is the current language code.
-- uselbl is boolean switch to display the label
-------------------------------------------------------------------------------
-- Dependencies: labelOrId(); donotlink[]
-------------------------------------------------------------------------------
local linkedItem = function(id, lprefix, lpostfix, prefix, postfix, dtxt, shortname, lang, uselbl)
lprefix = lprefix or "" -- toughen against nil values passed
lpostfix = lpostfix or ""
if sitelink then
if not (dtxt or shortname) then
-- if sitelink and label are the same except for case, no need to process further if sitelink:lower() ~= label:lower() then -- strip any namespace or dab from the sitelink 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(",.+$", "") -- if uselbl is false, use sitelink instead of label if not uselbl then -- use slink as display, 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
-- 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
-- no sitelink and no label, so return whatever was returned from labelOrId for now
-- add tracking category [[Category:Articles with missing Wikidata information]]
-- 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
else
for kr, vr in pairs(claim.references) do
local ref = mw.wikibase.renderSnaks(vr.snaks)
if not ref:find("WikipediaWiki") then
return true
end
if #out > 0 then
if sorted then table.sort(out) end
-- if there's something to display and a pen icon is wanted , add it the end of the last value local hasdisplay = false 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)
end
local shortname = args.shortname or args.sn
shortname = parseParam(shortname, false)
local uselbl = args.uselabel or args.uselbl
uselbl = parseParam(uselbl, false)
local snak = propval.mainsnak or propval
local dtype = snak.datatype
local qnumber = dv.id
if linked then
val = linkedItem(qnumber, lpre, lpost, pre, post, dtxt, shortname, args.lang, uselbl)
else -- no link wanted so check for display-text, otherwise test for lang code
local label, islabel
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
-- 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 a list of returned values for this property
-- mlt[] holds the language code if the datatype is monolingual text
local out = {}
local mlt = {}
 
for k, v in ipairs(objproperty) do
local hasvalue = true
-- See if qualifiers are to be returned:
local snak = v.mainsnak or v
if hasvalue and v.qualifiers and qualID ~= "" and snak.snaktype~="novalue" then local qsep = (args.qsep or ""):gsub('"', '') 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. -- collect all wanted qualifier values returned in qlist, ["sep"] = qsep, ["langobj"] = args.langobj, ["lang"] = args.langobj.code, ["df"] = args.qdf, ["sn"] = parseParam(args.qsn or args.qshortname, false), }indexed by propertyID
local qlist = {}
local t1timestart, t2 timeend = "", "" -- see if we want all qualifiers if qualID == "ALL" then if v["loop through qualifiers-order"] then -- the values in the order table are the keys for the qualifiers table: for k1, v1 in ipairspairs(v[".qualifiers-order"]) do if v1 allflag or qwanted[k1] then if k1 == "P1326" then local ts = v.qualifiers[v1][1].datavalue.value.time local dp = v.qualifiers[v1][1].datavalue.value.precision qlist[#qlist + 1k1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "before") elseif v1 k1 == "P1319" then local ts = v.qualifiers[v1][1].datavalue.value.time local dp = v.qualifiers[v1][1].datavalue.value.precision qlist[#qlist + 1k1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "after") else elseif k1 == "P580" then local q timestart = assembleoutput(propertyvalueandquals(v.qualifiers[v1], qargs), qargs) [1] or "" -- we already deal with circa via 'sourcing circumstances' if the datatype was treat only one start timeas valid -- circa may be either linked or unlinked *** internationalise later *** if datatype ~ elseif k1 = "time" or q ~= "circaP582" and not (type(q) then timeend == "string" and q:findpropertyvalueandquals("circa]]")v1, qargs) then qlist[#qlist + 1] = q end end or "" -- treat only one endtime as valid else -- are there cases where qualifiers-order doesn't exist? local ql q = assembleoutput(propertyvalueandquals(v.qualifiersv1, qargs) for k1, v1 in ipairs(qlqargs) do -- 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 v1 q ~= "circa" and not (type(v1q) == "string" and v1q:find("circa]]")) then qlist[#qlist + 1k1] = v1 end end end -- see if we want date/range elseif qualID == "DATES" then qargs.maxvals = 1 for k1, v1 in pairs(v.qualifiers) do if k1 == "P580" then -- P580 is "start time"q t1 = propertyvalueandquals(v1, qargs)[1] or "" elseif k1 == "P582" then -- P582 is " end time" t2 = propertyvalueandquals(v1, qargs)[1] or "" end end -- otherwise process qualID as a list of qualifiers else test for q in mw.text.gsplit(qualID, "%p") do -- split at punctuation and iteratewanted q = mw.text.trim(q):upper() -- remove whitespace and capitalise if q == "P1326" then -- latest date, so supply 'before' as well. Assume one date value. for k1, v1 in pairs(v.qualifiers) do if k1 == "P1326" then local ts = v1[1].datavalue.value.time local dp = v1[1].datavalue.value.precision qlist[#qlist + 1] = dateFormat(ts, dp, args.qdf, args.bc, pd, "", lang, "before") end end else for k1, v1 in pairs(v.qualifiers) do if k1 == q then local ql = propertyvalueandquals(v1, qargs) for k2, v2 in ipairs(ql) do qlist[#qlist + 1] = v2 end end end end end -- of loop through list of qualifiers in qualID end -- of testing for what qualID isset date separator local t = t1 timestart .. t2timeend
-- *** internationalise date separators later ***
local dsep = "–"
if t:find("%s") or t:find(" ") then dsep = " – " end
-- set the order for the list of qualifiers returned; start time and end time go last 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 > 0 [v2] end end if t ~= "" then qlistout[#qlistout+1] = timestart .. dsep .. timeend end local qstr = assembleoutput(qlistqlistout, qargs)
if qualsonly then
out[#out+1] = qstr
out[#out] = out[#out] .. " (" .. qstr .. ")"
end
elseif t > ~= "" then
if qualsonly then
out[#out+1] = t1 timestart .. dsep .. t2timeend
else
out[#out] = out[#out] .. " (" .. t1 timestart .. dsep .. t2 timeend .. ")"
end
end
end
-- check if it's an instance of (P31) a country (Q6256) or sovereign state (Q3624078) -- and terminate the chain if it is
local inst = mw.wikibase.getAllStatements(qid, "P31")
if #inst > 0 then
local instid = v.mainsnak.datavalue.value.id
-- stop if it's a country (or a country within the United Kingdom if skip is true)
if instid == "Q6256" or instid == "Q3624078" or (skip and instid == "Q3336843") then
prop = nil -- this will ensure this is treated as top-level location
break
end
elseif qtype == "url" then
if vqualifiers[v1][1].snaktype == "value" 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
end
else
-- 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 sitelinkor qid supplied, it returns nothing.
-------------------------------------------------------------------------------
-- Dependencies: none
-- from the item given by the parameter 'qid'
-- or from the Wikidata item associated with the current page if qid is not supplied.
-- It only checks ranks that are requested (preferred and normal by default)
-- If property is not supplied, then P31 (instance of) is assumed.
-- It returns val if found or nothing if not found.
if qid:sub(1,1) ~= "Q" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return nil end
ranks = setRanks(args.rank) local stats = {} if ranks.b then stats = mw.wikibase.getBestStatements(qid, pid) else stats = mw.wikibase.getAllStatements( qid, pid ) end
if not stats[1] then return nil end
if stats[1].mainsnak.datatype == "wikibase-item" then
for k, v in pairs( stats ) do
ms = v.mainsnak if ranks[v.mainsnakrank:sub(1,1)] and ms.snaktype == "value" and v.mainsnakms.datavalue.value.id == val then
return val
end
-- 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
prot = prot or url
addr = addr or ""
-- strip trailing / from end of domain-only url and add <wbr/> before . and / local disp, n = addr:gsub( "^([^/]+)/$", "%1" ):gsub("%/", "<wbr/>/"):gsub("%.", "<wbr/>%.")
return '<span class="url">[' .. prot .. addr .. " " .. disp .. "]</span>&nbsp;" .. icon
end
end
end
 
-------------------------------------------------------------------------------
-- 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 nil 0 (or the second unnamed parameter) if it the Wikidata does not exist.
-------------------------------------------------------------------------------
-- Dependencies: parseParam; sourced;
-------------------------------------------------------------------------------
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 nil 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)
if prec then return prec end
end
return default
end
Anonymous user

Navigation menu