Moduuli:Cs:Wikidata/Statement

Wikipediasta
Siirry navigaatioon Siirry hakuun

require "Module:No globals"

local p = {}
local lib = require 'Module:cs:Wikidata/lib'
local Formatters = require 'Module:cs:Wikidata/Formatters'

function p.formatMainsnak(mainsnak, options)
	return Formatters.getFormattedValue(mainsnak, options)
end

function p.formatQualifiers(qualifiers, options)
	local Filterers = require 'Module:cs:Wikidata/Filterers'
	local PropList = lib.textToTable(options.showqualifier)
	local Snaks = {}
	for _, property in ipairs(PropList) do
		local Values = {}
		local property = mw.ustring.upper(property)
		local format_options = {
			autoformat = true,
			isQualifier = true,
			precision = options.qual_precision or 9,
			property = property,
		}
		if property == "TIME" then
			local Data = {}
			for key, array in pairs(lib.props) do
				for _, prop in ipairs(array) do
					for _, snak in ipairs(qualifiers[prop] or {}) do
						if lib.IsSnakValue(snak) then
							Data[key] = Formatters.getRawValue(snak)
							break
						end
					end
				end
			end
		elseif qualifiers[property] then
			local Qualifiers = Filterers.filterQualifiers(qualifiers[property], options)
			for _, snak in ipairs(Qualifiers) do
				if lib.IsSnakValue(snak) then
					table.insert(Values, Formatters.getFormattedValue(snak, format_options))
				end
			end
		end
		if #Values > 0 then
			table.insert(Snaks, mw.text.listToText(Values))
		end
	end
	if #Snaks > 0 then
		return table.concat(Snaks, '; ')
	end
	return nil
end

function p.formatTargetdata(mainsnak, options)
	local entity
	if lib.IsSnakValue(mainsnak) then
		if mainsnak.datavalue.type == 'wikibase-entityid' then
			entity = getEntityFromId(Formatters.getRawValue(mainsnak))
		else
			return error(lib.raiseInvalidDatatype('formatTargetdata', mainsnak.datatype, {'wikibase-item', 'wikibase-property'}))
		end
	end

	local Filterers = require 'Module:cs:Wikidata/Filterers'
	if entity then
		local PropList = lib.textToTable(options.showtargetdata)
		local options = {
			autoformat = true,
			date = options.targetdate,
			entity = entity,
			isQualifier = true,
			precision = 9,
			rank = (options.targetdate and 'valid') or 'best',
			sort = 'date'
		}
		local Snaks = {}
		for _, property in ipairs(PropList) do
			local result
			if mw.ustring.lower(property) == 'time' then -- TODO: pryč s tím
				local Data = {}
				for key, array in pairs(lib.props) do
					for _, prop in ipairs(array) do
						options.property = prop
						local Statements = Filterers.filterStatementsFromEntity(entity, options)
						for _, statement in ipairs(Statements) do
							Data[key] = Formatters.getRawValue(statement.mainsnak)
							break
						end
					end
				end
				local Date = require 'Module:cs:Wikidata/datum'
				if Data.point then
					result = Date.formatDateFromTimevalue(Data.point, options)
				elseif Data.begin or Data.ending then
					result = Date.formatDateRange(Data, options)
				end
			else
				options.property = property
				local Module = require 'Module:cs:Wikidata'
				result = Module.formatStatementsFromLua(options)
			end
			if result then
				table.insert(Snaks, result)
			end
		end
		if #Snaks > 0 then
			return table.concat(Snaks, '; ')
		end
	end
	return nil
end

function p.formatStatement(statement, options)
	local mainsnak, qualifiers, targetdata, references
	if not lib.IsOptionTrue(options, 'qualifiersOnly') then -- b/c
		mainsnak = p.formatMainsnak(statement.mainsnak, options)
		if options.isQualifier == true then
			return mainsnak
		end
	end

	if statement.qualifiers and options.showqualifier then
		qualifiers = p.formatQualifiers(statement.qualifiers, options)
	end

	if not qualifiers and options.showtargetdata then
		targetdata = p.formatTargetdata(statement.mainsnak, options)
	end

	if statement.references and lib.IsOptionTrue(options, 'showsource') then
		local Module = require 'Module:Wikidata/cite'
		references = Module.formatSource(statement.references, options)
	end

	if not mainsnak then
		mainsnak = qualifiers or targetdata or ''
	elseif qualifiers or targetdata then
		if options.delimiter then
			mainsnak = mainsnak .. options.delimiter .. (qualifiers or targetdata)
		else
			mainsnak = mainsnak .. ' (' .. (qualifiers or targetdata) .. ')'
		end
	end

	if references then
		mainsnak = mainsnak .. references
	end
	return mainsnak
end

return p