Aller au contenu

Module:Sourdant/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.
-- get informations about a reference from Wikidata
local p = {}
local wd = require("Module:Wikidata")

-- return the table of a reference with data from Wikidata
-- or false if reference doesn't exist
function p.get_reference(QID)
	entity = wd.getEntity(QID);
	if entity ~= nil then
		local nat = entity:getClaims('P31')
		if nat ~= nil then
			-- if it's a person Q5 (as Leon Bernus)
			if nat:hasValue("Q5") then
				local data = {}
				data.author = entity:getLabel()
				
				-- iw links
				data.wikisource = entity:getSitelink( 'wawikisource' )
				data.wikipedia = entity:getSitelink( 'wawiki' )
				
				-- image
				local img = entity:getBestStatements('P18')
				if img[1] ~= nil and img[1].mainsnak.datavalue ~= nil then
					data.image = img[1].mainsnak.datavalue.value
				end
				
				-- birth and death date
				local birthdate = p.get_year_from_date(entity:getBestStatements('P569'))
				local deathdate = p.get_year_from_date(entity:getBestStatements('P570'))
				data.year = table.concat({birthdate, ' — ', deathdate})
				return data
				
			-- if it's an edition, an article, a dictionary or a magazine
			elseif nat:hasValue("Q3331189") or
				   nat:hasValue("Q7725634") or
				   nat:hasValue("Q191067")  or
				   nat:hasValue("Q88392887")or
				   nat:hasValue("Q41298")   or
				   nat:hasValue("Q23622")   then
				local data = {}
				data.author = {}
				local authors = entity:getBestStatements('P50')
				local i = 1
				while( authors[i] ~= nil and authors[i].mainsnak.datavalue ~= nil ) do
					table.insert(data.author, wd.getLabel(authors[i].mainsnak.datavalue.value.id))
					i = i + 1 
				end
				local authors_str = entity:getBestStatements('P2093')
				local i = 1
				while( authors_str[i] ~= nil and authors_str[i].mainsnak.datavalue ~= nil ) do
					table.insert(data.author, authors_str[i].mainsnak.datavalue.value)
					i = i + 1 
				end
				data.title = entity:getBestStatements('P1476')[1].mainsnak.datavalue.value.text
				data.year = p.get_year_from_date(entity:getBestStatements('P577'))
				
				-- image
				local img = entity:getBestStatements('P18')
				if img[1] ~= nil and img[1].mainsnak.datavalue ~= nil then
					data.image = img[1].mainsnak.datavalue.value
				else
					local imgFS = entity:getBestStatements('P996')
					if imgFS[1] ~= nil and imgFS[1].mainsnak.datavalue ~= nil then
						data.image = imgFS[1].mainsnak.datavalue.value
					end
				end
				
				-- iw links
				data.wikisource = entity:getSitelink( 'wawikisource' )
				data.wikipedia = entity:getSitelink( 'wawiki' )
				return data
			end
		end
	end
	return nil 
end

function p.get_year_from_date(dateWD)
	if dateWD == nil or dateWD[1] == nil then dateWD = "?" else 
		if dateWD[1].mainsnak.datavalue.value.precision == 7 then
			dateWD = (tonumber(string.sub(dateWD[1].mainsnak.datavalue.value.time, 2, 3))+1).."<sup>inme</sup> sièke"
		elseif dateWD[1].mainsnak.datavalue.value.precision == 8 then
			dateWD = string.sub(dateWD[1].mainsnak.datavalue.value.time, 2, 4).."x"
		else
			dateWD = string.sub(dateWD[1].mainsnak.datavalue.value.time, 2, 5)
		end
	end
	return dateWD
end

return p