Túto dokumentáciu [vytvoriť] [obnoviť]
Dokumentácia Dokumentácia

Dokumentácia pre tento modul zatiaľ neexistuje. Môžete ju vytvoriť na Modul:Wikiúdaje/Dokumentácia


Ak máte otázku k tomuto modulu, alebo potrebujete jeho rozšírenie a neviete ho správne naformátovať, pýtajte sa v diskusii k modulu. Pokiaľ je potrebné modul urgentne opraviť, obráťte sa na technickú podporu.

local i18n = {
	["errors"] = {
		["param-not-provided"] = "Nezadaný parameter %s",
		["entity-not-found"] = "Entita nenájdená",
		["unknown-claim-type"] = "Neznámy typ tvrdenia: %s",
		["unknown-snak-type"] = "Neznámy typ snaku: %s",
		["unknown-datavalue-type"] = "Neznámy typ dátovej hodnoty: %s",
		["unknown-entity-type"] = "Neznámy typ entity: %s",
		["unknown-value-module"] = "Musíte nastaviť oba parametre value-module i value-function.",
		["value-module-not-found"] = "Modul odkazovaný parametrom value-module nenájdený.",
		["value-function-not-found"] = "Funkcia odkazovaná parametrom value-function nenájdená.",
		["invalid-field"] = "Neplatné pole %s"
	},
	["somevalue"] = "''neznáma 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 getSitelink(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
	if options.entity and type( options.entity ) == "table" then
		entity = options.entity
	else
		entity = getEntityFromId( options.item )
	end

	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 )
	if not options.property or options.property == '' then
		return formatError( 'param-not-provided', 'property' )
	end
	
	--Get entity
	local entity = nil
	if options.entity and type( options.entity ) == "table" then
		entity = options.entity
	else
		entity = getEntityFromId( options.item )
		options.entity = entity
	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 = 100
	if options.limit and options.limit ~= '' and tonumber(options.limit) ~= nil then
		limit = tonumber(options.limit)
	end
	-- dočasne, všetky "rank=one" premeniť na "limit=1"
	if options.rank == 'one' then
		limit = 1
		options.rank = "normal"
	end

	local Statements, oldStatements = {}
	-- find all values of the property
	for _, statement in pairs(entity.claims[options.property:upper()]) do
		table.insert(Statements, statementTable(statement))
	end
	-- apply filter by rank
	oldStatements, Statements = Statements, {}
	local hasPref = false
	for _, statement in pairs(oldStatements) do
		if statement[2] == "preferred" then
			hasPref = true
			break
		end
	end
	for _, statement in pairs(oldStatements) do
		if options.rank == "deprecated" or options.rank == "preferred" then
			if statement[2] == options.rank then
				table.insert(Statements, statement)
			end
		elseif hasPref then
			if statement[2] == "preferred" then
				table.insert(Statements, statement)
			end
		elseif statement[2] == "preferred" or statement[2] == "normal" then
			table.insert(Statements, statement)
		end
	end
	-- apply filter by snak type
	oldStatements, Statements = Statements, {}
	for _, statement in pairs(oldStatements) do
		if statement[3] == "value" then
			table.insert(Statements, statement)
		elseif options.showspecial == "true" then
			table.insert(Statements, statement)
		end
	end
	-- apply filter by qualifier property
	if options.withqualifier and options.withqualifier ~= '' then
		oldStatements, Statements = Statements, {}
		for _, statement in pairs(oldStatements) do
			for p in mw.text.gsplit( statement[4], ",", true) do
				if p == options.withqualifier:upper() then
					table.insert(Statements, statement)
				end
			end
		end
	end
	-- Format statements and concat them cleanly
	local formattedStatements = {}
	-- apply filter by limit
	for i = 1, limit do
		if Statements[i] then
			table.insert(formattedStatements, formatStatement( Statements[i][1], options) )
			if options.showmore and options.showmore == 'true' and i == limit and Statements[i+1] then
				local more = '…&nbsp;viac vo [[d:' .. entity.id ..'#' .. options.property:upper() .. '|Wikiúdajoch]]'
				table.insert(formattedStatements, more)
			end
		else break end
	end
	return mw.text.listToText( formattedStatements, options.separator, options.conjunction )
end

function statementTable(statement)
	local rank = statement.rank
	local snaktype = statement.mainsnak.snaktype
	local qualifiers = ''
	if statement.qualifiers then
		for PropQ in pairs(statement.qualifiers) do
			if qualifiers == '' then
				qualifiers = qualifiers .. PropQ
			else
				qualifiers = qualifiers .. ',' .. PropQ
			end
		end
	end
--	local references = statement.references or {}
	return { statement, rank, snaktype, qualifiers --[[, references ]] }
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
	
	local References = {}
	if statement.references then
		References = statement.references
	end

	return formatSnak( statement.mainsnak, options, Qualifiers, References )
end

function formatSnak( snak, options, qualifiers, references )
	if snak.snaktype == 'value' then
		return formatDatavalue( snak.datavalue, options, qualifiers, references )
	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, references )
	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 not formated then
		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
	end

	-- Connect qualifiers to the value if needed
	if qualifiers and qualifiers ~= {} and options.showqualifier and options.showqualifier ~= '' then
		local QualList = {}
		for PropQ in mw.text.gsplit( options.showqualifier, ",", true) do
			local ValuesList = {}
			PropQ = PropQ:upper()
			if qualifiers[PropQ] then
				for _, qualData in pairs(qualifiers[PropQ]) do
					if qualData.snaktype == "value" then
						table.insert( ValuesList, formatQualifiers(qualData.datavalue) )
					end
				end
			elseif PropQ == "time" then
				if qualifiers.P580 or qualifiers.P582 then
					if qualifiers.P580 and qualifiers.P580[1] and qualifiers.P580[1].snaktype == "value" then
						if qualifiers.P582 and qualifiers.P582[1] and qualifiers.P582[1].snaktype == "value" then
							table.insert( ValuesList, formatQualifiers(qualifiers.P580[1].datavalue) .. '—' .. formatQualifiers(qualifiers.P582[1].datavalue) )
						else
							table.insert( ValuesList, 'od ' .. formatQualifiers(qualifiers.P580[1].datavalue) )
						end
					elseif qualifiers.P582 and qualifiers.P582[1] and qualifiers.P582[1].snaktype == "value" then
						table.insert( ValuesList, 'do ' .. formatQualifiers(qualifiers.P582[1].datavalue) )
					end
				end
			end
			if mw.text.listToText(ValuesList) ~= '' then
				table.insert( QualList, mw.text.listToText(ValuesList) )
			end
		end
		if mw.text.listToText(QualList) ~= '' then
 			formated = formated .. ' (' .. mw.text.listToText(QualList, '; ', '; ') .. ')'
 		end
	end
	
	-- Connect references to the value with qualifiers if needed
	if references and references ~= '' and options.showsource and options.showsource == 'true' then
		local useModule = require ('Module:Wikidata/cite')
		if useModule then
			local useFunction = "formatSource"
			if useModule[useFunction] then
				local Sources = useModule[useFunction](references, options)
				formated = formated .. Sources
			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 'Modul:Wikiúdaje/dátum'
		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 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 '[[Súbor:Wikidata-edit.svg|27x17px|link=d:' .. entityId .. '|Položka na Wikiúdajoch neobsahuje slovenský štítok; môžete ho doplniť]]<code>[[d:' .. entityId .. '|' .. entityId .. ']]</code>[[Kategória:Doplniť štítok na Wikiúdajoch|' .. 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.getBadge( frame )
	local args = frame and frame.args or nil
	local site = args and args.site or nil
	local entity = getEntityFromId( args and args.item or nil )
	local label = ''
	if entity and entity.sitelinks and site and entity.sitelinks[site] then
		for i in pairs(entity.sitelinks[site].badges ) do
			if entity.sitelinks[site].badges[i] == 'Q17437796' then
				label = mw.wikibase.label('Q17437796')
				break
			elseif entity.sitelinks[site].badges[i] == 'Q17437798' then
				label = mw.wikibase.label('Q17437798')
				break
			end
		end
	end
	return label
end

function p.getLabel( frame )
	local args = frame and frame.args or nil
	local lang = args and args.lang or nil
	local entity = getEntityFromId( args and args.item or nil )
	if not lang or lang == '' then
		lang = "sk"
	end
	if not entity or not entity.labels or not entity.labels[lang] then
		return ''
	end
	return entity.labels[lang].value
end

function p.getDescription( frame )
	local args = frame and frame.args or nil
	local lang = args and args.lang or nil
	local entity = getEntityFromId( args and args.item or nil )
	if not lang or lang == '' then
		lang = "sk"
	end
	if not entity or not entity.descriptions or not entity.descriptions[lang] then
		return ''
	end
	return entity.descriptions[lang].value
end

function p.getAliases( frame )
	local args = frame and frame.args or nil
	local lang = args and args.lang or nil
	local entity = getEntityFromId( args and args.item or nil )
	if not lang or lang == '' then
		lang = "sk"
	end
	if not entity or not entity.aliases or not entity.aliases[lang] then
		return ''
	end
	
	local limit = 100
	if args.limit and args.limit ~= '' and tonumber(args.limit) ~= nil then
		limit = tonumber(args.limit)
	end

	local Aliases = {}
	for i, alias in pairs( entity.aliases[lang] ) do
		if i <= limit then
			table.insert( Aliases, alias.value )
		else break end
	end
	return mw.text.listToText( Aliases, args.separator, args.conjunction ) or ''
end

function p.formatStatements( frame )
	local args = frame and frame.args or nil
	--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