Module:Wikidata/Formatage entité : Différence entre versions

De Lagny-sur-Marne Wiki
Aller à : navigation, rechercher
m (A changé le niveau de protection pour « Module:Wikidata/Formatage entité » ([Modifier=Autoriser uniquement les utilisateurs autopatrolled] (infini) [Renommer=Autoriser uniquement les administrateurs] (infini)))
 
(34 révisions intermédiaires par 4 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
 
local p = {}
 
local p = {}
local defaultlang = 'fr'
+
local defaultlang = mw.getContentLanguage():getCode()
local i18n = mw.loadData "Module:Wikidata/I18n"
+
local tools = require 'Module:Wikidata/Outils'
 +
-- local getClaims = require "Module:Wikidata/Récup".getClaims -- chargé uniquement si nécessaire
 +
local i18n = tools.i18n
  
local function addcat(cat, sortkey)
+
local formatError = tools.formatError
if sortkey then
 
return  '[[Category:' .. cat .. '|' .. (sortkey or '') .. ']]'
 
end
 
return '[[Category:' .. cat  .. ']]'
 
end
 
  
  
local function imageLink(entity)
+
function p.getLabel(entity, lang, labelformat)
local val = p.formatStatements{property = 'P18', entity = entity, numval = 1}
+
if (not entity) then
if not val then
+
return nil -- ou option de gestion des erreurs ?
return nil
 
 
end
 
end
return ':File:' .. val
 
end
 
  
local function wikipediaLink(entity, lang)
 
 
lang = lang or defaultlang
 
lang = lang or defaultlang
if (type(entity) == 'string') and (lang == defaultlang) then -- le plus économique
 
return mw.wikibase.sitelink(entity)
 
end
 
if type(entity) == 'string' then
 
entity = mw.wikibase.getEntityObject(entity)
 
end
 
if (not entity) or type(entity) ~= 'table' then
 
return formatError('entity-not-found')
 
end
 
local link = entity:getSitelink(lang .. 'wiki')
 
if link then
 
return ':' .. lang .. ':' .. link
 
end
 
end
 
 
local function wikidataLink(entity)
 
if type(entity) == 'string' then
 
return ':d:' .. entity
 
elseif type(entity) == 'table' then
 
return ':d:' .. entity.id
 
elseif type(entity) == nil then
 
return formatError('entity-not-found')
 
end
 
end
 
 
function p.getLink(entity, linktype, lang)
 
lang = lang or defaultlang
 
linktype = linktype or 'wikipedia'
 
if linktype == '-' then
 
return nil
 
end
 
if linktype == 'wikipedia' then
 
return wikipediaLink(entity, lang)
 
elseif linktype == 'wikidata' then
 
return wikidataLink(entity)
 
elseif linktype == 'image' then
 
return imageLink(entity)
 
end
 
 
-- analyse les valeurs du type 'enwiki' pour linktype = 'wikipedia', lang = 'en'
 
local endstr = string.sub(linktype, #linktype - 3, #linktype)
 
if endstr == 'wiki' then
 
lang = string.sub(linktype, 1, #linktype - 4)
 
return wikipediaLink(entity, lang)
 
end
 
local errormsg = i18n['invalid-linktype']
 
if type(linktype) ~= 'string' then
 
errormsg = errormsg .. ': ' .. linktype
 
else
 
errormsg = errormsg .. ':' .. type(linktype)
 
end
 
return formatError(errormsg)
 
end
 
  
function p.getLabel(entity, lang, site, displayformat)
+
if type(labelformat) == 'function' then
if (not entity) then
+
return labelformat(entity)
return formatError('entity-not-found')
 
end
 
 
 
lang = lang or defaultlang
 
site = site or (lang .. 'wiki')
 
 
if (site == '-') then
 
return nil
 
 
end
 
end
 
 
 
if (type(entity) == 'string') and (lang == defaultlang) then -- le plus économique
 
if (type(entity) == 'string') and (lang == defaultlang) then -- le plus économique
return mw.wikibase.label(entity)
+
local str = mw.wikibase.label(entity)
 +
if str then -- mw.wikibase.label() ne fonctionne pas avec les redirect https://phabricator.wikimedia.org/T157868
 +
return str
 +
end
 
end
 
end
  
Ligne 94 : Ligne 29 :
 
entity = mw.wikibase.getEntityObject(entity)
 
entity = mw.wikibase.getEntityObject(entity)
 
end
 
end
if not args then
+
args = {}
+
if entity and entity.labels and entity.labels[lang] then
end
 
if entity.labels and entity.labels[lang] then
 
 
return entity.labels[lang].value, true
 
return entity.labels[lang].value, true
 
end
 
end
Ligne 105 : Ligne 38 :
  
 
if (not entity) then
 
if (not entity) then
return formatError('entity-not-found')
+
return nil --formatError('entity-not-found')
 
end
 
end
 
local id = entity
 
local id = entity
Ligne 111 : Ligne 44 :
 
id = id.id
 
id = id.id
 
end
 
end
 
  
 
params = params or {}
 
params = params or {}
local lang = params.lang or lang
+
local lang = params.lang or defaultlang
 
local speciallabels = params.speciallabels
 
local speciallabels = params.speciallabels
 
local displayformat = params.displayformat
 
local displayformat = params.displayformat
 +
local labelformat = params.labelformat
 
local defaultlabel = params.defaultlabel or id
 
local defaultlabel = params.defaultlabel or id
 
local linktype = params.link
 
local linktype = params.link
local defaultlinktype = params.defaultlink or 'wikidata'
+
local defaultlinktype = params.defaultlink
local showotherlink = not(parenthesislink)
+
local defaultlinkquery = params.defaultlinkquery
 
 
  
 
if speciallabels and speciallabels[id] then --speciallabels override the standard label + link combination
 
if speciallabels and speciallabels[id] then --speciallabels override the standard label + link combination
Ligne 133 : Ligne 65 :
 
local str = '' -- l'intégralité du text à retourner
 
local str = '' -- l'intégralité du text à retourner
 
 
label = p.getLabel(entity, lang, displayformat)
+
if (not label) then
+
label = p.getLabel(entity, lang, labelformat)
 +
 +
-- détermination du fait qu'on soit ou non en train de rendre l'élément sur la page de son article
 +
local rendering_entity_on_its_page = tools.is_page_of_qid(id)
 +
 +
 
 +
if not label then
 
if (defaultlabel == '-') then  
 
if (defaultlabel == '-') then  
 
return nil
 
return nil
 
end
 
end
str = str .. addcat(i18n['to translate'])
+
link = tools.siteLink(id, 'wikidata')
link = p.getLink(id, 'wikidata') -- si pas de libellé, lier vers Wikidata pour avoir quelque chose de lisible
+
return str .. '[[' .. link .. '|' .. id .. ']]' .. tools.addcat(i18n['to translate'])
+
-- si pas de libellé, on met un lien vers Wikidata pour qu'on comprenne à quoi ça fait référence
return str .. '[[' .. link .. '|' .. defaultlabel .. ']]'
+
end
 +
 
 +
if (linktype == '-') or rendering_entity_on_its_page then
 +
return str .. label
 
end
 
end
  
 +
local link = tools.siteLink(entity, linktype, lang)
 
 
if (linktype == '-') then
+
 
return str
+
if (not link) and defaultlinkquery then
 +
defaultlinkquery.excludespecial = true
 +
defaultlinkquery.entity = entity
 +
local getClaims = require("Module:Wikidata/Récup").getClaims
 +
local claims = getClaims(defaultlinkquery)
 +
if claims then
 +
for i, j in pairs(claims) do
 +
local id = tools.getMainId(j)
 +
link = tools.siteLink(id, linktype, lang)
 +
if link then
 +
break
 +
end
 +
end
 +
end
 
end
 
end
 
local link = p.getLink(entity, linktype, lang)
 
  
if link then
+
if link then  
 
return str .. '[[' .. link .. '|' .. label .. ']]'
 
return str .. '[[' .. link .. '|' .. label .. ']]'
 
end
 
end
 +
 +
-- on est pas sur l'article de l'entité, et on a pas de lien de site
 
 
 
if (defaultlinktype ~= '-') then
 
if (defaultlinktype ~= '-') then
local rightlink
+
local linktype
if linktype then
+
local sidelink, site, langcode
rightlink = p.getLink(entity, defaultlinktype, lang)
+
if defaultlinktype then
 +
sidelink, site, langcode = tools.siteLink(entity, linktype, lang)
 +
else
 +
sidelink, site, langcode = tools.siteLink(entity, 'wiki', 'en')
 
end
 
end
if not rightlink then
+
if not sidelink then
rightlink = p.getLink(entity, 'wikidata', lang)
+
sidelink, site = tools.siteLink(entity, 'wikidata')
 
end
 
end
if rightlink then
+
local prefix = mw.text.split(rightlink, ':')[2]
+
local icon, class, title = site, nil, nil -- le texte affiché du lien
return str .. label .. ' <small>([[' .. rightlink .. '|' .. prefix .. ']])</small>'
+
if site == 'wiki' then
 +
icon, class, title = langcode, "indicateur-langue", tools.translate('see-another-language', mw.language.fetchLanguageName(langcode, defaultlang))
 +
elseif site == 'wikidata' then
 +
icon, class, title = 'd',  "indicateur-langue", tools.translate('see-wikidata')
 +
else
 +
title = tools.translate('see-another-project', site)
 
end
 
end
+
local val = '[[' .. sidelink .. '|' .. '<span class = "' .. (class or '').. '" title = "' .. (title or '') .. '">' .. icon .. '</span>]]'
return str .. label .. ' <small>([[' .. rightlink .. '|d]])</small>'
+
return str .. label .. '<small>&nbsp;(' .. val .. ')</small>'
end
+
end  
 
return str .. label
 
return str .. label
 +
end
 +
 +
-- Fonction dépréciée
 +
function p.getLink(entity, linktype, lang)
 +
return tools.siteLink(entity, linktype, lang)
 
end
 
end
  
 
return p
 
return p

Version actuelle datée du 17 mai 2017 à 13:42

La documentation pour ce module peut être créée à Module:Wikidata/Formatage entité/doc

local p = {}
local defaultlang = mw.getContentLanguage():getCode()
local tools = require 'Module:Wikidata/Outils'
-- local getClaims = require "Module:Wikidata/Récup".getClaims -- chargé uniquement si nécessaire
local i18n = tools.i18n

local formatError = tools.formatError


function p.getLabel(entity, lang, labelformat)
	if (not entity) then
		return nil -- ou option de gestion des erreurs ?
	end

	lang = lang or defaultlang

	if type(labelformat) == 'function' then
		return labelformat(entity)
	end
	
	if (type(entity) == 'string') and (lang == defaultlang) then -- le plus économique
		local str = mw.wikibase.label(entity)
		if str then -- mw.wikibase.label() ne fonctionne pas avec les redirect https://phabricator.wikimedia.org/T157868
			return str
		end
	end

	if type(entity) == 'string' then
		entity = mw.wikibase.getEntityObject(entity)
	end
	
	if entity and entity.labels and entity.labels[lang] then
		return entity.labels[lang].value, true
	end
end

function p.formatEntity( entity, params )

	if (not entity) then
		return nil --formatError('entity-not-found')
	end
	local id = entity
	if type(id) == 'table' then
		id = id.id
	end

	params = params or {}
	local lang = params.lang or defaultlang
	local speciallabels = params.speciallabels
	local displayformat = params.displayformat
	local labelformat = params.labelformat
	local defaultlabel = params.defaultlabel or id
	local linktype = params.link
	local defaultlinktype = params.defaultlink
	local defaultlinkquery = params.defaultlinkquery

	if speciallabels and speciallabels[id] then --speciallabels override the standard label + link combination
		return speciallabels[id]
	end
	if params.displayformat == 'raw' then
		return id
	end

	local link, label
	local str = '' -- l'intégralité du text à retourner	
	
	
	label = p.getLabel(entity, lang, labelformat)
	
	-- détermination du fait qu'on soit ou non en train de rendre l'élément sur la page de son article
	local rendering_entity_on_its_page = tools.is_page_of_qid(id)
		

	if not label then
		if (defaultlabel == '-') then 
			return nil
		end
		link = tools.siteLink(id, 'wikidata')
		return str .. '[[' .. link .. '|' .. id .. ']]' .. tools.addcat(i18n['to translate'])
-- si pas de libellé, on met un lien vers Wikidata pour qu'on comprenne à quoi ça fait référence
	end

	if (linktype == '-') or rendering_entity_on_its_page then
		return str .. label
	end

	local link	= tools.siteLink(entity, linktype, lang)
	

	if (not link) and defaultlinkquery then
		defaultlinkquery.excludespecial = true
		defaultlinkquery.entity = entity
		local getClaims = require("Module:Wikidata/Récup").getClaims
		local claims = getClaims(defaultlinkquery)
		if claims then
			for i, j in pairs(claims) do
				local id = tools.getMainId(j)
				link = tools.siteLink(id, linktype, lang)
				if link then
					break
				end
			end
		end	
	end

	if link then 
		return str .. '[[' .. link .. '|' .. label .. ']]'
	end

	-- on est pas sur l'article de l'entité, et on a pas de lien de site
	
	if (defaultlinktype ~= '-') then
		local linktype
		local sidelink, site, langcode
		if defaultlinktype then
			sidelink, site, langcode = tools.siteLink(entity, linktype, lang)
		else
			sidelink, site, langcode = tools.siteLink(entity, 'wiki', 'en')
		end
		if not sidelink then
			sidelink, site = tools.siteLink(entity, 'wikidata')
		end
		
		local icon, class, title = site, nil, nil -- le texte affiché du lien
		if site == 'wiki' then
			icon, class, title = langcode, "indicateur-langue", tools.translate('see-another-language', mw.language.fetchLanguageName(langcode, defaultlang))	
		elseif site == 'wikidata' then
			icon, class, title = 'd',  "indicateur-langue", tools.translate('see-wikidata')		
		else
			title = tools.translate('see-another-project', site)
		end
		local val = '[[' .. sidelink .. '|' .. '<span class = "' .. (class or '').. '" title = "' .. (title or '') .. '">' .. icon .. '</span>]]'
		return str  .. label .. '<small>&nbsp;(' .. val .. ')</small>'
	end 
	return str .. label
end

-- Fonction dépréciée
function p.getLink(entity, linktype, lang)
	return tools.siteLink(entity, linktype, lang)
end

return p