« Module:Wikidata/Formatage entité » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
plus pratique semble-t-il
0x010D (discussion | contributions)
ajouts ésotériques
Ligne 1 : Ligne 1 :
local p = {}
local p = {}
local defaultlang = 'fr'
local defaultlang = mw.getContentLanguage():getCode()
local tools = require 'Module:Wikidata/Outils'
local tools = require 'Module:Wikidata/Outils'
local getClaims = require "Module:Wikidata/Récup".getClaims
local i18n = tools.i18n
local i18n = tools.i18n


local formatError = tools.formatError
local formatError = tools.formatError


local function wikipediaLink(entity, lang)
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)
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)
function p.getLabel(entity, lang, site, displayformat)
Ligne 98 : Ligne 44 :
id = id.id
id = id.id
end
end


params = params or {}
params = params or {}
Ligne 106 : Ligne 51 :
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 'enwiki'
local defaultlinktype = params.defaultlink
local defaultlinkquery = params.defaultlinkquery
local showotherlink = not(parenthesislink)
local showotherlink = not(parenthesislink)


Ligne 130 : Ligne 76 :
end
end
str = str .. tools.addcat(i18n['to translate'])
str = str .. tools.addcat(i18n['to translate'])
link = p.getLink(id, 'wikidata')
link = tools.siteLink(id, 'wikidata')
return str .. '[[' .. link .. '|' .. id .. ']]'-- si pas de libellé, on met un lien vers Wikidata pour qu'on comprenne à quoi ça fait référence
return str .. '[[' .. link .. '|' .. id .. ']]'-- si pas de libellé, on met un lien vers Wikidata pour qu'on comprenne à quoi ça fait référence
end
end


if (linktype == '-') then
if (linktype == '-') then
return str .. label
return str .. label
end
local link = tools.siteLink(entity, linktype, lang)
if (not link) and defaultlinkquery then
defaultlinkquery.excludespecial = true
if not defaultlinkquery.entity then
defaultlinkquery.entity = entity
end
local items = getClaims(defaultlinkquery)
if items then
local id = tools.getMainId(items[1])
link = tools.siteLink(id, linktype, lang)
end
end
end
local link = p.getLink(entity, linktype, lang)
if link then
if link then
return str .. '[[' .. link .. '|' .. label .. ']]'
return str .. '[[' .. link .. '|' .. label .. ']]'
Ligne 146 : Ligne 102 :
if (defaultlinktype ~= '-') then
if (defaultlinktype ~= '-') then
local rightlink = p.getLink(entity, defaultlinktype, lang) or p.getLink(entity, 'wikidata', lang)
local linktype
if rightlink then
local sidelink, site, langcode
local prefix = mw.text.split(rightlink, ':')[2] or 'voir ailleurs'
if defaultlinktype then
return str .. (label or '?') .. '&nbsp;<small>([[' .. rightlink .. '|' .. prefix .. ']])</small>'
sidelink, site, langcode = tools.siteLink(entity, linktype, lang)
else
sidelink, site, langcode = tools.siteLink(entity, 'wiki', 'en')
end
end
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-project', langcode)
elseif site == 'wikidata' then
icon, title = 'd', tools.translate('see-another-project', site)
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
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 du 25 septembre 2015 à 12:09

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

local formatError = tools.formatError


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

lang = lang or defaultlang site = site or (lang .. 'wiki')

if (site == '-') then return nil end

if (type(entity) == 'string') and (lang == defaultlang) then -- le plus économique return mw.wikibase.label(entity) end

if type(entity) == 'string' then entity = mw.wikibase.getEntityObject(entity) end if not args then args = {} end if 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 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 lang local speciallabels = params.speciallabels local displayformat = params.displayformat local defaultlabel = params.defaultlabel or id local linktype = params.link local defaultlinktype = params.defaultlink local defaultlinkquery = params.defaultlinkquery local showotherlink = not(parenthesislink)


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

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

local link = tools.siteLink(entity, linktype, lang) if (not link) and defaultlinkquery then defaultlinkquery.excludespecial = true if not defaultlinkquery.entity then defaultlinkquery.entity = entity end local items = getClaims(defaultlinkquery) if items then local id = tools.getMainId(items[1]) link = tools.siteLink(id, linktype, lang) end end

if link then return str .. '' .. label .. '' end

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-project', langcode) elseif site == 'wikidata' then icon, title = 'd', tools.translate('see-another-project', site) else title = tools.translate('see-another-project', site) end local val = '' .. '' .. icon .. '' return str .. label .. ' (' .. val .. ')' 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