Module:Sandbox/RexxS/Concat
< Module:Sandbox | RexxS
local p = {}
p.test = function(frame)
local mytable = {}
for i=1,5 do
mytable[i] = i*10
end
return table.concat(mytable, ", ")
end
-- looking at Richard Burton Q151973 and his spouses P26
-- using {{#invoke:Sandbox/RexxS/Concat|getPropertyAndQualifier|P26|P580|FETCH_WIKIDATA}}
-- try {{#invoke:Wikidata|getRawQualifierValue|P26|P580|FETCH_WIKIDATA}} - which fails
-- see https://www.mediawiki.org/wiki/Wikibase/Notes/JSON
-- see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
-- see https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual
-- see https://en.wikipedia.org/wiki/Richard_Burton
-- see https://www.wikidata.org/wiki/Q151973
-- Also see {{#invoke:Sandbox/RexxS/Concat|getPropertyAndQualifier|P6|P580|FETCH_WIKIDATA}} for https://en.wikipedia.org/wiki/New_York_City
p.getPropertyAndQualifier = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local qualifierID = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[3] or "")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntityObject()
local props = entity.claims[propertyID]
if props then
local val, sitelink, label
for k, v in pairs(props) do
if v.mainsnak.snaktype == 'value' then
if v.mainsnak.datavalue.type == 'wikibase-entityid' then
-- type is entity
sitelink = mw.wikibase.sitelink('Q' .. v.mainsnak.datavalue.value['numeric-id'])
label = mw.wikibase.label('Q' .. v.mainsnak.datavalue.value['numeric-id'])
if val then val = val .. ', ' .. label else val = label end
if v.qualifiers and v.qualifiers[qualifierID] then
for kq, vq in pairs(v.qualifiers[qualifierID]) do
if vq.datavalue.type == 'time' then
-- return the year
local dt = vq.datavalue.value.time
-- local dt = v.qualifiers[qualifierID][1].datavalue.value.time
if #dt > 26 then --date later than 99 CE
val = val .. ' (' .. string.sub(dt, 9, 12) .. ')'
else -- date earlier than 100 CE
val = val .. ' (' .. string.sub(dt, 9, 10) .. ')'
end
end
end -- next kq, vq
end
elseif v.mainsnak.datavalue.type == 'string' then
-- type is string
if val then val = val .. ', ' .. v.mainsnak.datavalue.value else val = v.mainsnak.datavalue.value end
elseif v.mainsnak.datavalue.type == 'globecoordinate' then
-- type is globecoordinate - do this later
if val then val = val .. ', ' .. 'globecoordinate' else val = 'globecoordinate' end
elseif v.mainsnak.datavalue.type == 'time' then
-- type is time - do this later
if val then val = val .. ', ' .. 'time' else val = 'time' end
elseif v.mainsnak.datavalue.type == 'quantity' then
-- type is quantity
local quant = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
quant = mw.ustring.gsub(quant, "(%d),(%d)", "%1%2") -- remove thousands separators
if val then val = val .. ', ' .. quant else val = quant end
else
-- type is not known (to be dealt with later)
if val then val = val .. ', ' .. 'unknown' else val = 'unknown' end
end
else -- mainsnaktype is unknown value or novalue
if val then val = val .. ', noval' else val= 'noval' end
end
end -- next k,v
if ret then return ret else return val end
else -- no props
return ""
end
else -- wikidata value not required so return local value
return input_parm
end
end
p.getRawPropertyValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntityObject()
local props = entity.claims[propertyID]
if props then
local val, sitelink, label
for k, v in pairs(props) do
if v.mainsnak.snaktype == 'value' then
if v.mainsnak.datavalue.type == 'wikibase-entityid' then
-- type is entity
sitelink = mw.wikibase.sitelink('Q' .. v.mainsnak.datavalue.value['numeric-id'])
label = mw.wikibase.label('Q' .. v.mainsnak.datavalue.value['numeric-id'])
if val then val = val .. ', ' .. label else val = label end
elseif v.mainsnak.datavalue.type == 'string' then
-- type is string
if val then val = val .. ', ' .. v.mainsnak.datavalue.value else val = v.mainsnak.datavalue.value end
elseif v.mainsnak.datavalue.type == 'globecoordinate' then
-- type is globecoordinate - do this later
if val then val = val .. ', ' .. 'globecoordinate' else val = 'globecoordinate' end
elseif v.mainsnak.datavalue.type == 'time' then
-- type is time - do this later
if val then val = val .. ', ' .. 'time' else val = 'time' end
elseif v.mainsnak.datavalue.type == 'quantity' then
-- type is quantity
local quant = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
quant = mw.ustring.gsub(quant, "(%d),(%d)", "%1%2") -- remove thousands separators
if val then val = val .. ', ' .. quant else val = quant end
else
-- type is not known (to be dealt with later)
if val then val = val .. ', ' .. 'unknown' else val = 'unknown' end
end
else -- mainsnaktype is unknown value or novalue
if val then val = val .. ', noval' else val= 'noval' end
end
end -- next k,v
return val
else -- no props
return ""
end
else -- wikidata value not required so return local value
return input_parm
end
end
-- {{#invoke:Sandbox/RexxS/Concat|getDescription|FETCH_WIKIDATA|en}}
-- {{#invoke:Sandbox/RexxS/Concat|getDescription|FETCH_WIKIDATA}}
p.getDescription = function(frame)
local input_parm = mw.text.trim(frame.args[1] or "")
local lang = mw.text.trim(frame.args[2] or mw.language.getContentLanguage().code) -- if language code not supplied, use lang code local to this wiki
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntityObject()
local desc = entity.descriptions[lang] -- check this exists to avoid index nul error
if desc then
return mw.ustring.upper(mw.ustring.sub(desc.value, 1, 1)) .. mw.ustring.sub(desc.value, 2)
else -- no description in given language
return ""
end
else -- wikidata value not required so return local value
return input_parm
end
end
return p