« Module:Wikidata » : différence entre les versions
Apparence
Modèle:Infobox>HenkvD NO error "entity-not-found" |
Modèle:Infobox>Tpt Aucun résumé des modifications |
||
| Ligne 16 : | Ligne 16 : | ||
function getEntityFromId( id ) | function getEntityFromId( id ) | ||
return mw.wikibase.getEntity() --TODO support for getting other entities | |||
return mw.wikibase.getEntity() | |||
end | end | ||
function getEntityIdFromValue( value ) | function getEntityIdFromValue( value ) | ||
if value['entity-type'] == 'item' then | if value['entity-type'] == 'item' then | ||
return 'q' .. value['numeric-id'] | |||
elseif value['entity-type'] == 'property' then | elseif value['entity-type'] == 'property' then | ||
return 'p' .. value['numeric-id'] | |||
else | else | ||
return formatError( 'unknown-entity-type' ) | return formatError( 'unknown-entity-type' ) | ||
end | end | ||
end | end | ||
| Ligne 45 : | Ligne 40 : | ||
--Get entity | --Get entity | ||
local entity = getEntityFromId( options.entityId ) | local entity = nil | ||
if options.entity and type( options.entity ) == "table" then | |||
entity = options.entity | |||
else | |||
entity = getEntityFromId( options.entityId ) | |||
end | |||
if not entity then | if not entity then | ||
return | return '' --TODO error? | ||
end | end | ||
if | if not entity.claims or not entity.claims[string.lower(options.property)] then | ||
return '' --TODO error? | return '' --TODO error? | ||
end | end | ||
| Ligne 56 : | Ligne 57 : | ||
--Format statement and concat them cleanly | --Format statement and concat them cleanly | ||
local formattedStatements = {} | local formattedStatements = {} | ||
for i, statement in pairs( entity.claims[ | for i, statement in pairs( entity.claims[options.property:lower()] ) do | ||
table.insert( formattedStatements, formatStatement( statement, options ) ) | if options.rank == 'one' then | ||
return formatStatement( statement, options ) --Output only one value | |||
else | |||
table.insert( formattedStatements, formatStatement( statement, options ) ) | |||
end | |||
end | end | ||
return mw.text.listToText( formattedStatements, options.separator, options.conjunction ) | return mw.text.listToText( formattedStatements, options.separator, options.conjunction ) | ||
| Ligne 90 : | Ligne 95 : | ||
end | end | ||
local formatter = require ('Module:' .. options['value-module']) | local formatter = require ('Module:' .. options['value-module']) | ||
if formatter | if not formatter then | ||
return formatError( 'value-module-not-found' ) | return formatError( 'value-module-not-found' ) | ||
end | end | ||
local fun = formatter[options['value-function']] | local fun = formatter[options['value-function']] | ||
if fun | if not fun then | ||
return formatError( 'value-function-not-found' ) | return formatError( 'value-function-not-found' ) | ||
end | end | ||
| Ligne 104 : | Ligne 109 : | ||
return formatEntityId( getEntityIdFromValue( datavalue.value ), options ) | return formatEntityId( getEntityIdFromValue( datavalue.value ), options ) | ||
elseif datavalue.type == 'string' then | elseif datavalue.type == 'string' then | ||
return datavalue.value | if options.pattern and options.pattern ~= '' then | ||
return formatFromPattern( datavalue.value, options ) | |||
else | |||
return datavalue.value | |||
end | |||
else | else | ||
return formatError( 'unknown-datavalue-type' ) | return formatError( 'unknown-datavalue-type' ) | ||
| Ligne 122 : | Ligne 131 : | ||
return label --TODO what if no links and label + fallback language? | return label --TODO what if no links and label + fallback language? | ||
end | end | ||
end | |||
function formatFromPattern( str, options ) | |||
return mw.ustring.gsub( options.pattern, '$1', str ) .. '' --Hack to get only the first result of the function | |||
end | end | ||
| Ligne 134 : | Ligne 147 : | ||
end | end | ||
return formatStatements( frame.args ) | 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 | end | ||
return p | return p | ||
Version du 29 mai 2013 à 13:45
La documentation pour ce module peut être créée à Module:Wikidata/doc
local i18n = {
["errors"] = {
["property-param-not-provided"] = "Property parameter not provided.",
["entity-not-found"] = "Entity not found.",
["unknown-claim-type"] = "Unknown claim type.",
["unknown-snak-type"] = "Unknown snak type.",
["unknown-datavalue-type"] = "Unknown datavalue type.",
["unknown-entity-type"] = "Unknown entity type.",
["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."
},
["somevalue"] = "''unknown value''",
["novalue"] = "''no value''"
}
function getEntityFromId( id )
return mw.wikibase.getEntity() --TODO support for getting other entities
end
function getEntityIdFromValue( value )
if value['entity-type'] == 'item' then
return 'q' .. value['numeric-id']
elseif value['entity-type'] == 'property' then
return 'p' .. value['numeric-id']
else
return formatError( 'unknown-entity-type' )
end
end
function formatError( key )
return '<span class="error">' .. i18n.errors[key] .. '</span>'
end
function formatStatements( options )
if not options.property then
return formatError( 'property-param-not-provided' )
end
--Get entity
local entity = nil
if options.entity and type( options.entity ) == "table" then
entity = options.entity
else
entity = getEntityFromId( options.entityId )
end
if not entity then
return '' --TODO error?
end
if not entity.claims or not entity.claims[string.lower(options.property)] then
return '' --TODO error?
end
--Format statement and concat them cleanly
local formattedStatements = {}
for i, statement in pairs( entity.claims[options.property:lower()] ) 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
return formatSnak( statement.mainsnak, options )
--TODO reference and qualifiers
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 formatDatavalue( snak.datavalue, options )
else
return formatError( 'unknown-snak-type' )
end
end
function formatDatavalue( datavalue, options )
--Use the customize handler if provided
if options['value-module'] or options['value-function'] then
if not options['value-module'] or not options['value-function'] then
return formatError( 'unknown-value-module' )
end
local formatter = require ('Module:' .. options['value-module'])
if not formatter then
return formatError( 'value-module-not-found' )
end
local fun = formatter[options['value-function']]
if not fun then
return formatError( 'value-function-not-found' )
end
return fun( datavalue.value, options )
end
--Default formatters
if datavalue.type == 'wikibase-entityid' then
return formatEntityId( getEntityIdFromValue( datavalue.value ), options )
elseif datavalue.type == 'string' then
if options.pattern and options.pattern ~= '' then
return formatFromPattern( datavalue.value, options )
else
return datavalue.value
end
else
return formatError( 'unknown-datavalue-type' )
end
end
function formatEntityId( entityId, options )
local label = mw.wikibase.label( entityId )
local link = mw.wikibase.sitelink( entityId )
if link then
if label then
return '[[' .. link .. '|' .. label .. ']]'
else
return '[[' .. link .. ']]'
end
else
return label --TODO what if no links and label + fallback language?
end
end
function formatFromPattern( str, options )
return mw.ustring.gsub( options.pattern, '$1', str ) .. '' --Hack to get only the first result of the function
end
local p = {}
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
return p