Modul:Wikidata/sandbox
Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Wikidata/sandbox/Dokumentace
local i18n = {
["errors"] = {
["param-not-provided"] = "Nezadán parametr %s",
["entity-not-found"] = "Entita nenalezena",
["unknown-claim-type"] = "Neznámý typ tvrzení: %s",
["unknown-snak-type"] = "Neznámý typ snaku: %s",
["unknown-datavalue-type"] = "Neznámý typ datové hodnoty: %s",
["unknown-entity-type"] = "Neznámý typ entity: %s",
["unknown-value-module"] = "Musíte nastavit oba parametry value-module i value-function",
["value-module-not-found"] = "Modul odkazovaný parametrem value-module nenalezen",
["value-function-not-found"] = "Funkce odkazovaná parametrem value-function nenalezena",
["invalid-field"] = "Neplatné pole %s"
},
["somevalue"] = "''neznámá hodnota''",
["novalue"] = "''bez hodnoty''"
}
function wrapWithSpan(str, param, value)
return '<span ' .. param .. '="' .. value .. '">' .. str .. '</span>'
end
function formatError( key, ... )
return wrapWithSpan(string.format( i18n.errors[key], ... ), 'class', 'error')
end
function getEntityFromId( id )
return mw.wikibase.getEntityObject( id )
end
function getEntityIdFromValue( value )
local entityType = value['entity-type']
if entityType == 'item' then
return 'q' .. value['numeric-id']
elseif entityType == 'property' then
return 'p' .. value['numeric-id']
else
return formatError( 'unknown-entity-type', entityType )
end
end
--[[
function findEntity(options)
if not options.of or options.of == '' then
return options.item
else
local prop = options.of:upper()
local entity = getEntityFromId(options.item)
local q = nil
if entity.claims[prop] then
for i, statement in pairs( entity.claims[prop] ) do
if (statement.rank == "preferred" or statement.rank == "normal")
and statement["type"] == "statement"
and statement.mainsnak.datavalue["type"] == "wikibase-entityid"
and statement.mainsnak.snaktype == "value"
then
q = "q" .. tostring(statement.mainsnak.datavalue.value["numeric-id"])
end
end
end
end
if q and q ~= 'q' then
return q
else
return nil
end
end
]]--
function getSitelink(options)
-- options.item = findEntity(options)
local site = nil
if options.site ~= nil and options.site ~= '' then
site = options.site
elseif options[1] ~= nil and options[1] ~= '' then
site = options[1]
else
return formatError( 'param-not-provided', 'site' )
end
local entity = nil
entity = getEntityFromId( options.item )
if not entity or not entity.sitelinks then
return ''
end
local sitelink = nil
if entity.sitelinks[site] then
sitelink = entity.sitelinks[site].title
end
if not sitelink then
return ''
elseif not options.pattern or options.pattern == '' then
return sitelink
else
return formatFromPattern(sitelink, options)
end
end
function formatStatements( options )
-- options.item = findEntity(options)
--Get entity
local entity = nil
if options.entity and type( options.entity ) == "table" then
entity = options.entity
else
entity = getEntityFromId( options.item )
end
if not entity then
return '' --TODO error?
end
if not entity.claims or not entity.claims[string.upper(options.property)] then
return '' --TODO error?
end
local limit
if options.limit and options.limit ~= '' and type(options.limit) == 'number' then
limit = tonumber(options.limit)
else
limit = 100
end
--Format statements and concat them cleanly
local formattedStatements = {}
for i, statement in pairs( entity.claims[options.property:upper()] ) do
if options.rank == 'one' then
return formatStatement( statement, options ) --Output only one value
elseif (
((not options.rank or options.rank == 'normal') and (statement.rank == 'normal' or statement.rank == 'preferred')) or
(options.rank == 'preferred' and statement.rank == 'preferred') or
(options.rank == 'all')
) then
if options.withqualifier and options.withqualifier ~= '' then
if statement.qualifiers and statement.qualifiers[options.withqualifier:upper()] then
if options.withsource and options.withsource ~= '' then
if statement.references then
for j, ref in pairs( statement.references ) do
if ref.snaks[options.withsource:upper()] then
table.insert( formattedStatements, formatStatement( statement, options ) )
end
end
end
else
table.insert( formattedStatements, formatStatement( statement, options ) )
end
end
elseif options.withsource and options.withsource ~= '' then
if statement.references then
for j, ref in pairs( statement.references ) do
if ref.snaks[options.withsource:upper()] then
table.insert( formattedStatements, formatStatement( statement, options ) )
end
end
end
else
table.insert( formattedStatements, formatStatement( statement, options ) )
end
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', statement.type )
end
local Qualifiers = {}
if statement.qualifiers then
Qualifiers = statement.qualifiers
end
--[[
if options.showsource == 'true' then
if statement.refereces then
local useModule = require ('Module:Wikidata/sources')
if not useModule then return '' end
local useFunction = formatSource
if not useModule.useFunction then return '' end
local Sources = useModule.useFunction( statement.references, options )
end
end
]]--
return formatSnak( statement.mainsnak, options, Qualifiers )
end
function formatSnak( snak, options, qualifiers )
if snak.snaktype == 'value' then
return formatDatavalue( snak.datavalue, options, qualifiers )
elseif snak.snaktype == 'somevalue' then
return i18n['somevalue']
elseif snak.snaktype == 'novalue' then
return i18n['novalue']
else
return formatError( 'unknown-snak-type', snak.snaktype )
end
end
function formatDatavalue( datavalue, options, qualifiers )
local formated = nil
--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
formated = fun( datavalue.value, options )
end
--Default formatters
if datavalue.type == 'wikibase-entityid' then
formated = formatEntityId( getEntityIdFromValue( datavalue.value ) )
elseif datavalue.type == 'string' then
if options.pattern and options.pattern ~= '' then
formated = formatFromPattern( datavalue.value, options )
else
formated = datavalue.value
end
elseif datavalue.type == 'time' then
local Time = require 'Module:Time'
formated = Time.newFromWikidataValue( datavalue.value ):toString()
elseif datavalue.type == 'globecoordinate' then
if not options.field then
return formatError( 'param-not-provided', 'field' )
elseif options.field == "latitude" or options.field == "longitude" or options.field == "precision" or options.field == "globe" then
formated = datavalue.value[options.field]
else
return formatError( 'invalid-field', options.field )
end
elseif datavalue.type == 'monolingualtext' then
formated = wrapWithSpan(datavalue.value.text, 'lang', datavalue.value.language)
elseif datavalue.type == 'quantity' then
formated = tonumber(datavalue.value.amount)
else
return formatError( 'unknown-datavalue-type', datavalue.type )
end
if options.showqualifier and options.showqualifier ~= '' then
local formattedQualifiers = {}
if qualifiers and qualifiers ~= {} and qualifiers[options.showqualifier:upper()] then
for k, qualData in pairs( qualifiers[options.showqualifier:upper()] ) do
table.insert( formattedQualifiers, formatQualifiers(qualData.datavalue) )
end
if formattedQualifiers and formattedQualifiers ~= {} then
return connectQualifiers(formated, formattedQualifiers) or formated
end
end
end
return formated
end
function formatQualifiers(datavalue)
if datavalue['type'] == "wikibase-entityid" then
return formatEntityId( getEntityIdFromValue( datavalue.value ) )
elseif datavalue['type'] == "time" then
local Date = require 'Module:Wikidata/datum'
return Date.formatDate( datavalue.value )
elseif datavalue['type'] == "string" then
return datavalue.value
elseif datavalue['type'] == 'globecoordinate' then
return datavalue.value.latitude .. ' ' .. datavalue.value.longitude
elseif datavalue['type'] == "monolingualtext" then
return wrapWithSpan(datavalue.value.text, 'lang', datavalue.value.language)
elseif datavalue['type'] == 'quantity' then
return tonumber(datavalue.value.amount)
else
return i18n['somevalue']
end
end
function connectQualifiers(value, qualifier)
return value .. ' (' .. mw.text.listToText(qualifier) .. ')'
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
elseif label then
return wrapWithSpan(label, 'class', 'InfoFromWikidata')
else
return getLinkWhenNonexistingLabel(entityId)
end
end
function getLinkWhenNonexistingLabel( entityId )
local title = mw.title.getCurrentTitle().text
return '[[Soubor:Wikidata-edit.svg|27x17px|link=d:' .. entityId .. '|Položka na Wikidatech neobsahuje český štítek; můžete ho doplnit]]<code>[[d:' .. entityId .. '|' .. entityId .. ']]</code>[[Kategorie:Údržba:Doplnit štítek na Wikidatech|' .. title .. ']]'
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.dumpWikidataEntity ( frame )
local args = frame and frame.args or nil
return mw.dumpObject( getEntityFromId( args and args.id or nil ) )
end
function p.getSitelink( frame )
return getSitelink( frame.args )
end
function p.formatStatements( frame )
local args = frame.args
if not args.property or args.property == '' then
return formatError( 'param-not-provided', 'property' )
end
--If a value is 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 is already set, use it
if options.value and options.value ~= '' then
return options.value
end
return formatStatements( options )
end
return p