Modul:Wikidata: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
HenkvD (bicara | kontrib)
NO error "entity-not-found"
update
Baris 1:
local i18np = {}
["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-modul"] = "You must set both value-modul and value-function parameters.",
["value-modul-not-found"] = "The modul pointed by value-modul not found.",
["value-function-not-found"] = "The function pointed by value-function not found."
},
["somevalue"] = "''unknown value''",
["novalue"] = "''no value''"
}
 
-- This is used to get a normal wiki-linked value, or a comma separated list of them if multiple values exist
function getEntityFromId( id )
p.getValue = function(frame)
if id then
local propertyID return= mw.wikibasetext.getEntitytrim(frame.args[1] idor "")
local input_parm = mw.text.trim(frame.args[2] or "")
end
if input_parm == "FETCH_WIKIDATA" then
return mw.wikibase.getEntity()
local entity = mw.wikibase.getEntity()
end
if entity.claims[propertyID] ~= nil then
 
local out = {}
function getEntityIdFromValue( value )
for k, v in pairs(entity.claims[propertyID]) do
local prefix = ''
out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) .. "]]"
if value['entity-type'] == 'item' then
prefix = 'q' end
return table.concat(out, ", ")
elseif value['entity-type'] == 'property' then
prefix = 'p'else
return ""
end
else
return formatError( 'unknown-entity-type' )input_parm
end
return prefix .. value['numeric-id']
end
 
-- This is used to get a value like 'male' (for property p21) which won't be linked
function formatError( key )
p.getRawValue = function(frame)
return '<span class="error">' .. i18n.errors[key] .. '</span>'
local propertyID = mw.text.trim(frame.args[1] or "")
end
local input_parm = mw.text.trim(frame.args[2] or "")
 
if input_parm == "FETCH_WIKIDATA" then
 
local entity = mw.wikibase.getEntity()
function formatStatements( options )
if not options if entity.propertyclaims[propertyID] ~= nil then
local out = {}
return formatError( 'property-param-not-provided' )
for k, v in pairs(entity.claims[propertyID]) do
end
out[#out + 1] = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
 
--Get entity end
return table.concat(out, ", ")
local entity = getEntityFromId( options.entityId )
if not entity then else
return -- formatError( 'entity-not-found' )return ""
end
 
if (entity.claims == nil) 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[string.lower(options.property)] ) do
table.insert( formattedStatements, formatStatement( statement, options ) )
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-modul'] or options['value-function'] then
if not options['value-modul'] or not options['value-function'] then
return formatError( 'unknown-value-modul' )
end
local formatter = require ('Modul:' .. options['value-modul'])
if formatter == nil then
return formatError( 'value-modul-not-found' )
end
local fun = formatter[options['value-function']]
if fun == nil 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
return datavalue.value --TODO ids + media
else
return formatError( 'unknown-datavalue-type' )input_parm
end
end
 
-- This is used to get a date value for date_of_birth (p569), etc. which won't be linked -- consolidate by testing if entity.claims[propertyID].mainsnak.datavalue.type is "time"
function formatEntityId( entityId, options )
p.getDateValue = function(frame)
local label = mw.wikibase.label( entityId )
local linkpropertyID = mw.wikibasetext.sitelinktrim(frame.args[1] entityIdor "")
local input_parm = mw.text.trim(frame.args[2] or "")
if link then
local date_format = mw.text.trim(frame.args[3] or "dmy")
if label then
if input_parm == "FETCH_WIKIDATA" then
return '[[' .. link .. '|' .. label .. ']]'
local entity = mw.wikibase.getEntity()
if entity.claims[propertyID] ~= nil then
local out = {}
local dt = {}
for k, v in pairs(entity.claims[propertyID]) do
local d = v.mainsnak.datavalue.value.time
dt.year = string.sub(d, 9, 12)
dt.month = string.sub(d, 14, 15)
dt.day = string.sub(d, 17, 18)
if date_format == "dmy" then
out[#out + 1] = os.date("%e %B %Y", os.time(dt))
else
out[#out + 1] = os.date("%B %e, %Y", os.time(dt))
end
end
return table.concat(out, ", ")
else
return '[[' .. link .. ']]'""
end
else
return input_parm
return label --TODO what if no links and label + fallback language?
end
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