ماژول:WikidataF
بو ماژول اوچون بلگهلر ماژول:WikidataF/بلگه صفحهسینده یارادیلا بیلینر
--script that retrieves basic data stored in Wikidata, for the datamodel, see https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
local p = {}
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"
}
local function getEntityFromId( id )
return mw.wikibase.getEntityObject() --TODO support for getting other entities
end
local function formatError( key )
return '<span class="error">' .. i18n.errors[key] .. '</span>'
end
local function withtargetvalue(claims, targetvalue)
targetvalue = string.upper(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
local function validclaims(claims)
local oldclaims = claims
local claims = {}
for i, statement in pairs(oldclaims) do
if statement.rank == 'preferred' or statement.rank == 'normal' then
table.insert(claims, statement)
end
end
return claims
end
local function withrank(claims, rank)
if rank == 'best' then
local preferred = withrank(claims, 'preferred')
if #preferred > 0 then
return preferred
else
return withrank(claims, 'normal')
end
end
if rank == 'valid' then
return validclaims(claims)
end
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
local function withqualifier(claims, qualifier, qualifiervalue)
qualifier, qualifiervalue = string.upper(qualifier), string.upper(qualifiervalue or '')
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
local function withsource(claims, source, sourceproperty)
local oldclaims = claims
local claims = {}
source = string.upper(source or '')
sourceproperty = string.upper(sourceproperty or 'P248')
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
local function 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
local function firstvalue(claims)
-- return just the first value (useful for images for instance)
-- returns in priority claims with rank = 'preferred', else with rank == 'normal' do not return claims with rank == 'deprecated'
for i, statement in pairs(claims) do -- choisit en priorité les données "preferred"
if statement.rank == 'preferred' then
return {statement}
end
end
for i, statement in pairs(claims) do
if statement.rank == 'normal' then
return {statement}
end
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
local datatype = snak.datavalue.type
local value = snak.datavalue.value
if datatype == 'wikibase-entityid' then
if formatting == 'raw' then
return "Q" .. tostring(value['numeric-id'])
else
return p.formatEntityId("Q" .. tostring(value['numeric-id']), formatting)
end
elseif datatype == 'string' then
if formatting == 'weblink' then
local link = require ('Module:Weblink')
return link.makelink(value)
else
return value
end
elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
if formatting == 'raw' then
return value.time
else
local wdate = require('Module:Wikidata/Dates')
return wdate.objecttotext(wdate.dateobject(value))
end
elseif datatype == 'globecoordinate' then
-- retourne une table avec clés latitude, longitude, précision et globe à formater par un autre module (à changer ?)
value.globe = require('Module:Wikidata/Globes')[value.globe] -- transforme l'ID du globe en nom anglais utilisable par geohack
if formatting == 'latitude' then
return value.latitude
elseif formatting == 'longitude' then
return value.longitude
else
return value -- note : les coordonnées Wikidata peuvent être utilisée depuis Module:Coordinates. Faut-il aussi autoriser à appeler Module:Coordiantes ici ?
end
elseif datatype == 'url' then
return value.url
elseif datatype == 'quantity' then
return value.amount
else
return formatError( 'unknown-datavalue-type' )
end
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.upper(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]
-- mettre ~= '' pour le cas ou le paramètre est écrit mais laissé blanc ({{#invoke:formatStatements|property=pXX|targetvalue = xx}})
if args.targetvalue and args.targetvalue ~= '' then
claims = withtargetvalue(claims, args.targetvalue)
end
if args.qualifier and args.qualifier ~= '' then
claims = withqualifier(claims, args.qualifier, args.qualifiervalue)
end
if args.source and args.source ~= '' then
claims = withsource(claims, args.source, args.sourceproperty)
end
if args.excludespecial == 'true' and args.excludespecial ~= '' then
claims = excludespecial(claims)
end
if args.rank ~= 'all' then -- à laisser vers la fin, quan les élimination sont faites
if not args.rank or args.rank == '' then
args.rank = 'best'
end
claims = withrank(claims, args.rank)
end
if args.first == 'true' and args.first ~= '' then -- retourne une seule valeur
claims = firstvalue(claims)
end
return claims
end
function p.numOfClaims(frame)
local claims = p.getClaims(frame.args)
if claims then
return #claims
else
return 0
end
end
function p._getQualifier(args)
claims = p.getClaims( args )
if args.qualifier then
qualifier = args.qualifier
else
return formatError( 'qualifier-param-not-provided' )
end
if not claims or #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.getQualifier( frame )
return p._getQualifier(frame.args)
end
function p._formatStatements( args )--Format statement and concat them cleanly
--If a value is already set, use it
if args.value and args.value ~= '' then
return args.value
end
local rawStatements = p.getClaims( args )
local formattedStatements = {}
if not rawStatements or #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 )
-- .. '[[Category:Page utilisant des données de Wikidata/' .. string.upper(args.property) .. ']]'
end
function p.formatStatements( frame )
local args = {}
if frame == mw.getCurrentFrame() then
args = frame:getParent().args -- paramètres du modèle appelant (est-ce vraiment une bonne idée ?)
for k, v in pairs(frame.args) do
args[k] = v
end
else
args = frame
end
return p._formatStatements( 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 not args then args = {} end -- pour faciliter l'appel depuis d'autres modules
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
if formatting == 'nolink' then
return label
else
local link = mw.wikibase.sitelink( entityId )
if link then
return '[[' .. link .. '|' .. label .. ']]'
else
return '[[wikidata:' .. entityId .. '|' .. label .. ']]'
end
end
end
return p