Module:Sandbox/MSGJ
Jump to navigation
Jump to search
require('strict')
local p = {}
local fetchNested = function(outer, nestedIndex)
if not outer or type(outer)~='table' or not nestedIndex or type(nestedIndex)~='table' then
return nil
end
local current = outer
for _, indx in ipairs(nestedIndex) do
current = current[indx]
if not current then
return current
end
end
return current
end
local getValue = function(snak)
if snak and snak.snaktype=='value' then -- value defined
if snak.datatype=='wikibase-item' then
return fetchNested(snak, {'datavalue', 'value', 'id'})
elseif snak.datatype=='string' then
return fetchNested(snak, {'datavalue', 'value'})
elseif snak.datatype=='time' then
return fetchNested(snak, {'datavalue', 'value', 'time'})
end
end
end
p.main = function(frame)
local prop = fetchNested(mw.wikibase.getBestStatements(frame.args.qid, 'P225'), {1})
if fetchNested(prop, {'mainsnak', 'snaktype'})~='value' then
return 'no snak'
end
local out = getValue(prop.mainsnak)
if not out then
return nil
end
local authors = {}
if prop.qualifiers and prop.qualifiers['P405'] then
for _, author in ipairs(prop.qualifiers['P405']) do
local qid = getValue(author)
if qid then
local surname_c = fetchNested(mw.wikibase.getBestStatements(qid, 'P734'), {1})
if getValue(surname_c) then -- family named defined in Wikidata
table.insert(authors, mw.wikibase.renderSnak(surname_c.mainsnak))
else -- use label of author
local name = mw.wikibase.renderSnak(author)
if name and name~='' then
if mw.ustring.find(name, '%s') then -- label contains multiple words
table.insert(authors, mw.ustring.match(name, '%s(%w+)$'))
else -- label is single word
table.insert(authors, name)
end
end
end
end
end
end
if getValue(fetchNested(prop, {'qualifiers', 'P574', 1})) then
local year = mw.wikibase.renderSnak(fetchNested(prop, {'qualifiers', 'P574', 1}))
table.insert(authors, year)
end
out = '<i>' .. out .. '</i> ' .. table.concat(authors, ', ')
return out
end
return p