Modulo:CreateStatement
Inpostasion de letura
local p = {}
local SA = require "Module:SimpleArgs"
local SD = require "Module:SimpleDebug"
local WD = require "Module:Wikidades"
function FromWbEntity (value)
local Char = ''
if value["entity-type"] == "item" then
Char = 'Q'
else -- == "property",
Char = 'P'
end
return '{{'..Char..'-|'..value["numeric-id"]..'}}'
end --FromWbEntity
function p.main (frame)
local args, NArgs = SA.GetArgs (frame)
if NArgs == 0 then return end
local qid = SA.RStr_Par (args, 'item')
local pid = SA.RStr_Par (args, 'property')
local long_names = SA.Bool_Par (args, 'long_names', false)
local S = '{{Statement+<br/>'
S = S..'|P = {{P-|'..string.sub(pid,2)..'}}<br/><small>{{label|'..qid..'}}</small><br/>'
tab = mw.wikibase.getAllStatements (qid, pid)
local v = 1
local q = 1
local val = ''
local qualifiers = nil
local qualorder = nil
local references = nil
local reforder = nil
function prepQ (id)
return '{{Q-|'..string.sub(id,2)..'}}'
end
function forValue (datatype, value)
local val1 = ''
local val2 = ''
if datatype == 'string' then
val1 = value
elseif datatype == 'monolingualtext' then
local lang = value["language"]
val1 = value["text"]..' ('..lang..')'
elseif datatype == "quantity" then
val1 = tonumber(value["amount"])
if (val1 > 999) or (val1 ~= math.floor(val1)) then
val1 = '{{Nts|'..val1..'}}'
end
local unit = value["unit"]
if unit == '1' then
unit = ''
else
unit = string.sub (unit, (#unit - unit:reverse():find("/"))+2)
unit = '{{label|'..unit..'}}'
end
if unit ~= '' then
val1 = val1.. ' '..unit
end
elseif datatype == 'time' then
val1 = value["time"]
precision = value["precision"]
val1 = string.sub(val1, 1, precision)
if precision == 11 then
val1 = '{{date|'..string.sub(val1,2)..'}}'
else
val1 = string.sub(val1, 1, 5)
val1 = tonumber (val1)
end
local calmod = value["calendarmodel"]
if calmod ~= nil then
calmod = string.sub (calmod, (#calmod - calmod:reverse():find("/"))+2)
--[[]
if calmod == 'Q1985727' then
calmod = '{{int:Wikibase-time-calendar-gregorian}}'
else
calmod = '{{label|'..calmod..'}}'
end
val1 = val1..' <sup><i>'..calmod..'</i></sup>'
--]]
if calmod ~= 'Q1985727' then
val1 = val1..' <sup><i>{{label|'..calmod..'}}</i></sup>'
end
end
elseif datatype == 'globecoordinate' then
val1 = '{{coord|'..value["latitude"]..'|'..value["longitude"]..'}}'
elseif datatype == 'wikibase-entityid' then
val1 = FromWbEntity (value)
end
return val1, val2
end --forValue
function forDatavalue (dv, char1, char2, AsProp)
if dv == nil then return end
local val1, val2 = forValue (dv["type"], dv["value"])
local ini = char1..v
if char1 == 'r' then
ini = 'v'..v..'.'..ini
end
local SNow = '|'..ini
if AsProp then
SNow = SNow..' = '..val1..'<br/>'
else
SNow = SNow..'.'..char2..q..'.v1='..val1..'<br/>'
if val2 ~= '' then
SNow = SNow..'.'..char2..q..'.v2='..val2..'<br/>'
end
q = q + 1
end
S = S..SNow
end --forDatavalue
function forDatavalueProp (dv, z)
local val1, val2 = forValue (dv["type"], dv["value"])
local SNow = '|v'..v..'.q'..q..'.v'..z..'='..val1..'<br/>'
S = S..SNow
end --forDatavalueProp
function forRef ()
if reforder == nil then
else
for c, d in pairs(reforder) do
forp = references[d][1]["datavalue"]
S = S..'|v'..v..'.r'..v..'.p'..q..' = {{P-|'..string.sub(d,2)..'}}<br/>'
forDatavalue (forp, 'r', 'p')
end
end
end --forRef
local Docprop = {
p = {'property', 'p', 0},
e = {'example', 'e', 0},
qid = {'case', 'qid', 0},
m = {'monovalue', 'm', 2, 'Q19474404', 'Q21510857'},
s = {'symmetric', 's', 1, 'Q21510862'},
u = {'units', 'u', 1, 'Q21514353'},
r = {'reference', 'r', 1, 'Q54554025'},
i = {'inverse', 'i', 1, 'Q21510855'},
o = {'one_of', 'o', 1, 'Q21510859'},
c = {'contemporary','c', 1, 'Q25796498'},
q = {'qualifier', 'q', 0, 'Q21510851'},
d = {'description', 'd', 0},
}
---begin main---
for a, b in pairs(tab) do
q = 1
if b["qualifiers"] ~= nil then
local qualis = b["qualifiers"][1]
qualorder = b["qualifiers-order"]
qualifiers = b["qualifiers"]
end
if b["references"] ~= nil then
local refs = b["references"][1]
reforder = refs["snaks-order"]
references = refs["snaks"]
end
local datatype = b["mainsnak"]["datatype"]
local datavalue = b["mainsnak"]["datavalue"]
local Isdv = false
if datatype == nil then
datatype = b["dv"]["datatype"]
Isdv = true
end
if datatype == "wikibase-property" then
if qualorder == nil then
else
S = S..'|v'..v..'= '..FromWbEntity (datavalue["value"])..'<br/>'
for c, d in pairs(qualorder) do
S = S..'|v'..v..'.q'..q..'.p = {{P-|'..string.sub(d,2)..'}}<br/>'
local z = 1
for cc, dd in ipairs(qualifiers[d]) do
forp = dd["datavalue"]
forDatavalueProp (forp, z)
z = z + 1
end
q = q + 1
end
end
elseif datatype == "wikibase-item" then
local id = datavalue["value"]["id"]
S = S..'|v'..v..'= '..prepQ(id)..'<br/>'
local forp = ''
if qualorder == nil then
forDatavalue (datavalue, 'v', 'q')
else
for c, d in pairs(qualorder) do
forp = qualifiers[d][1]["datavalue"]
S = S..'|v'..v..'.q'..q..'.p = {{P-|'..string.sub(d,2)..'}}<br/>'
forDatavalue (forp, 'v', 'q')
end
end
elseif datatype == "commonsMedia" then
local name = datavalue["value"]
local media = 'File:'..name..'|thumb|left|'..name
S = S..'|v'..v..' = '..media..'<br/>'
else
if Isdv then
forDatavalue (b["dv"]["datavalue"], 'v', 'q', true)
else --external-id, quantity
local forp = datavalue
if forp == nil then
--unknown value
else
if qualorder == nil then
forDatavalue (forp, 'v', 'q', true)
else
local val = forValue (forp["type"], forp["value"])
S = S..'|v'..v..'= '..val..'<br/>'
for c, d in pairs(qualorder) do
forp = qualifiers[d][1]["datavalue"]
S = S..'|v'..v..'.q'..q..'.p = {{P-|'..string.sub(d,2)..'}}<br/>'
forDatavalue (forp, 'v', 'q')
end
end
end
end
end
q = 1
forRef ()
v = v + 1
end
function ItemName (what)
if long_names then
return Docprop[what][1]
else
return Docprop[what][2]
end
end
local doc = '{{DocInfobox docStatement |doc = {{{doc|}}} |'..ItemName('p')..'='..pid..' |'..ItemName('e')..'={{DocInfobox prefix}}/doc/'..pid
doc = doc..' |'..ItemName('qid')..'='..qid
local lines = WD.claim{item=pid, property='P2302', formatting='raw', separator=',', editicon='false'}
if lines ~= nil then
lines = mw.text.split(mw.text.trim(lines), ',')
for k, v in pairs (Docprop) do
if v[3] > 0 then
for kk, vv in ipairs (lines) do
if v[3] == 2 then
if vv == v[4] then
doc = doc..' |'..ItemName(k)..'=1'
break
elseif vv == v[5] then
doc = doc..' |'..ItemName(k)..'=2'
break
end
elseif vv == v[4] then
doc = doc..' |'..ItemName(k)..'=y'
break
end
end
end
end
for kk, vv in ipairs (lines) do
if vv == Docprop['q'][4] then
lines = WD.claim {item=pid, property='P2302', qualifier='P2306',
formatting='table', whitelist0='Q21510851', rowformat='$1', colformat1='raw', editicon='false'}
doc = doc..' |'..ItemName('q')..'='..lines
break
end
end
local subdoc = ''
function WDHere (whitelist0, qualifier, labchar)
local lin = WD.claim {item=pid, property='P2302', qualifier=qualifier,
formatting='table', whitelist0=whitelist0, rowformat='$1', colformat1='raw', editicon='false'}
if lin ~= nil then
local sep = ''
if labchar == 'p' then
sep = '<br />'
else --labchar == 'q' then
sep = ', '
end
lin = mw.text.split(mw.text.trim(lin), sep)
local labels = ''
for j, w in ipairs (lin) do
if labels ~= '' then
labels = labels..','
end
labels = labels..' {{'..labchar..'-|'..w..'}}'
end
subdoc = subdoc..'{{Button ST|red}} {{label|'..whitelist0..'}} →'..labels..'<br>'
end
end
line = WDHere ('Q21503247', 'P2306', 'p')
line = WDHere ('Q21503250', 'P2308', 'q')
line = WDHere ('Q21510865', 'P2308', 'q')
if subdoc ~= '' then
doc = doc..' |'..ItemName('d')..'='..'<small><br>'..subdoc..'</small>'
end
end
S = S..'|doc = '..doc..'}}'
S = S..'<br/>}}'
return S
end --main
return p