Module:WikidataIB/sandbox1: Difference between revisions

Content deleted Content added
rm marker
sync with sandbox
Line 51:
["abbr"] =
{
["Q828224"] = "km",
["Q11573"] = "m",
["Q174728"] = "cm",
["Q174789"] = "mm",
["Q11570Q712226"] = "kgsq km",
["Q41803Q11570"] = "gkg",
["Q3241121Q41803"] = "mgg",
["Q2332346Q3241121"] = "mlmg",
["Q7727Q2332346"] = "minml",
["Q7727"] = "min",
["Q11574"] = "s",
},
}
Line 242 ⟶ 244:
-- sourced takes a table representing a statement that may or may not have references
-- it counts how many references are sourced to something not containing the word "wikipedia"
-- the reference string "ref" is available for debugging
-- it returns a boolean = true if there are any sourced references.
local sourced = function(claim)
local refs = 0
if claim.references then
for kr, vr in pairs(claim.references) do
local ref = mw.wikibase.renderSnaks(vr.snaks)
if not ref:find("Wikipedia") then refs = refs + 1 end
return true
end
end
end
end
return refs > 0
 
 
-------------------------------------------------------------------------------
-- setRanks takes a flag (parameter passed) that requests the values to return
-- "b[est]" returns preferred if available, otherwise normal
-- "p[referred]" returns preferred
-- "n[ormal]" returns normal
-- "d[eprecated]" returns deprecated
-- multiple values are allowed, e.g. "preferred normal" (which is the default)
-- "best" will override the other flags, and set p and n
local function setRanks(rank)
rank = (rank or ""):lower()
-- if nothing passed, return preferred and normal
-- if rank == "" then rank = "p n" end
local ranks = {}
for w in string.gmatch(rank, "%a+") do
w = w:sub(1,1)
if w == "b" or w == "p" or w == "n" or w == "d" then
ranks[w] = true
end
end
-- check if "best" is requested or no ranks requested; and if so, set preferred and normal
if ranks.b or not next(ranks) then
ranks.p = true
ranks.n = true
end
return ranks
end
 
Line 261 ⟶ 290:
-- it returns (1) either the qid or nil indicating whether or not the call should continue
-- and (2) a table containing all of the statements for the propertyID and relevant Qid
-- if "best" ranks are requested, it returns those instead of all ranks
local parseInput = function(frame, input_parm, property_id)
-- There may be a local parameter supplied, if it's blank, set it to nil
Line 307 ⟶ 337:
 
-- See what's on Wikidata (the call always returns a table, but it may be empty):
local props = mw.wikibase.getAllStatements(qid, property_id){}
if args.reqranks.b then
props = mw.wikibase.getBestStatements(qid, property_id)
else
props = mw.wikibase.getAllStatements(qid, property_id)
end
if props[1] then
return qid, props
Line 381 ⟶ 416:
-------------------------------------------------------------------------------
-- propertyvalueandquals takes a property object, the arguments passed from frame,
-- the prefered/normal filter function, and a qualifier propertyID.
-- It returns a sequence (table) of values representing the values of that property
-- and qualifiers that match the qualifierID if supplied.
--
local function propertyvalueandquals(objproperty, args, filter, qualID)
 
-- onlysourced is a boolean passed to return only values sourced to other than Wikipedia
Line 444 ⟶ 479:
local datavalue = snak.datavalue
datavalue = datavalue and datavalue.value
if (not filter(v)) or (onlysrc and not sourced(v)) then
-- nothing added to 'out': it --isn't debug:sourced out[#out + 1]when onlysourced= "nosrc"true
------------------------------------
-- either isn't preferred when prefered values are requested
elseif v.rank == "deprecated" and not args.reqranks.d then
-- or isn't sourced when onlysourced=true
-- nothing added to 'out': value is deprecated but not requested
------------------------------------
elseif v.rank == "normal" and not args.reqranks.n then
-- nothing added to 'out': value is normal but not requested
------------------------------------
elseif v.rank == "preferred" and not args.reqranks.p then
-- nothing added to 'out': value is preferred but not requested
------------------------------------
elseif snak.snaktype == "somevalue" then -- value is unknown
Line 523 ⟶ 565:
if dateprecision >= 11 then -- prepend day
fpvdate = tonumber(timestamp:sub(10, 11)) .. " " .. fpvdate
end
if timestamp:sub(1, 1) == "-" then
fpvdate = fpvdate .. " BCE"
end
end
if timestamp:sub(1, 1) == "-" then
fpvdate = fpvdate .. " BCE"
end
fdate = format_Date(fpvdate, dateformat, args.bc)
-- testing the ability to add "circa" from P1480 "sourcing circumstances"
-- just testing on years for now
if dateprecision == 9 and v.qualifiers then
local sc = v.qualifiers.P1480
if sc then
local circa = ""
for k1, v1 in pairs(sc) do
if v1.datavalue.value.id == "Q5727902" then
circa = '<abbr title="circa">c.</abbr>&nbsp;'
break
end
end
fdate = circa .. fdate
end
end
elseif dateprecision == 7 then -- century
local century = math.floor((fpvdate - 1) / 100) + 1
Line 663 ⟶ 720:
}
if qualID == "DATES" then qargs.maxvals = 1 end
local function qf(dummy)
return true
end
local qlist = {}
local t1, t2, dsep = "", "", ""
Line 672 ⟶ 726:
if qualID == "DATES" then
if k1 == "P580" then -- P580 is "start time"
t1 = propertyvalueandquals(v1, qargs, qf)[1]
elseif k1 == "P582" then -- P582 is "end time"
t2 = propertyvalueandquals(v1, qargs, qf)[1]
end
-- check for latest date qualifier:
Line 681 ⟶ 735:
-- otherwise see if we want this qualifier:
elseif qualID == "ALL" or qualID == k1 then
local ql = propertyvalueandquals(v1, qargs, qf)
for k2, v2 in ipairs(ql) do
qlist[#qlist + 1] = v2
Line 714 ⟶ 768:
local qualID = mw.text.trim(frame.args.qual or ""):upper()
if qualID == "" then qualID = nil end
 
-- create a filter function
--
-- rank is a parameter passed, which indicates "preferred" or "normal"
--
local rank = (frame.args.rank or ""):lower()
if rank:sub(1,1) == "p" then
rank = "preferred"
elseif rank:sub(1,1) == "n" then
rank = "normal"
else
rank = ""
end
 
local function filter(claim)
return true
end
 
if rank > "" then
local rankflag = false
for k, v in pairs(props) do
if v.rank == rank then
rankflag = true
break
end
end
function filter(claim)
return claim.rank == rank or rankflag == false
end
end
 
-- table 'out' stores the return value(s):
local out = propertyvalueandquals(props, frame.args, filter, qualID)
 
-- format the table of values and return it as a string:
Line 771 ⟶ 795:
 
local propertyID = mw.text.trim(frame.args[1] or "")
 
frame.args.reqranks = setRanks(frame.args.rank)
 
local entityid, props = parseInput(frame, frame.args[2], propertyID)
 
if not entityid then
return props -- either the input parameter or nothing
end
 
--[[
local function filter(claim)
return true
end
--]]
return _getvalue(frame, props, propertyID, entityid)
end
Line 789 ⟶ 812:
-- (or a comma separated list of them if multiple values exist).
-- If preferred ranks are set, it will return those values, otherwise values with normal ranks
-- now redundant to getValue with |rank=best
--
p.getPreferredValue = function(frame)
frame.args.rank = "preferredbest"
return p.getValue(frame)
end
Line 800 ⟶ 823:
-- (or a comma separated list of them if multiple values exist).
-- If normal ranks are set, it will return those values, otherwise all values
-- now redundant to getValue with |rank=normal
--
p.getNormalValue = function(frame)
frame.args.rank = "normal"
Line 875 ⟶ 898:
-- if "false" or "no" or 0 is passed set it false
local onlysrc = parseParam(frame.args.onlysourced or frame.args.osd, true)
 
-- set the requested ranks flags
frame.args.reqranks = setRanks(frame.args.rank)
 
-- check for locally supplied parameter in second unnamed parameter
Line 895 ⟶ 921:
local quals = v1.qualifiers[qualifierID]
if quals then
local functionvals filter= propertyvalueandquals(claimquals, frame.args, qid)
return true
end
local vals = propertyvalueandquals(quals, frame.args, filter)
for k, v in pairs(vals) do
out[#out + 1] = v