Modul:EntityBuilder

Izvor: Wječnik

Documentation for this module may be created at Modul:EntityBuilder/doc

local p = {}
local JSON = require('Module:JSON')

function p.newItem()
	return {}
end

function p.newProperty(datatype)
	return {datatype=datatype}
end

function p.addLabel(entity, language, label, action)
	local newentity=entity
	if not newentity.labels then
		newentity.labels={}
	end
	if newentity.labels[language] and action=="add" then
		return newentity
	else
		newentity.labels[language]={language=language,value=label}
		if action then
			newentity.labels[language][action]=""
		end	
		return newentity
	end
end

function p.addDescription(entity, language, description, action)
	local newentity=entity
	if not newentity.descriptions then
		newentity.descriptions={}
	end
	if newentity.descriptions[language] and action=="add" then
		return newentity
	else
		newentity.descriptions[language]={language=language,value=description}
		if action then
			newentity.descriptions[language][action]=""
		end	
		return newentity
	end
end

function p.addAlias(entity, language, alias, action)
	local newentity=entity
	if not newentity.aliases then
		newentity.aliases={}
	end
	actionitem={language=language,value=alias}
	if action then
		actionitem[action]=""
	end
	table.insert(newentity.aliases,actionitem)
end

function p.addSitelink(entity, site, sitelink, badges, action)
	local newentity=entity
	if not newentity.sitelinks then
		newentity.sitelinks={}
	end
	if newentity.sitelinks[site] and action=="add" and not (badges and badges~={}) then
		return newentity
	else
		newentity.sitelinks[site]={site=site,title=sitelink}
		if action then
			newentity.sitelinks[site][action]=""
		end	
		if badges then
			newentity.sitelinks[site].badges=badges
		end
		return newentity
	end
end

function p.ItemValue(id)
	return {value={["entity-type"]="item",["numeric-id"]=id},type="wikibase-entityid"}
end

function p.PropertyValue(id)
	return {value={["entity-type"]="property",["numeric-id"]=id},type="wikibase-entityid"}
end

function p.LexemeValue(id)
	return {value={["entity-type"]="lexeme",["numeric-id"]=id},type="wikibase-entityid"}
end

function p.FormValue(id)
	return {value={["entity-type"]="form",id=id},type="wikibase-entityid"}
end

function p.SenseValue(id)
	return {value={["entity-type"]="sense",id=id},type="wikibase-entityid"}
end

function p.StringValue(s)
	return {value=s,type="string"}
end

function p.CoordinateValue(lat,long,prec,globe)
	if not globe then
		globe="http:\/\/www.wikidata.org\/entity\/Q2"
	end
	return {value={latitude=lat,longitude=long,precision=prec,globe=globe},type="globecoordinate"}
end

function p.QuantityValue(amount,unit,upper_bound,lower_bound)
	if not unit then
		unit="1"
	end
	if upper_bound then
		return {value={amount=amount,upperBound=upper_bound,lowerBound=lower_bound,unit=unit},type="quantity"}
    else
		return {value={amount=amount,unit=unit},type="quantity"}
	end
end

function p.TimeValue()
	--to do
end

function p.Snak(property,value)
	return {snaktype="value",property=property,datavalue=value}
end

function p.NoValue(property)
	return {snaktype="novalue",property=property}
end

function p.SomeValue(property)
	return {snaktype="somevalue",property=property}
end

function p.newClaim(property,snak,rank)
	if not rank then
		rank="normal"
	end
	return {mainsnak=snak,type="statement",rank=rank}
end

function p.addReference(claim,snaks)
	local newclaim=claim
	if not newclaim.references then
		newclaim.references={}
	end
	table.insert(newclaim.references,{snaks=snaks})
	return newclaim
end

function p.addQualifier(claim,snak)
	local newclaim=claim
	if not newclaim.qualifiers then
		newclaim.qualifiers={}
	end
	if not newclaim.qualifiers[snak.property] then
		newclaim.qualifiers[snak.property]={}
	end
	table.insert(newclaim.qualifiers[snak.property],snak)
	return newclaim
end

function p.addClaim(entity,claim)
	newentity=entity
	if not newentity.claims then
		newentity.claims={}
	end
	if not newentity.claims[claim.mainsnak.property] then
		newentity.claims[claim.mainsnak.property]={}
	end
	table.insert(newentity.claims[claim.mainsnak.property],claim)
	return newentity
end

function p.Export(entity)
	return JSON:encode(entity)
end

return p