Aller au contenu

Module:Wikidata

Èn årtike di Wiccionaire.
I gn a nén co di dzo-pådje /doc pol notule d’ esplikêyes. Clitchîz cial po l’ ahiver.
local p = {}
local defaultlang = mw.getContentLanguage():getCode()
local Entity = {}
local Claim = {}
Entity.__index = Entity
Claim.__index = Claim

-- @claims table tåvlea des declaråcions
-- @value string Qid u on tecse
function p.filterClaims(claims, value)
	if type(value) ~= "string" or
	   type(claims) ~= "table" then return nil end
	
	for i, prop in ipairs(claims) do
		if (prop.mainsnak.datavalue.type == "wikibase-entityid" and 
		    prop.mainsnak.datavalue.value.id == value) or
		   (prop.mainsnak.datavalue.type == "string" and
		   	prop.mainsnak.datavalue.value == value) then
		   	return prop
		end
	end
	return nil
end

-- @propertyId string Pid d' ene prôpieté
-- @return tåvlea ki contént les declaråcions d' ene intité
function Claim:hasValue(value)
	if not value then return nil end
	if not self:isClaim() then return nil end
	for i, prop in ipairs(self.data) do
		if (prop.mainsnak.datavalue.type == "wikibase-entityid" and 
		    prop.mainsnak.datavalue.value.id == value) or
		   (prop.mainsnak.datavalue.type == "string" and
		   	prop.mainsnak.datavalue.value == value) then
		   	return true
		end
	end
	return nil
end

-- @entity string|table Qid u bén mwaisse snak table
-- @propertyId string Pid d' ene prôpieté
function p.getClaims(entity, propertyId)
	if p.isEntity(entity) and entity.claims then
		if propertyId and entity.claims[propertyId] then
			return entity.claims[propertyId]
		end
		return entity.claims
	elseif type(entity) == 'string' then
		return mw.wikibase.getBestStatements(entity, propertyId)
	end
	return nil
end

-- @propertyId string Pid d' ene prôpieté
-- @return tåvlea ki contént les declaråcions d' ene intité
function Entity:getClaims(propertyId)
	if self:isEntity() and self.claims then
		if propertyId and self.claims[propertyId] then
			Claim.data = self.claims[propertyId]
		else
			Claim.data = self.claims
		end
		Claim.type = "statements"
		setmetatable(Claim, {
			__index = function (t,k) return Claim.data[k] end,
			__call = function() return Claim.data end,
		})
		return Claim
	end
	return nil
end

-- @entity string|table Qid u bén mwaisse snak table
-- @lang string côde do 
-- @return string li label di l' intité dins on lingaedje
function p.getLabel(entity, lang)
	if not entity then return nil end
	lang = lang or defaultlang

	if p.isEntity(entity) and entity.labels and entity.labels[lang] then
		return entity.labels[lang].value
	elseif type(entity) == 'string'	and lang == defaultlang then
		local str = mw.wikibase.label(entity)
		 -- mw.wikibase.label() ni rote nén avou les rdjiblaedjes 
		 -- https://phabricator.wikimedia.org/T157868
		if str then	return str end
	end
	
	return mw.wikibase.getLabelByLang(entity, lang)
end

-- @entity string|table Qid u bén mwaisse snak table
-- @lang string côde do lingaedje
-- @return string li discrijhaedje di l' intité dins on lingaedje
function p.getDescription(entity, lang)
	if not entity then return nil end
	lang = lang or defaultlang

	if p.isEntity(entity) and entity.descriptions and entity.descriptions[lang] then
		return entity.descriptions[lang].value
	elseif type(entity) == 'string'	and lang == defaultlang then
		local str = mw.wikibase.description(entity)
		if str then	return str end
	end
	
	return mw.wikibase.getDescriptionByLang(entity, lang)
end

-- @id string come Qid
-- @return table
function p.getEntity(id) 
	if id and type(id) ~= 'string' then return nil end
	Entity.data = mw.wikibase.getEntity(id)
	setmetatable(Entity, {
		__index = function (t,k) return Entity.data[k] end,
		__call = function() return Entity.data end
	})
	return Entity
end

-- @entity table tåvlea Wikidata d' on tipe item
-- @return bool
function p.isEntity(entity)
	return type(entity) == "table" and 
	       entity.type and entity.type == "item"
end

-- @return bool si Entity est on obdjet Wikidata d' on tipe item
function Entity:isEntity()
	return type(self) == "table" and 
	       self.type and self.type == "item"
end

-- @return bool si Claim est on obdjet d' on tipe statements
function Claim:isClaim()
	return type(self) == "table" and 
	       self.type and self.type == "statements"
end

return p