« Module:Wikidata/Formatage entité » : différence entre les versions
Aucun résumé des modifications |
Aucun résumé des modifications |
||
| Ligne 16 : | Ligne 16 : | ||
end | end | ||
if type(entity) == 'string' then | if type(entity) == 'string' then | ||
entity = | entity = mw.wikibase.getEntityObject(entity) | ||
end | end | ||
if (not entity) or type(entity) ~= 'table' then | if (not entity) or type(entity) ~= 'table' then | ||
| Ligne 36 : | Ligne 36 : | ||
end | end | ||
end | end | ||
function p.getLink(entity, linktype, lang) | function p.getLink(entity, linktype, lang) | ||
| Ligne 50 : | Ligne 49 : | ||
elseif linktype == 'image' then | elseif linktype == 'image' then | ||
return imageLink(entity) | 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 | end | ||
local errormsg = i18n['invalid-linktype'] | local errormsg = i18n['invalid-linktype'] | ||
| Ligne 77 : | Ligne 83 : | ||
if type(entity) == 'string' then | if type(entity) == 'string' then | ||
entity = | entity = mw.wikibase.getEntityObject(entity) | ||
end | end | ||
if not args then | if not args then | ||
| Ligne 104 : | Ligne 110 : | ||
local defaultlabel = params.defaultlabel or id-- usecase : titres d'infobox : s'il n'y a pas de libellé, afficher le titre de la page | local defaultlabel = params.defaultlabel or id-- usecase : titres d'infobox : s'il n'y a pas de libellé, afficher le titre de la page | ||
local linktype = params.link | local linktype = params.link | ||
local defaultlinktype = params.defaultlink | local defaultlinktype = params.defaultlink or 'wikidata' | ||
local showotherlink = not(parenthesislink) | local showotherlink = not(parenthesislink) | ||
| Ligne 134 : | Ligne 140 : | ||
local link = p.getLink(entity, linktype, lang) | local link = p.getLink(entity, linktype, lang) | ||
if link then | if link then | ||
return str .. '[[' .. link .. '|' .. label .. ']]' | return str .. '[[' .. link .. '|' .. label .. ']]' | ||
end | end | ||
if | |||
if (defaultlinktype ~= '-') then | |||
local rightlink | |||
if linktype then | |||
rightlink = p.getLink(entity, defaultlinktype, lang) | |||
end | |||
if not rightlink then | |||
rightlink = p.getLink(entity, 'wikidata', lang) | |||
end | |||
if rightlink then | if rightlink then | ||
return str .. label .. ' <small>([[' .. rightlink .. '| | local prefix = mw.text.split(rightlink, ':')[2] | ||
return str .. label .. ' <small>([[' .. rightlink .. '|' .. prefix ']])</small>' | |||
end | end | ||
return str .. label .. ' <small>([[' .. rightlink .. '|d]])</small>' | return str .. label .. ' <small>([[' .. rightlink .. '|d]])</small>' | ||
end | end | ||
Version du 26 août 2015 à 17:03
local p = {} local defaultlang = 'fr'
local function imageLink(entity) local val = p.formatStatements{property = 'P18', entity = entity, numval = 1} if not val then return nil end return ':File:' .. val end
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) 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 (not entity) then return formatError('entity-not-found') 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-- usecase : titres d'infobox : s'il n'y a pas de libellé, afficher le titre de la page
local linktype = params.link
local defaultlinktype = params.defaultlink or 'wikidata'
local showotherlink = not(parenthesislink)
if speciallabels and speciallabels[id] then --speciallabels override the standard label + link combination
return speciallabels[id]
end
if args.displayformat == 'raw' then
return id
end
local link, label local str = -- l'intégralité du text à retourner
label = p.getLabel(entity, lang, displayformat) if (not label) then if (defaultlabel == '-') then return nil end str = str .. addcat(i18n['to translate']) link = p.getLink(id, 'wikidata') -- si pas de libellé, lier vers Wikidata pour avoir quelque chose de lisible return str .. '' .. id .. '' end
if (linktype == '-') then
return str
end
local link = p.getLink(entity, linktype, lang)
if link then return str .. '' .. label .. '' end
if (defaultlinktype ~= '-') then local rightlink if linktype then rightlink = p.getLink(entity, defaultlinktype, lang) end if not rightlink then rightlink = p.getLink(entity, 'wikidata', lang) end if rightlink then local prefix = mw.text.split(rightlink, ':')[2] return str .. label .. ' (' .. prefix ')' end
return str .. label .. ' (d)' end return str .. label end
return p