Mòdul:Wikidata: diferència entre les revisions
Aparença
Contingut suprimit Contingut afegit
correcció per format inconsistent d'anys<100 |
mw.wikibase.getEntity obsolet |
||
Línia 16: | Línia 16: | ||
} |
} |
||
function getEntityFromId( id ) |
|||
return mw.wikibase.getEntity() --TODO support for getting other entities |
|||
end |
|||
function getClaims( args ) -- returns a table of the claims (TODO: matching some conditions given in args) |
function getClaims( args ) -- returns a table of the claims (TODO: matching some conditions given in args) |
||
if not args.property then |
if not args.property then |
||
Línia 26: | Línia 22: | ||
--Get entity |
--Get entity |
||
local entity = nil |
local entity = nil |
||
local property = string. |
local property = string.upper(args.property) |
||
if args.entity and type( args.entity ) == "table" then |
if args.entity and type( args.entity ) == "table" then |
||
entity = args.entity |
entity = args.entity |
||
else |
else |
||
entity = |
entity = mw.wikibase.getEntityObject() |
||
end |
end |
||
Línia 44: | Línia 40: | ||
elseif claims == {} then |
elseif claims == {} then |
||
return 0 |
return 0 |
||
elseif claims[0] then -- taula Wikibase no modificada amb una clau 0 |
|||
return #claims + 1 |
|||
else |
else |
||
return #claims |
return #claims |
||
Línia 189: | Línia 183: | ||
function p.getSiteLink( frame ) |
function p.getSiteLink( frame ) |
||
if frame.args[1] == nil then |
if frame.args[1] == nil then |
||
entity = mw.wikibase. |
entity = mw.wikibase.getEntityObject() |
||
if not entity then |
if not entity then |
||
return nil |
return nil |
||
Línia 216: | Línia 210: | ||
--Format statement and concat them cleanly |
--Format statement and concat them cleanly |
||
⚫ | |||
local sortedLabels = {} -- l'índex 0 passa a ser 1, necessari per deixar-ho ordenat |
|||
for i, statement in pairs( rawStatements ) do |
for i, statement in pairs( rawStatements ) do |
||
sortedLabels[i+1] = statement |
|||
end |
|||
⚫ | |||
for i, statement in pairs( sortedLabels ) do |
|||
if args.rank == 'one' then |
if args.rank == 'one' then |
||
return formatStatement( statement, args ) --Output only one value |
return formatStatement( statement, args ) --Output only one value |
Revisió del 18:53, 22 feb 2015
![Icona de documentació de mòdul](http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Test_Template_Info-Icon.svg/50px-Test_Template_Info-Icon.svg.png)
A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]
![]() | Aquesta pàgina és una redirecció. Per les darreres versions d'aquest mòdul, a partir de 20200210, vegeu Module:Wikidades. |
![]() | (en) This is a redirect page. For newer versions of this module, from 20200210, see Module:Wikidades. |
local i18n = {
["errors"] = {
["property-param-not-provided"] = "Falta el paràmetre de la propietat.",
["qualifier-property-param-not-provided"] = "Falta el paràmetre de la propietat del qualificador.",
["entity-not-found"] = "Entitat no trobada.",
["unknown-claim-type"] = "Tipus d'afirmació (claim) desconegut.",
["unknown-snak-type"] = "Tipus de connector (snak) desconegut.",
["unknown-datavalue-type"] = "Tipus de valor (datavalue) desconegut.",
["unknown-entity-type"] = "Tipus d'entitat desconegut.",
["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"] = "''valor desconegut''",
["novalue"] = "''cap valor aplicable''"
}
function getClaims( args ) -- returns a table of the claims (TODO: 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 = mw.wikibase.getEntityObject()
end
if not entity or not entity.claims or not entity.claims[property] then
return nil
end
return entity.claims[property]
end
function numOfClaims( claims )
if type(claims) ~= "table" then
return 0
elseif claims == {} then
return 0
else
return #claims
end
end
function getDatavalue(snak, formatting)
if snak.snaktype ~= 'value' then
return nil
end
datatype = snak.datavalue.type
if datatype == 'wikibase-entityid' then
local entityId = "Q" .. tostring(snak.datavalue.value['numeric-id'])
local label = mw.wikibase.label( entityId )
local sitelink = mw.wikibase.sitelink( entityId )
if formatting == 'raw' then
return entityId
elseif formatting == 'label' then
return ( label or entityId )
elseif formatting == 'sitelink' then
return ( sitelink or 'wikidata:' .. entityId )
else
return formatEntityId( entityId )
end
elseif datatype == 'string' then
local astring = snak.datavalue.value
if formatting == 'weblink' then
return '[' .. astring .. ' ' .. mw.text.split( astring, '//' )[2] .. ']'
elseif mw.ustring.find( (formatting or ''), '$1', 1, true ) then -- formatting = a pattern
return mw.ustring.gsub( formatting, '$1', astring ) .. '' --Hack to get only the first result of the function
else
return astring
end
elseif datatype == 'time' then
-- format de 28 caràcters per any>99: +00000002013-02-15T00:00:00Z
-- format de 26 caràcters per any<100: +000000050-01-01T00:00:00Z
local d = snak.datavalue.value.time
local lang = mw.getContentLanguage()
if formatting == 'year' then
if #d>26 then
return lang:formatDate("Y", string.sub(d, 9, 18)) -- format aaaa
else
return lang:formatDate("Y", string.sub(d, 9, 16))
end
else
if #d>26 then
return lang:formatDate("j F Y", string.sub(d, 9, 18)) -- default format dd mes aaaa
else
return lang:formatDate("j F Y", string.sub(d, 9, 16))
end
end
elseif datatype == 'globecoordinate' then
local coord = snak.datavalue.value
local globetable = { Q2 = 'earth', Q111 = 'mars', Q405 = 'moon'}
if formatting == 'latitude' then
return coord.latitude
elseif formatting == 'longitude' then
return coord.longitude
elseif formatting == 'dimension' then
return coord.dimension
else
if coord.globe == '' or coord.globe == nil then
return 'earth'
else
local globenum = mw.text.split( snak.datavalue.value.globe, 'entity/' )[2] -- http://www.wikidata.org/wiki/Q2
if globetable[globenum] then
return globetable[globenum]
else
return globenum
end
end
end
else
return formatError( 'unknown-datavalue-type' )
end
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 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 formatQualifier ( qualifiers, qualifierProperty, options )
if not qualifiers[qualifierProperty] then
return nil
end
return formatSnak( qualifiers[qualifierProperty][0], options )
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 )
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 '[[wikidata:' .. entityId .. '|' .. (label or entityId) .. ']]'
end
end
local p = {}
-- Return the site link (for the current site) for a given data item.
function p.getSiteLink( frame )
if frame.args[1] == nil then
entity = mw.wikibase.getEntityObject()
if not entity then
return nil
end
id = entity.id
else
id = frame.args[1]
end
return mw.wikibase.sitelink( id )
end
-- Return the statements with a format
function p.formatStatements( frame ) -- Format statement and concat them cleanly
local args = frame.args
--If a value is already set, use it
if args.value and args.value ~= '' then
return args.value
end
local rawStatements = getClaims( args )
if not rawStatements or numOfClaims(rawStatements) == 0 then
return nil
end
--Format statement and concat them cleanly
local formattedStatements = {}
for i, statement in pairs( rawStatements ) do
if args.rank == 'one' then
return formatStatement( statement, args ) --Output only one value
elseif args.rank == 'preferred' then --This should be set as the default case
if #sortedLabels == 1 then
return formatStatement( statement, args ) --Output the only value
else
if statement.rank == 'preferred' then
table.insert( formattedStatements, formatStatement( statement, args ) ) --Output only the preferred values
end
end
else
table.insert( formattedStatements, formatStatement( statement, args ) )
end
end
return mw.text.listToText( formattedStatements, args.separator, args.conjunction )
end
--Return the qualifiers wth a format
function p.formatQualifiers( frame )
local args = frame.args
--If a value is already set, use it
if args.value and args.value ~= '' then
return args.value
end
--Required parameters
local property = args.property
local qualifierProperty = args.qualifierProperty
if not property then
return formatError( 'property-param-not-provided' )
end
if not qualifierProperty then
return formatError( 'qualifier-property-param-not-provided' )
end
--Get statements
local rawStatements = getClaims( args )
if not rawStatements or numOfClaims(rawStatements) == 0 then
return nil
end
--Format statement and concat them cleanly
local sortedStatements = {} -- l'índex 0 passa a ser 1, necessari per deixar-ho ordenat
for i, statement in pairs( rawStatements ) do
sortedStatements[i+1] = statement
end
local formattedQualifiers = {}
for i, statement in pairs( sortedStatements ) do
if not statement.qualifiers then
return nil
end
if args.rank == 'one' then
return formatQualifier( statement.qualifiers, qualifierProperty, args )
elseif args.rank == 'preferred' then --This should be set as the default case
if #sortedStatements == 1 then
return formatQualifier( statement.qualifiers, qualifierProperty, args ) --Output the only value
else
if statement.rank == 'preferred' then
table.insert( formattedQualifiers, formatQualifier( statement.qualifiers, qualifierProperty, args ) ) --Output only the preferred values
end
end
else
table.insert( formattedQualifiers, formatQualifier( statement.qualifiers, qualifierProperty, args ) )
end
end
return mw.text.listToText( formattedQualifiers, args.separator, args.conjunction )
end
return p