Module:Wikidata

Èn årtike di Wikipedia.
Notule d' esplikêyes

Ci module ci est eployî pa d' ôtes modules po rprinde des dnêyes ki sont so Wikidata. Vey eto Extension:Wikibase Client/Lua.

Tcherdjaedje[candjî l’ côde wiki]

Po poleur eployî ci module ci dins èn ôte, i l' fåt tcherdjî :

wd = require("Module:Wikidata")

Fonccions po les modules[candjî l’ côde wiki]

p.getEntity( id )

Dene li tåvlea des dnêyes di Wikidata po l' elemint id.
  • id (nén oblidjî) c'est l'id di Wikidata k' est dizo l' cogne Qxxx avou xxx come limero, metans pol Beldjike c' est Q31. Si on n' mete rén, adon c' est l' elemint raloyî a l' pådje k' est diné.
wd.getEntity("Q31")

p.getLabel( entity, lang ),
Entity:getLabel( lang )

Dene li label d'on elemint dins on lingaedje
  • entity c'est l' elemint di Wikidata ki pour esse on id u bén on tåvlea diné pa p.getEntity().
  • lang c'est l' code do lingaedje. Si on n' mete rén, adon c' est l' walon k' est eployî
wd.getLabel("Q31") -- dene Beldjike
wd.getLabel("Q31", "fr") -- dene Belgique
-- k' est l' minme ki
local entity = wd.getEntity("Q31")
wd.getLabel(entity) -- dene Beldjike
wd.getLabel(entity, "fr") -- dene Belgique
-- u co pus simplumint
entity:getLabel() -- dene Beldjike
entity:getLabel("fr") -- dene Belgique

p.getDescription( entity, lang ),
Entity:getDescription( lang )

Dene li discrijhaedje d'on elemint dins on lingaedje
  • entity c'est l' elemint di Wikidata ki pour esse on id u bén on tåvlea diné pa p.getEntity().
  • lang c'est l' code do lingaedje. Si on n' mete rén, adon c' est l' walon k' est eployî
wd.getDescription("Q31") -- dene payis di l' Urope do Coûtchant
wd.getDescription("Q31", "fr") -- dene État d'Europe occidentale
-- k' est l' minme ki
local entity = wd.getEntity("Q31")
wd.getDescription(entity) -- dene payis di l' Urope do Coûtchant
wd.getDescription(entity, "fr") -- dene État d'Europe occidentale
-- u co pus simplumint
entity:getDescription() -- dene payis di l' Urope do Coûtchant
entity:getDescription("fr") -- dene État d'Europe occidentale

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