Module:Wikidata
Apparence
La documentation pour ce module peut être créée à Module:Wikidata/doc
--script that retrieves basic data stored in Wikidata, for the datamodel, see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
local p = {}
local formatDate = require( 'Module:Date')
local formatCoord = require( 'Module:Coordinates')
local Outils = require( 'Module:Outils' )
local i18n = {
["errors"] = {
["property-param-not-provided"] = "Paramètre propriété non renseigné.",
["qualifier-param-not-provided"] = "Paramètre qualifier non renseigné.",
["entity-not-found"] = "Entité non trouvée.",
["unknown-claim-type"] = "type d'affirmation inconnu.",
["unknown-snak-type"] = "Type de snak inconnu.",
["unknown-datavalue-type"] = "Type de donnée non reconnu.",
["unknown-entity-type"] = "Type d'entité non reconnu.",
["unknown-value-module"] = "You must set both value-module and value-function parameters.",
["value-module-not-found"] = "The module pointed by value-module not found.",
["value-function-not-found"] = "The function pointed by value-function not found.",
["ambigous"] = "Ambigu : plusieurs valeurs possibles",
},
["somevalue"] = "inconnu",
["novalue"] = "pas applicable"
}
function getEntityFromId( id )
return mw.wikibase.getEntity() --TODO support for getting other entities
end
function formatError( key )
return '<span class="error">' .. i18n.errors[key] .. '</span>'
end
function p.getClaims( args ) -- returns a table of the claims matching some conditions given in args
if not args.property then
return formatError( 'property-param-not-provided' )
end
--Get entity
local entity = nil
local property = string.lower(args.property)
if args.entity and type( args.entity ) == "table" then
entity = args.entity
else
entity = getEntityFromId( args.entityId )
end
if not entity or not entity.claims or not entity.claims[property] then
return nil
end
claims = entity.claims[property]
if args.targetvalue then
claims = p.withtargetvalue(claims, args.targetvalue)
end
if args.rank then
claims = p.withrank(claims, args.rank)
end
if args.qualifier then
claims = p.withqualifier(claims, args.qualifier, args.qualifiervalue)
end
if args.source then
claims = p.withsource(claims, args.source, args.sourproperty)
end
if args.excludespecial == 'true' then
claims = p.excludespecial(claims)
end
if args.first == 'true' then -- à laisser en dernier
claims = p.firstvalue(claims)
end
return claims
end
function p.withtargetvalue(claims, targetvalue)
local oldclaims = claims
local claims = {}
for i, statement in pairs(oldclaims) do
if p.getRawvalue(statement.mainsnak) == targetvalue then
table.insert(claims, statement)
end
end
return claims
end
function p.withrank(claims, rank)
local oldclaims = claims
local claims = {}
for i, statement in pairs(oldclaims) do
if statement.rank == rank then
table.insert(claims, statement)
end
end
return claims
end
function p.withqualifier(claims, qualifier, qualifiervalue)
local oldclaims = claims
local claims = {}
for i, statement in pairs(oldclaims) do
if statement.qualifiers and statement.qualifiers[qualifier] then
if qualifiervalue then
for j, qualif in pairs(statement.qualifiers[qualifier]) do
if p.getRawvalue(qualif) == qualifiervalue then
table.insert(claims, statement)
end
end
else
table.insert(claims, statement)
end
end
end
return claims
end
function p.withsource(claims, source, sourceproperty)
local oldclaims = claims
local claims = {}
if not sourceproperty then
sourceproperty = 'p248' -- stated in
end
for i, statement in pairs(oldclaims) do
if statement.references then
for j, reference in pairs(statement.references) do
for prop, content in pairs(reference.snaks) do
if prop == sourceproperty then
for l, m in pairs(content) do
if p.getRawvalue(m) == source then
table.insert(claims, statement)
end
end
end
end
end
end
end
return claims
end
function p.excludespecial(claims)
local oldclaims = claims
local claims = {}
for i, statement in pairs(oldclaims) do
if statement.mainsnak.snaktype == 'value' then
table.insert(claims, statement)
end
end
return claims
end
function p.firstvalue(claims) -- return just the first value (useful for images for instance)
if claims[1] then return {claims[1]} end
end
function p.getRawvalue(snak)
return p.getDatavalue(snak, 'raw')
end
function p.getDatavalue(snak, formatting)
if snak.snaktype ~= 'value' then
return nil
end
datatype = snak.datavalue.type
if datatype == 'wikibase-entityid' then
if formatting == 'raw' then
return "Q" .. tostring(snak.datavalue.value['numeric-id'])
else
return p.formatEntityId("Q" .. tostring(snak.datavalue.value['numeric-id']))
end
elseif datatype == 'string' then
if formatting == 'weblink' then
local link = require ('Module:Weblink')
return link.makelink(snak.datavalue.value)
else
return snak.datavalue.value
end
elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
return p.formatTimevalue(snak.datavalue.value, formatting)
elseif datatype == 'globecoordinate' then
out = snak.datavalue.value
if out.globe == 'http://www.wikidata.org/wiki/Q2' then -- by far the most common case
out.globe = 'earth'
else
globetable = { Q2 = 'earth', Q111 = 'mars', Q405 = 'moon'}
f = mw.getCurrentFrame() -- to use titleparts to get the relevant part of the value.globe, better method ??
out.globe = globetable[f:callParserFunction( '#titleparts', { snak.datavalue.value.globe, '', 2 } )] -- error handling needed
end
return out
else return formatError( 'unknown-datavalue-type' )
end
end
function numOfClaims( claims )
if type(claims) ~= "table" then
return 0
elseif claims == {} then
return 0
elseif claims[0] then -- table Wikibase non modifiée avec une clé 0
return #claims + 1
else
return #claims
end
end
function p.numOfClaims(frame)
return numOfClaims( p.getClaims(frame.args) )
end
function p.getQualifier( frame )
local args = Outils.extractArgs( frame )
claims = p.getClaims( args )
if args.qualifier then
qualifier = args.qualifier
else
return formatError( 'qualifier-param-not-provided' )
end
if not claims or numOfClaims(claims) == 0 then
return nil
else
result = {}
for i, j in pairs(claims) do
for k, l in pairs( j.qualifiers[qualifier] ) do
table.insert(result, p.getDatavalue(l, 'standard'))
end
end
end
return mw.text.listToText(result)
end
function p._formatStatements( args )--Format statement and concat them cleanly
local rawStatements = p.getClaims( args )
local formattedStatements = {}
if notrawStatements or numOfClaims(rawStatements) == 0 then
return nil
end
for i, statement in pairs( rawStatements ) do
table.insert( formattedStatements, formatStatement( statement, args ) )
end
return mw.text.listToText( formattedStatements, args.separator, args.conjunction )
end
function p.formatStatements( frame )
return p._formatStatements(frame.args)
end
function formatStatement( statement, args )
if not statement.type or statement.type ~= 'statement' then
return formatError( 'unknown-claim-type' )
end
mainsnak = p.formatSnak( statement.mainsnak, args )
if args.showqualifiers then -- to be improved
qualifier = args.showqualifiers
if statement.qualifiers and statement.qualifiers[qualifier] then
qualifvalues = {}
for i, j in pairs (statement.qualifiers[qualifier]) do
table.insert(qualifvalues, p.getDatavalue(j))
end
return mainsnak .. ' (' .. mw.text.listToText(qualifvalues) .. ')'
end
end
return mainsnak
end
function p.formatSnak( snak, args )
if snak.snaktype == 'somevalue' then
return i18n['somevalue']
elseif snak.snaktype == 'novalue' then
return i18n['novalue']
elseif snak.snaktype == 'value' then
return p.getDatavalue( snak, args.formatting)
else
return formatError( 'unknown-snak-type' )
end
end
function p.formatEntityId( entityId, formatting )
local label = mw.wikibase.label( entityId )
if not label then
label = entityId --TODO what if no links and label + fallback language?
end
local link = mw.wikibase.sitelink( entityId )
if link then
return '[[' .. link .. '|' .. label .. ']]'
else
return '[[wikidata:' .. entityId .. '|' .. label .. ']]'
end
end
function p.formatTimevalue(value, formatting) -- date managed in the dedicated Module:Wikidata/Dates
if formatting == 'raw' then return value.time
else
local wdate = require('Module:Wikidata/Dates')
return wdate.objecttotext(wdate.dateobject(value))
end
end
return p