Modiwl:Entity formatting
Documentation for this module may be created at Modiwl:Entity formatting/doc
local p = {}
local defaultlang = mw.getContentLanguage():getCode()
local tools = require 'Module:Wikidata/Tools'
-- local getClaims = require "Module:Wikidata/Recovery".getClaims -- Loaded only if necessary
local i18n = tools.i18n
local formatError = tools.formatError
function p.getLabel(entity, lang, labelformat)
if (not entity) then
return nil -- Or error handling option ?
end
lang = lang or defaultlang
if type(labelformat) == 'function' then
return labelformat(entity)
end
if (type(entity) == 'string') and (lang == defaultlang) then -- The most economical
local str = mw.wikibase.label(entity)
if str then -- mw.wikibase.label() does not work with redirects 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 = '' -- The entire text to return
label = p.getLabel(entity, lang, labelformat)
-- Determining whether or not the element is being rendered on the page of its 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'])
-- If not worded, we put a link to Wikidata so that we understand what it refers to
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
-- We are not on the article of the entity, and we have no site link
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 -- The displayed text of the link
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> (' .. 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