Mòdul:Wikidata: diferència entre les revisions
Contingut suprimit Contingut afegit
monolingualtext: selecció en català |
protecció casos formatting amb monolingualtext |
||
Línia 160: | Línia 160: | ||
local function printDatavalueMonolingualText(data, parameter, lang) |
local function printDatavalueMonolingualText(data, parameter, lang) |
||
-- data fields: language [string], text [string] |
-- data fields: language [string], text [string] |
||
if parameter then |
if parameter == "language" or parameter == "text" then |
||
return data[parameter] |
return data[parameter] |
||
elseif lang == "ca" then |
elseif lang == "ca" then |
Revisió del 16:04, 26 maig 2016
Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]
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 p = {}
-- module local variables
local wiki =
{
langcode = mw.language.getContentLanguage().code
}
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''",
["warnDump"] = "[[Categoria:Funció Dump del mòdul Wikidata]]",
["datetime"] =
{
-- $1 is a placeholder for the actual number
[0] = "$1 mil milions d'anys", -- precision: billion years
[1] = "$100 milions d'anys", -- precision: hundred million years
[2] = "$10 milions d'anys", -- precision: ten million years
[3] = "$1 milions d'anys", -- precision: million years
[4] = "$100.000 anys", -- precision: hundred thousand years
[5] = "$10.000 anys", -- precision: ten thousand years
[6] = "mil·lenni $1", -- precision: millennium
[7] = "segle $1", -- precision: century
[8] = "dècada del $1", -- precision: decade
-- the following use the format of #time parser function
[9] = "Y", -- precision: year,
[10] = "F Y", -- precision: month
[11] = "j F Y", -- precision: day
[12] = "j F Y ga", -- precision: hour
[13] = "j F Y g:ia", -- precision: minute
[14] = "j F Y g:i:sa", -- precision: second
["beforenow"] = "fa $1", -- how to format negative numbers for precisions 0 to 5
["afternow"] = "d'aquí $1", -- how to format positive numbers for precisions 0 to 5
["bc"] = '$1 aC', -- how print negative years
["ad"] = "$1" -- how print positive years
},
["monolingualtext"] = '<span lang="%language">%text</span>'
}
local function printDatavalueString(data, parameter)
if parameter == 'weblink' then
return '[' .. data .. ' ' .. mw.text.split(data, '//' )[2] .. ']'
elseif mw.ustring.find((parameter or ''), '$1', 1, true) then -- formatting = a pattern
return mw.ustring.gsub(parameter, '$1', data) .. '' -- Hack to get only the first result of the function
else
return data
end
end
local function printDatavalueCoordinate(data, parameter)
local globetable = {Q2 = 'earth', Q111 = 'mars', Q405 = 'moon'}
if parameter == 'latitude' then
return data.latitude
elseif parameter == 'longitude' then
return data.longitude
elseif parameter == 'dimension' then
return data.dimension
else
if data.globe == '' or data.globe == nil then
return 'earth'
else
local globenum = mw.text.split(data.globe, 'entity/')[2] -- http://www.wikidata.org/wiki/Q2
if globetable[globenum] then
return globetable[globenum]
else
return globenum
end
end
end
end
local function printDatavalueQuantity(data, args)
-- exemples: 277±1 Centímetre, 1,94 metre
local itemID = args.item; if itemID == "" then itemID = nil end
local entity = mw.wikibase.getEntityObject(itemID)
local result = entity:formatPropertyValues(string.upper(args.property)).value
result = mw.ustring.gsub(result, "±%d+", "")
if args.formatting ~= "unit" then
result = mw.ustring.gsub(result, "(%d) %D+", "%1")
end
result = mw.ustring.gsub(result, "%+", "")
return mw.text.trim(result)
end
local function printDatavalueTime(data, parameter)
-- Dates and times are stored in ISO 8601 format
-- check for negative date, ex. "-0027-01-16T00:00:00Z"
local suffix = ""
local timestamp = data.time
if string.sub(timestamp, 1, 1) == '-' then
timestamp = '+' .. string.sub(timestamp, 2)
suffix = " aC"
elseif string.sub(timestamp, 2, 3) == '00' then
suffix = " dC"
end
local function d(f)
return mw.language.new(wiki.langcode):formatDate(f, timestamp) .. suffix
end
local precision = data.precision or 11
local any = tonumber(mw.ustring.match(timestamp, "^\+?%d+"))
local ret = ""
-- precision is 10000 years or more
if precision <= 5 then
local factor = 10 ^ ((5 - precision) + 4)
local y2 = math.ceil(math.abs(any) / factor)
local relative = mw.ustring.gsub(i18n.datetime[precision], "$1", tostring(y2))
if suffix == " aC" then
ret = mw.ustring.gsub(i18n.datetime.beforenow, "$1", relative)
else
ret = mw.ustring.gsub(i18n.datetime.afternow, "$1", relative)
end
-- precision is decades, centuries or millennia
elseif precision == 6 then
ret = mw.ustring.gsub(i18n.datetime[6], "$1", tostring(math.floor((math.abs(any) - 1) / 1000) + 1)) .. suffix
elseif precision == 7 then -- segles
ret = mw.ustring.gsub(i18n.datetime[7], "$1", tostring(math.floor((math.abs(any) - 1) / 100) + 1)) .. suffix
elseif precision == 8 then
ret = mw.ustring.gsub(i18n.datetime[8], "$1", tostring(math.floor(math.abs(any) / 10) * 10)) .. suffix
-- precision is year
elseif parameter == 'year' or precision == 9 then
ret = tostring(any) .. suffix
-- precision is month
elseif precision == 10 then
ret, _ = string.gsub(d("F Y"), " 0+", " ")
else
ret, _ = string.gsub(d("j F Y"), " 0+", " ")
end
return ret
end
local function printDatavalueEntity(data, parameter)
local entityId = "Q" .. tostring(data['numeric-id'])
local label = mw.wikibase.label(entityId)
local sitelink = mw.wikibase.sitelink(entityId)
if parameter == 'raw' then
return entityId
elseif parameter == 'label' then
return (label or entityId)
elseif parameter == 'sitelink' then
return (sitelink or 'wikidata:' .. entityId)
else
return formatEntityId(entityId)
end
end
local function printDatavalueMonolingualText(data, parameter, lang)
-- data fields: language [string], text [string]
if parameter == "language" or parameter == "text" then
return data[parameter]
elseif lang == "ca" then
if data["language"] == lang then
return data["text"]
end
else
local result = mw.ustring.gsub(mw.ustring.gsub(i18n.monolingualtext, "%%language", data["language"]), "%%text", data["text"])
return result
end
return nil
end
local function getSnakValue(snak, args)
if snak.snaktype == 'value' then
-- call the respective snak parser
if snak.datavalue.type == "string" then
return printDatavalueString(snak.datavalue.value, args.formatting)
elseif snak.datavalue.type == "globecoordinate" then
return printDatavalueCoordinate(snak.datavalue.value, args.formatting)
elseif snak.datavalue.type == "quantity" then
return printDatavalueQuantity(snak.datavalue.value, args)
elseif snak.datavalue.type == "time" then
return printDatavalueTime(snak.datavalue.value, args.formatting)
elseif snak.datavalue.type == 'wikibase-entityid' then
return printDatavalueEntity(snak.datavalue.value, args.formatting)
elseif snak.datavalue.type == 'monolingualtext' then
return printDatavalueMonolingualText(snak.datavalue.value, args.formatting, args.rank)
else
return formatError('unknown-datavalue-type')
end
end
return nil
end
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 itemID = args.item; if itemID == "" then itemID = nil end
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(itemID)
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 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, args)
if not statement.type or statement.type ~= 'statement' then
return formatError( 'unknown-claim-type' )
end
return formatSnak(statement.mainsnak, args)
--TODO reference and qualifiers
end
function formatQualifier ( qualifiers, qualifierProperty, options )
if not qualifiers[qualifierProperty] then
return nil
end
return formatSnak( qualifiers[qualifierProperty][1], options )
end
function 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 getSnakValue(snak, args)
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
-- 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 #rawStatements == 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 with 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 = string.upper(args.property)
local qualifierProperty = string.upper(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 formattedQualifiers = {}
for i, statement in pairs( rawStatements ) do
if not statement.qualifiers or not statement.qualifiers[qualifierProperty] then
table.insert(formattedQualifiers, "")
elseif args.rank == 'one' then
return formatQualifier( statement.qualifiers, qualifierProperty, args )
elseif args.rank == 'preferred' then --This should be set as the default case
if #rawStatements == 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
-- This is used to get the TA98 (Terminologia Anatomica first edition 1998) values like 'A01.1.00.005' (property P1323)
-- which are then linked to http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/01.1.00.005%20Entity%20TA98%20EN.htm
-- uses the newer mw.wikibase calls instead of directly using the snaks
-- formatPropertyValues returns a table with the P1323 values concatenated with ", " so we have to split them out into a table in order to construct the return string
p.getTAValue = function(frame)
local ent = mw.wikibase.getEntityObject()
local props = ent:formatPropertyValues('P1323')
local out = {}
local t = {}
for k, v in pairs(props) do
if k == 'value' then
t = mw.text.split( v, ", ")
for k2, v2 in pairs(t) do
out[#out + 1] = "[http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/" .. string.sub(v2, 2) .. "%20Entity%20TA98%20EN.htm " .. v2 .. "]"
end
end
end
local ret = table.concat(out, "<br> ")
if #ret == 0 then
ret = "Invalid TA"
end
return ret
end
-- look into entity object
function p.ViewSomething(frame)
local f = (frame.args[1] or frame.args.item) and frame or frame:getParent()
local id = f.args.item
if id and (#id == 0) then
id = nil
end
local data = mw.wikibase.getEntityObject(id)
if not data then
return nil
end
local i = 1
while true do
local index = f.args[i]
if not index then
if type(data) == "table" then
return mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY)
else
return tostring(data)
end
end
data = data[index] or data[tonumber(index)]
if not data then
return
end
i = i + 1
end
end
-- Dump data tree structure
-- From pl:Module:Wikidane, by User:Paweł Ziemian
-- Funció pensada com a eina d'ajuda en previsualització.
function p.Dump(frame)
local data = mw.wikibase.getEntityObject()
if not data then
return i18n.warnDump
end
local f = frame.args[1] and frame or frame:getParent()
local i = 1
while true do
local index = f.args[i]
if not index then
return "<pre>" .. mw.dumpObject(data) .. "</pre>" .. i18n.warnDump
end
data = data[index] or data[tonumber(index)]
if not data then
return i18n.warnDump
end
i = i + 1
end
end
-- Look into entity object
-- From pl:Module:Wikidane, function V, by User:Paweł Ziemian
function p.getEntityFromTree(frame)
local data = mw.wikibase.getEntityObject()
if not data then
return nil
end
local f = frame.args[1] and frame or frame:getParent()
local i = 1
while true do
local index = f.args[i]
if not index then
return tostring(data)
end
data = data[index] or data[tonumber(index)]
if not data then
return
end
i = i + 1
end
end
return p