Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Wikidata/build/Dokumentace

-- Tato stránka je pravidelně aktualizována robotem. Jakákoliv modifikace bude při příští aktualizaci přepsána a je třeba ji provádět na Wikipedii. 

local p = {}

local lib = require 'Modul:Wikidata/lib'

p.datatypes = {}
for dt in pairs(lib.datatypeToValueType) do
	table.insert(p.datatypes, dt)
end

function p.randomDatatype()
	return p.datatypes[math.floor(math.random(#p.datatypes))]
end

function p.randomLanguage()
	local langs = mw.language.fetchLanguageNames()
	local codes = {}
	for key in pairs( langs ) do
		table.insert( codes, key )
	end
	return codes[math.floor(math.random(#codes))]
end

local function buildAmount( number )
	return ( number < 0 and tostring( number ) ) or '+' .. tostring( number )
end

function p.buildValueFromDatatype( args )
	if lib.datatypeToValueType[args.datatype] == "string" then
		return args.value
	elseif args.datatype == "quantity" then
		if type( args.value ) == "table" then
			return args.value
		elseif type( args.value ) == "number" then
			return {
				amount = buildAmount( args.value ),
				lowerBound = args.diff and buildAmount( args.value - args.diff ),
				unit = args.unit and mw.ustring.format( 'http://www.wikidata.org/entity/%s', args.unit ) or "1",
				upperBound = args.diff and buildAmount( args.value + args.diff )
			}
		end
	elseif args.datatype == "wikibase-item" or args.datatype == "wikibase-property" then
		local value = {
			['entity-type'] = mw.text.split( args.datatype, '-', true )[2],
			['numeric-id'] = args.value,
		}
		value.id = lib.getEntityIdFromValue(value)
		return value
	elseif args.datatype == "time" then
		local obj = require('Modul:Time').newFromIso8601( args.value )
		local precision = obj.precision
		obj.precision = 14
		return {
			after = 0,
			before = 0,
			calendarmodel = "http://www.wikidata.org/entity/Q1985727",
			precision = precision,
			time = obj:toIso8601(),
			timezone = 0,
		}
	elseif args.datatype == "monolingualtext" then
		return {
			language = args.lang or p.randomLanguage(),
			text = args.value
		}
	end
	return error()
end

function p.buildSnak( args )
	if not args.snaktype or args.snaktype == "value" then
		return {
			datatype = args.datatype or error('Missing datatype'),
			datavalue = {
				type = lib.datatypeToValueType[args.datatype] or error('Invalid datatype'),
				value = p.buildValueFromDatatype( args )
			},
			snaktype = "value"
		}
	else
		return {
			datatype = args.datatype,
			snaktype = args.snaktype
		}
	end
end

function p.buildStatement( args )
	return {
		mainsnak = p.buildSnak( args ),
		rank = args.rank or 'normal',
		type = 'statement',
		--qualifiers = {},
		--references = {},
	}
end

return p