« Module:Wikidata » : différence entre les versions
Apparence
Modèle:Infobox>Zolo support pour calendrier julien, et petite réorganisation |
Modèle:Infobox>Zolo Annulation temporaire |
||
| Ligne 1 : | Ligne 1 : | ||
local formatDate = require( 'Module:Date') | local formatDate = require( 'Module:Date') | ||
local formatCoord = require( 'Module:Coordinates | local formatCoord = require( 'Module:Coordinates/Test') | ||
local i18n = { | local i18n = { | ||
| Ligne 39 : | Ligne 34 : | ||
end | end | ||
function | function getClaims( options ) -- returns a table of the claims matching some conditions given in options | ||
if not options.property then | |||
if not | |||
return formatError( 'property-param-not-provided' ) | return formatError( 'property-param-not-provided' ) | ||
end | end | ||
--Get entity | --Get entity | ||
local entity = nil | local entity = nil | ||
local property = string.lower( | local property = string.lower(options.property) | ||
if | if options.entity and type( options.entity ) == "table" then | ||
entity = | entity = options.entity | ||
else | else | ||
entity = getEntityFromId( | entity = getEntityFromId( options.entityId ) | ||
end | end | ||
| Ligne 58 : | Ligne 52 : | ||
claims = entity.claims[property] | claims = entity.claims[property] | ||
if | if options.excludespecial == 'true' then | ||
oldclaims = claims | oldclaims = claims | ||
claims = {} | claims = {} | ||
| Ligne 67 : | Ligne 61 : | ||
end | end | ||
end | end | ||
if | if options.targetvalue then | ||
targetvalue = | targetvalue = options.targetvalue | ||
oldclaims = claims | oldclaims = claims | ||
claims = {} | claims = {} | ||
for i, statement in pairs(oldclaims) do | for i, statement in pairs(oldclaims) do | ||
if | if getDatavalue(statement.mainsnak, 'raw') == targetvalue then | ||
table.insert(claims, statement) | table.insert(claims, statement) | ||
end | end | ||
end | end | ||
end | end | ||
if | if options.qualifier then -- ne marche que pour les propriétés de type item | ||
qualifier = | qualifier = options.qualifier | ||
qualifiervalue = | qualifiervalue = options.qualifiervalue | ||
oldclaims = claims | oldclaims = claims | ||
claims = {} | claims = {} | ||
| Ligne 86 : | Ligne 80 : | ||
if qualifiervalue then | if qualifiervalue then | ||
for j, qualif in pairs(statement.qualifiers[qualifier]) do | for j, qualif in pairs(statement.qualifiers[qualifier]) do | ||
if | if getDatavalue(qualif, 'raw') == qualifiervalue then | ||
table.insert(claims, statement) | table.insert(claims, statement) | ||
end | end | ||
| Ligne 96 : | Ligne 90 : | ||
end | end | ||
end | end | ||
if | if options.source then | ||
if | if options.sourceproperty then | ||
sourceproperty = | sourceproperty = options.sourceproperty | ||
else | else | ||
sourceproperty = "p248" | sourceproperty = "p248" | ||
end | end | ||
sourcevalue = | sourcevalue = options.source | ||
oldclaims = claims | oldclaims = claims | ||
claims = {} | claims = {} | ||
| Ligne 111 : | Ligne 105 : | ||
if k == sourceproperty then | if k == sourceproperty then | ||
for l, m in pairs(prop) do | for l, m in pairs(prop) do | ||
if | if getDatavalue(m) == "Q" .. sourcevalue then | ||
table.insert(claims, statement) | table.insert(claims, statement) | ||
end | end | ||
| Ligne 125 : | Ligne 119 : | ||
end | end | ||
function | function getDatavalue(snak, formatting) | ||
datatype = snak.datavalue.type | datatype = snak.datavalue.type | ||
| Ligne 132 : | Ligne 126 : | ||
return "Q" .. tostring(snak.datavalue.value['numeric-id']) | return "Q" .. tostring(snak.datavalue.value['numeric-id']) | ||
else | else | ||
return | return formatEntityId("Q" .. tostring(snak.datavalue.value['numeric-id'])) | ||
end | end | ||
| Ligne 139 : | Ligne 133 : | ||
elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z | elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z | ||
d = snak.datavalue.value.time | |||
precision = snak.datavalue.value.precision | |||
era = '' | |||
if string.sub(d,1,1) == '-' or string.sub(d,2,12) == '00000000000' then -- Before Christ or year 0 (see datamodel) | |||
era = '-' | |||
end | |||
year = '' | |||
if precision >= 9 then | |||
year = string.sub(d, 9, 12) | |||
if era == '-' then -- remove one Year for BC years because of year 0 in the datamodel | |||
year = tostring(tonumber(year) + 1) | |||
end | |||
end | |||
month = '' | |||
if precision >= 10 then | |||
month = string.sub(d, 14, 15) | |||
end | |||
day = '' | |||
if precision >= 11 then | |||
day = string.sub(d, 17, 18) | |||
end | |||
if formatting == 'ISO' then -- quirky, should be integrated to Module:Date | |||
return year .. '-' .. month .. '-' .. day | |||
else | |||
return formatDate.modeleDate({day, month, era .. year}) | |||
end | |||
elseif datatype == 'globecoordinate' then | elseif datatype == 'globecoordinate' then | ||
latitude = tostring(snak.datavalue.value.latitude) | latitude = tostring(snak.datavalue.value.latitude) | ||
| Ligne 146 : | Ligne 164 : | ||
-- precision = snak.datavalue.value.precision | -- precision = snak.datavalue.value.precision | ||
--globe = to do transform string like http://www.wikidata.org/entity/Q2 into the globe parameter of Module:Coordinates | --globe = to do transform string like http://www.wikidata.org/entity/Q2 into the globe parameter of Module:Coordinates | ||
return formatCoord.coord2({latitude, longitude}) | |||
else return formatError( 'unknown-datavalue-type' ) | else return formatError( 'unknown-datavalue-type' ) | ||
| Ligne 153 : | Ligne 170 : | ||
end | end | ||
function | function numOfClaims( claims ) | ||
if type(claims) ~= "table" then | if type(claims) ~= "table" then | ||
return 0 | return 0 | ||
| Ligne 171 : | Ligne 182 : | ||
end | end | ||
function | function getQualifier( options ) | ||
claims = getClaims( options ) | |||
claims = | if options.qualifier then | ||
if | qualifier = options.qualifier | ||
qualifier = | |||
else | else | ||
return formatError( 'qualifier-param-not-provided' ) | return formatError( 'qualifier-param-not-provided' ) | ||
| Ligne 185 : | Ligne 195 : | ||
for i, j in pairs(claims) do | for i, j in pairs(claims) do | ||
for k, l in pairs( j.qualifiers[qualifier] ) do | for k, l in pairs( j.qualifiers[qualifier] ) do | ||
table.insert(result, | table.insert(result, getDatavalue(l, 'standard')) | ||
end | end | ||
end | end | ||
| Ligne 192 : | Ligne 202 : | ||
end | end | ||
function | function formatStatements( options )--Format statement and concat them cleanly | ||
local formattedStatements = {} | local formattedStatements = {} | ||
local rawStatements = | local rawStatements = getClaims( options ) | ||
if notrawStatements or numOfClaims(rawStatements) == 0 then | if notrawStatements or numOfClaims(rawStatements) == 0 then | ||
return nil | return nil | ||
end | end | ||
for i, statement in pairs( rawStatements ) do | for i, statement in pairs( rawStatements ) do | ||
if | if options.rank == 'one' then | ||
return formatStatement( statement, | return formatStatement( statement, options ) --Output only one value | ||
else | else | ||
table.insert( formattedStatements, formatStatement( statement, | table.insert( formattedStatements, formatStatement( statement, options ) ) | ||
end | end | ||
end | end | ||
return mw.text.listToText( formattedStatements, | return mw.text.listToText( formattedStatements, options.separator, options.conjunction ) | ||
end | end | ||
function formatStatement( statement, | function formatStatement( statement, options ) | ||
if not statement.type or statement.type ~= 'statement' then | if not statement.type or statement.type ~= 'statement' then | ||
return formatError( 'unknown-claim-type' ) | return formatError( 'unknown-claim-type' ) | ||
end | end | ||
mainsnak = | mainsnak = formatSnak( statement.mainsnak, options ) | ||
if | if options.showqualifiers then -- to be improved | ||
qualifier = | qualifier = options.showqualifiers | ||
if statement.qualifiers and statement.qualifiers[qualifier] then | if statement.qualifiers and statement.qualifiers[qualifier] then | ||
qualifvalues = {} | qualifvalues = {} | ||
for i, j in pairs (statement.qualifiers[qualifier]) do | for i, j in pairs (statement.qualifiers[qualifier]) do | ||
table.insert(qualifvalues, | table.insert(qualifvalues, getDatavalue(j)) | ||
end | end | ||
return mainsnak .. ' (' .. mw.text.listToText(qualifvalues) .. ')' | return mainsnak .. ' (' .. mw.text.listToText(qualifvalues) .. ')' | ||
| Ligne 229 : | Ligne 238 : | ||
end | end | ||
function | function formatSnak( snak, options ) | ||
if snak.snaktype == 'somevalue' then | if snak.snaktype == 'somevalue' then | ||
return i18n['somevalue'] | return i18n['somevalue'] | ||
| Ligne 235 : | Ligne 244 : | ||
return i18n['novalue'] | return i18n['novalue'] | ||
elseif snak.snaktype == 'value' then | elseif snak.snaktype == 'value' then | ||
return | return getDatavalue( snak, options.formatting) | ||
else | else | ||
return formatError( 'unknown-snak-type' ) | return formatError( 'unknown-snak-type' ) | ||
| Ligne 241 : | Ligne 250 : | ||
end | end | ||
function | function formatEntityId( entityId, options ) | ||
local label = mw.wikibase.label( entityId ) | local label = mw.wikibase.label( entityId ) | ||
if not label then | if not label then | ||
| Ligne 254 : | Ligne 263 : | ||
end | end | ||
local p = {} | |||
function p.id(frame) | function p.id(frame) | ||
| Ligne 298 : | Ligne 269 : | ||
end | end | ||
function p.formatStatements( frame ) | |||
local args = frame.args | |||
--If a value if already set, use it | |||
if args.value and args.value ~= '' then | |||
return args.value | |||
end | |||
return formatStatements( frame.args ) | |||
end | |||
function p.formatStatementsFromLua( options ) | |||
--If a value if already set, use it | |||
if options.value and options.value ~= '' then | |||
return options.value | |||
end | |||
return formatStatements( options ) | |||
end | |||
function p.getClaimsFromLua( options ) | |||
return getClaims( options ) | |||
end | |||
function p.numOfClaims(frame) | |||
return numOfClaims( getClaims(frame.args) ) | |||
end | |||
function p.getQualifier(frame) | |||
return getQualifier(frame.args) | |||
end | |||
return p | return p | ||
Version du 2 octobre 2013 à 13:28
La documentation pour ce module peut être créée à Module:Wikidata/doc
local formatDate = require( 'Module:Date')
local formatCoord = require( 'Module:Coordinates/Test')
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 possible",
},
["somevalue"] = "inconnu",
["novalue"] = "pas de données"
}
function getEntityFromId( id )
return mw.wikibase.getEntity() --TODO support for getting other entities
end
function getId( id ) -- gets the ID of the page
entity = getEntityFromId( id )
return entity.id
end
function formatError( key )
return '<span class="error">' .. i18n.errors[key] .. '</span>'
end
function getClaims( options ) -- returns a table of the claims matching some conditions given in options
if not options.property then
return formatError( 'property-param-not-provided' )
end
--Get entity
local entity = nil
local property = string.lower(options.property)
if options.entity and type( options.entity ) == "table" then
entity = options.entity
else
entity = getEntityFromId( options.entityId )
end
if not entity or not entity.claims or not entity.claims[property] then
return nil
end
claims = entity.claims[property]
if options.excludespecial == 'true' then
oldclaims = claims
claims = {}
for i, statement in pairs(oldclaims) do
if statement.mainsnak.snaktype == 'value' then
table.insert(claims, statement)
end
end
end
if options.targetvalue then
targetvalue = options.targetvalue
oldclaims = claims
claims = {}
for i, statement in pairs(oldclaims) do
if getDatavalue(statement.mainsnak, 'raw') == targetvalue then
table.insert(claims, statement)
end
end
end
if options.qualifier then -- ne marche que pour les propriétés de type item
qualifier = options.qualifier
qualifiervalue = options.qualifiervalue
oldclaims = claims
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 getDatavalue(qualif, 'raw') == qualifiervalue then
table.insert(claims, statement)
end
end
else
table.insert(claims, statement)
end
end
end
end
if options.source then
if options.sourceproperty then
sourceproperty = options.sourceproperty
else
sourceproperty = "p248"
end
sourcevalue = options.source
oldclaims = claims
claims = {}
for i, statement in pairs(oldclaims) do
if statement.references then
for j, reference in pairs(statement.references) do
for k, prop in pairs(reference.snaks) do
if k == sourceproperty then
for l, m in pairs(prop) do
if getDatavalue(m) == "Q" .. sourcevalue then
table.insert(claims, statement)
end
end
end
end
end
end
end
end
return claims
end
function getDatavalue(snak, formatting)
datatype = snak.datavalue.type
if datatype == 'wikibase-entityid' then
if formatting == 'raw' then
return "Q" .. tostring(snak.datavalue.value['numeric-id'])
else
return formatEntityId("Q" .. tostring(snak.datavalue.value['numeric-id']))
end
elseif datatype == 'string' then
return snak.datavalue.value
elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
d = snak.datavalue.value.time
precision = snak.datavalue.value.precision
era = ''
if string.sub(d,1,1) == '-' or string.sub(d,2,12) == '00000000000' then -- Before Christ or year 0 (see datamodel)
era = '-'
end
year = ''
if precision >= 9 then
year = string.sub(d, 9, 12)
if era == '-' then -- remove one Year for BC years because of year 0 in the datamodel
year = tostring(tonumber(year) + 1)
end
end
month = ''
if precision >= 10 then
month = string.sub(d, 14, 15)
end
day = ''
if precision >= 11 then
day = string.sub(d, 17, 18)
end
if formatting == 'ISO' then -- quirky, should be integrated to Module:Date
return year .. '-' .. month .. '-' .. day
else
return formatDate.modeleDate({day, month, era .. year})
end
elseif datatype == 'globecoordinate' then
latitude = tostring(snak.datavalue.value.latitude)
longitude = tostring(snak.datavalue.value.longitude)
-- precision = snak.datavalue.value.precision
--globe = to do transform string like http://www.wikidata.org/entity/Q2 into the globe parameter of Module:Coordinates
return formatCoord.coord2({latitude, longitude})
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 (buggy)
return #claims + 1
else
return #claims
end
end
function getQualifier( options )
claims = getClaims( options )
if options.qualifier then
qualifier = options.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, getDatavalue(l, 'standard'))
end
end
end
return mw.text.listToText(result)
end
function formatStatements( options )--Format statement and concat them cleanly
local formattedStatements = {}
local rawStatements = getClaims( options )
if notrawStatements or numOfClaims(rawStatements) == 0 then
return nil
end
for i, statement in pairs( rawStatements ) do
if options.rank == 'one' then
return formatStatement( statement, options ) --Output only one value
else
table.insert( formattedStatements, formatStatement( statement, options ) )
end
end
return mw.text.listToText( formattedStatements, options.separator, options.conjunction )
end
function formatStatement( statement, options )
if not statement.type or statement.type ~= 'statement' then
return formatError( 'unknown-claim-type' )
end
mainsnak = formatSnak( statement.mainsnak, options )
if options.showqualifiers then -- to be improved
qualifier = options.showqualifiers
if statement.qualifiers and statement.qualifiers[qualifier] then
qualifvalues = {}
for i, j in pairs (statement.qualifiers[qualifier]) do
table.insert(qualifvalues, getDatavalue(j))
end
return mainsnak .. ' (' .. mw.text.listToText(qualifvalues) .. ')'
end
end
return mainsnak
end
function formatSnak( snak, options )
if snak.snaktype == 'somevalue' then
return i18n['somevalue']
elseif snak.snaktype == 'novalue' then
return i18n['novalue']
elseif snak.snaktype == 'value' then
return getDatavalue( snak, options.formatting)
else
return formatError( 'unknown-snak-type' )
end
end
function formatEntityId( entityId, options )
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
local p = {}
function p.id(frame)
return getId(frame.args[1])
end
function p.formatStatements( frame )
local args = frame.args
--If a value if already set, use it
if args.value and args.value ~= '' then
return args.value
end
return formatStatements( frame.args )
end
function p.formatStatementsFromLua( options )
--If a value if already set, use it
if options.value and options.value ~= '' then
return options.value
end
return formatStatements( options )
end
function p.getClaimsFromLua( options )
return getClaims( options )
end
function p.numOfClaims(frame)
return numOfClaims( getClaims(frame.args) )
end
function p.getQualifier(frame)
return getQualifier(frame.args)
end
return p