Module:Wikidata: Difference between revisions

no edit summary
(Created page with "--script that retrieves basic data stored in Wikidata, for the datamodel, see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua local p = {} local linguistic = re...")
 
No edit summary
 
Line 157: Line 157:


local function tableToText(values, params) -- takes a list of already formatted values and make them a text
local function tableToText(values, params) -- takes a list of already formatted values and make them a text
if not values then
if not values or #values == 0 then
return nil
return nil
end
end
Line 353: Line 353:


local function wikipediaLink(entity, lang)
local function wikipediaLink(entity, lang)
local link = entity:getSitelink(lang .. 'wiki')
local link
if type(entity) == 'table' then
link = entity:getSitelink(lang .. 'wiki')
else
link = mw.wikibase.getSitelink(entity, lang .. 'wiki')
end
if link then
if link then
return ':' .. lang .. ':' .. link
return ':' .. lang .. ':' .. link
Line 365: Line 370:
if entity.type == 'property' then
if entity.type == 'property' then
return 'd:P:' .. entity.id
return 'd:P:' .. entity.id
elseif entity.type == 'lexeme' then
return 'd:L:' .. entity.id
else
else
return 'd:' .. entity.id
return 'd:' .. entity.id
Line 371: Line 378:
if string.sub(entity, 1, 1) == 'P' then
if string.sub(entity, 1, 1) == 'P' then
return 'd:P:' .. entity
return 'd:P:' .. entity
elseif string.sub(entity, 1, 1) == 'L' then
return 'd:L:' .. entity
else
else
return 'd:' .. entity
return 'd:' .. entity
Line 451: Line 460:


local displayformat = params.displayformat
local displayformat = params.displayformat
local datatype = snak.datavalue.type
local valuetype = snak.datavalue.type
local value = snak.datavalue.value
local value = snak.datavalue.value


if datatype == 'wikibase-entityid' then
if valuetype == 'wikibase-entityid' then
if type(displayformat) == 'function' then
if type(displayformat) == 'function' then
return displayformat(snak, params)
return displayformat(snak, params)
Line 467: Line 476:
end
end


elseif datatype == 'string' then
elseif valuetype == 'string' then
local showntext = params.showntext
local showntext = params.showntext
if displayformat == 'weblink' then
if displayformat == 'weblink' then
Line 476: Line 485:
end
end
end
end
if snak.datatype == 'math' and displayformat ~= 'raw' then
if ({['math'] = 1, ['musical-notation'] = 1})[snak.datatype] == 1 and displayformat ~= 'raw' then
value = mw.getCurrentFrame():extensionTag('math', value)
value = mw.wikibase.formatValue(snak)
else
else
if params.urlpattern then
if params.urlpattern then
Line 495: Line 504:
return value
return value


elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
elseif valuetype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
if displayformat == 'raw' then
if displayformat == 'raw' then
return value.time
return value.time
Line 502: Line 511:
end
end


elseif datatype == 'globecoordinate' then
elseif valuetype == 'globecoordinate' then
-- retourne une table avec clés latitude, longitude, précision et globe à formater par un autre module (à changer ?)
-- retourne une table avec clés latitude, longitude, précision et globe à formater par un autre module (à changer ?)
if displayformat == 'latitude' then
if displayformat == 'latitude' then
Line 518: Line 527:
end
end


elseif datatype == 'quantity' then -- todo : gérer les paramètre précision
elseif valuetype == 'quantity' then -- todo : gérer les paramètre précision
if displayformat == 'raw' then
if displayformat == 'raw' then
return tonumber(value.amount)
return tonumber(value.amount)
Line 530: Line 539:
return number
return number
end
end
elseif datatype == 'monolingualtext' then
elseif valuetype == 'monolingualtext' then
return '<span lang="' .. value.language .. '">' .. value.text .. '</span>'
return '<span lang="' .. value.language .. '">' .. value.text .. '</span>'
else
else
return formatError( 'unknown-datavalue-type', datatype )
return formatError( 'unknown-datavalue-type', valuetype )
end
end
end
end
Line 564: Line 573:
args.entity = args.item
args.entity = args.item
end
end
local entity = p.getEntity(args.entity)
local property = string.upper(args.property)
local property = string.upper(args.property)
if not entity or not entity.claims or not entity.claims[property] then
local allClaims
local entity = args.entity
if type(entity) == 'table' then
allClaims = (entity and entity.claims and entity.claims[property]) or {}
else
allClaims = mw.wikibase.getAllStatements(entity, property)
end
if #allClaims == 0 then
return nil
return nil
end
end
Line 574: Line 589:
end
end
local claims = {}
local claims = {}
-- ~= '' lorsque le paramètre est écrit mais laissé blanc dans une fonction frame
for _, statement in ipairs(allClaims) do
for i, statement in pairs(entity.claims[property]) do
if
if
(
(
Line 859: Line 873:


local function formattedLabel(label, entity, args)
local function formattedLabel(label, entity, args)
if not args then args = {} end
local link = getLink(entity, args.link, args.lang)
local link = getLink(entity, args.link, args.lang)
if not link then
if not link then
Line 875: Line 888:
return nil
return nil
end
end
if not args then args = {} end
if not args then
args = {}
end
local label = p._getLabel(entity, args.lang, 'id', args.fallback)
local label = p._getLabel(entity, args.lang, 'id', args.fallback)
return formattedLabel(label, entity, args)
return formattedLabel(label, entity, args)
Line 890: Line 905:
if string.sub(entity, 1, 10) == 'Property:P' then
if string.sub(entity, 1, 10) == 'Property:P' then
entity = string.sub(entity, 10)
entity = string.sub(entity, 10)
elseif (string.sub(entity, 1, 1) ~= 'P' and string.sub(entity, 1, 1) ~= 'Q') or (not tonumber(string.sub(entity, 2))) then
elseif string.sub(entity, 1, 8) == 'Lexeme:L' then
entity = string.sub(entity, 8)
elseif not ({L = 1, P = 1, Q = 1})[string.sub(entity, 1, 1)] or not tonumber(string.sub(entity, 2)) then
return i18n('invalid-id')
return i18n('invalid-id')
end
end
Line 929: Line 946:
end
end
local str = ''
local str = ''
for i, j in pairs(claims) do
local new
local new = p.getFormattedQualifiers(j, qualifs, args) or ''
for _, cl in ipairs(claims) do
new = p.getFormattedQualifiers(cl, qualifs, args) or ''
str = str .. new
str = str .. new
end
end
Line 950: Line 968:
end
end
local formattedvalues = {}
local formattedvalues = {}
for i, j in pairs(claims) do
for _, cl in ipairs(claims) do
table.insert(formattedvalues, p.getFormattedDate(j))
table.insert(formattedvalues, p.getFormattedDate(cl))
end
end
local val = linguistic.conj(formattedvalues)
local val = linguistic.conj(formattedvalues)