Module:Infobox/Fonctions/Personne

De Lagny-sur-Marne Wiki
< Module:Infobox‎ | Fonctions
Révision datée du 2 septembre 2015 à 15:16 par Oliv0 (discussion) (si féminin : précédée, suivie)
Aller à : navigation, rechercher

La documentation pour ce module peut être créée à Module:Infobox/Fonctions/Personne/doc

-- Functions utilisées par les infobox personnes
local p = {}
local wikidata = require "Module:Interface Wikidata".fromLua
local general = require "Module:Infobox/Fonctions"
local datemodule = require "Module:Date"
local linguistic = require "Module:Linguistique"

 -- libellés spéciaux lorsque ceux de Wikconidata ne conviennent pas
local occupations = mw.loadData "Module:Dictionnaire Wikidata/Métiers"

local function setgender() -- établit la valeur de la variable "gender" qui sert à adapter la grammaire au sexe de la personne
	local validvalues = {
		['Q6581072'] = 'f',
		['Q6581097'] = 'm',
	}
	local gender = wikidata.formatStatements{entity = item, property = 'P21', displayformat = 'raw'}
	return validvalues[gender]
end
local gender = gender or setgender()

local function setgenderedlabels()
	if gender == 'f' then
		return occupations.female
	elseif gender == 'm' then
		return occupations.male
	end
end	
local genderedlabels = genderedlabels or setgenderedlabels()

local datelinks = { -- lien vers le domaine d'activité approprié
	Q483501 = 'en arts plastiques', -- artiste
	Q1028181 = 'en arts plastiques', -- peintre
	Q1281618 = 'en arts plastiques', -- sculpteur
	Q2309784 = 'en cyclisme', -- cycliste
	Q16947657 = 'en arts plastiques', -- lithographe
}

-- ========== Fonctions d'aide ====================================
local function getdatetopic() -- obtient le lien le plus approprié pour une date en fonction de la profession
	local claims = wikidata.getClaims{entity = item, property = 'P106', excludespecial = true}
	if not claims then
		return nil
	end
	for i, j in pairs(claims) do
		local v = wikidata.getmainid(j)
		if datelinks[v] then
			return datelinks[v]
		end
	end
end

local linktopic = getdatetopic()

local function timeline(statements, prop) -- affiche date : événement (suppose les événements déjà triés)
	if not statements then
		return nil
	end
	local rows = {}
	local previousstr = '' -- pour fusionner deux lignes sans différences visibles
	
	for i, statement in pairs(statements) do
		local mainstr = wikidata.formatStatement(statement, {speciallabels = genderedlabels})
		local period = wikidata.statementDate(statement, {precision = 'year', linktopic = '-'})
			
		-- rétrolien à activer si on veut inciter les gens à aller sur Wikidata
		--if (not period) and item then
		--	period = '[[:d:' .. item.id .. '#' .. (prop or '') .. '|<i>date à ajouter</i>]
		--end
	
		-- "de", circonscription, diocèse, assemblée délibérante, parti, employeur
		local qualifs = wikidata.getFormattedQualifiers(statement, {'P642', 'P768', 'P708', 'P194', 'P102', 'P108'})
		if qualifs then
			mainstr = mainstr .. ' (' .. qualifs .. ')' 
		end
		
		-- prédecesseur et successeur
		local predecessor = wikidata.getFormattedQualifiers(statement, {'P155', 'P1365'})
		local successor = wikidata.getFormattedQualifiers(statement, {'P156', 'P1366'})
		if predecessor then
			if gender == 'f' then
				mainstr = mainstr .. '<small><br />&nbsp;précédée par ' .. predecessor .. '</small>'
			elseif gender == 'm' then
				mainstr = mainstr .. '<small><br />&nbsp;précédé par ' .. predecessor .. '</small>'
			end
		end
		if successor then
			if gender == 'f' then
				mainstr = mainstr .. '<small><br />&nbsp;suivie par ' .. successor .. '</small>'
			elseif gender == 'm' then
				mainstr = mainstr .. '<small><br />&nbsp;suivi par ' .. successor .. '</small>'
			end
		end	

		-- à la dernière ligne : ajout rétrolien et catégorie de maintenance
		
		if (i == #statements) and prop then
			mainstr = wikidata.formatAndCat{value = mainstr, property = prop, entity = entity}
		end
		
		local row = {type = 'row', label = period or '', value = function() return mainstr end}
		table.insert(rows, row)
	end
	-- ajout de rétrolien, cat
	return rows
end

local function dateandplace(thedate, theplace)
	if thedate and theplace then
		return thedate .. '<br />' .. theplace
	else
		return thedate or theplace --retourne tout ce qu'il trouve
	end
end


--Titre
function p.title(icon, style)
	return general.title(icon, style)
end

--Image
function p.mainimage()
	
	 -- demande d'illustration que si la personne a moins de 100 ans, sinon c'est souvent impossible à trouver
	local age = wikidata.getClaims{property = 'P569'}
	local defaultimage
	if age and (age[1].mainsnak.snaktype == 'value') then -- un peu long, devrait être assuré par Module:Wikidata
		local pattern = "(%W)(%d+)%-(%d+)%-(%d+)"
		local era, year = age[1].mainsnak.datavalue.value.time:match(pattern)
		if (era == '+') and (tonumber(year)) > 1900 then
			defaultimage = 'Defaut 2.svg'
		end
	end
	return  general.mainimage('Article à illustrer/Biographie', defaultimage)
end

-- Noms
function p.othernames()
	
	-- si "autres noms" existe, le renvoie, sinon, fait plusieurs lignes en utilisant diverses propriétés ed Wikidata
	if localdata['autres noms'] or localdata['nom de naissance'] then
		return {type = 'multi', rows = {
			{type = 'row', label = 'Autres noms', value = 'autres noms'},
			{type = 'row', label ='Nom de naissance', value = 'nom de naissance'},
			}
		}
	end
	local names = {
		{'P1477', 'Nom de naissance', 'Noms de naissance', 'nom de naissance'},
		{'P1449', 'Surnom', 'Surnoms', 'surnom'},
		{'P742', 'Pseudonyme', 'Pseudonymes', 'pseudonyme',},
		{'P1782', 'Prénom social', 'Prénoms sociaux', 'prénom social'},
		{'P1786', 'Nom posthume', 'Noms posthumes', 'nom posthume'},
		{'P1785', 'Nom de temple', 'Noms de temple', 'nom de temple'},
		{'P1787', 'Nom de pinceau', 'Noms de pinceau', 'nom de pinceau'},
	}

	local rows = {type = 'multi', rows = {}}
	for i, j in pairs(names) do
		local query = {property = j[1], showqualifiers = {'P1721'}, conjtype = ',<br />'}
		table.insert(rows.rows, {type = 'row', value = j[4], wikidata = query, label = j[2], plurallabel = j[3]})
	end
	return rows
end

-- NAISSANCE ET MORT
local function wikidatadate(prop)
	local vals = wikidata.getClaims{entity = item, property = prop, excludespecial = true}
	if not vals then
		return nil
	end
	local newvals = {}
	for i, val in pairs(vals) do
		local v = wikidata.statementDate(val, {linktopic = linktopic}) -- essaye les qualifs date de début/fin pour une date plus précise (en attendant un bon fonctionnement du "before/after" intégré)
		if not v then
			if val.mainsnak.snaktype ~= 'value' then
				v = wikidata.formatStatement(val, {linktopic = linktopic})
			elseif val.mainsnak.datavalue.value.precision > 7 then
				v = wikidata.formatStatement(val, {linktopic = linktopic})
			end
		end
		if val.qualifiers and val.qualifiers.P1480 then
			local q = wikidata.getid( val.qualifiers.P1480[1])
			if q == 'Q5727902' then
				v = 'vers ' .. v
			end
		end
		if v then
			table.insert(newvals, v)
		end
	end
	local str = linguistic.conj(newvals, 'or')
	if str then
		return wikidata.formatAndCat{entity = item, property = prop, value = str}
	end
end


local birthdate = localdata['naissance'] or localdata['date de naissance'] or wikidatadate('P569') or ''
local deathdate = localdata['décès'] or localdata['date de décès'] or wikidatadate('P570') or ''


function p.birth() -- date de naissance en première ligne, lieu de naissance en deuxième
	return {
		type = 'row',
		label = 'Naissance',
		value =
			function()
			local thedate = datemodule.dateInfobox{args = {[1] = 'naissance', [2] = birthdate, [3] = deathdate, qualificatif = linktopic}}
			local theplace = localdata['lieu de naissance'] or wikidata.formatAndCat({entity =item, property= 'P19', rank = 'best', conjtype= ' ou '})
			return dateandplace(thedate, theplace)
			end
	}
end

function p.death() -- même fonctionnement que la fonction p.birth
	return {
		type = 'row',
		label = 'Décès',
		value = 
			function()
			local thedate = datemodule.dateInfobox{args = {[1] = 'décès', [3] = birthdate, [2] = deathdate, qualificatif = linktopic}}
			local theplace = localdata['lieu de décès'] or wikidata.formatAndCat({entity = item, property= 'P20', rank = 'best', conjtype= ' ou '})
			return dateandplace(thedate, theplace)
			end
	}
end

function p.floruit()
	return {
		type = 'row',
		label = 'Période d’activité',
		value = "Période d'activité",
		wikidata = function() return wikidatadate('P1317')	end
	}
end

function p.nationality() -- Wikidata désactivé avant 1800 (problème de fiabilité des données
	return {
		type = 'row',
		label = 'Nationalité',
		plurallabel = 'Nationalités',
		value = 'nationalité',
		wikidata = 
			function()
				local nation = require "Module:Country data".nationality
				local statements = wikidata.getClaims{entity = item, property = 'P27'}
				if not statements then
					return nil
				end
				return wikidata.formatAndCat{
					property = 'P27',
					vals = statements,
					showdate = true,
					entity = item,
					conjtype = 'comma',
					linktopic = '-',
					displayformat = 
						function(snak)
							local val, success = nation(wikidata.getid(snak), 'f')
							if not success then
								val = wikidata.formatSnak(snak)
							end
							return val
						end
				}, #statements
			end
	}
end

-- parcours professionel
function p.education() -- à améliorer
	local query = {sorttype= 'chronological', property = 'P69', showdate = true, showqualifiers = 'P512', conjtype = '<br />'}
	return {
		type = 'row',
		value = {'éducation', 'formation'},
		wikidata = query,
		label = 'Formation',
	}
end

function p.occupation()
	return {
		type = 'row', 
		value = 'activité',
		wikidata = {property = 'P106', showdate = true, conjtype = 'comma', speciallabels = genderedlabels, defaultlink = '-'}, 
		label = 'Activité',
		plurallabel = 'Activités'
	}
end

function p.employer()
	return {
		type = 'row', 
		value = 'employeur',
		wikidata = {property = 'P108', showdate = true}, 
		label = 'Employeur',
		plurallabel = 'Employeurs'
	}
end

function p.officialposition()
	local title = 'Fonctions'
	local singtitle = 'Fonction'
	
	local rows
	if localdata['fonction'] then
		return {type = 'table', rows = {type = 'row',  label = title, value = 'fonction'}}
	else
		local statements = wikidata.getClaims{property = 'P39', entity = item, sorttype = 'chronological'}
		if not statements then
			return nil
		end
		if #statements == 1 then
			title = singtitle
		end
		rows = timeline(statements, 'P39')
	end
	return
	{
		type = 'table',
		title = title,
		rows = rows,
	}	

end

function p.politicalparty()
	return {
		type = 'row', 
		value = 'parti politique',
		label = 'Parti politique',
		plurallabel = 'Partis politiques',
		wikidata = { property = 'P102', sorttype= 'chronological', showdate = true, conjtype = '<br />', excludespecial = true}, 
	}
end

function p.awards() -- à améliorer
	return {
		type = 'row', 
		value = {'prix', 'récompenses', 'distinction', 'distinctions'},
		wikidata = { property= 'P166', showqualifiers = 'P642', sorttype= 'chronological' , showdate= true, conjtype= '<br />', precision = 'year', textformat = 'minimum', linktopic = '-'}, 
		label = 'Distinction',
		plurallabel = 'Distinctions',
	}
end

-- Influences 
function p.influencedby()
	return {
		type = 'row',
		label = 
		function(localdata, item) 
		if not gender then setgender(localdata, item) end
		if gender == 'f' then
			return 'Influencée par'
		elseif gender == 'm' then
			return 'Influencé par'
		else
			return 'Influencé(e) par'
		end
		end,
		value = 'influencé par',
		wikidata = {property = 'P737'},
    }
end

function p.influenced()
	return {
		type = 'row',
		label = 'A influencé',
		value = {'a influencé', 'influence de'},
		wikidata = {property = 'P738'},
}
end

-- Sport
function p.sport()
	return {type = 'multi', rows = {
{
		type = 'row',
		label = 'Sport',
		plurallabel = 'Sports',
		value = 'sport',
		wikidata = {property = 'P641', showdate = true},
},
{
		type = 'row',
		label = 'Équipe',
		plurallabel = 'Équipes',
		value = 'équipe',
		wikidata = {property = 'P54', sorttype= 'chronological', showdate = true, conjtype = '<br />'},
},
{
		type = 'row',
		label = 'Sponsor',
		plurallabel = 'Sponsors',
		value = 'sponsor',
		wikidata = {property = 'P859', sorttype= 'chronological', showdate = true},
},
{
		type = 'row',
		label = 'Domicile',
		plurallabel = 'Domiciles',
		value = 'domicile',
		wikidata = {property = 'P551', showdate = true, sorttype= 'chronological'},
},
	}}
end

-- Famille
function p.family()
	return {type = 'multi', rows = {
{
		type = 'row',
		label = 'Père', 
		value = 'père',
		property = 'P22',
},
{
		type = 'row',
		label = 'Mère',
		value = 'mère',
		property = 'P25',
},
{
		type = 'row',
		label = 'Frère', 
		plurallabel = 'Frères',
		value = 'Frère',
		property = 'P7',
},
{
		type = 'row',
		label = 'Sœur',
		plurallabel = 'Sœurs',
		value = 'Sœur',
		property = 'P9',
},
{
		type = 'row',
		label = function()
			-- retourne une forme singulière ou plurielle. Pourrait éventuellement adapter féminin masculine, mais demanderait logiquement de vérifier le sexe de chaque valeur
			local claims = wikidata.getClaims{property = 'P26', entity = item}
			if (not claims) or #claims == 1 then
				return "Conjoint"
			end
			return "Conjoints"
			end,
		value = 'conjoint',
		wikidata = function() 
			local claims = wikidata.getClaims{entity = item, property = 'P26', sorttype = 'chronological'}
			if not claims then
				return nil
			end
			if #claims > 4 then -- quand il y a trop de conjoints, on ne va pas à la ligne à chaque fois
				return wikidata.formatStatements{entity = item, property = 'P26', showdate = true, textformat = 'minimum', precision = 'year', linktopic = '-'}	
			end
			-- sinon si (textformat = 'long', showdate = true, precision = 'year', linktopic = '-')
			return wikidata.formatStatements{entity = item, property = 'P26', showdate = true, textformat = 'long', precision = 'year', linktopic = '-', conjtype = '<br />'}	
			end
},
{
		type = 'row',
		label = 'Enfant',
		plurallabel = 'Enfants',
		value = 'enfant',
		wikidata = {property = 'P40'}
},
	}}
end

-- oeuvres
function p.works()
	local title = 'Œuvres réputées'
	-- on pourrait mettre quelque chose comme "oeuvre principale" lorsque #vals == 1 mais les données de Wikidata sont trop approximatives pour que cela ait du sens
	return 
	{type = 'table', title = title, rows =
		{
			{
			type = 'row',
			value = 'œuvres principales',
			wikidata = {
				property = 'P800',
				numval = 5,
				excludespecial = true,
				displayformat =
					function(snak)
						return '<i>' .. wikidata.formatEntity(wikidata.getid(snak), {defaultlink = 'image'}) .. '</i>'
					end
			}
			}
		}
	}
end

-- Signature
function p.signature(default)
	return {
		type = 'images',
		imageparameters =  {'signature'},
		defaultimage = default,
		defaultsize = '150px',
		captionparameter = 'légende signature',
		defaultcaption = 'signature',
		property = 'P109',
		numval = 1
	}
end

--== Site web
function p.website()
	return general.website()
end

return p